Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions integrations/blender/plugin/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
isWslDaemon,
loadMethodRegistry,
type OfferRegistry,
offerRegistryName,
registerPluginTools,
requestHost,
type SessionBindings,
Expand Down Expand Up @@ -166,13 +167,13 @@ export function helloParams(_options: BlenderOptions) {
// hellos by: after an upgrade without restarting OpenCode, an older plugin version stays loaded in the same
// process, and a list keyed by the application alone would judge this version's add-on by the older range.
export function registryName(expected: HostExpectation) {
return `${APP}.protocol-${expected.protocol}.part-${expected.range}.app-${expected.appVersions}`
return offerRegistryName(APP, expected)
}

export function createRegistry(marker: IntegrationMarker, options: BlenderOptions): OfferRegistry {
const host = marker.host as NonNullable<IntegrationMarker["host"]>
const expected = { protocol: host.protocol, range: host.range, appVersions: host.appVersions }
return sharedOfferRegistry(registryName(expected), {
return sharedOfferRegistry(APP, {
expected,
hello: (endpoint) => requestHost(endpoint, "hello", helloParams(options), { timeoutMs: HELLO_TIMEOUT_MS }),
})
Expand Down
12 changes: 8 additions & 4 deletions integrations/blender/test/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import assert from "node:assert/strict"
import { after, it } from "node:test"

import { startFakeHost } from "../../../packages/testing/src/index.ts"
import { createRegistry, readPackage, registryName } from "../plugin/src/integration.ts"
import { offerRegistryKey, sharedOfferRegistry } from "../plugin/src/kit.ts"
import { APP, createRegistry, readPackage, registryName } from "../plugin/src/integration.ts"
import { createOfferRegistry, offerRegistryKey, sharedOfferRegistry } from "../plugin/src/kit.ts"
import { readOptions } from "../plugin/src/options.ts"
import { announcement } from "./helpers/plugin.ts"

Expand All @@ -24,7 +24,8 @@ it("a plugin loaded after an older version in the same process judges its own ad
const fake = await startFakeHost({ hostVersion: readPackage().version, appVersion: "5.3.0" })
try {
// What 0.2.0 created when it loaded: the kit's shared list under "blender", with its own expectation.
const older = sharedOfferRegistry("blender", { expected: { protocol: 1, range: "^0.2.0", appVersions: ">=5.2" } })
const older = createOfferRegistry({ expected: { protocol: 1, range: "^0.2.0", appVersions: ">=5.2" } })
store[offerRegistryKey("blender")] = { generation: 2, registry: older }
const registry = createRegistry(marker, readOptions(undefined, {}))
assert.notEqual(registry, older, "this version keeps its own list")
assert.deepEqual(await registry.advertise(announcement(fake.endpoint, fake.scriptHandle)), { outcome: "accepted" })
Expand All @@ -37,6 +38,9 @@ it("a plugin loaded after an older version in the same process judges its own ad
await older.advertise(announcement(fake.endpoint, fake.scriptHandle))
assert.equal(older.hosts().offers[0]?.protocol, "incompatible")
assert.equal(createRegistry(marker, readOptions(undefined, {})), registry, "one list per contract and process")
// The kit names the list as 0.4.8 did: the published Blender versions find theirs.
assert.equal(registryName(expected), `blender.protocol-1.part-${host.range}.app-${host.appVersions}`)
assert.ok(store[offerRegistryKey(registryName(expected))])
} finally {
await fake.close()
}
Expand All @@ -48,7 +52,7 @@ it("an upgrade from 0.4.1 without restarting OpenCode keeps this version's add-o
const published = { protocol: 1, range: "^0.4.1", appVersions: ">=5.3" }
keys.push(offerRegistryKey(registryName(published)))
try {
const older = sharedOfferRegistry(registryName(published), { expected: published })
const older = sharedOfferRegistry(APP, { expected: published })
const registry = createRegistry(marker, readOptions(undefined, {}))
assert.notEqual(registry, older)
await registry.advertise(announcement(fake.endpoint, fake.scriptHandle, "after-upgrade"))
Expand Down
49 changes: 37 additions & 12 deletions integrations/gaffer/host/python/GafferOpenCode/Host.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,40 @@ def _jsonValue( value ) :
return repr( value )


# The error codes of the wire protocol (spec/README.md); a host code outside them travels as data["reason"].
WIRE_CODES = (
"unauthorized", "invalid_request", "method_not_found", "method_disabled", "invalid_args", "stale_handle",
"stale_revision", "expectation_failed", "mode_conflict", "busy", "quarantined", "permission_denied", "internal",
)
WIRE_ALIASES = {
"unknown_script" : "stale_handle", "revision_conflict" : "stale_revision",
"unknown_method" : "method_not_found", "script_quarantined" : "quarantined",
"rollback_failed" : "quarantined", "request_too_large" : "invalid_request",
"read_only" : "permission_denied",
}
WIRE_MESSAGE_MAX = 2000
WIRE_DATA_MAX = 64 * 1024

def wireError( error ) :

"""The refusal sent to the plugin: its wire code, the host's own message and data, and the host's code."""

code = WIRE_ALIASES.get( error.code, error.code )
if code.startswith( ( "invalid_", "unsupported_" ) ) and code != "invalid_request" :
code = "invalid_args"
if code not in WIRE_CODES :
code = "internal"
data = _jsonValue( dict( error.data or {} ) )
if code != error.code :
data["reason"] = error.code
if len( json.dumps( data, allow_nan = False ) ) > WIRE_DATA_MAX :
data = { "reason" : error.code, "truncated" : True }
refusal = { "code" : code, "message" : str( error )[:WIRE_MESSAGE_MAX] }
if data :
refusal["data"] = data
return refusal


def _valuesEqual( observed, expected ) :

if isinstance( observed, ( int, float ) ) and not isinstance( observed, bool ) and isinstance( expected, ( int, float ) ) and not isinstance( expected, bool ) :
Expand Down Expand Up @@ -1754,19 +1788,10 @@ def __handleClient( self, client ) :
result = self.host.dispatch( str( request.get( "method", "" ) ), request.get( "params" ) or {} )
response = { "id" : requestID, "ok" : True, "result" : result }
except HostError as error :
code = {
"unknown_script" : "stale_handle", "revision_conflict" : "stale_revision",
"unknown_method" : "method_not_found", "script_quarantined" : "quarantined",
"rollback_failed" : "quarantined", "request_too_large" : "invalid_request",
"read_only" : "permission_denied",
}.get( error.code, error.code )
if code.startswith( ( "invalid_", "unsupported_" ) ) and code != "invalid_request" :
code = "invalid_args"
if code not in ( "unauthorized", "invalid_request", "method_not_found", "method_disabled", "invalid_args", "stale_handle", "stale_revision", "expectation_failed", "mode_conflict", "busy", "quarantined", "permission_denied", "internal" ) :
code = "internal"
response = { "id" : requestID, "ok" : False, "error" : { "code" : code, "message" : str( error )[:300] } }
response = { "id" : requestID, "ok" : False, "error" : wireError( error ) }
except Exception as error :
response = { "id" : requestID, "ok" : False, "error" : { "code" : "internal", "message" : str( error )[:300] } }
message = "{}: {}".format( type( error ).__name__, error )[:WIRE_MESSAGE_MAX]
response = { "id" : requestID, "ok" : False, "error" : { "code" : "internal", "message" : message } }

try :
client.sendall( ( json.dumps( response, allow_nan = False ) + "\n" ).encode( "utf-8" ) )
Expand Down
Binary file modified integrations/gaffer/plugin/host/gaffer/opencode_link-0.2.0.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion integrations/gaffer/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"range": "^0.2.0",
"appVersions": ">=1.6",
"payload": "host/gaffer/opencode_link-0.2.0.zip",
"sha256": "d11773ecfd29d6608ee84293be76b7145713264f4b72c8308b33272bc2b37b53"
"sha256": "4db41db5c28953af8d56266e9f49b62d8ebf2ded7136e6b9051cf4b72ed2bf29"
}
}
}
22 changes: 13 additions & 9 deletions integrations/gaffer/plugin/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ export function readPackage(): { version: string; opencodeIntegration: Integrati
return JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8"))
}

// The offer list shared by the daemon's Locations, named after this version's host contract (the kit's
// sharedOfferRegistry): after an upgrade without restarting OpenCode, the new host is judged by this range.
export function createRegistry(marker: IntegrationMarker): OfferRegistry {
const host = marker.host as NonNullable<IntegrationMarker["host"]>
return sharedOfferRegistry("gaffer", {
expected: { protocol: host.protocol, range: host.range, appVersions: host.appVersions },
hello: (endpoint, scriptHandle) =>
requestHost(endpoint, "hello", { script_handle: scriptHandle }, { timeoutMs: HELLO_TIMEOUT_MS }),
})
}

export function resultContent(result: Record<string, unknown>) {
const clean = JSON.parse(JSON.stringify(result))
const jobs = [clean.job, ...(clean.jobs ?? [])].filter(Boolean) as Array<{
Expand Down Expand Up @@ -47,15 +58,8 @@ export function resultContent(result: Record<string, unknown>) {
export async function setupGaffer(ctx: Plugin.Context, deps: { registry?: OfferRegistry; wsl?: () => boolean } = {}) {
const pkg = readPackage()
const marker = pkg.opencodeIntegration
const host = marker.host
if (!host) throw new Error("Gaffer requires a native host marker.")
const registry =
deps.registry ??
sharedOfferRegistry("gaffer", {
expected: { protocol: host.protocol, range: host.range, appVersions: host.appVersions },
hello: (endpoint, scriptHandle) =>
requestHost(endpoint, "hello", { script_handle: scriptHandle }, { timeoutMs: HELLO_TIMEOUT_MS }),
})
if (!marker.host) throw new Error("Gaffer requires a native host marker.")
const registry = deps.registry ?? createRegistry(marker)
const rpc = createIntegrationRpc({
marker,
version: pkg.version,
Expand Down
11 changes: 10 additions & 1 deletion integrations/gaffer/test/conformance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from "node:path"
import { createInterface } from "node:readline"
import { it } from "node:test"
import { checkCodeExecutionGate, checkHostListener, runSentinelScenario } from "../../../packages/testing/src/index.ts"
import { requestHost } from "../plugin/src/kit.ts"
import { type HostError, requestHost } from "../plugin/src/kit.ts"
import { loadPlugin } from "./helpers.ts"

it("real Gaffer HostServer: loopback-only, 256-bit token, constant-time authentication and valid hello", async () => {
Expand Down Expand Up @@ -50,6 +50,15 @@ it("real Gaffer HostServer: loopback-only, 256-bit token, constant-time authenti
}),
})
assert.deepEqual(errors, [])
// A refusal keeps the host's message, its data (made JSON-safe) and its own code (data.reason).
await assert.rejects(requestHost(endpoint, "refuse", { script_handle: "test-script" }), (error: HostError) => {
assert.equal(error.code, "invalid_args")
assert.match(error.message, /\(invalid_args\): SceneView renderer is not registered: Arnold /)
assert.deepEqual(error.data?.registered_renderers, ["OpenGL"])
assert.equal(error.data?.reason, "unsupported_renderer")
assert.equal(typeof error.data?.plug, "string")
return true
})
} finally {
await plugin.cleanup()
}
Expand Down
15 changes: 12 additions & 3 deletions integrations/gaffer/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { readPackage, setupGaffer } from "../plugin/src/integration.ts"
import { createOfferRegistry } from "../plugin/src/kit.ts"

export async function loadPlugin(
input: { now?: () => number; wsl?: () => boolean; answer?: (action: string) => Promise<void> } = {},
input: {
now?: () => number
wsl?: () => boolean
answer?: (action: string) => Promise<void>
// The daemon's shared offer list, as the plugin creates it, instead of a private one.
shared?: boolean
} = {},
) {
const host = readPackage().opencodeIntegration.host!
const registry = createOfferRegistry({
Expand Down Expand Up @@ -58,11 +64,14 @@ export async function loadPlugin(
},
},
} as unknown as Plugin.Context
const cleanup = await setupGaffer(ctx, { registry, wsl: input.wsl ?? (() => false) })
const cleanup = await setupGaffer(ctx, {
registry: input.shared ? undefined : registry,
wsl: input.wsl ?? (() => false),
})
const signal = new AbortController().signal
return {
cleanup,
registry,
registry: cleanup.internals.registry,
events,
permissions,
sessions,
Expand Down
10 changes: 10 additions & 0 deletions integrations/gaffer/test/python/real_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ def probe():
client.sendall((json.dumps({"id": "bad-handle", "token": info["token"], "method": "hello", "params": {"script_handle": "gone"}}) + "\n").encode())
response = json.loads(client.makefile("rb").readline())
assert response["error"]["code"] == "stale_handle"
# A refusal keeps the host's message, its data and its own code (data.reason).
with socket.create_connection((info["host"], info["port"]), timeout=5) as client:
request = {"id": "newer", "token": info["token"], "method": "context",
"params": {"script_handle": handle, "session_id": "private-smoke", "if_revision": 10 ** 6}}
client.sendall((json.dumps(request) + "\n").encode())
error = json.loads(client.makefile("rb").readline())["error"]
assert error["code"] == "stale_revision", error
assert error["message"] == "Requested revision is newer than the Gaffer graph", error
assert error["data"]["reason"] == "revision_conflict", error
assert isinstance(error["data"]["current_revision"], int), error
wire.append("ok")
except Exception as error:
wire.append(type(error).__name__)
Expand Down
3 changes: 3 additions & 0 deletions integrations/gaffer/test/python/stub_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def dispatch(self, method, params):
if params.get("script_handle") != state.handle:
raise module.HostError("stale_handle", "Select the exact script")
return state.hello()
if method == "refuse":
raise module.HostError("unsupported_renderer", "SceneView renderer is not registered: Arnold",
{"registered_renderers": ["OpenGL"], "plug": object()})
raise module.HostError("method_not_found", "Unknown method")

server = module.HostServer(Host())
Expand Down
79 changes: 79 additions & 0 deletions integrations/gaffer/test/registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// An upgrade without restarting OpenCode: the daemon loaded an older OpenCode for Gaffer, whose offer list lives
// on globalThis under the application's name alone, then this version in the same process. This version's list
// never reuses that one, which judges hellos by the older range and calls this version's host incompatible.

import assert from "node:assert/strict"
import { after, it } from "node:test"

import { startFakeHost } from "../../../packages/testing/src/index.ts"
import { readPackage } from "../plugin/src/integration.ts"
import { createOfferRegistry, offerRegistryKey, offerRegistryName } from "../plugin/src/kit.ts"
import { loadPlugin } from "./helpers.ts"

const marker = readPackage().opencodeIntegration
const host = marker.host as NonNullable<typeof marker.host>
const expected = { protocol: host.protocol, range: host.range, appVersions: host.appVersions }
const store = globalThis as unknown as Record<symbol, unknown>
const keys = [offerRegistryKey("gaffer"), offerRegistryKey(offerRegistryName("gaffer", expected))]
after(() => {
for (const key of keys) delete store[key]
})

const announce = (fake: Awaited<ReturnType<typeof startFakeHost>>, offerKey: string) => ({
...fake.endpoint,
script_handle: fake.scriptHandle,
label: "Gaffer",
protocol: 1,
offer_key: offerKey,
})

it("a plugin loaded after an older version in the same process judges its own host ok", async () => {
const fake = await startFakeHost({ hostVersion: readPackage().version, appVersion: "1.7.1.0" })
// What an older version created when it loaded: the kit's shared list under "gaffer", with its own range.
const older = createOfferRegistry({ expected: { ...expected, range: "^0.1.0" } })
store[offerRegistryKey("gaffer")] = { generation: 2, registry: older }
const plugin = await loadPlugin({ shared: true })
try {
assert.notEqual(plugin.registry, older, "this version keeps its own list")
assert.deepEqual(await plugin.rpc("advertise", announce(fake, "after-upgrade")), { outcome: "accepted" })
assert.deepEqual(
plugin.registry.hosts().offers.map((offer) => offer.protocol),
["ok"],
)
await older.advertise(announce(fake, "after-upgrade"))
assert.equal(older.hosts().offers[0]?.protocol, "incompatible")
} finally {
await plugin.cleanup()
await fake.close()
}
})

it("a tool's error carries the host's own message and data", async () => {
const fake = await startFakeHost({
hostVersion: readPackage().version,
appVersion: "1.7.1.0",
methods: {
context: () => {
throw {
code: "invalid_args",
message: "SceneView renderer is not registered: Arnold",
data: { reason: "unsupported_renderer", registered_renderers: ["OpenGL", "Cycles"] },
}
},
},
})
const plugin = await loadPlugin()
try {
await plugin.rpc("advertise", announce(fake, "errors"))
const offerID = plugin.registry.hosts().offers[0]?.offerID as string
assert.deepEqual(await plugin.rpc("attachHost", { offerID, sessionID: "ses_test" }), { outcome: "attached" })
await assert.rejects(plugin.tool("gaffer_context", { script_handle: fake.scriptHandle }), (error: Error) => {
assert.match(error.message, /\(invalid_args\): SceneView renderer is not registered: Arnold/)
assert.match(error.message, /"reason":"unsupported_renderer","registered_renderers":\["OpenGL","Cycles"\]/)
return true
})
} finally {
await plugin.cleanup()
await fake.close()
}
})
Loading
Loading