From 947d07319ae0784997cad3b28f34d7a504b84126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Andr=C3=A9?= Date: Mon, 16 Nov 2026 00:25:55 +0100 Subject: [PATCH 1/4] fix(kit): offer lists named by host contract, host refusals keep their text and data Two defects shared by OpenCode for Houdini and OpenCode for Gaffer, fixed in the kit and in their installed parts before their next versions. Offer list. sharedOfferRegistry kept one list per application on globalThis. After an upgrade without restarting OpenCode, the older plugin version stays loaded in the same process, and the new version reused its list, which judges hellos by the older host range: the new installed part was called incompatible. Blender had worked around it by passing a contract name; the kit now names every shared list after the contract it judges by (.protocol-

.part-.app-, offerRegistryName). Blender passes its application name and gets the same list as before. Refusals. The kit's HostError message was always "Host refused the request ().": the host's explanation stayed in HostError.detail, which only Blender read, and error data was dropped. A refusal's message now carries the host's text (control characters removed, at most 300 characters) and its error.data object (at most 2,000 characters in the message), also kept as HostError.data. Blender builds its own message from code and detail, so it does not change. Installed parts. Houdini answered every refusal "Houdini refused the request" without data, and turned most codes, busy included, into invalid_args. Gaffer sent the message but dropped the data. Both now send the host's message, its data made JSON-safe (dropped above 64 KiB), and the host's own code as data.reason when it maps to another wire code; Houdini passes every wire code through. Their payloads are rebuilt at the same versions (Houdini 0.2.1, Gaffer 0.2.0): they are published with Houdini 0.3.0 and the next Gaffer. The specification describes the optional error.data. The kit README no longer says the specification forbids sending code to a host: it has no such rule, and Blender sends its fixed programs to its add-on's execute_code. Validation: new registry and tool-error tests for Houdini and Gaffer, a wire test of each installed part (Houdini under system Python, Gaffer's real HostServer) and a Houdini unit test of the refusal all fail on 903fa2d and pass. Kit tests cover the contract name and the refusal message. Blender's registry test, adjusted to place the legacy list itself and to check the name, passes on both. The real Gaffer test also passes with Gaffer 1.7.1.0. lint, typecheck, build, npm test (279 pass, 5 skipped), check:markers and the trace scan pass. --- .../blender/plugin/src/integration.ts | 5 +- integrations/blender/test/registry.test.ts | 12 ++- .../gaffer/host/python/GafferOpenCode/Host.py | 49 +++++++--- .../host/gaffer/opencode_link-0.2.0.zip | Bin 183437 -> 184638 bytes integrations/gaffer/plugin/package.json | 2 +- integrations/gaffer/plugin/src/integration.ts | 22 +++-- integrations/gaffer/test/conformance.test.ts | 11 ++- integrations/gaffer/test/helpers.ts | 15 ++- integrations/gaffer/test/python/real_host.py | 10 ++ integrations/gaffer/test/python/stub_host.py | 3 + integrations/gaffer/test/registry.test.ts | 79 ++++++++++++++++ .../host/scripts/opencode_houdini/host.py | 41 +++++++-- .../host/houdini/opencode_link-0.2.1.zip | Bin 207667 -> 209643 bytes integrations/houdini/plugin/package.json | 2 +- .../houdini/plugin/src/integration.ts | 18 ++-- integrations/houdini/test/conformance.test.ts | 17 +++- integrations/houdini/test/helpers.ts | 10 +- integrations/houdini/test/host_probe.py | 13 +++ integrations/houdini/test/registry.test.ts | 86 ++++++++++++++++++ packages/integration-kit/README.md | 48 +++++----- packages/integration-kit/src/host-client.ts | 38 +++++++- packages/integration-kit/src/index.ts | 8 +- .../integration-kit/src/offer-registry.ts | 15 ++- .../integration-kit/test/contract.test.ts | 39 +++++++- .../test/session-bindings.test.ts | 3 +- packages/testing/src/fake-host.ts | 11 ++- spec/README.md | 4 +- 27 files changed, 472 insertions(+), 89 deletions(-) create mode 100644 integrations/gaffer/test/registry.test.ts create mode 100644 integrations/houdini/test/registry.test.ts diff --git a/integrations/blender/plugin/src/integration.ts b/integrations/blender/plugin/src/integration.ts index 353f2b4..c4d38e9 100644 --- a/integrations/blender/plugin/src/integration.ts +++ b/integrations/blender/plugin/src/integration.ts @@ -23,6 +23,7 @@ import { isWslDaemon, loadMethodRegistry, type OfferRegistry, + offerRegistryName, registerPluginTools, requestHost, type SessionBindings, @@ -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 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 }), }) diff --git a/integrations/blender/test/registry.test.ts b/integrations/blender/test/registry.test.ts index 82e189f..4ad610d 100644 --- a/integrations/blender/test/registry.test.ts +++ b/integrations/blender/test/registry.test.ts @@ -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" @@ -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" }) @@ -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() } @@ -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")) diff --git a/integrations/gaffer/host/python/GafferOpenCode/Host.py b/integrations/gaffer/host/python/GafferOpenCode/Host.py index 13c624d..c4f3b78 100644 --- a/integrations/gaffer/host/python/GafferOpenCode/Host.py +++ b/integrations/gaffer/host/python/GafferOpenCode/Host.py @@ -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 ) : @@ -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" ) ) diff --git a/integrations/gaffer/plugin/host/gaffer/opencode_link-0.2.0.zip b/integrations/gaffer/plugin/host/gaffer/opencode_link-0.2.0.zip index 993b3412edeb5945fe718ef0dd1b94fac887f1e2..173e7f1b531ee622249ec1a142f53ef047cf45a8 100644 GIT binary patch delta 18055 zcmbV!33wdEwQi~~#>m8Ayl?VotjIH{J(d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3&|T1 zwsq%H7G)Qg&d`TdEbuhkj}+^Anr@zxvhj<_>z~H1EOz{9x@oH=GI9u3gg+{Aet{R@ z1u%krJnpgBe?3h%*5<#>^c1AD+5^2Ww#TvRmdbQ9CRMpv>oo);SUN9b5Bh$+m1 zZ^Q(Cm%TuFIZW9L^uktNk2#`qbHfue{3H4&+VHq?$_{8&HEjImzE96s%O-pqYZvJ zE{dJq`hDcA*r^w3D;b|&@)BK0NXzuPuhaJPk(_d2@dA4uD7v~530^o{&{rr}RjiD^~TOrq&1r)5z7Mn9I97W!qj8QDW0;v=#2*vO)pyttnr__)M`QxJFM$84Z^?mhKGT5y|@@({YvWGmpm!h zW2fomi;?Z?MpA~~0r#-qo(3-9O`M_f1weso{UBP-^o?ie-E?98|AoLVJ4-vLyo!~* z0p@?`EWM0&U&3G3v%m0{GvcLz#;e`V&%QwmH{}1%1LQ*sKQ!~d$uai*H|XW`KVM-> O-=yKW$6lrQ{C@y!dMKp; diff --git a/integrations/gaffer/plugin/package.json b/integrations/gaffer/plugin/package.json index 1b0258d..43bfc0d 100644 --- a/integrations/gaffer/plugin/package.json +++ b/integrations/gaffer/plugin/package.json @@ -61,7 +61,7 @@ "range": "^0.2.0", "appVersions": ">=1.6", "payload": "host/gaffer/opencode_link-0.2.0.zip", - "sha256": "d11773ecfd29d6608ee84293be76b7145713264f4b72c8308b33272bc2b37b53" + "sha256": "4db41db5c28953af8d56266e9f49b62d8ebf2ded7136e6b9051cf4b72ed2bf29" } } } diff --git a/integrations/gaffer/plugin/src/integration.ts b/integrations/gaffer/plugin/src/integration.ts index b178bcb..c3f8f9b 100644 --- a/integrations/gaffer/plugin/src/integration.ts +++ b/integrations/gaffer/plugin/src/integration.ts @@ -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 + 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) { const clean = JSON.parse(JSON.stringify(result)) const jobs = [clean.job, ...(clean.jobs ?? [])].filter(Boolean) as Array<{ @@ -47,15 +58,8 @@ export function resultContent(result: Record) { 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, diff --git a/integrations/gaffer/test/conformance.test.ts b/integrations/gaffer/test/conformance.test.ts index c5e8cb6..f585b5f 100644 --- a/integrations/gaffer/test/conformance.test.ts +++ b/integrations/gaffer/test/conformance.test.ts @@ -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 () => { @@ -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() } diff --git a/integrations/gaffer/test/helpers.ts b/integrations/gaffer/test/helpers.ts index 0766a81..b4a6989 100644 --- a/integrations/gaffer/test/helpers.ts +++ b/integrations/gaffer/test/helpers.ts @@ -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 } = {}, + input: { + now?: () => number + wsl?: () => boolean + answer?: (action: string) => Promise + // 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({ @@ -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, diff --git a/integrations/gaffer/test/python/real_host.py b/integrations/gaffer/test/python/real_host.py index a301908..51ccdcc 100644 --- a/integrations/gaffer/test/python/real_host.py +++ b/integrations/gaffer/test/python/real_host.py @@ -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__) diff --git a/integrations/gaffer/test/python/stub_host.py b/integrations/gaffer/test/python/stub_host.py index 5e00951..cce06d4 100644 --- a/integrations/gaffer/test/python/stub_host.py +++ b/integrations/gaffer/test/python/stub_host.py @@ -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()) diff --git a/integrations/gaffer/test/registry.test.ts b/integrations/gaffer/test/registry.test.ts new file mode 100644 index 0000000..ecfe316 --- /dev/null +++ b/integrations/gaffer/test/registry.test.ts @@ -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 +const expected = { protocol: host.protocol, range: host.range, appVersions: host.appVersions } +const store = globalThis as unknown as Record +const keys = [offerRegistryKey("gaffer"), offerRegistryKey(offerRegistryName("gaffer", expected))] +after(() => { + for (const key of keys) delete store[key] +}) + +const announce = (fake: Awaited>, 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() + } +}) diff --git a/integrations/houdini/host/scripts/opencode_houdini/host.py b/integrations/houdini/host/scripts/opencode_houdini/host.py index 7fce464..ee9bca9 100644 --- a/integrations/houdini/host/scripts/opencode_houdini/host.py +++ b/integrations/houdini/host/scripts/opencode_houdini/host.py @@ -106,6 +106,39 @@ def __init__(self, code: str, message: str, data: dict[str, Any] | None = None) self.data = data or {} +# 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", "script_quarantined": "quarantined", + "unknown_method": "method_not_found", "ui_timeout": "busy", +} +WIRE_MESSAGE_MAX = 2000 +WIRE_DATA_MAX = 64 * 1024 + + +def _wire_message(text: Any) -> str: + return str(text)[:WIRE_MESSAGE_MAX] + + +def wire_error(error: HostError) -> dict[str, Any]: + """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 not in WIRE_CODES: + code = "invalid_args" + data = _json_value(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: dict[str, Any] = {"code": code, "message": _wire_message(error)} + if data: + refusal["data"] = data + return refusal + + def _json_value(value: Any) -> Any: if isinstance(value, float) and not math.isfinite(value): return "NaN" if math.isnan(value) else ("Infinity" if value > 0 else "-Infinity") @@ -1277,16 +1310,12 @@ def _handle_client(self, client: socket.socket) -> None: if len(json.dumps(response).encode("utf-8")) > 8 * 1024 * 1024: raise HostError("invalid_request", "Narrow the requested working set") except HostError as error: - response = { - "id": request_id, - "ok": False, - "error": {"code": {"unknown_script": "stale_handle", "revision_conflict": "stale_revision", "script_quarantined": "quarantined", "unknown_method": "method_not_found", "ui_timeout": "busy"}.get(error.code, error.code if error.code in ("unauthorized", "invalid_request", "expectation_failed", "mode_conflict", "permission_denied") else "invalid_args"), "message": "Houdini refused the request"}, - } + response = {"id": request_id, "ok": False, "error": wire_error(error)} except Exception as error: response = { "id": request_id, "ok": False, - "error": {"code": "internal", "message": "Houdini host request failed"}, + "error": {"code": "internal", "message": _wire_message("%s: %s" % (type(error).__name__, error))}, } try: client.sendall((json.dumps(response, allow_nan=False) + "\n").encode("utf-8")) diff --git a/integrations/houdini/plugin/host/houdini/opencode_link-0.2.1.zip b/integrations/houdini/plugin/host/houdini/opencode_link-0.2.1.zip index 2db9cb2750582c1d207bf5c097236482e97321b8..fcc09c6b2c0edca063ee4aec12e4556b83a38d92 100644 GIT binary patch delta 2336 zcmZuzL1-LR7*2L2?QTmEg4R;SKVw5@((PnTYlT_1VQseBVp>Qd7SlMH%)H%9ot@df z_aPZkUdews$5fl#|glH;QjQ;PvnIzj7NHY88{r~^{ z-}n80vhn@l-+np#$$P^`ey8orZ~i(kAiiNB85zqTPwtH64{UrpJb9@549qPPAe2TF z+{hygq6Tn0TJa37} zfy2OaxHGR&;;<;x7R=(SmuBkrg{xEbS*U_u1b{*4y|s1XxfbEek!Obyw;NF$dRhsz z^vv^_vlI|{Sg=cI9g{_*p7WcRsj#U~_YDU~}{Bf;D)rVCCNVsyjX|x0gq_sUTLQmB-Kx`&dQL-2{UfUVek!n@!Me2u5&A?Ep-Ad$pfaTI2zzv8KVx2Ub zNl80!T%w0$6=q1Yeygo(*RQwNFSs2ofd{$M-87L3oP>GPH0ASx$pRlE%cXri8s?42 z4|NS;Sdby;>}zWZDU(z5a8Ht5ko`nQq*fZ-zO6X_d? zls5VD{}7NiqBPx)W;$KXYM8`=Rw^rS0pS^EP))`Q`4im}>*5kj!lDK0xwDArjWu;qM8Qs(Q&m{R#4;f53U^E9r>R%6zcmOTyR}b| z%vna~dOT5{I41zggbJcq2`Me9Arj}C-0;1#|K@%u_|o-Ci)v;?sTn1#jT1pkIb4&{ z`-TFqb~aASP8&O+{RF2IDy^M-d@z5sfb%Hnsw6)Rw%dz~KCwKS6;J~4Qy!TG=T%FHWE%*jlNPb^9=R?^f_P|8g$E>28ORkBi0^2jeu z$;``CC`wH$Ely2QD9K0#vI|R7i%XOyFZ?Ar`9+NS0q!Ii01JtJZpZ5*za? z&gmL_%rdMX|L9ClXJuC5)=SV)NJ%XL8m2kDGlx-byNMw4Z3QkwfK8V)Vvb{4{(ZW( n5wrB<+TR@0=NK^y$RKQFWZ-5%1|Kb_pD<#UW&5nb3={?cnU9Ng diff --git a/integrations/houdini/plugin/package.json b/integrations/houdini/plugin/package.json index a7fbcf5..503ad2b 100644 --- a/integrations/houdini/plugin/package.json +++ b/integrations/houdini/plugin/package.json @@ -63,7 +63,7 @@ "range": "^0.2.0", "appVersions": ">=21.0", "payload": "host/houdini/opencode_link-0.2.1.zip", - "sha256": "518330d99122b6cf090acb64543c6513b172a4798ecd4a5b3af776c0bfa5bc8f" + "sha256": "25feefc3f10e5820116e0b8c625bb5a66872ad70b0e0369d3944fa44b9676ffc" } } } diff --git a/integrations/houdini/plugin/src/integration.ts b/integrations/houdini/plugin/src/integration.ts index 295b71b..346e842 100644 --- a/integrations/houdini/plugin/src/integration.ts +++ b/integrations/houdini/plugin/src/integration.ts @@ -19,6 +19,16 @@ export function readPackage() { } } +// 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 + return sharedOfferRegistry("houdini", { + expected: { protocol: host.protocol, range: host.range, appVersions: host.appVersions }, + hello: (endpoint) => requestHost(endpoint, "hello", {}, { timeoutMs: 2000 }), + }) +} + const contextSchema = { type: "object", properties: { @@ -60,13 +70,7 @@ export async function setupHoudini( ) { const pkg = readPackage() const marker = pkg.opencodeIntegration - const host = marker.host as NonNullable - const registry = - deps.registry ?? - sharedOfferRegistry("houdini", { - expected: { protocol: host.protocol, range: host.range, appVersions: host.appVersions }, - hello: (endpoint) => requestHost(endpoint, "hello", {}, { timeoutMs: 2000 }), - }) + const registry = deps.registry ?? createRegistry(marker) const raw = ctx.options as { hostInspectionDir?: unknown } | undefined const preferences = typeof raw?.hostInspectionDir === "string" diff --git a/integrations/houdini/test/conformance.test.ts b/integrations/houdini/test/conformance.test.ts index 0daae7e..1787891 100644 --- a/integrations/houdini/test/conformance.test.ts +++ b/integrations/houdini/test/conformance.test.ts @@ -3,7 +3,7 @@ import { spawn, spawnSync } from "node:child_process" import { join } from "node:path" import { createInterface } from "node:readline" import { it } from "node:test" -import { requestHost } from "../../../packages/integration-kit/src/index.ts" +import { type HostError, requestHost } from "../../../packages/integration-kit/src/index.ts" import { checkCodeExecutionGate, checkHostListener, runSentinelScenario } from "../../../packages/testing/src/index.ts" import { loadPlugin } from "./helpers.ts" @@ -47,6 +47,21 @@ it("the actual Houdini host transport is authenticated, loopback-only and has no await assert.rejects( requestHost(endpoint, "execute_code", { script_handle: "hip:test-document", code: "raise Exception()" }), ) + // A refusal keeps the host's message, its data and its own code (data.reason). + await assert.rejects( + requestHost(endpoint, "apply", { + script_handle: "hip:test-document", + session_id: "ses_test", + if_revision: 0, + command_id: "stale", + }), + (error: HostError) => { + assert.equal(error.code, "stale_revision") + assert.match(error.message, /\(stale_revision\): The Houdini graph changed after the intent was prepared /) + assert.deepEqual(error.data, { expected_revision: 0, current_revision: 1, reason: "revision_conflict" }) + return true + }, + ) } finally { await plugin.cleanup() const stopped = new Promise((resolve) => child.once("exit", resolve)) diff --git a/integrations/houdini/test/helpers.ts b/integrations/houdini/test/helpers.ts index 4c9ce47..c55cf54 100644 --- a/integrations/houdini/test/helpers.ts +++ b/integrations/houdini/test/helpers.ts @@ -12,6 +12,8 @@ export async function loadPlugin( options?: object state?: string wsl?: () => boolean + // The daemon's shared offer list, as the plugin creates it, instead of a private one. + shared?: boolean } = {}, ) { const marker = readPackage().opencodeIntegration @@ -69,10 +71,14 @@ export async function loadPlugin( }, }, } as unknown as Plugin.Context - const cleanup = await setupHoudini(ctx, { registry, wsl: input.wsl ?? (() => false), state: input.state }) + const cleanup = await setupHoudini(ctx, { + registry: input.shared ? undefined : registry, + wsl: input.wsl ?? (() => false), + state: input.state, + }) const signal = new AbortController().signal return { - registry, + registry: cleanup.internals.registry, cleanup, events, permissions, diff --git a/integrations/houdini/test/host_probe.py b/integrations/houdini/test/host_probe.py index 6a258ef..b1c37ab 100644 --- a/integrations/houdini/test/host_probe.py +++ b/integrations/houdini/test/host_probe.py @@ -82,6 +82,19 @@ def test_read_bounds_and_replay_ledger(self): value.apply({"session_id": "ses_test", "command_id": "new"}) self.assertEqual(caught.exception.code, "busy") + def test_refusals_keep_message_data_and_code(self): + refusal = module.wire_error(module.HostError("busy", "Too many commands", {"limit": 256})) + self.assertEqual(refusal, {"code": "busy", "message": "Too many commands", "data": {"limit": 256}}) + refusal = module.wire_error(module.HostError("unknown_ref", "Unknown ref: /obj/geo1", {"ref": object()})) + self.assertEqual(refusal["code"], "invalid_args") + self.assertEqual(refusal["message"], "Unknown ref: /obj/geo1") + self.assertEqual(refusal["data"]["reason"], "unknown_ref") + self.assertIsInstance(refusal["data"]["ref"], str) + json.dumps(refusal, allow_nan=False) + refusal = module.wire_error(module.HostError("expectation_failed", "x" * 5000, {"observed": "y" * 100000})) + self.assertEqual(len(refusal["message"]), module.WIRE_MESSAGE_MAX) + self.assertEqual(refusal["data"], {"reason": "expectation_failed", "truncated": True}) + def test_unknown_method_and_stale_handle(self): host = module.Host.__new__(module.Host) host.state = state() diff --git a/integrations/houdini/test/registry.test.ts b/integrations/houdini/test/registry.test.ts new file mode 100644 index 0000000..6272be1 --- /dev/null +++ b/integrations/houdini/test/registry.test.ts @@ -0,0 +1,86 @@ +// An upgrade without restarting OpenCode: the daemon loaded an older OpenCode for Houdini, 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 { + createOfferRegistry, + offerRegistryKey, + offerRegistryName, +} from "../../../packages/integration-kit/src/index.ts" +import { startFakeHost } from "../../../packages/testing/src/index.ts" +import { readPackage } from "../plugin/src/integration.ts" +import { loadPlugin } from "./helpers.ts" + +const marker = readPackage().opencodeIntegration +const host = marker.host as NonNullable +const expected = { protocol: host.protocol, range: host.range, appVersions: host.appVersions } +const store = globalThis as unknown as Record +const keys = [offerRegistryKey("houdini"), offerRegistryKey(offerRegistryName("houdini", expected))] +after(() => { + for (const key of keys) delete store[key] +}) + +const announce = (fake: Awaited>, offerKey: string) => ({ + ...fake.endpoint, + script_handle: fake.scriptHandle, + label: "Houdini", + 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: "22.0.429" }) + // What an older version created when it loaded: the kit's shared list under "houdini", with its own range. + const older = createOfferRegistry({ expected: { ...expected, range: "^0.1.0" } }) + store[offerRegistryKey("houdini")] = { 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: "22.0.429", + methods: { + context: () => { + throw { + code: "stale_revision", + message: "The Houdini graph changed after the intent was prepared: /obj/geo1", + data: { reason: "revision_conflict", expected_revision: 3, current_revision: 5 }, + } + }, + }, + }) + 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("context", { script_handle: fake.scriptHandle }), (error: Error) => { + assert.match( + error.message, + /\(stale_revision\): The Houdini graph changed after the intent was prepared: \/obj\/geo1/, + ) + assert.match(error.message, /"reason":"revision_conflict","expected_revision":3,"current_revision":5/) + return true + }) + } finally { + await plugin.cleanup() + await fake.close() + } +}) diff --git a/packages/integration-kit/README.md b/packages/integration-kit/README.md index de0234b..d097668 100644 --- a/packages/integration-kit/README.md +++ b/packages/integration-kit/README.md @@ -2,23 +2,23 @@ Shared core of the integration plugins: the plugin side of [`spec/README.md`](../../spec/README.md) (`spec: 0`, RPC contract 1). Each plugin bundles it into its build, so two integrations loaded in the same OpenCode daemon cannot diverge. It is not published to npm. -| Module | Role | -| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `types.ts` | Method manifest types (`methods/**.json`) | -| `method-registry.ts` | Loads the manifests of a plugin, sorted by name | -| `manifest-args.ts` | Validates and normalizes tool arguments against a manifest | -| `plugin-tools.ts` | Builds OpenCode V2 tools (`@opencode/plugin` 2.2) from manifests and registers them under the integration's namespace, honoring cancellation; executors return text or content parts (`ToolContent`, for attachments) | -| `coherence-check.ts` | Checks manifests, handlers and the tools mentioned by skills against each other | -| `marker.ts` | Marker types, the draft specification integer and ID helpers | -| `contract.ts` | RPC status contract 1: constants (lease, bounds), types, wire schemas (`rpcDefinition` registers them without `pattern`, which OpenCode 2.2.5 refuses), `advertise`/`withdraw` input validation, display bounds | -| `integration-rpc.ts` | `createIntegrationRpc`: handlers for `ctx.rpc.register`, `status` assembly, bounded `installHost`, payload-free event relay and lease sweeping | -| `offer-registry.ts` | Offer registry with injectable clock and hello transport; `sharedOfferRegistry` keeps it on `globalThis[Symbol.for("neuralnomads..offers")]`; `liveHellos()` also gives each live host's method table | -| `session-bindings.ts` | Per-Location exact native session snapshots, bounded lookups and synchronous registry/generation/revision fencing for reads, attachments and host access | -| `host-client.ts` | Loopback client of host wire protocol 1 (127.0.0.1 only, bounded, cancellable; errors never carry the endpoint, `HostError.detail` keeps the host's bounded explanation) | -| `hello.ts` | `hello` validation, canonical JSON and capability hash, compatibility with the marker's `host` block | -| `host-state.ts` | Derivation of `status.host.state`, WSL detection | -| `validators.ts`, `output.ts` | Small JSON helpers | -| `python/opencode_service.py` | Reference discovery client of headless hosts (standard library only): service registrations, the `global.config` Location with the `OPENCODE_CONFIG_DIR` fallback setting, `service-` targeting, capability hash. Host parts vendor it. | +| Module | Role | +| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `types.ts` | Method manifest types (`methods/**.json`) | +| `method-registry.ts` | Loads the manifests of a plugin, sorted by name | +| `manifest-args.ts` | Validates and normalizes tool arguments against a manifest | +| `plugin-tools.ts` | Builds OpenCode V2 tools (`@opencode/plugin` 2.2) from manifests and registers them under the integration's namespace, honoring cancellation; executors return text or content parts (`ToolContent`, for attachments) | +| `coherence-check.ts` | Checks manifests, handlers and the tools mentioned by skills against each other | +| `marker.ts` | Marker types, the draft specification integer and ID helpers | +| `contract.ts` | RPC status contract 1: constants (lease, bounds), types, wire schemas (`rpcDefinition` registers them without `pattern`, which OpenCode 2.2.5 refuses), `advertise`/`withdraw` input validation, display bounds | +| `integration-rpc.ts` | `createIntegrationRpc`: handlers for `ctx.rpc.register`, `status` assembly, bounded `installHost`, payload-free event relay and lease sweeping | +| `offer-registry.ts` | Offer registry with injectable clock and hello transport; `sharedOfferRegistry(app, options)` keeps one per host contract on `globalThis[Symbol.for("neuralnomads..offers")]` (`.protocol-

.part-.app-`), so a newer plugin version loaded without restarting OpenCode judges hellos by its own range; `liveHellos()` also gives each live host's method table | +| `session-bindings.ts` | Per-Location exact native session snapshots, bounded lookups and synchronous registry/generation/revision fencing for reads, attachments and host access | +| `host-client.ts` | Loopback client of host wire protocol 1 (127.0.0.1 only, bounded, cancellable; errors never carry the endpoint, a refusal's message carries the host's text and its `error.data` object, also kept as `HostError.data`; `HostError.detail` keeps its explanation without path separators) | +| `hello.ts` | `hello` validation, canonical JSON and capability hash, compatibility with the marker's `host` block | +| `host-state.ts` | Derivation of `status.host.state`, WSL detection | +| `validators.ts`, `output.ts` | Small JSON helpers | +| `python/opencode_service.py` | Reference discovery client of headless hosts (standard library only): service registrations, the `global.config` Location with the `OPENCODE_CONFIG_DIR` fallback setting, `service-` targeting, capability hash. Host parts vendor it. | Skill loading lives in the Blender pilot (`integrations/blender/plugin/src/skills.ts`) until a second integration needs it. @@ -36,10 +36,10 @@ Host discovery prepares each candidate Location with the integration's payload-f The kit reconciles the two OpenCode V2 migrations of the previous runtime, reviewed file by file: -| Module | Taken from | Notes | -| ---------------------------------------------------------------------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `plugin-tools.ts` | Verified V2 port (the M365 variant) | The only variant built on `@opencode/plugin` V2 `ToolEditor`/`Info`: namespaced tools, argument validation before execution, abort checks. The other variants still targeted the V1 `tool()` helper or a hand-written schema type. Checked against the 2.2.5 type definitions. | -| `manifest-args.ts`, `method-registry.ts`, `validators.ts`, `output.ts` | Shared runtime | Identical in every copy apart from line endings. | -| `coherence-check.ts` | Shared runtime | Unused import removed; `bundleName` renamed `integration`. | -| `types.ts` | Shared runtime | Bridge transport types removed with the host dispatcher; `requires.bridgeMethods` renamed `hostMethods`; `dangerous` flag added. | -| host dispatcher | Not taken | It inlined Python scripts from the npm package and sent them to the host's code-execution primitive, which the specification forbids: `native` hosts expose a fixed method table and never import code from the package. Its V2 cancellation fix lives on in `plugin-tools.ts`: executors receive the tool context and its abort `signal`. | +| Module | Taken from | Notes | +| ---------------------------------------------------------------------- | ----------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `plugin-tools.ts` | Verified V2 port (the M365 variant) | The only variant built on `@opencode/plugin` V2 `ToolEditor`/`Info`: namespaced tools, argument validation before execution, abort checks. The other variants still targeted the V1 `tool()` helper or a hand-written schema type. Checked against the 2.2.5 type definitions. | +| `manifest-args.ts`, `method-registry.ts`, `validators.ts`, `output.ts` | Shared runtime | Identical in every copy apart from line endings. | +| `coherence-check.ts` | Shared runtime | Unused import removed; `bundleName` renamed `integration`. | +| `types.ts` | Shared runtime | Bridge transport types removed with the host dispatcher; `requires.bridgeMethods` renamed `hostMethods`; `dangerous` flag added. | +| host dispatcher | Not taken | It inlined Python scripts from the npm package and sent them to the host's code-execution primitive. The kit keeps no such dispatcher: an integration that runs code in its application sends it through its own host method (Blender sends its fixed programs to its add-on's `execute_code`). Its V2 cancellation fix lives on in `plugin-tools.ts`: executors receive the tool context and its abort `signal`. | diff --git a/packages/integration-kit/src/host-client.ts b/packages/integration-kit/src/host-client.ts index 404e830..77be214 100644 --- a/packages/integration-kit/src/host-client.ts +++ b/packages/integration-kit/src/host-client.ts @@ -34,14 +34,44 @@ export class HostError extends Error { readonly code: string // The host's own explanation of a refusal, as bounded display text; never the endpoint. readonly detail: string | undefined - constructor(code: string, message: string, detail?: string) { + // The details the host gave with its refusal (`error.data`, a JSON object), such as the current revision. + readonly data: Record | undefined + constructor(code: string, message: string, detail?: string, data?: Record) { super(message) this.name = "HostError" this.code = code this.detail = detail + this.data = data } } +// What a refusal's message carries of the host's text and data, so that an agent reading the error reads them. +const MESSAGE_TEXT_MAX = 300 +const MESSAGE_DATA_MAX = 2_000 + +function refusalText(message: unknown) { + if (typeof message !== "string") return undefined + const clean = message + .replace(/\p{Cc}/gu, " ") + .replace(/\s+/g, " ") + .trim() + return clean ? [...clean].slice(0, MESSAGE_TEXT_MAX).join("") : undefined +} + +function refusalData(data: unknown): Record | undefined { + if (typeof data !== "object" || data === null || Array.isArray(data) || !Object.keys(data).length) return undefined + return data as Record +} + +function refusalMessage(code: string, text: string | undefined, data: Record | undefined) { + let message = `Host refused the request (${code})${text ? `: ${text}` : "."}` + if (data) { + const json = JSON.stringify(data) + message += ` ${json.length > MESSAGE_DATA_MAX ? `${json.slice(0, MESSAGE_DATA_MAX)}…` : json}` + } + return message +} + // Messages never carry the endpoint: port and token stay out of errors and logs. export async function requestHost( endpoint: HostEndpoint, @@ -95,14 +125,16 @@ export async function requestHost( id?: unknown ok?: unknown result?: unknown - error?: { code?: unknown; message?: unknown } + error?: { code?: unknown; message?: unknown; data?: unknown } } if (response.id !== id) return finish(new HostError("invalid_response", "Host returned an invalid envelope.")) if (response.ok === false) { const raw = response.error?.code const code = typeof raw === "string" && HOST_ERROR_CODES.includes(raw) ? raw : "internal" const detail = typeof response.error?.message === "string" ? boundedDetail(response.error.message) : undefined - return finish(new HostError(code, `Host refused the request (${code}).`, detail)) + const data = refusalData(response.error?.data) + const message = refusalMessage(code, refusalText(response.error?.message), data) + return finish(new HostError(code, message, detail, data)) } if (response.ok !== true || !("result" in response)) { return finish(new HostError("invalid_response", "Host returned an invalid envelope.")) diff --git a/packages/integration-kit/src/index.ts b/packages/integration-kit/src/index.ts index 58ceace..c81c87d 100644 --- a/packages/integration-kit/src/index.ts +++ b/packages/integration-kit/src/index.ts @@ -69,7 +69,13 @@ export { export type { MethodRegistry } from "./method-registry.ts" export { loadMethodRegistry } from "./method-registry.ts" export type { HelloProbe, HostBinding, OfferChange, OfferRegistry, OfferRegistryOptions } from "./offer-registry.ts" -export { createOfferRegistry, MAX_SESSIONS, offerRegistryKey, sharedOfferRegistry } from "./offer-registry.ts" +export { + createOfferRegistry, + MAX_SESSIONS, + offerRegistryKey, + offerRegistryName, + sharedOfferRegistry, +} from "./offer-registry.ts" export { errorToMessage, toJson } from "./output.ts" export type { ManifestExecutor, ToolContent } from "./plugin-tools.ts" export { buildPluginTools, buildToolInput, registerPluginTools } from "./plugin-tools.ts" diff --git a/packages/integration-kit/src/offer-registry.ts b/packages/integration-kit/src/offer-registry.ts index 073cf8a..037b539 100644 --- a/packages/integration-kit/src/offer-registry.ts +++ b/packages/integration-kit/src/offer-registry.ts @@ -387,13 +387,20 @@ const REGISTRY_GENERATION = 2 type RegistrySlot = { generation: number; registry: OfferRegistry } -export function offerRegistryKey(app: string) { - return Symbol.for(`neuralnomads.${app}.offers`) +export function offerRegistryKey(name: string) { + return Symbol.for(`neuralnomads.${name}.offers`) } -// The registry shared by every Location of the daemon process. +// The name of an application's shared offer list: it carries the contract the list judges hellos by. After an +// upgrade without restarting OpenCode, the older plugin version stays loaded in the same process; a list named +// after the application alone would judge the new version's application part by the older range. +export function offerRegistryName(app: string, expected: HostExpectation) { + return `${app}.protocol-${expected.protocol}.part-${expected.range}.app-${expected.appVersions}` +} + +// The registry shared by every Location of the daemon process that loaded this contract of `app`. export function sharedOfferRegistry(app: string, options: OfferRegistryOptions): OfferRegistry { - const key = offerRegistryKey(app) + const key = offerRegistryKey(offerRegistryName(app, options.expected)) const store = globalThis as unknown as Record const slot = store[key] if (slot?.generation === REGISTRY_GENERATION) return slot.registry diff --git a/packages/integration-kit/test/contract.test.ts b/packages/integration-kit/test/contract.test.ts index e5eebb9..f1b1683 100644 --- a/packages/integration-kit/test/contract.test.ts +++ b/packages/integration-kit/test/contract.test.ts @@ -16,6 +16,7 @@ import { isWslDaemon, MAX_OFFERS, offerRegistryKey, + offerRegistryName, parseAdvertiseInput, parseHello, parseWithdrawInput, @@ -515,13 +516,19 @@ describe("offer registry", () => { assert.deepEqual(strict.attach(offer?.offerID ?? "", "ses_1"), { outcome: "refused" }) }) - it("is shared by every module instance through Symbol.for", () => { - const key = offerRegistryKey("demo-shared") - assert.equal(key, Symbol.for("neuralnomads.demo-shared.offers")) + it("is shared by every module instance through Symbol.for, one list per host contract", () => { + const name = offerRegistryName("demo-shared", expected) + assert.equal(name, `demo-shared.protocol-${expected.protocol}.part-${expected.range}.app-${expected.appVersions}`) + const key = offerRegistryKey(name) + assert.equal(key, Symbol.for(`neuralnomads.${name}.offers`)) const one = sharedOfferRegistry("demo-shared", { expected }) const two = sharedOfferRegistry("demo-shared", { expected }) assert.equal(one, two) assert.ok((globalThis as unknown as Record)[key]) + // A plugin version expecting another host range, loaded in the same process, gets its own list. + const other = sharedOfferRegistry("demo-shared", { expected: { ...expected, range: "^9.0.0" } }) + assert.notEqual(other, one) + assert.equal(sharedOfferRegistry("demo-shared", { expected }), one) }) }) @@ -535,6 +542,16 @@ describe("host client and versions", () => { busy: () => { throw Object.assign(new Error("x"), { code: "busy" }) }, + refuse: () => { + throw { + code: "stale_revision", + message: "Node /obj/geo1\nchanged", + data: { current_revision: 4, reason: "revision_conflict" }, + } + }, + long: () => { + throw { code: "internal", message: "x".repeat(5_000), data: { blob: "y".repeat(10_000) } } + }, }, }) await assert.rejects(requestHost(host.endpoint, "leak"), (error: HostError) => { @@ -543,6 +560,22 @@ describe("host client and versions", () => { return true }) await assert.rejects(requestHost(host.endpoint, "busy"), (error: HostError) => error.code === "busy") + // The host's own message and data reach the error's message, which a tool's caller reads. + await assert.rejects(requestHost(host.endpoint, "refuse"), (error: HostError) => { + assert.equal(error.code, "stale_revision") + assert.equal( + error.message, + 'Host refused the request (stale_revision): Node /obj/geo1 changed {"current_revision":4,"reason":"revision_conflict"}', + ) + assert.equal(error.detail, "Node obj geo1 changed") + assert.deepEqual(error.data, { current_revision: 4, reason: "revision_conflict" }) + return true + }) + await assert.rejects(requestHost(host.endpoint, "long"), (error: HostError) => { + assert.ok(error.message.length < 2_400, String(error.message.length)) + assert.match(error.message, /^Host refused the request \(internal\): x{300} \{"blob":"y+…$/) + return true + }) await assert.rejects(requestHost({ ...host.endpoint, host: "localhost" }, "hello"), /127.0.0.1 only/) const wrongID = net.createServer((socket) => { socket.resume() diff --git a/packages/integration-kit/test/session-bindings.test.ts b/packages/integration-kit/test/session-bindings.test.ts index a12cc59..f7c0fb2 100644 --- a/packages/integration-kit/test/session-bindings.test.ts +++ b/packages/integration-kit/test/session-bindings.test.ts @@ -6,6 +6,7 @@ import { advanceBindingAuthority, createOfferRegistry, offerRegistryKey, + offerRegistryName, sharedOfferRegistry, } from "../src/offer-registry.ts" import { createSessionBindings } from "../src/session-bindings.ts" @@ -123,7 +124,7 @@ describe("binding authority", () => { symbol, { generation: number; registry: ReturnType } | undefined > - const key = offerRegistryKey(app) + const key = offerRegistryKey(offerRegistryName(app, expected)) try { const old = sharedOfferRegistry(app, { expected, hello: async () => hello }) await old.advertise(offer) diff --git a/packages/testing/src/fake-host.ts b/packages/testing/src/fake-host.ts index 88db9e1..2dbdef6 100644 --- a/packages/testing/src/fake-host.ts +++ b/packages/testing/src/fake-host.ts @@ -106,8 +106,15 @@ export async function startFakeHost(options: FakeHostOptions = {}): Promise) }) } catch (error) { - const code = (error as { code?: string }).code ?? "internal" - return reply({ id, ok: false, error: { code } }) + // A thrown Error answers its code alone; a thrown plain { code, message?, data? } is the host's refusal, + // answered as such. + const thrown = error as { code?: string; message?: unknown; data?: unknown } + const refusal: Record = { code: thrown.code ?? "internal" } + if (!(error instanceof Error)) { + if (typeof thrown.message === "string" && thrown.message) refusal.message = thrown.message + if (thrown.data !== undefined) refusal.data = thrown.data + } + return reply({ id, ok: false, error: refusal }) } }) socket.on("error", () => {}) diff --git a/spec/README.md b/spec/README.md index 2eee5ba..fec5418 100644 --- a/spec/README.md +++ b/spec/README.md @@ -91,10 +91,10 @@ withdraw({ offer_key, token }) => { outcome: "withdrawn" | "none" } ``` { "id", "token", "method", "params" } // at most 1 MiB -{ "id", "ok": true, "result" } | { "id", "ok": false, "error": { "code", "message"? } } // at most 8 MiB +{ "id", "ok": true, "result" } | { "id", "ok": false, "error": { "code", "message"?, "data"? } } // at most 8 MiB ``` -A request without the exact token gets `unauthorized`. The other error codes are `invalid_request`, `method_not_found`, `method_disabled`, `invalid_args`, `stale_handle`, `stale_revision`, `expectation_failed`, `mode_conflict`, `busy`, `quarantined`, `permission_denied` and `internal`; the plugin reads an unknown code as `internal`. `hello` answers: +A request without the exact token gets `unauthorized`. The other error codes are `invalid_request`, `method_not_found`, `method_disabled`, `invalid_args`, `stale_handle`, `stale_revision`, `expectation_failed`, `mode_conflict`, `busy`, `quarantined`, `permission_denied` and `internal`; the plugin reads an unknown code as `internal`. `message` is the application's explanation and `data` an object of details (such as the current revision, or the application's own code as `reason`); the plugin passes both on in the tool's error. `hello` answers: ```ts { protocol, host_version, app_version, capabilities: { methods, operations, undo, background }, From b81e4df61f0b9b0f50cc4dae5c3f9c3d66188354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Andr=C3=A9?= Date: Mon, 16 Nov 2026 02:13:05 +0100 Subject: [PATCH 2/4] feat(houdini): OpenCode for Houdini 0.3.0 on the kit's shared host session OpenCode for Houdini 0.2 offered Houdini only while its own panel was connected, so a CodeNomad conversation alone could not use it, and its tools were a typed apply with no code execution, inspection or verification. 0.3.0 brings it to the level of OpenCode for Blender 0.4.8, on a base extracted from Blender into the integration kit. Installed part (OpenCode Link 0.3.0) - Starts with Houdini's interface (the package's scripts/python/pythonrc.py) and offers it to the OpenCode services every 5 s, labelled with the open file; OPENCODE_HOUDINI_OFFER=0 keeps it from offering itself; background hython never offers itself. - hello names the process and its crash folder (houdini_temp); status answers off the main thread with the request running there and for how long. - execute_code: one undo group per run, undone when the run fails, and the node graph compared with its state before; when that cannot be proven, the scene is quarantined until release with the revision of a read. A save is judged by the open file's date on disk (copies fire Houdini's save events too); a change by its nodes or undo items, never a selection. - Hints read Houdini itself (hints.py): closest node types in the parent's network, the real parm names when parm() returned None, the errors of the nodes that failed to cook, a hou function's signature, closest attributes. - Every request runs on Houdini's main thread, in hython too (requests queued, served by pump()): hou records no undo off the main thread (undos.areEnabled() is false there, measured in 22.0.429). - The panel is a plain client: Connect attaches its conversation (taken back per file) to this Houdini's own offer, found by its label; closing it releases that conversation only and no longer withdraws the offer or stops the host. Plugin - Tools from manifests: houdini_hosts (listing asks no permission), houdini_context (+ release_quarantine), houdini_execute_code, houdini_inspect, houdini_verify (criteria, node errors, render; saved by a background hython), houdini_save, houdini_render (ROP render or PDG cook as a task), houdini_apply kept as it was, houdini_install_host. The fixed programs (programs/*.py) run read only through execute_code. - The only free compatible Houdini attaches itself at the first call; the working method joins the conversation; attachments survive an OpenCode restart and a file change. - A crash is said within two seconds of the crash files Houdini writes for its process (its log, or the scene saved at the crash, which recovers the changes); closed, frozen or still busy are said as such. - Installation finds the newest Houdini 21+ preferences folder; an earlier OpenCode package (packages/opencode_houdini.json) is a legacy_install notice. - Waits: 120 s per request by default, up to an hour for execute_code; the installed part waits up to an hour for the main thread and answers busy when it cannot take a request within 10 s. Shared base (packages/integration-kit) - host-session.ts: ownership, attached offer, automatic and restored attachment, host calls with crash, close and status diagnosis, the hosts tool and maintain; attachments.ts, closed.ts (crashWatch), method.ts, scripts.ts. Names and texts come from each integration; nothing knows an application. Blender now uses them through thin wrappers with its own names and texts: its tests pass unchanged. - The fake test host answers asynchronous methods. - Gaffer's payload is rebuilt at 0.2.0: the kit's new exports rename its minified bundle's identifiers; its unminified code is unchanged. Validation: new and rewritten Houdini tests (plugin, contract, conformance with the code execution gate, system-Python host and hint probes) pass; on real Houdini 22.0.429 Apprentice, 15 hython tests (programs, hints, execute_code undo, status, saves) pass, and a windowed Houdini on a hidden desktop offered itself to a private OpenCode 2.2.5 service without a panel, ran every tool, kept another conversation attached when its panel closed, and was reported crashed 2.0 s after a real crash with its recovery scene. Blender's suites (unit, plugin, 27 real-Blender tests, native sessions) and the real Gaffer test pass. lint, typecheck, build, npm test (288 pass), check:markers and the trace scan pass. --- catalog.json | 8 +- .../blender/plugin/src/attachments.ts | 229 +------ integrations/blender/plugin/src/closed.ts | 84 +-- .../blender/plugin/src/integration.ts | 341 ++------- integrations/blender/plugin/src/method.ts | 25 +- integrations/blender/plugin/src/tasks.ts | 11 +- .../host/gaffer/opencode_link-0.2.0.zip | Bin 184638 -> 184642 bytes integrations/gaffer/plugin/package.json | 2 +- integrations/houdini/host/manifest.json | 2 +- .../host/scripts/opencode_houdini/__init__.py | 84 ++- .../scripts/opencode_houdini/advertiser.py | 97 +++ .../host/scripts/opencode_houdini/hints.py | 351 ++++++++++ .../host/scripts/opencode_houdini/host.py | 154 ++++- .../host/scripts/opencode_houdini/panel.py | 14 +- .../host/scripts/opencode_houdini/runner.py | 191 ++++++ .../houdini/host/scripts/python/pythonrc.py | 8 + integrations/houdini/plugin/README.md | 65 +- ...link-0.2.1.zip => opencode_link-0.3.0.zip} | Bin 209643 -> 258083 bytes .../plugin/methods/tools/houdini_apply.json | 29 + .../plugin/methods/tools/houdini_context.json | 27 + .../methods/tools/houdini_execute_code.json | 19 + .../plugin/methods/tools/houdini_hosts.json | 12 + .../plugin/methods/tools/houdini_inspect.json | 45 ++ .../methods/tools/houdini_install_host.json | 9 + .../plugin/methods/tools/houdini_render.json | 62 ++ .../plugin/methods/tools/houdini_save.json | 27 + .../plugin/methods/tools/houdini_verify.json | 51 ++ integrations/houdini/plugin/package.json | 13 +- .../houdini/plugin/programs/inspect.py | 638 +++++++++++++++++ .../houdini/plugin/programs/render.py | 176 +++++ .../houdini/plugin/programs/render_job.py | 180 +++++ integrations/houdini/plugin/programs/save.py | 72 ++ .../houdini/plugin/programs/verify.py | 533 +++++++++++++++ .../houdini/plugin/programs/verify_saved.py | 76 +++ .../houdini/plugin/scripts/build-host.ts | 10 + .../plugin/skills/houdini-pipeline/SKILL.md | 18 - integrations/houdini/plugin/src/closed.ts | 22 + .../houdini/plugin/src/host-install.ts | 15 +- .../houdini/plugin/src/integration.ts | 547 ++++++++++----- integrations/houdini/plugin/src/kit.ts | 2 + integrations/houdini/plugin/src/legacy.ts | 37 + integrations/houdini/plugin/src/method.ts | 25 + integrations/houdini/plugin/src/options.ts | 25 + integrations/houdini/plugin/src/paths.ts | 52 ++ integrations/houdini/plugin/src/programs.ts | 29 + integrations/houdini/plugin/src/sidecar.ts | 152 +++-- integrations/houdini/plugin/src/tasks.ts | 141 ++++ integrations/houdini/test/conformance.test.ts | 137 +++- integrations/houdini/test/contract.test.ts | 152 +++-- integrations/houdini/test/helpers.ts | 111 ++- integrations/houdini/test/hints_probe.py | 233 +++++++ integrations/houdini/test/host_probe.py | 169 ++++- integrations/houdini/test/hython.test.ts | 24 + integrations/houdini/test/panel_probe.py | 62 +- integrations/houdini/test/plugin.test.ts | 185 +++++ integrations/houdini/test/programs_hython.py | 645 ++++++++++++++++++ integrations/houdini/test/registry.test.ts | 4 +- package-lock.json | 2 +- packages/integration-kit/README.md | 39 +- packages/integration-kit/src/attachments.ts | 228 +++++++ packages/integration-kit/src/closed.ts | 101 +++ packages/integration-kit/src/host-session.ts | 349 ++++++++++ packages/integration-kit/src/index.ts | 15 + packages/integration-kit/src/method.ts | 30 + packages/integration-kit/src/scripts.ts | 25 + packages/testing/src/fake-host.ts | 11 +- 66 files changed, 6138 insertions(+), 1094 deletions(-) create mode 100644 integrations/houdini/host/scripts/opencode_houdini/advertiser.py create mode 100644 integrations/houdini/host/scripts/opencode_houdini/hints.py create mode 100644 integrations/houdini/host/scripts/opencode_houdini/runner.py create mode 100644 integrations/houdini/host/scripts/python/pythonrc.py rename integrations/houdini/plugin/host/houdini/{opencode_link-0.2.1.zip => opencode_link-0.3.0.zip} (79%) create mode 100644 integrations/houdini/plugin/methods/tools/houdini_apply.json create mode 100644 integrations/houdini/plugin/methods/tools/houdini_context.json create mode 100644 integrations/houdini/plugin/methods/tools/houdini_execute_code.json create mode 100644 integrations/houdini/plugin/methods/tools/houdini_hosts.json create mode 100644 integrations/houdini/plugin/methods/tools/houdini_inspect.json create mode 100644 integrations/houdini/plugin/methods/tools/houdini_install_host.json create mode 100644 integrations/houdini/plugin/methods/tools/houdini_render.json create mode 100644 integrations/houdini/plugin/methods/tools/houdini_save.json create mode 100644 integrations/houdini/plugin/methods/tools/houdini_verify.json create mode 100644 integrations/houdini/plugin/programs/inspect.py create mode 100644 integrations/houdini/plugin/programs/render.py create mode 100644 integrations/houdini/plugin/programs/render_job.py create mode 100644 integrations/houdini/plugin/programs/save.py create mode 100644 integrations/houdini/plugin/programs/verify.py create mode 100644 integrations/houdini/plugin/programs/verify_saved.py delete mode 100644 integrations/houdini/plugin/skills/houdini-pipeline/SKILL.md create mode 100644 integrations/houdini/plugin/src/closed.ts create mode 100644 integrations/houdini/plugin/src/kit.ts create mode 100644 integrations/houdini/plugin/src/legacy.ts create mode 100644 integrations/houdini/plugin/src/method.ts create mode 100644 integrations/houdini/plugin/src/options.ts create mode 100644 integrations/houdini/plugin/src/programs.ts create mode 100644 integrations/houdini/plugin/src/tasks.ts create mode 100644 integrations/houdini/test/hints_probe.py create mode 100644 integrations/houdini/test/hython.test.ts create mode 100644 integrations/houdini/test/plugin.test.ts create mode 100644 integrations/houdini/test/programs_hython.py create mode 100644 packages/integration-kit/src/attachments.ts create mode 100644 packages/integration-kit/src/closed.ts create mode 100644 packages/integration-kit/src/host-session.ts create mode 100644 packages/integration-kit/src/method.ts create mode 100644 packages/integration-kit/src/scripts.ts diff --git a/catalog.json b/catalog.json index c3f0403..e5952d4 100644 --- a/catalog.json +++ b/catalog.json @@ -58,17 +58,17 @@ { "id": "houdini", "package": "@neuralnomads/opencode-houdini", - "range": "^0.2", + "range": "^0.3", "displayName": "OpenCode for Houdini", "family": "native", "plugin": "neuralnomads.houdini", "rpc": "neuralnomads.houdini", "summary": { - "en": "Inspect and edit the graph open in Houdini through an authenticated local host and optional conversation panel.", - "fr": "Inspecter et modifier le graphe ouvert dans Houdini grâce à un hôte local authentifié et un panneau de conversation facultatif." + "en": "Read, change and check the open Houdini scene from any OpenCode conversation, with an optional panel inside Houdini.", + "fr": "Lire, modifier et vérifier la scène Houdini ouverte depuis toute conversation OpenCode, avec un panneau facultatif dans Houdini." }, "app": { "name": "Houdini", "versions": ">=21.0" }, - "host": { "protocol": 1, "range": "^0.2.0" }, + "host": { "protocol": 1, "range": "^0.3.0" }, "docs": "https://github.com/NeuralNomadsAI/opencode-integrations/blob/main/integrations/houdini/plugin/README.md", "status": "preview", "sources": { "git": ["github:NeuralNomadsAI/opencode-integrations#main::path:integrations/houdini/plugin"] } diff --git a/integrations/blender/plugin/src/attachments.ts b/integrations/blender/plugin/src/attachments.ts index 5bce7fd..758bd64 100644 --- a/integrations/blender/plugin/src/attachments.ts +++ b/integrations/blender/plugin/src/attachments.ts @@ -1,218 +1,29 @@ -// Attachments that survive an OpenCode restart. The offer registry lives in memory (HST-12): a restarted daemon -// forgets which conversation used which Blender, while the Blender keeps offering itself under the same offer key -// for its whole process life. Each Location's plugin keeps the attachments of its own conversations in its storage -// (`attachment/`, the offer key only as a SHA-256 digest, never a token, port or script handle) and, when -// the same Blender offers itself again, attaches the conversation back after the exact-session ownership check of -// RPC-12. A stored attachment lives while its Blender is offered and LEASE_MS after it stops; an explicit detach -// removes it. Restoring happens when the plugin loads and when offers change, never inside a read (RPC-13). +// Blender's attachments kept across an OpenCode restart: the kit's attachments (packages/integration-kit), under +// Blender's own keys. -import { createHash } from "node:crypto" +import { + type AttachmentStorage, + type Attachments, + createAttachments as createKitAttachments, + hostDigest as kitHostDigest, + trackAdvertise as kitTrackAdvertise, + type OfferRegistry, + resetAttachmentsForTests as resetKit, +} from "./kit.ts" -import type { BindingAuthority, OfferRegistry } from "./kit.ts" +export { ATTACHMENT_LEASE_MS as LEASE_MS } from "./kit.ts" +export type { AttachmentStorage, Attachments } -export const LEASE_MS = 120_000 -const PERSIST_EVERY_MS = 30_000 -const PREFIX = "attachment/" -const PAGES = 100 -const SESSION_ID = /^[A-Za-z0-9_-]{1,128}$/ -const HOST = /^[0-9a-f]{64}$/ +const APP = "blender" -export type AttachmentStorage = { - get(key: string): Promise - set(key: string, value: never): Promise - remove(key: string): Promise - scan(options: { prefix: string; after?: string; limit?: number }): Promise<{ - entries: readonly { key: string; value: unknown }[] - next?: string - }> -} - -type Stored = { session: string; host: string; label: string; since: number; seen: number } - -// Per daemon process, per registry: the digest of each live offer's key, and the conversations attachments were -// recorded for (all Locations), so that a list can say which conversation holds which Blender. -type Shared = { - hosts: WeakMap> - sessions: Set - advertising: Promise -} -const SHARED = Symbol.for("neuralnomads.blender.attachments") -const store = globalThis as unknown as Record -store[SHARED] ??= { hosts: new WeakMap(), sessions: new Set(), advertising: Promise.resolve() } -const shared = store[SHARED] - -export function hostDigest(offerKey: string) { - return createHash("sha256").update(`neuralnomads.blender.offer\0${offerKey}`).digest("hex") -} - -function digests(registry: OfferRegistry) { - let map = shared.hosts.get(registry) - if (!map) { - map = new Map() - shared.hosts.set(registry, map) - } - return map -} - -function valid(value: unknown): value is Stored { - const entry = value as Stored - return ( - typeof entry === "object" && - entry !== null && - typeof entry.session === "string" && - SESSION_ID.test(entry.session) && - typeof entry.host === "string" && - HOST.test(entry.host) && - typeof entry.label === "string" && - entry.label.length <= 120 && - Number.isFinite(entry.since) - ) -} - -// The advertise handler, serialized per daemon process: the offer an accepted announcement created is the one -// that appeared during the call, so its key's digest names it (the registry keeps keys private). -export function trackAdvertise( - registry: OfferRegistry, - advertise: (input: unknown) => Promise, -): (input: unknown) => Promise { - return (input) => { - const run = shared.advertising.then(async () => { - const before = new Set(registry.hosts().offers.map((offer) => offer.offerID)) - const result = (await advertise(input)) as { outcome?: string } - const key = (input as { offer_key?: unknown } | null)?.offer_key - if (result?.outcome === "accepted" && typeof key === "string") { - const created = registry.hosts().offers.filter((offer) => !before.has(offer.offerID)) - if (created.length === 1 && created[0]) digests(registry).set(hostDigest(key), created[0].offerID) - } - return result - }) - shared.advertising = run.catch(() => {}) - return run - } -} +export const hostDigest = (offerKey: string) => kitHostDigest(offerKey, APP) -export type Attachments = ReturnType +export const trackAdvertise = (registry: OfferRegistry, advertise: (input: unknown) => Promise) => + kitTrackAdvertise(registry, advertise, APP) -export function createAttachments(options: { - registry: OfferRegistry - storage?: AttachmentStorage - now?: () => number - // A fresh ownership snapshot of the session at this Location (null when it is not verifiably ours). - owns: (sessionID: string) => Promise -}) { - const { registry, storage } = options - const now = options.now ?? Date.now - const records = new Map() - const persisted = new Map() - - const live = () => new Map(registry.hosts().offers.map((offer) => [offer.offerID, offer])) - const offerOf = (host: string) => digests(registry).get(host) - const hostOf = (offerID: string) => [...digests(registry)].find(([, id]) => id === offerID)?.[0] - - // Storage failures never fail a Blender call: the attachment then lasts until OpenCode stops. - const write = async (record: Stored) => { - persisted.set(record.session, now()) - try { - await storage?.set(PREFIX + record.session, record as never) - } catch {} - } - - const forget = async (sessionID: string) => { - if (!records.delete(sessionID)) return - persisted.delete(sessionID) - try { - await storage?.remove(PREFIX + sessionID) - } catch {} - } - - return { - // The stored attachments, once; their lease starts again now. - async load() { - if (!storage) return - let after: string | undefined - for (let page = 0; page < PAGES; page += 1) { - let result: Awaited> | undefined - try { - result = await storage.scan({ prefix: PREFIX, ...(after ? { after } : {}), limit: 100 }) - } catch {} - const entries = Array.isArray(result?.entries) ? result.entries : [] - for (const entry of entries) { - if (!valid(entry?.value) || entry.key !== PREFIX + entry.value.session) continue - records.set(entry.value.session, { ...entry.value, seen: now() }) - shared.sessions.add(entry.value.session) - } - if (typeof result?.next !== "string" || entries.length === 0) break - after = result.next - } - }, - - // The conversation is attached now (tool, automatic or RPC attachment): keep it. - async remember(sessionID: string) { - const bound = registry.binding(sessionID) - const host = bound && hostOf(bound.offerID) - const offer = bound && live().get(bound.offerID) - if (!bound || !host || !offer) return - shared.sessions.add(sessionID) - const previous = records.get(sessionID) - const record = { - session: sessionID, - host, - label: offer.label, - since: previous?.host === host ? previous.since : now(), - seen: now(), - } - records.set(sessionID, record) - await write(record) - }, - - forget, - - // Attaches back each stored conversation (or only `sessionID`) whose Blender is offered again. - async restore(sessionID?: string) { - const offers = live() - for (const record of [...records.values()]) { - if (sessionID !== undefined && record.session !== sessionID) continue - if (registry.binding(record.session)) continue - const offerID = offerOf(record.host) - if (!offerID || offers.get(offerID)?.protocol !== "ok") continue - const authority = await options.owns(record.session) - // Synchronous after the snapshot (RPC-12): the same registry state, still stored (a detach may have - // forgotten it meanwhile), still no binding, the offer still live. - if (!authority || !registry.current(authority) || records.get(record.session) !== record) continue - if (registry.binding(record.session)) continue - if (!live().has(offerID)) continue - if (registry.attach(offerID, record.session).outcome === "attached") record.seen = now() - } - }, - - // Renews the lease of the stored attachments whose Blender is offered; forgets those gone longer than it. - async sweep() { - const offers = live() - for (const record of [...records.values()]) { - const offerID = offerOf(record.host) - const offer = offerID ? offers.get(offerID) : undefined - if (offer) { - record.seen = now() - record.label = offer.label - if (now() - (persisted.get(record.session) ?? 0) > PERSIST_EVERY_MS) await write(record) - } else if (now() - record.seen > LEASE_MS) await forget(record.session) - } - }, - - // Conversations other than `except` attached to the offer (those this daemon recorded, all Locations). - heldBy(offerID: string, except?: string) { - return [...shared.sessions].some( - (session) => session !== except && registry.binding(session)?.offerID === offerID, - ) - }, - - has(sessionID: string) { - return records.has(sessionID) - }, - } -} +export const createAttachments = (options: Omit[0], "app">) => + createKitAttachments({ ...options, app: APP }) export function resetAttachmentsForTests() { - shared.sessions.clear() - shared.advertising = Promise.resolve() + resetKit(APP) } diff --git a/integrations/blender/plugin/src/closed.ts b/integrations/blender/plugin/src/closed.ts index 6616a68..1575ef5 100644 --- a/integrations/blender/plugin/src/closed.ts +++ b/integrations/blender/plugin/src/closed.ts @@ -1,81 +1,19 @@ -// What became of a Blender that stopped answering a conversation: crashed (Blender wrote its crash report; on -// Windows its process stays open behind Blender's crash window), closed (its process gone, no report: closed, or -// ended by another program), still running a long call, or no longer answering. The add-on's hello names its -// process, the folder Blender writes ".crash.txt" to, and its document. Every Blender of a user shares -// that folder: only the report named after this Blender's document counts. +// What became of a Blender that stopped answering a conversation (the kit's crashWatch): crashed, closed, or +// alive without renewing its offer. Blender writes ".crash.txt" to its temporary folder; every Blender +// of a user shares that folder: only the report named after this Blender's document counts. -import { readdirSync, statSync } from "node:fs" -import { join } from "node:path" +import { crashWatch, type HostProcess, readProcess } from "./kit.ts" -import { isAlive } from "./tasks.ts" - -export type HostProcess = { - pid: number - tempDir: string - label?: string - since: number - offerID?: string - document?: string -} - -export function readProcess(hello: unknown, label?: string, now = Date.now()): HostProcess | null { - const read = hello as { process?: { pid?: unknown; temp_dir?: unknown }; document?: unknown } | null - const found = read?.process - if (!found || !Number.isInteger(found.pid) || (found.pid as number) <= 0) return null - if (typeof found.temp_dir !== "string" || !found.temp_dir) return null - const document = typeof read?.document === "string" && read.document ? read.document : undefined - return { pid: found.pid as number, tempDir: found.temp_dir, label, since: now, document } -} +export { type HostProcess, readProcess } // The name Blender gives its crash report: the file's name, or "blender" for a file never saved. function reportName(host: HostProcess) { return `${host.document ? host.document.replace(/\.blend$/i, "") : "blender"}.crash.txt` } -// This Blender's crash report when written since `after` (by default since this conversation first reached it). -export function crashReport(host: HostProcess, after = host.since): string | null { - const wanted = reportName(host).toLowerCase() - let names: string[] - try { - names = readdirSync(host.tempDir) - } catch { - return null - } - for (const name of names) { - if (name.toLowerCase() !== wanted) continue - const path = join(host.tempDir, name) - try { - if (statSync(path).mtimeMs >= after - 1_000) return path - } catch { - // removed meanwhile - } - } - return null -} - -const again = "Open Blender again and attach it (blender_hosts)." - -// Crashed or closed; null while its process lives without a new crash report. -export function closedMessage( - host: HostProcess, - alive: (pid: number) => boolean = isAlive, - after = host.since, -): string | null { - const name = host.label ? ` (${host.label})` : "" - const report = crashReport(host, after) - const living = alive(host.pid) - if (report) - return `Blender crashed${name}: crash report ${report}. ${ - living - ? `Its process ${host.pid} is still open (Blender's crash window) and answers nothing.` - : `Its process ${host.pid} has ended.` - } Changes after its last save are lost. ${again}` - if (living) return null - return `Blender closed${name}: its process ${host.pid} has ended, with no crash report in ${host.tempDir} (closed, or ended by another program). ${again}` -} - -// Its offer is gone while its process lives without a crash report: the add-on stopped renewing it. -export function silentMessage(host: HostProcess) { - const name = host.label ? ` (${host.label})` : "" - return `Blender${name} no longer offers itself: its process ${host.pid} still runs but stopped renewing its offer (frozen, or OpenCode Link was disabled). blender_hosts lists the Blenders offered.` -} +export const { crashReport, closedMessage, silentMessage } = crashWatch({ + appName: "Blender", + app: "blender", + part: "OpenCode Link", + isReport: (name, host) => name.toLowerCase() === reportName(host).toLowerCase(), +}) diff --git a/integrations/blender/plugin/src/integration.ts b/integrations/blender/plugin/src/integration.ts index c4d38e9..6efdab7 100644 --- a/integrations/blender/plugin/src/integration.ts +++ b/integrations/blender/plugin/src/integration.ts @@ -2,28 +2,29 @@ // no socket, process, download or installation (PKG-6); the host is reached only once a session is // attached to an offer validated by hello. -import { readFileSync, statSync } from "node:fs" +import { readFileSync } from "node:fs" import { join, resolve } from "node:path" import type { Plugin } from "@opencode/plugin" import type { ToolContext } from "@opencode/plugin/promise/tool" import { type AttachmentStorage, createAttachments, trackAdvertise } from "./attachments.ts" -import { closedMessage, crashReport, type HostProcess, readProcess, silentMessage } from "./closed.ts" +import { closedMessage, crashReport, silentMessage } from "./closed.ts" import { extensionsDir, inspectHost, installHost } from "./host-install.ts" import { - type BindingAuthority, buildPluginTools, + createHostSession, createIntegrationRpc, HELLO_TIMEOUT_MS, - type HostBinding, HostError, type HostExpectation, + hostErrorWith, type IntegrationMarker, isWslDaemon, loadMethodRegistry, type OfferRegistry, offerRegistryName, + readScript, registerPluginTools, requestHost, type SessionBindings, @@ -35,7 +36,7 @@ import { type EntryPut, methodAttacher } from "./method.ts" import { type BlenderOptions, readOptions } from "./options.ts" import { defaultPathEnv, methodsDir, type PathEnv, packageRoot } from "./paths.ts" import { type ProgramName, programCode } from "./programs.ts" -import { addTask, cancelTask, getTask, isAlive, type RenderStart, taskStatus } from "./tasks.ts" +import { addTask, cancelTask, getTask, type RenderStart, taskStatus } from "./tasks.ts" import { validatePlan } from "./validate.ts" export const APP = "blender" @@ -70,15 +71,9 @@ export const LAB_TOOLS = [ const MAX_TEXT = 100_000 const VERIFY_TIMEOUT_MS = 600_000 -// The status method answers off Blender's main thread: no answer within this long means the add-on is frozen. -const STATUS_TIMEOUT_MS = 3_000 -// How often a running call looks for its Blender's crash report. -const CRASH_POLL_MS = 2_000 // A save that failed while the .blend was briefly held by another process, retried once after SAVE_RETRY_MS. export const TRANSIENT_SAVE = /unable to make version backup|file saved with @/i export const SAVE_RETRY_MS = 1_000 -// Ownership snapshots taken again when other conversations or offers moved the shared registry meanwhile. -const OWNERSHIP_ATTEMPTS = 5 // Longest wait of one blender_render call that follows a task. export const RENDER_WAIT_MAX_S = 3_600 @@ -116,32 +111,11 @@ export function readPackage(): PackageJson { return JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8")) as PackageJson } -// The longest script blender_execute_code reads from a file: the host reads requests of at most 1 MiB. -export const SCRIPT_MAX_BYTES = 900 * 1024 +// The longest script blender_execute_code reads from a file, and its reading (the kit's). +export { readScript, SCRIPT_MAX_BYTES } from "./kit.ts" -// A Python script on disk (absolute, or relative to the Location's directory), as UTF-8 text. -export function readScript(path: string, base?: string) { - const file = resolve(base ?? process.cwd(), path) - let bytes: Buffer - try { - if (statSync(file).size > SCRIPT_MAX_BYTES) - throw new Error(`${file} is over ${SCRIPT_MAX_BYTES} bytes: split it, or have it exec() its parts.`) - bytes = readFileSync(file) - } catch (error) { - if ((error as NodeJS.ErrnoException).code === "ENOENT") throw new Error(`No script ${file}.`) - throw error - } - const code = new TextDecoder("utf-8", { fatal: true }).decode(bytes).replace(/^/, "") - return { path: file, code } -} - -function hostError(error: unknown): Error { - if (error instanceof HostError) { - const hint = HINTS[error.code] ?? `Blender refused the request (${error.code}).` - return new Error(error.detail ? `${hint} ${error.detail}` : hint) - } - return error instanceof Error ? error : new Error(String(error)) -} +// The add-on's explanation follows the hint (HostError.detail: bounded, without path separators). +const hostError = hostErrorWith(HINTS, "Blender", (error) => error.detail ?? "") function bounded(text: string) { return text.length > MAX_TEXT @@ -179,11 +153,8 @@ export function createRegistry(marker: IntegrationMarker, options: BlenderOption }) } -// label: the attached offer's label when the ticket was taken (a permission prompt shows it). -type Ticket = { authority: BindingAuthority; bound: HostBinding; label?: string } // A fixed program's run: its result, with what the open document's file lacks and what a save wrote. type ProgramRun = { result?: unknown; unsaved?: unknown; saved?: boolean; saved_steps?: string[] } -type Hello = { script_handle: string; revision: number; host_version?: string; document?: unknown } export async function setupBlender(ctx: Plugin.Context, deps: BlenderDeps = {}) { const env = deps.env ?? process.env @@ -266,202 +237,28 @@ export async function setupBlender(ctx: Plugin.Context, deps: BlenderDeps = {}) const put = session?.instructions?.entry?.put const attachMethod = methodAttacher(put ? (input) => put.call(session?.instructions?.entry, input) : undefined) - // Whether a conversation other than `sessionID` is attached to the offer: the conversations this daemon recorded - // (attachments.ts); with one offer, also the registry's count of attached sessions, which are all on it. - const heldElsewhere = (offerID: string, sessionID: string) => { - if (attachments.heldBy(offerID, sessionID)) return true - const offers = registry.hosts().offers - if (offers.length !== 1 || offers[0]?.offerID !== offerID) return false - return registry.counts().attachedSessions - (registry.binding(sessionID)?.offerID === offerID ? 1 : 0) > 0 - } - - // This conversation's ownership, fenced by the registry's revision. That revision is shared by every offer and - // conversation: another conversation attaching another Blender, or an offer appearing, during the native - // lookup moves it. Only then is the snapshot taken again (an ownership that does not hold fails at once). - const ownership = async (sessionID: string, signal?: AbortSignal) => { - for (let attempt = 0; attempt < OWNERSHIP_ATTEMPTS; attempt += 1) { - const before = registry.authority() - const authority = await bindings.snapshot(sessionID, signal) - if (authority || signal?.aborted) return authority - const after = registry.authority() - const moved = - before && - after && - before.serviceGeneration === after.serviceGeneration && - (before.registryGeneration !== after.registryGeneration || before.revision !== after.revision) - if (!moved) return null - } - return null - } - - // After a wait (a permission): this conversation still owned, still attached to the same offer. Unrelated - // changes of other offers and conversations do not count. - const reheld = async (context: ToolContext, held: Ticket): Promise => { - const authority = await ownership(context.sessionID, context.signal) - const bound = authority && registry.current(authority) ? registry.binding(context.sessionID) : null - const label = attachedTo(context.sessionID)?.label - return bound && - bound.offerID === held.bound.offerID && - bound.scriptHandle === held.bound.scriptHandle && - label === held.label - ? { authority: authority as BindingAuthority, bound, label } - : null - } - - // The Blender this conversation acts on, as blender_hosts and blender_context name it. - const attachedTo = (sessionID: string) => { - const bound = registry.binding(sessionID) - const offer = bound && registry.hosts().offers.find((candidate) => candidate.offerID === bound.offerID) - return offer ? { offerID: offer.offerID, label: offer.label } : null - } - - // The process of the Blender each conversation last reached (its hello): a later call that cannot reach it says - // that Blender closed, and where its crash report is. Not from WSL, where Blender's process is on Windows. - const reached = new Map() - const helloed = new Map() // sessionID -> the offer whose hello was read + // The conversations' attached Blender and its calls (the kit's host session): ownership, automatic and + // restored attachment, and what a call says of a Blender that crashed, closed or does not answer. const wsl = deps.wsl ?? (() => isWslDaemon()) - // Its offer gone: crashed, closed, or alive without renewing its offer. Never another Blender in its place. - const goneFor = (sessionID: string) => { - const host = reached.get(sessionID) - if (!host || wsl() || registry.hosts().offers.some((offer) => offer.offerID === host.offerID)) return null - return closedMessage(host) ?? silentMessage(host) - } - // A call that failed, timed out or was refused as busy: what Blender is doing. Its crash report (written since - // this conversation reached that process), the process gone, or the request its main thread runs now (the status - // method answers off the main thread). - const diagnose = async (sessionID: string, bound: HostBinding, code: string) => { - const host = reached.get(sessionID) - if (!host || wsl()) return null - const closed = closedMessage(host, isAlive) - if (closed || code === "unreachable" || !bound.methods.includes("status")) return closed - try { - const status = (await requestHost(bound.endpoint, "status", {}, { timeoutMs: STATUS_TIMEOUT_MS })) as { - busy?: boolean - running?: string - seconds?: number - } - if (!status.busy) return null - return `Blender is still running ${status.running ?? "a request"} (${status.seconds ?? "?"} s so far): it goes on in Blender, nothing is lost. blender_context answers once it has finished; read it before acting again.` - } catch { - return `Blender does not answer: its process ${host.pid} runs, but OpenCode Link answered nothing within ${STATUS_TIMEOUT_MS / 1_000} s (frozen?).` - } - } - const remember = async (context: ToolContext, held: Ticket) => { - if (helloed.get(context.sessionID) === held.bound.offerID) return - helloed.set(context.sessionID, held.bound.offerID) - try { - const found = readProcess(await hello(held.bound, context.signal), held.label) - if (found) reached.set(context.sessionID, { ...found, offerID: held.bound.offerID }) - else reached.delete(context.sessionID) - } catch { - // an add-on without process in its hello, or a failed hello: the call itself reports - } - } - - const hello = async (bound: HostBinding, signal: AbortSignal) => - (await requestHost(bound.endpoint, "hello", {}, { signal, timeoutMs: HELLO_TIMEOUT_MS })) as Hello - - // The conversation's attached Blender. Without one, the only offered Blender, compatible and attached to no - // conversation, attaches itself (spec FAM-5): checked against the ownership snapshot with no wait in between. - const ticket = async (context: ToolContext): Promise => { - const authority = await ownership(context.sessionID, context.signal) - if (!authority || context.signal.aborted || !registry.current(authority)) { - throw new Error("This conversation's ownership is unverifiable: try again.") - } - let bound = registry.binding(context.sessionID) - // Its stored attachment first: the Blender it used before an OpenCode restart. - if (!bound && attachments.has(context.sessionID)) { - await attachments.restore(context.sessionID) - bound = registry.binding(context.sessionID) - } - if (!bound) { - // Its Blender crashed or closed: said, and no other Blender attaches itself in its place. - const gone = goneFor(context.sessionID) - if (gone) throw new Error(gone) - const offers = registry.hosts().offers - const only = offers.length === 1 ? offers[0] : undefined - if ( - only && - only.protocol === "ok" && - !heldElsewhere(only.offerID, context.sessionID) && - registry.current(authority) - ) { - if (registry.attach(only.offerID, context.sessionID).outcome === "attached") { - bound = registry.binding(context.sessionID) - await attachments.remember(context.sessionID) - } - } - if (!bound) { - if (!offers.length) - throw new Error("No Blender is offered: open Blender 5.3 or later with the OpenCode Link add-on enabled.") - const list = offers - .map( - (offer) => - `${offer.label} [${offer.offerID}]${heldElsewhere(offer.offerID, context.sessionID) ? " (attached to another conversation)" : ""}${offer.protocol === "incompatible" ? " (incompatible add-on)" : ""}`, - ) - .join(", ") - throw new Error( - `No Blender is attached to this conversation. Offered: ${list}. Attach one with blender_hosts attach.`, - ) - } - } - const current = registry.authority() - if (!current) throw new Error("This conversation's ownership is unverifiable: try again.") - await attachMethod(context.sessionID) - return { authority: current, bound, label: attachedTo(context.sessionID)?.label } - } - - const callHost = async ( - context: ToolContext, - method: string, - params: Record, - timeoutMs = options.requestTimeoutMs, - captured?: Ticket, - ) => { - const held = captured ? await reheld(context, captured) : await ticket(context) - if (!held) throw new Error("This conversation's Blender changed while permission was asked; retry the action.") - if (context.signal.aborted || !registry.current(held.authority)) - throw new Error("Application attachment is unverifiable.") - const { bound } = held - if (!bound.methods.includes(method)) { - throw new Error( - `The attached Blender's OpenCode Link has no ${method} method: install OpenCode Link ${pkg.version} (blender_install_host) and restart Blender.`, - ) - } - await remember(context, held) - // A crash ends the wait at once: on Windows a crashed Blender stays open behind its crash window and its - // connection with it, so the call would wait its whole timeout. Its crash report is watched meanwhile. - const host = wsl() ? undefined : reached.get(context.sessionID) - const stop = new AbortController() - const abort = () => stop.abort() - context.signal.addEventListener("abort", abort) - let crashed: string | null = null - const watch = host - ? setInterval(() => { - crashed = crashReport(host) ? closedMessage(host, isAlive) : null - if (crashed) stop.abort() - }, CRASH_POLL_MS) - : undefined - try { - return await requestHost( - bound.endpoint, - method, - { script_handle: bound.scriptHandle, ...params }, - { signal: stop.signal, timeoutMs }, - ) - } catch (error) { - if (crashed) throw new Error(crashed) - const said = - error instanceof HostError && ["unreachable", "timeout", "busy"].includes(error.code) - ? await diagnose(context.sessionID, bound, error.code) - : null - throw said ? new Error(said) : hostError(error) - } finally { - clearInterval(watch) - context.signal.removeEventListener("abort", abort) - } - } - + const hostSession = createHostSession({ + texts: { + appName: "Blender", + app: APP, + part: "OpenCode Link", + partKind: "add-on", + noneOffered: "No Blender is offered: open Blender 5.3 or later with the OpenCode Link add-on enabled.", + installHint: `install OpenCode Link ${pkg.version} (blender_install_host) and restart Blender`, + }, + registry, + bindings, + attachments, + attachMethod, + crash: { crashReport, closedMessage, silentMessage }, + wsl, + requestTimeoutMs: options.requestTimeoutMs, + hostError, + }) + const { attachedTo, ticket, callHost, hello } = hostSession // A fixed program through the host's code execution, read only (programs.ts). const programRun = async (context: ToolContext, name: ProgramName, params: unknown, timeoutMs?: number) => { const out = (await callHost( @@ -499,51 +296,14 @@ export async function setupBlender(ctx: Plugin.Context, deps: BlenderDeps = {}) (args: Record, context: ToolContext) => Promise > = { async blender_hosts(args, context) { - const out: Record = {} - // Only a conversation of this Location sees and changes its attachment (RPC-12). - const owned = await ownership(context.sessionID, context.signal) - if (!owned) throw new Error("This conversation's ownership is unverifiable: try again.") - if (args.detach === true) { - await attachments.forget(context.sessionID) // first: the detach's change event must not restore it - const authority = await ownership(context.sessionID, context.signal) - if (!authority || !registry.current(authority)) - throw new Error("This conversation's ownership is unverifiable: try again.") - out.detached = registry.detach(context.sessionID).outcome - reached.delete(context.sessionID) // detached on purpose: its Blender closing later is no news here - helloed.delete(context.sessionID) - } - if (typeof args.attach === "string") { - const offer = registry.hosts().offers.find((candidate) => candidate.offerID === args.attach) - if (!offer) throw new Error(`No offered Blender ${String(args.attach)}: call blender_hosts again.`) - await assert(context, PERMISSIONS.attach, [offer.label], { label: offer.label }) - // After the permission: this conversation still owned, the chosen offer still the same Blender. - const authority = await ownership(context.sessionID, context.signal) - const still = registry.hosts().offers.find((candidate) => candidate.offerID === offer.offerID) - if (!authority || !registry.current(authority) || !still || still.label !== offer.label) - throw new Error("This conversation or the chosen Blender changed while permission was asked: try again.") - out.attached = registry.attach(offer.offerID, context.sessionID).outcome - if (out.attached === "attached") { - await attachments.remember(context.sessionID) - await attachMethod(context.sessionID) - if (heldElsewhere(offer.offerID, context.sessionID)) - out.shared = - "Another conversation is attached to this Blender too: both change the same document, and each call is checked against its revision." - } - } - void legacy.refresh() - const status = await rpc.status() - const mine = registry.binding(context.sessionID)?.offerID - out.attached_to = attachedTo(context.sessionID) - out.host = status.host - out.blenders = registry.hosts().offers.map((offer) => ({ - ...offer, - attached: - offer.offerID === mine - ? "this conversation" - : heldElsewhere(offer.offerID, context.sessionID) - ? "another conversation" - : null, - })) + const out = await hostSession.hosts(args, context, { + allow: (held, offer) => assert(held, PERMISSIONS.attach, [offer.label], { label: offer.label }), + status: async () => { + void legacy.refresh() + return rpc.status() + }, + list: "blenders", + }) return json(out) }, @@ -813,27 +573,8 @@ export async function setupBlender(ctx: Plugin.Context, deps: BlenderDeps = {}) ) // Renews or forgets the stored attachments and attaches back the conversations whose Blender offers itself - // again: one run at a time, and once more when the offers changed during it. - let running: Promise | undefined - let again = false - const maintain = (): Promise => { - if (running) { - again = true - return running - } - running = (async () => { - do { - again = false - try { - await attachments.sweep() - await attachments.restore() - } catch {} - } while (again) - })().finally(() => { - running = undefined - }) - return running - } + // again (the host session's maintain). + const { maintain } = hostSession const registrations: Array<{ dispose: () => Promise }> = [] let disconnect = () => {} diff --git a/integrations/blender/plugin/src/method.ts b/integrations/blender/plugin/src/method.ts index dc60fc7..e651c3f 100644 --- a/integrations/blender/plugin/src/method.ts +++ b/integrations/blender/plugin/src/method.ts @@ -1,6 +1,8 @@ // The working method, attached once to a conversation when it attaches a Blender (a native instructions // entry, not a skill): what to check before writing code, and how to verify a step. +import { type EntryPut, methodAttacher as kitAttacher, resetMethodForTests as resetKit } from "./kit.ts" + export const METHOD_KEY = "blender.method" export const METHOD_TEXT = [ "Working on the open Blender:", @@ -14,26 +16,11 @@ export const METHOD_TEXT = [ "- Write down what you discover (a recent change, a silent failure) in your report, with the Blender version.", ].join("\n") -export type EntryPut = (input: { sessionID: string; key: string; value: string }) => Promise - -// Sessions the method is attached to, per daemon process (OpenCode loads the plugin once per Location). -const shared = globalThis as { __neuralnomadsBlenderMethod?: Set } -shared.__neuralnomadsBlenderMethod ??= new Set() -const attached = shared.__neuralnomadsBlenderMethod +export type { EntryPut } -// A failure never fails the Blender call: the method is tried again at the next one. -export function methodAttacher(put: EntryPut | undefined) { - return async (sessionID: string) => { - if (!put || !sessionID || attached.has(sessionID)) return - attached.add(sessionID) - try { - await put({ sessionID, key: METHOD_KEY, value: METHOD_TEXT }) - } catch { - attached.delete(sessionID) - } - } -} +// The kit attaches it once per conversation; a failure is tried again at the next Blender call. +export const methodAttacher = (put: EntryPut | undefined) => kitAttacher(put, { key: METHOD_KEY, text: METHOD_TEXT }) export function resetMethodForTests() { - attached.clear() + resetKit(METHOD_KEY) } diff --git a/integrations/blender/plugin/src/tasks.ts b/integrations/blender/plugin/src/tasks.ts index 670dff5..6bdebdb 100644 --- a/integrations/blender/plugin/src/tasks.ts +++ b/integrations/blender/plugin/src/tasks.ts @@ -4,6 +4,8 @@ import { closeSync, existsSync, openSync, readSync, statSync } from "node:fs" import { basename } from "node:path" +import { isAlive } from "./kit.ts" + export type RenderStart = { pid: number log: string @@ -37,14 +39,7 @@ export function addTask(start: RenderStart): RenderTask { export const getTask = (id: string) => tasks.get(id) export const listTasks = () => [...tasks.values()] -export function isAlive(pid: number) { - try { - process.kill(pid, 0) - return true - } catch (error) { - return (error as NodeJS.ErrnoException).code === "EPERM" - } -} +export { isAlive } function readTail(path: string) { if (!existsSync(path)) return "" diff --git a/integrations/gaffer/plugin/host/gaffer/opencode_link-0.2.0.zip b/integrations/gaffer/plugin/host/gaffer/opencode_link-0.2.0.zip index 173e7f1b531ee622249ec1a142f53ef047cf45a8..fad9e8a4e8867a014f806d6072e4d02404e89443 100644 GIT binary patch delta 11691 zcmbVyX?Psfm1xc#+jt>1jJ!x*rKT;q?CVBm3+Z-QNUhdl?NV#EESI{grINd=H9oqMYn*(UGD z(+}Oc%Q^SlbI*SJ*r5d{4lTI$2hg6F7kT%g{7ud9wN0!q0XIS;`!E3;VU%4x1!^KMPCF@3dssrQQ;6iHmly+H2s_xmzsx z(kn`6ExZ02xVvS_wM`f4rO8mw;* zw)C|&1p|Hf(OT2k;On#~d-+AkXZAEKoj=KU4#sG|vomzD$EM-#<^8r-9UnGK+mgGi zq~sq}%))lQw29q1ja;y28OZx?n?9sM^5WgEKJ zm2CLthF%vnbh)R;wRNlIq8ocD|BAS1vxT2cD($vjcEJ)bSacV3UOgduY-&r&&e!XASaay4`5bd#hWrYs+D7~_Ybn#ZBHv6w#PzWYlvm1(F zf|czCx6|aY#yv2bU9%hJ|HFjG#ftX8V)o!}C|HKQ`m&ma^i8YYAS*ap__)FZ2 zeYOYYBLse5gd4889#*YF!1A`5x(WyN)~)NE^71Q@;^3Z`*oWm{ccQyuaU zSC5v8LwIMfv^VJt<<8xkOs^8?u;-kJ<3SYLoaI@p2=B~`17tSp8I@72dgrWjn6~f+ zSXLeN=JpVa|IS7kPNa9r;Z1MF;x|CP5R(gUf+g(!8{j-VF7m#B9lHS*tiUsOwv;tg z1<$xZhY-BB#nV&cQ=L;=x2|*OcGjLn__{Pl&qmanlu#0~a_9a3nWt${n!yf4d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ey%dYr)whvf;ZLhp+)l2|58+@*2@T_qq|{4t2glemx0CDf)$B?n z4#sC4caShX);)g*IYj&_3*Sj3gmB%^|go4H< z@50pi-GLB1cln_PvzHv9N)av=i3p;4z<#}3~^uAKFq&kF7(->9mR#~nXXhL15}m1agO;LgZEt zqcBSzRXm!FarLk7CF|$)^Y2Cr_rZPS;?qzv?n5o^XZPNRn%mFby$`hmL+bm9ds$pb zMoc5_(z#`cST2!dN7=UfNdP^<9ru$8y6qsTw4w15@BUopvP|zsV=j=xZiytEL*ZHuh^4CvK53tlB zy!!xq{}9=GSqENF$f_Y_io#)@h%`FB*z!nH7=6FRtNj5KG1(|C?qUx>ail)&UNAZeDtmu1W8LNAW%unt6F4;X7E!D!O$hx_; zSk%JFC&^MixivkEMEt>1sJDZuS9t?|_PeJ^K2)c^cp8n|!XQQs!7ZCh&}YU1!x2;*m9-xsCD`0>gzR?47aT>l2AFgd&C52{dK68-6x)B4wBlR# z2S>?#fL8XO&yhROB=3HnJc+%FUm&-`K>ZiY8gFAhO*$;znmn0t#f1n zOB|>Qo<=VUplWl+O;HxKDHU}DWv3nIc$#NsNz#sqW}1{4`LAZ{eo7v6aVbZ-Bnb%G+1knA-kx5}GsXXV^daCEEK#>>F>A4d-lsoqX+UU;Do~R^CSd diff --git a/integrations/gaffer/plugin/package.json b/integrations/gaffer/plugin/package.json index 43bfc0d..73444c0 100644 --- a/integrations/gaffer/plugin/package.json +++ b/integrations/gaffer/plugin/package.json @@ -61,7 +61,7 @@ "range": "^0.2.0", "appVersions": ">=1.6", "payload": "host/gaffer/opencode_link-0.2.0.zip", - "sha256": "4db41db5c28953af8d56266e9f49b62d8ebf2ded7136e6b9051cf4b72ed2bf29" + "sha256": "b704233e3f45b9f7be2894bf878341162018c9c7cb978a578a6b184e63ebc9b6" } } } diff --git a/integrations/houdini/host/manifest.json b/integrations/houdini/host/manifest.json index a1c0d2a..9669212 100644 --- a/integrations/houdini/host/manifest.json +++ b/integrations/houdini/host/manifest.json @@ -1 +1 @@ -{ "name": "OpenCode Link", "version": "0.2.1", "protocol": 1, "license": "MIT", "appVersions": ">=21.0" } +{ "name": "OpenCode Link", "version": "0.3.0", "protocol": 1, "license": "MIT", "appVersions": ">=21.0" } diff --git a/integrations/houdini/host/scripts/opencode_houdini/__init__.py b/integrations/houdini/host/scripts/opencode_houdini/__init__.py index 6988755..4051ee0 100644 --- a/integrations/houdini/host/scripts/opencode_houdini/__init__.py +++ b/integrations/houdini/host/scripts/opencode_houdini/__init__.py @@ -1,50 +1,100 @@ +"""OpenCode Link for Houdini: the installed part. + +Started with Houdini's interface by the package's pythonrc.py (start_with_houdini): it listens on 127.0.0.1 and +offers this Houdini to the OpenCode services every 5 seconds, so any conversation (CodeNomad, the terminal, the +OpenCode Link panel) can attach it. OPENCODE_HOUDINI_OFFER=0 in Houdini's environment keeps it from offering +itself. Background hython (verification, renders) never starts it by itself. +""" + from __future__ import annotations import atexit +import os from .host import Host, HostError, HostServer, ScriptState _host: Host | None = None _server: HostServer | None = None +_advertiser = None _panel_owners: set[str] = set() -def initialise(application=None) -> None: - global _host, _server +def _snapshot(): + host, server = _host, _server + if host is None or server is None or not server.running: + return None, None, None, None + return server.port, server.token, host.state.handle, host.label + + +def initialise(application=None, advertise: bool | None = None) -> None: + """Starts the host once per Houdini process; with advertise (by default unless OPENCODE_HOUDINI_OFFER=0 or a + background role), offers it to OpenCode.""" + global _host, _server, _advertiser if _host is not None: return host = Host() server = HostServer(host) _host = host _server = server + if advertise is None: + advertise = os.environ.get("OPENCODE_HOUDINI_OFFER", "1") != "0" and not os.environ.get("OPENCODE_HOUDINI_ROLE") + if advertise: + from .advertiser import Advertiser # noqa: PLC0415 + + _advertiser = Advertiser(_snapshot).start() + + +def start_with_houdini() -> None: + """pythonrc.py: in Houdini's interface, start once its event loop runs (never in hython).""" + import hou # noqa: PLC0415 + + if not hou.isUIAvailable() or os.environ.get("OPENCODE_HOUDINI_ROLE"): + return + + def once() -> None: + try: + hou.ui.removeEventLoopCallback(once) + except Exception: # noqa: BLE001 + pass + initialise() + + hou.ui.addEventLoopCallback(once) + + +def pump(seconds: float = 0.05, stop=None) -> None: + """hython: serve the queued requests on the main thread (Houdini's interface serves them by itself).""" + if _host is not None: + _host.ui.pump(seconds, stop) def shutdown() -> None: - global _host, _server - if _server is not None: - _server.close() + global _host, _server, _advertiser + advertiser, server = _advertiser, _server + token = server.token if server is not None else None + if advertiser is not None: + advertiser.stop(token) + if server is not None: + server.close() if _host is not None: _host.close() + _advertiser = None _server = None _host = None _panel_owners.clear() def acquirePanel(channel: str) -> None: + """A panel uses the host (started with Houdini; started here when Houdini was started without it).""" initialise() _panel_owners.add(channel) def releasePanel(channel: str) -> None: - if channel not in _panel_owners: - return - _panel_owners.remove(channel) - if not _panel_owners: - shutdown() + """A panel closed: the host and its offer stay, for the other conversations attached to this Houdini.""" + _panel_owners.discard(channel) -# Process exit also cleans up any remaining consenting panels. atexit.register(shutdown) @@ -59,6 +109,15 @@ def hostInfo() -> dict: return _server.info() +def offerInfo() -> dict: + """What the panel needs to find this Houdini's offer among the offered hosts.""" + from .advertiser import OFFER_KEY # noqa: PLC0415 + + if _host is None: + raise RuntimeError("OpenCode Houdini host has not been initialised") + return {"label": _host.label, "offer_key": OFFER_KEY, "advertising": _advertiser is not None} + + def scriptHandle(script=None) -> str: if _host is None: raise RuntimeError("OpenCode Houdini host has not been initialised") @@ -80,8 +139,11 @@ def currentState() -> ScriptState: "currentState", "hostInfo", "initialise", + "offerInfo", + "pump", "reset", "releasePanel", "scriptHandle", "shutdown", + "start_with_houdini", ] diff --git a/integrations/houdini/host/scripts/opencode_houdini/advertiser.py b/integrations/houdini/host/scripts/opencode_houdini/advertiser.py new file mode 100644 index 0000000..8811ce0 --- /dev/null +++ b/integrations/houdini/host/scripts/opencode_houdini/advertiser.py @@ -0,0 +1,97 @@ +"""Offers this Houdini to the discovered OpenCode services (standard library only). + +Runs on its own thread: discovery and RPC calls never block Houdini's main thread. One offer per service, +renewed every 5 seconds; stopping withdraws it from every service, and an offer whose renewals stop expires +within 15 seconds anyway. Results carry service file names and outcomes only, never the daemon password nor +the host token. +""" + +from __future__ import annotations + +import os +import re +import secrets +import threading +from typing import Any, Callable + +RPC_ID = "neuralnomads.houdini" +PROTOCOL = 1 +RENEW_SECONDS = 5.0 + + +def _launch_key() -> str | None: + """A Houdini started by OpenCode offers itself under the key OpenCode chose.""" + key = os.environ.get("OPENCODE_HOUDINI_OFFER_KEY", "") + return key if re.fullmatch(r"houdini-launch-[0-9a-f]{32}", key) else None + + +# Stable for the life of the Houdini process: the service recognises a renewed or re-announced offer. +OFFER_KEY = _launch_key() or "houdini-%d-%s" % (os.getpid(), secrets.token_hex(8)) + + +class Advertiser: + """`snapshot()` returns (port, token, script_handle, label), or a None token while not ready.""" + + def __init__(self, snapshot: Callable[[], tuple[Any, Any, Any, Any]], env: dict[str, str] | None = None, + interval: float = RENEW_SECONDS) -> None: + self._snapshot = snapshot + self._env = env + self._interval = interval + self._stop = threading.Event() + self._lock = threading.Lock() + self._results: list[dict[str, Any]] = [] + self._thread = threading.Thread(target=self._loop, name="OpenCodeHoudiniAdvertiser", daemon=True) + + def start(self) -> "Advertiser": + self._thread.start() + return self + + @property + def results(self) -> list[dict[str, Any]]: + with self._lock: + return list(self._results) + + def advertise_once(self) -> list[dict[str, Any]] | None: + port, token, script_handle, label = self._snapshot() + if token is None: + return None + offer = { + "host": "127.0.0.1", + "port": port, + "token": token, + "script_handle": script_handle, + "label": label, + "protocol": PROTOCOL, + "offer_key": OFFER_KEY, + } + from . import opencode_service # noqa: PLC0415 - the kit's client, vendored into the payload + + try: + results = opencode_service.advertise_all(RPC_ID, offer, env=self._env) + except (OSError, ValueError): + results = [] + with self._lock: + self._results = results + return results + + def _loop(self) -> None: + while not self._stop.is_set(): + try: + self.advertise_once() + except Exception: # noqa: BLE001 - the next renewal tries again + pass + self._stop.wait(self._interval) + + def stop(self, token: str | None, withdraw: bool = True) -> list[dict[str, Any]]: + """Stops renewing; withdraws the offer from every service with the token it was announced with.""" + from . import opencode_service # noqa: PLC0415 + + self._stop.set() + if self._thread.is_alive() and self._thread is not threading.current_thread(): + self._thread.join(timeout=opencode_service.TIMEOUT * 2) + if not withdraw or token is None: + return [] + try: + return opencode_service.withdraw_all(RPC_ID, OFFER_KEY, token, env=self._env) + except (OSError, ValueError): + return [] diff --git a/integrations/houdini/host/scripts/opencode_houdini/hints.py b/integrations/houdini/host/scripts/opencode_houdini/hints.py new file mode 100644 index 0000000..8e1493a --- /dev/null +++ b/integrations/houdini/host/scripts/opencode_houdini/hints.py @@ -0,0 +1,351 @@ +"""Hints for a failed execute_code run, computed inside Houdini in the except block, while the failing frames live. + +explain(error, code, filename) returns a few lines saying what Houdini really offers where the code went wrong, or +None. Each hint reads only: node types and parameters of existing nodes, errors() and warnings(), docstrings and +dir() of hou classes. Expressions from the failing line are never evaluated, except a plain name (or a dotted name +through hou objects and modules) found in the frame and hou.node(""). +""" + +from __future__ import annotations + +import difflib +import re +import traceback +from typing import Any, Iterable + +MAX_CHARS = 1500 +MAX_ITEMS = 40 +USER_FILES = ("",) + +_RECEIVER = r"(hou\s*\.\s*node\(\s*(['\"])([^'\"]+)\2\s*\)|[A-Za-z_][\w]*(?:\s*\.\s*[A-Za-z_]\w*)*)" +# Groups: 1 receiver (2, 3: a hou.node literal), 4 method, 5 quote, 6 name. +_LOOKUP = re.compile(_RECEIVER + r"\s*\.\s*(parm|parmTuple|node|evalParm|evalParmTuple)\(\s*(['\"])([^'\"]*)\5\s*\)") +_HOU_NODE = re.compile(r"hou\s*\.\s*node\(\s*(['\"])([^'\"]+)\1\s*\)") +# Groups: 1 receiver, 4 quote, 5 type name. +_CREATE = re.compile(_RECEIVER + r"\s*\.\s*createNode\(\s*(['\"])([^'\"]*)\4") +_SWIG = re.compile(r"(?:in method|overloaded function) '(\w+?)_(\w+)'") + + +def _hou() -> Any: + try: + import hou # noqa: PLC0415 - imported where Houdini runs; tests use a stub + except ImportError: + return None + return hou + + +def closest(name: str, candidates: Iterable[str], n: int = 5) -> list[str]: + """The candidates closest to name: difflib matches (case-insensitive), then those containing it.""" + pool = sorted({str(c) for c in candidates if c}) + if not name or not pool: + return [] + lower = {c.lower(): c for c in pool} + found = [lower[c] for c in difflib.get_close_matches(name.lower(), list(lower), n=n, cutoff=0.5)] + needle = name.lower() + found += [c for c in pool if c not in found and (needle in c.lower() or (len(c) > 2 and c.lower() in needle))] + return found[:n] + + +def _bounded(names: Iterable[str], limit: int = MAX_ITEMS) -> str: + items = list(names) + shown = ", ".join(items[:limit]) + return shown + (" ... (%d more)" % (len(items) - limit) if len(items) > limit else "") + + +def _frames(error: BaseException) -> list[tuple[Any, int]]: + return list(traceback.walk_tb(error.__traceback__)) + + +def _user_frames(frames: list[tuple[Any, int]], filename: str | None) -> list[tuple[Any, int]]: + names = set(USER_FILES) | ({filename} if filename else set()) + return [(f, n) for f, n in frames if f.f_code.co_filename in names] + + +def _line(code: str | None, frames: list[tuple[Any, int]], filename: str | None) -> tuple[Any, str]: + """The innermost user frame and its source line (from the submitted code).""" + users = _user_frames(frames, filename) + if not users or not code: + return (users[-1][0] if users else None), "" + frame, number = users[-1] + lines = code.splitlines() + return frame, (lines[number - 1] if 0 < number <= len(lines) else "") + + +def _resolve(text: str, frame: Any) -> Any: + """A receiver from the failing line, only when reading it has no effect: hou.node(""), a name in the + frame, or a dotted name whose intermediate objects are hou objects or modules.""" + hou = _hou() + if hou is None or frame is None: + return None + literal = _HOU_NODE.fullmatch(text.strip()) + if literal: + try: + return hou.node(literal.group(2)) + except Exception: # noqa: BLE001 + return None + parts = [p.strip() for p in text.split(".")] + scope = dict(frame.f_globals) + scope.update(frame.f_locals) + if parts[0] not in scope: + return None + value = scope[parts[0]] + for part in parts[1:]: + kind = type(value) + if not (kind.__module__ == "hou" or type(value).__name__ == "module"): + return None + try: + # A hou method is not called: only an attribute that is a plain value or object is followed. + value = getattr(value, part) + except Exception: # noqa: BLE001 + return None + if callable(value) and type(value).__name__ != "module": + return None + return value + + +def _is_node(value: Any) -> bool: + hou = _hou() + return hou is not None and isinstance(value, getattr(hou, "OpNode", getattr(hou, "Node", ()))) + + +def _missing_path(path: str) -> str: + """For a hou.node(path) that found nothing: the deepest existing ancestor and its closest children.""" + hou = _hou() + parts = [p for p in path.split("/") if p] + for depth in range(len(parts) - 1, -1, -1): + parent = hou.node("/" + "/".join(parts[:depth])) + if parent is not None: + names = [c.name() for c in parent.children()] + near = closest(parts[depth], names) + return "hou.node(%r) is None: %s has no child %r%s; its children: %s" % ( + path, parent.path(), parts[depth], (" (closest: %s)" % ", ".join(near)) if near else "", + _bounded(names) or "none") + return "hou.node(%r) is None" % path + + +def _parm_hint(line: str, frame: Any, attribute: str) -> str | None: + """A .parm("x") (or parmTuple, node, hou.node) that gave None before .attribute.""" + hits = [] + for match in _LOOKUP.finditer(line): + receiver, method, name = match.group(1), match.group(4), match.group(6) + rest = line[match.end():].lstrip() + if attribute and not rest.startswith("." + attribute) and not rest.startswith(". " + attribute): + hits.append((False, receiver, method, name)) + else: + hits.insert(0, (True, receiver, method, name)) + for _, receiver, method, name in hits: + literal = _HOU_NODE.fullmatch(receiver.strip()) + node = _resolve(receiver, frame) + if literal and node is None: + return _missing_path(literal.group(2)) + if not _is_node(node): + continue + if method == "node": + try: + target = node.node(name) + except Exception: # noqa: BLE001 + target = None + if target is not None: + continue + names = [c.name() for c in node.children()] + near = closest(name.split("/")[-1], names) + return "%s.node(%r) is None%s; children of %s: %s" % ( + receiver, name, (" (closest: %s)" % ", ".join(near)) if near else "", node.path(), _bounded(names) or "none") + if method in ("parm", "evalParm"): + if node.parm(name) is not None: + continue + names = [p.name() for p in node.parms()] + tuples = [t.name() for t in node.parmTuples()] + near = closest(name, names) + lines = ["%s has no parameter %r (%s.%s returned None)%s" % ( + node.path(), name, receiver, method, (": closest %s" % ", ".join(near)) if near else "")] + if name in tuples: + lines.append("%r is a parameter tuple: use parmTuple(%r) or one of %s" % ( + name, name, ", ".join(p.name() for p in node.parmTuple(name)))) + lines.append("parameters of %s (%s): %s" % (node.path(), node.type().nameWithCategory(), _bounded(names))) + return "\n".join(lines) + if method in ("parmTuple", "evalParmTuple"): + if node.parmTuple(name) is not None: + continue + tuples = [t.name() for t in node.parmTuples()] + near = closest(name, tuples) + lines = ["%s has no parameter tuple %r%s" % (node.path(), name, (": closest %s" % ", ".join(near)) if near else "")] + if node.parm(name) is not None: + lines.append("%r is a single parameter: use parm(%r)" % (name, name)) + lines.append("parameter tuples of %s: %s" % (node.path(), _bounded(tuples))) + return "\n".join(lines) + for match in _HOU_NODE.finditer(line): + hou = _hou() + if hou.node(match.group(2)) is None: + return _missing_path(match.group(2)) + return None + + +def _category_hint(parent: Any, wanted: str) -> str: + hou = _hou() + category = parent.childTypeCategory() + if category is None: + return "%s holds no nodes (it is not a network)" % parent.path() + types = category.nodeTypes() + near = closest(wanted, types.keys()) + by_label = [name for name, t in types.items() if t.description().lower() == wanted.lower()] + lines = ["%s (%s network) has no node type %r%s" % ( + parent.path(), category.name(), wanted, (": closest %s" % ", ".join(dict.fromkeys(by_label + near))) if near or by_label else "")] + elsewhere = [name for name, other in hou.nodeTypeCategories().items() if other != category and wanted in other.nodeTypes()] + if elsewhere: + lines.append("%r is a type of %s; %s takes %s types%s" % ( + wanted, "/".join(elsewhere), parent.path(), category.name(), + " (SOPs go inside a geo object: hou.node('/obj').createNode('geo'))" if "Sop" in elsewhere and category.name() == "Object" else "")) + return "\n".join(lines) + + +def _create_hint(error: BaseException, frames: list[tuple[Any, int]], line: str, frame: Any) -> str | None: + for f, _ in reversed(frames): + if f.f_code.co_name == "createNode" and _is_node(f.f_locals.get("self")): + wanted = f.f_locals.get("node_type_name") + if wanted is None: + args = f.f_locals.get("args") or () + wanted = args[0] if args else None + if isinstance(wanted, str): + return _category_hint(f.f_locals["self"], wanted) + match = _CREATE.search(line) + if match: + parent = _resolve(match.group(1), frame) + if _is_node(parent): + return _category_hint(parent, match.group(5)) + return None + + +def _cook_hint(frames: list[tuple[Any, int]], line: str, frame: Any) -> str | None: + hou = _hou() + nodes = [] + for f, _ in reversed(frames): + value = f.f_locals.get("self") + if _is_node(value): + nodes.append(value) + break + if not nodes: + match = re.search(_RECEIVER + r"\s*\.\s*(cook|geometry|stage|render|cookWorkItems)\(", line) + node = _resolve(match.group(1), frame) if match else None + if _is_node(node): + nodes.append(node) + if not nodes and frame is not None: + nodes = [v for v in frame.f_locals.values() if _is_node(v)][:8] + seen, lines = set(), [] + networks = [] + for node in nodes: + try: + parent = node.parent() + except Exception: # noqa: BLE001 + continue + if parent is not None and parent.path() not in seen: + seen.add(parent.path()) + networks.append(parent) + candidates = list(nodes) + for node in nodes: + try: + candidates += list(node.inputAncestors())[:200] + except Exception: # noqa: BLE001 + pass + for net in networks: + candidates += list(net.children())[:500] + done = set() + for node in candidates: + try: + key = node.path() + if key in done: + continue + done.add(key) + errors, warnings = node.errors(), node.warnings() + except Exception: # noqa: BLE001 + continue + if errors or warnings: + text = "; ".join(" ".join(m.split()) for m in list(errors)[:2]) or "" + if warnings and not errors: + text = "warning: " + "; ".join(" ".join(m.split()) for m in list(warnings)[:2]) + lines.append("%s (%s): %s" % (key, node.type().name(), text[:300])) + if len(lines) >= 8: + break + if not lines: + return None + return "nodes in error or warning:\n" + "\n".join(lines) + + +def _signature(function: Any, label: str) -> str | None: + doc = getattr(function, "__doc__", None) or "" + lines = [l.rstrip() for l in doc.strip("\n").splitlines()] + head = [] + for l in lines: + if not l.strip() and head and len(head) > 1: + break + if l.strip(): + head.append(l.strip()) + if len(head) >= 6: + break + return ("%s:\n%s" % (label, "\n".join(head))) if head else None + + +def _type_error_hint(error: BaseException, frames: list[tuple[Any, int]]) -> str | None: + hou = _hou() + match = _SWIG.search(str(error)) + if match: + cls = getattr(hou, match.group(1), None) + fn = getattr(cls, match.group(2), None) if cls is not None else getattr(hou, match.group(2), None) + if fn is not None: + return _signature(fn, "hou.%s.%s" % (match.group(1), match.group(2))) + for f, _ in reversed(frames): + name = f.f_code.co_filename.replace("\\", "/") + if not name.endswith("/hou.py"): + continue + owner = f.f_locals.get("self") + fn = getattr(type(owner), f.f_code.co_name, None) if owner is not None else getattr(hou, f.f_code.co_name, None) + label = ("hou.%s.%s" % (type(owner).__name__, f.f_code.co_name)) if owner is not None else "hou.%s" % f.f_code.co_name + if fn is not None: + return _signature(fn, label) + return None + + +def _attribute_hint(error: AttributeError) -> str | None: + hou = _hou() + obj, name = getattr(error, "obj", None), getattr(error, "name", None) + if obj is None or not name: + return None + if obj is hou: + near = closest(name, [n for n in dir(hou) if not n.startswith("_")]) + return ("hou has no %r: closest %s" % (name, ", ".join(near))) if near else None + if type(obj).__module__ != "hou": + return None + names = [n for n in dir(type(obj)) if not n.startswith("_")] + near = closest(name, names) + lines = ["hou.%s has no %r%s" % (type(obj).__name__, name, (": closest %s" % ", ".join(near)) if near else "")] + if not near: + lines.append("its methods: %s" % _bounded(names, 60)) + return "\n".join(lines) + + +def explain(error: BaseException, code: str | None = None, filename: str | None = None) -> str | None: + """A few lines on what Houdini really offers where the run failed, or None.""" + hou = _hou() + if hou is None: + return None + frames = _frames(error) + frame, line = _line(code, frames, filename) + message = str(error) + hint = None + operation_failed = getattr(hou, "OperationFailed", ()) + if isinstance(error, operation_failed) and "Invalid node type name" in message: + hint = _create_hint(error, frames, line, frame) + elif isinstance(error, AttributeError) and message.startswith("'NoneType' object has no attribute"): + hint = _parm_hint(line, frame, getattr(error, "name", None) or message.rsplit("'", 2)[-2]) + elif isinstance(error, AttributeError): + hint = _attribute_hint(error) + elif isinstance(error, TypeError): + hint = _type_error_hint(error, frames) + if hint is None and ( + (isinstance(error, operation_failed) and "cook" in message.lower()) + or (isinstance(error, getattr(hou, "Error", ())) and any(f.f_code.co_name in ("cook", "cookWorkItems", "render") for f, _ in frames)) + ): + hint = _cook_hint(frames, line, frame) + if not hint: + return None + return hint if len(hint) <= MAX_CHARS else hint[: MAX_CHARS - 4] + " ..." diff --git a/integrations/houdini/host/scripts/opencode_houdini/host.py b/integrations/houdini/host/scripts/opencode_houdini/host.py index ee9bca9..a4c8b1f 100644 --- a/integrations/houdini/host/scripts/opencode_houdini/host.py +++ b/integrations/houdini/host/scripts/opencode_houdini/host.py @@ -11,11 +11,16 @@ import secrets import socket import threading +import os +import queue +import time import uuid from typing import Any, Callable import hou +from . import runner + PROTOCOL_VERSION = 1 MAX_REQUEST_BYTES = 1024 * 1024 JOURNAL_LIMIT = 256 @@ -28,7 +33,11 @@ "graph.connect", "graph.delete", ) -METHODS = ("list_scripts", "hello", "context", "sync", "apply", "cancel", "resume") +METHODS = ("list_scripts", "hello", "context", "sync", "apply", "cancel", "resume", "execute_code", "release", "status") +# How long a request waits for Houdini's main thread to take it before answering busy (a modal dialog, a long +# cook or render), and the longest main-thread job a request waits for (the plugin stops waiting earlier). +QUEUE_WAIT_S = 10.0 +RUN_LIMIT_S = 3600.0 NODE_TYPE_REGISTRY = { "Object": frozenset(("cam", "envlight", "geo", "hlight", "light", "null", "subnet")), "Sop": frozenset(("box", "grid", "line", "merge", "null", "output", "sphere", "subnet", "xform")), @@ -95,6 +104,26 @@ def capability_hash(): return hashlib.sha256(json.dumps(capabilities(), sort_keys=True, separators=(",", ":")).encode("utf-8")).hexdigest() +def offer_label(document: str | None) -> str: + """The offer's label: "Houdini: ", as the user knows the open scene.""" + name = (document or "untitled.hip").replace("/", " ").replace("\\", " ") + name = "".join(char for char in name if ord(char) >= 32 and char != "\x7f").strip() or "untitled.hip" + return ("Houdini: %s" % name)[:120] + + +def temp_dir() -> str: + """Where Houdini writes its crash reports: $HOUDINI_TEMP_DIR (by default /houdini_temp).""" + try: + found = hou.getenv("HOUDINI_TEMP_DIR") + except Exception: # noqa: BLE001 - a stub hou in tests + found = None + if not found: + import tempfile # noqa: PLC0415 + + found = os.path.join(tempfile.gettempdir(), "houdini_temp") + return os.path.normpath(found) + + def host_version(): return json.loads((Path(__file__).resolve().parents[2] / "manifest.json").read_text(encoding="utf-8"))["version"] @@ -206,6 +235,7 @@ def __init__(self, root: hou.OpNode | None = None) -> None: self.epoch = str(uuid.uuid4()) self.revision = 1 self.quarantined = False + self.quarantine: dict[str, Any] | None = None self._refs_by_key: dict[tuple[Any, ...], str] = {} self._objects_by_ref: dict[str, hou.OpNode | hou.Parm] = {} self._tombstones: collections.OrderedDict[str, dict[str, Any]] = collections.OrderedDict() @@ -246,6 +276,8 @@ def hello(self) -> dict[str, Any]: "host_version": host_version(), "app_version": hou.applicationVersionString(), "document": hou.hipFile.basename()[:120], + # This process and the folder Houdini writes its crash reports to (crash..__log.txt). + "process": {"pid": os.getpid(), "temp_dir": temp_dir()}, "capabilities": capabilities(), "operations": list(OPERATIONS), "node_types": {category: sorted(types) for category, types in NODE_TYPE_REGISTRY.items()}, @@ -350,6 +382,7 @@ def add_node(subject: Any) -> None: "entities": entities, "working_set": entities, "diagnostics": ["Remote writes are quarantined"] if self.quarantined else [], + **({"quarantine": self.quarantine or {"cause": "a failed change"}} if self.quarantined else {}), "truncated": truncated, } if "affordances" in include: @@ -410,7 +443,7 @@ def apply( cancellation_generation = self.cancellation_state(session_id)[0] self._check_cancelled(session_id, cancellation_generation, request_was_cancelled) if self.quarantined: - raise HostError("script_quarantined", "Remote writes are disabled until the HIP is reloaded") + raise HostError("script_quarantined", runner.QUARANTINED, {"quarantine": self.quarantine or {}}) command_id = str(request.get("command_id", "")) if not command_id or len(command_id) > 128: @@ -489,6 +522,7 @@ def apply( rollback_error = RuntimeError("The OpenCode undo item was not available") if rollback_error is not None: self.quarantined = True + self.quarantine = {"cause": ("failed apply: %s" % failure)[:200], "undo_error": str(rollback_error)[:300]} self._reconcile_after_rollback() self.flush(cause="rollback_failed", command_id=command_id) raise HostError( @@ -1144,48 +1178,107 @@ def callback() -> None: class UIThreadGate: + """Runs requests on Houdini's main thread, and says which runs. + + In the interface, through Houdini's event loop; in hython, through pump(), which the main thread calls (hou + records no undo off the main thread: undos.areEnabled() is false there). A request not taken within + QUEUE_WAIT_S is answered busy. `running()` answers from any thread: the request the main thread runs now and + for how long, so a call whose wait ended can learn that it still runs. + """ + def __init__(self) -> None: self._ui_thread = threading.get_ident() - - def run(self, callback: Callable[[], Any]) -> Any: - if not hou.isUIAvailable() or not hasattr(hou, "ui") or threading.get_ident() == self._ui_thread: + self._running: tuple[str, float] | None = None + self._queued = 0 + self._count = threading.Lock() + self._jobs: "queue.Queue[Callable[[], None]]" = queue.Queue() + + def running(self) -> dict[str, Any]: + current = self._running + if current is None: + return {"busy": False, "queued": self._queued} + label, since = current + return {"busy": True, "running": label, "seconds": round(time.monotonic() - since, 1), "queued": self._queued} + + def _invoke(self, label: str, callback: Callable[[], Any]) -> Any: + self._running = (label, time.monotonic()) + try: return callback() + finally: + self._running = None + + def pump(self, seconds: float = 0.05, stop: Callable[[], bool] | None = None) -> None: + """hython: runs the queued requests on this (the main) thread for `seconds`, or until `stop()`.""" + deadline = time.monotonic() + seconds + while time.monotonic() < deadline and not (stop is not None and stop()): + try: + job = self._jobs.get(timeout=min(0.05, max(0.0, deadline - time.monotonic()))) + except queue.Empty: + continue + job() + + def run(self, callback: Callable[[], Any], label: str = "a request") -> Any: + if threading.get_ident() == self._ui_thread: + return self._invoke(label, callback) + started = threading.Event() complete = threading.Event() expired = threading.Event() + lock = threading.Lock() result: list[Any] = [] def invoke() -> None: - if expired.is_set(): - complete.set() - return + with lock: + if expired.is_set(): + return + started.set() try: - result.append((True, callback())) + result.append((True, self._invoke(label, callback))) except Exception as error: result.append((False, error)) finally: complete.set() - if hasattr(hou.ui, "postEventCallback"): - hou.ui.postEventCallback(invoke) - else: - def event_loop_callback() -> None: - hou.ui.removeEventLoopCallback(event_loop_callback) - invoke() - - hou.ui.addEventLoopCallback(event_loop_callback) - if not complete.wait(120): - expired.set() - raise HostError("ui_timeout", "Timed out waiting for Houdini's UI thread") + with self._count: + self._queued += 1 + try: + if not hou.isUIAvailable() or not hasattr(hou, "ui"): + self._jobs.put(invoke) + elif hasattr(hou.ui, "postEventCallback"): + hou.ui.postEventCallback(invoke) + else: + def event_loop_callback() -> None: + hou.ui.removeEventLoopCallback(event_loop_callback) + invoke() + + hou.ui.addEventLoopCallback(event_loop_callback) + if not started.wait(QUEUE_WAIT_S): + with lock: + if not started.is_set(): + expired.set() + raise HostError( + "ui_timeout", + "Houdini's main thread did not take the request within %d s (a modal dialog, a long " + "cook or render)" % QUEUE_WAIT_S, + {"running": self.running().get("running")}, + ) + finally: + with self._count: + self._queued -= 1 + if not complete.wait(RUN_LIMIT_S): + raise HostError("ui_timeout", "Houdini did not finish the request within %d s" % RUN_LIMIT_S) succeeded, value = result[0] if not succeeded: raise value return value - class Host: def __init__(self) -> None: self.ui = UIThreadGate() self.state = ScriptState() + # Saves and loads seen (hip events): an execute_code run that saved or opened a file says so. + self.saves = 0 + self.loads = 0 + self.label = offer_label(hou.hipFile.basename()) hou.hipFile.addEventCallback(self._hip_event) def close(self) -> None: @@ -1203,6 +1296,9 @@ def script_handle(self, script: Any = None) -> str: return self.state.handle def dispatch(self, method: str, params: dict[str, Any]) -> Any: + if method == "status": + # Answered on the request's own thread, never through the main thread: what it runs now. + return self.ui.running() if method in ("cancel", "resume"): state = self._require_state(params) session_id = params.get("session_id") @@ -1235,9 +1331,15 @@ def execute() -> Any: return state.sync(params.get("after_revision", 0)) if method == "apply": return state.apply(params, cancellation[0], cancellation[1]) + if method == "execute_code": + from . import hints # noqa: PLC0415 - Houdini's own knowledge, loaded on first use + + return runner.execute_code(self, state, params, hints.explain) + if method == "release": + return runner.release(state, params) raise HostError("unknown_method", "Unknown Houdini host method: " + method) - return self.ui.run(execute) + return self.ui.run(execute, method) def _require_state(self, params: dict[str, Any]) -> ScriptState: if str(params.get("script_handle", "")) != self.state.handle: @@ -1246,10 +1348,18 @@ def _require_state(self, params: dict[str, Any]) -> ScriptState: def _hip_event(self, event_type: hou.hipFileEventType) -> None: if event_type in (hou.hipFileEventType.AfterLoad, hou.hipFileEventType.AfterClear): + self.loads += 1 self.reset() elif event_type in (hou.hipFileEventType.AfterSave, hou.hipFileEventType.AfterMerge): + if event_type == hou.hipFileEventType.AfterSave: + self.saves += 1 self.state._summary_dirty = True self.state._schedule_flush() + # On the main thread: the offer's label follows the open file (the advertiser reads it from its thread). + try: + self.label = offer_label(hou.hipFile.basename()) + except Exception: # noqa: BLE001 + pass class HostServer: diff --git a/integrations/houdini/host/scripts/opencode_houdini/panel.py b/integrations/houdini/host/scripts/opencode_houdini/panel.py index fe9f53d..60f5478 100644 --- a/integrations/houdini/host/scripts/opencode_houdini/panel.py +++ b/integrations/houdini/host/scripts/opencode_houdini/panel.py @@ -43,10 +43,9 @@ def __init__(self, parent=None) -> None: def start(self) -> None: if self._process.state() != QtCore.QProcess.ProcessState.NotRunning: return - bun = os.environ.get("NEURALNOMADS_HOUDINI_NODE") or shutil.which("node") + node = os.environ.get("NEURALNOMADS_HOUDINI_NODE") or shutil.which("node") sidecar = str(Path(__file__).resolve().parents[2] / "sidecar.cjs") - if not bun: - _log("bun missing") + if not node: self.diagnostic.emit("Install Node.js 22 or set NEURALNOMADS_HOUDINI_NODE") return if not Path(sidecar).is_file(): @@ -55,8 +54,7 @@ def start(self) -> None: "OpenCode Link sidecar is missing; reinstall the application part." ) return - _log("starting sidecar bun=" + bun + " sidecar=" + sidecar + " cwd=" + str(Path(sidecar).resolve().parent.parent)) - self._process.setProgram(bun) + self._process.setProgram(node) self._process.setArguments([sidecar]) self._process.setWorkingDirectory(str(Path(sidecar).resolve().parent.parent)) self._process.start() @@ -229,8 +227,13 @@ def _connect_params(self) -> dict: file_name = hou.hipFile.path() is_untitled = bool(hou.hipFile.isNewFile()) or not file_name or Path(file_name).name == "untitled.hip" directory = os.getcwd() if is_untitled else str(Path(file_name).resolve().parent) + offer = opencode_houdini.offerInfo() return { **info, + # This Houdini offers itself (OpenCode Link starts with Houdini): the panel finds that offer by its + # label and attaches its own conversation to it, which it takes back per file next time. + "offer_label": offer["label"], + "sessions_file": str(Path(__file__).resolve().parents[3].parent / "panel-sessions.json"), "script_handle": opencode_houdini.scriptHandle(), "script_key": "untitled:" + opencode_houdini.scriptHandle() if is_untitled else str(Path(file_name).resolve()), "script_name": hou.hipFile.basename(), @@ -255,6 +258,7 @@ def _new_session(self) -> None: self._client.send(self._channel, "new_session", self._connect_params()) def _disconnect(self) -> None: + # The host and its offer stay: other conversations attached to this Houdini keep it. opencode_houdini.releasePanel(self._channel) self._client.send(self._channel, "close", {}) self._connected = False diff --git a/integrations/houdini/host/scripts/opencode_houdini/runner.py b/integrations/houdini/host/scripts/opencode_houdini/runner.py new file mode 100644 index 0000000..d768f44 --- /dev/null +++ b/integrations/houdini/host/scripts/opencode_houdini/runner.py @@ -0,0 +1,191 @@ +"""execute_code and release: Python run in Houdini, grouped as one undo step, undone when it fails. + +A run that changes the scene is one undo group ("OpenCode: code"). When it raises after a change, the group is +undone and the node graph compared with its state before the run: restored, or the scene is quarantined (changes +refused, reads and read_only runs allowed) until the agent read it and releases it with that read's revision. +read_only runs (the plugin's fixed programs, inspections) make no undo group: a change they make is reported. +Hints (hints.py) are computed in the except block, before anything is undone, while the failing frames live. +""" + +from __future__ import annotations + +import contextlib +import hashlib +import io +import json +import os +import traceback +from typing import Any + +import hou + +STDOUT_MAX = 64 * 1024 +TRACE_MAX = 16 * 1024 +FILENAME_MAX = 1024 +QUARANTINED = ( + "A failed change could not be proven undone: changes are refused until you read the scene (houdini_context) " + "and release the quarantine with that read's revision (houdini_context release_quarantine)." +) + + +class _Capture(io.StringIO): + """stdout or stderr of one run, bounded.""" + + def write(self, text: str) -> int: + if self.tell() < STDOUT_MAX * 2: + return super().write(text) + return len(text) + + +def fingerprint(root: Any) -> str: + """The node graph's structure: every node's path, type and inputs (what an undone change must give back).""" + digest = hashlib.sha256() + try: + nodes = root.allSubChildren(recurse_in_locked_nodes=False) + except TypeError: + nodes = root.allSubChildren() + for node in nodes: + try: + inputs = [connected.path() if connected is not None else "" for connected in node.inputs()] + digest.update(("%s|%s|%s\n" % (node.path(), node.type().nameWithCategory(), ",".join(inputs))).encode()) + except hou.ObjectWasDeleted: + continue + return digest.hexdigest() + + +def _json_repr(value: Any) -> Any: + return json.loads(json.dumps(value, default=repr)) + + +def _open_file_mtime() -> float | None: + """The open file's date on disk: a save writes it; a copy (hscript mwrite -n, the save and verify programs) + fires Houdini's save events too, but leaves it as it was.""" + try: + path = hou.hipFile.path() + return os.path.getmtime(path) if path and os.path.isfile(path) else None + except Exception: # noqa: BLE001 - a stub hou in tests, a path being written + return None + + +def _undo_labels() -> tuple[str, ...]: + try: + return tuple(hou.undos.undoLabels()) + except Exception: # noqa: BLE001 - undo unavailable + return () + + +def execute_code(host: Any, state: Any, params: dict[str, Any], explain: Any = None) -> dict[str, Any]: + from .host import HostError # noqa: PLC0415 - host imports this module + + code = params.get("code") + if not isinstance(code, str) or not code.strip(): + raise HostError("invalid_request", "code is required") + read_only = params.get("read_only") is True + if state.quarantined and not read_only: + raise HostError("script_quarantined", QUARANTINED, {"quarantine": state.quarantine or {}}) + filename = params.get("filename") + if filename is not None and ( + not isinstance(filename, str) or not 0 < len(filename) <= FILENAME_MAX or any(ord(c) < 32 for c in filename) + ): + raise HostError("invalid_request", "filename must be a file path of at most 1024 characters") + namespace: dict[str, Any] = {"hou": hou, "__name__": "__opencode__", "result": {}} + if filename: + namespace["__file__"] = filename + state.flush() + revision_before = state.revision + loads = host.loads + written = _open_file_mtime() + labels_before = _undo_labels() + before = None if read_only else fingerprint(state.root) + out, err = _Capture(), _Capture() + error = trace = hint = None + group = contextlib.nullcontext() if read_only else hou.undos.group("OpenCode: code") + with group: + try: + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + exec(compile(code, filename or "", "exec"), namespace) # noqa: S102 + except BaseException as exc: # noqa: BLE001 - SystemExit from supplied code must not quit Houdini + error = ("%s: %s" % (type(exc).__name__, exc))[:2000] + trace = traceback.format_exc()[-TRACE_MAX:] + if explain is not None: + try: + hint = explain(exc, code, filename) + except Exception: # noqa: BLE001 - a hint never hides the error + hint = None + replaced = host.loads > loads or host.state is not state + now_written = _open_file_mtime() + saved = now_written is not None and now_written != written + output: dict[str, Any] = {} + restored = None + if replaced: + # The run opened or cleared a file: the old scene's undo group means nothing for the new one. + changed = True + current = host.state + else: + current = state + # A change is to nodes (created, removed, edited) or an undo item; a selection, or the summary a save or a + # copy refreshes, is none. + touched = bool(getattr(state, "_dirty_nodes", None) or getattr(state, "_removed_refs", None)) + state.flush(cause="opencode") + changed = touched or len(_undo_labels()) > len(labels_before) or saved + if error is not None and changed and not read_only: + grew = len(_undo_labels()) > len(labels_before) + rollback = state._rollback(labels_before) if grew else RuntimeError("the run left no undo item to undo") + state._reconcile_after_rollback() + restored = rollback is None and before == fingerprint(state.root) + if not restored: + state.quarantined = True + state.quarantine = { + "cause": ("failed code: %s" % error)[:200], + "release": "read the scene (houdini_context), check what may have changed, then release the " + "quarantine with that read's revision", + **({"undo_error": str(rollback)[:300]} if rollback is not None else {}), + } + state._summary_dirty = True + state.flush(cause="rollback") + output.update( + status="failed" if error else "executed", + script_handle=current.handle, + result=_json_repr(namespace.get("result", {})), + stdout=out.getvalue()[:STDOUT_MAX], + stderr=err.getvalue()[:STDOUT_MAX], + truncated=len(out.getvalue()) > STDOUT_MAX or len(err.getvalue()) > STDOUT_MAX, + revision_before=revision_before, + revision_after=current.revision, + changed=changed, + ) + if changed and not read_only and error is None and not replaced: + output["undo_step"] = "OpenCode: code" + if saved: + output["saved"] = True + if replaced: + output["document_replaced"] = True + if read_only and changed and not replaced: + output["warnings"] = ["OpenCode: this read_only run changed the scene; no undo group holds it."] + if error: + output.update(message=error, traceback=trace) + if hint: + output["hint"] = hint + if restored is not None: + output["restored"] = restored + if not restored: + output["quarantine"] = state.quarantine + return output + + +def release(state: Any, params: dict[str, Any]) -> dict[str, Any]: + from .host import HostError # noqa: PLC0415 + + if not state.quarantined: + return {"released": False, "revision": state.revision} + revision = params.get("revision") + state.flush() + if not isinstance(revision, int) or isinstance(revision, bool) or revision != state.revision: + raise HostError( + "revision_conflict", + "Read the scene again (houdini_context) and release with its revision.", + {"current_revision": state.revision}, + ) + state.quarantined = False + state.quarantine = None + return {"released": True, "revision": state.revision} diff --git a/integrations/houdini/host/scripts/python/pythonrc.py b/integrations/houdini/host/scripts/python/pythonrc.py new file mode 100644 index 0000000..7923090 --- /dev/null +++ b/integrations/houdini/host/scripts/python/pythonrc.py @@ -0,0 +1,8 @@ +# OpenCode Link: offers this Houdini to OpenCode once its interface runs (opencode_houdini.start_with_houdini). +# Houdini runs every pythonrc.py found on HOUDINI_PATH; this one never fails Houdini's startup. +try: + import opencode_houdini + + opencode_houdini.start_with_houdini() +except Exception as error: # noqa: BLE001 + print("OpenCode Link did not start: %s" % error) diff --git a/integrations/houdini/plugin/README.md b/integrations/houdini/plugin/README.md index 1728b37..4af3c7a 100644 --- a/integrations/houdini/plugin/README.md +++ b/integrations/houdini/plugin/README.md @@ -1,43 +1,60 @@ # OpenCode for Houdini (preview) -Inspect and edit an open Houdini graph from any OpenCode V2 client, including the terminal, desktop and CodeNomad. The optional **OpenCode Link** Python panel keeps a conversation inside Houdini. Houdini 21+ with Python 3.11+ and PySide6, Node.js 22+ and OpenCode 2.2+ are required. This preview targets specification draft 0.2. +Read, change and check the scene open in Houdini from any OpenCode conversation: CodeNomad, the terminal, the desktop app, or the **OpenCode Link** panel inside Houdini. Houdini 21 or later and OpenCode 2.2 or later are required; the panel also needs Node.js 22 or later. -## Install in any OpenCode client +## Install -Add the integration to the **Global** OpenCode configuration only: +Add the plugin to the **global** OpenCode configuration (in CodeNomad: from the reviewed catalogue, in Plugins): ```jsonc { "$schema": "https://opencode.ai/config.json", - "plugins": [ - { - "package": "@neuralnomads/opencode-houdini@^0.2", - "options": { "hostInspectionDir": "/your/Houdini/user/preferences" } - } - ] + "plugins": [{ "package": "@neuralnomads/opencode-houdini@^0.3" }] } ``` -Use the existing Houdini user preferences folder (`hou.getenv("HOUDINI_USER_PREF_DIR")`), for example `Documents/houdini21.0` on Windows, `~/houdini21.0` on Linux or `~/Library/Preferences/houdini/21.0` on macOS. Set `hostInspectionDir` explicitly; the integration never guesses a Houdini version or creates a preference directory. `NEURALNOMADS_HOUDINI_HOST_INSPECTION_DIR` is a fallback. Omit it to keep host state unknown until a live host advertises. +Then install OpenCode Link, the part that runs inside Houdini: `houdini_install_host` (it asks permission), or CodeNomad's install action. It goes into the newest Houdini 21+ preferences folder found (`Documents/houdini22.0` on Windows, `~/houdini22.0` on Linux, `~/Library/Preferences/houdini/22.0` on macOS); `hostInspectionDir` in the plugin's options names another one. Houdini creates that folder at its first start: start Houdini once before installing. The installation writes `packages/neuralnomads_houdini.json`, which points at a checksummed copy of OpenCode Link under `%LOCALAPPDATA%/NeuralNomads/opencode-houdini/hosts/` (`$XDG_STATE_HOME/neuralnomads/opencode-houdini/hosts/` elsewhere); installing again is idempotent. WSL installation is refused. -Call `houdini_install_host` and approve its named permission. A client can also invoke RPC `neuralnomads.houdini.installHost` after its explicit confirmation. Installation uses the embedded, checksummed archive offline: the stable, content-addressed payload goes under `%LOCALAPPDATA%/NeuralNomads/opencode-houdini/hosts/` on Windows, or `$XDG_STATE_HOME/neuralnomads/opencode-houdini/hosts/` (default `~/.local/state/…`) elsewhere. Houdini's `packages/neuralnomads_houdini.json` references this stable copy, never an npm cache path. Repeating installation is idempotent. WSL installation is refused. +Restart Houdini: OpenCode Link starts with Houdini's interface and offers it to OpenCode every 5 seconds, labelled with the open file (`Houdini: shot.hipnc`). `OPENCODE_HOUDINI_OFFER=0` in Houdini's environment keeps it from offering itself. Background `hython` never offers itself. -Open Houdini and choose the **OpenCode Link** Python panel. The host starts only when you choose **Connect** (or **New**), never at plugin load, Houdini startup or panel creation. Node.js is found on PATH, with `NEURALNOMADS_HOUDINI_NODE` as an override. Start OpenCode yourself first: the panel discovers the existing authenticated shared service without starting one. Connect opens a new conversation and offers the host for other clients. Disconnecting or closing a panel closes its conversation channel and withdraws its offer. Python Panel teardown (`onDestroyInterface`), widget close and destruction share idempotent cleanup. The shared host stays available while another consenting panel remains. Disconnecting or closing the last consenting panel immediately closes the listener and invalidates its token; offers and bindings expire within 15 seconds even if withdrawal fails. Opening or clearing a HIP disconnects the panel. After service failure or explicit detachment, choose Connect again; polling never recreates a binding. +An earlier OpenCode installation (`packages/opencode_houdini.json` in a Houdini preferences folder) loads another panel and host next to OpenCode Link: `houdini_hosts` and CodeNomad's status say so. Remove that file, or set `"enable": false` in it, then restart Houdini. -Outside the panel, use `houdini_hosts` then `houdini_attach` with permission. `houdini_context` can initially omit its handle to read the attached document's handle and revision. All mutations use `houdini_apply`, the exact revision, an idempotent command ID and 1–50 operations. Start with dry-run; changing the graph requires `dry_run: false`, `confirm: true` and `houdini_write` permission. `expect` contains preconditions checked before execution; operation results are verified afterwards. The `houdini-pipeline` reference skill is loaded by the plugin without replacing a user skill. +## Use -## In CodeNomad +The first Houdini tool call of a conversation attaches the only Houdini offered when no other conversation uses it; otherwise `houdini_hosts` lists the Houdinis offered and `houdini_hosts attach` attaches one (permission `houdini_attach`). An attachment is kept across an OpenCode restart and when Houdini opens another file. A working method joins the conversation when it first uses a Houdini. -Add the integration from the reviewed catalogue (preview), then use its Status section to install the application part, list user-offered hosts and attach the displayed conversation. The same status RPC contract supplies host versions, live offers and exact-session attachment authority. Installation runs on the OpenCode daemon's machine. Credentials, ports and document handles stay out of status outputs and events. +| Tool | | +| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `houdini_hosts` | The Houdinis offered, which conversation holds each; `attach`, `detach`. | +| `houdini_context` | A bounded read: revision, open file, selection, node cards; `release_quarantine` after a failed change. | +| `houdini_execute_code` | Python in Houdini (`hou`), as code or a script file; permission `houdini_execute_code` on every run. | +| `houdini_inspect` | Search node types, parms and `hou` symbols; a type with its parm templates and its SideFX documentation link; a live node (parms, errors, geometry, USD stage, work items). | +| `houdini_verify` | Measured criteria, node errors and warnings, an optional low-resolution render; `saved: true` reads the saved file in a background `hython`. | +| `houdini_save` | Save the open file, or a copy that leaves the open file's name. | +| `houdini_render` | A ROP render or a PDG cook in a background `hython`, followed as a task (`task`, `wait_s`, `cancel`). | +| `houdini_apply` | Typed graph edits (`node.create`, `node.rename`, `parm.set_value`, `graph.connect`, `graph.delete`); dry run by default, changes with `confirm` and permission `houdini_write`, in the interface only. | +| `houdini_install_host` | Install or update OpenCode Link. | -## Scope, safety and qualification +A code run that changes the scene is one undo group (`OpenCode: code`); when it fails, the group is undone and the node graph compared with what it was. When that cannot be proven, changes are refused (reads and checks still run) until the agent reads the scene and releases the quarantine with that read's revision. A failure says what Houdini knows: the closest node types in the parent's network, the real parm names when `parm()` returned `None`, the errors of the nodes that failed to cook, a function's signature, the closest attributes. -- Supported operations: `node.create`, `node.rename`, `parm.set_value` (numeric/boolean), `graph.connect`, `graph.delete`. Node creation uses the host's bounded type registry. No arbitrary code tool, parameter expressions, string writes, renders, file saving or network import operations are exposed. Existing scenes, node assets and their callbacks remain trusted application content. -- In UI mode, undo is `grouped_best_effort`: one undo group, operation verification, rollback attempt, quarantine on failed restoration. Headless mode declares `none` and refuses mutation. Actual HOM undo behaviour still needs licensed, interactive qualification. -- Command replay results are retained for the HIP lifetime. After 256 successful commands the host refuses new commands until the HIP is reloaded, preserving idempotence without eviction. Reusing a command ID for a different request is refused. -- The host binds only `127.0.0.1` on an ephemeral port, authenticates every request with a process-random 256-bit token and invalidates it on shutdown. Tokens exist only in memory and private pipes. The panel sidecar bundles its client dependencies inside the host archive; the plugin's compiled ESM keeps the OpenCode SDK external. -- System-Python tests exercise the actual listener and protocol with a stub HOM boundary, plus RPC sentinel, ownership, expiry, installation and refusal cases. Python Panel callback tests cover two consenting panels through teardown to one and zero owners, repeated teardown and widget-close fallback. Real Houdini Apprentice 22.0.429 qualifies the same sequence with isolated preferences and a private OpenCode service: offers and exact-session bindings are withdrawn, the final listener refuses connections and its old token is rejected. -- Scripted `hou.exit()` in Houdini 22.0.429 can segfault after closing Python panels. The same crash occurs with plain PySide6 label panels and the integration absent; the integration's listener and host threads are already stopped before exit. -- **QO5 — CI licence decision:** maintainers must choose whether to provide an already licensed self-hosted Houdini runner. Recommendation: keep public CI licence-free and the integration preview until a maintainer performs licensed read-only/interactive validation; add an opt-in self-hosted job only when credentials, licence terms and runner isolation are approved. Public CI must never download Houdini or attempt licence activation. +A call that outlasts its wait says whether Houdini still runs it (OpenCode Link answers `status` off Houdini's main thread). When Houdini crashes, the call running then stops within two seconds and says so, with the files Houdini wrote in `houdini_temp`: the scene as it was at the crash (`crash.._.hipnc`), from which the changes can be recovered, or its crash log. A Houdini closed, or alive without renewing its offer, is said as such, and no other Houdini attaches itself in its place. -Publication uses the repository's manually approved OIDC workflow and `houdini-v` tags. Building or installing never publishes anything. +## The OpenCode Link panel + +The **OpenCode Link** Python panel is a conversation inside Houdini. **Connect** attaches the panel's conversation to this Houdini's own offer, taking back the conversation it had for this file; **New** starts another. Closing the panel releases its conversation only: Houdini stays offered and other conversations attached to it keep it. Both write to the same scene, each change checked against its revision. Node.js is found on `PATH`; `NEURALNOMADS_HOUDINI_NODE` names another one. + +## Limits + +- Houdini Apprentice: files are non-commercial, renders are at most 1920×1080 and carry the Houdini logo; render results say so. +- `houdini_apply` refuses changes in `hython` (no interface); `houdini_execute_code` works in both. Houdini records undo only on its main thread: OpenCode Link runs every request there (in `hython`, a script serves them with `opencode_houdini.pump()`). +- `hou.hipFile.hasUnsavedChanges()` stays true after a save in `hython`: OpenCode Link judges a save by the file's date on disk. + +## Safety + +OpenCode Link listens on `127.0.0.1` only, on an ephemeral port, and authenticates every request with a process-random 256-bit token that lives in memory and private pipes only. Credentials, ports and document handles stay out of status outputs and events. Loading the plugin opens no socket and starts no process. + +## Tests + +System Python runs OpenCode Link's listener, code runs, undo, quarantine and `status` with a stub `hou`, and the hints against a stub. With `HOUDINI_TEST_BIN` naming `hython` (or Houdini's `bin` folder), `test/programs_hython.py` runs the programs, the hints and OpenCode Link itself in a real Houdini, with private preferences, packages and temp folders. + +Publication uses the repository's manually approved OIDC workflow and `houdini-v` tags. diff --git a/integrations/houdini/plugin/host/houdini/opencode_link-0.2.1.zip b/integrations/houdini/plugin/host/houdini/opencode_link-0.3.0.zip similarity index 79% rename from integrations/houdini/plugin/host/houdini/opencode_link-0.2.1.zip rename to integrations/houdini/plugin/host/houdini/opencode_link-0.3.0.zip index fcc09c6b2c0edca063ee4aec12e4556b83a38d92..961d77383789d489dd601e4419a152fae850d476 100644 GIT binary patch delta 45018 zcmb__3zQVsdFBiw^k@kLVgP|aDjFfG(M-c2kmNxF9>GYAg#a-K#TiCTPj}5si=OUQ zcMmWIT+U%%{Mg&S{^?a+UHo@%RQ%Sauu!PfD(ylc-=_hw{7d z-+))Fl{59Jsi4_HpOuy!&uiCZ3a}vK&ui7u$-IYe zxYH`OEA_DE`JC|4`fRb>C;b5Q*-EWgm2Vl>t6-#2tOeDaS1Q)LV!K@|%>dZEckKAc z(SyehjT8Qvr)6!@BM*5_(vZ$=R2`GB}GRVfkZTA$YpYGq6z=hZk#56jYQONT(X z=_G^o%w(dE-M0Id%509V>Px7~nVVY>adBSgD%D3xh1K@ygl(9^c3if>NOB;1M@a??=_lrkWy=(lDO;6^MUAcFjbtZw!OJp z6-};920%|y<}?{EYqMV69OU|R>1E|W#+KOffGm1$<;Giqb~|MB(uyCxowx(?-L>c6 zwop%faqP%QHfJfoj2pIs zcwL2>^A4h>XDDyjk(}pk#bD1D2fgD*4({4Bus0+9abj+&LU!>ZzN63_m%9l<&15n_ zzL0&GmLGo3>)$VntWW`y36TTgl4MCIr$Acl81e?o5B{O(0Gn3p^@a!HZF#=(8J<>t z%yEeX^37(7kY7v8E&A@u*~eZdVq-Ka?JH(DPlDw~tM6YEz4cS8qtAWuo*Q{3mwE4P z-5XZNqdt0MWY?~NOmu8u!%9l^N)d82Ahx2NU-J6(8O2;6&TGQKvhooGg6u{9G^xk>3pPp#@YyMw5MJi$G3$rWq8=o^QA(r3!?T zh1F)M7F0?J9;45$*t$u?On$DCPlBevQ+S?qE}DnE%=d}9Cp2~ZY9^H==wG!Rzz}5{D_@aR#oO21 zQLeN~#b((rK@?L*g63+gz8*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ytXuS==Il1ZrmV&8#jLOHTmP$MX<9(wI)-J*#T`QXq$WOCp4LciWyp0*~wH{(=Dz# zrZvg-w8LtLHkQPAEXwvo-K&44W28~BCk>U2qz%>4O@k@g1jl{ZScOrx8&2u8e#%Oz z@~pgDmi+45ov*=!X*=F`hIIo|HY$7b_Pj1-TJvcIt^aTa2ty+4@iDL)R zlJMvmx_Em&WKtRCNzct66wFhW>6ogCid6-XIWoen_!yt`jPd3B@5yP+l($zKw}{f{ znh<<{o`xS%Ri1LN@K{R=R-v6vI^8l7Q#HpqYwGG^5sMoDGj>!~k zx(RMEFP%mCO}OvAPYJ2XZ3V%T(ZZ0VDC=4WU!0+C zZH6ajXvI_~%NxtOmdkpcIy(!wYThlaY-jqo$I7^2YCN8=BiTaQnd4ix4)#T*sB9;7 zm3yXlNB8W^h4N|jf4zeRcQ`gfrz>)h%oY(GI8P&RW|~Uy-82n{4J}txj7dI&Iy_8s zfOSCZ?=-Th$(P?ElMk3|LP_cx@<=Q6&77wkL27#qEk~b1cH!80ss?9gp$mvu>;jeH z*B9uqra9$5H@|E)=|>OK8u#^o&^yVpIRN_wgPpvSjo`7%-LEdvy9mgi&=z;%6M9!s zc@BU=6u0UUeWwTxUZsoSv(IT?kd0$zl~E3f%XEwTm&>$zf&cdcAACXghi?A0z@uN% zf$+Su*gKwQuR}{<*EQM^p4awgwccRkcb-K9+;N@Ohi~>$5-6Q#_H5VDf6?0VARhm% MMS;L?R#H^|7qMC*rvLx| diff --git a/integrations/houdini/plugin/methods/tools/houdini_apply.json b/integrations/houdini/plugin/methods/tools/houdini_apply.json new file mode 100644 index 0000000..dfce119 --- /dev/null +++ b/integrations/houdini/plugin/methods/tools/houdini_apply.json @@ -0,0 +1,29 @@ +{ + "name": "houdini_apply", + "domain": "tools", + "description": "A revision-checked graph intent of 1 to 50 typed operations: node.create, node.rename, parm.set_value (numbers and booleans), graph.connect, graph.delete. Dry run by default; dry_run false with confirm true applies it, with permission, as one undo group (restored on failure). expect: preconditions. Needs Houdini's interface (refused in hython). For anything else, houdini_execute_code.", + "kind": "host-backed", + "risk": "write", + "args": { + "if_revision": { "type": "integer", "required": true, "description": "The revision of your last houdini_context." }, + "command_id": { + "type": "string", + "required": true, + "description": "Idempotent ID: a replay applies nothing twice." + }, + "summary": { "type": "string" }, + "operations": { + "type": "array", + "items": { "type": "object" }, + "description": "{op, ...}: node.create {parent, type, name?, bind?}, node.rename {target, name}, parm.set_value {target, value}, graph.connect {source, destination, input_index?}, graph.delete {target}. $alias names a node created earlier in the intent." + }, + "expect": { + "type": "array", + "items": { "type": "object" }, + "description": "{subject, predicate: exists, name_equals, value_equals, connected_to, value?}" + }, + "dry_run": { "type": "boolean", "default": true }, + "confirm": { "type": "boolean", "default": false } + }, + "execution": { "strategy": "host_method", "method": "apply" } +} diff --git a/integrations/houdini/plugin/methods/tools/houdini_context.json b/integrations/houdini/plugin/methods/tools/houdini_context.json new file mode 100644 index 0000000..6e90d95 --- /dev/null +++ b/integrations/houdini/plugin/methods/tools/houdini_context.json @@ -0,0 +1,27 @@ +{ + "name": "houdini_context", + "domain": "tools", + "description": "Bounded read of the attached Houdini scene: script_handle, revision, the open file, selection, and node cards (path, type, flags, inputs; values with include values). refs, match (glob on name and type) and radius pick the nodes; without them, the selection or the top-level nodes. Call first; pass revision to houdini_apply. quarantined: changes wait until release_quarantine with this read's revision.", + "kind": "host-backed", + "risk": "read", + "args": { + "refs": { "type": "array", "items": { "type": "string" }, "description": "Up to 50 node or parm refs or paths." }, + "match": { "type": "object", "description": "{name, type}: glob patterns." }, + "radius": { "type": "integer", "description": "Neighbours of the picked nodes, 0 to 2.", "default": 0 }, + "include": { + "type": "array", + "items": { "type": "string" }, + "description": "values (parm values), affordances (the apply operations and node types)." + }, + "limit": { "type": "integer", "description": "Nodes listed, 1 to 100.", "default": 20 }, + "if_revision": { + "type": "integer", + "description": "The changes since that revision, when the journal still holds them." + }, + "release_quarantine": { + "type": "integer", + "description": "After a failed change that could not be proven undone: the revision of the read that showed the scene, once you checked it; lifts the quarantine." + } + }, + "execution": { "strategy": "host_method", "method": "context" } +} diff --git a/integrations/houdini/plugin/methods/tools/houdini_execute_code.json b/integrations/houdini/plugin/methods/tools/houdini_execute_code.json new file mode 100644 index 0000000..2a6039e --- /dev/null +++ b/integrations/houdini/plugin/methods/tools/houdini_execute_code.json @@ -0,0 +1,19 @@ +{ + "name": "houdini_execute_code", + "domain": "tools", + "description": "DANGEROUS: run Python in the attached Houdini (hou), given as code or as a script file (path). Assign a JSON-able dict to `result`; print() output is returned. A run that changes the scene is one undo group; a failing one is undone, and a failure says what Houdini knows (closest node types or parm names, node errors, the function's signature). Every run asks permission.", + "kind": "host-backed", + "risk": "destructive", + "dangerous": true, + "args": { + "code": { "type": "string", "description": "Python code (or path)." }, + "path": { + "type": "string", + "description": "A .py file to run instead of code, read as is (absolute or relative to the project): long scripts, no escaping." + }, + "read_only": { "type": "boolean", "description": "Inspection: no undo group." }, + "timeout_s": { "type": "integer", "description": "Longest wait in seconds (1 to 3600).", "default": 120 } + }, + "execution": { "strategy": "host_method", "method": "execute_code" }, + "requires": { "hostMethods": ["execute_code"] } +} diff --git a/integrations/houdini/plugin/methods/tools/houdini_hosts.json b/integrations/houdini/plugin/methods/tools/houdini_hosts.json new file mode 100644 index 0000000..d82851c --- /dev/null +++ b/integrations/houdini/plugin/methods/tools/houdini_hosts.json @@ -0,0 +1,12 @@ +{ + "name": "houdini_hosts", + "domain": "tools", + "description": "List the Houdinis offered to OpenCode on this computer (Houdini with OpenCode Link installed offers itself when it starts): label, document, Houdini version, and whether this conversation or another one is attached to each (attached_to: the one this conversation acts on), with the installed part's state. attach: attach one (its offerID) to this conversation, which asks permission; detach: release this conversation's Houdini. With one offered Houdini that no conversation uses, the first Houdini tool call attaches it.", + "kind": "hostless", + "risk": "write", + "args": { + "attach": { "type": "string", "description": "An offerID from this list." }, + "detach": { "type": "boolean", "description": "Release this conversation's Houdini." } + }, + "execution": { "strategy": "plugin" } +} diff --git a/integrations/houdini/plugin/methods/tools/houdini_inspect.json b/integrations/houdini/plugin/methods/tools/houdini_inspect.json new file mode 100644 index 0000000..ec083e5 --- /dev/null +++ b/integrations/houdini/plugin/methods/tools/houdini_inspect.json @@ -0,0 +1,45 @@ +{ + "name": "houdini_inspect", + "domain": "tools", + "description": "Ask THIS Houdini what it really exposes, in one call: search (words found in node type names and descriptions, parameter names and labels of every node type, and hou classes, functions and methods with their signature line; ranked), a node type (description, inputs min/max, outputs, parameter templates with the real parameter names, defaults, ranges and menu items, its HDA library and version, the SideFX page of this version), a live node (type, flags, inputs and outputs, errors/warnings/messages, changed parameters with values and expressions, children; a SOP's or geometry object's cooked geometry: points, prims, vertices, bbox, attributes per class, groups; a LOP's USD stage: prims and types; a TOP's work items; a ROP's output and frame range) and/or a hou symbol (its docstring signature and page). Entries beyond limit are still named. Always says the Houdini version and license. Read only.", + "kind": "host-backed", + "risk": "read", + "args": { + "search": { + "type": "string", + "description": "Words, e.g. \"scatter\", \"point count\", \"create node\"." + }, + "type": { + "type": "string", + "description": "A node type: Category/name (Sop/box, Lop/sphere, Object/geo, Driver/karma, Top/wedge) or a name with category." + }, + "category": { + "type": "string", + "description": "Node type category (Sop, Lop, Object, Driver, Top, Dop, Vop, Cop...): for type without one, or to keep search to it." + }, + "node": { + "type": "string", + "description": "A live node path, e.g. /obj/geo1/scatter1 or /stage/sublayer1." + }, + "hou": { + "type": "string", + "description": "A hou path, e.g. hou.OpNode.createNode, hou.Geometry, hou.node." + }, + "match": { + "type": "string", + "description": "Keep the parameters (and stage prims) matching: a glob on names (size*), or words found in name or label." + }, + "limit": { + "type": "integer", + "description": "Entries in detail (1 to 200); the others are named.", + "default": 40 + } + }, + "execution": { + "strategy": "program", + "program": "programs/inspect.py" + }, + "requires": { + "hostMethods": ["execute_code"] + } +} diff --git a/integrations/houdini/plugin/methods/tools/houdini_install_host.json b/integrations/houdini/plugin/methods/tools/houdini_install_host.json new file mode 100644 index 0000000..f46324e --- /dev/null +++ b/integrations/houdini/plugin/methods/tools/houdini_install_host.json @@ -0,0 +1,9 @@ +{ + "name": "houdini_install_host", + "domain": "tools", + "description": "Install or update OpenCode Link (the version of this plugin) as a Houdini package in the newest Houdini 21 or later preferences folder of the computer that runs OpenCode. Asks permission; restart Houdini afterwards if it is open.", + "kind": "hostless", + "risk": "write", + "args": {}, + "execution": { "strategy": "plugin" } +} diff --git a/integrations/houdini/plugin/methods/tools/houdini_render.json b/integrations/houdini/plugin/methods/tools/houdini_render.json new file mode 100644 index 0000000..1656eb1 --- /dev/null +++ b/integrations/houdini/plugin/methods/tools/houdini_render.json @@ -0,0 +1,62 @@ +{ + "name": "houdini_render", + "domain": "tools", + "description": "Render a ROP (Karma, OpenGL, USD Render, Mantra, a geometry or USD export...) or cook a TOP node or TOP network (PDG) in a separate background hython: the open Houdini stays usable and its file keeps its name. It reads a working copy of the open scene (default) or the file as saved. A ROP renders its frames to its own output, or to the EXACT output you name (shots/shot_####.png gives shot_0001.png...; $F4 also works), with size [w, h] for this render only. Returns a task: call again with task (and wait_s) for progress, or cancel. Relative paths are next to the .hip. An Apprentice license writes images of at most 1920x1080 with the Houdini logo; the result says the size written.", + "kind": "host-backed", + "risk": "write", + "args": { + "rop": { + "type": "string", + "description": "A ROP node path, e.g. /out/karma1." + }, + "top": { + "type": "string", + "description": "A TOP node path, or a TOP network (its display node is cooked)." + }, + "output": { + "type": "string", + "description": "ROP only: the exact output path; several frames need a # run or $F4." + }, + "frames": { + "type": "any", + "description": "ROP only: a frame, [start, end] or [start, end, step] (default: the ROP's own range)." + }, + "size": { + "type": "array", + "items": { + "type": "integer" + }, + "description": "ROP only: [width, height], 4 to 16384 (Karma, OpenGL, USD Render, Mantra)." + }, + "source": { + "type": "string", + "description": "What is rendered.", + "enum": ["copy", "saved"], + "default": "copy" + }, + "overwrite": { + "type": "boolean", + "default": false + }, + "task": { + "type": "string", + "description": "A task from an earlier call: its progress." + }, + "wait_s": { + "type": "integer", + "description": "With task: wait up to this long for it to finish (0 to 3600).", + "default": 0 + }, + "cancel": { + "type": "boolean", + "description": "With task: stop it." + } + }, + "execution": { + "strategy": "program", + "program": "programs/render.py" + }, + "requires": { + "hostMethods": ["execute_code"] + } +} diff --git a/integrations/houdini/plugin/methods/tools/houdini_save.json b/integrations/houdini/plugin/methods/tools/houdini_save.json new file mode 100644 index 0000000..956b033 --- /dev/null +++ b/integrations/houdini/plugin/methods/tools/houdini_save.json @@ -0,0 +1,27 @@ +{ + "name": "houdini_save", + "domain": "tools", + "description": "Save the open scene; with path, save it under that .hip/.hipnc/.hiplc (as the open file, or a copy with copy true: the open scene keeps its name). Relative paths are next to the current file; an existing other file needs overwrite. Says what was written, read from the disk (path, bytes, time), the open file afterwards and the license's file kind. houdini_verify with saved reads the file back.", + "kind": "host-backed", + "risk": "write", + "args": { + "path": { + "type": "string" + }, + "copy": { + "type": "boolean", + "default": false + }, + "overwrite": { + "type": "boolean", + "default": false + } + }, + "execution": { + "strategy": "program", + "program": "programs/save.py" + }, + "requires": { + "hostMethods": ["execute_code"] + } +} diff --git a/integrations/houdini/plugin/methods/tools/houdini_verify.json b/integrations/houdini/plugin/methods/tools/houdini_verify.json new file mode 100644 index 0000000..e8b5fe5 --- /dev/null +++ b/integrations/houdini/plugin/methods/tools/houdini_verify.json @@ -0,0 +1,51 @@ +{ + "name": "houdini_verify", + "domain": "tools", + "description": "Check a result by measurement. criteria: your own acceptance checks, each measured on cooked geometry, USD stages or parameters (at a frame, without moving the playbar) and passed or failed with the measured value. errors (default on): every node with errors or warnings, after cooking the criteria's nodes and the display and render nodes of every geometry object and LOP network. render: a low-resolution image with facts read from its pixels (size, brightness, black, uniform, transparent), made by a separate background hython on a working copy, so a crash there never touches this Houdini. saved: the same checks on the file as written on disk, read by a background hython; check the delivered file before reporting. Nothing in the open scene changes.", + "kind": "host-backed", + "risk": "read", + "args": { + "criteria": { + "type": "array", + "description": "Up to 50 checks: {expr: a read-only Python expression; op: < <= > >= == != approx between in contains; value; tol (approx, default 1e-4); frame (default: the call's); label}. In expr: node(path), geo(path) (cooked hou.Geometry of a SOP, or a geometry object's display SOP), npoints(path), nprims(path), bbox(path).min/.max/.size/.center, attrib_names(path, cls='point'), attrib_values(path, name, cls='point'), parm(path) (a parm or parm tuple, evaluated), stage(path) (a LOP's Usd.Stage), prim(path, prim_path), usd_attr(path, prim_path, name), usd_prims(path, type_name=None), errors(path), warnings(path), frame, hou, math. Sequences compare element by element.", + "items": { + "type": "object" + } + }, + "frame": { + "type": "number", + "description": "Default: the current frame." + }, + "errors": { + "type": "boolean", + "description": "Report the nodes with errors or warnings.", + "default": true + }, + "roots": { + "type": "array", + "items": { + "type": "string" + }, + "description": "Networks the errors are read under (default: /)." + }, + "render": { + "type": "any", + "description": "true, or {camera?, lop?, rop?, renderer?: karma (default) or opengl, size?: longest side 64 to 1024 (default 320), frame?}. Default source: the first camera in /obj, else the display LOP of /stage." + }, + "saved": { + "type": "any", + "description": "true: read the open scene's file as saved on disk (same criteria, errors and render), or a .hip/.hipnc/.hiplc path." + }, + "timeout_s": { + "type": "integer", + "description": "For render and saved: how long the background hython may take (default 300; 540 with saved)." + } + }, + "execution": { + "strategy": "program", + "program": "programs/verify.py" + }, + "requires": { + "hostMethods": ["execute_code"] + } +} diff --git a/integrations/houdini/plugin/package.json b/integrations/houdini/plugin/package.json index 503ad2b..2b47d3e 100644 --- a/integrations/houdini/plugin/package.json +++ b/integrations/houdini/plugin/package.json @@ -1,7 +1,7 @@ { "name": "@neuralnomads/opencode-houdini", - "version": "0.2.1", - "description": "OpenCode for Houdini: bounded graph edits through an authenticated local host, with an optional conversation panel.", + "version": "0.3.0", + "description": "OpenCode for Houdini: read, change and check the open Houdini scene from any OpenCode conversation, with an optional panel inside Houdini.", "license": "MIT", "type": "module", "keywords": [ @@ -23,7 +23,8 @@ }, "files": [ "dist", - "skills", + "methods", + "programs", "host", "README.md", "NOTICE.md", @@ -60,10 +61,10 @@ "host": { "app": "houdini", "protocol": 1, - "range": "^0.2.0", + "range": "^0.3.0", "appVersions": ">=21.0", - "payload": "host/houdini/opencode_link-0.2.1.zip", - "sha256": "25feefc3f10e5820116e0b8c625bb5a66872ad70b0e0369d3944fa44b9676ffc" + "payload": "host/houdini/opencode_link-0.3.0.zip", + "sha256": "ecb899429f413c946970039953142c62fd16c4bfe45aa6b4b413b6969188b4fd" } } } diff --git a/integrations/houdini/plugin/programs/inspect.py b/integrations/houdini/plugin/programs/inspect.py new file mode 100644 index 0000000..21dfddb --- /dev/null +++ b/integrations/houdini/plugin/programs/inspect.py @@ -0,0 +1,638 @@ +# SPDX-License-Identifier: MIT +# houdini_inspect (fixed program, read only): what THIS Houdini really exposes for a node type, a live node or a +# hou symbol, or where words appear among its node types, parameters and hou API, with the SideFX documentation +# of this version. Everything is read from the running Houdini (node types, parm templates, docstrings, the +# cooked geometry or USD stage); nothing is created. +# PARAMS is prepended by the plugin: {"search"?, "type"?, "category"?, "node"?, "hou"?, "match"?, "limit"?} +import difflib +import fnmatch +import inspect as _inspect +import math +import sys +import time +import types + +import hou + +P = PARAMS # noqa: F821 +VERSION = hou.applicationVersion() +VER = "%d.%d" % VERSION[:2] +DOCS = "https://www.sidefx.com/docs/houdini%s/" % VER +LIMIT = max(1, min(200, int(P.get("limit") or 40))) +MATCH = P.get("match") +NAMES_MAX = 300 +VALUE_CHARS = 300 +# Category -> docs folder; categories missing here use their lower-case name. +DOC_DIRS = {"Object": "obj", "Driver": "out", "Sop": "sop", "Lop": "lop", "Top": "top", "Dop": "dop", "Vop": "vop", + "Cop": "copernicus", "Cop2": "cop2", "Chop": "chop", "Shop": "shop", "TopNet": "top", "ChopNet": "chop", + "CopNet": "cop2", "VopNet": "vop", "Data": "data"} +PREFERRED = ("Sop", "Lop", "Object", "Driver", "Top", "Dop", "Cop", "Vop", "Chop", "Cop2", "Shop") + + +def license_name(): + try: + return str(hou.licenseCategory()).rsplit(".", 1)[-1] + except Exception: # noqa: BLE001 + return "unknown" + + +out = {"houdini": {"version": hou.applicationVersionString(), "license": license_name(), + "python": "%d.%d" % sys.version_info[:2], "ui": hou.isUIAvailable()}, + "links": {"docs": DOCS, "hom": DOCS + "hom/hou/index.html", "nodes": DOCS + "nodes/index.html"}} + + +def num(v): + if isinstance(v, float): + return round(v, 6) if math.isfinite(v) else str(v) + return v + + +def plain(v, depth=0): + """A JSON-ready, bounded copy of a parameter value or a hou object.""" + if isinstance(v, (bool, int)) or v is None: + return v + if isinstance(v, float): + return num(v) + if isinstance(v, str): + return v if len(v) <= VALUE_CHARS else v[:VALUE_CHARS] + "... (%d chars)" % len(v) + if hasattr(v, "path") and callable(v.path): + try: + return v.path() + except Exception: # noqa: BLE001 + pass + if depth < 2 and isinstance(v, (list, tuple)) or (depth < 2 and hasattr(v, "__iter__") and type(v).__module__ == "hou"): + try: + items = list(v) + except Exception: # noqa: BLE001 + return repr(v)[:VALUE_CHARS] + return [plain(x, depth + 1) for x in items[:32]] + (["... (%d)" % len(items)] if len(items) > 32 else []) + return repr(v)[:VALUE_CHARS] + + +def matches(*texts): + """match: a glob on the name when it has * ? or [, otherwise words all found in name, label or help.""" + if not MATCH: + return True + if any(c in MATCH for c in "*?["): + return fnmatch.fnmatchcase(texts[0], MATCH) + hay = " ".join(t or "" for t in texts).lower() + return all(w in hay for w in MATCH.lower().split()) + + +def doc_url(node_type): + """The SideFX page of a node type in this version: nodes//<[namespace--]name[-version]>.html.""" + category = node_type.category().name() + scope, ns, name, version = node_type.nameComponents() + page = (ns + "--" if ns else "") + name + ("-" + version if version else "") + return "%snodes/%s/%s.html" % (DOCS, DOC_DIRS.get(category, category.lower()), page) + + +def hom_url(path): + """hou.Node.createNode -> hom/hou/Node.html#createNode; hou.node -> hom/hou/node.html.""" + parts = path.split(".")[1:] + if not parts: + return DOCS + "hom/hou/index.html" + if len(parts) == 1: + return "%shom/hou/%s.html" % (DOCS, parts[0]) + return "%shom/hou/%s.html#%s" % (DOCS, parts[0], parts[1]) + + +SCHEMES = {"XYZW": "xyzw", "RGBA": "rgba", "UVW": "uvw", "MinMax": ["min", "max"], "MaxMin": ["max", "min"], + "StartEnd": ["start", "end"], "BeginEnd": ["begin", "end"], "XYWH": "xywh"} + + +def parm_names(t): + """The real parameter names a template makes (size with XYZW -> sizex, sizey, sizez).""" + n = t.numComponents() if hasattr(t, "numComponents") else 1 + if n <= 1: + return [t.name()] + scheme = str(t.namingScheme()).rsplit(".", 1)[-1] + suffixes = SCHEMES.get(scheme) + if suffixes is None: + return ["%s%d" % (t.name(), i + 1) for i in range(n)] + return [t.name() + s for s in list(suffixes)[:n]] + + +def template_info(t, folder=None): + kind = str(t.type()).rsplit(".", 1)[-1] + d = {"name": t.name(), "label": t.label(), "type": kind} + names = parm_names(t) + if names != [t.name()]: + d["parms"] = names + if folder: + d["folder"] = folder + try: + default = t.defaultValue() + d["default"] = plain(default[0] if isinstance(default, tuple) and len(default) == 1 else default) + except Exception: # noqa: BLE001 + pass + try: + if t.defaultExpression() and any(t.defaultExpression()): + d["default_expression"] = [e for e in t.defaultExpression()][:4] + except Exception: # noqa: BLE001 + pass + for attr in ("minValue", "maxValue"): + try: + d.setdefault("range", []).append(num(getattr(t, attr)())) + except Exception: # noqa: BLE001 + pass + try: + items = list(t.menuItems()) + if items: + labels = list(t.menuLabels()) + d["menu"] = [{"token": i, "label": l} if i != l else i for i, l in zip(items, labels)][:40] + if len(items) > 40: + d["menu_more"] = len(items) - 40 + elif t.itemGeneratorScript(): + d["menu"] = "dynamic (read it from a live node: menuItems() of its parm)" + except Exception: # noqa: BLE001 + pass + if kind == "Folder": + try: + if "Multiparm" in str(t.folderType()): + d["multiparm"] = True + d["instances"] = [c.name() for c in t.parmTemplates()][:20] + except Exception: # noqa: BLE001 + pass + try: + if t.isHidden(): + d["hidden"] = True + except Exception: # noqa: BLE001 + pass + try: + help_text = t.help() + if help_text: + d["help"] = " ".join(help_text.split())[:160] + except Exception: # noqa: BLE001 + pass + return d + + +def walk_templates(group_entries, folder=None): + for t in group_entries: + kind = str(t.type()).rsplit(".", 1)[-1] + if kind == "Folder" and "Multiparm" not in str(getattr(t, "folderType", lambda: "")()): + yield from walk_templates(t.parmTemplates(), ((folder + "/") if folder else "") + t.label()) + elif kind not in ("FolderSet", "Separator", "Label"): + yield t, folder + + +def detailed(items, info, key, d, name_of): + d[key] = [info(i) for i in items[:LIMIT]] + rest = [name_of(i) for i in items[LIMIT:]] + if rest: + d[key + "_more"] = {"count": len(rest), "names": rest[:NAMES_MAX], "details": "pass match (a glob or words) or a larger limit"} + + +def find_types(name, category=None): + cats = hou.nodeTypeCategories() + if "/" in name and category is None: + category, name = name.split("/", 1) + if category is not None: + cat = cats.get(category) or next((c for k, c in cats.items() if k.lower() == str(category).lower()), None) + if cat is None: + raise ValueError("category %r: one of %s" % (category, sorted(k for k, c in cats.items() if c.nodeTypes()))) + chosen = [cat] + else: + order = {k: i for i, k in enumerate(PREFERRED)} + chosen = sorted(cats.values(), key=lambda c: order.get(c.name(), 99)) + found = [] + for cat in chosen: + t = cat.nodeTypes().get(name) + if t is None: + # A name without namespace or version: the type it resolves to (the newest one preferred). + try: + resolved = hou.preferredNodeType("%s/%s" % (cat.name(), name)) + except Exception: # noqa: BLE001 + resolved = None + t = resolved + if t is not None: + found.append(t) + return found, chosen + + +def type_info(t): + d = {"type": t.nameWithCategory(), "description": t.description(), "doc": doc_url(t), + "inputs": {"min": t.minNumInputs(), "max": t.maxNumInputs()}, "outputs": {"max": t.maxNumOutputs()}, + "generator": t.isGenerator()} + child = t.childTypeCategory() + if child is not None: + d["contains"] = child.name() + try: + versions = list(t.namespaceOrder()) + if len(versions) > 1: + d["versions"] = versions[:12] + except Exception: # noqa: BLE001 + pass + definition = t.definition() + if definition is not None: + d["hda"] = {"library": definition.libraryFilePath(), "version": definition.version(), + "embedded": definition.libraryFilePath() == "Embedded", "installed": definition.isInstalled()} + try: + labels = definition.sections().get("DialogScript") is not None + d["hda"]["sections"] = sorted(definition.sections())[:30] if labels else [] + except Exception: # noqa: BLE001 + pass + try: + comment = definition.comment() + if comment: + d["hda"]["comment"] = comment[:200] + except Exception: # noqa: BLE001 + pass + templates = [(tt, f) for tt, f in walk_templates(t.parmTemplateGroup().entries()) if matches(tt.name(), tt.label(), "")] + detailed(templates, lambda x: template_info(*x), "parm_templates", d, lambda x: x[0].name()) + if MATCH and not templates: + d["match_found_nothing"] = {"match": MATCH, "parms": [tt.name() for tt, _ in walk_templates(t.parmTemplateGroup().entries())][:NAMES_MAX]} + return d + + +if P.get("type"): + wanted = str(P["type"]) + found, cats = find_types(wanted, P.get("category")) + if not found: + bare = wanted.split("/", 1)[-1] + close = [] + for cat in cats: + names = list(cat.nodeTypes()) + close += ["%s/%s" % (cat.name(), n) for n in difflib.get_close_matches(bare, names, n=4, cutoff=0.6)] + close += ["%s/%s" % (cat.name(), n) for n, nt in cat.nodeTypes().items() + if nt.description().lower() == bare.lower() and "%s/%s" % (cat.name(), n) not in close] + out["type"] = {"error": "no node type %r in this Houdini%s" % (wanted, " (category %s)" % P["category"] if P.get("category") else ""), + "closest": close[:20], "details": "pass Category/name, e.g. Sop/box, or search words"} + else: + out["type"] = type_info(found[0]) + if len(found) > 1: + out["type"]["also_in"] = [t.nameWithCategory() for t in found[1:]] + + +# --- live node ---------------------------------------------------------------------------------------------- +def parm_entry(p): + d = {"name": p.name()} + try: + d["value"] = plain(p.eval()) + except Exception as ex: # noqa: BLE001 + d["error"] = str(ex)[:160] + try: + raw = p.unexpandedString() + if raw != d.get("value"): + d["raw"] = plain(raw) + except Exception: # noqa: BLE001 + pass + try: + d["expression"] = p.expression() + d["language"] = str(p.expressionLanguage()).rsplit(".", 1)[-1] + except Exception: # noqa: BLE001 + pass + try: + keys = p.keyframes() + if keys: + d["keyframes"] = len(keys) + except Exception: # noqa: BLE001 + pass + try: + ref = p.getReferencedParm() + if ref is not None and ref.path() != p.path(): + d["references"] = ref.path() + except Exception: # noqa: BLE001 + pass + try: + if p.isLocked(): + d["locked"] = True + if p.isDisabled(): + d["disabled"] = True + except Exception: # noqa: BLE001 + pass + try: + items = p.menuItems() + if items: + d["menu"] = list(items)[:30] + except Exception: # noqa: BLE001 + pass + return d + + +FLAGS = ("isDisplayFlagSet", "isRenderFlagSet", "isBypassed", "isTemplateFlagSet", "isHardLocked", "isSoftLocked", + "isLockedHDA", "isEditable", "isTimeDependent") + + +def flags_of(node): + d = {} + for name in FLAGS: + fn = getattr(node, name, None) + if fn is None: + continue + try: + d[name[2:] if name.startswith("is") else name] = bool(fn()) + except Exception: # noqa: BLE001 + pass + return d + + +def messages(node): + d = {} + for key in ("errors", "warnings", "messages"): + try: + values = [" ".join(m.split())[:400] for m in getattr(node, key)()] + except Exception: # noqa: BLE001 + values = [] + if values: + d[key] = values[:10] + return d + + +def geometry_summary(geo): + if geo is None: + return {"none": "no geometry (did it cook?)"} + d = {"points": len(geo.iterPoints()) if hasattr(geo, "iterPoints") else len(geo.points()), + "prims": geo.intrinsicValue("primitivecount"), "vertices": geo.intrinsicValue("vertexcount")} + try: + box = geo.boundingBox() + if d["points"]: + d["bbox"] = {"min": [num(x) for x in box.minvec()], "max": [num(x) for x in box.maxvec()], + "size": [num(x) for x in box.sizevec()], "center": [num(x) for x in box.center()]} + except Exception: # noqa: BLE001 + pass + attribs = {} + for cls, fn in (("point", "pointAttribs"), ("prim", "primAttribs"), ("vertex", "vertexAttribs"), ("detail", "globalAttribs")): + try: + values = ["%s:%s%s" % (a.name(), str(a.dataType()).rsplit(".", 1)[-1].lower(), a.size() if a.size() > 1 else "") + for a in getattr(geo, fn)()] + except Exception: # noqa: BLE001 + values = [] + if values: + attribs[cls] = values[:60] + d["attributes"] = attribs + groups = {} + for cls, fn in (("point", "pointGroups"), ("prim", "primGroups"), ("edge", "edgeGroups"), ("vertex", "vertexGroups")): + try: + names = [g.name() for g in getattr(geo, fn)()] + except Exception: # noqa: BLE001 + names = [] + if names: + groups[cls] = names[:40] + if groups: + d["groups"] = groups + try: + kinds = {} + for prim in geo.prims()[:20000]: + key = prim.type().name() + kinds[key] = kinds.get(key, 0) + 1 + d["prim_types"] = dict(sorted(kinds.items(), key=lambda kv: -kv[1])[:12]) + except Exception: # noqa: BLE001 + pass + return d + + +def stage_summary(stage): + if stage is None: + return {"none": "no stage (did it cook?)"} + prims = [] + total = 0 + types_count = {} + for prim in stage.Traverse(): + total += 1 + kind = prim.GetTypeName() or "" + types_count[kind or "(untyped)"] = types_count.get(kind or "(untyped)", 0) + 1 + if len(prims) < max(LIMIT, 60) and matches(str(prim.GetPath()), kind, ""): + prims.append({"path": str(prim.GetPath()), "type": kind, **({"inactive": True} if not prim.IsActive() else {})}) + d = {"prims": total, "types": dict(sorted(types_count.items(), key=lambda kv: -kv[1])[:20]), "listed": prims} + if total > len(prims): + d["listed_more"] = total - len(prims) + try: + default = stage.GetDefaultPrim() + d["default_prim"] = str(default.GetPath()) if default else None + d["time_codes"] = [num(stage.GetStartTimeCode()), num(stage.GetEndTimeCode())] + d["up_axis"] = stage.GetMetadata("upAxis") + d["layers"] = len(stage.GetLayerStack()) + except Exception: # noqa: BLE001 + pass + return d + + +def node_info(node): + t = node.type() + d = {"path": node.path(), "type": t.nameWithCategory(), "description": t.description(), "doc": doc_url(t), + "flags": flags_of(node)} + try: + d["inputs"] = [{"index": i, "from": c.path() if c is not None else None, + **({"label": node.inputLabels()[i]} if i < len(node.inputLabels()) else {})} + for i, c in enumerate(node.inputs())] + d["outputs"] = [c.path() for c in node.outputs()][:40] + except Exception: # noqa: BLE001 + pass + d.update(messages(node)) + parms = [p for p in node.parms() if matches(p.name(), p.description(), "")] + changed = [p for p in parms if not p.isAtDefault()] + default = [p for p in parms if p.isAtDefault()] + detailed(changed, parm_entry, "parms_changed", d, lambda p: p.name()) + if default: + d["parms_at_default"] = {"count": len(default), "names": [p.name() for p in default][:NAMES_MAX], + "details": "pass match to read them with their values"} + if MATCH: + d["parms_at_default"]["values"] = [parm_entry(p) for p in default[:LIMIT]] + children = node.children() + if children: + d["children"] = [{"name": c.name(), "type": c.type().name(), **{k: v for k, v in flags_of(c).items() if v and k in ("DisplayFlagSet", "RenderFlagSet", "Bypassed")}, + **({"errors": len(c.errors())} if c.errors() else {})} for c in children[:LIMIT]] + if len(children) > LIMIT: + d["children_more"] = {"count": len(children) - LIMIT, "names": [c.name() for c in children[LIMIT:LIMIT + NAMES_MAX]]} + for getter in ("displayNode", "renderNode"): + fn = getattr(node, getter, None) + try: + target = fn() if fn else None + except Exception: # noqa: BLE001 + target = None + if target is not None: + d[getter.replace("Node", "_node")] = target.path() + category = t.category().name() + try: + if category == "Sop": + d["geometry"] = geometry_summary(node.geometry()) + elif category == "Object" and getattr(node, "displayNode", None) and node.displayNode() is not None: + d["geometry"] = dict(geometry_summary(node.displayNode().geometry()), of=node.displayNode().path()) + d["transform"] = {"world_translate": [num(x) for x in node.worldTransform().extractTranslates()]} + elif category == "Lop": + d["stage"] = stage_summary(node.stage()) + elif category == "Top" and hasattr(node, "getPDGNode"): + pdg_node = node.getPDGNode() + if pdg_node is not None: + states = {} + for item in pdg_node.workItems: + key = str(item.state).rsplit(".", 1)[-1] + states[key] = states.get(key, 0) + 1 + d["work_items"] = states + elif category == "Driver": + for name in ("picture", "sopoutput", "lopoutput", "vm_picture", "outputimage", "copoutput", "filename"): + p = node.parm(name) + if p is not None: + d.setdefault("output", {})[name] = p.eval() + for name in ("trange", "f1", "f2", "f3"): + p = node.parm(name) + if p is not None: + d.setdefault("frame_range", {})[name] = plain(p.eval()) + except Exception as ex: # noqa: BLE001 + d["read_error"] = ("%s: %s" % (type(ex).__name__, ex))[:400] + d.update({k: v for k, v in messages(node).items() if k not in d}) + return d + + +if P.get("node"): + node = hou.node(str(P["node"])) + if node is None: + path = str(P["node"]) + parts = [p for p in path.split("/") if p] + parent, depth = None, len(parts) + while depth > 0 and parent is None: + depth -= 1 + parent = hou.node("/" + "/".join(parts[:depth])) + names = [c.name() for c in parent.children()] if parent is not None else [] + missing = parts[depth] if depth < len(parts) else "" + out["node"] = {"error": "no node %r" % path, "deepest_existing": parent.path() if parent is not None else None, + "closest": difflib.get_close_matches(missing, names, n=6, cutoff=0.5), "children": names[:NAMES_MAX]} + else: + out["node"] = node_info(node) + + +# --- hou symbol --------------------------------------------------------------------------------------------- +def doc_head(obj, lines=40): + text = (getattr(obj, "__doc__", None) or "").strip("\n") + rows = text.splitlines() + return "\n".join(rows[:lines]) + ("\n... (%d more lines)" % (len(rows) - lines) if len(rows) > lines else "") + + +if P.get("hou"): + path = str(P["hou"]).strip() + if not path.startswith("hou"): + path = "hou." + path + target, reached, missing = hou, ["hou"], None + for part in path.split(".")[1:]: + nxt = getattr(target, part, None) + if nxt is None: + missing = part + break + target = nxt + reached.append(part) + if missing is not None: + names = [n for n in dir(target) if not n.startswith("_")] + out["hou"] = {"error": "%s has no %r" % (".".join(reached), missing), + "closest": [".".join(reached + [n]) for n in difflib.get_close_matches(missing, names, n=8, cutoff=0.5)]} + # Methods moved between classes (hou.Node -> hou.OpNode): where else the name exists. + owners = [n for n in dir(hou) if _inspect.isclass(getattr(hou, n, None)) and missing in vars(getattr(hou, n))] + if owners: + out["hou"]["defined_on"] = ["hou.%s.%s" % (o, missing) for o in owners[:12]] + else: + info = {"path": path, "kind": "class" if _inspect.isclass(target) else "module" if isinstance(target, types.ModuleType) + else "callable" if callable(target) else type(target).__name__, "doc": doc_head(target), "url": hom_url(path)} + if _inspect.isclass(target): + own = [n for n in vars(target) if not n.startswith("_") and n != "thisown"] + info["bases"] = [b.__name__ for b in target.__mro__[1:] if b.__module__ == "hou"] + info["methods"] = sorted(n for n in own if matches(n, (getattr(getattr(target, n, None), "__doc__", "") or "")[:200]))[:NAMES_MAX] + inherited = sorted(n for n in dir(target) if not n.startswith("_") and n not in own and n != "thisown") + if inherited: + info["inherited"] = inherited[:NAMES_MAX] + elif not callable(target) and not isinstance(target, types.ModuleType): + info["value"] = plain(target) + out["hou"] = info + + +# --- search ------------------------------------------------------------------------------------------------- +def parm_index(): + """(category, type, parm, label) for every parm template of every node type, kept for this Houdini session.""" + cache = sys.modules.get("_opencode_houdini_inspect_cache") + if cache is None: + cache = types.ModuleType("_opencode_houdini_inspect_cache") + sys.modules[cache.__name__] = cache + if getattr(cache, "parms", None) is None: + rows = [] + for cname, cat in hou.nodeTypeCategories().items(): + for tname, t in cat.nodeTypes().items(): + try: + entries = t.parmTemplateGroup().entriesWithoutFolders() + except Exception: # noqa: BLE001 + continue + for e in entries: + # The template (size) and the parameters it makes (sizex, sizey, sizez). + for name in dict.fromkeys([e.name()] + parm_names(e)): + rows.append((cname, tname, name, e.label())) + cache.parms = rows + return cache.parms + + +def search(text): + words = [w.lower() for w in str(text).split() if w] + if not words: + raise ValueError("search: give one or more words") + started = time.time() + category = P.get("category") + + def score(name, label, desc): + name, label = name.lower(), (label or "").lower() + hay = " ".join((name, label, (desc or "").lower())) + if not all(w in hay for w in words): + return 0 + return sum(3 if w in name else 2 if w in label else 1 for w in words) + (2 if name in words else 0) + + hits = [] + for cname, cat in hou.nodeTypeCategories().items(): + if category and cname.lower() != str(category).lower(): + continue + for tname, t in cat.nodeTypes().items(): + try: + hidden = t.hidden() if hasattr(t, "hidden") else False + except Exception: # noqa: BLE001 + hidden = False + s = score(tname, t.description(), "") + if s: + # Node types first, the hidden and versioned variants after the plain one. + hits.append((s + 3 - (0.5 if hidden or "::" in tname else 0), {"kind": "node type", "path": "%s/%s" % (cname, tname), + "description": t.description(), "doc": doc_url(t)})) + by_parm = {} + for cname, tname, pname, label in parm_index(): + if category and cname.lower() != str(category).lower(): + continue + s = score(pname, label, "") + if s: + entry = by_parm.setdefault(pname, [s, [], []]) + entry[0] = max(entry[0], s) + if label not in entry[1]: + entry[1].append(label) + entry[2].append("%s/%s" % (cname, tname)) + for pname, (s, labels, owners) in by_parm.items(): + hits.append((s - 1, {"kind": "parm", "path": pname, "labels": labels[:4], "on": owners[:8], + **({"on_count": len(owners)} if len(owners) > 8 else {})})) + if not category: + for name in dir(hou): + if name.startswith("_"): + continue + obj = getattr(hou, name, None) + doc = (getattr(obj, "__doc__", None) or "").strip() + first = " ".join(doc.splitlines()[2:4]) if doc.startswith("hou.") else " ".join(doc.splitlines()[:2]) + s = score(name, "", first[:200]) + if s: + hits.append((s, {"kind": "hou class" if _inspect.isclass(obj) else "hou function" if callable(obj) else "hou value", + "path": "hou." + name, "doc": first[:160], "url": hom_url("hou." + name)})) + if _inspect.isclass(obj): + for m in vars(obj): + if m.startswith("_") or m == "thisown": + continue + mdoc = (getattr(getattr(obj, m, None), "__doc__", None) or "").strip() + sig = mdoc.splitlines()[0] if mdoc else "" + s = score(m, "", " ".join(mdoc.split()[:40])) + if s: + hits.append((s - 0.25, {"kind": "hou method", "path": "hou.%s.%s" % (name, m), "signature": sig[:200], + "url": hom_url("hou.%s.%s" % (name, m))})) + hits.sort(key=lambda h: (-h[0], len(h[1]["path"]), h[1]["path"])) + found = {"words": words, "found": len(hits), "results": [h for _, h in hits[:LIMIT]], "seconds": round(time.time() - started, 2)} + if len(hits) > LIMIT: + found["more"] = {"count": len(hits) - LIMIT, "paths": [h[1]["path"] for h in hits[LIMIT:LIMIT + NAMES_MAX]], + "details": "add words or category, or inspect a type, node or hou path"} + return found + + +if P.get("search"): + out["search"] = search(P["search"]) + +if not any(P.get(k) for k in ("search", "type", "node", "hou")): + raise ValueError("give search (words), type (e.g. Sop/box), node (a node path) or hou (e.g. hou.OpNode.createNode)") + +result = out diff --git a/integrations/houdini/plugin/programs/render.py b/integrations/houdini/plugin/programs/render.py new file mode 100644 index 0000000..89b849e --- /dev/null +++ b/integrations/houdini/plugin/programs/render.py @@ -0,0 +1,176 @@ +# SPDX-License-Identifier: MIT +# houdini_render (fixed program, runs in the open Houdini): checks the request, writes a working copy of the open +# scene (hscript mwrite -n: the open file keeps its name and its own file is untouched) or takes the saved file, +# then starts a detached background hython running render_job.py on it: a ROP render (frames, to the exact output +# when given) or a PDG cook of a TOP node. Returns at once; the job writes a log the plugin follows: +# OPENCODE_RENDER {...} what is rendered (once, at the start) +# OPENCODE_PROGRESS {"done": n, "total": m, ...} +# OPENCODE_DONE {"output": ..., "files": [...], ...} or OPENCODE_FAILED "" +# PARAMS: {"rop" | "top", "output"?, "frames"?, "size"?, "source"?: "copy" | "saved", "overwrite"?} +# JOB: render_job.py, prepended by the plugin. +import json +import os +import re +import subprocess +import tempfile +import time +import uuid + +import hou + +P = PARAMS # noqa: F821 +OUTPUT_PARMS = ("picture", "vm_picture", "outputimage", "sopoutput", "lopoutput", "filename", "copoutput", + "outputfile", "file") +RUN = re.compile(r"#+") + + +def license_name(): + try: + return str(hou.licenseCategory()).rsplit(".", 1)[-1] + except Exception: # noqa: BLE001 + return "unknown" + + +if bool(P.get("rop")) == bool(P.get("top")): + raise ValueError("give rop (a ROP node path, e.g. /out/karma1) or top (a TOP node or TOP network path), one of them") +current = hou.hipFile.path() +never_saved = hou.hipFile.isNewFile() +base_dir = os.path.dirname(current) if not never_saved else None +kind = "render" if P.get("rop") else "pdg" +path = P.get("rop") or P.get("top") +node = hou.node(path) +if node is None: + raise ValueError("no node %r" % path) + +facts = {"license": license_name()} +if facts["license"] in ("Apprentice", "ApprenticeHD", "Education"): + facts["license_note"] = ("%s: rendered images are at most 1920x1080 (larger sizes are scaled down, aspect kept) and " + "carry the Houdini logo in their bottom-right corner; files are non-commercial" % facts["license"]) + + +def frame_list(spec): + if spec is None: + return None + if isinstance(spec, (int, float)): + values = [spec, spec, 1] + elif isinstance(spec, list) and 2 <= len(spec) <= 3 and all(isinstance(v, (int, float)) for v in spec): + values = list(spec) + ([1] if len(spec) == 2 else []) + else: + raise ValueError("frames: a frame, [start, end] or [start, end, step]") + start, end, step = (float(v) for v in values) + if end < start or step <= 0: + raise ValueError("frames: end before start, or a step that is not positive") + count = int((end - start) / step + 1e-6) + 1 + if count > 10000: + raise ValueError("frames: %d frames, at most 10000" % count) + return [(int(f) if float(f).is_integer() else f) for f in (start + i * step for i in range(count))] + + +output = output_pattern = out_parm = None +targets, frames = [], [] +if kind == "render": + if not isinstance(node, hou.RopNode): + raise ValueError("rop: %s is a %s, not a ROP (render nodes live in /out or a ROP network)" % (path, node.type().nameWithCategory())) + out_parm = next((n for n in OUTPUT_PARMS if node.parm(n) is not None), None) + frames = frame_list(P.get("frames")) + if frames is None: + trange = node.parm("trange") + if trange is None or trange.eval() == 0: + frames = [int(hou.frame()) if float(hou.frame()).is_integer() else hou.frame()] + else: + frames = frame_list([node.evalParm("f1"), node.evalParm("f2"), node.evalParm("f3") or 1]) + if P.get("output"): + output = P["output"] + if not os.path.isabs(output): + if base_dir is None: + raise ValueError("output %r is relative and the scene was never saved: give an absolute path" % output) + output = os.path.join(base_dir, output) + output = os.path.normpath(output).replace("\\", "/") + runs = RUN.findall(os.path.basename(output)) + if len(runs) > 1: + raise ValueError("output: one run of # marks the frame number") + if len(frames) > 1 and not runs and "$F" not in output: + raise ValueError("output: %d frames need a frame number in the name: a # run (shot_####.exr) or $F4" % len(frames)) + if out_parm is None: + raise ValueError("rop: %s (%s) has no output parameter this tool knows (%s)" % (path, node.type().name(), ", ".join(OUTPUT_PARMS))) + output_pattern = output.replace(runs[0], "$F%d" % len(runs[0])) if runs else output + targets = [hou.expandStringAtFrame(output_pattern, f) for f in frames] + elif out_parm is not None: + targets = [node.parm(out_parm).evalAtFrame(f) for f in frames] + output = node.parm(out_parm).unexpandedString() + targets = [os.path.normpath(t) for t in targets if t] + if not targets: + facts["targets_note"] = "the ROP names no output file here (a USD render settings prim may): the files are read back from the log" + taken = [t for t in targets if os.path.exists(t)] + if taken and not P.get("overwrite"): + raise ValueError("%s already exists (%d file(s)): pass overwrite true or name another output" % (taken[0], len(taken))) + for t in targets[:1]: + os.makedirs(os.path.dirname(t), exist_ok=True) + size = P.get("size") + if size is not None and (not isinstance(size, list) or len(size) != 2 or not all(isinstance(v, int) and 4 <= v <= 16384 for v in size)): + raise ValueError("size: [width, height], 4 to 16384") +else: + target = node + if node.childTypeCategory() is not None and node.childTypeCategory().name() == "Top": + target = node.displayNode() if hasattr(node, "displayNode") else None + if target is None: + outputs = [c for c in node.children() if c.type().name() == "output"] + target = outputs[0] if outputs else None + if target is None: + raise ValueError("top: %s has no display or output node to cook: name a TOP node" % path) + if target.type().category().name() != "Top": + raise ValueError("top: %s is a %s, not a TOP node or TOP network" % (path, target.type().nameWithCategory())) + node = target + if P.get("output") or P.get("frames") or P.get("size"): + raise ValueError("top: output, frames and size are a ROP's; a PDG cook writes what its nodes say") + +source = P.get("source", "copy") +if source not in ("copy", "saved"): + raise ValueError("source: copy (a working copy of the open scene, the default) or saved (the file as saved)") +stamp = time.strftime("%Y%m%d-%H%M%S") +# One work folder per job (several can start in the same second from the same Houdini): never shared. +work = os.path.join(tempfile.gettempdir(), "opencode-houdini-render", "%s-%d-%s" % (stamp, os.getpid(), uuid.uuid4().hex[:12])) +os.makedirs(work) +working_copy = None +if source == "saved": + if never_saved or not os.path.isfile(current): + raise ValueError("source saved: the scene was never saved (houdini_save first, or source copy)") + hip = current + if hou.isUIAvailable() and hou.hipFile.hasUnsavedChanges(): + facts["unsaved_changes"] = "the open scene has changes the saved file lacks: they are not rendered" +else: + # The copy keeps the scene's file name, so $HIPNAME expands as in the open scene; the job sets $HIP back. + working_copy = os.path.join(work, os.path.basename(current) or "untitled.hipnc").replace("\\", "/") + out_err = hou.hscript('mwrite -n "%s"' % working_copy) + if not os.path.isfile(working_copy): + raise RuntimeError("the working copy was not written: %s" % " ".join(out_err).strip()[:300]) + hip = working_copy + +job = {"kind": kind, "hip": hip, "node": node.path(), "out_parm": out_parm, "output_pattern": output_pattern, + "frames": frames, "targets": targets, "size": P.get("size"), "work": work, + # $HIP of the copy is its work folder: outputs relative to $HIP still go next to the scene. + "hip_dir": base_dir} +with open(os.path.join(work, "job.json"), "w", encoding="utf-8") as fh: + json.dump(job, fh) +script = os.path.join(work, "render_job.py") +with open(script, "w", encoding="utf-8") as fh: + fh.write(JOB) # noqa: F821 - render_job.py, prepended by the plugin +exe = os.path.join(hou.getenv("HFS") or "", "bin", "hython.exe" if os.name == "nt" else "hython") +if not os.path.isfile(exe): + raise RuntimeError("no hython at %s" % exe) +log_path = os.path.join(work, "render.log") +env = {k: v for k, v in os.environ.items() if k != "OPENCODE_HOUDINI_OFFER_KEY"} +env["OPENCODE_HOUDINI_ROLE"] = "render" # never offered to OpenCode +env["PYTHONUNBUFFERED"] = "1" +log = open(log_path, "wb") +kwargs = {"stdin": subprocess.DEVNULL, "stdout": log, "stderr": subprocess.STDOUT, "close_fds": True, "env": env, "cwd": work} +if os.name == "nt": + kwargs["creationflags"] = 0x08000000 | 0x00000200 # no console window, own process group +else: + kwargs["start_new_session"] = True +process = subprocess.Popen([exe, script, os.path.join(work, "job.json")], **kwargs) +log.close() +result = {"pid": process.pid, "log": log_path, "work": work, "kind": kind, "output": output, "targets": targets, + "frames": frames, "start": frames[0] if frames else None, "end": frames[-1] if frames else None, + "node": node.path(), "node_type": node.type().nameWithCategory(), "source": source, "hip": hip, + "facts": facts, **({"working_copy": working_copy} if working_copy else {})} diff --git a/integrations/houdini/plugin/programs/render_job.py b/integrations/houdini/plugin/programs/render_job.py new file mode 100644 index 0000000..1a57692 --- /dev/null +++ b/integrations/houdini/plugin/programs/render_job.py @@ -0,0 +1,180 @@ +# SPDX-License-Identifier: MIT +# The background hython's script (written next to its log by render.py, run as: hython render_job.py job.json): +# loads the working copy (or saved file), sets $HIP back to the scene's folder, overrides the output, frames and +# size in memory only, then renders the ROP's frames in one call or cooks the TOP node, and prints for the plugin: +# OPENCODE_RENDER {...}, OPENCODE_PROGRESS {"done": n, "total": m, ...}, then OPENCODE_DONE {...} or +# OPENCODE_FAILED "". +import json +import os +import sys +import threading +import time +import traceback + +import hou + +with open(sys.argv[1], encoding="utf-8") as fh: + o = json.load(fh) + + +def mark(key, value): + sys.stdout.write("OPENCODE_%s %s\n" % (key, json.dumps(value))) + sys.stdout.flush() + + +def set_parm(node, name, value): + p = node.parmTuple(name) if isinstance(value, (list, tuple)) else node.parm(name) + if p is None: + return False + p.set(value) + return True + + +def apply_size(rop, size): + kind = rop.type().name().split("::")[0] + w, h = size + if kind == "opengl": + return set_parm(rop, "tres", True) and set_parm(rop, "res", (w, h)) + if kind == "karma": + return set_parm(rop, "resolutionx", w) and set_parm(rop, "resolutiony", h) + if kind == "usdrender": + return set_parm(rop, "override_res", "specific") and set_parm(rop, "res_user", (w, h)) + if kind == "ifd": + return set_parm(rop, "override_camerares", True) and set_parm(rop, "res_fraction", "specific") and set_parm(rop, "res_override", (w, h)) + return False + + +def render(): + rop = hou.node(o["node"]) + if rop is None: + raise RuntimeError("the file read has no node %s" % o["node"]) + used = {"node": rop.path(), "type": rop.type().nameWithCategory(), "frames": [o["frames"][0], o["frames"][-1]], + "count": len(o["frames"]), "license": str(hou.licenseCategory()).rsplit(".", 1)[-1]} + if o["output_pattern"]: + rop.parm(o["out_parm"]).set(o["output_pattern"]) + if o["size"]: + used["size_applied"] = apply_size(rop, o["size"]) + used["size"] = o["size"] + if rop.parm("trange") is not None: + rop.parm("trange").set(1) + step = (o["frames"][1] - o["frames"][0]) if len(o["frames"]) > 1 else 1 + rop.parmTuple("f").deleteAllKeyframes() + rop.parmTuple("f").set((o["frames"][0], o["frames"][-1], step)) + targets = [rop.parm(o["out_parm"]).evalAtFrame(f) for f in o["frames"]] if o["out_parm"] else [] + targets = [os.path.normpath(t) for t in targets if t] + if o["targets"] and targets != o["targets"]: + used["targets_differ"] = {"expected": o["targets"][:3], "here": targets[:3]} + used["targets"] = targets[:3] + (["... (%d)" % len(targets)] if len(targets) > 3 else []) + mark("RENDER", used) + total = len(o["frames"]) + state = {"done": 0, "events": 0} + lock = threading.Lock() + + def progress(count, frame=None): + with lock: + if count > state["done"]: + state["done"] = min(count, total) + mark("PROGRESS", dict({"done": state["done"], "total": total}, **({"frame": frame} if frame is not None else {}))) + + def on_event(node, event, when): + # Per-frame events (OpenGL, Mantra); Karma renders through husk without them: the files say it then. + if event == hou.ropRenderEventType.PostFrame: + state["events"] += 1 + frame = round(hou.timeToFrame(when), 3) + progress(state["events"], int(frame) if float(frame).is_integer() else frame) + + stop = threading.Event() + begun = time.time() + + def watch(): + while not stop.wait(1.0): + written = 0 + for t in targets: + try: + if os.path.getmtime(t) >= begun - 1: + written += 1 + except OSError: + pass + progress(written) + + rop.addRenderEventCallback(on_event) + watcher = threading.Thread(target=watch, daemon=True) + watcher.start() + step = (o["frames"][1] - o["frames"][0]) if len(o["frames"]) > 1 else 1 + try: + rop.render(frame_range=(o["frames"][0], o["frames"][-1], step), verbose=False, output_progress=False) + except hou.OperationFailed as ex: + messages = list(rop.errors()) + raise RuntimeError(("%s %s" % (ex, " ".join(messages))).strip()[:1500]) + finally: + stop.set() + watcher.join(2) + if rop.errors(): + raise RuntimeError("; ".join(" ".join(m.split()) for m in rop.errors())[:1500]) + missing = [t for t in targets if not os.path.isfile(t)] + if missing: + raise RuntimeError("%d of %d file(s) were not written, first %s" % (len(missing), len(targets), missing[0])) + progress(total) + warnings = [" ".join(m.split())[:300] for m in rop.warnings()][:5] + done = {"output": o["output_pattern"] or (rop.parm(o["out_parm"]).unexpandedString() if o["out_parm"] else None), + "files": targets, "bytes": sum(os.path.getsize(t) for t in targets), **({"warnings": warnings} if warnings else {})} + try: + # The size really written (Apprentice scales larger renders down to 1920x1080). + from PIL import Image + with Image.open(targets[0]) as image: + done["image_size"] = list(image.size) + if o["size"] and done["image_size"] != list(o["size"]): + done["size_note"] = "asked %dx%d, written %dx%d (%s)" % (o["size"][0], o["size"][1], done["image_size"][0], done["image_size"][1], used["license"]) + except Exception: # noqa: BLE001 - not an image PIL reads (EXR, geometry, USD) + pass + mark("DONE", done) + + +def cook(): + top = hou.node(o["node"]) + if top is None: + raise RuntimeError("the file read has no node %s" % o["node"]) + mark("RENDER", {"node": top.path(), "type": top.type().nameWithCategory(), "kind": "pdg"}) + top.cookWorkItems(block=False) + context = top.getPDGGraphContext() + last = None + while True: + pdg_node = top.getPDGNode() + items = list(pdg_node.workItems) if pdg_node is not None else [] + states = {} + for item in items: + key = str(item.state).rsplit(".", 1)[-1] + states[key] = states.get(key, 0) + 1 + finished = sum(v for k, v in states.items() if k in ("CookedSuccess", "CookedFail", "CookedCancel", "CookedCache")) + now = (finished, len(items)) + if now != last: + mark("PROGRESS", {"done": finished, "total": len(items), "states": states}) + last = now + if context is None or not context.cooking: + break + time.sleep(0.5) + failed = [i for i in items if "Fail" in str(i.state)] + files = [] + for item in items: + for f in getattr(item, "outputFiles", []) or []: + files.append(getattr(f, "path", str(f))) + if failed or top.errors(): + first = failed[0] if failed else None + detail = (" first failed work item %s" % first.name) if first is not None else "" + raise RuntimeError(("%d work item(s) failed;%s %s" % (len(failed), detail, "; ".join(top.errors()))).strip()[:1500]) + mark("DONE", {"output": None, "files": files[:500], "work_items": len(items), **({"files_more": len(files) - 500} if len(files) > 500 else {})}) + + +code = 0 +try: + hou.hipFile.load(o["hip"], suppress_save_prompt=True, ignore_load_warnings=True) + if o.get("hip_dir"): + hou.putenv("HIP", o["hip_dir"].replace("\\", "/")) + hou.hscript("varchange") + render() if o["kind"] == "render" else cook() +except Exception as ex: # noqa: BLE001 + mark("FAILED", ("%s: %s" % (type(ex).__name__, ex))[:1500] + "\n" + traceback.format_exc()[-1500:]) + code = 1 +sys.stdout.flush() +# hython can crash in its own exit handlers after a render: the outcome is written, leave at once. +os._exit(code) diff --git a/integrations/houdini/plugin/programs/save.py b/integrations/houdini/plugin/programs/save.py new file mode 100644 index 0000000..bb991b1 --- /dev/null +++ b/integrations/houdini/plugin/programs/save.py @@ -0,0 +1,72 @@ +# SPDX-License-Identifier: MIT +# houdini_save (fixed program): save the open scene, or save it under a path (as the open file, or as a copy that +# leaves the open file's name alone: hscript mwrite -n). Says what was really written (path, bytes, time), read +# from the disk: hou.hipFile.hasUnsavedChanges() is not consulted (it stays true after a save in hython). +# PARAMS: {"path"?, "copy"?, "overwrite"?} +import os +import time + +import hou + +P = PARAMS # noqa: F821 +EXTENSIONS = (".hip", ".hipnc", ".hiplc") +current = hou.hipFile.path() +never_saved = hou.hipFile.isNewFile() +target = P.get("path") +copy = bool(P.get("copy")) + + +def license_name(): + try: + return str(hou.licenseCategory()).rsplit(".", 1)[-1] + except Exception: # noqa: BLE001 + return "unknown" + + +if copy and not target: + raise ValueError("copy: give path, the file the copy goes to") +if target: + if not os.path.isabs(target): + if never_saved: + raise ValueError("path %r is relative and the scene was never saved: give an absolute path" % target) + target = os.path.join(os.path.dirname(current), target) + target = os.path.normpath(target) + if os.path.splitext(target)[1].lower() not in EXTENSIONS: + raise ValueError("path must end with %s" % ", ".join(EXTENSIONS)) + if '"' in target: + raise ValueError("path must not hold a double quote") + same = os.path.normcase(target) == os.path.normcase(os.path.normpath(current)) + if os.path.exists(target) and not same and not P.get("overwrite"): + raise ValueError("%s exists: pass overwrite true or name another file" % target) + os.makedirs(os.path.dirname(target), exist_ok=True) +elif never_saved: + raise ValueError("the scene was never saved: pass path") + +before = None +written = (target or current).replace("\\", "/") +if os.path.exists(written): + before = os.stat(written).st_mtime_ns +if copy: + out, err = hou.hscript('mwrite -n "%s"' % written) + if err.strip(): + raise RuntimeError("mwrite -n %s: %s" % (written, err.strip()[:300])) +elif target: + hou.hipFile.save(written) +else: + hou.hipFile.save() +if not os.path.isfile(written): + raise RuntimeError("nothing was written at %s" % written) +stat = os.stat(written) +if before is not None and stat.st_mtime_ns == before: + raise RuntimeError("%s was not rewritten (its modification time did not change)" % written) +result = {"saved": os.path.normpath(written), "bytes": stat.st_size, + "saved_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stat.st_mtime)), + "open_file": os.path.normpath(hou.hipFile.path()), "license": license_name()} +if copy: + result["copy"] = True + result["note"] = "a copy: the open scene keeps its name, and its own file is unchanged" +lic = result["license"] +if lic in ("Apprentice", "ApprenticeHD", "Education") and not written.lower().endswith(".hipnc"): + result["license_note"] = "%s files are non-commercial whatever their extension; .hipnc names them so" % lic +elif lic == "Indie" and not written.lower().endswith(".hiplc"): + result["license_note"] = "Indie files are limited-commercial whatever their extension; .hiplc names them so" diff --git a/integrations/houdini/plugin/programs/verify.py b/integrations/houdini/plugin/programs/verify.py new file mode 100644 index 0000000..001f538 --- /dev/null +++ b/integrations/houdini/plugin/programs/verify.py @@ -0,0 +1,533 @@ +# SPDX-License-Identifier: MIT +# houdini_verify (fixed program): check a result by measurement on the open scene. criteria: the agent's own +# acceptance checks, read-only expressions measured on cooked geometry, USD stages and parameters (at a frame +# without moving the playbar), each passed or failed with its measured value. errors: every node under the roots +# with errors() or warnings(), after cooking the criteria's nodes and the display (and render) nodes of every +# geometry object and LOP network. render: a low-resolution image (Karma CPU by default, or OpenGL, or a ROP you +# name) made by a separate background hython on a working copy (a crash there never touches this Houdini), with +# facts read from its pixels. Nothing in the open scene is changed: no frame change, no node made. +# PARAMS is prepended by the plugin: {"criteria"?, "frame"?, "errors"?, "roots"?, "render"?, "timeout_s"?} +import ast +import base64 +import json +import math +import os +import re +import subprocess +import tempfile +import time + +import hou + +P = PARAMS # noqa: F821 +STARTED = time.time() +CRITERIA = P.get("criteria") or [] +FRAME = float(P["frame"]) if P.get("frame") is not None else hou.frame() +COOK_BUDGET = 60.0 # seconds spent cooking display nodes for the error scan, at most +ERRORS_MAX = 60 +NODES_MAX = 200000 +OPS = ("<", "<=", ">", ">=", "==", "!=", "approx", "between", "in", "contains") +THROWAWAY = os.environ.get("OPENCODE_HOUDINI_ROLE") in ("verify", "render") and not hou.isUIAvailable() +if not isinstance(CRITERIA, list) or len(CRITERIA) > 50: + raise ValueError("criteria: a list of at most 50 {expr, op, value, tol?, frame?, label?}") + + +def license_name(): + try: + return str(hou.licenseCategory()).rsplit(".", 1)[-1] + except Exception: # noqa: BLE001 + return "unknown" + + +SAFE_BUILTINS = {n: __builtins__[n] if isinstance(__builtins__, dict) else getattr(__builtins__, n) for n in ( + "abs", "min", "max", "len", "round", "sum", "any", "all", "sorted", "int", "float", "bool", "str", "list", + "tuple", "set", "dict", "range", "zip", "enumerate", "isinstance", "map", "filter", "reversed")} +# Attribute names that change something (hou and USD): refused in a criterion. +WRITES = re.compile(r"^(set|create|destroy|delete|remove|insert|append|add|clear|save|load|merge|render|press|move|" + r"copy|paste|lock|unlock|allow|bypass|hscript|execute|run|revert|undo|define|override|export|" + r"reload|mute|unmute|change|rename|layout|cut|write|open|close|install|uninstall|exit|system|" + r"cook|dirty|force|update|apply|bake|stash|expand|collapse|hide|show|display)(?![a-z])") +READS = {"renderNode", "displayNode", "isDisplayFlagSet", "isRenderFlagSet"} +BLOCKED = {"ui", "undos", "hipFile", "hda", "session", "qt", "shelves", "hscript", "system", "exit"} + + +def read_only_expression(text): + """An expression that reads: no assignment, no lambda, no private name, no attribute that changes things.""" + tree = ast.parse(text, mode="eval") + for n in ast.walk(tree): + if isinstance(n, (ast.NamedExpr, ast.Lambda)): + raise ValueError("criteria: %r is not a plain read" % text) + if isinstance(n, ast.Attribute) and (n.attr.startswith("_") or n.attr in BLOCKED or (WRITES.match(n.attr[:1].lower() + n.attr[1:]) and n.attr not in READS)): + raise ValueError("criteria: %r reaches %s, which is not a read" % (text, n.attr)) + if isinstance(n, ast.Name) and n.id.startswith("_"): + raise ValueError("criteria: %r names %s" % (text, n.id)) + return compile(tree, "", "eval") + + +class Box(object): + """World bounds of cooked geometry: min, max, size, center (x, y, z tuples).""" + + def __init__(self, bb): + self.min = tuple(bb.minvec()) + self.max = tuple(bb.maxvec()) + self.size = tuple(bb.sizevec()) + self.center = tuple(bb.center()) + + def __repr__(self): + return "Box(min=%s, max=%s)" % (rounded(self.min), rounded(self.max)) + + +def need(path): + n = hou.node(path) + if n is None: + raise ValueError("no node %r" % path) + return n + + +def sop_of(path): + n = need(path) + if n.type().category().name() == "Object": + shown = n.displayNode() if hasattr(n, "displayNode") else None + if shown is None: + raise ValueError("%s has no display SOP" % path) + return shown + if not hasattr(n, "geometry"): + raise ValueError("%s is a %s, not a SOP or a geometry object" % (path, n.type().nameWithCategory())) + return n + + +COOKED = [] + + +def namespace(frame): + at_current = abs(frame - hou.frame()) < 1e-9 + + def geo(path): + n = sop_of(path) + g = n.geometry() if at_current else n.geometryAtFrame(frame) + if n.path() not in COOKED: + COOKED.append(n.path()) + if g is None: + raise ValueError("%s has no cooked geometry: %s" % (n.path(), "; ".join(n.errors())[:300] or "no error reported")) + return g + + def bbox(path): + return Box(geo(path).boundingBox()) + + def stage(path): + n = need(path) + if not hasattr(n, "stage"): + raise ValueError("%s is a %s, not a LOP" % (path, n.type().nameWithCategory())) + s = n.stage() + if n.path() not in COOKED: + COOKED.append(n.path()) + if s is None: + raise ValueError("%s has no stage: %s" % (path, "; ".join(n.errors())[:300] or "no error reported")) + return s + + def prim(path, prim_path): + p = stage(path).GetPrimAtPath(prim_path) + if not p or not p.IsValid(): + raise ValueError("%s: no prim %s" % (path, prim_path)) + return p + + def usd_attr(path, prim_path, name): + from pxr import Usd + a = prim(path, prim_path).GetAttribute(name) + if not a or not a.IsValid(): + raise ValueError("%s %s: no attribute %r" % (path, prim_path, name)) + value = a.Get(Usd.TimeCode(frame)) + return tuple(value) if hasattr(value, "__len__") and not isinstance(value, str) else value + + def usd_prims(path, type_name=None): + return [str(p.GetPath()) for p in stage(path).Traverse() if type_name is None or p.GetTypeName() == type_name] + + def parm(path): + p = hou.parm(path) + if p is not None: + return p.eval() if at_current else p.evalAtFrame(frame) + t = hou.parmTuple(path) + if t is None: + raise ValueError("no parm or parm tuple %r" % path) + return tuple(t.eval() if at_current else t.evalAtFrame(frame)) + + def attrib_names(path, cls="point"): + fn = {"point": "pointAttribs", "prim": "primAttribs", "vertex": "vertexAttribs", "detail": "globalAttribs"}[cls] + return [a.name() for a in getattr(geo(path), fn)()] + + def attrib_values(path, name, cls="point"): + g = geo(path) + fn = {"point": "pointFloatAttribValues", "prim": "primFloatAttribValues", "vertex": "vertexFloatAttribValues"} + if cls == "detail": + return g.attribValue(name) + attrib = {"point": g.findPointAttrib, "prim": g.findPrimAttrib, "vertex": g.findVertexAttrib}[cls](name) + if attrib is None: + raise ValueError("%s: no %s attribute %r" % (path, cls, name)) + if attrib.dataType() in (hou.attribData.Float, hou.attribData.Int): + values = list(getattr(g, fn[cls])(name)) if attrib.dataType() == hou.attribData.Float else \ + list(getattr(g, fn[cls].replace("Float", "Int"))(name)) + size = attrib.size() + return values if size == 1 else [tuple(values[i:i + size]) for i in range(0, len(values), size)] + return list(getattr(g, fn[cls].replace("Float", "String"))(name)) + + return {"__builtins__": SAFE_BUILTINS, "hou": hou, "math": math, "frame": frame, "node": need, "geo": geo, + "bbox": bbox, "npoints": lambda path: geo(path).intrinsicValue("pointcount"), + "nprims": lambda path: geo(path).intrinsicValue("primitivecount"), "stage": stage, "prim": prim, + "usd_attr": usd_attr, "usd_prims": usd_prims, "parm": parm, "attrib_names": attrib_names, + "attrib_values": attrib_values, "errors": lambda path: list(need(path).errors()), + "warnings": lambda path: list(need(path).warnings())} + + +def rounded(value): + if isinstance(value, float): + return round(value, 6) if math.isfinite(value) else str(value) + if isinstance(value, (bool, int, str)) or value is None: + return value if not isinstance(value, str) or len(value) <= 300 else value[:300] + "..." + if isinstance(value, Box): + return {"min": rounded(value.min), "max": rounded(value.max), "size": rounded(value.size)} + if hasattr(value, "path") and callable(value.path): + try: + return value.path() + except Exception: # noqa: BLE001 + pass + if isinstance(value, dict): + return {str(k): rounded(v) for k, v in list(value.items())[:64]} + try: + return [rounded(v) for v in list(value)[:64]] + except TypeError: + return repr(value)[:200] + + +def compare(measured, op, value, tol): + def numbers(x): + if isinstance(x, (str, Box)): + return None + try: + return [float(v) for v in x] + except (TypeError, ValueError): + return None + + if op == "in": + return measured in value + if op == "contains": + return value in measured + if op == "between": + lo, hi = value + seq = numbers(measured) + return all(lo <= v <= hi for v in seq) if seq is not None else lo <= measured <= hi + seq = numbers(measured) + if seq is not None and (op not in ("==", "!=") or isinstance(value, (list, tuple))): + targets = numbers(value) if isinstance(value, (list, tuple)) else [value] * len(seq) + if targets is None or len(targets) != len(seq): + raise ValueError("value: %d numbers to compare with %s" % (len(seq), rounded(measured))) + if op == "!=": + return any(m != t for m, t in zip(seq, targets)) + return all(compare(m, op, t, tol) for m, t in zip(seq, targets)) + if op == "approx": + return abs(measured - value) <= tol + return {"<": lambda: measured < value, "<=": lambda: measured <= value, ">": lambda: measured > value, + ">=": lambda: measured >= value, "==": lambda: measured == value, "!=": lambda: measured != value}[op]() + + +rows = [] +for i, c in enumerate(CRITERIA): + if not isinstance(c, dict) or "expr" not in c or c.get("op") not in OPS or "value" not in c: + raise ValueError("criteria[%d]: {expr, op: one of %s, value, tol?, frame?, label?}" % (i, " ".join(OPS))) + code = read_only_expression(str(c["expr"])) + frame = float(c["frame"]) if c.get("frame") is not None else FRAME + row = {"label": c.get("label") or c["expr"], "check": "%s %s %s" % (c["expr"], c["op"], json.dumps(c["value"])), "frame": rounded(frame)} + try: + measured = eval(code, namespace(frame)) # noqa: S307 - a checked read-only expression + row["measured"] = rounded(measured) + row["passed"] = bool(compare(measured, c["op"], c["value"], float(c.get("tol", 1e-4)))) + except Exception as ex: # noqa: BLE001 + row["passed"] = False + row["error"] = ("%s: %s" % (type(ex).__name__, ex))[:400] + rows.append(row) + +facts = {"hip": None if hou.hipFile.isNewFile() else hou.hipFile.path(), "frame": rounded(hou.frame()), "fps": rounded(hou.fps()), + "frame_range": [rounded(float(hou.expandString(v))) for v in ("$FSTART", "$FEND")]} + + +def cook_targets(): + """Display (and render) SOPs of every geometry object, display LOPs of every LOP network, under the roots.""" + found = [] + for root in roots: + for n in [root] + list(root.allSubChildren(recurse_in_locked_nodes=False)): + category = n.childTypeCategory() + if category is None or not n.children(): + continue + if category.name() in ("Sop", "Lop"): + for getter in ("displayNode", "renderNode"): + fn = getattr(n, getter, None) + try: + target = fn() if fn else None + except Exception: # noqa: BLE001 + target = None + if target is not None and target.path() not in found: + found.append(target.path()) + return found + + +def scan_errors(): + cooked, skipped = [], [] + for path in COOKED + [p for p in cook_targets() if p not in COOKED]: + if time.time() - STARTED > COOK_BUDGET: + skipped.append(path) + continue + n = hou.node(path) + try: + n.cook() + cooked.append(path) + except hou.OperationFailed: + cooked.append(path) # its errors() say why + except Exception as ex: # noqa: BLE001 + skipped.append("%s (%s)" % (path, str(ex)[:120])) + found, counted, total = [], {"errors": 0, "warnings": 0}, 0 + for root in roots: + for n in [root] + list(root.allSubChildren(recurse_in_locked_nodes=False)): + total += 1 + if total > NODES_MAX: + break + try: + errors, warnings = n.errors(), n.warnings() + except Exception: # noqa: BLE001 + continue + if errors: + counted["errors"] += 1 + elif warnings: + counted["warnings"] += 1 + else: + continue + if len(found) < ERRORS_MAX: + entry = {"node": n.path(), "type": n.type().nameWithCategory()} + if errors: + entry["errors"] = [" ".join(m.split())[:400] for m in errors[:4]] + if warnings: + entry["warnings"] = [" ".join(m.split())[:400] for m in warnings[:4]] + found.append(entry) + found.sort(key=lambda e: 0 if "errors" in e else 1) + return found, {"nodes_with_errors": counted["errors"], "nodes_with_warnings_only": counted["warnings"], + "nodes_read": min(total, NODES_MAX), "cooked": cooked[:40], **({"cooked_more": len(cooked) - 40} if len(cooked) > 40 else {}), + **({"not_cooked": skipped[:40]} if skipped else {})} + + +roots = [need(r) for r in (P.get("roots") or ["/"])] +error_rows = [] +if P.get("errors", True): + error_rows, facts["error_scan"] = scan_errors() + if len(error_rows) >= ERRORS_MAX: + facts["error_scan"]["listed"] = ERRORS_MAX + + +# --- render: a low-resolution image from a separate hython --------------------------------------------------- +RENDER_SCRIPT = r''' +import base64, json, os, time, hou +o = OPTIONS +facts = {"renderer": None} +out = {"facts": facts} +def first_camera(): + cams = [n for n in hou.node("/obj").allSubChildren() if n.type().name() in ("cam", "camera") or n.type().name().startswith("cam::")] + return cams[0] if cams else None +def res_for(cam_res): + w, h = cam_res + k = o["size"] / float(max(w, h)) + return max(16, int(round(w * k))), max(16, int(round(h * k))) +def set_parm(node, name, value): + p = node.parm(name) if not isinstance(value, (tuple, list)) else node.parmTuple(name) + if p is None: + return False + p.set(value) + return True +png = o["png"] +frame = o["frame"] +rop = hou.node(o["rop"]) if o.get("rop") else None +if o.get("rop") and rop is None: + raise ValueError("render.rop: no node %r" % o["rop"]) +lop = hou.node(o["lop"]) if o.get("lop") else None +cam = hou.node(o["camera"]) if o.get("camera") else None +if o.get("camera") and cam is None: + raise ValueError("render.camera: no node %r" % o["camera"]) +if rop is None: + if lop is None and cam is None: + cam = first_camera() + if cam is None: + stage_net = hou.node("/stage") + shown = stage_net.displayNode() if stage_net is not None and stage_net.children() else None + if shown is None: + raise ValueError("render: no camera in /obj and no LOP display node in /stage: give render.camera, lop or rop") + lop = shown + if lop is not None: + rop = hou.node("/out").createNode("usdrender", "opencode_verify") + rop.parm("loppath").set(lop.path()) + set_parm(rop, "renderer", "BRAY_HdKarma") + set_parm(rop, "outputimage", png) + set_parm(rop, "override_res", "specific") + set_parm(rop, "res_user", (o["size"], int(o["size"] * 0.75))) + if o.get("renderer") == "opengl": + rop.destroy() + rop = hou.node("/out").createNode("opengl", "opencode_verify") + set_parm(rop, "opsource", "lop") or set_parm(rop, "opsource", 1) + rop.parm("loppath").set(lop.path()) + facts["source"] = "LOP %s" % lop.path() + else: + kind = "opengl" if o.get("renderer") == "opengl" else "karma" + rop = hou.node("/out").createNode(kind, "opencode_verify") + rop.parm("camera").set(cam.path()) + facts["source"] = "camera %s" % cam.path() + facts["camera"] = cam.path() +made = rop.name() == "opencode_verify" +kind = rop.type().name().split("::")[0] +facts["rop"] = o.get("rop") or "a temporary %s ROP" % kind +facts["renderer"] = kind +cam_node = hou.node(rop.parm("camera").eval()) if rop.parm("camera") and rop.parm("camera").eval() else None +cam_res = tuple(cam_node.parmTuple("res").eval()) if cam_node is not None and cam_node.parmTuple("res") else (4, 3) +w, h = res_for(cam_res) +if kind == "opengl": + set_parm(rop, "tres", True); set_parm(rop, "res", (w, h)); set_parm(rop, "picture", png) +elif kind == "karma": + set_parm(rop, "resolutionx", w); set_parm(rop, "resolutiony", h); set_parm(rop, "picture", png) + set_parm(rop, "samplesperpixel", min(rop.evalParm("samplesperpixel") if rop.parm("samplesperpixel") else 9, 9)) +elif kind == "usdrender": + set_parm(rop, "outputimage", png); set_parm(rop, "override_res", "specific"); set_parm(rop, "res_user", (w, h)) +elif kind == "ifd": + set_parm(rop, "vm_picture", png); set_parm(rop, "override_camerares", True) + set_parm(rop, "res_fraction", "specific"); set_parm(rop, "res_override", (w, h)) +else: + raise ValueError("render.rop: %s is a %s ROP; verify renders opengl, karma, usdrender or ifd (Mantra) ROPs" % (rop.path(), kind)) +for name in ("trange",): + set_parm(rop, name, 1) +started = time.time() +try: + rop.render(frame_range=(frame, frame), verbose=False, output_progress=False) +except hou.OperationFailed as ex: + facts["render_error"] = str(ex)[:500] +facts["seconds"] = round(time.time() - started, 2) +messages = list(rop.errors()) + ["warning: " + m for m in rop.warnings()] +if messages: + facts["rop_messages"] = [" ".join(m.split())[:300] for m in messages[:6]] +if not os.path.isfile(png): + raise RuntimeError("the %s render wrote no image%s" % (kind, (": " + "; ".join(facts.get("rop_messages", [])))[:600])) +from PIL import Image +im = Image.open(png).convert("RGBA") +iw, ih = im.size +px = list(im.getdata()) +n = len(px) +lum = [(0.2126 * r + 0.7152 * g + 0.0722 * b) / 255.0 for r, g, b, a in px] +alpha = [a / 255.0 for r, g, b, a in px] +mean = sum(lum) / n +std = (sum((x - mean) ** 2 for x in lum) / n) ** 0.5 +# The Apprentice watermark sits in the bottom-right corner: measured apart, so a black scene still reads black. +corner = [lum[y * iw + x] for y in range(int(ih * 0.85), ih) for x in range(int(iw * 0.7), iw)] +body = [lum[y * iw + x] for y in range(ih) for x in range(iw) if not (y >= int(ih * 0.85) and x >= int(iw * 0.7))] +facts["image"] = {"size": [iw, ih], "mean_luminance": round(mean, 4), "max_luminance": round(max(lum), 4), + "luminance_std": round(std, 4), "mean_alpha": round(sum(alpha) / n, 4), + "transparent_fraction": round(sum(1 for a in alpha if a < 0.01) / n, 4), + "black": max(body) < 0.02, "uniform": (sum((x - sum(body) / len(body)) ** 2 for x in body) / len(body)) ** 0.5 < 0.002, + "corner_max_luminance": round(max(corner), 4)} +facts["frame"] = frame +with open(png, "rb") as fh: + out["image_base64"] = base64.b64encode(fh.read()).decode("ascii") +''' + + +def render_options(spec): + spec = {} if spec is True else dict(spec) + unknown = set(spec) - {"rop", "camera", "lop", "renderer", "size", "frame"} + if unknown: + raise ValueError("render: unknown keys %s; give {rop?, camera?, lop?, renderer?: karma or opengl, size?, frame?}" % sorted(unknown)) + size = int(spec.get("size", 320)) + if not 64 <= size <= 1024: + raise ValueError("render.size: 64 to 1024 pixels (longest side)") + if spec.get("renderer") not in (None, "karma", "opengl"): + raise ValueError("render.renderer: karma or opengl") + return {"rop": spec.get("rop"), "camera": spec.get("camera"), "lop": spec.get("lop"), "renderer": spec.get("renderer"), + "size": size, "frame": float(spec["frame"]) if spec.get("frame") is not None else FRAME} + + +def hython(): + exe = os.path.join(hou.getenv("HFS") or "", "bin", "hython.exe" if os.name == "nt" else "hython") + if not os.path.isfile(exe): + raise RuntimeError("no hython at %s" % exe) + return exe + + +def child_env(role): + env = {k: v for k, v in os.environ.items() if k != "OPENCODE_HOUDINI_OFFER_KEY"} + env["OPENCODE_HOUDINI_ROLE"] = role # never offered to OpenCode + return env + + +def render_image(options): + work = tempfile.mkdtemp(prefix="opencode_verify_render_") + options["png"] = os.path.join(work, "verify.png").replace("\\", "/") + if THROWAWAY: + # Already a background hython on its own copy (houdini_verify saved): render here. + scope = {"OPTIONS": options, "__name__": "__opencode_render__"} + try: + exec(compile(RENDER_SCRIPT, "", "exec"), scope) # noqa: S102 + got = scope["out"] + finally: + try: + os.remove(options["png"]) + os.rmdir(work) + except OSError: + pass + got["facts"]["by"] = "this background hython (on the file read)" + return got + started = time.time() + copy = os.path.join(work, "working_copy" + (os.path.splitext(hou.hipFile.path())[1] or ".hipnc")).replace("\\", "/") + out_err = hou.hscript('mwrite -n "%s"' % copy) + if not os.path.isfile(copy): + raise RuntimeError("the working copy was not written: %s" % " ".join(out_err).strip()[:300]) + result_path = os.path.join(work, "result.json") + script = os.path.join(work, "render.py") + with open(script, "w", encoding="utf-8") as fh: + fh.write("import json, sys, traceback, hou\n") + fh.write("OPTIONS = json.loads(%r)\n" % json.dumps(options)) + fh.write("try:\n hou.hipFile.load(%r, suppress_save_prompt=True, ignore_load_warnings=True)\n" % copy) + fh.write(" exec(compile(%r, '', 'exec'))\n" % RENDER_SCRIPT) + fh.write("except Exception:\n out = {'error': traceback.format_exc()[-2000:]}\n") + fh.write("with open(%r, 'w', encoding='utf-8') as fh:\n json.dump(out, fh)\n" % result_path) + fh.write("sys.stdout.flush()\nimport os\nos._exit(0)\n") + kwargs = {"stdin": subprocess.DEVNULL, "stdout": subprocess.PIPE, "stderr": subprocess.STDOUT, "env": child_env("verify")} + if os.name == "nt": + kwargs["creationflags"] = 0x08000000 # no console window + try: + run = subprocess.run([hython(), script], timeout=int(P.get("timeout_s") or 300), **kwargs) + if not os.path.isfile(result_path): + tail = run.stdout.decode("utf-8", "replace")[-1500:] if run.stdout else "" + raise RuntimeError("the render hython gave no result (exit %s):\n%s" % (run.returncode, tail)) + with open(result_path, encoding="utf-8") as fh: + got = json.load(fh) + finally: + for name in os.listdir(work): + try: + os.remove(os.path.join(work, name)) + except OSError: + pass + try: + os.rmdir(work) + except OSError: + pass + if "error" in got: + raise RuntimeError("render: " + got["error"]) + got["facts"]["by"] = "a background hython on a working copy of the open scene (written with mwrite -n: the open file keeps its name)" + got["facts"]["total_seconds"] = round(time.time() - started, 1) + return got + + +image = None +if P.get("render"): + rendered = render_image(render_options(P["render"])) + facts["render"] = rendered["facts"] + image = rendered.get("image_base64") + if license_name() in ("Apprentice", "ApprenticeHD", "Education"): + facts["render"]["license_note"] = ("%s: every image carries the Houdini logo in its bottom-right corner " + "(read apart: corner_max_luminance)" % license_name()) +facts["seconds"] = round(time.time() - STARTED, 2) +passed = sum(1 for r in rows if r["passed"]) +result = {"houdini": {"version": hou.applicationVersionString(), "license": license_name()}, + "passed": passed, "failed": len(rows) - passed, "all_passed": passed == len(rows), + "failed_labels": [r["label"] for r in rows if not r["passed"]], "criteria": rows, "errors": error_rows, + "facts": facts, **({"image_base64": image} if image else {})} diff --git a/integrations/houdini/plugin/programs/verify_saved.py b/integrations/houdini/plugin/programs/verify_saved.py new file mode 100644 index 0000000..1f63a4b --- /dev/null +++ b/integrations/houdini/plugin/programs/verify_saved.py @@ -0,0 +1,76 @@ +# SPDX-License-Identifier: MIT +# houdini_verify with saved (fixed program): the same verification, read from the file as written on disk instead +# of the open scene: a background hython loads the .hip/.hipnc and runs the verify program there (criteria, +# errors, render), so what is delivered can be compared with what the open scene shows. That hython never offers +# itself to OpenCode (OPENCODE_HOUDINI_ROLE=verify, no offer key) and opens no window. +# PARAMS: the verify parameters with "saved": true (the open file as saved) or a .hip/.hipnc path; "timeout_s"?. +# JOB: verify.py, prepended by the plugin. +import json +import os +import shutil +import subprocess +import tempfile +import time + +import hou + +P = PARAMS # noqa: F821 +target = P.get("saved") +current = hou.hipFile.path() +never_saved = hou.hipFile.isNewFile() or not os.path.isfile(current) +if target is True: + if never_saved: + raise ValueError("saved: the open scene was never saved, there is no file to read (houdini_save first)") + path = current +elif isinstance(target, str) and target: + base = os.path.dirname(current) if not never_saved else os.getcwd() + path = os.path.normpath(target if os.path.isabs(target) else os.path.join(base, target)) +else: + raise ValueError("saved: true (the open scene as saved) or a .hip, .hipnc or .hiplc path") +if not os.path.isfile(path): + raise ValueError("saved: no file %s" % path) + +exe = os.path.join(hou.getenv("HFS") or "", "bin", "hython.exe" if os.name == "nt" else "hython") +if not os.path.isfile(exe): + raise RuntimeError("no hython at %s" % exe) +params = {key: value for key, value in P.items() if key != "saved"} +work = tempfile.mkdtemp(prefix="opencode_verify_saved_") +started = time.time() +try: + out_path = os.path.join(work, "result.json") + script = os.path.join(work, "verify.py") + with open(script, "w", encoding="utf-8") as fh: + fh.write("import json, os, sys, traceback\nimport hou\n") + fh.write("warnings = []\ntry:\n hou.hipFile.load(%r, suppress_save_prompt=True, ignore_load_warnings=False)\n" % path.replace("\\", "/")) + fh.write("except hou.LoadWarning as w:\n warnings.append(str(w)[:1500])\n") + fh.write("try:\n PARAMS = json.loads(%r)\n result = {}\n" % json.dumps(params)) + fh.write(" exec(compile(%r, '', 'exec'))\n" % JOB) # noqa: F821 - verify.py, from the plugin + fh.write(" result['load_warnings'] = warnings\n") + fh.write("except Exception:\n result = {'error': traceback.format_exc()[-3000:]}\n") + fh.write("with open(%r, 'w', encoding='utf-8') as _out:\n json.dump(result, _out, default=repr)\n" % out_path) + # hython may crash in its own exit handlers after renders: the result is written, leave at once. + fh.write("sys.stdout.flush()\nos._exit(0)\n") + env = {k: v for k, v in os.environ.items() if k != "OPENCODE_HOUDINI_OFFER_KEY"} + env["OPENCODE_HOUDINI_ROLE"] = "verify" # never offered to OpenCode + kwargs = {"stdin": subprocess.DEVNULL, "stdout": subprocess.PIPE, "stderr": subprocess.STDOUT, "env": env} + if os.name == "nt": + kwargs["creationflags"] = 0x08000000 # no console window + try: + run = subprocess.run([exe, script], timeout=int(P.get("timeout_s") or 540), **kwargs) + except subprocess.TimeoutExpired: + raise RuntimeError("the background hython reading %s did not finish within %s s (timeout_s)" % (path, P.get("timeout_s") or 540)) + if not os.path.exists(out_path): + tail = run.stdout.decode("utf-8", "replace")[-1500:] if run.stdout else "" + raise RuntimeError("the background hython reading %s gave no result (exit %s):\n%s" % (path, run.returncode, tail)) + with open(out_path, encoding="utf-8") as fh: + result = json.load(fh) +finally: + shutil.rmtree(work, ignore_errors=True) +if "error" in result: + raise RuntimeError("verify in the background hython reading %s failed:\n%s" % (path, result["error"])) +stat = os.stat(path) +load_warnings = result.pop("load_warnings", []) +result["read"] = {"file": path, "saved_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stat.st_mtime)), + "bytes": stat.st_size, "seconds": round(time.time() - started, 1), + "by": "a background hython on the file as written on disk, not the open scene", + **({"load_warnings": load_warnings} if load_warnings else {})} diff --git a/integrations/houdini/plugin/scripts/build-host.ts b/integrations/houdini/plugin/scripts/build-host.ts index 0b012a1..ac585df 100644 --- a/integrations/houdini/plugin/scripts/build-host.ts +++ b/integrations/houdini/plugin/scripts/build-host.ts @@ -36,6 +36,16 @@ const entries = walk(host).map((file) => ({ data: Buffer.from(readFileSync(file, "utf8").replaceAll("\r\n", "\n")), })) entries.push({ name: "sidecar.cjs", data: sidecar }) +// The kit's discovery client, which the installed part's advertiser uses to offer Houdini to OpenCode. +entries.push({ + name: "scripts/opencode_houdini/opencode_service.py", + data: Buffer.from( + readFileSync( + join(root, "..", "..", "..", "packages", "integration-kit", "python", "opencode_service.py"), + "utf8", + ).replaceAll("\r\n", "\n"), + ), +}) const payload = writeStoredZip(entries.sort((a, b) => (a.name < b.name ? -1 : 1))) const name = `host/houdini/opencode_link-${pkg.version}.zip` const digest = createHash("sha256").update(payload).digest("hex") diff --git a/integrations/houdini/plugin/skills/houdini-pipeline/SKILL.md b/integrations/houdini/plugin/skills/houdini-pipeline/SKILL.md deleted file mode 100644 index 3d09124..0000000 --- a/integrations/houdini/plugin/skills/houdini-pipeline/SKILL.md +++ /dev/null @@ -1,18 +0,0 @@ ---- - -name: houdini-pipeline -description: Revision-checked Houdini graph work through the attached OpenCode Link host. -autoinvoke: false ---- - -# Houdini pipeline - -List `houdini_hosts` and use `houdini_attach` with permission when the user wants to attach an offered application. Never infer an active scene. - -Read `houdini_context` for the current `script_handle`, revision and narrow working set (`refs`, `match`, `radius`, `include`, `limit`). The initial read can omit the handle. Subsequent calls use the returned handle. - -Use `houdini_apply` with the exact revision, a fresh `command_id`, and 1–50 operations: `node.create`, `node.rename`, `parm.set_value`, `graph.connect`, `graph.delete`. Each operation is preflighted; parameter expressions and arbitrary code are refused. Read the returned affordances and node-type registry before constructing a graph. Aliases from `bind` are referenced with `$name`. - -Start with `dry_run: true`. Mutation requires `dry_run: false` and `confirm: true`, under `houdini_write` permission. Expectations are preconditions, checked before mutation. Inspect the resulting revision with `houdini_context`. An interrupted request may still finish: read first and retry the identical command ID instead of making a new one. - -An apply groups undo and attempts rollback on failure; failure to restore quarantines writes until the HIP is reloaded. Headless mutations are refused. Code execution, rendering, saving files and network imports are outside the supported operation set. diff --git a/integrations/houdini/plugin/src/closed.ts b/integrations/houdini/plugin/src/closed.ts new file mode 100644 index 0000000..b248d3b --- /dev/null +++ b/integrations/houdini/plugin/src/closed.ts @@ -0,0 +1,22 @@ +// What became of a Houdini that stopped answering a conversation (the kit's crashWatch): crashed, closed, or +// alive without renewing its offer. A crashing Houdini writes to its temporary folder (houdini_temp, shared by every +// Houdini of the user) crash..__log.txt, and the scene as it was at the crash, +// crash.._.hip(nc|lc): only the files naming this process count. + +import { crashWatch } from "./kit.ts" + +const SCENE = /\.hip(nc|lc)?$/i + +export const { crashReport, closedMessage, silentMessage } = crashWatch({ + appName: "Houdini", + app: "houdini", + part: "OpenCode Link", + isReport: (name, host) => { + const lower = name.toLowerCase() + return lower.startsWith("crash.") && new RegExp(`_${host.pid}(_log\\.txt|\\.hip(nc|lc)?)$`).test(lower) + }, + crashNote: (report) => + SCENE.test(report) + ? "Houdini saved the scene as it was at the crash in that file: open it to recover the changes since the last save." + : undefined, +}) diff --git a/integrations/houdini/plugin/src/host-install.ts b/integrations/houdini/plugin/src/host-install.ts index f89f66d..c13b51a 100644 --- a/integrations/houdini/plugin/src/host-install.ts +++ b/integrations/houdini/plugin/src/host-install.ts @@ -1,16 +1,12 @@ import { createHash, randomUUID } from "node:crypto" import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs" import { dirname, join, resolve } from "node:path" - -import type { - HostInspection, - InstallHostOutput, - IntegrationMarker, -} from "../../../../packages/integration-kit/src/index.ts" import { readStoredZip } from "../../../../packages/integration-kit/src/zip.ts" +import type { HostInspection, InstallHostOutput, IntegrationMarker } from "./kit.ts" import { packageRoot, stateDir } from "./paths.ts" -// Houdini's preference folder is version-specific. The user selects it explicitly; never guess a version. +// Houdini's preferences folder is version-specific: hostInspectionDir, or the newest existing one of Houdini 21 or +// later (paths.ts). It is never created here: Houdini creates it when it first starts. export function inspectHost(preferences?: string): HostInspection { if (!preferences || !existsSync(preferences)) return { kind: "not-inspected" } try { @@ -28,7 +24,8 @@ export function installHost(marker: IntegrationMarker, preferences?: string, sta if (!preferences || !existsSync(preferences)) return { outcome: "refused", - detail: "Set hostInspectionDir to the existing Houdini user preferences folder, then retry.", + detail: + "No Houdini 21 or later preferences folder: start Houdini once, or set hostInspectionDir to its preferences folder.", } const host = marker.host if (!host) return { outcome: "refused" } @@ -86,6 +83,6 @@ export function installHost(marker: IntegrationMarker, preferences?: string, sta } return { outcome: current.kind === "found" ? "updated" : "installed", - detail: "Open Houdini, then open the OpenCode Link panel and choose Connect.", + detail: "Restart Houdini: OpenCode Link offers it to OpenCode when it starts.", } } diff --git a/integrations/houdini/plugin/src/integration.ts b/integrations/houdini/plugin/src/integration.ts index 346e842..7b9307b 100644 --- a/integrations/houdini/plugin/src/integration.ts +++ b/integrations/houdini/plugin/src/integration.ts @@ -1,81 +1,140 @@ +// OpenCode for Houdini: RPC status contract, tools and the working method. Loading has no side effect: no +// socket, process or installation; the host is reached only once a conversation is attached to an offer validated +// by hello. The conversations' attachment and host calls are the kit's host session, as for Blender. + import { readFileSync } from "node:fs" import { join } from "node:path" + import type { Plugin } from "@opencode/plugin" -import type { Info, ToolContext } from "@opencode/plugin/promise/tool" +import type { ToolContext } from "@opencode/plugin/promise/tool" + +import { closedMessage, crashReport, silentMessage } from "./closed.ts" +import { inspectHost, installHost } from "./host-install.ts" import { + type AttachmentStorage, + buildPluginTools, + createAttachments, + createHostSession, createIntegrationRpc, + HELLO_TIMEOUT_MS, + HostError, + hostErrorWith, type IntegrationMarker, + isWslDaemon, + loadMethodRegistry, type OfferRegistry, + readScript, + registerPluginTools, requestHost, + type SessionBindings, sharedOfferRegistry, -} from "../../../../packages/integration-kit/src/index.ts" -import { inspectHost, installHost } from "./host-install.ts" -import { packageRoot } from "./paths.ts" + type ToolContent, + trackAdvertise, +} from "./kit.ts" +import { legacyInstalls } from "./legacy.ts" +import { type EntryPut, methodAttacher } from "./method.ts" +import { type HoudiniOptions, readOptions } from "./options.ts" +import { defaultPathEnv, defaultPrefsDir, methodsDir, type PathEnv, packageRoot } from "./paths.ts" +import { type ProgramName, programCode } from "./programs.ts" +import { addTask, cancelTask, getTask, isRenderStart, taskStatus } from "./tasks.ts" -export function readPackage() { - return JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8")) as { - version: string - opencodeIntegration: IntegrationMarker - } +export const APP = "houdini" +const NAMESPACE_DESCRIPTION = "Houdini, through OpenCode Link: read and change the attached scene." + +export const PERMISSIONS = { + attach: "houdini_attach", + install: "houdini_install_host", + executeCode: "houdini_execute_code", + write: "houdini_write", +} as const + +const MAX_TEXT = 100_000 +// A verification may cook the network, render or start a background hython on the saved file. +const VERIFY_TIMEOUT_MS = 600_000 +// Longest wait of one houdini_render call that follows a task. +export const RENDER_WAIT_MAX_S = 3_600 + +const HINTS: Record = { + stale_handle: + "The attached Houdini opened another file: call houdini_context again (houdini_hosts shows the open Houdinis).", + stale_revision: "The scene changed since your last read: call houdini_context and use its revision.", + expectation_failed: "An expect clause does not hold: nothing was changed.", + mode_conflict: "houdini_apply changes the scene only in Houdini's interface: in hython, use houdini_execute_code.", + busy: "Houdini is busy (a modal dialog, a long cook or render): try again shortly.", + quarantined: + "A failed change could not be proven undone: read the scene (houdini_context), check it, then give that read's revision as release_quarantine.", + method_disabled: "Code execution is not available in the attached Houdini.", + method_not_found: + "The attached Houdini's OpenCode Link lacks this method: install this plugin's OpenCode Link (houdini_install_host) and restart Houdini.", + invalid_args: "Houdini refused the request.", + unreachable: "The attached Houdini is unreachable: it may have closed or stopped offering itself.", + timeout: + "Houdini did not answer in time; the change may still complete. Call houdini_context, then retry with the same command_id if it did not: a replayed command_id applies nothing twice.", + cancelled: + "The request was cancelled; a change may still complete in Houdini. Call houdini_context, then retry with the same command_id if it did not.", +} + +// The installed part's own text and data follow the hint (the kit's message after its "Host refused" prefix). +const hostError = hostErrorWith(HINTS, "Houdini", (error: HostError) => + error.message.replace(/^Host refused the request \([^)]*\)[.:]?\s*/, ""), +) + +export type HoudiniDeps = { + env?: NodeJS.ProcessEnv + paths?: PathEnv + // A private registry for tests; the daemon shares one registry across Locations. + registry?: OfferRegistry + // The clock of the stored attachments' lease, for tests. + now?: () => number + wsl?: () => boolean + // The installed payload's folder, for tests. + state?: string +} + +type PackageJson = { version: string; opencodeIntegration: IntegrationMarker } + +export function readPackage(): PackageJson { + return JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8")) as PackageJson } // 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 - return sharedOfferRegistry("houdini", { + return sharedOfferRegistry(APP, { expected: { protocol: host.protocol, range: host.range, appVersions: host.appVersions }, - hello: (endpoint) => requestHost(endpoint, "hello", {}, { timeoutMs: 2000 }), + hello: (endpoint) => requestHost(endpoint, "hello", {}, { timeoutMs: HELLO_TIMEOUT_MS }), }) } -const contextSchema = { - type: "object", - properties: { - script_handle: { type: "string" }, - if_revision: { type: "integer", minimum: 0 }, - include: { type: "array", items: { type: "string" }, maxItems: 20, uniqueItems: true }, - refs: { type: "array", items: { type: "string" }, maxItems: 50 }, - match: { - type: "object", - properties: { name: { type: "string" }, type: { type: "string" } }, - additionalProperties: false, - }, - radius: { type: "integer", minimum: 0, maximum: 2 }, - limit: { type: "integer", minimum: 1, maximum: 100 }, - }, - additionalProperties: false, +function bounded(text: string) { + return text.length > MAX_TEXT + ? `${text.slice(0, MAX_TEXT)}\n[truncated: ${text.length - MAX_TEXT} more characters]` + : text } -const applySchema = { - type: "object", - properties: { - script_handle: { type: "string" }, - if_revision: { type: "integer", minimum: 0 }, - command_id: { type: "string", minLength: 1 }, - summary: { type: "string", maxLength: 300 }, - intent: { type: "object" }, - patch: { type: "object" }, - operations: { type: "array", minItems: 1, maxItems: 50, items: { type: "object" } }, - expect: { type: "array", maxItems: 50, items: { type: "object" } }, - dry_run: { type: "boolean", default: true }, - confirm: { type: "boolean", default: false }, - }, - required: ["script_handle", "if_revision", "command_id"], - additionalProperties: false, + +function json(value: unknown) { + return bounded(JSON.stringify(value)) } -export async function setupHoudini( - ctx: Plugin.Context, - deps: { registry?: OfferRegistry; wsl?: () => boolean; state?: string } = {}, -) { +// After a save: what reads the file back, as written on disk (the open scene can differ from it). +export function verifySavedHint(saved: true | string) { + return `houdini_verify with saved: ${saved === true ? "true" : JSON.stringify(saved)} reads what this file holds.` +} + +type ProgramRun = { result?: unknown; status?: string; message?: string; traceback?: string; hint?: string } + +export async function setupHoudini(ctx: Plugin.Context, deps: HoudiniDeps = {}) { + const env = deps.env ?? process.env + const paths = deps.paths ?? defaultPathEnv() + const options: HoudiniOptions = readOptions(ctx.options as Record | undefined, env) const pkg = readPackage() const marker = pkg.opencodeIntegration const registry = deps.registry ?? createRegistry(marker) - const raw = ctx.options as { hostInspectionDir?: unknown } | undefined - const preferences = - typeof raw?.hostInspectionDir === "string" - ? raw.hostInspectionDir - : process.env.NEURALNOMADS_HOUDINI_HOST_INSPECTION_DIR + const preferences = () => options.hostInspectionDir ?? defaultPrefsDir(paths) ?? undefined + const legacy = () => legacyInstalls(paths, options.hostInspectionDir).filter((found) => found.enabled) + const manifests = loadMethodRegistry(methodsDir) + const rpc = createIntegrationRpc({ marker, version: pkg.version, @@ -84,142 +143,298 @@ export async function setupHoudini( location: ctx.location, get: ctx.session?.get ? (input) => ctx.session.get(input as Parameters[0]) : undefined, }, - inspectHost: async () => inspectHost(preferences), - installHost: async () => installHost(marker, preferences, deps.state), + inspectHost: async () => inspectHost(preferences()), + installHost: async () => installHost(marker, preferences(), deps.state), wsl: deps.wsl, + notices: async () => (legacy().length ? ["legacy_install"] : []), }) - const bindings = rpc.bindings as NonNullable - const assert = (tool: ToolContext, action: string, resources = ["*"]) => - ctx.permission.assert({ - sessionID: tool.sessionID, - agent: tool.agent, + const bindings = rpc.bindings as SessionBindings + // Attachments kept across an OpenCode restart and a file change (a new offer under the same key). + const attachments = createAttachments({ + app: APP, + registry, + storage: (ctx as unknown as { storage?: AttachmentStorage }).storage, + now: deps.now, + owns: (sessionID) => bindings.snapshot(sessionID), + }) + const handlers: Record Promise> = { + ...rpc.handlers, + advertise: trackAdvertise(registry, (input) => rpc.handlers.advertise?.(input) as Promise, APP), + attachHost: async (input: unknown, context?: { signal?: AbortSignal }) => { + const result = (await rpc.handlers.attachHost?.(input, context)) as { outcome?: string } + const sessionID = (input as { sessionID?: unknown } | null)?.sessionID + if (result?.outcome === "attached" && typeof sessionID === "string") await attachments.remember(sessionID) + return result + }, + // The stored attachment goes first, once the session is verifiably this Location's: the detach's own change + // event must not find it and attach the conversation back. + detachHost: async (input: unknown, context?: { signal?: AbortSignal }) => { + const sessionID = (input as { sessionID?: unknown } | null)?.sessionID + if (typeof sessionID === "string" && (await bindings.snapshot(sessionID, context?.signal))) + await attachments.forget(sessionID) + return rpc.handlers.detachHost?.(input, context) + }, + } + + const assert = async ( + context: ToolContext, + action: string, + resources: string[], + metadata?: Record, + ) => { + await ctx.permission.assert({ + sessionID: context.sessionID, + agent: context.agent, action, resources, - source: { type: "tool", messageID: tool.messageID, id: tool.id }, + ...(metadata ? { metadata } : {}), + source: { type: "tool", messageID: context.messageID, id: context.id }, } as Parameters[0]) - const tools: Info[] = [] - const add = ( - name: string, - description: string, - input: object, - execute: (input: Record, tool: ToolContext) => Promise, - ) => { - tools.push({ - name, - description, - input, - options: { namespace: "houdini", codemode: true }, - async execute(input, tool) { - return { content: JSON.stringify(await execute(input as Record, tool)) } - }, - } as Info) } - const call = async (method: string, input: Record, tool: ToolContext) => { - const ticket = await bindings.binding(tool.sessionID, tool.signal) - if (input.script_handle && input.script_handle !== ticket.bound.scriptHandle) - throw new Error("The script handle is not attached to this conversation.") - if (method === "apply" && input.dry_run === false) { - if (input.confirm !== true) throw new Error("Mutations require dry_run=false and confirm=true.") - await assert(tool, "houdini_write") - if (!(await bindings.verify(tool.sessionID, ticket.authority, tool.signal))) - throw new Error("Application attachment changed during permission.") - } - if (tool.signal.aborted || !registry.current(ticket.authority)) - throw new Error("Application attachment is unverifiable.") - return requestHost( - ticket.bound.endpoint, - method, - { ...input, script_handle: ticket.bound.scriptHandle, session_id: tool.sessionID }, - { signal: tool.signal }, - ) + + // The method joins a conversation when it uses a Houdini (an instructions entry, where OpenCode has them). + const instructions = (ctx as unknown as { session?: { instructions?: { entry?: { put?: EntryPut } } } }).session + const put = instructions?.instructions?.entry?.put + const attachMethod = methodAttacher(put ? (input) => put.call(instructions?.instructions?.entry, input) : undefined) + + const hostSession = createHostSession({ + texts: { + appName: "Houdini", + app: APP, + part: "OpenCode Link", + partKind: "OpenCode Link", + noneOffered: + "No Houdini is offered: start Houdini 21 or later with OpenCode Link installed (houdini_install_host); it offers itself when it starts.", + installHint: `install OpenCode Link ${pkg.version} (houdini_install_host) and restart Houdini`, + }, + registry, + bindings, + attachments, + attachMethod, + crash: { crashReport, closedMessage, silentMessage }, + wsl: deps.wsl ?? (() => isWslDaemon()), + requestTimeoutMs: options.requestTimeoutMs, + hostError, + }) + const { attachedTo, ticket, callHost, hello, maintain } = hostSession + + // Every host call names its conversation: the installed part keeps per-conversation cancellation. + const call = ( + context: ToolContext, + method: string, + params: Record, + timeoutMs?: number, + captured?: Parameters[4], + ) => callHost(context, method, { session_id: context.sessionID, ...params }, timeoutMs, captured) + + // A fixed program through the host's code execution, read only (programs.ts). + const program = async (context: ToolContext, name: ProgramName, params: unknown, timeoutMs?: number) => { + const out = (await call( + context, + "execute_code", + { code: programCode(name, params), read_only: true }, + timeoutMs, + )) as ProgramRun + if (out.status === "failed") + throw new Error( + `houdini_${name}: ${String(out.message ?? "")}${out.hint ? `\n${out.hint}` : ""}\n${String(out.traceback ?? "").slice(-1200)}`, + ) + return out.result } - add( - "context", - "Read bounded context from the attached Houdini graph, including its current handle and revision.", - contextSchema, - (input, tool) => call("context", input, tool), - ) - add( - "apply", - "Preflight or apply a revision-checked Houdini graph intent. Defaults to dry-run; mutation requires confirm=true.", - applySchema, - (input, tool) => call("apply", input, tool), - ) - add( - "hosts", - "List Houdini hosts offered by the user.", - { type: "object", additionalProperties: false }, - async (_input, tool) => { - const authority = await bindings.snapshot(tool.sessionID, tool.signal) - if (!authority) throw new Error("Session ownership is unverifiable.") - await assert(tool, "houdini_attach") - if (!(await bindings.verify(tool.sessionID, authority, tool.signal)) || !registry.current(authority)) - throw new Error("Session ownership or host offers changed.") - return registry.hosts() + + const withImage = (value: unknown, name: string): string | ToolContent[] => { + const { image_base64, ...rest } = (value ?? {}) as { image_base64?: string } & Record + const text = json(rest) + if (typeof image_base64 !== "string" || !image_base64) return text + return [ + { type: "text", text }, + { type: "file", uri: `data:image/png;base64,${image_base64}`, mime: "image/png", name }, + ] + } + + const executors: Record< + string, + (args: Record, context: ToolContext) => Promise + > = { + async houdini_hosts(args, context) { + const out = await hostSession.hosts(args, context, { + allow: (held, offer) => assert(held, PERMISSIONS.attach, [offer.label], { label: offer.label }), + status: () => rpc.status(), + list: "houdinis", + }) + const found = legacy() + return json( + found.length + ? { + ...out, + legacy_install: { + found, + fix: 'An earlier OpenCode package also loads in Houdini (another panel and host): remove it, or set "enable": false in it, then restart Houdini.', + }, + } + : out, + ) }, - ) - add( - "attach", - "Attach a user-offered Houdini to this conversation, with permission.", - { type: "object", properties: { offerID: { type: "string" } }, required: ["offerID"], additionalProperties: false }, - async (input, tool) => { - const authority = await bindings.snapshot(tool.sessionID, tool.signal) - if (!authority) return { outcome: "refused" } - await assert(tool, "houdini_attach") - if (!(await bindings.verify(tool.sessionID, authority, tool.signal)) || !registry.current(authority)) - return { outcome: "refused" } - return registry.attach(String(input.offerID), tool.sessionID) + + async houdini_install_host(_args, context) { + await assert(context, PERMISSIONS.install, ["*"]) + return json(await handlers.installHost?.({})) }, - ) - add( - "detach", - "Detach this conversation from Houdini.", - { type: "object", additionalProperties: false }, - (_input, tool) => bindings.detach(tool.sessionID, tool.signal), - ) - add( - "install_host", - "Install the embedded OpenCode Link host in the configured Houdini preferences folder.", - { type: "object", additionalProperties: false }, - async (_input, tool) => { - await assert(tool, "houdini_install_host") - return rpc.handlers.installHost?.({}) + + async houdini_context(args, context) { + const { release_quarantine: release, ...query } = args + if (release === undefined) + return json({ + ...((await call(context, "context", query)) as object), + attached_to: attachedTo(context.sessionID), + }) + // After a failed change whose undo could not be proven: the agent read the scene (this revision) and lifts + // the quarantine; the scene is read again with it. + const released = (await call(context, "release", { revision: release })) as { released?: boolean } + return json({ + ...((await call(context, "context", query)) as object), + attached_to: attachedTo(context.sessionID), + released: released.released === true, + }) + }, + + // The typed intent, unchanged: a dry run by default; a change needs confirm and the write permission. + async houdini_apply(args, context) { + const held = await ticket(context) + const applying = args.dry_run === false + if (applying) { + if (args.confirm !== true) throw new Error("A change needs dry_run: false and confirm: true.") + await assert(context, PERMISSIONS.write, [held.label ?? "*"], { operations: args.operations }) + } + return json(await call(context, "apply", args, undefined, applying ? held : undefined)) + }, + + // The dangerous method: every run asks permission. + async houdini_execute_code(args, context) { + // A script file is read here, byte for byte: no escaping of the code through a JSON or JavaScript string. + const script = + typeof args.path === "string" && args.path ? readScript(args.path, ctx.location?.directory) : undefined + if (script && args.code !== undefined) throw new Error("Give code or path, not both.") + const code = script?.code ?? String(args.code ?? "") + if (!code.trim()) throw new Error(script ? `${script.path} is empty.` : "code or path is required.") + const held = await ticket(context) + if (!held.bound.methods.includes("execute_code")) throw new HostError("method_disabled", "") + await assert(context, PERMISSIONS.executeCode, [script?.path ?? "*"], { + ...(script ? { path: script.path } : {}), + code: code.length > 4_000 ? `${code.slice(0, 4_000)}\n…` : code, + }) + const seconds = typeof args.timeout_s === "number" ? Math.min(Math.max(args.timeout_s, 1), 3_600) : undefined + const out = (await call( + context, + "execute_code", + { + code, + ...(script ? { filename: script.path } : {}), + ...(args.read_only === true ? { read_only: true } : {}), + }, + seconds ? seconds * 1_000 : undefined, + held, + )) as Record + return json(out.saved === true ? { ...out, check: verifySavedHint(true) } : out) + }, + + async houdini_inspect(args, context) { + if (args.search === undefined && args.type === undefined && args.node === undefined && args.hou === undefined) + throw new Error("Give search, type, node and/or hou.") + return json(await program(context, "inspect", args)) + }, + + async houdini_verify(args, context) { + // saved: the file as written on disk, read by a background hython, instead of the open scene. + const saved = args.saved !== undefined && args.saved !== false + if (saved && args.saved !== true && typeof args.saved !== "string") + throw new Error("saved: true (the open file as saved) or a .hip, .hipnc or .hiplc path.") + return withImage(await program(context, saved ? "verify_saved" : "verify", args, VERIFY_TIMEOUT_MS), "verify.png") + }, + + async houdini_render(args, context) { + const { task: id, wait_s, cancel, ...params } = args + if (id !== undefined) { + const task = getTask(String(id)) + if (!task) throw new Error(`No render or PDG task ${String(id)} in this OpenCode.`) + if (cancel === true) return json(cancelTask(task)) + // A whole render can be waited for in one call (the tool call itself has no time limit). + const until = Date.now() + Math.min(Math.max(Number(wait_s ?? 0), 0), RENDER_WAIT_MAX_S) * 1_000 + let status = taskStatus(task) + while ( + (status.state === "rendering" || status.state === "cooking") && + Date.now() < until && + !context.signal.aborted + ) { + await new Promise((resolve) => setTimeout(resolve, 500)) + status = taskStatus(task) + } + return json(status) + } + if (params.rop === undefined && params.top === undefined) + throw new Error( + "Give rop (a render node) or top (a PDG network or node) to start a task, or task to follow one.", + ) + const started = await program(context, "render", params) + if (!isRenderStart(started)) throw new Error(`houdini_render: the render did not start: ${json(started)}`) + const task = addTask(started) + const until = Date.now() + Math.min(Math.max(Number(wait_s ?? 0), 0), RENDER_WAIT_MAX_S) * 1_000 + let status = taskStatus(task) + while ( + (status.state === "rendering" || status.state === "cooking") && + Date.now() < until && + !context.signal.aborted + ) { + await new Promise((resolve) => setTimeout(resolve, 500)) + status = taskStatus(task) + } + return json(status) + }, + + async houdini_save(args, context) { + const result = (await program(context, "save", args)) as Record + const copy = typeof result?.copy === "string" ? result.copy : result?.copy === true ? String(result.path) : null + return json({ ...result, check: verifySavedHint(copy ?? true) }) + }, + } + + const allTools = buildPluginTools( + manifests.list().filter((manifest) => manifest.domain === "tools"), + APP, + async (manifest, args, context) => { + const executor = executors[manifest.name] + if (!executor) throw new Error(`No executor for ${manifest.name}`) + try { + return await executor(args, context) + } catch (error) { + throw hostError(error) + } }, ) - const registrations: Array<{ dispose(): Promise }> = [] + + const registrations: Array<{ dispose: () => Promise }> = [] let disconnect = () => {} + let unsubscribe = () => {} const cleanup = async () => { + unsubscribe() disconnect() - for (const r of registrations.splice(0).reverse()) await r.dispose() + for (const registration of registrations.splice(0).reverse()) await registration.dispose().catch(() => {}) } try { - const registration = await ctx.rpc.register(rpc.definition as never, rpc.handlers as never) + await attachments.load() + const registration = await ctx.rpc.register(rpc.definition as never, handlers as never) registrations.push(registration) disconnect = rpc.connect(registration as never) registrations.push( - await ctx.tool.transform((editor) => { - editor.namespace({ name: "houdini", description: "Inspect and edit the explicitly attached Houdini scene." }) - for (const tool of tools) editor.add(tool) - }), - ) - const file = join(packageRoot, "skills", "houdini-pipeline", "SKILL.md") - const source = readFileSync(file, "utf8").replaceAll("\r\n", "\n") - registrations.push( - await ctx.skill.transform((editor) => { - if (!editor.get("houdini-pipeline")) - editor.add({ - id: "houdini-pipeline", - name: "houdini-pipeline", - description: "Revision-checked Houdini graph work.", - autoinvoke: false, - path: file, - content: source.replace(/^---\n[\s\S]*?\n---\n/, ""), - } as never) - }), + await ctx.tool.transform((editor) => registerPluginTools(editor, APP, NAMESPACE_DESCRIPTION, allTools)), ) + unsubscribe = registry.onChange(() => void maintain()) + void maintain() } catch (error) { await cleanup() throw error } - return Object.assign(cleanup, { internals: { registry, rpc, tools } }) + return Object.assign(cleanup, { + internals: { registry, rpc, handlers, tools: allTools, executors, options, attachments, maintain }, + }) } diff --git a/integrations/houdini/plugin/src/kit.ts b/integrations/houdini/plugin/src/kit.ts new file mode 100644 index 0000000..0562792 --- /dev/null +++ b/integrations/houdini/plugin/src/kit.ts @@ -0,0 +1,2 @@ +// The integration kit, bundled into the build. +export * from "../../../../packages/integration-kit/src/index.ts" diff --git a/integrations/houdini/plugin/src/legacy.ts b/integrations/houdini/plugin/src/legacy.ts new file mode 100644 index 0000000..902694e --- /dev/null +++ b/integrations/houdini/plugin/src/legacy.ts @@ -0,0 +1,37 @@ +// Read-only detection of an earlier OpenCode installation in Houdini (notice `legacy_install`): its package +// packages/opencode_houdini.json in a Houdini preferences folder, which loads another OpenCode panel and host next +// to OpenCode Link. Nothing is changed: the user removes it (or sets "enable": false in it). + +import { existsSync, readFileSync, statSync } from "node:fs" +import { join } from "node:path" + +import { type PathEnv, prefsDirs } from "./paths.ts" + +export const LEGACY_PACKAGE = "opencode_houdini.json" + +export type LegacyInstall = { package: string; enabled: boolean; points_to?: string } + +// The earlier packages found in every Houdini preferences folder (and the explicit one), with whether each is on. +export function legacyInstalls(paths: PathEnv, explicit?: string): LegacyInstall[] { + const dirs = new Set(prefsDirs(paths).map((entry) => entry.dir)) + if (explicit) dirs.add(explicit) + const found: LegacyInstall[] = [] + for (const dir of dirs) { + const file = join(dir, "packages", LEGACY_PACKAGE) + if (!existsSync(file)) continue + let enabled = true + let pointsTo: string | undefined + try { + if (statSync(file).size <= 1024 * 1024) { + const pkg = JSON.parse(readFileSync(file, "utf8")) as { enable?: unknown; path?: unknown; hpath?: unknown } + enabled = pkg.enable !== false && pkg.enable !== "false" + const target = pkg.path ?? pkg.hpath + if (typeof target === "string") pointsTo = target + } + } catch { + // unreadable: counted as on + } + found.push({ package: file, enabled, ...(pointsTo ? { points_to: pointsTo } : {}) }) + } + return found +} diff --git a/integrations/houdini/plugin/src/method.ts b/integrations/houdini/plugin/src/method.ts new file mode 100644 index 0000000..ee0813f --- /dev/null +++ b/integrations/houdini/plugin/src/method.ts @@ -0,0 +1,25 @@ +// The working method, attached once to a conversation when it attaches a Houdini (a native instructions entry, +// not a skill): what to check before writing code, and how to verify a step. + +import { type EntryPut, methodAttacher as kitAttacher, resetMethodForTests as resetKit } from "./kit.ts" + +export const METHOD_KEY = "houdini.method" +export const METHOD_TEXT = [ + "Working on the open Houdini:", + "- Your knowledge of hou may be older than it: read its version and license (houdini_hosts, houdini_inspect).", + "- Inspect the exact element before coding it (houdini_inspect search, type with its parms, node, hou symbol).", + "- After an error, read its hint and inspect again; never guess twice. A parm that does not exist returns None.", + "- Cook what you build and read its errors and warnings; a node in error is not done.", + "- Turn each requirement of your task into a measured check (houdini_verify criteria).", + "- The saved file is what you deliver: save, then run every check on it with houdini_verify saved: true.", + "- Report each requirement with its measured value from the file. A failed or unchecked one is not done: say so.", + "- Still unclear: the SideFX documentation of THIS version (links in houdini_inspect).", +].join("\n") + +export type { EntryPut } + +export const methodAttacher = (put: EntryPut | undefined) => kitAttacher(put, { key: METHOD_KEY, text: METHOD_TEXT }) + +export function resetMethodForTests() { + resetKit(METHOD_KEY) +} diff --git a/integrations/houdini/plugin/src/options.ts b/integrations/houdini/plugin/src/options.ts new file mode 100644 index 0000000..8359f85 --- /dev/null +++ b/integrations/houdini/plugin/src/options.ts @@ -0,0 +1,25 @@ +// Plugin settings: the `options` of the plugin entry first, `NEURALNOMADS_HOUDINI_*` variables as a fallback. + +export type HoudiniOptions = { + // The Houdini user preferences folder to inspect and install into, instead of the newest one found. + hostInspectionDir?: string + // Longest wait for one host request (a tool may give a longer one, such as a verification). + requestTimeoutMs: number +} + +// The installed part waits up to an hour for Houdini's main thread to finish a request; a call waits this long by +// default, then says whether Houdini still runs it (status). +const DEFAULT_TIMEOUT_MS = 120_000 + +function text(value: unknown): string | undefined { + return typeof value === "string" && value.trim() ? value.trim() : undefined +} + +export function readOptions(options: Record | undefined, env: NodeJS.ProcessEnv): HoudiniOptions { + const source = options ?? {} + const timeout = Number(source.requestTimeoutMs ?? env.NEURALNOMADS_HOUDINI_REQUEST_TIMEOUT_MS) + return { + hostInspectionDir: text(source.hostInspectionDir) ?? text(env.NEURALNOMADS_HOUDINI_HOST_INSPECTION_DIR), + requestTimeoutMs: Number.isFinite(timeout) && timeout >= 1_000 ? Math.min(timeout, 600_000) : DEFAULT_TIMEOUT_MS, + } +} diff --git a/integrations/houdini/plugin/src/paths.ts b/integrations/houdini/plugin/src/paths.ts index 3f90816..3d4dbe0 100644 --- a/integrations/houdini/plugin/src/paths.ts +++ b/integrations/houdini/plugin/src/paths.ts @@ -1,11 +1,63 @@ +// Folders the plugin reads: its own package (methods, programs, host payload), its state folder (the installed +// payload) and Houdini's user preferences folders. + +import { existsSync, readdirSync } from "node:fs" import { homedir } from "node:os" import { join } from "node:path" import { fileURLToPath } from "node:url" +// dist/index.js and src/*.ts both sit one level below the package root. export const packageRoot = fileURLToPath(new URL("..", import.meta.url)) +export const methodsDir = join(packageRoot, "methods") +export const programsDir = join(packageRoot, "programs") + +export const MIN_HOUDINI = [21, 0] as const + +export type PathEnv = { env: NodeJS.ProcessEnv; platform: NodeJS.Platform; home: string } + +export const defaultPathEnv = (): PathEnv => ({ env: process.env, platform: process.platform, home: homedir() }) export function stateDir(env = process.env, home = homedir(), platform = process.platform) { return platform === "win32" ? join(env.LOCALAPPDATA ?? join(home, "AppData", "Local"), "NeuralNomads", "opencode-houdini") : join(env.XDG_STATE_HOME ?? join(home, ".local", "state"), "neuralnomads", "opencode-houdini") } + +// Where Houdini keeps its per-version user preferences folders: houdini in the Documents folder +// (or in HOME when it is set) on Windows and in the home folder on Linux; under +// ~/Library/Preferences/houdini on macOS. +function prefsRoot({ env, platform, home }: PathEnv): { root: string; pattern: RegExp } { + if (platform === "darwin") return { root: join(home, "Library", "Preferences", "houdini"), pattern: /^(\d+)\.(\d+)$/ } + if (platform === "win32") return { root: env.HOME || join(home, "Documents"), pattern: /^houdini(\d+)\.(\d+)$/ } + return { root: home, pattern: /^houdini(\d+)\.(\d+)$/ } +} + +// Every existing Houdini preferences folder (any version), newest first. +export function prefsDirs(paths: PathEnv): Array<{ dir: string; version: [number, number] }> { + const { root, pattern } = prefsRoot(paths) + if (!existsSync(root)) return [] + let entries: string[] + try { + entries = readdirSync(root, { withFileTypes: true }) + .filter((entry) => entry.isDirectory()) + .map((entry) => entry.name) + } catch { + return [] + } + return entries + .map((name) => ({ name, match: pattern.exec(name) })) + .filter((entry): entry is { name: string; match: RegExpExecArray } => entry.match !== null) + .map(({ name, match }) => ({ + dir: join(root, name), + version: [Number(match[1]), Number(match[2])] as [number, number], + })) + .sort((a, b) => b.version[0] - a.version[0] || b.version[1] - a.version[1]) +} + +// The newest existing preferences folder of Houdini 21 or later (Houdini creates it on its first start), or null. +export function defaultPrefsDir(paths: PathEnv): string | null { + const found = prefsDirs(paths).find( + ({ version }) => version[0] > MIN_HOUDINI[0] || (version[0] === MIN_HOUDINI[0] && version[1] >= MIN_HOUDINI[1]), + ) + return found?.dir ?? null +} diff --git a/integrations/houdini/plugin/src/programs.ts b/integrations/houdini/plugin/src/programs.ts new file mode 100644 index 0000000..bff01b0 --- /dev/null +++ b/integrations/houdini/plugin/src/programs.ts @@ -0,0 +1,29 @@ +// The fixed programs of houdini_inspect, houdini_verify, houdini_render and houdini_save (programs/*.py in the +// package). They run through the host's code execution, read only, with their parameters as JSON data: the +// plugin's own code, never code from the model, so they ask no permission. + +import { readFileSync } from "node:fs" +import { join } from "node:path" + +import { programsDir } from "./paths.ts" + +const read = (file: string) => readFileSync(join(programsDir, file), "utf8") +const PROGRAMS = { + inspect: read("inspect.py"), + verify: read("verify.py"), + verify_saved: read("verify_saved.py"), + save: read("save.py"), + render: read("render.py"), +} +// The background hython's script: render.py's job, and for verify_saved the verify program itself. +const JOBS: Partial> = { + render: read("render_job.py"), + verify_saved: PROGRAMS.verify, +} + +export type ProgramName = keyof typeof PROGRAMS + +export function programCode(name: ProgramName, params: unknown) { + const job = JOBS[name] === undefined ? "" : `JOB = ${JSON.stringify(JOBS[name])}\n` + return `import json\nPARAMS = json.loads(${JSON.stringify(JSON.stringify(params))})\n${job}${PROGRAMS[name]}` +} diff --git a/integrations/houdini/plugin/src/sidecar.ts b/integrations/houdini/plugin/src/sidecar.ts index 484a57b..d7f561e 100644 --- a/integrations/houdini/plugin/src/sidecar.ts +++ b/integrations/houdini/plugin/src/sidecar.ts @@ -1,5 +1,6 @@ // Private JSONL panel client. Built into the installed host; credentials never leave this process. -import { randomUUID } from "node:crypto" +import { mkdirSync, readFileSync, writeFileSync } from "node:fs" +import { dirname } from "node:path" import readline from "node:readline" import { OpenCode } from "@opencode/client" import { Service } from "@opencode/client/service" @@ -11,7 +12,6 @@ type Channel = { directory: string sessionID: string label: string - key: string binding: { host: string; port: number; token: string } handle: string abort: AbortController @@ -26,7 +26,15 @@ const channels = new Map() const queues = new Map>() const send = (value: unknown) => process.stdout.write(`${JSON.stringify(value)}\n`) const event = (c: Channel, name: string, data: object) => send({ channel: c.id, event: name, data }) -const options = (c: Channel) => ({ location: { directory: c.directory }, signal: AbortSignal.timeout(3000) }) +const options = (c: { directory: string }) => ({ + location: { directory: c.directory }, + signal: AbortSignal.timeout(3000), +}) +// A message of this sidecar's own, safe to show in the panel (SDK and host errors never are). +class PanelError extends Error {} + +// How long Connect waits for this Houdini's own offer (it offers itself every 5 s from its start). +const OFFER_WAIT_MS = 8_000 async function api() { if (client) return client @@ -37,29 +45,56 @@ async function api() { return client } -async function advertise(c: Channel) { - const result = await rpc.advertise?.( - { - host: c.binding.host, - port: c.binding.port, - token: c.binding.token, - script_handle: c.handle, - label: c.label, - protocol: 1, - offer_key: c.key, - }, - options(c), - ) - if (result?.outcome !== "accepted" && result?.outcome !== "renewed") throw new Error("Houdini offer was refused.") +// The panel's conversation per Houdini file, kept next to the installed payloads: Connect takes it back. +type Sessions = Record +function readSessions(file: string): Sessions { + try { + const value = JSON.parse(readFileSync(file, "utf8")) as unknown + return typeof value === "object" && value !== null && !Array.isArray(value) ? (value as Sessions) : {} + } catch { + return {} + } +} +function rememberSession(file: string, key: string, session: string, directory: string) { + if (!file || !key || key.startsWith("untitled:")) return + try { + const sessions = readSessions(file) + sessions[key] = { session, directory } + const keys = Object.keys(sessions) + for (const old of keys.slice(0, Math.max(0, keys.length - 200))) delete sessions[old] + mkdirSync(dirname(file), { recursive: true }) + writeFileSync(file, `${JSON.stringify(sessions)}\n`) + } catch { + // a conversation that is not taken back next time + } } -async function verify(c: Channel) { +// This Houdini's offer: the one with its label among the offered hosts (Houdini offers itself; the panel never). +async function findOffer(c: { directory: string; label: string }) { + const until = Date.now() + OFFER_WAIT_MS + for (;;) { + const result = await rpc.hosts?.({}, options(c)) + const offers = ((result?.offers ?? []) as Array<{ label: string; offerID: string }>).filter( + (offer) => offer.label === c.label, + ) + if (offers.length > 1) + throw new PanelError(`Several Houdinis offer ${c.label}: attach this conversation from OpenCode (houdini_hosts).`) + if (offers[0]) return offers[0] + if (Date.now() > until) + throw new PanelError( + "This Houdini does not offer itself to OpenCode (OPENCODE_HOUDINI_OFFER=0, or OpenCode Link not started).", + ) + await new Promise((resolve) => setTimeout(resolve, 1_000)) + } +} + +async function attached(c: Channel) { const read = await rpc.bindingRead?.({ sessionID: c.sessionID }, options(c)) - if (read?.state !== "attached") throw new Error("Houdini was detached; choose Connect to attach again.") - const binding = read.binding as { label?: string } | undefined - if (binding?.label !== c.label) throw new Error("This conversation is attached to another host.") + return read?.state === "attached" && (read.binding as { label?: string } | undefined)?.label === c.label } +// Closing the panel releases its own conversation only: the offer stays, and other conversations attached to +// this Houdini (CodeNomad, a terminal) keep it. async function close(c: Channel) { channels.delete(c.id) if (c.timer) clearInterval(c.timer) @@ -71,10 +106,9 @@ async function close(c: Channel) { { timeoutMs: 2000 }, ).catch(() => {}) // A different client may have moved the session to another host. Never detach that binding. - await verify(c) - .then(() => rpc.detachHost?.({ sessionID: c.sessionID }, options(c))) + await attached(c) + .then((still) => (still ? rpc.detachHost?.({ sessionID: c.sessionID }, options(c)) : undefined)) .catch(() => {}) - await rpc.withdraw?.({ offer_key: c.key, token: c.binding.token }, options(c)).catch(() => {}) } async function stream(c: Channel) { @@ -97,7 +131,7 @@ async function stream(c: Channel) { } } -async function connect(id: string, params: Record) { +async function connect(id: string, params: Record, fresh: boolean) { const service = await api() const previous = channels.get(id) if (previous) await close(previous) @@ -105,38 +139,55 @@ async function connect(id: string, params: Record) { throw new Error("Invalid host endpoint.") const directory = String(params.directory ?? "") if (!directory) throw new Error("Select a project directory.") - const session = await service.session.create({ title: "Houdini", location: { directory } }) + const label = String(params.offer_label ?? "") + if (!label) throw new PanelError("This Houdini's offer is unknown.") + const key = String(params.script_key ?? "") + const store = String(params.sessions_file ?? "") + // The file's conversation, when it still exists; otherwise (or with New) a new one. + let sessionID: string | undefined + let resumed = false + const known = !fresh && store ? readSessions(store)[key] : undefined + if (known?.session) { + try { + const found = (await service.session.get({ sessionID: known.session } as never)) as { id?: string } | undefined + if (found?.id === known.session) { + sessionID = known.session + resumed = true + } + } catch { + // gone: a new one + } + } + if (!sessionID) { + const label = String(params.script_name || "Houdini") + sessionID = (await service.session.create({ title: `Houdini: ${label}`, location: { directory } })).id + } const c: Channel = { id, - directory, - sessionID: session.id, - label: `Houdini ${randomUUID().slice(0, 8)}`, - key: randomUUID(), + directory: resumed && known ? known.directory : directory, + sessionID, + label, binding: { host: "127.0.0.1", port: Number(params.port), token: String(params.token) }, handle: String(params.script_handle), abort: new AbortController(), pending: false, } try { - await advertise(c) - const result = await rpc.hosts?.({}, options(c)) - const offer = ((result?.offers ?? []) as Array<{ label: string; offerID: string }>).find( - (offer) => offer.label === c.label, - ) - if (!offer) throw new Error("No validated Houdini offer.") - const attached = await rpc.attachHost?.({ offerID: offer.offerID, sessionID: c.sessionID }, options(c)) - if (attached?.outcome !== "attached") throw new Error("Houdini attachment was refused.") + const offer = await findOffer(c) + const result = await rpc.attachHost?.({ offerID: offer.offerID, sessionID: c.sessionID }, options(c)) + if (result?.outcome !== "attached") throw new PanelError("Houdini attachment was refused.") + rememberSession(store, key, c.sessionID, c.directory) channels.set(id, c) let warned = false c.timer = setInterval(() => { if (c.pending) return c.pending = true void api() - .then(() => advertise(c)) - .then(() => verify(c)) + .then(() => attached(c)) + .then((still) => { + if (!still) throw new Error("detached") + }) .catch(() => { - // Offering the application is independent from its conversation binding. Keep renewing - // for other clients after a detach; discovery may recover, attachment is always explicit. client = undefined if (!warned) event(c, "error", { message: "Conversation detached or service unavailable; choose Connect to reconnect." }) @@ -147,7 +198,7 @@ async function connect(id: string, params: Record) { }) }, 5000) void stream(c) - event(c, "session", { id: session.id, resumed: false }) + event(c, "session", { id: c.sessionID, resumed }) event(c, "history", { messages: [] }) event(c, "status", { state: "idle" }) } catch (error) { @@ -155,11 +206,11 @@ async function connect(id: string, params: Record) { throw error } } - async function dispatch(request: Request) { try { const params = request.params ?? {} - if (request.method === "connect" || request.method === "new_session") await connect(request.channel, params) + if (request.method === "connect" || request.method === "new_session") + await connect(request.channel, params, request.method === "new_session") else { const c = channels.get(request.channel) if (!c) throw new Error("Choose Connect first.") @@ -174,7 +225,7 @@ async function dispatch(request: Request) { ) await service.session.interrupt({ sessionID: c.sessionID, resume: false }) } else if (request.method === "prompt") { - await verify(c) + if (!(await attached(c))) throw new PanelError("Houdini was detached; choose Connect to attach again.") await requestHost( c.binding, "resume", @@ -189,12 +240,17 @@ async function dispatch(request: Request) { } else throw new Error("Unknown panel action.") } send({ id: request.id, result: {} }) - } catch { + } catch (error) { // Never forward SDK/host errors: their details can include credentials or endpoints. send({ channel: request.channel, event: "error", - data: { message: "Connection or action failed. Check OpenCode and Houdini, then choose Connect." }, + data: { + message: + error instanceof PanelError + ? error.message + : "Connection or action failed. Check OpenCode and Houdini, then choose Connect.", + }, }) send({ id: request.id, error: { message: "Panel action failed." } }) } diff --git a/integrations/houdini/plugin/src/tasks.ts b/integrations/houdini/plugin/src/tasks.ts new file mode 100644 index 0000000..af9182a --- /dev/null +++ b/integrations/houdini/plugin/src/tasks.ts @@ -0,0 +1,141 @@ +// Background render and PDG tasks started by houdini_render: a hython on this computer, followed from the plugin +// through its log (OPENCODE_PROGRESS, OPENCODE_DONE and OPENCODE_FAILED lines) and the files it writes. Per daemon +// process, as the offers. + +import { closeSync, existsSync, openSync, readSync, statSync } from "node:fs" +import { basename } from "node:path" + +import { isAlive } from "./kit.ts" + +export type RenderStart = { + pid: number + log: string + work?: string + kind: "render" | "pdg" + output?: string + targets: string[] + start?: number + end?: number + node: string + source?: string + working_copy?: string + facts?: unknown +} +export type RenderTask = RenderStart & { id: string; started: number; cancelled?: boolean; finished?: number } + +const shared = globalThis as { __neuralnomadsHoudiniTasks?: Map } +shared.__neuralnomadsHoudiniTasks ??= new Map() +const tasks = shared.__neuralnomadsHoudiniTasks +const LOG_TAIL = 256 * 1024 + +export function isRenderStart(value: unknown): value is RenderStart { + const start = value as RenderStart + return ( + typeof start === "object" && + start !== null && + Number.isInteger(start.pid) && + start.pid > 0 && + typeof start.log === "string" && + (start.kind === "render" || start.kind === "pdg") && + Array.isArray(start.targets) + ) +} + +export function addTask(start: RenderStart): RenderTask { + const id = `${start.kind}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}` + const task = { ...start, id, started: Date.now() } + tasks.set(id, task) + return task +} + +export const getTask = (id: string) => tasks.get(id) + +function readTail(path: string) { + if (!existsSync(path)) return "" + const size = statSync(path).size + const length = Math.min(size, LOG_TAIL) + const buffer = Buffer.alloc(length) + const fd = openSync(path, "r") + try { + readSync(fd, buffer, 0, length, size - length) + } finally { + closeSync(fd) + } + return buffer.toString("utf8") +} + +function marker(text: string, key: string) { + const prefix = `OPENCODE_${key} ` + const line = text + .split(/\r?\n/) + .filter((entry) => entry.startsWith(prefix)) + .at(-1) + if (!line) return undefined + try { + return JSON.parse(line.slice(prefix.length)) as unknown + } catch { + return line.slice(prefix.length) + } +} + +export function taskStatus(task: RenderTask) { + const text = readTail(task.log) + const done = marker(text, "DONE") as { output?: unknown; files?: unknown } | string | undefined + const failed = marker(text, "FAILED") + const progress = marker(text, "PROGRESS") as { done?: number; total?: number } | undefined + const alive = isAlive(task.pid) + // "done" only when the job said so and every expected file is there, not empty. + const missing = done ? task.targets.filter((file) => !existsSync(file) || statSync(file).size === 0) : [] + const verified = done !== undefined && !missing.length + const state = task.cancelled + ? "cancelled" + : verified + ? "done" + : done !== undefined || failed !== undefined || !alive + ? "failed" + : task.kind === "pdg" + ? "cooking" + : "rendering" + if (state !== "rendering" && state !== "cooking") task.finished ??= Date.now() + const errors = text + .split(/\r?\n/) + .filter((line) => /\b(Error|ERROR)\b/.test(line) && !line.startsWith("OPENCODE_")) + .slice(-5) + return { + task: task.id, + kind: task.kind, + state, + node: task.node, + ...(task.output ? { output: task.output } : {}), + ...(progress && typeof progress === "object" ? { progress } : {}), + ...(task.start !== undefined ? { frames: [task.start, task.end ?? task.start] } : {}), + elapsed_s: Math.round(((task.finished ?? Date.now()) - task.started) / 100) / 10, + ...(verified && typeof done === "object" ? { result: done } : {}), + ...(state === "failed" + ? { + error: missing.length + ? `the job finished but ${missing.length} of ${task.targets.length} expected file(s) are missing or empty: ${missing + .slice(0, 5) + .map((file) => basename(file)) + .join(", ")}${missing.length > 5 ? ", ..." : ""}` + : typeof failed === "string" + ? failed.slice(-1500) + : failed !== undefined + ? JSON.stringify(failed).slice(-1500) + : errors.join(" | ") || `the background hython stopped without finishing; see ${task.log}`, + } + : {}), + ...(errors.length && state !== "failed" ? { log_errors: errors } : {}), + ...(task.facts ? { facts: task.facts } : {}), + log: task.log, + ...(task.working_copy ? { working_copy: task.working_copy } : {}), + } +} + +export function cancelTask(task: RenderTask) { + task.cancelled = true + try { + process.kill(task.pid) + } catch {} + return taskStatus(task) +} diff --git a/integrations/houdini/test/conformance.test.ts b/integrations/houdini/test/conformance.test.ts index 1787891..14d98c7 100644 --- a/integrations/houdini/test/conformance.test.ts +++ b/integrations/houdini/test/conformance.test.ts @@ -1,17 +1,38 @@ import assert from "node:assert/strict" import { spawn, spawnSync } from "node:child_process" +import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs" +import { tmpdir } from "node:os" import { join } from "node:path" import { createInterface } from "node:readline" -import { it } from "node:test" +import { after, it } from "node:test" import { type HostError, requestHost } from "../../../packages/integration-kit/src/index.ts" -import { checkCodeExecutionGate, checkHostListener, runSentinelScenario } from "../../../packages/testing/src/index.ts" -import { loadPlugin } from "./helpers.ts" +import { + checkCodeExecutionGate, + checkHostListener, + type ManifestLike, + runSentinelScenario, +} from "../../../packages/testing/src/index.ts" +import { readPackage } from "../plugin/src/integration.ts" +import { methodsDir } from "../plugin/src/paths.ts" +import { announcement, loadPlugin } from "./helpers.ts" const python = process.env.PYTHON ?? (process.platform === "win32" ? "python" : "python3") const probe = join(import.meta.dirname, "host_probe.py") +const scratch = mkdtempSync(join(tmpdir(), "houdini-conformance-")) +after(() => rmSync(scratch, { recursive: true, force: true })) -it("the actual Houdini host transport is authenticated, loopback-only and has no code execution", async () => { - const child = spawn(python, ["-I", probe, "--serve"], { stdio: ["pipe", "pipe", "inherit"], windowsHide: true }) +function manifests(): ManifestLike[] { + return readdirSync(methodsDir, { recursive: true, withFileTypes: true }) + .filter((entry) => entry.isFile() && entry.name.endsWith(".json")) + .map((entry) => JSON.parse(readFileSync(join(entry.parentPath, entry.name), "utf8")) as ManifestLike) +} + +// The installed part's real listener and dispatcher under system Python, with only the HOM boundary stubbed. +async function servedHost() { + const child = spawn(python, ["-I", probe, "--serve"], { + stdio: ["pipe", "pipe", "inherit"], + windowsHide: true, + }) const line = await new Promise((resolve, reject) => { const timer = setTimeout(() => { child.kill() @@ -27,29 +48,23 @@ it("the actual Houdini host transport is authenticated, loopback-only and has no }) }) const endpoint = JSON.parse(line) as { host: string; port: number; token: string } - const plugin = await loadPlugin() + return { + endpoint, + close: async () => { + const stopped = new Promise((resolve) => child.once("exit", resolve)) + child.stdin.end("stop\n") + await stopped + }, + } +} + +it("the actual Houdini host transport: authenticated, loopback-only, refusals with their text, data and code", async () => { + const host = await servedHost() try { - assert.deepEqual((await checkHostListener(endpoint)).errors, []) - const hello = (await requestHost(endpoint, "hello")) as { capabilities: { methods: string[] } } - assert.deepEqual( - await checkCodeExecutionGate({ - app: "houdini", - manifests: [], - defaults: { appPreference: false, pluginOption: false }, - probe: async () => ({ - helloMethods: hello.capabilities.methods, - toolNames: [...plugin.tools.keys()].map((name) => `houdini_${name}`), - call: async () => ({ executed: false, permissionsAsked: [], error: "method_disabled" }), - }), - }), - [], - ) - await assert.rejects( - requestHost(endpoint, "execute_code", { script_handle: "hip:test-document", code: "raise Exception()" }), - ) + assert.deepEqual((await checkHostListener(host.endpoint)).errors, []) // A refusal keeps the host's message, its data and its own code (data.reason). await assert.rejects( - requestHost(endpoint, "apply", { + requestHost(host.endpoint, "apply", { script_handle: "hip:test-document", session_id: "ses_test", if_revision: 0, @@ -62,20 +77,72 @@ it("the actual Houdini host transport is authenticated, loopback-only and has no return true }, ) + // status answers without the main thread. + assert.deepEqual(await requestHost(host.endpoint, "status", {}), { busy: false, queued: 0 }) } finally { - await plugin.cleanup() - const stopped = new Promise((resolve) => child.once("exit", resolve)) - child.stdin.end("stop\n") - await stopped + await host.close() } }) +it("checkCodeExecutionGate: declared dangerous, listed by the host, asked on every call", async () => { + let marker = 0 + const cleanups: Array<() => Promise> = [] + const errors = await checkCodeExecutionGate({ + app: "houdini", + manifests: manifests(), + async probe() { + const host = await servedHost() + let answer: "allow" | "deny" = "allow" + const plugin = await loadPlugin({ + answer: (action) => { + if (action === "houdini_execute_code" && answer === "deny") throw new Error("Permission denied") + }, + }) + cleanups.push(async () => { + await plugin.cleanup() + await host.close() + }) + const hello = (await requestHost(host.endpoint, "hello", {})) as { + script_handle: string + capabilities: { methods: string[] } + } + assert.deepEqual(await plugin.rpc("advertise", announcement(host.endpoint, hello.script_handle)), { + outcome: "accepted", + }) + return { + helloMethods: hello.capabilities.methods, + toolNames: [...plugin.tools.keys()], + async call(permission) { + answer = permission + marker += 1 + const file = join(scratch, `executed-${marker}.txt`) + const code = `open(${JSON.stringify(file)}, "w").write("ran")` + const asked = plugin.permissions.length + let error: string | undefined + try { + await plugin.tool("houdini_execute_code", { code }) + } catch (thrown) { + error = String(thrown) + } + return { + executed: existsSync(file), + permissionsAsked: plugin.permissions.slice(asked), + ...(error ? { error } : {}), + } + }, + } + }, + }) + for (const cleanup of cleanups.splice(0)) await cleanup() + assert.deepEqual(errors, []) +}) + it("RPC sentinels stay out of every output and event, including bindingRead", async () => { const plugin = await loadPlugin() try { const result = await runSentinelScenario( { call: (method, input) => plugin.rpc(method, input as object), events: plugin.events }, - { sessionID: "ses_test", hostVersion: "0.2.0", appVersion: "22.0.429" }, + { sessionID: "ses_test", hostVersion: readPackage().version, appVersion: "22.0.429" }, ) assert.deepEqual(result.errors, []) } finally { @@ -83,7 +150,15 @@ it("RPC sentinels stay out of every output and event, including bindingRead", as } }) -it("host preconditions, revision, headless and string-code boundaries", () => { +it("host code runs, undo, quarantine, status and string-code boundaries under system Python", () => { const result = spawnSync(python, ["-I", probe], { windowsHide: true, encoding: "utf8" }) assert.equal(result.status, 0, result.stderr) }) + +it("hints read Houdini's own knowledge (stub hou)", () => { + const result = spawnSync(python, ["-I", join(import.meta.dirname, "hints_probe.py")], { + windowsHide: true, + encoding: "utf8", + }) + assert.equal(result.status, 0, result.stderr) +}) diff --git a/integrations/houdini/test/contract.test.ts b/integrations/houdini/test/contract.test.ts index d48565d..1c09583 100644 --- a/integrations/houdini/test/contract.test.ts +++ b/integrations/houdini/test/contract.test.ts @@ -1,32 +1,25 @@ import assert from "node:assert/strict" import { spawnSync } from "node:child_process" -import { mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs" +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" import { it } from "node:test" import { startFakeHost } from "../../../packages/testing/src/index.ts" import { inspectHost } from "../plugin/src/host-install.ts" import { readPackage } from "../plugin/src/integration.ts" -import { loadPlugin } from "./helpers.ts" +import { announcement, content, isolatedPaths, loadPlugin, prefsFolder } from "./helpers.ts" + +const python = process.env.PYTHON ?? (process.platform === "win32" ? "python" : "python3") +const version = readPackage().version it("exact-session ownership, detach, expiry and no attachment replay", async () => { let now = 0 const plugin = await loadPlugin({ now: () => now }) - const host = await startFakeHost({ - hostVersion: "0.2.0", - helloOverride: (hello) => ({ ...hello, app_version: "22.0.429" }), - }) + const host = await startFakeHost({ hostVersion: version, appVersion: "22.0.429" }) try { - assert.deepEqual( - await plugin.rpc("advertise", { - ...host.endpoint, - script_handle: host.hello.script_handle, - label: "Houdini", - protocol: 1, - offer_key: "host-test", - }), - { outcome: "accepted" }, - ) + assert.deepEqual(await plugin.rpc("advertise", announcement(host.endpoint, host.scriptHandle, "host-test")), { + outcome: "accepted", + }) const offerID = plugin.registry.hosts().offers[0]?.offerID as string assert.deepEqual(await plugin.rpc("attachHost", { offerID, sessionID: "ses_foreign" }), { outcome: "refused" }) assert.deepEqual(await plugin.rpc("bindingRead", { sessionID: "ses_foreign" }), { state: "unknown" }) @@ -43,38 +36,41 @@ it("exact-session ownership, detach, expiry and no attachment replay", async () } }) -it("installer needs explicit preferences, installs stable payload, is idempotent and refuses WSL", async () => { +it("installs in the newest Houdini 21+ preferences folder, idempotently, says to restart Houdini, refuses WSL", async () => { const root = mkdtempSync(join(tmpdir(), "houdini-install-")) - const preferences = join(root, "prefs") - mkdirSync(preferences) - const plugin = await loadPlugin({ options: { hostInspectionDir: preferences }, state: join(root, "state") }) + const paths = isolatedPaths(join(root, "home")) + for (const version of ["20.5", "22.0", "21.0"]) mkdirSync(prefsFolder(paths, version), { recursive: true }) + const preferences = prefsFolder(paths, "22.0") + const plugin = await loadPlugin({ paths, state: join(root, "state") }) try { assert.deepEqual(inspectHost(preferences), { kind: "not-found" }) assert.deepEqual(await plugin.rpc("installHost"), { outcome: "installed", - detail: "Open Houdini, then open the OpenCode Link panel and choose Connect.", + detail: "Restart Houdini: OpenCode Link offers it to OpenCode when it starts.", }) - const native = JSON.parse(readFileSync(join(preferences, "packages", "neuralnomads_houdini.json"), "utf8")) + const file = join(preferences, "packages", "neuralnomads_houdini.json") + const native = JSON.parse(readFileSync(file, "utf8")) assert.ok(native.env[0].NEURALNOMADS_HOUDINI_ROOT.startsWith(join(root, "state").replaceAll("\\", "/"))) - assert.equal(native.neuralnomadsHostVersion, "0.2.1") + assert.equal(native.neuralnomadsHostVersion, version) const imported = spawnSync( - process.env.PYTHON ?? (process.platform === "win32" ? "python" : "python3"), - [ - "-I", - join(import.meta.dirname, "panel_probe.py"), - "test_installed_import", - join(preferences, "packages", "neuralnomads_houdini.json"), - ], - { windowsHide: true, encoding: "utf8" }, + python, + ["-I", join(import.meta.dirname, "panel_probe.py"), "test_installed_import", file], + { + windowsHide: true, + encoding: "utf8", + }, ) assert.equal(imported.status, 0, imported.stderr) - assert.deepEqual(inspectHost(preferences), { kind: "found", version: "0.2.1" }) + // The startup script that offers Houdini to OpenCode is installed with it. + const payload = native.env[0].NEURALNOMADS_HOUDINI_ROOT as string + assert.match(readFileSync(join(payload, "scripts", "python", "pythonrc.py"), "utf8"), /start_with_houdini/) + assert.match( + readFileSync(join(payload, "scripts", "opencode_houdini", "opencode_service.py"), "utf8"), + /def advertise_all/, + ) + assert.deepEqual(inspectHost(preferences), { kind: "found", version }) assert.deepEqual(await plugin.rpc("installHost"), { outcome: "unchanged" }) - const wsl = await loadPlugin({ - options: { hostInspectionDir: preferences }, - state: join(root, "never"), - wsl: () => true, - }) + const wsl = await loadPlugin({ paths, state: join(root, "never"), wsl: () => true }) try { assert.equal(((await wsl.rpc("installHost")) as { outcome: string }).outcome, "refused") } finally { @@ -86,12 +82,37 @@ it("installer needs explicit preferences, installs stable payload, is idempotent } }) -it("real onDestroyInterface callbacks revoke panel ownership idempotently through 2 → 1 → 0", () => { - const result = spawnSync( - process.env.PYTHON ?? (process.platform === "win32" ? "python" : "python3"), - ["-I", join(import.meta.dirname, "panel_probe.py"), "test_shared_host_lifetime"], - { windowsHide: true, encoding: "utf8" }, - ) +it("an earlier OpenCode package in Houdini's preferences is a notice and said by houdini_hosts", async () => { + const root = mkdtempSync(join(tmpdir(), "houdini-legacy-")) + const paths = isolatedPaths(join(root, "home")) + const packages = join(prefsFolder(paths, "22.0"), "packages") + mkdirSync(packages, { recursive: true }) + const plugin = await loadPlugin({ paths }) + try { + assert.equal(content(await plugin.tool("houdini_hosts")).legacy_install, undefined) + writeFileSync( + join(packages, "opencode_houdini.json"), + JSON.stringify({ path: "/opt/earlier/opencode/bundles/houdini" }), + ) + const status = (await plugin.rpc("status")) as { notices?: string[] } + assert.deepEqual(status.notices, ["legacy_install"]) + const listed = content(await plugin.tool("houdini_hosts")).legacy_install + assert.equal(listed.found[0].points_to, "/opt/earlier/opencode/bundles/houdini") + assert.match(listed.fix, /remove it, or set "enable": false/) + // Disabled by the user: no notice. + writeFileSync(join(packages, "opencode_houdini.json"), JSON.stringify({ enable: false, path: "x" })) + assert.deepEqual(((await plugin.rpc("status")) as { notices?: string[] }).notices ?? [], []) + } finally { + await plugin.cleanup() + rmSync(root, { recursive: true, force: true }) + } +}) + +it("panels share the host Houdini started; closing every panel keeps its offer for other conversations", () => { + const result = spawnSync(python, ["-I", join(import.meta.dirname, "panel_probe.py"), "test_shared_host_lifetime"], { + windowsHide: true, + encoding: "utf8", + }) assert.equal(result.status, 0, result.stderr) }) @@ -99,39 +120,48 @@ it("attachment and write permissions fence revoked hosts, without dispatch or re let revoke = () => {} const plugin = await loadPlugin({ answer: async () => revoke() }) const host = await startFakeHost({ - hostVersion: "0.2.0", - helloOverride: (hello) => ({ ...hello, app_version: "22.0.429" }), + hostVersion: version, + appVersion: "22.0.429", + methods: { apply: () => ({ status: "applied" }) }, }) - const offer = { - ...host.endpoint, - script_handle: host.hello.script_handle, - label: "Houdini", - protocol: 1, - offer_key: "write-test", - } + const offer = announcement(host.endpoint, host.scriptHandle, "write-test") try { await plugin.rpc("advertise", offer) let offerID = plugin.registry.hosts().offers[0]?.offerID as string revoke = () => { plugin.registry.withdraw({ offer_key: offer.offer_key, token: offer.token }) } - assert.deepEqual(JSON.parse(String((await plugin.tool("attach", { offerID }))?.content)), { outcome: "refused" }) + await assert.rejects(plugin.tool("houdini_hosts", { attach: offerID }), /changed while permission was asked/) assert.deepEqual(plugin.permissions, ["houdini_attach"]) + revoke = () => {} await plugin.rpc("advertise", offer) offerID = plugin.registry.hosts().offers[0]?.offerID as string await plugin.rpc("attachHost", { offerID, sessionID: "ses_test" }) - const writesBefore = host.requests.filter((request) => request.method === "apply").length + const writes = () => host.requests.filter((request) => request.method === "apply").length + // A dry run asks nothing; a change without confirm is refused before Houdini. + assert.equal( + content(await plugin.tool("houdini_apply", { if_revision: 1, command_id: "a", operations: [{}] })).status, + "applied", + ) + assert.equal(plugin.permissions.length, 1) + await assert.rejects( + plugin.tool("houdini_apply", { if_revision: 1, command_id: "b", operations: [{}], dry_run: false }), + /confirm: true/, + ) + revoke = () => { + plugin.registry.withdraw({ offer_key: offer.offer_key, token: offer.token }) + } + const before = writes() await assert.rejects( - plugin.tool("apply", { - script_handle: host.hello.script_handle, + plugin.tool("houdini_apply", { if_revision: 1, - command_id: "a", + command_id: "c", operations: [{}], dry_run: false, confirm: true, }), ) - assert.equal(host.requests.filter((request) => request.method === "apply").length, writesBefore) + assert.equal(writes(), before) assert.equal(plugin.permissions.at(-1), "houdini_write") } finally { await plugin.cleanup() @@ -139,10 +169,11 @@ it("attachment and write permissions fence revoked hosts, without dispatch or re } }) -it("package publishing metadata and preview marker keep the SDK optional and the host embedded", () => { +it("package publishing metadata and marker keep the SDK optional and the host embedded", () => { const pkg = readPackage() assert.equal(pkg.opencodeIntegration.id, "houdini") assert.equal(pkg.opencodeIntegration.plugin, "neuralnomads.houdini") + assert.equal(pkg.opencodeIntegration.host?.range, `^${version}`) const manifest = JSON.parse(readFileSync(join(import.meta.dirname, "..", "plugin", "package.json"), "utf8")) assert.equal(manifest.publishConfig.access, "public") assert.equal(manifest.peerDependencies["@opencode/plugin"], "^2.2.0 || ^0.0.0-dev") @@ -150,4 +181,7 @@ it("package publishing metadata and preview marker keep the SDK optional and the assert.equal(manifest.dependencies, undefined) for (const hook of ["preinstall", "install", "postinstall", "prepare"]) assert.equal(manifest.scripts[hook], undefined) + assert.ok(manifest.files.includes("methods") && manifest.files.includes("programs")) + const host = JSON.parse(readFileSync(join(import.meta.dirname, "..", "host", "manifest.json"), "utf8")) + assert.equal(host.version, version) }) diff --git a/integrations/houdini/test/helpers.ts b/integrations/houdini/test/helpers.ts index c55cf54..7faab90 100644 --- a/integrations/houdini/test/helpers.ts +++ b/integrations/houdini/test/helpers.ts @@ -1,43 +1,100 @@ -import { resolve } from "node:path" +import { mkdtempSync } from "node:fs" +import { tmpdir } from "node:os" +import { join, resolve } from "node:path" import type { Plugin } from "@opencode/plugin" import type { Info, ToolContext } from "@opencode/plugin/promise/tool" -import { createOfferRegistry, requestHost } from "../../../packages/integration-kit/src/index.ts" +import { createOfferRegistry, HELLO_TIMEOUT_MS, requestHost } from "../../../packages/integration-kit/src/index.ts" import { readPackage, setupHoudini } from "../plugin/src/integration.ts" +import { resetMethodForTests } from "../plugin/src/method.ts" +import type { PathEnv } from "../plugin/src/paths.ts" + +// A plugin storage as OpenCode keeps it: JSON values by key, scanned by prefix in key order. +export function memoryStorage() { + const values = new Map() + return { + values, + async get(key: string) { + return values.get(key) + }, + async set(key: string, value: unknown) { + values.set(key, structuredClone(value)) + }, + async remove(key: string) { + values.delete(key) + }, + async scan({ prefix, after, limit = 100 }: { prefix: string; after?: string; limit?: number }) { + const keys = [...values.keys()].filter((key) => key.startsWith(prefix) && (!after || key > after)).sort() + const page = keys.slice(0, limit) + return { + entries: page.map((key) => ({ key, value: values.get(key) })), + ...(keys.length > limit ? { next: page.at(-1) } : {}), + } + }, + } +} + +// A home folder of the test's own: Houdini preferences are looked for there, never in the user's. +export function isolatedPaths(home = mkdtempSync(join(tmpdir(), "houdini-home-"))): PathEnv { + return { + env: process.platform === "win32" ? { HOME: join(home, "Documents") } : {}, + platform: process.platform, + home, + } +} + +// Where Houdini keeps its preferences under those paths (paths.ts reads them there). +export function prefsFolder(paths: PathEnv, version: string) { + if (paths.platform === "darwin") return join(paths.home, "Library", "Preferences", "houdini", version) + return join(paths.platform === "win32" ? (paths.env.HOME as string) : paths.home, `houdini${version}`) +} export async function loadPlugin( input: { now?: () => number nativeGet?: (input: { sessionID: string }) => Promise - answer?: () => Promise + answer?: (action: string) => Promise | void options?: object state?: string wsl?: () => boolean + paths?: PathEnv + storage?: ReturnType // The daemon's shared offer list, as the plugin creates it, instead of a private one. shared?: boolean } = {}, ) { + resetMethodForTests() const marker = readPackage().opencodeIntegration const host = marker.host as NonNullable const registry = createOfferRegistry({ expected: { protocol: host.protocol, range: host.range, appVersions: host.appVersions }, now: input.now, - hello: (endpoint) => requestHost(endpoint, "hello", {}, { timeoutMs: 2000 }), + hello: (endpoint) => requestHost(endpoint, "hello", {}, { timeoutMs: HELLO_TIMEOUT_MS }), }) const handlers: Record Promise> = {} const events: Array<{ name: string; data: unknown }> = [] const tools = new Map() const permissions: string[] = [] + const methods: Array<{ sessionID: string; key: string }> = [] const location = { directory: resolve("test-location") } + const storage = input.storage ?? memoryStorage() const ctx = { options: input.options, location, + storage, session: { get: input.nativeGet ?? (async ({ sessionID }: { sessionID: string }) => { - if (sessionID !== "ses_test") throw new Error("not found") + if (sessionID !== "ses_test" && sessionID !== "ses_other") throw new Error("not found") return { id: sessionID, location } }), + instructions: { + entry: { + put: async ({ sessionID, key }: { sessionID: string; key: string }) => { + methods.push({ sessionID, key }) + }, + }, + }, }, rpc: { register: async (_definition: unknown, methods: typeof handlers) => { @@ -54,20 +111,14 @@ export async function loadPlugin( }, tool: { transform: async (callback: (editor: object) => void) => { - callback({ namespace: () => {}, add: (tool: Info) => tools.set(tool.name, tool) }) - return { dispose: async () => {} } - }, - }, - skill: { - transform: async (callback: (editor: object) => void) => { - callback({ get: () => null, add: () => {} }) + callback({ namespace: () => {}, add: (tool: Info) => tools.set(`houdini_${tool.name}`, tool) }) return { dispose: async () => {} } }, }, permission: { assert: async ({ action }: { action: string }) => { permissions.push(action) - await input.answer?.() + await input.answer?.(action) }, }, } as unknown as Plugin.Context @@ -75,6 +126,8 @@ export async function loadPlugin( registry: input.shared ? undefined : registry, wsl: input.wsl ?? (() => false), state: input.state, + paths: input.paths ?? isolatedPaths(), + now: input.now, }) const signal = new AbortController().signal return { @@ -82,14 +135,32 @@ export async function loadPlugin( cleanup, events, permissions, + methods, tools, + storage, rpc: async (name: string, payload: object = {}) => handlers[name]?.(payload, { signal }), - tool: async (name: string, payload: object = {}) => - tools - .get(name) - ?.execute( - payload as never, - { sessionID: "ses_test", signal, agent: "build", messageID: "msg_test", id: "call_test" } as ToolContext, - ), + tool: async (name: string, payload: object = {}, sessionID = "ses_test") => { + const tool = tools.get(name) + if (!tool) throw new Error(`No tool ${name}`) + return tool.execute( + payload as never, + { sessionID, signal, agent: "build", messageID: "msg_test", id: "call_test" } as ToolContext, + ) + }, } } + +// What a tool returned, as JSON. +export function content(result: unknown) { + return JSON.parse(String((result as { content?: unknown })?.content ?? result)) +} + +// An announcement of a fake host to the plugin's advertise RPC. +export function announcement( + endpoint: { host: string; port: number; token: string }, + scriptHandle: string, + offerKey = "test-offer", + label = "Houdini: test.hipnc", +) { + return { ...endpoint, script_handle: scriptHandle, label, protocol: 1, offer_key: offerKey } +} diff --git a/integrations/houdini/test/hints_probe.py b/integrations/houdini/test/hints_probe.py new file mode 100644 index 0000000..21fd7c0 --- /dev/null +++ b/integrations/houdini/test/hints_probe.py @@ -0,0 +1,233 @@ +"""execute_code hints (host/scripts/opencode_houdini/hints.py) under system Python with a stub hou; no Houdini.""" +import importlib.util +from pathlib import Path +import sys +import types +import unittest + +FILE = Path(__file__).resolve().parents[1] / "host" / "scripts" / "opencode_houdini" / "hints.py" + + +def load(name): + spec = importlib.util.spec_from_file_location(name, FILE) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def stub_hou(): + hou = types.ModuleType("hou") + + class OperationFailed(Exception): + __module__ = "hou" + + class Error(Exception): + __module__ = "hou" + + class NodeType: + __module__ = "hou" + + def __init__(self, name, description): + self._name, self._description = name, description + + def name(self): + return self._name + + def description(self): + return self._description + + def nameWithCategory(self): + return "Sop/" + self._name + + class Category: + __module__ = "hou" + + def __init__(self, name, types_): + self._name, self._types = name, {t: NodeType(t, d) for t, d in types_.items()} + + def name(self): + return self._name + + def nodeTypes(self): + return self._types + + sop = Category("Sop", {"box": "Box", "sphere": "Sphere", "skybox": "Sky Box", "attribwrangle": "Attribute Wrangle"}) + obj = Category("Object", {"geo": "Geometry", "cam": "Camera"}) + + class Parm: + __module__ = "hou" + + def __init__(self, name): + self._name = name + + def name(self): + return self._name + + class OpNode: + __module__ = "hou" + all = {} + + def __init__(self, path, category=None, parms=(), tuples=None, kind="geo", errors=()): + self._path, self._category, self._kind, self._errors = path, category, kind, list(errors) + self._parms = [Parm(p) for p in parms] + self._tuples = tuples or {} + OpNode.all[path] = self + + def path(self): + return self._path + + def name(self): + return self._path.rsplit("/", 1)[-1] + + def parent(self): + return OpNode.all.get(self._path.rsplit("/", 1)[0] or "/") + + def children(self): + return [n for p, n in OpNode.all.items() if p.rsplit("/", 1)[0] == self._path and p != self._path] + + def childTypeCategory(self): + return self._category + + def type(self): + return NodeType(self._kind, self._kind) + + def parm(self, name): + return next((p for p in self._parms if p.name() == name), None) + + def parms(self): + return list(self._parms) + + def parmTuple(self, name): + return [Parm(p) for p in self._tuples[name]] if name in self._tuples else None + + def parmTuples(self): + return [Parm(t) for t in self._tuples] + + def node(self, rel): + return OpNode.all.get(self._path + "/" + rel) + + def errors(self): + return self._errors + + def warnings(self): + return [] + + def inputAncestors(self): + return [] + + def createNode(self, node_type_name, node_name=None): + """createNode(self, node_type_name, node_name=None, run_init_scripts=True) -> hou.Node + + Create a new node of type node_type_name as a child of this node. + """ + raise OperationFailed("The attempted operation failed.\nInvalid node type name") + + def cook(self, force=False): + if self._errors: + raise OperationFailed("The attempted operation failed.\nError while cooking.") + + hou.OperationFailed, hou.Error, hou.OpNode, hou.Node = OperationFailed, Error, OpNode, OpNode + OpNode("/", None, kind="root") + OpNode("/obj", obj, kind="obj") + OpNode("/obj/geo1", sop) + OpNode("/obj/geo1/box1", None, parms=("type", "sizex", "sizey", "sizez", "scale"), tuples={"size": ("sizex", "sizey", "sizez")}, kind="box") + OpNode("/obj/geo1/broken", None, kind="attribwrangle", errors=["Call to undefined function 'undefined_fn'. (1,9:20)"]) + hou.node = lambda path: OpNode.all.get(path) + hou.nodeTypeCategories = lambda: {"Sop": sop, "Object": obj} + hou.nodee = None + del hou.nodee + return hou + + +class NoHou(unittest.TestCase): + def test_imports_and_explains_nothing_without_hou(self): + saved = sys.modules.pop("hou", None) + try: + sys.modules["hou"] = None # import hou raises ImportError + module = load("hints_without_hou") + self.assertIsNone(module.explain(ValueError("x"), "x", None)) + self.assertEqual(module.closest("boxx", ["box", "skybox", "sphere"]), ["box", "skybox"]) + finally: + sys.modules.pop("hou", None) + if saved is not None: + sys.modules["hou"] = saved + + +class Hints(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.hou = stub_hou() + sys.modules["hou"] = cls.hou + cls.hints = load("hints_stub") + + @classmethod + def tearDownClass(cls): + sys.modules.pop("hou", None) + + def explain(self, code): + namespace = {"hou": self.hou} + try: + exec(compile(code, "", "exec"), namespace) # noqa: S102 + except Exception as exc: # noqa: BLE001 + return self.hints.explain(exc, code, None) + self.fail("the code did not fail") + + def test_closest(self): + self.assertEqual(self.hints.closest("SIZEXX", ["sizex", "sizey", "scale"])[0], "sizex") + self.assertEqual(self.hints.closest("", ["a"]), []) + + def test_unknown_node_type(self): + hint = self.explain("geo = hou.node('/obj/geo1')\ngeo.createNode('boxx')\n") + self.assertIn("/obj/geo1 (Sop network) has no node type 'boxx'", hint) + self.assertIn("box", hint) + + def test_type_of_another_category(self): + hint = self.explain("hou.node('/obj').createNode('box')\n") + self.assertIn("'box' is a type of Sop", hint) + self.assertIn("createNode('geo')", hint) + + def test_parm_none(self): + hint = self.explain("box = hou.node('/obj/geo1/box1')\nbox.parm('sizexx').set(2)\n") + self.assertIn("/obj/geo1/box1 has no parameter 'sizexx'", hint) + self.assertIn("closest sizex", hint) + + def test_parm_is_a_tuple(self): + hint = self.explain("hou.node('/obj/geo1/box1').parm('size').set(2)\n") + self.assertIn("use parmTuple('size')", hint) + + def test_missing_node_path(self): + hint = self.explain("hou.node('/obj/geo1/boxx').parm('sizex').set(2)\n") + self.assertIn("/obj/geo1 has no child 'boxx'", hint) + self.assertIn("box1", hint) + + def test_unsafe_receiver_is_not_evaluated(self): + calls = [] + self.hou.spy = lambda: calls.append(1) or None + hint = self.explain("hou.spy().parm('x').set(1)\n") + self.assertEqual(calls, [1]) # only the run itself called it + self.assertIsNone(hint) + + def test_cook_error(self): + hint = self.explain("hou.node('/obj/geo1/broken').cook(force=True)\n") + self.assertIn("undefined_fn", hint) + + def test_attribute_on_hou_object(self): + hint = self.explain("hou.node('/obj/geo1/box1').parmm('sizex')\n") + self.assertIn("closest parm", hint) + hint = self.explain("hou.nodee('/obj')\n") + self.assertIn("node", hint) + + def test_swig_type_error(self): + hint = self.hints.explain(TypeError("in method 'OpNode_createNode', argument 2 of type 'char const *'"), "", None) + self.assertIn("createNode(self, node_type_name", hint) + + def test_bounded(self): + many = ["p%d" % i for i in range(500)] + self.hou.OpNode("/obj/geo1/big", None, parms=many, kind="big") + hint = self.explain("hou.node('/obj/geo1/big').parm('zz').set(1)\n") + self.assertLessEqual(len(hint), self.hints.MAX_CHARS) + self.assertIn("more)", hint) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/integrations/houdini/test/host_probe.py b/integrations/houdini/test/host_probe.py index b1c37ab..d57115e 100644 --- a/integrations/houdini/test/host_probe.py +++ b/integrations/houdini/test/host_probe.py @@ -1,7 +1,10 @@ """The integration's own host transport under system Python; no Houdini or licence required.""" import collections -import importlib.util +import contextlib +import importlib import json +import threading +import time from pathlib import Path import sys import types @@ -15,12 +18,80 @@ hou.applicationVersionString = lambda: "22.0.429" hou.hipFile = types.SimpleNamespace(basename=lambda: "test.hip") hou.parmData = types.SimpleNamespace(Int=1, Float=2, String=3) +hou.ObjectWasDeleted = type("ObjectWasDeleted", (Exception,), {}) +hou.getenv = lambda name, default=None: default + + +# A scene of nodes and Houdini's grouped undo, simulated: enough for execute_code's undo group, its rollback +# and the structural check of a restoration. +class _Node: + def __init__(self, name): + self._path = "/obj/" + name + + def path(self): + return self._path + + def type(self): + return types.SimpleNamespace(nameWithCategory=lambda: "Object/geo") + + def inputs(self): + return [] + + +class _Undo: + def __init__(self): + self.labels, self.stack, self.open, self.broken = [], [], None, False + + @contextlib.contextmanager + def group(self, label): + self.open = [] + try: + yield + finally: + actions, self.open = self.open, None + if actions: + self.labels.append(label) + self.stack.append(actions) + + def undoLabels(self): + return list(self.labels) + + def performUndo(self): + if self.broken: + raise RuntimeError("undo failed") + self.labels.pop() + for action in reversed(self.stack.pop()): + action() + + +SCENE = [] + + +def add_node(name): + node = _Node(name) + SCENE.append(node) + if hou.undos.open is not None: + hou.undos.open.append(lambda: SCENE.remove(node)) + return node + + +class _Root: + def allSubChildren(self, **kwargs): + return list(SCENE) + + def setUserData(self, *args): + pass + + def destroyUserData(self, *args): + pass + + +hou.undos = _Undo() +hou.add_node = add_node sys.modules["hou"] = hou -file = Path(__file__).resolve().parents[1] / "host" / "scripts" / "opencode_houdini" / "host.py" -spec = importlib.util.spec_from_file_location("houdini_host", file) -module = importlib.util.module_from_spec(spec) -sys.modules[spec.name] = module -spec.loader.exec_module(module) +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "host" / "scripts")) +module = importlib.import_module("opencode_houdini.host") +runner = importlib.import_module("opencode_houdini.runner") def state(): @@ -31,6 +102,10 @@ def state(): value.flush = lambda **kwargs: None value.summary = lambda: {} value.quarantined = False + value.quarantine = None + value.root = _Root() + value._summary_dirty = False + value._reconcile_after_rollback = lambda: None value._closed = False value._commands = collections.OrderedDict() value.cancellation_state = lambda session: (0, False) @@ -95,14 +170,83 @@ def test_refusals_keep_message_data_and_code(self): self.assertEqual(len(refusal["message"]), module.WIRE_MESSAGE_MAX) self.assertEqual(refusal["data"], {"reason": "expectation_failed", "truncated": True}) + def execute(self, value, code, **params): + host = types.SimpleNamespace(saves=0, loads=0, state=value) + return runner.execute_code(host, value, dict(params, code=code), lambda error, code, filename: "hint: " + str(error)) + + def setUp(self): + SCENE.clear() + hou.undos = _Undo() + + def test_execute_code_groups_a_change_and_undoes_a_failure(self): + value = state() + out = self.execute(value, "hou.add_node('a')\nresult = {'nodes': 1}") + self.assertEqual((out["status"], out["result"], out["changed"], out["undo_step"]), ("executed", {"nodes": 1}, True, "OpenCode: code")) + self.assertEqual(hou.undos.labels, ["OpenCode: code"]) + out = self.execute(value, "hou.add_node('b')\nhou.add_node('c')\nraise ValueError('nope')") + self.assertEqual((out["status"], out["restored"], out["hint"]), ("failed", True, "hint: nope")) + self.assertIn("ValueError: nope", out["message"]) + self.assertEqual([node.path() for node in SCENE], ["/obj/a"]) + self.assertFalse(value.quarantined) + + def test_unproven_restoration_quarantines_until_released_with_the_read_revision(self): + value = state() + hou.undos.broken = True + out = self.execute(value, "hou.add_node('a')\nraise RuntimeError('boom')") + self.assertEqual((out["status"], out["restored"]), ("failed", False)) + self.assertTrue(value.quarantined) + self.assertIn("failed code", out["quarantine"]["cause"]) + with self.assertRaises(module.HostError) as caught: + self.execute(value, "hou.add_node('b')") + self.assertEqual(module.wire_error(caught.exception)["code"], "quarantined") + self.assertEqual(self.execute(value, "result = {'read': True}", read_only=True)["status"], "executed") + with self.assertRaises(module.HostError) as caught: + runner.release(value, {"revision": value.revision + 1}) + self.assertEqual(module.wire_error(caught.exception)["code"], "stale_revision") + self.assertEqual(runner.release(value, {"revision": value.revision})["released"], True) + self.assertFalse(value.quarantined) + + def test_read_only_runs_make_no_undo_group(self): + value = state() + out = self.execute(value, "print('hello')\nresult = {'x': 1}", read_only=True) + self.assertEqual((out["stdout"], out["changed"], hou.undos.labels), ("hello\n", False, [])) + + def test_status_answers_off_the_main_thread_during_a_long_call(self): + host = module.Host.__new__(module.Host) + host.state = state() + host.ui = module.UIThreadGate() + host.saves = host.loads = 0 + # A request from a connection's thread runs on the main thread (here, pumped as hython does). + thread = threading.Thread( + target=host.dispatch, + args=("execute_code", {"script_handle": host.state.handle, "code": "import time\ntime.sleep(1.5)", "read_only": True}), + ) + seen = [] + asker = threading.Thread(target=lambda: (time.sleep(0.5), seen.append(host.dispatch("status", {})))) + thread.start() + asker.start() + host.ui.pump(3.0, stop=lambda: not thread.is_alive() and not asker.is_alive()) + thread.join() + asker.join() + status = seen[0] + self.assertEqual((status["busy"], status["running"]), (True, "execute_code")) + self.assertEqual(host.dispatch("status", {})["busy"], False) + + def test_hello_names_the_process_and_its_crash_folder(self): + self.assertTrue(module.temp_dir().endswith("houdini_temp")) + self.assertEqual(module.offer_label("shot/v2.hipnc"), "Houdini: shot v2.hipnc") + self.assertIn("status", module.METHODS) + self.assertIn("execute_code", module.METHODS) + def test_unknown_method_and_stale_handle(self): host = module.Host.__new__(module.Host) host.state = state() - host.ui = types.SimpleNamespace(run=lambda callback: callback()) + host.ui = module.UIThreadGate() + host.saves = host.loads = 0 with self.assertRaises(module.HostError): host.dispatch("context", {"script_handle": "wrong"}) with self.assertRaises(module.HostError): - host.dispatch("execute_code", {"script_handle": host.state.handle}) + host.dispatch("no_such_method", {"script_handle": host.state.handle}) host.state._closed = True with self.assertRaises(module.HostError): host.dispatch("hello", {}) @@ -111,11 +255,16 @@ def test_unknown_method_and_stale_handle(self): if "--serve" in sys.argv: host = module.Host.__new__(module.Host) host.state = state() - host.ui = types.SimpleNamespace(run=lambda callback: callback()) + host.ui = module.UIThreadGate() + host.saves = host.loads = 0 server = module.HostServer(host) # Private pipe to the test process, never logged by the integration. print(json.dumps(server.info()), flush=True) - sys.stdin.readline() + # The main thread serves the requests (as hython's main thread does) until the test says stop. + stopping = threading.Event() + threading.Thread(target=lambda: (sys.stdin.readline(), stopping.set()), daemon=True).start() + while not stopping.is_set(): + host.ui.pump(0.2) server.close() else: unittest.main() diff --git a/integrations/houdini/test/hython.test.ts b/integrations/houdini/test/hython.test.ts new file mode 100644 index 0000000..f1c26bc --- /dev/null +++ b/integrations/houdini/test/hython.test.ts @@ -0,0 +1,24 @@ +// The fixed programs, the hints and the installed part itself (execute_code with its undo, status, saves) in a +// real Houdini: opt-in, with HOUDINI_TEST_BIN naming hython or Houdini's bin folder. The run uses private +// preferences, packages and temp folders (programs_hython.py), never the user's. + +import assert from "node:assert/strict" +import { spawnSync } from "node:child_process" +import { join } from "node:path" +import { it } from "node:test" + +it("programs, hints and the installed part in a real Houdini (HOUDINI_TEST_BIN)", { + skip: !process.env.HOUDINI_TEST_BIN, +}, () => { + const python = process.env.PYTHON ?? (process.platform === "win32" ? "python" : "python3") + const run = spawnSync(python, ["-I", join(import.meta.dirname, "programs_hython.py")], { + windowsHide: true, + encoding: "utf8", + timeout: 1_800_000, + maxBuffer: 64 * 1024 * 1024, + }) + const report = (run.stdout ?? "") + .split(/\r?\n/) + .filter((line) => / (ok|FAIL) +\d/.test(line) || /passed, \d+ failed/.test(line)) + assert.equal(run.status, 0, `${report.join("\n")}\n${(run.stdout ?? "").slice(-4000)}${run.stderr ?? ""}`) +}) diff --git a/integrations/houdini/test/panel_probe.py b/integrations/houdini/test/panel_probe.py index 7b184c4..9e7be40 100644 --- a/integrations/houdini/test/panel_probe.py +++ b/integrations/houdini/test/panel_probe.py @@ -1,15 +1,19 @@ """Exercise the actual panel import and shared listener without Houdini or Qt.""" import json +import os from pathlib import Path import socket from string import Template import sys +import threading import types import unittest from unittest.mock import Mock, patch import xml.etree.ElementTree as ET +# Never offer this test's host to the OpenCode services of the computer running it. +os.environ["OPENCODE_HOUDINI_OFFER"] = "0" hou = types.ModuleType("hou") hou.nodeEventType = types.SimpleNamespace(**{name: name for name in ( "BeingDeleted", "ChildCreated", "ChildDeleted", "ChildReordered", "FlagChanged", "InputRewired", @@ -110,11 +114,21 @@ def assert_live(): self.assertIs(integration._host, host) self.assertEqual(integration.hostInfo(), endpoint) self.assertTrue(second._connected) - with socket.create_connection((endpoint["host"], endpoint["port"]), timeout=2) as connection: - request = {"id": "probe", "token": endpoint["token"], "method": "hello", "params": {}} - connection.sendall((json.dumps(request) + "\n").encode()) - with connection.makefile("rb") as response: - result = json.loads(response.readline()) + replies = [] + + def ask(): + with socket.create_connection((endpoint["host"], endpoint["port"]), timeout=5) as connection: + request = {"id": "probe", "token": endpoint["token"], "method": "hello", "params": {}} + connection.sendall((json.dumps(request) + "\n").encode()) + with connection.makefile("rb") as response: + replies.append(json.loads(response.readline())) + + # The request runs on this, the main thread, which serves it as hython's does. + asker = threading.Thread(target=ask) + asker.start() + integration.pump(5.0, stop=lambda: not asker.is_alive()) + asker.join() + result = replies[0] self.assertTrue(result["ok"]) self.assertEqual(result["result"]["script_handle"], state.handle) @@ -134,46 +148,36 @@ def assert_live(): first._connect() self.assertEqual(client.send.call_count, sends) assert_live() - state.close.assert_not_called() - # Close delivery is asynchronous (or can fail); host revocation is synchronous. + # The last panel closing leaves the host with Houdini: its offer serves the other conversations. client.send.side_effect = RuntimeError("sidecar unavailable") interfaces[1]["onDestroyInterface"]() self.assertEqual(integration._panel_owners, set()) self.assertIsNone(interfaces[1]["panel"]) self.assertEqual(client.eventReceived.disconnect.call_count, 2) self.assertEqual(client.diagnostic.disconnect.call_count, 2) - self.assertFalse(server.running) - self.assertNotEqual(server.token, endpoint["token"]) - self.assertIsNone(integration._host) - with self.assertRaises(OSError): - socket.create_connection((endpoint["host"], endpoint["port"]), timeout=2) - state.close.assert_called_once() + self.assertTrue(server.running) + self.assertEqual(integration.hostInfo(), endpoint) + state.close.assert_not_called() client.send.side_effect = None - # Repeated release is harmless; fresh consent creates fresh authority. - second._destroyed() + # A new panel uses the same host, authority unchanged. third = panel.OpenCodePanel.__new__(panel.OpenCodePanel) third.__dict__.update(second.__dict__, _disposed=False, _channel="third") third._connect() - replacement = integration._server - self.assertNotEqual(replacement.token, endpoint["token"]) - first._destroyed() # A disconnected panel cannot revoke the remaining owner. - self.assertTrue(replacement.running) - event = Mock() - third.closeEvent(event) - event.accept.assert_called_once() + self.assertIs(integration._server, server) + params = client.send.call_args.args[2] + self.assertEqual(params["offer_label"], "Houdini: untitled.hip") + self.assertTrue(params["sessions_file"].endswith("panel-sessions.json")) third._destroyed() - client.send.assert_called_with(third._channel, "close", {}) - self.assertFalse(replacement.running) - self.assertIsNone(integration._host) - with self.assertRaises(OSError): - socket.create_connection(("127.0.0.1", replacement.port), timeout=2) - self.assertEqual(state.close.call_count, 2) + self.assertTrue(server.running) finally: integration.shutdown() + # Houdini exiting (atexit) stops the host and invalidates its token. self.assertFalse(server.running) self.assertNotEqual(server.token, endpoint["token"]) self.assertIsNone(integration._host) - self.assertEqual(state.close.call_count, 2) + with self.assertRaises(OSError): + socket.create_connection((endpoint["host"], endpoint["port"]), timeout=2) + state.close.assert_called_once() integration.shutdown() # Explicit cleanup and process exit are idempotent. diff --git a/integrations/houdini/test/plugin.test.ts b/integrations/houdini/test/plugin.test.ts new file mode 100644 index 0000000..f39e957 --- /dev/null +++ b/integrations/houdini/test/plugin.test.ts @@ -0,0 +1,185 @@ +// OpenCode for Houdini 0.3.0 against a fake installed part: the conversation attaches itself to the only free +// Houdini, the working method joins it, code runs only with permission, and a Houdini that crashes, closes or +// stays busy is said as such. + +import assert from "node:assert/strict" +import { mkdtempSync, rmSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { describe, it } from "node:test" + +import { startFakeHost } from "../../../packages/testing/src/index.ts" +import { readPackage } from "../plugin/src/integration.ts" +import { METHOD_KEY } from "../plugin/src/method.ts" +import { announcement, content, loadPlugin } from "./helpers.ts" + +const version = readPackage().version +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + +function capabilities(methods: string[]) { + return { + methods: [...new Set(["hello", "context", "apply", "status", "release", ...methods])], + operations: [], + undo: "grouped_best_effort" as const, + background: false, + } +} + +async function offered( + methods: Record) => unknown> = {}, + process?: { pid: number; temp_dir: string }, +) { + const host = await startFakeHost({ + hostVersion: version, + appVersion: "22.0.429", + capabilities: capabilities(Object.keys(methods)), + methods: { context: () => ({ revision: 3, entities: [] }), ...methods }, + ...(process + ? { helloOverride: (hello: Record) => ({ ...hello, process, document: "shot.hipnc" }) } + : {}), + }) + return host +} + +describe("OpenCode for Houdini 0.3.0", () => { + it("attaches the only free Houdini at the first call, with the working method; listing asks no permission", async () => { + const plugin = await loadPlugin() + const host = await offered() + try { + await plugin.rpc("advertise", announcement(host.endpoint, host.scriptHandle)) + const listed = content(await plugin.tool("houdini_hosts")) + assert.equal(listed.houdinis.length, 1) + assert.equal(listed.attached_to, null) + assert.deepEqual(plugin.permissions, []) + const read = content(await plugin.tool("houdini_context")) + assert.equal(read.revision, 3) + assert.equal(read.attached_to.label, "Houdini: test.hipnc") + assert.deepEqual(plugin.methods, [{ sessionID: "ses_test", key: METHOD_KEY }]) + // Another conversation does not take the Houdini this one uses: it is told who holds it. + await assert.rejects(plugin.tool("houdini_context", {}, "ses_other"), /attached to another conversation/) + const shared = content(await plugin.tool("houdini_hosts", { attach: listed.houdinis[0].offerID }, "ses_other")) + assert.match(shared.shared, /Another conversation is attached to this Houdini too/) + } finally { + await plugin.cleanup() + await host.close() + } + }) + + it("says how to offer a Houdini when none is", async () => { + const plugin = await loadPlugin() + try { + await assert.rejects( + plugin.tool("houdini_context"), + /No Houdini is offered: start Houdini 21 or later with OpenCode Link installed/, + ) + } finally { + await plugin.cleanup() + } + }) + + it("execute_code asks permission for each run, reads a script file as is, and keeps the host's hint", async () => { + const answers: string[] = [] + const plugin = await loadPlugin({ answer: (action) => void answers.push(action) }) + const dir = mkdtempSync(join(tmpdir(), "houdini-script-")) + const runs: Array> = [] + const host = await offered({ + execute_code: (params) => { + runs.push(params) + return { status: "executed", result: { ok: 1 }, changed: true, undo_step: "OpenCode: code" } + }, + }) + try { + await plugin.rpc("advertise", announcement(host.endpoint, host.scriptHandle)) + const out = content(await plugin.tool("houdini_execute_code", { code: "result = {'ok': 1}" })) + assert.deepEqual([out.status, out.undo_step], ["executed", "OpenCode: code"]) + assert.deepEqual(answers, ["houdini_execute_code"]) + const file = join(dir, "build.py") + writeFileSync(file, "geo = hou.node('/obj').createNode('geo')\nprint(\"é\")\n") + await plugin.tool("houdini_execute_code", { path: file }) + assert.equal(runs[1]?.code, "geo = hou.node('/obj').createNode('geo')\nprint(\"é\")\n") + assert.equal(runs[1]?.filename, file) + assert.equal(runs[1]?.session_id, "ses_test") + assert.deepEqual(answers, ["houdini_execute_code", "houdini_execute_code"]) + } finally { + await plugin.cleanup() + await host.close() + rmSync(dir, { recursive: true, force: true }) + } + }) + + it("a refusal reads the hint for its code with Houdini's own text and data", async () => { + const plugin = await loadPlugin() + const host = await offered({ + release: () => { + throw { + code: "stale_revision", + message: "Read the scene again (houdini_context) and release with its revision.", + data: { current_revision: 7, reason: "revision_conflict" }, + } + }, + }) + try { + await plugin.rpc("advertise", announcement(host.endpoint, host.scriptHandle)) + await assert.rejects( + plugin.tool("houdini_context", { release_quarantine: 2 }), + /^Error: The scene changed since your last read: call houdini_context and use its revision\. Read the scene again \(houdini_context\) and release with its revision\. \{"current_revision":7,"reason":"revision_conflict"\}$/, + ) + } finally { + await plugin.cleanup() + await host.close() + } + }) + + it("a Houdini that crashes during a call is said within 2 s with its crash report (0.3.0)", async () => { + const temp = mkdtempSync(join(tmpdir(), "houdini_temp-")) + const plugin = await loadPlugin() + // The crashed Houdini's connection stays open: the call would wait its whole timeout. + const host = await offered({ execute_code: () => new Promise(() => {}) }, { pid: process.pid, temp_dir: temp }) + try { + await plugin.rpc("advertise", announcement(host.endpoint, host.scriptHandle)) + // Another Houdini's report in the same folder is not this one's. + writeFileSync(join(temp, "crash.other.Admin_1_log.txt"), "") + setTimeout(() => writeFileSync(join(temp, `crash.shot.Admin_${process.pid}_log.txt`), "Houdini crashed"), 300) + const started = Date.now() + await assert.rejects( + plugin.tool("houdini_execute_code", { code: "hou.node('/obj').cook()", timeout_s: 60 }), + /^Error: Houdini crashed \(Houdini: test\.hipnc\): crash report .*crash\.shot\.Admin_\d+_log\.txt\. Its process \d+ is still open/, + ) + assert.ok(Date.now() - started < 4_000, `said after ${Date.now() - started} ms`) + // Houdini 22 also saves the scene at the crash (crash.._.hipnc): the way to recover it. + writeFileSync(join(temp, `crash.shot.Admin_${process.pid}.hipnc`), "scene") + rmSync(join(temp, `crash.shot.Admin_${process.pid}_log.txt`)) + await assert.rejects( + plugin.tool("houdini_execute_code", { code: "hou.node('/obj').cook()", timeout_s: 60 }), + /crash report .*crash\.shot\.Admin_\d+\.hipnc\. Its process \d+ is still open .* Houdini saved the scene as it was at the crash in that file/, + ) + } finally { + await plugin.cleanup() + await host.close() + rmSync(temp, { recursive: true, force: true }) + } + }) + + it("a call that outlasts its wait says Houdini still runs it (status off the main thread)", async () => { + const temp = mkdtempSync(join(tmpdir(), "houdini_temp-")) + const plugin = await loadPlugin() + const host = await offered( + { + execute_code: () => sleep(5_000).then(() => ({ status: "executed" })), + status: () => ({ busy: true, running: "execute_code", seconds: 1.2, queued: 0 }), + }, + { pid: process.pid, temp_dir: temp }, + ) + try { + await plugin.rpc("advertise", announcement(host.endpoint, host.scriptHandle)) + await assert.rejects( + plugin.tool("houdini_execute_code", { code: "import time; time.sleep(5)", timeout_s: 1 }), + /Houdini is still running execute_code \(1\.2 s so far\): it goes on in Houdini, nothing is lost/, + ) + } finally { + await plugin.cleanup() + await host.close() + rmSync(temp, { recursive: true, force: true }) + } + }) +}) diff --git a/integrations/houdini/test/programs_hython.py b/integrations/houdini/test/programs_hython.py new file mode 100644 index 0000000..38131b9 --- /dev/null +++ b/integrations/houdini/test/programs_hython.py @@ -0,0 +1,645 @@ +"""The fixed programs and hints under a real Houdini (opt-in: HOUDINI_TEST_BIN = hython, or Houdini's bin folder). + +Run with any Python 3: python programs_hython.py [test names...]. Under system Python it starts hython on itself in +an isolated environment (private preferences, packages and temp folders; never the user's), then, under hython, +builds scenes in a temp folder, runs each program as the plugin sends it (PARAMS, and JOB for verify_saved and +render, prepended; exec with hou, PARAMS and result) and checks the results. Evidence (each program's result) is +written to OPENCODE_HOUDINI_EVIDENCE (default: the run's temp folder) as programs_hython.json. +""" + +import json +import os +import shutil +import subprocess +import sys +import tempfile +import time +import traceback + +HERE = os.path.dirname(os.path.abspath(__file__)) +PROGRAMS = os.path.join(HERE, "..", "plugin", "programs") +HOST_SCRIPTS = os.path.join(HERE, "..", "host", "scripts") + + +def hython_path(): + given = os.environ.get("HOUDINI_TEST_BIN", "") + if not given: + return None + if os.path.isdir(given): + given = os.path.join(given, "hython.exe" if os.name == "nt" else "hython") + return given if os.path.isfile(given) else None + + +def launch(): + hython = hython_path() + if hython is None: + print("SKIP: set HOUDINI_TEST_BIN to hython (or Houdini's bin folder)") + return 0 + root = tempfile.mkdtemp(prefix="oc_houdini_programs_") + version = subprocess.run([hython, "-c", "import hou;print('%d.%d' % hou.applicationVersion()[:2])"], + capture_output=True, text=True, env=isolated(root), timeout=600, + creationflags=0x08000000 if os.name == "nt" else 0).stdout.strip().splitlines() + os.makedirs(os.path.join(root, "prefs", "houdini" + (version[-1] if version else "")), exist_ok=True) + env = isolated(root) + env["OPENCODE_HOUDINI_TEST_ROOT"] = root + started = time.time() + run = subprocess.run([hython, os.path.abspath(__file__)] + sys.argv[1:], env=env, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, timeout=3600, creationflags=0x08000000 if os.name == "nt" else 0) + text = run.stdout.decode("utf-8", "replace") + # Loading every HDA's parameters in hython warns about viewer handles that only exist with the UI. + noise = [l for l in text.splitlines() if l.startswith("oplib:") and "error binding handle" in l] + sys.stdout.write("\n".join(l for l in text.splitlines() if l not in noise) + "\n") + if noise: + print("(%d 'error binding handle' lines from HDA definitions loaded without UI, hidden)" % len(noise)) + if "missing __HVER__" in text: + print("FAIL: Houdini ignored the private preferences folder") + return 1 + summary = [l for l in text.splitlines() if l.startswith("OPENCODE_TESTS ")] + print("hython run: exit %d in %.1f s (root %s)" % (run.returncode, time.time() - started, root)) + if not summary: + print("FAIL: hython ended before reporting its tests") + return 1 + # hython can crash in its own exit handlers after the tests reported (exit 139): the report decides. + return 0 if json.loads(summary[-1].split(" ", 1)[1])["failed"] == 0 else 1 + + +def isolated(root): + env = {k: v for k, v in os.environ.items() if k != "OPENCODE_HOUDINI_OFFER_KEY" and not k.startswith("HOUDINI_")} + tmp = os.path.join(root, "tmp") + for d in ("prefs", "packages", "tmp"): + os.makedirs(os.path.join(root, d), exist_ok=True) + env.update(HOUDINI_USER_PREF_DIR=os.path.join(root, "prefs", "houdini__HVER__"), + HOUDINI_PACKAGE_DIR=os.path.join(root, "packages"), HOUDINI_NO_ENV_FILE="1", + HOUDINI_TEMP_DIR=os.path.join(tmp, "houdini_temp"), TEMP=tmp, TMP=tmp, TMPDIR=tmp) + if "HOUDINI_TEST_BIN" in os.environ: + env["HOUDINI_TEST_BIN"] = os.environ["HOUDINI_TEST_BIN"] + return env + + +try: + import hou # noqa: E402 +except ImportError: + hou = None + +if hou is None: + if __name__ == "__main__": + sys.exit(launch()) + raise SystemExit(0) + +# --- under hython --------------------------------------------------------------------------------------------- +sys.path.insert(0, HOST_SCRIPTS) +from opencode_houdini import hints # noqa: E402 + +ROOT = os.environ.get("OPENCODE_HOUDINI_TEST_ROOT") or tempfile.mkdtemp(prefix="oc_houdini_programs_") +WORK = tempfile.mkdtemp(prefix="scenes_", dir=os.environ.get("TEMP")) +EVIDENCE = {} +RESULTS = [] + + +def source(name): + with open(os.path.join(PROGRAMS, name), encoding="utf-8") as fh: + return fh.read() + + +def program(name, params, job=None): + """As the plugin sends it: PARAMS (and JOB) prepended, run with exec in a namespace holding hou and result.""" + text = "import json\nPARAMS = json.loads(%s)\n" % json.dumps(json.dumps(params)) + if job is not None: + text += "JOB = %s\n" % json.dumps(source(job)) + text += source(name) + namespace = {"hou": hou, "__name__": "__opencode__", "result": {}} + exec(compile(text, "", "exec"), namespace) # noqa: S102 + return json.loads(json.dumps(namespace.get("result", {}), default=repr)) + + +def failing(name, params, job=None): + try: + program(name, params, job) + except Exception as exc: # noqa: BLE001 + return "%s: %s" % (type(exc).__name__, exc) + raise AssertionError("%s with %s did not fail" % (name, params)) + + +def hint_of(code): + """execute_code's except block: the code fails inside exec, explain runs while its frames live.""" + namespace = {"hou": hou, "__name__": "__opencode__", "result": {}} + try: + exec(compile(code, "", "exec"), namespace) # noqa: S102 + except Exception as exc: # noqa: BLE001 + return hints.explain(exc, code, None), "%s: %s" % (type(exc).__name__, exc) + raise AssertionError("the code did not fail") + + +def check(condition, message): + if not condition: + raise AssertionError(message) + + +TESTS = [] + + +def test(fn): + TESTS.append(fn) + return fn + + +def reset_scene(): + hou.hipFile.clear(suppress_save_prompt=True) + + +def sop_scene(): + """/obj/geo1: box (size 2, 1, 1) -> wrangle (adds point attribute 'mask'); camera; an erroring wrangle.""" + reset_scene() + geo = hou.node("/obj").createNode("geo", "geo1") + box = geo.createNode("box", "box1") + box.parmTuple("size").set((2, 1, 1)) + wrangle = geo.createNode("attribwrangle", "mask") + wrangle.setInput(0, box) + wrangle.parm("snippet").set("f@mask = @P.x > 0;") + wrangle.setDisplayFlag(True) + wrangle.setRenderFlag(True) + bad = geo.createNode("attribwrangle", "broken") + bad.setInput(0, box) + bad.parm("snippet").set("@P.y += undefined_fn(1);") + cam = hou.node("/obj").createNode("cam", "cam1") + cam.parmTuple("t").set((4, 3, 6)) + cam.parmTuple("r").set((-22, 33, 0)) + cam.parmTuple("res").set((320, 240)) + light = hou.node("/obj").createNode("hlight::2.0", "key") if "hlight::2.0" in hou.objNodeTypeCategory().nodeTypes() else None + if light is not None: + light.parmTuple("t").set((3, 5, 4)) + return geo + + +# --- inspect --------------------------------------------------------------------------------------------------- +@test +def inspect_type(): + r = program("inspect.py", {"type": "Sop/box"}) + EVIDENCE["inspect_type"] = r + t = r["type"] + check(r["houdini"]["version"] == hou.applicationVersionString(), "version") + check(t["type"] == "Sop/box" and t["inputs"] == {"min": 0, "max": 1}, t) + check(t["doc"] == "https://www.sidefx.com/docs/houdini%d.%d/nodes/sop/box.html" % hou.applicationVersion()[:2], t["doc"]) + size = next(p for p in t["parm_templates"] if p["name"] == "size") + check(size["parms"] == ["sizex", "sizey", "sizez"], size) + kind = next(p for p in t["parm_templates"] if p["name"] == "type") + check(isinstance(kind["menu"], list) and kind["menu"], kind) + r = program("inspect.py", {"type": "boxx"}) + check("error" in r["type"] and any(c.endswith("/box") for c in r["type"]["closest"]), r["type"]) + r = program("inspect.py", {"type": "box", "category": "Sop", "match": "size"}) + check([p["name"] for p in r["type"]["parm_templates"]] == ["size"], r["type"]["parm_templates"]) + labs = [n for n in hou.sopNodeTypeCategory().nodeTypes() if n.startswith("labs::")] + if labs: + r = program("inspect.py", {"type": "Sop/" + labs[0]}) + EVIDENCE["inspect_hda"] = {k: v for k, v in r["type"].items() if k != "parm_templates"} + check(r["type"]["hda"]["library"].lower().endswith(".hda"), r["type"].get("hda")) + check("labs--" in r["type"]["doc"], r["type"]["doc"]) + + +@test +def inspect_node(): + sop_scene() + r = program("inspect.py", {"node": "/obj/geo1/mask"}) + EVIDENCE["inspect_node_sop"] = r + n = r["node"] + check(n["geometry"]["points"] == 8 and n["geometry"]["prims"] == 6, n["geometry"]) + check(any(a.startswith("mask:") for a in n["geometry"]["attributes"]["point"]), n["geometry"]["attributes"]) + check(n["geometry"]["bbox"]["size"] == [2.0, 1.0, 1.0], n["geometry"]["bbox"]) + check(any(p["name"] == "snippet" for p in n["parms_changed"]), n["parms_changed"]) + check(n["inputs"][0]["from"] == "/obj/geo1/box1", n["inputs"]) + hou.node("/obj/geo1/broken").cook(force=False) if False else None + r = program("inspect.py", {"node": "/obj/geo1/broken"}) + check(r["node"].get("errors"), r["node"]) + r = program("inspect.py", {"node": "/obj/geo1"}) + check(r["node"]["display_node"] == "/obj/geo1/mask" and len(r["node"]["children"]) == 3, r["node"]) + stage = hou.node("/stage") + cube = stage.createNode("cube", "cube1") + r = program("inspect.py", {"node": cube.path()}) + EVIDENCE["inspect_node_lop"] = r + check(any(p["path"] == "/cube1" and p["type"] == "Cube" for p in r["node"]["stage"]["listed"]), r["node"]["stage"]) + r = program("inspect.py", {"node": "/obj/geo1/maks"}) + check("mask" in r["node"]["closest"], r["node"]) + + +@test +def inspect_hou_and_search(): + r = program("inspect.py", {"hou": "hou.OpNode.createNode"}) + check("createNode(self, node_type_name" in r["hou"]["doc"], r["hou"]["doc"][:200]) + check(r["hou"]["url"].endswith("hom/hou/OpNode.html#createNode"), r["hou"]["url"]) + r = program("inspect.py", {"hou": "hou.Node.parm"}) + check("hou.OpNode.parm" in r["hou"].get("defined_on", []), r["hou"]) + started = time.time() + r = program("inspect.py", {"search": "scatter", "limit": 15}) + first = time.time() - started + EVIDENCE["inspect_search"] = r + paths = [h["path"] for h in r["search"]["results"]] + check(any(p.startswith("Sop/scatter") for p in paths[:5]), paths) + r = program("inspect.py", {"search": "scatter", "category": "Lop", "limit": 5}) + check(all(h["path"].startswith("Lop/") for h in r["search"]["results"] if h["kind"] == "node type"), r["search"]["results"]) + started = time.time() + r = program("inspect.py", {"search": "sizex"}) + check(any(h["kind"] == "parm" and h["path"] == "sizex" for h in r["search"]["results"]), r["search"]["results"][:5]) + EVIDENCE["inspect_search_seconds"] = {"first": round(first, 2), "cached": round(time.time() - started, 2)} + r = program("inspect.py", {"search": "create node child"}) + check(any(h["path"] in ("hou.Node.createNode", "hou.OpNode.createNode") for h in r["search"]["results"]), [h["path"] for h in r["search"]["results"]]) + + +# --- hints ----------------------------------------------------------------------------------------------------- +@test +def hint_node_type(): + sop_scene() + hint, error = hint_of("geo = hou.node('/obj/geo1')\ngeo.createNode('boxx')\n") + EVIDENCE["hint_node_type"] = [error, hint] + check(hint and "box" in hint and "/obj/geo1" in hint, hint) + hint, _ = hint_of("hou.node('/obj').createNode('box')\n") + EVIDENCE["hint_wrong_category"] = hint + check(hint and "Sop" in hint and "geo" in hint, hint) + + +@test +def hint_parm(): + sop_scene() + hint, error = hint_of("box = hou.node('/obj/geo1/box1')\nbox.parm('sizexx').set(2)\n") + EVIDENCE["hint_parm"] = [error, hint] + check(hint and "sizex" in hint and "sizexx" in hint, hint) + hint, _ = hint_of("hou.node('/obj/geo1/box1').parm('size').set(2)\n") + EVIDENCE["hint_parm_tuple"] = hint + check(hint and "parmTuple('size')" in hint, hint) + hint, _ = hint_of("hou.node('/obj/geo1/boxx').parm('sizex').set(2)\n") + EVIDENCE["hint_missing_node"] = hint + check(hint and "box1" in hint, hint) + + +@test +def hint_cook_type_attribute(): + sop_scene() + hint, error = hint_of("hou.node('/obj/geo1/broken').cook(force=True)\n") + EVIDENCE["hint_cook"] = [error, hint] + check(hint and "undefined_fn" in hint, hint) + hint, error = hint_of("hou.node('/obj/geo1').createNode(12)\n") + EVIDENCE["hint_type_error"] = [error, hint] + check(hint and "createNode(" in hint, hint) + hint, error = hint_of("hou.node('/obj/geo1/box1').parmm('sizex')\n") + EVIDENCE["hint_attribute"] = [error, hint] + check(hint and "parm" in hint, hint) + hint, _ = hint_of("hou.nodee('/obj')\n") + check(hint and "node" in hint, hint) + + +# --- verify ---------------------------------------------------------------------------------------------------- +def fingerprint(): + nodes = hou.node("/").allSubChildren(recurse_in_locked_nodes=False) + return sorted("%s|%s" % (n.path(), n.type().nameWithCategory()) for n in nodes), hou.frame(), hou.hipFile.path() + + +def verify_scene(): + geo = sop_scene() + geo.node("box1").parm("ty").setExpression("$F") + bad = hou.node("/obj").createNode("geo", "bad") + broken = bad.createNode("attribwrangle", "broken") + broken.setInput(0, bad.createNode("sphere")) + broken.parm("snippet").set("@P.y += undefined_fn(1);") + broken.setDisplayFlag(True) + hou.node("/stage").createNode("cube", "cube1") + hou.setFrame(1) + + +CRITERIA = [ + {"expr": "npoints('/obj/geo1')", "op": "==", "value": 8, "label": "box points"}, + {"expr": "bbox('/obj/geo1/box1').size", "op": "approx", "value": [2, 1, 1], "tol": 1e-5}, + {"expr": "attrib_names('/obj/geo1')", "op": "contains", "value": "mask"}, + {"expr": "sum(attrib_values('/obj/geo1', 'mask'))", "op": "==", "value": 4}, + {"expr": "parm('/obj/geo1/box1/sizex')", "op": "==", "value": 2}, + {"expr": "parm('/obj/geo1/box1/size')", "op": "==", "value": [2, 1, 1]}, + {"expr": "bbox('/obj/geo1/box1').center[1]", "op": "approx", "value": 10, "frame": 10, "label": "ty follows $F at frame 10"}, + {"expr": "usd_prims('/stage/cube1', 'Cube')", "op": "contains", "value": "/cube1"}, + {"expr": "usd_attr('/stage/cube1', '/cube1', 'size')", "op": "approx", "value": 2}, + {"expr": "nprims('/obj/geo1')", "op": ">", "value": 100, "label": "deliberately failing"}, + {"expr": "npoints('/obj/nothing')", "op": "==", "value": 0, "label": "missing node"}, +] + + +@test +def verify_criteria_and_errors(): + verify_scene() + before = fingerprint() + r = program("verify.py", {"criteria": CRITERIA}) + EVIDENCE["verify"] = r + check(fingerprint() == before, "the scene or frame changed") + by = {c["label"]: c for c in r["criteria"]} + check(r["passed"] == len(CRITERIA) - 2 and r["failed"] == 2, [(c["label"], c["passed"], c.get("measured"), c.get("error")) for c in r["criteria"]]) + check(by["deliberately failing"]["measured"] == 6 and "no node" in by["missing node"]["error"], by) + check(by["ty follows $F at frame 10"]["passed"] and hou.frame() == 1, by["ty follows $F at frame 10"]) + broken = [e for e in r["errors"] if e["node"] == "/obj/bad/broken"] + check(broken and "undefined_fn" in broken[0]["errors"][0], r["errors"]) + check(r["houdini"]["license"] and r["facts"]["error_scan"]["nodes_with_errors"] >= 1, r["facts"]) + message = failing("verify.py", {"criteria": [{"expr": "hou.node('/obj').createNode('geo')", "op": "==", "value": 1}]}) + check("not a read" in message, message) + message = failing("verify.py", {"criteria": [{"expr": "hou.hipFile.save()", "op": "==", "value": 1}]}) + check("not a read" in message, message) + + +@test +def verify_render(): + verify_scene() + before = fingerprint() + started = time.time() + r = program("verify.py", {"errors": False, "render": {"size": 160}}) + karma_s = time.time() - started + EVIDENCE["verify_render_karma"] = dict(r, image_base64=len(r.get("image_base64") or "")) + check(fingerprint() == before, "the scene changed") + image = r["facts"]["render"]["image"] + check(r.get("image_base64") and image["size"][0] == 160 and not image["black"], r["facts"]["render"]) + started = time.time() + try: + r = program("verify.py", {"errors": False, "render": {"size": 160, "renderer": "opengl"}}) + EVIDENCE["verify_render_opengl"] = dict(r, image_base64=len(r.get("image_base64") or "")) + check(r.get("image_base64"), r["facts"]) + except Exception as exc: # noqa: BLE001 + EVIDENCE["verify_render_opengl"] = {"failed": str(exc)[:2000]} + EVIDENCE["verify_render_seconds"] = {"karma": round(karma_s, 1), "opengl": round(time.time() - started, 1)} + + +# --- save and verify saved ------------------------------------------------------------------------------------- +@test +def save_and_verify_saved(): + verify_scene() + message = failing("save.py", {}) + check("never saved" in message, message) + first = os.path.join(WORK, "shot.hipnc") + r = program("save.py", {"path": first}) + EVIDENCE["save"] = r + check(os.path.normcase(r["open_file"]) == os.path.normcase(first) and r["bytes"] > 1000, r) + copy = os.path.join(WORK, "copies", "shot_copy.hipnc") + r = program("save.py", {"path": copy, "copy": True}) + EVIDENCE["save_copy"] = r + check(os.path.isfile(copy) and os.path.normcase(r["open_file"]) == os.path.normcase(first), r) + message = failing("save.py", {"path": copy, "copy": True}) + check("overwrite" in message, message) + plain = os.path.join(WORK, "plain.hip") + r = program("save.py", {"path": plain, "copy": True}) + EVIDENCE["save_hip_extension"] = r + check(os.path.isfile(plain), r) + if hou.isApprentice(): + check("non-commercial" in r.get("license_note", ""), r) + saved_children = len(hou.node("/obj").children()) + r = program("save.py", {}) + check(os.path.normcase(r["saved"]) == os.path.normcase(first), r) + hou.node("/obj").createNode("geo", "unsaved") # in the open scene only + criteria = [{"expr": "len(node('/obj').children())", "op": "==", "value": saved_children, "label": "objects in the file"}, + {"expr": "npoints('/obj/geo1')", "op": "==", "value": 8}] + live = program("verify.py", {"criteria": criteria, "errors": False}) + check(live["failed"] == 1, live["criteria"]) + r = program("verify_saved.py", {"saved": True, "criteria": criteria}, job="verify.py") + EVIDENCE["verify_saved"] = r + check(r["all_passed"] and r["read"]["file"] and r["read"]["bytes"] > 1000, r) + check(any(e["node"] == "/obj/bad/broken" for e in r["errors"]), r["errors"]) + r2 = program("verify_saved.py", {"saved": copy, "criteria": criteria[:1], "render": {"size": 128}}, job="verify.py") + EVIDENCE["verify_saved_render"] = dict(r2, image_base64=len(r2.get("image_base64") or "")) + check(r2["all_passed"] and r2.get("image_base64"), r2) + EVIDENCE["verify_saved_seconds"] = {"criteria_and_errors": r["read"]["seconds"], "with_render": r2["read"]["seconds"]} + + +# --- render and PDG -------------------------------------------------------------------------------------------- +def wait_job(started, timeout=600): + """Follow the log as the plugin's task tracker does: markers, then the process's end.""" + deadline = time.time() + timeout + marks = [] + while time.time() < deadline: + with open(started["log"], encoding="utf-8", errors="replace") as fh: + lines = fh.read().splitlines() + marks = [l for l in lines if l.startswith("OPENCODE_")] + if any(l.startswith(("OPENCODE_DONE ", "OPENCODE_FAILED ")) for l in marks): + break + time.sleep(0.5) + parsed = [(l.split(" ", 1)[0][9:], json.loads(l.split(" ", 1)[1])) for l in marks] + return parsed + + +def render_scene(): + verify_scene() + hou.node("/obj/bad").destroy() + karma = hou.node("/out").createNode("karma", "karma1") + karma.parm("camera").set("/obj/cam1") + karma.parm("samplesperpixel").set(2) + gl = hou.node("/out").createNode("opengl", "gl1") + gl.parm("camera").set("/obj/cam1") + top = hou.node("/obj").createNode("topnet", "tasks") + wedge = top.createNode("wedge", "wedge1") + wedge.parm("wedgecount").set(3) + script = top.createNode("pythonscript", "write") + script.setInput(0, wedge) + folder = os.path.join(WORK, "pdg").replace("\\", "/") + script.parm("script").set( + "import os\nos.makedirs(%r, exist_ok=True)\npath = %r + '/item_%%d.txt' %% work_item.index\n" + "open(path, 'w').write('ok')\nwork_item.addResultData(path, 'file/text', 0)\n" % (folder, folder)) + script.setDisplayFlag(True) + hou.hipFile.save(os.path.join(WORK, "render_scene.hipnc").replace("\\", "/")) + return folder + + +@test +def render_karma_frames(): + render_scene() + name = hou.hipFile.path() + output = os.path.join(WORK, "out", "shot_####.png") + started = time.time() + r = program("render.py", {"rop": "/out/karma1", "output": output, "frames": [1, 5], "size": [160, 120]}, job="render_job.py") + returned = time.time() - started + EVIDENCE["render_karma_start"] = r + check(hou.hipFile.path() == name, "the open file name changed") + check(r["kind"] == "render" and len(r["targets"]) == 5 and r["targets"][0].endswith("shot_0001.png"), r) + marks = wait_job(r) + EVIDENCE["render_karma_marks"] = marks + EVIDENCE["render_karma_seconds"] = {"returned": round(returned, 2), "finished": round(time.time() - started, 1)} + kinds = [k for k, _ in marks] + check(kinds[0] == "RENDER" and kinds[-1] == "DONE", marks) + check([v["done"] for k, v in marks if k == "PROGRESS"][-1] == 5, marks) + check(all(os.path.isfile(t) for t in r["targets"]), r["targets"]) + from PIL import Image + check(Image.open(r["targets"][0]).size == (160, 120), "size not applied") + message = failing("render.py", {"rop": "/out/karma1", "output": output, "frames": [1, 3]}, job="render_job.py") + check("overwrite" in message, message) + + +@test +def render_apprentice_limits(): + """What the license really allows: a 4K Karma still, its written size, and the watermark corner.""" + render_scene() + output = os.path.join(WORK, "big", "big.png") + r = program("render.py", {"rop": "/out/karma1", "output": output, "frames": 1, "size": [3840, 2160]}, job="render_job.py") + marks = wait_job(r) + check(marks[-1][0] == "DONE", marks) + if hou.isApprentice(): + check(marks[-1][1].get("size_note") and marks[-1][1]["image_size"] == [1920, 1080], marks[-1]) + from PIL import Image + image = Image.open(output).convert("RGB") + w, h = image.size + px = image.load() + lit = [(x, y) for y in range(int(h * 0.8), h, 2) for x in range(int(w * 0.6), w, 2) if sum(px[x, y]) > 60] + EVIDENCE["apprentice_limits"] = {"license": str(hou.licenseCategory()), "asked": [3840, 2160], "written": [w, h], + "corner_lit_box": [min(p[0] for p in lit), min(p[1] for p in lit), max(p[0] for p in lit), max(p[1] for p in lit)] if lit else None} + + +@test +def render_opengl(): + render_scene() + output = os.path.join(WORK, "gl", "gl_####.png") + r = program("render.py", {"rop": "/out/gl1", "output": output, "frames": [1, 2], "size": [160, 120]}, job="render_job.py") + marks = wait_job(r) + EVIDENCE["render_opengl_marks"] = marks + check(marks and marks[-1][0] in ("DONE", "FAILED"), marks) + EVIDENCE["render_opengl_files"] = [os.path.isfile(t) for t in r["targets"]] + + +@test +def render_pdg(): + folder = render_scene() + r = program("render.py", {"top": "/obj/tasks"}, job="render_job.py") + EVIDENCE["render_pdg_start"] = r + check(r["kind"] == "pdg" and r["node"] == "/obj/tasks/write", r) + marks = wait_job(r) + EVIDENCE["render_pdg_marks"] = marks + check(marks[-1][0] == "DONE", marks) + check(sorted(os.listdir(folder)) == ["item_0.txt", "item_1.txt", "item_2.txt"], os.listdir(folder) if os.path.isdir(folder) else folder) + message = failing("render.py", {"rop": "/obj/geo1"}, job="render_job.py") + check("not a ROP" in message, message) + + +# --- the installed part itself: Host, its listener and execute_code over the wire, in a real Houdini ----------- + +def served(): + """A Host and its listener (never offered: OPENCODE_HOUDINI_OFFER is not involved, nothing is advertised).""" + from opencode_houdini.host import Host, HostServer # noqa: PLC0415 + + reset_scene() + host = Host() + return host, HostServer(host) + + +def ask(server, method, params): + """One request over the wire, as the plugin sends it (from any thread).""" + import socket # noqa: PLC0415 + + with socket.create_connection(("127.0.0.1", server.port), timeout=120) as client: + request = {"id": "t", "token": server.token, "method": method, "params": params} + client.sendall((json.dumps(request) + "\n").encode()) + with client.makefile("rb") as response: + return json.loads(response.readline()) + + +def call(server, method, params): + """A request from another thread while this, the main thread, serves it (hython's pump).""" + import threading # noqa: PLC0415 + + reply = [] + worker = threading.Thread(target=lambda: reply.append(ask(server, method, params))) + worker.start() + server.host.ui.pump(120, stop=lambda: not worker.is_alive()) + worker.join() + return reply[0] + + +@test +def host_execute_code_undo_and_hints(): + host, server = served() + try: + handle = call(server, "hello", {})["result"]["script_handle"] + hello = call(server, "hello", {})["result"] + check(hello["process"]["pid"] == os.getpid() and hello["process"]["temp_dir"], hello["process"]) + check({"execute_code", "status", "release"} <= set(hello["capabilities"]["methods"]), hello["capabilities"]) + ok = call(server, "execute_code", {"script_handle": handle, "code": + "geo = hou.node('/obj').createNode('geo', 'kept')\ngeo.createNode('box')\nresult = {'path': geo.path()}"})["result"] + EVIDENCE["host_execute_ok"] = ok + check(ok["status"] == "executed" and ok["changed"] and ok["undo_step"] == "OpenCode: code", ok) + # A run that changes the scene, then fails: undone, the graph as it was. + failed = call(server, "execute_code", {"script_handle": handle, "code": + "g = hou.node('/obj').createNode('geo', 'lost')\ng.createNode('sphere')\nraise ValueError('late')"})["result"] + EVIDENCE["host_execute_failed"] = failed + check(failed["status"] == "failed" and failed["restored"] is True, failed) + check(hou.node("/obj/lost") is None and hou.node("/obj/kept") is not None, [n.name() for n in hou.node("/obj").children()]) + check(not host.state.quarantined, host.state.quarantine) + # Houdini's own knowledge in the failure. + typo = call(server, "execute_code", {"script_handle": handle, "code": "hou.node('/obj/kept').createNode('boxx')"})["result"] + EVIDENCE["host_hint_type"] = typo.get("hint") + check("closest box" in (typo.get("hint") or ""), typo) + parm = call(server, "execute_code", {"script_handle": handle, "code": "hou.node('/obj/kept/box1').parm('sizexx').set(2)"})["result"] + EVIDENCE["host_hint_parm"] = parm.get("hint") + check("sizex" in (parm.get("hint") or ""), parm) + # A read-only program run (inspect) changes nothing and asks for no undo group. + text = "import json\nPARAMS = json.loads(%s)\n%s" % (json.dumps(json.dumps({"node": "/obj/kept/box1"})), source("inspect.py")) + inspected = call(server, "execute_code", {"script_handle": handle, "code": text, "read_only": True})["result"] + check(inspected["status"] == "executed" and inspected["changed"] is False and "undo_step" not in inspected, {k: v for k, v in inspected.items() if k != "result"}) + finally: + server.close() + host.close() + + +@test +def host_status_off_the_main_thread_and_copies_are_not_saves(): + import threading # noqa: PLC0415 + + host, server = served() + try: + handle = call(server, "hello", {})["result"]["script_handle"] + box = os.path.join(WORK, "status.hipnc") + call(server, "execute_code", {"script_handle": handle, "code": + "hou.node('/obj').createNode('geo').createNode('box')\nhou.hipFile.save(%r)" % box}) + handle = call(server, "hello", {})["result"]["script_handle"] + done, seen = [], [] + worker = threading.Thread(target=lambda: done.append(ask(server, "execute_code", { + "script_handle": handle, "code": "import time\ntime.sleep(2)", "read_only": True}))) + asker = threading.Thread(target=lambda: (time.sleep(0.6), seen.append(ask(server, "status", {})["result"]))) + worker.start() + asker.start() + host.ui.pump(10, stop=lambda: not worker.is_alive() and not asker.is_alive()) + status = seen[0] + EVIDENCE["host_status"] = status + check(status["busy"] is True and status["running"] == "execute_code", status) + check(done and done[0]["result"]["status"] == "executed", done) + # The save program's copy (mwrite -n) fires Houdini's save events: neither a save nor a change. + handle = call(server, "hello", {})["result"]["script_handle"] + copy = os.path.join(WORK, "status_copy.hipnc") + text = "import json\nPARAMS = json.loads(%s)\n%s" % (json.dumps(json.dumps({"path": copy, "copy": True})), source("save.py")) + copied = call(server, "execute_code", {"script_handle": handle, "code": text, "read_only": True})["result"] + EVIDENCE["host_copy"] = {k: v for k, v in copied.items() if k not in ("stdout", "stderr")} + check(copied["status"] == "executed" and os.path.isfile(copy), copied) + check("saved" not in copied and copied["changed"] is False and "warnings" not in copied, copied) + # A real save of the open file is one. + saved = call(server, "execute_code", {"script_handle": handle, "code": "import time\ntime.sleep(1.1)\nhou.hipFile.save()"})["result"] + check(saved.get("saved") is True, saved) + finally: + server.close() + host.close() + + +def main(): + names = set(sys.argv[1:]) + started = time.time() + for fn in TESTS: + if names and fn.__name__ not in names: + continue + t = time.time() + try: + fn() + RESULTS.append((fn.__name__, "ok", round(time.time() - t, 1), "")) + except Exception: # noqa: BLE001 + RESULTS.append((fn.__name__, "FAIL", round(time.time() - t, 1), traceback.format_exc()[-3000:])) + for name, status, seconds, detail in RESULTS: + print("%-28s %-4s %6.1fs %s" % (name, status, seconds, ("\n" + detail) if detail else "")) + folder = os.environ.get("OPENCODE_HOUDINI_EVIDENCE") or ROOT + os.makedirs(folder, exist_ok=True) + with open(os.path.join(folder, "programs_hython.json"), "w", encoding="utf-8") as fh: + json.dump({"houdini": hou.applicationVersionString(), "license": str(hou.licenseCategory()), + "results": [r[:3] for r in RESULTS], "evidence": EVIDENCE}, fh, indent=1, default=repr) + failed = [r for r in RESULTS if r[1] != "ok"] + print("%d passed, %d failed in %.1f s; evidence in %s" % (len(RESULTS) - len(failed), len(failed), time.time() - started, folder)) + print("OPENCODE_TESTS " + json.dumps({"passed": len(RESULTS) - len(failed), "failed": len(failed)})) + shutil.rmtree(WORK, ignore_errors=True) + return 1 if failed else 0 + + +if __name__ == "__main__": + code = main() + sys.stdout.flush() + os._exit(code) # hython can hang on exit after renders; the results are written diff --git a/integrations/houdini/test/registry.test.ts b/integrations/houdini/test/registry.test.ts index 6272be1..68be2fd 100644 --- a/integrations/houdini/test/registry.test.ts +++ b/integrations/houdini/test/registry.test.ts @@ -71,10 +71,10 @@ it("a tool's error carries the host's own message and data", async () => { 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("context", { script_handle: fake.scriptHandle }), (error: Error) => { + await assert.rejects(plugin.tool("houdini_context"), (error: Error) => { assert.match( error.message, - /\(stale_revision\): The Houdini graph changed after the intent was prepared: \/obj\/geo1/, + /^The scene changed since your last read: call houdini_context and use its revision\. The Houdini graph changed after the intent was prepared: \/obj\/geo1 /, ) assert.match(error.message, /"reason":"revision_conflict","expected_revision":3,"current_revision":5/) return true diff --git a/package-lock.json b/package-lock.json index 76f84ae..d5b04d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -74,7 +74,7 @@ }, "integrations/houdini/plugin": { "name": "@neuralnomads/opencode-houdini", - "version": "0.2.1", + "version": "0.3.0", "license": "MIT", "devDependencies": { "@opencode/client": "2.2.5", diff --git a/packages/integration-kit/README.md b/packages/integration-kit/README.md index d097668..b2b6575 100644 --- a/packages/integration-kit/README.md +++ b/packages/integration-kit/README.md @@ -2,23 +2,28 @@ Shared core of the integration plugins: the plugin side of [`spec/README.md`](../../spec/README.md) (`spec: 0`, RPC contract 1). Each plugin bundles it into its build, so two integrations loaded in the same OpenCode daemon cannot diverge. It is not published to npm. -| Module | Role | -| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `types.ts` | Method manifest types (`methods/**.json`) | -| `method-registry.ts` | Loads the manifests of a plugin, sorted by name | -| `manifest-args.ts` | Validates and normalizes tool arguments against a manifest | -| `plugin-tools.ts` | Builds OpenCode V2 tools (`@opencode/plugin` 2.2) from manifests and registers them under the integration's namespace, honoring cancellation; executors return text or content parts (`ToolContent`, for attachments) | -| `coherence-check.ts` | Checks manifests, handlers and the tools mentioned by skills against each other | -| `marker.ts` | Marker types, the draft specification integer and ID helpers | -| `contract.ts` | RPC status contract 1: constants (lease, bounds), types, wire schemas (`rpcDefinition` registers them without `pattern`, which OpenCode 2.2.5 refuses), `advertise`/`withdraw` input validation, display bounds | -| `integration-rpc.ts` | `createIntegrationRpc`: handlers for `ctx.rpc.register`, `status` assembly, bounded `installHost`, payload-free event relay and lease sweeping | -| `offer-registry.ts` | Offer registry with injectable clock and hello transport; `sharedOfferRegistry(app, options)` keeps one per host contract on `globalThis[Symbol.for("neuralnomads..offers")]` (`.protocol-

.part-.app-`), so a newer plugin version loaded without restarting OpenCode judges hellos by its own range; `liveHellos()` also gives each live host's method table | -| `session-bindings.ts` | Per-Location exact native session snapshots, bounded lookups and synchronous registry/generation/revision fencing for reads, attachments and host access | -| `host-client.ts` | Loopback client of host wire protocol 1 (127.0.0.1 only, bounded, cancellable; errors never carry the endpoint, a refusal's message carries the host's text and its `error.data` object, also kept as `HostError.data`; `HostError.detail` keeps its explanation without path separators) | -| `hello.ts` | `hello` validation, canonical JSON and capability hash, compatibility with the marker's `host` block | -| `host-state.ts` | Derivation of `status.host.state`, WSL detection | -| `validators.ts`, `output.ts` | Small JSON helpers | -| `python/opencode_service.py` | Reference discovery client of headless hosts (standard library only): service registrations, the `global.config` Location with the `OPENCODE_CONFIG_DIR` fallback setting, `service-` targeting, capability hash. Host parts vendor it. | +| Module | Role | +| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `types.ts` | Method manifest types (`methods/**.json`) | +| `method-registry.ts` | Loads the manifests of a plugin, sorted by name | +| `manifest-args.ts` | Validates and normalizes tool arguments against a manifest | +| `plugin-tools.ts` | Builds OpenCode V2 tools (`@opencode/plugin` 2.2) from manifests and registers them under the integration's namespace, honoring cancellation; executors return text or content parts (`ToolContent`, for attachments) | +| `coherence-check.ts` | Checks manifests, handlers and the tools mentioned by skills against each other | +| `marker.ts` | Marker types, the draft specification integer and ID helpers | +| `contract.ts` | RPC status contract 1: constants (lease, bounds), types, wire schemas (`rpcDefinition` registers them without `pattern`, which OpenCode 2.2.5 refuses), `advertise`/`withdraw` input validation, display bounds | +| `integration-rpc.ts` | `createIntegrationRpc`: handlers for `ctx.rpc.register`, `status` assembly, bounded `installHost`, payload-free event relay and lease sweeping | +| `offer-registry.ts` | Offer registry with injectable clock and hello transport; `sharedOfferRegistry(app, options)` keeps one per host contract on `globalThis[Symbol.for("neuralnomads..offers")]` (`.protocol-

.part-.app-`), so a newer plugin version loaded without restarting OpenCode judges hellos by its own range; `liveHellos()` also gives each live host's method table | +| `session-bindings.ts` | Per-Location exact native session snapshots, bounded lookups and synchronous registry/generation/revision fencing for reads, attachments and host access | +| `host-client.ts` | Loopback client of host wire protocol 1 (127.0.0.1 only, bounded, cancellable; errors never carry the endpoint, a refusal's message carries the host's text and its `error.data` object, also kept as `HostError.data`; `HostError.detail` keeps its explanation without path separators) | +| `hello.ts` | `hello` validation, canonical JSON and capability hash, compatibility with the marker's `host` block | +| `host-state.ts` | Derivation of `status.host.state`, WSL detection | +| `host-session.ts` | `createHostSession`: a native integration's conversations and their application, without knowing it: ownership, the attached offer (by the tool, restored, or automatically the only free compatible one), host calls that say what became of an application that crashed, closed, still runs a long call or is frozen (`status`), and the hosts tool; `hostErrorWith` turns a refusal into the integration's hint and the host's text. Names and texts come from the integration | +| `attachments.ts` | Attachments kept across an OpenCode restart and a document change, in the plugin's storage, per application (the offer key only as a digest) | +| `closed.ts` | `crashWatch`: the crash report an application wrote for its process (each integration says which file names are its), and the messages of a crashed, closed or silent application; `isAlive`, `readProcess` | +| `method.ts` | Attaches an integration's working method to a conversation once (a native instructions entry) | +| `scripts.ts` | `readScript`: an execute_code script file read byte for byte (at most 900 KiB) | +| `validators.ts`, `output.ts` | Small JSON helpers | +| `python/opencode_service.py` | Reference discovery client of headless hosts (standard library only): service registrations, the `global.config` Location with the `OPENCODE_CONFIG_DIR` fallback setting, `service-` targeting, capability hash. Host parts vendor it. | Skill loading lives in the Blender pilot (`integrations/blender/plugin/src/skills.ts`) until a second integration needs it. diff --git a/packages/integration-kit/src/attachments.ts b/packages/integration-kit/src/attachments.ts new file mode 100644 index 0000000..d71da7a --- /dev/null +++ b/packages/integration-kit/src/attachments.ts @@ -0,0 +1,228 @@ +// Attachments that survive an OpenCode restart. The offer registry lives in memory (HST-12): a restarted daemon +// forgets which conversation used which application, while the application keeps offering itself under the same +// offer key for its whole process life. Each Location's plugin keeps the attachments of its own conversations in +// its storage (`attachment/`, the offer key only as a SHA-256 digest, never a token, port or script +// handle) and, when the same application offers itself again (restarted OpenCode, another document under the +// same key), attaches the conversation back after the exact-session ownership check of RPC-12. A stored +// attachment lives while its application is offered and LEASE_MS after it stops; an explicit detach removes it. +// Restoring happens when the plugin loads and when offers change, never inside a read (RPC-13). + +import { createHash } from "node:crypto" + +import type { BindingAuthority } from "./contract.ts" +import type { OfferRegistry } from "./offer-registry.ts" + +export const LEASE_MS = 120_000 +const PERSIST_EVERY_MS = 30_000 +const PREFIX = "attachment/" +const PAGES = 100 +const SESSION_ID = /^[A-Za-z0-9_-]{1,128}$/ +const HOST = /^[0-9a-f]{64}$/ + +export type AttachmentStorage = { + get(key: string): Promise + set(key: string, value: never): Promise + remove(key: string): Promise + scan(options: { prefix: string; after?: string; limit?: number }): Promise<{ + entries: readonly { key: string; value: unknown }[] + next?: string + }> +} + +type Stored = { session: string; host: string; label: string; since: number; seen: number } + +// Per daemon process and application, per registry: the digest of each live offer's key, and the conversations +// attachments were recorded for (all Locations), so that a list can say which conversation holds which offer. +type Shared = { + hosts: WeakMap> + sessions: Set + advertising: Promise +} + +function sharedFor(app: string): Shared { + const store = globalThis as unknown as Record + const key = Symbol.for(`neuralnomads.${app}.attachments`) + store[key] ??= { hosts: new WeakMap(), sessions: new Set(), advertising: Promise.resolve() } + return store[key] +} + +export function hostDigest(offerKey: string, app: string) { + return createHash("sha256").update(`neuralnomads.${app}.offer\0${offerKey}`).digest("hex") +} + +function digests(shared: Shared, registry: OfferRegistry) { + let map = shared.hosts.get(registry) + if (!map) { + map = new Map() + shared.hosts.set(registry, map) + } + return map +} + +function valid(value: unknown): value is Stored { + const entry = value as Stored + return ( + typeof entry === "object" && + entry !== null && + typeof entry.session === "string" && + SESSION_ID.test(entry.session) && + typeof entry.host === "string" && + HOST.test(entry.host) && + typeof entry.label === "string" && + entry.label.length <= 120 && + Number.isFinite(entry.since) + ) +} + +// The advertise handler, serialized per daemon process: the offer an accepted announcement created is the one +// that appeared during the call, so its key's digest names it (the registry keeps keys private). +export function trackAdvertise( + registry: OfferRegistry, + advertise: (input: unknown) => Promise, + app: string, +): (input: unknown) => Promise { + const shared = sharedFor(app) + return (input) => { + const run = shared.advertising.then(async () => { + const before = new Set(registry.hosts().offers.map((offer) => offer.offerID)) + const result = (await advertise(input)) as { outcome?: string } + const key = (input as { offer_key?: unknown } | null)?.offer_key + if (result?.outcome === "accepted" && typeof key === "string") { + const created = registry.hosts().offers.filter((offer) => !before.has(offer.offerID)) + if (created.length === 1 && created[0]) digests(shared, registry).set(hostDigest(key, app), created[0].offerID) + } + return result + }) + shared.advertising = run.catch(() => {}) + return run + } +} + +export type Attachments = ReturnType + +export function createAttachments(options: { + app: string + registry: OfferRegistry + storage?: AttachmentStorage + now?: () => number + // A fresh ownership snapshot of the session at this Location (null when it is not verifiably ours). + owns: (sessionID: string) => Promise +}) { + const { registry, storage } = options + const shared = sharedFor(options.app) + const now = options.now ?? Date.now + const records = new Map() + const persisted = new Map() + + const live = () => new Map(registry.hosts().offers.map((offer) => [offer.offerID, offer])) + const offerOf = (host: string) => digests(shared, registry).get(host) + const hostOf = (offerID: string) => [...digests(shared, registry)].find(([, id]) => id === offerID)?.[0] + + // Storage failures never fail an application call: the attachment then lasts until OpenCode stops. + const write = async (record: Stored) => { + persisted.set(record.session, now()) + try { + await storage?.set(PREFIX + record.session, record as never) + } catch {} + } + + const forget = async (sessionID: string) => { + if (!records.delete(sessionID)) return + persisted.delete(sessionID) + try { + await storage?.remove(PREFIX + sessionID) + } catch {} + } + + return { + // The stored attachments, once; their lease starts again now. + async load() { + if (!storage) return + let after: string | undefined + for (let page = 0; page < PAGES; page += 1) { + let result: Awaited> | undefined + try { + result = await storage.scan({ prefix: PREFIX, ...(after ? { after } : {}), limit: 100 }) + } catch {} + const entries = Array.isArray(result?.entries) ? result.entries : [] + for (const entry of entries) { + if (!valid(entry?.value) || entry.key !== PREFIX + entry.value.session) continue + records.set(entry.value.session, { ...entry.value, seen: now() }) + shared.sessions.add(entry.value.session) + } + if (typeof result?.next !== "string" || entries.length === 0) break + after = result.next + } + }, + + // The conversation is attached now (tool, automatic or RPC attachment): keep it. + async remember(sessionID: string) { + const bound = registry.binding(sessionID) + const host = bound && hostOf(bound.offerID) + const offer = bound && live().get(bound.offerID) + if (!bound || !host || !offer) return + shared.sessions.add(sessionID) + const previous = records.get(sessionID) + const record = { + session: sessionID, + host, + label: offer.label, + since: previous?.host === host ? previous.since : now(), + seen: now(), + } + records.set(sessionID, record) + await write(record) + }, + + forget, + + // Attaches back each stored conversation (or only `sessionID`) whose application is offered again. + async restore(sessionID?: string) { + const offers = live() + for (const record of [...records.values()]) { + if (sessionID !== undefined && record.session !== sessionID) continue + if (registry.binding(record.session)) continue + const offerID = offerOf(record.host) + if (!offerID || offers.get(offerID)?.protocol !== "ok") continue + const authority = await options.owns(record.session) + // Synchronous after the snapshot (RPC-12): the same registry state, still stored (a detach may have + // forgotten it meanwhile), still no binding, the offer still live. + if (!authority || !registry.current(authority) || records.get(record.session) !== record) continue + if (registry.binding(record.session)) continue + if (!live().has(offerID)) continue + if (registry.attach(offerID, record.session).outcome === "attached") record.seen = now() + } + }, + + // Renews the lease of the stored attachments whose application is offered; forgets those gone longer than it. + async sweep() { + const offers = live() + for (const record of [...records.values()]) { + const offerID = offerOf(record.host) + const offer = offerID ? offers.get(offerID) : undefined + if (offer) { + record.seen = now() + record.label = offer.label + if (now() - (persisted.get(record.session) ?? 0) > PERSIST_EVERY_MS) await write(record) + } else if (now() - record.seen > LEASE_MS) await forget(record.session) + } + }, + + // Conversations other than `except` attached to the offer (those this daemon recorded, all Locations). + heldBy(offerID: string, except?: string) { + return [...shared.sessions].some( + (session) => session !== except && registry.binding(session)?.offerID === offerID, + ) + }, + + has(sessionID: string) { + return records.has(sessionID) + }, + } +} + +export function resetAttachmentsForTests(app: string) { + const shared = sharedFor(app) + shared.sessions.clear() + shared.advertising = Promise.resolve() +} diff --git a/packages/integration-kit/src/closed.ts b/packages/integration-kit/src/closed.ts new file mode 100644 index 0000000..cd9c489 --- /dev/null +++ b/packages/integration-kit/src/closed.ts @@ -0,0 +1,101 @@ +// What became of an application that stopped answering a conversation: crashed (it wrote its crash report; on +// Windows its process may stay open behind a crash window), closed (its process gone, no report: closed, or ended +// by another program), or alive without renewing its offer. The application part's hello names its process and +// the folder its crash reports go to; each integration says which report names are this process's. + +import { readdirSync, statSync } from "node:fs" +import { join } from "node:path" + +export type HostProcess = { + pid: number + tempDir: string + label?: string + since: number + offerID?: string + document?: string +} + +export function isAlive(pid: number) { + try { + process.kill(pid, 0) + return true + } catch (error) { + return (error as NodeJS.ErrnoException).code === "EPERM" + } +} + +export function readProcess(hello: unknown, label?: string, now = Date.now()): HostProcess | null { + const read = hello as { process?: { pid?: unknown; temp_dir?: unknown }; document?: unknown } | null + const found = read?.process + if (!found || !Number.isInteger(found.pid) || (found.pid as number) <= 0) return null + if (typeof found.temp_dir !== "string" || !found.temp_dir) return null + const document = typeof read?.document === "string" && read.document ? read.document : undefined + return { pid: found.pid as number, tempDir: found.temp_dir, label, since: now, document } +} + +export type CrashWatchOptions = { + // "Blender": the application as messages name it. + appName: string + // The tool prefix ("blender": blender_hosts). + app: string + // The application part's name ("OpenCode Link"). + part: string + // Whether a file name in the crash folder is this process's crash report. + isReport: (name: string, host: HostProcess) => boolean + // What a report says of the unsaved changes, when not that they are lost (a scene saved at the crash). + crashNote?: (report: string) => string | undefined +} + +export function crashWatch(options: CrashWatchOptions) { + const { appName, app, part } = options + const again = `Open ${appName} again and attach it (${app}_hosts).` + + // This process's crash report when written since `after` (by default since the conversation first reached it). + const crashReport = (host: HostProcess, after = host.since): string | null => { + let names: string[] + try { + names = readdirSync(host.tempDir) + } catch { + return null + } + for (const name of names) { + if (!options.isReport(name, host)) continue + const path = join(host.tempDir, name) + try { + if (statSync(path).mtimeMs >= after - 1_000) return path + } catch { + // removed meanwhile + } + } + return null + } + + // Crashed or closed; null while its process lives without a new crash report. + const closedMessage = ( + host: HostProcess, + alive: (pid: number) => boolean = isAlive, + after = host.since, + ): string | null => { + const name = host.label ? ` (${host.label})` : "" + const report = crashReport(host, after) + const living = alive(host.pid) + if (report) + return `${appName} crashed${name}: crash report ${report}. ${ + living + ? `Its process ${host.pid} is still open (${appName}'s crash window) and answers nothing.` + : `Its process ${host.pid} has ended.` + } ${options.crashNote?.(report) ?? "Changes after its last save are lost."} ${again}` + if (living) return null + return `${appName} closed${name}: its process ${host.pid} has ended, with no crash report in ${host.tempDir} (closed, or ended by another program). ${again}` + } + + // Its offer is gone while its process lives without a crash report: the application part stopped renewing it. + const silentMessage = (host: HostProcess) => { + const name = host.label ? ` (${host.label})` : "" + return `${appName}${name} no longer offers itself: its process ${host.pid} still runs but stopped renewing its offer (frozen, or ${part} was disabled). ${app}_hosts lists the ${appName}s offered.` + } + + return { crashReport, closedMessage, silentMessage } +} + +export type CrashWatch = ReturnType diff --git a/packages/integration-kit/src/host-session.ts b/packages/integration-kit/src/host-session.ts new file mode 100644 index 0000000..631ba54 --- /dev/null +++ b/packages/integration-kit/src/host-session.ts @@ -0,0 +1,349 @@ +// A native integration's conversations and their application: ownership (RPC-12), the attached offer (attached +// by the tool, restored after an OpenCode restart, or automatically the only free compatible offer, FAM-5), host +// calls that say what became of an application that stopped answering (crashed, closed, still running a long +// call, frozen), and the hosts tool. Knows nothing of any application: names and texts come from the integration. + +import type { ToolContext } from "@opencode/plugin/promise/tool" + +import type { Attachments } from "./attachments.ts" +import { type CrashWatch, type HostProcess, isAlive, readProcess } from "./closed.ts" +import { type BindingAuthority, HELLO_TIMEOUT_MS } from "./contract.ts" +import { HostError, requestHost } from "./host-client.ts" +import type { HostBinding, OfferRegistry } from "./offer-registry.ts" +import type { SessionBindings } from "./session-bindings.ts" + +// The status method answers off the application's main thread: no answer within this long means it is frozen. +export const STATUS_TIMEOUT_MS = 3_000 +// How often a running call looks for its application's crash report. +export const CRASH_POLL_MS = 2_000 +// Ownership snapshots taken again when other conversations or offers moved the shared registry meanwhile. +const OWNERSHIP_ATTEMPTS = 5 + +// label: the attached offer's label when the ticket was taken (a permission prompt shows it). +export type Ticket = { authority: BindingAuthority; bound: HostBinding; label?: string } + +export type HostSessionTexts = { + // "Blender": the application as messages name it; "blender": the tool prefix. + appName: string + app: string + // The application part ("OpenCode Link") and what it is ("add-on"). + part: string + partKind: string + // No offer at all: how to offer one. + noneOffered: string + // How to get a part with a missing method ("install OpenCode Link 0.4.8 (blender_install_host) and restart Blender"). + installHint: string +} + +export type HostSessionOptions = { + texts: HostSessionTexts + registry: OfferRegistry + bindings: SessionBindings + attachments: Attachments + // Attaches the working method to a conversation that uses the application. + attachMethod: (sessionID: string) => Promise + crash: CrashWatch + // Whether the daemon runs in WSL, where the application's process is on Windows (no process diagnosis). + wsl: () => boolean + requestTimeoutMs: number + // A host refusal as the agent reads it (the integration's hints). + hostError: (error: unknown) => Error +} + +export function createHostSession(options: HostSessionOptions) { + const { texts, registry, bindings, attachments, crash, wsl } = options + const { appName, app } = texts + + // Whether a conversation other than `sessionID` is attached to the offer: the conversations this daemon recorded + // (attachments); with one offer, also the registry's count of attached sessions, which are all on it. + const heldElsewhere = (offerID: string, sessionID: string) => { + if (attachments.heldBy(offerID, sessionID)) return true + const offers = registry.hosts().offers + if (offers.length !== 1 || offers[0]?.offerID !== offerID) return false + return registry.counts().attachedSessions - (registry.binding(sessionID)?.offerID === offerID ? 1 : 0) > 0 + } + + // This conversation's ownership, fenced by the registry's revision. That revision is shared by every offer and + // conversation: another conversation attaching another application, or an offer appearing, during the native + // lookup moves it. Only then is the snapshot taken again (an ownership that does not hold fails at once). + const ownership = async (sessionID: string, signal?: AbortSignal) => { + for (let attempt = 0; attempt < OWNERSHIP_ATTEMPTS; attempt += 1) { + const before = registry.authority() + const authority = await bindings.snapshot(sessionID, signal) + if (authority || signal?.aborted) return authority + const after = registry.authority() + const moved = + before && + after && + before.serviceGeneration === after.serviceGeneration && + (before.registryGeneration !== after.registryGeneration || before.revision !== after.revision) + if (!moved) return null + } + return null + } + + // The offer this conversation acts on, as the hosts and context tools name it. + const attachedTo = (sessionID: string) => { + const bound = registry.binding(sessionID) + const offer = bound && registry.hosts().offers.find((candidate) => candidate.offerID === bound.offerID) + return offer ? { offerID: offer.offerID, label: offer.label } : null + } + + // After a wait (a permission): this conversation still owned, still attached to the same offer. Unrelated + // changes of other offers and conversations do not count. + const reheld = async (context: ToolContext, held: Ticket): Promise => { + const authority = await ownership(context.sessionID, context.signal) + const bound = authority && registry.current(authority) ? registry.binding(context.sessionID) : null + const label = attachedTo(context.sessionID)?.label + return bound && + bound.offerID === held.bound.offerID && + bound.scriptHandle === held.bound.scriptHandle && + label === held.label + ? { authority: authority as BindingAuthority, bound, label } + : null + } + + // The process of the application each conversation last reached (its hello): a later call that cannot reach it + // says that it closed, and where its crash report is. Not from WSL, where the process is on Windows. + const reached = new Map() + const helloed = new Map() // sessionID -> the offer whose hello was read + // Its offer gone: crashed, closed, or alive without renewing its offer. Never another application in its place. + const goneFor = (sessionID: string) => { + const host = reached.get(sessionID) + if (!host || wsl() || registry.hosts().offers.some((offer) => offer.offerID === host.offerID)) return null + return crash.closedMessage(host) ?? crash.silentMessage(host) + } + // A call that failed, timed out or was refused as busy: what the application is doing. Its crash report + // (written since this conversation reached that process), the process gone, or the request its main thread runs + // now (the status method answers off the main thread). + const diagnose = async (sessionID: string, bound: HostBinding, code: string) => { + const host = reached.get(sessionID) + if (!host || wsl()) return null + const closed = crash.closedMessage(host, isAlive) + if (closed || code === "unreachable" || !bound.methods.includes("status")) return closed + try { + const status = (await requestHost(bound.endpoint, "status", {}, { timeoutMs: STATUS_TIMEOUT_MS })) as { + busy?: boolean + running?: string + seconds?: number + } + if (!status.busy) return null + return `${appName} is still running ${status.running ?? "a request"} (${status.seconds ?? "?"} s so far): it goes on in ${appName}, nothing is lost. ${app}_context answers once it has finished; read it before acting again.` + } catch { + return `${appName} does not answer: its process ${host.pid} runs, but ${texts.part} answered nothing within ${STATUS_TIMEOUT_MS / 1_000} s (frozen?).` + } + } + + const hello = async (bound: HostBinding, signal: AbortSignal) => + (await requestHost(bound.endpoint, "hello", {}, { signal, timeoutMs: HELLO_TIMEOUT_MS })) as { + script_handle: string + revision: number + host_version?: string + document?: unknown + } + + const remember = async (context: ToolContext, held: Ticket) => { + if (helloed.get(context.sessionID) === held.bound.offerID) return + helloed.set(context.sessionID, held.bound.offerID) + try { + const found = readProcess(await hello(held.bound, context.signal), held.label) + if (found) reached.set(context.sessionID, { ...found, offerID: held.bound.offerID }) + else reached.delete(context.sessionID) + } catch { + // a part without process in its hello, or a failed hello: the call itself reports + } + } + + const unverifiable = () => new Error("This conversation's ownership is unverifiable: try again.") + + // The conversation's attached application. Without one, the only offered one, compatible and attached to no + // conversation, attaches itself (spec FAM-5): checked against the ownership snapshot with no wait in between. + const ticket = async (context: ToolContext): Promise => { + const authority = await ownership(context.sessionID, context.signal) + if (!authority || context.signal.aborted || !registry.current(authority)) throw unverifiable() + let bound = registry.binding(context.sessionID) + // Its stored attachment first: the application it used before an OpenCode restart. + if (!bound && attachments.has(context.sessionID)) { + await attachments.restore(context.sessionID) + bound = registry.binding(context.sessionID) + } + if (!bound) { + // Its application crashed or closed: said, and no other one attaches itself in its place. + const gone = goneFor(context.sessionID) + if (gone) throw new Error(gone) + const offers = registry.hosts().offers + const only = offers.length === 1 ? offers[0] : undefined + if ( + only && + only.protocol === "ok" && + !heldElsewhere(only.offerID, context.sessionID) && + registry.current(authority) + ) { + if (registry.attach(only.offerID, context.sessionID).outcome === "attached") { + bound = registry.binding(context.sessionID) + await attachments.remember(context.sessionID) + } + } + if (!bound) { + if (!offers.length) throw new Error(texts.noneOffered) + const list = offers + .map( + (offer) => + `${offer.label} [${offer.offerID}]${heldElsewhere(offer.offerID, context.sessionID) ? " (attached to another conversation)" : ""}${offer.protocol === "incompatible" ? ` (incompatible ${texts.partKind})` : ""}`, + ) + .join(", ") + throw new Error( + `No ${appName} is attached to this conversation. Offered: ${list}. Attach one with ${app}_hosts attach.`, + ) + } + } + const current = registry.authority() + if (!current) throw unverifiable() + await options.attachMethod(context.sessionID) + return { authority: current, bound, label: attachedTo(context.sessionID)?.label } + } + + const callHost = async ( + context: ToolContext, + method: string, + params: Record, + timeoutMs = options.requestTimeoutMs, + captured?: Ticket, + ) => { + const held = captured ? await reheld(context, captured) : await ticket(context) + if (!held) throw new Error(`This conversation's ${appName} changed while permission was asked; retry the action.`) + if (context.signal.aborted || !registry.current(held.authority)) + throw new Error("Application attachment is unverifiable.") + const { bound } = held + if (!bound.methods.includes(method)) { + throw new Error(`The attached ${appName}'s ${texts.part} has no ${method} method: ${texts.installHint}.`) + } + await remember(context, held) + // A crash ends the wait at once: on Windows a crashed application may stay open behind its crash window, its + // connection with it, so the call would wait its whole timeout. Its crash report is watched meanwhile. + const host = wsl() ? undefined : reached.get(context.sessionID) + const stop = new AbortController() + const abort = () => stop.abort() + context.signal.addEventListener("abort", abort) + let crashed: string | null = null + const watch = host + ? setInterval(() => { + crashed = crash.crashReport(host) ? crash.closedMessage(host, isAlive) : null + if (crashed) stop.abort() + }, CRASH_POLL_MS) + : undefined + try { + return await requestHost( + bound.endpoint, + method, + { script_handle: bound.scriptHandle, ...params }, + { signal: stop.signal, timeoutMs }, + ) + } catch (error) { + if (crashed) throw new Error(crashed) + const said = + error instanceof HostError && ["unreachable", "timeout", "busy"].includes(error.code) + ? await diagnose(context.sessionID, bound, error.code) + : null + throw said ? new Error(said) : options.hostError(error) + } finally { + clearInterval(watch) + context.signal.removeEventListener("abort", abort) + } + } + + // The hosts tool: detach, attach (after `allow`, the attach permission), then the offers and which + // conversation holds each. `list` names the offers' key in the output ("blenders"). + const hosts = async ( + args: Record, + context: ToolContext, + tool: { + allow: (context: ToolContext, offer: { offerID: string; label: string }) => Promise + status: () => Promise<{ host?: unknown }> + list: string + }, + ) => { + const out: Record = {} + // Only a conversation of this Location sees and changes its attachment (RPC-12). + const owned = await ownership(context.sessionID, context.signal) + if (!owned) throw unverifiable() + if (args.detach === true) { + await attachments.forget(context.sessionID) // first: the detach's change event must not restore it + const authority = await ownership(context.sessionID, context.signal) + if (!authority || !registry.current(authority)) throw unverifiable() + out.detached = registry.detach(context.sessionID).outcome + reached.delete(context.sessionID) // detached on purpose: its application closing later is no news here + helloed.delete(context.sessionID) + } + if (typeof args.attach === "string") { + const offer = registry.hosts().offers.find((candidate) => candidate.offerID === args.attach) + if (!offer) throw new Error(`No offered ${appName} ${String(args.attach)}: call ${app}_hosts again.`) + await tool.allow(context, offer) + // After the permission: this conversation still owned, the chosen offer still the same application. + const authority = await ownership(context.sessionID, context.signal) + const still = registry.hosts().offers.find((candidate) => candidate.offerID === offer.offerID) + if (!authority || !registry.current(authority) || !still || still.label !== offer.label) + throw new Error(`This conversation or the chosen ${appName} changed while permission was asked: try again.`) + out.attached = registry.attach(offer.offerID, context.sessionID).outcome + if (out.attached === "attached") { + await attachments.remember(context.sessionID) + await options.attachMethod(context.sessionID) + if (heldElsewhere(offer.offerID, context.sessionID)) + out.shared = `Another conversation is attached to this ${appName} too: both change the same document, and each call is checked against its revision.` + } + } + const status = await tool.status() + const mine = registry.binding(context.sessionID)?.offerID + out.attached_to = attachedTo(context.sessionID) + out.host = status.host + out[tool.list] = registry.hosts().offers.map((offer) => ({ + ...offer, + attached: + offer.offerID === mine + ? "this conversation" + : heldElsewhere(offer.offerID, context.sessionID) + ? "another conversation" + : null, + })) + return out + } + + // Renews or forgets the stored attachments and attaches back the conversations whose application offers + // itself again: one run at a time, and once more when the offers changed during it. + let running: Promise | undefined + let again = false + const maintain = (): Promise => { + if (running) { + again = true + return running + } + running = (async () => { + do { + again = false + try { + await attachments.sweep() + await attachments.restore() + } catch {} + } while (again) + })().finally(() => { + running = undefined + }) + return running + } + + return { ownership, reheld, attachedTo, heldElsewhere, ticket, callHost, hello, hosts, maintain } +} + +export type HostSession = ReturnType + +// A host refusal as the agent reads it: the integration's hint for its code, then the host's own text. +export function hostErrorWith(hints: Record, appName: string, detail: (error: HostError) => string) { + return (error: unknown): Error => { + if (error instanceof HostError) { + const hint = hints[error.code] ?? `${appName} refused the request (${error.code}).` + const text = detail(error) + return new Error(text ? `${hint} ${text}` : hint) + } + return error instanceof Error ? error : new Error(String(error)) + } +} diff --git a/packages/integration-kit/src/index.ts b/packages/integration-kit/src/index.ts index c81c87d..72588d1 100644 --- a/packages/integration-kit/src/index.ts +++ b/packages/integration-kit/src/index.ts @@ -1,5 +1,15 @@ // Public entrypoint of the integration kit, bundled into each integration plugin. +export type { AttachmentStorage, Attachments } from "./attachments.ts" +export { + createAttachments, + hostDigest, + LEASE_MS as ATTACHMENT_LEASE_MS, + resetAttachmentsForTests, + trackAdvertise, +} from "./attachments.ts" +export type { CrashWatch, CrashWatchOptions, HostProcess } from "./closed.ts" +export { crashWatch, isAlive, readProcess } from "./closed.ts" export type { CoherenceCheckOptions, CoherenceReport } from "./coherence-check.ts" export { printCoherenceReport, runCoherenceCheck } from "./coherence-check.ts" export type { @@ -52,6 +62,8 @@ export { } from "./hello.ts" export type { HostEndpoint } from "./host-client.ts" export { HOST_ADDRESS, HostError, requestHost } from "./host-client.ts" +export type { HostSession, HostSessionOptions, HostSessionTexts, Ticket } from "./host-session.ts" +export { CRASH_POLL_MS, createHostSession, hostErrorWith, STATUS_TIMEOUT_MS } from "./host-session.ts" export type { HostInspection, HostStateInput, LiveHello, WslProbe } from "./host-state.ts" export { deriveHostState, hostUnsupported, isWslDaemon } from "./host-state.ts" export type { IntegrationRpcOptions, RpcEventSink } from "./integration-rpc.ts" @@ -66,6 +78,8 @@ export { pluginIdFor, SPEC_VERSION, } from "./marker.ts" +export type { EntryPut } from "./method.ts" +export { methodAttacher, resetMethodForTests } from "./method.ts" export type { MethodRegistry } from "./method-registry.ts" export { loadMethodRegistry } from "./method-registry.ts" export type { HelloProbe, HostBinding, OfferChange, OfferRegistry, OfferRegistryOptions } from "./offer-registry.ts" @@ -79,6 +93,7 @@ export { export { errorToMessage, toJson } from "./output.ts" export type { ManifestExecutor, ToolContent } from "./plugin-tools.ts" export { buildPluginTools, buildToolInput, registerPluginTools } from "./plugin-tools.ts" +export { readScript, SCRIPT_MAX_BYTES } from "./scripts.ts" export type { NativeSessionReader, SessionBindings } from "./session-bindings.ts" export { createSessionBindings } from "./session-bindings.ts" export type { diff --git a/packages/integration-kit/src/method.ts b/packages/integration-kit/src/method.ts new file mode 100644 index 0000000..ef5c943 --- /dev/null +++ b/packages/integration-kit/src/method.ts @@ -0,0 +1,30 @@ +// An integration's working method, attached once to a conversation when it attaches the application (a native +// instructions entry, not a skill). The text belongs to the integration; the kit only attaches it. + +export type EntryPut = (input: { sessionID: string; key: string; value: string }) => Promise + +// Sessions each method is attached to, per daemon process (OpenCode loads the plugin once per Location). +function attachedFor(key: string): Set { + const store = globalThis as unknown as Record | undefined> + const slot = Symbol.for(`neuralnomads.method.${key}`) + store[slot] ??= new Set() + return store[slot] +} + +// A failure never fails the application call: the method is tried again at the next one. +export function methodAttacher(put: EntryPut | undefined, method: { key: string; text: string }) { + const attached = attachedFor(method.key) + return async (sessionID: string) => { + if (!put || !sessionID || attached.has(sessionID)) return + attached.add(sessionID) + try { + await put({ sessionID, key: method.key, value: method.text }) + } catch { + attached.delete(sessionID) + } + } +} + +export function resetMethodForTests(key: string) { + attachedFor(key).clear() +} diff --git a/packages/integration-kit/src/scripts.ts b/packages/integration-kit/src/scripts.ts new file mode 100644 index 0000000..8d124ea --- /dev/null +++ b/packages/integration-kit/src/scripts.ts @@ -0,0 +1,25 @@ +// A script an agent asks to run from a file (an execute_code tool's `path`): read byte for byte, so its code never +// goes through a JSON or JavaScript string. + +import { readFileSync, statSync } from "node:fs" +import { resolve } from "node:path" + +// The longest script read from a file (900 KiB): hosts read requests of at most 1 MiB. A literal, so that a +// bundle importing the kit without reading scripts drops it. +export const SCRIPT_MAX_BYTES = 921_600 + +// A Python script on disk (absolute, or relative to the Location's directory), as UTF-8 text. +export function readScript(path: string, base?: string) { + const file = resolve(base ?? process.cwd(), path) + let bytes: Buffer + try { + if (statSync(file).size > SCRIPT_MAX_BYTES) + throw new Error(`${file} is over ${SCRIPT_MAX_BYTES} bytes: split it, or have it exec() its parts.`) + bytes = readFileSync(file) + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") throw new Error(`No script ${file}.`) + throw error + } + const code = new TextDecoder("utf-8", { fatal: true }).decode(bytes).replace(/^/, "") + return { path: file, code } +} diff --git a/packages/testing/src/fake-host.ts b/packages/testing/src/fake-host.ts index 2dbdef6..d24fca4 100644 --- a/packages/testing/src/fake-host.ts +++ b/packages/testing/src/fake-host.ts @@ -104,7 +104,16 @@ export async function startFakeHost(options: FakeHostOptions = {}): Promise) }) + const result = handler(params as Record) + // An asynchronous method (a long call, a crash simulated during it) answers when it settles. + if (result instanceof Promise) { + result.then( + (value) => reply({ id, ok: true, result: value }), + (error) => reply({ id, ok: false, error: { code: (error as { code?: string }).code ?? "internal" } }), + ) + return + } + return reply({ id, ok: true, result }) } catch (error) { // A thrown Error answers its code alone; a thrown plain { code, message?, data? } is the host's refusal, // answered as such. From 920abbd257f68bbf703cbce2f09eba749f2813d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Andr=C3=A9?= Date: Mon, 16 Nov 2026 07:06:56 +0100 Subject: [PATCH 3/4] fix(houdini): offers that hold during cooks, crashed renders said as crashes The Sol trial lost Houdini offers while another Houdini cooked or loaded, saw two offers with one label, and an OpenGL ROP render crashed both its background hython and, rendered again in the interface, the open Houdini. hello is now answered off Houdini's main thread from values read once at start (protocol, capabilities, process) and the current script state. OpenCode checks new offers one at a time with hello; a hello waiting behind a cook held every offer of the computer back, until their leases ran out and conversations were detached (the service logged 499s). The advertiser renews straight at the services that accepted the offer, one request each with 8 s for a busy service, discovers again after a failure and every 12 renewals, and retries 1 s after a failed renewal instead of 5 s (an offer lasts 15 s). houdini_hosts adds same_label when several offers share a label (two Houdinis with the same file open). Measured with Houdini 22.0.429: an OpenGL ROP with High Quality Lighting off crashes in its Vulkan renderer every time, in hython as in the interface. houdini_render says so in facts.crash_risk when it starts such a render (a warning, not a refusal); the task tracker reads a crashed hython's log (Fatal error / Caught signal and its first frames) and says "the background hython crashed (...); the open Houdini is not affected" with that note. The working method asks for long renders and cooks through houdini_render; the README's Limits say the same. Tests: hello answered while the main thread is busy, renewals straight to the service and rediscovery after a failure (host_probe), same_label (plugin.test), a crashed render log said as a crash (tasks.test), and the real hython crash and its warning (programs_hython). The first four fail on the previous commit. --- .../scripts/opencode_houdini/advertiser.py | 67 ++++++++++++++++-- .../host/scripts/opencode_houdini/host.py | 28 ++++++++ integrations/houdini/plugin/README.md | 1 + .../host/houdini/opencode_link-0.3.0.zip | Bin 258083 -> 262540 bytes integrations/houdini/plugin/package.json | 2 +- .../houdini/plugin/programs/render.py | 6 ++ .../houdini/plugin/src/integration.ts | 18 +++-- integrations/houdini/plugin/src/method.ts | 1 + integrations/houdini/plugin/src/tasks.ts | 25 ++++++- integrations/houdini/test/host_probe.py | 56 +++++++++++++++ integrations/houdini/test/panel_probe.py | 2 +- integrations/houdini/test/plugin.test.ts | 18 +++++ integrations/houdini/test/programs_hython.py | 26 +++++++ integrations/houdini/test/tasks.test.ts | 54 ++++++++++++++ 14 files changed, 288 insertions(+), 16 deletions(-) create mode 100644 integrations/houdini/test/tasks.test.ts diff --git a/integrations/houdini/host/scripts/opencode_houdini/advertiser.py b/integrations/houdini/host/scripts/opencode_houdini/advertiser.py index 8811ce0..929d238 100644 --- a/integrations/houdini/host/scripts/opencode_houdini/advertiser.py +++ b/integrations/houdini/host/scripts/opencode_houdini/advertiser.py @@ -12,11 +12,17 @@ import re import secrets import threading +import time from typing import Any, Callable RPC_ID = "neuralnomads.houdini" PROTOCOL = 1 RENEW_SECONDS = 5.0 +# One renewal may wait this long for a busy service: an offer lasts 15 s, renewed every 5 s. +RENEW_TIMEOUT = 8.0 +# Renewals go straight to the services that accepted the offer; every this many renewals (or after a failure) +# the services are discovered again, which finds a service started since. +DISCOVER_EVERY = 12 def _launch_key() -> str | None: @@ -64,23 +70,70 @@ def advertise_once(self) -> list[dict[str, Any]] | None: "protocol": PROTOCOL, "offer_key": OFFER_KEY, } + results = self._renew(offer) + with self._lock: + self._results = results + return results + + def _renew(self, offer: dict[str, Any]) -> list[dict[str, Any]]: + """Renews at the services that took the offer (one request each); discovers them again on a failure and + every DISCOVER_EVERY renewals (a discovery reads each service's info and the plugin's status first).""" from . import opencode_service # noqa: PLC0415 - the kit's client, vendored into the payload + self._count = getattr(self, "_count", 0) + 1 + targets = getattr(self, "_targets", {}) + if targets and self._count % DISCOVER_EVERY: + results, kept = [], {} + for name, (service, directory) in targets.items(): + try: + status, body = opencode_service.call(service, RPC_ID, "advertise", offer, directory, RENEW_TIMEOUT) + outcome = opencode_service._outcome("advertise", status, body) + except (OSError, ValueError): + outcome = "error" + results.append({"service": name, "outcome": outcome}) + if outcome in ("accepted", "renewed"): + kept[name] = (service, directory) + self._targets = kept + if len(kept) == len(targets): + return results + found: dict[str, Any] = {} + results = [] try: - results = opencode_service.advertise_all(RPC_ID, offer, env=self._env) + services = opencode_service.discover(env=self._env, timeout=RENEW_TIMEOUT) except (OSError, ValueError): - results = [] - with self._lock: - self._results = results + services = [] + for service in services: + outcome = "unavailable" + for directory in opencode_service.location_candidates(None, self._env): + try: + deadline = time.monotonic() + RENEW_TIMEOUT + status, body = opencode_service._ready(service, RPC_ID, directory, deadline) + if opencode_service._unavailable(status, body): + continue + if status != 200: + outcome = "error" + break + status, body = opencode_service.call(service, RPC_ID, "advertise", offer, directory, + max(0.5, deadline - time.monotonic())) + outcome = opencode_service._outcome("advertise", status, body) + except (OSError, ValueError): + outcome = "error" + if outcome in ("accepted", "renewed"): + found[service.file] = (service, directory) + break + results.append({"service": service.file, "outcome": outcome}) + self._targets = found return results def _loop(self) -> None: while not self._stop.is_set(): try: - self.advertise_once() + results = self.advertise_once() or [] except Exception: # noqa: BLE001 - the next renewal tries again - pass - self._stop.wait(self._interval) + results = [] + taken = any(result.get("outcome") in ("accepted", "renewed") for result in results) + # A renewal that failed is tried again soon: the offer lasts 15 s from the last one taken. + self._stop.wait(self._interval if taken else min(self._interval, 1.0)) def stop(self, token: str | None, withdraw: bool = True) -> list[dict[str, Any]]: """Stops renewing; withdraws the offer from every service with the token it was announced with.""" diff --git a/integrations/houdini/host/scripts/opencode_houdini/host.py b/integrations/houdini/host/scripts/opencode_houdini/host.py index a4c8b1f..aaa8102 100644 --- a/integrations/houdini/host/scripts/opencode_houdini/host.py +++ b/integrations/houdini/host/scripts/opencode_houdini/host.py @@ -1279,8 +1279,31 @@ def __init__(self) -> None: self.saves = 0 self.loads = 0 self.label = offer_label(hou.hipFile.basename()) + self.document = hou.hipFile.basename()[:120] + # What hello says that does not change while Houdini runs, read once here on the main thread. + self._hello = { + "protocol": PROTOCOL_VERSION, + "capability_hash": capability_hash(), + "host_version": host_version(), + "app_version": hou.applicationVersionString(), + "process": {"pid": os.getpid(), "temp_dir": temp_dir()}, + "capabilities": capabilities(), + "operations": list(OPERATIONS), + "node_types": {category: sorted(types) for category, types in NODE_TYPE_REGISTRY.items()}, + } hou.hipFile.addEventCallback(self._hip_event) + def quick_hello(self) -> dict[str, Any] | None: + """hello without Houdini's main thread: OpenCode checks each new offer with it, and a hello waiting behind a + long cook or a file being loaded would hold every offer of the computer back (they are checked one at a + time). The revision is the last one recorded.""" + static = getattr(self, "_hello", None) + if static is None: + return None + state = self.state + return dict(static, script_handle=state.handle, script_epoch=state.epoch, revision=state.revision, + document=self.document) + def close(self) -> None: try: hou.hipFile.removeEventCallback(self._hip_event) @@ -1299,6 +1322,10 @@ def dispatch(self, method: str, params: dict[str, Any]) -> Any: if method == "status": # Answered on the request's own thread, never through the main thread: what it runs now. return self.ui.running() + if method == "hello": + quick = self.quick_hello() + if quick is not None: + return quick if method in ("cancel", "resume"): state = self._require_state(params) session_id = params.get("session_id") @@ -1358,6 +1385,7 @@ def _hip_event(self, event_type: hou.hipFileEventType) -> None: # On the main thread: the offer's label follows the open file (the advertiser reads it from its thread). try: self.label = offer_label(hou.hipFile.basename()) + self.document = hou.hipFile.basename()[:120] except Exception: # noqa: BLE001 pass diff --git a/integrations/houdini/plugin/README.md b/integrations/houdini/plugin/README.md index 4af3c7a..22fe2e5 100644 --- a/integrations/houdini/plugin/README.md +++ b/integrations/houdini/plugin/README.md @@ -46,6 +46,7 @@ The **OpenCode Link** Python panel is a conversation inside Houdini. **Connect** ## Limits - Houdini Apprentice: files are non-commercial, renders are at most 1920×1080 and carry the Houdini logo; render results say so. +- With Houdini 22.0.429, an OpenGL ROP with High Quality Lighting off (`hqlighting` 0) crashes in Houdini's Vulkan renderer, in `hython` as in the interface: `houdini_render` says so when it starts such a render and when its background `hython` crashed. Rendered with `houdini_render`, a crash never closes the open Houdini; `.render()` in `houdini_execute_code` runs in it. - `houdini_apply` refuses changes in `hython` (no interface); `houdini_execute_code` works in both. Houdini records undo only on its main thread: OpenCode Link runs every request there (in `hython`, a script serves them with `opencode_houdini.pump()`). - `hou.hipFile.hasUnsavedChanges()` stays true after a save in `hython`: OpenCode Link judges a save by the file's date on disk. diff --git a/integrations/houdini/plugin/host/houdini/opencode_link-0.3.0.zip b/integrations/houdini/plugin/host/houdini/opencode_link-0.3.0.zip index 961d77383789d489dd601e4419a152fae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f5RMEUYVprsf>IK3?QsFdA+&J=A*KLOp_nUCvR?2T*Ef`tY-T3UzUu5 zlW!YyO}?rtD4m#6mReMjS)3Z5n3JOs6yO~1>7t{MpO%(dG}+zUb@E+f;mN@oVUzzD z#Y`?Z$2vLPSYW!5KcnpAghr9c>x^}$J3M4$onGV2$T9ua1xB99hfTybnVz=ejXZwf zp3ENx1`t*NIz)E!*W#2%(^p0_@=TZA#;7!%JCc!edv!EpG}HE|IL3JH$+KPc+7+@G zftYE#LKgGyDO?E8OjrHSoWgWjd-4Wz<>~AGGYiO|n9qO={-gs{$xQ$DpV@#ZEM@9N z7U}8VN*RTxhOuZd`SpVN@8&UbOkE1(f4BhT`!Sn*WSqW>iAj38EhCE-^NUZ6)3X>^ MWZ3-PFar$+0NmJkV*mgE diff --git a/integrations/houdini/plugin/package.json b/integrations/houdini/plugin/package.json index 2b47d3e..3d202d3 100644 --- a/integrations/houdini/plugin/package.json +++ b/integrations/houdini/plugin/package.json @@ -64,7 +64,7 @@ "range": "^0.3.0", "appVersions": ">=21.0", "payload": "host/houdini/opencode_link-0.3.0.zip", - "sha256": "ecb899429f413c946970039953142c62fd16c4bfe45aa6b4b413b6969188b4fd" + "sha256": "b0ea40242d2a349e31afeb16bb96f53db0a4ddd5b69ac988d50d410fed91511a" } } } diff --git a/integrations/houdini/plugin/programs/render.py b/integrations/houdini/plugin/programs/render.py index 89b849e..5a21ca2 100644 --- a/integrations/houdini/plugin/programs/render.py +++ b/integrations/houdini/plugin/programs/render.py @@ -106,6 +106,12 @@ def frame_list(spec): raise ValueError("%s already exists (%d file(s)): pass overwrite true or name another output" % (taken[0], len(taken))) for t in targets[:1]: os.makedirs(os.path.dirname(t), exist_ok=True) + # Measured with Houdini 22.0.429: an OpenGL ROP with High Quality Lighting off crashes in its Vulkan renderer + # (RV_VKImage::getInfo or DM_RenderVulkan::compositePasses), in hython as in the interface. + hq = node.parm("hqlighting") if node.type().name() == "opengl" else None + if hq is not None and not hq.eval(): + facts["crash_risk"] = ("High Quality Lighting is off on this OpenGL ROP (hqlighting 0): with it off, Houdini " + "22.0.429's OpenGL ROP crashed in its Vulkan renderer every time; set hqlighting to 1") size = P.get("size") if size is not None and (not isinstance(size, list) or len(size) != 2 or not all(isinstance(v, int) and 4 <= v <= 16384 for v in size)): raise ValueError("size: [width, height], 4 to 16384") diff --git a/integrations/houdini/plugin/src/integration.ts b/integrations/houdini/plugin/src/integration.ts index 7b9307b..47a24a0 100644 --- a/integrations/houdini/plugin/src/integration.ts +++ b/integrations/houdini/plugin/src/integration.ts @@ -263,17 +263,25 @@ export async function setupHoudini(ctx: Plugin.Context, deps: HoudiniDeps = {}) list: "houdinis", }) const found = legacy() - return json( - found.length + // Two Houdinis with the same file open offer the same label: said, so that the right one is attached. + const labels = registry.hosts().offers.map((offer) => offer.label) + const twice = [...new Set(labels.filter((label, index) => labels.indexOf(label) !== index))] + return json({ + ...out, + ...(twice.length + ? { + same_label: `Several Houdinis have the same file open (${twice.join(", ")}): their offers differ by offerID only; attach the one you mean, or close the others.`, + } + : {}), + ...(found.length ? { - ...out, legacy_install: { found, fix: 'An earlier OpenCode package also loads in Houdini (another panel and host): remove it, or set "enable": false in it, then restart Houdini.', }, } - : out, - ) + : {}), + }) }, async houdini_install_host(_args, context) { diff --git a/integrations/houdini/plugin/src/method.ts b/integrations/houdini/plugin/src/method.ts index ee0813f..52c9855 100644 --- a/integrations/houdini/plugin/src/method.ts +++ b/integrations/houdini/plugin/src/method.ts @@ -10,6 +10,7 @@ export const METHOD_TEXT = [ "- Inspect the exact element before coding it (houdini_inspect search, type with its parms, node, hou symbol).", "- After an error, read its hint and inspect again; never guess twice. A parm that does not exist returns None.", "- Cook what you build and read its errors and warnings; a node in error is not done.", + "- Render and cook long jobs with houdini_render: a background hython runs them, so a renderer crash leaves this Houdini open.", "- Turn each requirement of your task into a measured check (houdini_verify criteria).", "- The saved file is what you deliver: save, then run every check on it with houdini_verify saved: true.", "- Report each requirement with its measured value from the file. A failed or unchecked one is not done: say so.", diff --git a/integrations/houdini/plugin/src/tasks.ts b/integrations/houdini/plugin/src/tasks.ts index af9182a..db40e0f 100644 --- a/integrations/houdini/plugin/src/tasks.ts +++ b/integrations/houdini/plugin/src/tasks.ts @@ -84,6 +84,7 @@ export function taskStatus(task: RenderTask) { const failed = marker(text, "FAILED") const progress = marker(text, "PROGRESS") as { done?: number; total?: number } | undefined const alive = isAlive(task.pid) + const crash = crashOf(text) // "done" only when the job said so and every expected file is there, not empty. const missing = done ? task.targets.filter((file) => !existsSync(file) || statSync(file).size === 0) : [] const verified = done !== undefined && !missing.length @@ -91,7 +92,7 @@ export function taskStatus(task: RenderTask) { ? "cancelled" : verified ? "done" - : done !== undefined || failed !== undefined || !alive + : done !== undefined || failed !== undefined || crash || !alive ? "failed" : task.kind === "pdg" ? "cooking" @@ -122,7 +123,13 @@ export function taskStatus(task: RenderTask) { ? failed.slice(-1500) : failed !== undefined ? JSON.stringify(failed).slice(-1500) - : errors.join(" | ") || `the background hython stopped without finishing; see ${task.log}`, + : crash + ? `the background hython crashed (${crash}); the open Houdini is not affected.${ + typeof (task.facts as { crash_risk?: unknown } | undefined)?.crash_risk === "string" + ? ` ${(task.facts as { crash_risk: string }).crash_risk}.` + : "" + } See ${task.log}` + : errors.join(" | ") || `the background hython stopped without finishing; see ${task.log}`, } : {}), ...(errors.length && state !== "failed" ? { log_errors: errors } : {}), @@ -132,6 +139,20 @@ export function taskStatus(task: RenderTask) { } } +// A hython that crashed writes "Fatal error: Segmentation fault" (or "Caught signal N") and its crashing thread's +// frames: the signal and the first functions, innermost first. +export function crashOf(text: string): string | undefined { + const fatal = /Fatal error: ([^\r\n]+)|Caught signal (\d+)/.exec(text) + if (!fatal) return undefined + const what = fatal[1]?.trim() ?? `signal ${fatal[2]}` + const thread = text.slice(text.indexOf("CURRENT THREAD") >= 0 ? text.indexOf("CURRENT THREAD") : fatal.index) + const frames = [...thread.matchAll(/\[([A-Za-z_][\w:~<>]*)\]/g)] + .map((match) => match[1]) + .filter((name, index, all) => name && all.indexOf(name) === index) + .slice(0, 3) + return frames.length ? `${what} in ${frames.join(" < ")}` : what +} + export function cancelTask(task: RenderTask) { task.cancelled = true try { diff --git a/integrations/houdini/test/host_probe.py b/integrations/houdini/test/host_probe.py index d57115e..1bc1f16 100644 --- a/integrations/houdini/test/host_probe.py +++ b/integrations/houdini/test/host_probe.py @@ -232,6 +232,62 @@ def test_status_answers_off_the_main_thread_during_a_long_call(self): self.assertEqual((status["busy"], status["running"]), (True, "execute_code")) self.assertEqual(host.dispatch("status", {})["busy"], False) + def test_hello_answers_while_the_main_thread_is_busy(self): + # OpenCode checks each new offer with hello, one offer at a time: a hello waiting behind a cook held every + # offer of the computer back in the Sol trial. It no longer goes through the main thread. + host = module.Host.__new__(module.Host) + host.state = state() + host.ui = module.UIThreadGate() + host.saves = host.loads = 0 + host.document = "shot.hipnc" + host._hello = {"protocol": 1, "host_version": "0.3.0", "app_version": "22.0.429", "capability_hash": "x", + "capabilities": {"methods": ["hello"]}, "process": {"pid": 1, "temp_dir": "t"}} + replies = [] + asker = threading.Thread(target=lambda: replies.append(host.dispatch("hello", {}))) + started = time.monotonic() + asker.start() + asker.join(2) # nobody serves the main thread here + self.assertTrue(replies, "hello waited for the main thread") + self.assertLess(time.monotonic() - started, 1) + self.assertEqual((replies[0]["script_handle"], replies[0]["revision"], replies[0]["document"]), + (host.state.handle, host.state.revision, "shot.hipnc")) + + def test_renewals_go_straight_to_the_service_and_discover_again_after_a_failure(self): + advertiser = importlib.import_module("opencode_houdini.advertiser") + calls = [] + service = types.SimpleNamespace(file="service.json") + outcomes = [] + fake = types.ModuleType("opencode_service") + fake.TIMEOUT = 3.0 + fake.discover = lambda env=None, timeout=3.0: calls.append("discover") or [service] + fake.location_candidates = lambda setting=None, env=None: ["global"] + fake._ready = lambda service, rpc, directory, deadline: (calls.append("status") or (200, {"output": {}})) + fake._unavailable = lambda status, body: False + fake._outcome = lambda method, status, body: body + fake.call = lambda service, rpc, method, payload, directory, timeout: ( + calls.append("%s:%.0f" % (method, timeout)) or (200, outcomes.pop(0) if outcomes else "renewed")) + sys.modules["opencode_houdini.opencode_service"] = fake + import opencode_houdini + opencode_houdini.opencode_service = fake + try: + ad = advertiser.Advertiser(lambda: (1, "t" * 64, "hip:x", "Houdini: a.hipnc")) + outcomes[:] = ["accepted"] + ad.advertise_once() + self.assertEqual(calls, ["discover", "status", "advertise:8"]) + calls.clear() + ad.advertise_once() + ad.advertise_once() + # Renewals: one request each, with time for a busy service. + self.assertEqual(calls, ["advertise:8", "advertise:8"]) + calls.clear() + outcomes[:] = ["error", "accepted"] + results = ad.advertise_once() + self.assertEqual(calls, ["advertise:8", "discover", "status", "advertise:8"]) + self.assertEqual(results, [{"service": "service.json", "outcome": "accepted"}]) + finally: + sys.modules.pop("opencode_houdini.opencode_service", None) + del opencode_houdini.opencode_service + def test_hello_names_the_process_and_its_crash_folder(self): self.assertTrue(module.temp_dir().endswith("houdini_temp")) self.assertEqual(module.offer_label("shot/v2.hipnc"), "Houdini: shot v2.hipnc") diff --git a/integrations/houdini/test/panel_probe.py b/integrations/houdini/test/panel_probe.py index 9e7be40..1eb6d40 100644 --- a/integrations/houdini/test/panel_probe.py +++ b/integrations/houdini/test/panel_probe.py @@ -86,7 +86,7 @@ def test_shared_host_lifetime(self): import opencode_houdini as integration from opencode_houdini import panel register.assert_called_once_with(integration.shutdown) - state = Mock(handle="hip:two-panels", _closed=False) + state = Mock(handle="hip:two-panels", epoch="test-epoch", revision=1, _closed=False) state.hello.return_value = {"script_handle": state.handle, "revision": 1} # Only the HOM scene boundary is stubbed; Host, HostServer and panel methods are real. with patch.object(integration.host, "ScriptState", return_value=state): diff --git a/integrations/houdini/test/plugin.test.ts b/integrations/houdini/test/plugin.test.ts index f39e957..933bbbd 100644 --- a/integrations/houdini/test/plugin.test.ts +++ b/integrations/houdini/test/plugin.test.ts @@ -65,6 +65,24 @@ describe("OpenCode for Houdini 0.3.0", () => { } }) + it("two Houdinis with the same file open are said, not left to look like one (Sol ex03)", async () => { + const plugin = await loadPlugin() + const first = await offered() + const second = await offered() + try { + await plugin.rpc("advertise", announcement(first.endpoint, first.scriptHandle, "first", "Houdini: ex03.hipnc")) + assert.equal(content(await plugin.tool("houdini_hosts")).same_label, undefined) + await plugin.rpc("advertise", announcement(second.endpoint, second.scriptHandle, "second", "Houdini: ex03.hipnc")) + const listed = content(await plugin.tool("houdini_hosts")) + assert.equal(listed.houdinis.length, 2) + assert.match(listed.same_label, /Several Houdinis have the same file open \(Houdini: ex03\.hipnc\)/) + } finally { + await plugin.cleanup() + await first.close() + await second.close() + } + }) + it("says how to offer a Houdini when none is", async () => { const plugin = await loadPlugin() try { diff --git a/integrations/houdini/test/programs_hython.py b/integrations/houdini/test/programs_hython.py index 38131b9..51c8e97 100644 --- a/integrations/houdini/test/programs_hython.py +++ b/integrations/houdini/test/programs_hython.py @@ -492,6 +492,32 @@ def render_opengl(): EVIDENCE["render_opengl_files"] = [os.path.isfile(t) for t in r["targets"]] +@test +def render_opengl_without_hq_lighting(): + # Sol ex07: an OpenGL ROP with High Quality Lighting off crashed its render hython (and, rendered in the open + # Houdini, Houdini itself) in the Vulkan renderer. The start says so; the job's log holds the crash. + render_scene() + hou.node("/out/gl1").parm("hqlighting").set(0) + output = os.path.join(WORK, "gl_nohq", "frame.png") + r = program("render.py", {"rop": "/out/gl1", "output": output, "frames": 1}, job="render_job.py") + EVIDENCE["render_opengl_nohq_start"] = r.get("facts") + check("hqlighting" in r["facts"].get("crash_risk", ""), r["facts"]) + deadline = time.time() + 120 + text = "" + while time.time() < deadline and "TRACEBACK END" not in text and "OPENCODE_DONE" not in text: + time.sleep(0.5) + with open(r["log"], encoding="utf-8", errors="replace") as fh: + text = fh.read() + current = text[text.find("CURRENT THREAD"):] if "CURRENT THREAD" in text else "" + EVIDENCE["render_opengl_nohq_crash"] = [line.split("[")[1].split("]")[0] for line in current.splitlines() if "[" in line][:4] + check("Segmentation fault" in text, text[-600:]) + hou.node("/out/gl1").parm("hqlighting").set(1) + r = program("render.py", {"rop": "/out/gl1", "output": os.path.join(WORK, "gl_hq", "frame.png"), "frames": 1}, + job="render_job.py") + check("crash_risk" not in r["facts"], r["facts"]) + check(wait_job(r)[-1][0] == "DONE", "the same ROP with High Quality Lighting on") + + @test def render_pdg(): folder = render_scene() diff --git a/integrations/houdini/test/tasks.test.ts b/integrations/houdini/test/tasks.test.ts new file mode 100644 index 0000000..96b4443 --- /dev/null +++ b/integrations/houdini/test/tasks.test.ts @@ -0,0 +1,54 @@ +// Render and PDG tasks: a background hython that crashed is said as a crash, with its first frames, and with the +// render program's risk note when it had one (the Sol trial's ex07 render log said only "stopped"). + +import assert from "node:assert/strict" +import { mkdtempSync, rmSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { it } from "node:test" + +import { addTask, crashOf, taskStatus } from "../plugin/src/tasks.ts" + +// The head of the ex07 render job's log (Houdini 22.0.429, an OpenGL ROP with High Quality Lighting off). +const CRASHED = [ + 'OPENCODE_RENDER {"node": "/out/preview", "type": "Driver/opengl", "frames": [1, 1], "count": 1}', + "20052: Fatal error: Segmentation fault", + "-- TRACEBACK BEGIN --", + "Traceback from hython 22.0.429 (Compiled on windows-x86_64-cl19.42):", + "CURRENT THREAD 6560", + "+0x117291600 [RV_VKImage::getInfo] C:\\PROGRA~1\\SIDEEF~1\\HOUDIN~1.429\\bin\\libRV.dll", + "+0x118342303 [DM_RenderVulkan::resizeTransparentBuffer] C:\\PROGRA~1\\SIDEEF~1\\HOUDIN~1.429\\bin\\libDM.dll", + "+0x11833a127 [DM_RenderVulkan::renderHeadlight] C:\\PROGRA~1\\SIDEEF~1\\HOUDIN~1.429\\bin\\libDM.dll", + "+0x11833b296 [DM_RenderVulkan::renderLighting] C:\\PROGRA~1\\SIDEEF~1\\HOUDIN~1.429\\bin\\libDM.dll", + "-- TRACEBACK END --", +].join("\n") + +it("a crashed render hython is said as a crash with its first frames and the ROP's risk note", () => { + assert.equal( + crashOf(CRASHED), + "Segmentation fault in RV_VKImage::getInfo < DM_RenderVulkan::resizeTransparentBuffer < DM_RenderVulkan::renderHeadlight", + ) + assert.equal(crashOf("OPENCODE_PROGRESS {}\nfine"), undefined) + const dir = mkdtempSync(join(tmpdir(), "houdini-task-")) + try { + const log = join(dir, "render.log") + writeFileSync(log, CRASHED) + const task = addTask({ + // This test process, alive: a crash is said even while the crashed hython is still being torn down. + pid: process.pid, + log, + kind: "render", + targets: [join(dir, "preview.png")], + node: "/out/preview", + facts: { crash_risk: "High Quality Lighting is off on this OpenGL ROP (hqlighting 0): set hqlighting to 1" }, + }) + const status = taskStatus(task) as { state: string; error?: string } + assert.equal(status.state, "failed") + assert.match( + String(status.error), + /^the background hython crashed \(Segmentation fault in RV_VKImage::getInfo < .*\); the open Houdini is not affected\. High Quality Lighting is off on this OpenGL ROP .* See /, + ) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +}) From 8ed524142cc34f8f916cd82a94dd2e19920d678e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Andr=C3=A9?= Date: Mon, 16 Nov 2026 08:32:56 +0100 Subject: [PATCH 4/4] fix(houdini): verify saved never waits on Houdini's main thread, says what it read and where it stopped In the Sol ex03 retrial, houdini_verify saved: true on a Vellum scene with 48 cached frames ran 10 minutes and said only that Houdini was still running execute_code. The verify itself takes 2 s on that file (16 criteria, the cache read from disk): it never started. In Houdini's interface hou serialises its calls across threads. The host's gate called hou.isUIAvailable() and hou.ui.postEventCallback() on the request's thread, so a request waited for whatever the main thread ran (here the agent's own execute_code that reloaded the file) and its 10 s "busy" answer never came; houdini_context waited 120 s and houdini_verify 600 s. Measured on a hidden-desktop Houdini: a context sent during a 40 s execute_code waited 37 s. The gate now queues requests without any hou call on their thread; an event loop callback registered once on the main thread runs them, never one inside another. A request not taken within 10 s is answered with what runs and since when. verify saved no longer holds the main thread while its background hython reads the file: verify_saved.py only starts it (with Houdini's environment) and returns. The plugin follows it outside Houdini (saved-verify.ts): its result, its crash, and timeout_s, after which it stops the hython and says what it was doing (the OPENCODE_STEP lines of its log: loading, criterion i/n and its label and frame, the error scan's cooks, the render). verify.py times each criterion and lists in facts.read where the geometry it read came from: files on disk (a File SOP, a File Cache that loads), cooked, or simulated from the start frame by an upstream solver with no cache read. Its error scan no longer cooks the display node inside a network of its own kind (a solver asset's inside), which could simulate up to the current frame. Tests: the gate under a HOM-like lock (host_probe), following a background verify to its result, its crash and its timeout (saved-verify.test), and in real hython a cached simulation read back (cache files vs simulated solver, steps in the log) plus reloading the open file through execute_code. The first and the hython ones fail on the previous commit; saved-verify.ts is new. A real trial on a copy of the ex03 scene in a hidden-desktop Houdini: 16/16 in 2.1 s with cached_cloth read from $HIP/cache/vellum; refused in 10.0 s while the main thread was busy; a criterion reading the solver at frame 48 with timeout_s 6 stopped at 6.5 s naming that criterion. --- .../host/scripts/opencode_houdini/host.py | 57 ++++-- integrations/houdini/plugin/README.md | 3 +- .../host/houdini/opencode_link-0.3.0.zip | Bin 262540 -> 264160 bytes .../plugin/methods/tools/houdini_verify.json | 2 +- integrations/houdini/plugin/package.json | 2 +- .../houdini/plugin/programs/verify.py | 70 ++++++- .../houdini/plugin/programs/verify_saved.py | 77 ++++---- .../houdini/plugin/src/integration.ts | 9 +- .../houdini/plugin/src/saved-verify.ts | 180 ++++++++++++++++++ integrations/houdini/test/host_probe.py | 77 ++++++++ integrations/houdini/test/programs_hython.py | 91 ++++++++- .../houdini/test/saved-verify.test.ts | 110 +++++++++++ 12 files changed, 619 insertions(+), 59 deletions(-) create mode 100644 integrations/houdini/plugin/src/saved-verify.ts create mode 100644 integrations/houdini/test/saved-verify.test.ts diff --git a/integrations/houdini/host/scripts/opencode_houdini/host.py b/integrations/houdini/host/scripts/opencode_houdini/host.py index aaa8102..c61b74b 100644 --- a/integrations/houdini/host/scripts/opencode_houdini/host.py +++ b/integrations/houdini/host/scripts/opencode_houdini/host.py @@ -1184,6 +1184,11 @@ class UIThreadGate: records no undo off the main thread: undos.areEnabled() is false there). A request not taken within QUEUE_WAIT_S is answered busy. `running()` answers from any thread: the request the main thread runs now and for how long, so a call whose wait ended can learn that it still runs. + + A request's thread never calls hou: hou serialises its calls across threads, so a hou call made there (even + hou.ui.postEventCallback) waits until the main thread's request ends, and the QUEUE_WAIT_S answer never came + (Sol ex03: calls waited 2 and 10 minutes behind an execute_code). Requests wait in a queue that an event loop + callback, registered here on the main thread, runs; never one inside another. """ def __init__(self) -> None: @@ -1192,6 +1197,33 @@ def __init__(self) -> None: self._queued = 0 self._count = threading.Lock() self._jobs: "queue.Queue[Callable[[], None]]" = queue.Queue() + self._event_loop = False + try: + if hou.isUIAvailable() and hasattr(hou, "ui") and hasattr(hou.ui, "addEventLoopCallback"): + hou.ui.addEventLoopCallback(self._drain) + self._event_loop = True + except Exception: # noqa: BLE001 - no interface: pump() serves the queue + self._event_loop = False + + def _drain(self) -> None: + """Houdini's event loop, on the main thread: the queued requests, unless one runs already (a request that + lets the event loop turn, a file load for instance, never runs another inside it).""" + if self._running is not None: + return + while True: + try: + job = self._jobs.get_nowait() + except queue.Empty: + return + job() + + def close(self) -> None: + if self._event_loop: + self._event_loop = False + try: + hou.ui.removeEventLoopCallback(self._drain) + except Exception: # noqa: BLE001 + pass def running(self) -> dict[str, Any]: current = self._running @@ -1241,25 +1273,23 @@ def invoke() -> None: with self._count: self._queued += 1 try: - if not hou.isUIAvailable() or not hasattr(hou, "ui"): - self._jobs.put(invoke) - elif hasattr(hou.ui, "postEventCallback"): - hou.ui.postEventCallback(invoke) - else: - def event_loop_callback() -> None: - hou.ui.removeEventLoopCallback(event_loop_callback) - invoke() - - hou.ui.addEventLoopCallback(event_loop_callback) + # The interface's event loop (_drain) or hython's pump() takes it: no hou call on this thread. + self._jobs.put(invoke) if not started.wait(QUEUE_WAIT_S): with lock: if not started.is_set(): expired.set() + now = self.running() + doing = ( + "it still runs %s (%s s so far): nothing was done for this request; ask again once it has " + "finished" % (now["running"], now["seconds"]) + if now.get("busy") + else "a modal dialog, a long cook or render, or a script outside OpenCode holds it" + ) raise HostError( "ui_timeout", - "Houdini's main thread did not take the request within %d s (a modal dialog, a long " - "cook or render)" % QUEUE_WAIT_S, - {"running": self.running().get("running")}, + "Houdini's main thread did not take the request within %d s: %s" % (QUEUE_WAIT_S, doing), + {"running": now.get("running"), "seconds": now.get("seconds")}, ) finally: with self._count: @@ -1309,6 +1339,7 @@ def close(self) -> None: hou.hipFile.removeEventCallback(self._hip_event) except hou.Error: pass + self.ui.close() self.state.close() def reset(self) -> None: diff --git a/integrations/houdini/plugin/README.md b/integrations/houdini/plugin/README.md index 22fe2e5..670ab79 100644 --- a/integrations/houdini/plugin/README.md +++ b/integrations/houdini/plugin/README.md @@ -47,7 +47,8 @@ The **OpenCode Link** Python panel is a conversation inside Houdini. **Connect** - Houdini Apprentice: files are non-commercial, renders are at most 1920×1080 and carry the Houdini logo; render results say so. - With Houdini 22.0.429, an OpenGL ROP with High Quality Lighting off (`hqlighting` 0) crashes in Houdini's Vulkan renderer, in `hython` as in the interface: `houdini_render` says so when it starts such a render and when its background `hython` crashed. Rendered with `houdini_render`, a crash never closes the open Houdini; `.render()` in `houdini_execute_code` runs in it. -- `houdini_apply` refuses changes in `hython` (no interface); `houdini_execute_code` works in both. Houdini records undo only on its main thread: OpenCode Link runs every request there (in `hython`, a script serves them with `opencode_houdini.pump()`). +- `houdini_apply` refuses changes in `hython` (no interface); `houdini_execute_code` works in both. Houdini records undo only on its main thread: OpenCode Link runs every request there (in `hython`, a script serves them with `opencode_houdini.pump()`). A request the main thread has not taken within 10 s, because another one still runs there, is answered with what runs and since when; nothing is done for it. +- `houdini_verify` with `saved` only starts its background `hython` from Houdini: the file is read while Houdini stays free, `timeout_s` bounds the read and a stopped read says what it was doing. Geometry read without its cache is recomputed: a simulation read at a late frame is simulated again from its start frame; `facts.read` says which geometry came from cache files. - `hou.hipFile.hasUnsavedChanges()` stays true after a save in `hython`: OpenCode Link judges a save by the file's date on disk. ## Safety diff --git a/integrations/houdini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V4@&B}X4YDq`bL4I z^}W3$WmM5ulcXd1c~Ml)j%NPbb#Wnv-OI Lb6Yam)YSAp^~FvF delta 506 zcmaFRFVNF0u;FwuZ(;L=h9iuO3?Qs9dA+&p=C8%4FK4njZ!qWqmGqOy7kjKa}`GWxq$Ow61EQ%*9eih?{7^ZBC=Ez47jsrW3G4os|rs;f~Ow!XQ Xva@J01+q-v%g!Rpw*4=21.0", "payload": "host/houdini/opencode_link-0.3.0.zip", - "sha256": "b0ea40242d2a349e31afeb16bb96f53db0a4ddd5b69ac988d50d410fed91511a" + "sha256": "af455684d246060085779f53937e36aabb4c3ce41d146a2a28ec2cd2c1baade7" } } } diff --git a/integrations/houdini/plugin/programs/verify.py b/integrations/houdini/plugin/programs/verify.py index 001f538..07d8444 100644 --- a/integrations/houdini/plugin/programs/verify.py +++ b/integrations/houdini/plugin/programs/verify.py @@ -28,6 +28,9 @@ NODES_MAX = 200000 OPS = ("<", "<=", ">", ">=", "==", "!=", "approx", "between", "in", "contains") THROWAWAY = os.environ.get("OPENCODE_HOUDINI_ROLE") in ("verify", "render") and not hou.isUIAvailable() +# houdini_verify saved: the background hython's job defines STEP, a line in its log saying what it does now (the +# plugin reads it to say where a verify that ran out of time stopped). In the open scene: nothing. +STEP = globals().get("STEP") or (lambda **what: None) if not isinstance(CRITERIA, list) or len(CRITERIA) > 50: raise ValueError("criteria: a list of at most 50 {expr, op, value, tol?, frame?, label?}") @@ -97,6 +100,58 @@ def sop_of(path): COOKED = [] +READ = [] # nodes the criteria named (node(path)): where their geometry came from is said in facts.read +SOLVERS = ("dopnet", "dopimport", "dopimportfield", "dopimportrecords") + + +def source_of(n): + """Where a SOP's geometry comes from: files on disk (a File SOP, or a File Cache that loads from disk), or + cooked, simulated by an upstream solver from its start frame when no cache is read on the way.""" + kind = n.type().name() + loads = kind == "file" or (kind.startswith("filecache") and n.parm("loadfromdisk") is not None and n.evalParm("loadfromdisk")) + if loads: + p = n.parm("file") or n.parm("sopoutput") + return {"from": "files on disk", **({"files": p.unexpandedString()} if p is not None else {})} + solvers = [] + try: + for a in n.inputAncestors(): + name = a.type().name() + if name == "file" or (name.startswith("filecache") and a.parm("loadfromdisk") is not None and a.evalParm("loadfromdisk")): + continue + if "solver" in name or name in SOLVERS: + solvers.append(a.path()) + except Exception: # noqa: BLE001 + pass + if solvers and (kind in SOLVERS or "solver" in kind or n.isTimeDependent()): + return {"from": "cooked: simulated from the start frame (no cache read)", "solvers": solvers[:4]} + if "solver" in kind or kind in SOLVERS: + return {"from": "cooked: simulated from the start frame (no cache read)", "solvers": [n.path()]} + return {"from": "cooked"} + + +def read_sources(): + found = [] + for path in READ + [p for p in COOKED if p not in READ]: + n = hou.node(path) + if n is None or n.type().category().name() != "Sop": + continue + # A node a criterion only named (its parms, its inputs) gave no geometry: not listed. + if path not in COOKED and not n.cookCount(): + continue + try: + found.append(dict({"node": path}, **source_of(n))) + except Exception: # noqa: BLE001 + continue + if len(found) >= 20: + break + return found + + +def node_read(path): + n = need(path) + if path not in READ: + READ.append(n.path()) + return n def namespace(frame): @@ -170,7 +225,7 @@ def attrib_values(path, name, cls="point"): return values if size == 1 else [tuple(values[i:i + size]) for i in range(0, len(values), size)] return list(getattr(g, fn[cls].replace("Float", "String"))(name)) - return {"__builtins__": SAFE_BUILTINS, "hou": hou, "math": math, "frame": frame, "node": need, "geo": geo, + return {"__builtins__": SAFE_BUILTINS, "hou": hou, "math": math, "frame": frame, "node": node_read, "geo": geo, "bbox": bbox, "npoints": lambda path: geo(path).intrinsicValue("pointcount"), "nprims": lambda path: geo(path).intrinsicValue("primitivecount"), "stage": stage, "prim": prim, "usd_attr": usd_attr, "usd_prims": usd_prims, "parm": parm, "attrib_names": attrib_names, @@ -236,6 +291,8 @@ def numbers(x): code = read_only_expression(str(c["expr"])) frame = float(c["frame"]) if c.get("frame") is not None else FRAME row = {"label": c.get("label") or c["expr"], "check": "%s %s %s" % (c["expr"], c["op"], json.dumps(c["value"])), "frame": rounded(frame)} + STEP(step="criterion", index=i + 1, of=len(CRITERIA), label=row["label"][:120], frame=rounded(frame)) + measuring = time.time() try: measured = eval(code, namespace(frame)) # noqa: S307 - a checked read-only expression row["measured"] = rounded(measured) @@ -243,6 +300,7 @@ def numbers(x): except Exception as ex: # noqa: BLE001 row["passed"] = False row["error"] = ("%s: %s" % (type(ex).__name__, ex))[:400] + row["seconds"] = round(time.time() - measuring, 2) rows.append(row) facts = {"hip": None if hou.hipFile.isNewFile() else hou.hipFile.path(), "frame": rounded(hou.frame()), "fps": rounded(hou.fps()), @@ -257,6 +315,10 @@ def cook_targets(): category = n.childTypeCategory() if category is None or not n.children(): continue + # A network inside a network of its kind (a SOP subnet, a solver asset's inside): its parent's display + # node cooks what is shown; cooking its own could run a whole simulation up to this frame. + if n.type().category() == category: + continue if category.name() in ("Sop", "Lop"): for getter in ("displayNode", "renderNode"): fn = getattr(n, getter, None) @@ -275,6 +337,7 @@ def scan_errors(): if time.time() - STARTED > COOK_BUDGET: skipped.append(path) continue + STEP(step="cook", node=path) n = hou.node(path) try: n.cook() @@ -313,8 +376,12 @@ def scan_errors(): roots = [need(r) for r in (P.get("roots") or ["/"])] +sources = read_sources() +if sources: + facts["read"] = sources error_rows = [] if P.get("errors", True): + STEP(step="errors", roots=[r.path() for r in roots]) error_rows, facts["error_scan"] = scan_errors() if len(error_rows) >= ERRORS_MAX: facts["error_scan"]["listed"] = ERRORS_MAX @@ -519,6 +586,7 @@ def render_image(options): image = None if P.get("render"): + STEP(step="render") rendered = render_image(render_options(P["render"])) facts["render"] = rendered["facts"] image = rendered.get("image_base64") diff --git a/integrations/houdini/plugin/programs/verify_saved.py b/integrations/houdini/plugin/programs/verify_saved.py index 1f63a4b..6aafd14 100644 --- a/integrations/houdini/plugin/programs/verify_saved.py +++ b/integrations/houdini/plugin/programs/verify_saved.py @@ -1,13 +1,14 @@ # SPDX-License-Identifier: MIT # houdini_verify with saved (fixed program): the same verification, read from the file as written on disk instead -# of the open scene: a background hython loads the .hip/.hipnc and runs the verify program there (criteria, -# errors, render), so what is delivered can be compared with what the open scene shows. That hython never offers +# of the open scene. This program only starts it and returns at once: a background hython, started from this +# Houdini (its environment: packages, HDA paths, variables), loads the .hip/.hipnc and runs the verify program there +# (criteria, errors, render). The plugin follows it from its log (OPENCODE_STEP lines: what it does now), bounds it +# with timeout_s and reads its result; Houdini's main thread is never held while it runs. That hython never offers # itself to OpenCode (OPENCODE_HOUDINI_ROLE=verify, no offer key) and opens no window. # PARAMS: the verify parameters with "saved": true (the open file as saved) or a .hip/.hipnc path; "timeout_s"?. # JOB: verify.py, prepended by the plugin. import json import os -import shutil import subprocess import tempfile import time @@ -35,42 +36,40 @@ raise RuntimeError("no hython at %s" % exe) params = {key: value for key, value in P.items() if key != "saved"} work = tempfile.mkdtemp(prefix="opencode_verify_saved_") -started = time.time() -try: - out_path = os.path.join(work, "result.json") - script = os.path.join(work, "verify.py") - with open(script, "w", encoding="utf-8") as fh: - fh.write("import json, os, sys, traceback\nimport hou\n") - fh.write("warnings = []\ntry:\n hou.hipFile.load(%r, suppress_save_prompt=True, ignore_load_warnings=False)\n" % path.replace("\\", "/")) - fh.write("except hou.LoadWarning as w:\n warnings.append(str(w)[:1500])\n") - fh.write("try:\n PARAMS = json.loads(%r)\n result = {}\n" % json.dumps(params)) - fh.write(" exec(compile(%r, '', 'exec'))\n" % JOB) # noqa: F821 - verify.py, from the plugin - fh.write(" result['load_warnings'] = warnings\n") - fh.write("except Exception:\n result = {'error': traceback.format_exc()[-3000:]}\n") - fh.write("with open(%r, 'w', encoding='utf-8') as _out:\n json.dump(result, _out, default=repr)\n" % out_path) - # hython may crash in its own exit handlers after renders: the result is written, leave at once. - fh.write("sys.stdout.flush()\nos._exit(0)\n") - env = {k: v for k, v in os.environ.items() if k != "OPENCODE_HOUDINI_OFFER_KEY"} - env["OPENCODE_HOUDINI_ROLE"] = "verify" # never offered to OpenCode - kwargs = {"stdin": subprocess.DEVNULL, "stdout": subprocess.PIPE, "stderr": subprocess.STDOUT, "env": env} +out_path = os.path.join(work, "result.json") +log_path = os.path.join(work, "verify.log") +script = os.path.join(work, "verify.py") +with open(script, "w", encoding="utf-8") as fh: + fh.write( + "import json, os, sys, time, traceback\nimport hou\n" + "T0 = time.time()\n" + "def STEP(**what):\n" + " print('OPENCODE_STEP ' + json.dumps(dict(what, at_s=round(time.time() - T0, 1)), default=repr), flush=True)\n" + "warnings = []\n" + "STEP(step='load', file=%r)\n" + "try:\n hou.hipFile.load(%r, suppress_save_prompt=True, ignore_load_warnings=False)\n" + "except hou.LoadWarning as w:\n warnings.append(str(w)[:1500])\n" + "LOAD_S = round(time.time() - T0, 1)\n" + "STEP(step='loaded', seconds=LOAD_S)\n" + % (path, path.replace("\\", "/")) + ) + fh.write("try:\n PARAMS = json.loads(%r)\n result = {}\n" % json.dumps(params)) + fh.write(" exec(compile(%r, '', 'exec'))\n" % JOB) # noqa: F821 - verify.py, from the plugin + fh.write(" result['load_warnings'] = warnings\n result['load_seconds'] = LOAD_S\n") + fh.write("except Exception:\n result = {'error': traceback.format_exc()[-3000:]}\n") + # Written whole, then renamed: the plugin never reads half a result. + fh.write("with open(%r, 'w', encoding='utf-8') as _out:\n json.dump(result, _out, default=repr)\n" % (out_path + ".part")) + fh.write("os.replace(%r, %r)\nSTEP(step='done')\n" % (out_path + ".part", out_path)) + # hython may crash in its own exit handlers after renders: the result is written, leave at once. + fh.write("sys.stdout.flush()\nos._exit(0)\n") +env = {k: v for k, v in os.environ.items() if k != "OPENCODE_HOUDINI_OFFER_KEY"} +env["OPENCODE_HOUDINI_ROLE"] = "verify" # never offered to OpenCode +with open(log_path, "wb") as log: + kwargs = {"stdin": subprocess.DEVNULL, "stdout": log, "stderr": subprocess.STDOUT, "env": env, "cwd": work} if os.name == "nt": kwargs["creationflags"] = 0x08000000 # no console window - try: - run = subprocess.run([exe, script], timeout=int(P.get("timeout_s") or 540), **kwargs) - except subprocess.TimeoutExpired: - raise RuntimeError("the background hython reading %s did not finish within %s s (timeout_s)" % (path, P.get("timeout_s") or 540)) - if not os.path.exists(out_path): - tail = run.stdout.decode("utf-8", "replace")[-1500:] if run.stdout else "" - raise RuntimeError("the background hython reading %s gave no result (exit %s):\n%s" % (path, run.returncode, tail)) - with open(out_path, encoding="utf-8") as fh: - result = json.load(fh) -finally: - shutil.rmtree(work, ignore_errors=True) -if "error" in result: - raise RuntimeError("verify in the background hython reading %s failed:\n%s" % (path, result["error"])) + child = subprocess.Popen([exe, script], **kwargs) stat = os.stat(path) -load_warnings = result.pop("load_warnings", []) -result["read"] = {"file": path, "saved_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stat.st_mtime)), - "bytes": stat.st_size, "seconds": round(time.time() - started, 1), - "by": "a background hython on the file as written on disk, not the open scene", - **({"load_warnings": load_warnings} if load_warnings else {})} +result = {"started": {"pid": child.pid, "work": work, "log": log_path, "result": out_path, "file": path, + "saved_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stat.st_mtime)), + "bytes": stat.st_size, "timeout_s": int(P.get("timeout_s") or 540)}} diff --git a/integrations/houdini/plugin/src/integration.ts b/integrations/houdini/plugin/src/integration.ts index 47a24a0..8036284 100644 --- a/integrations/houdini/plugin/src/integration.ts +++ b/integrations/houdini/plugin/src/integration.ts @@ -36,6 +36,7 @@ import { type EntryPut, methodAttacher } from "./method.ts" import { type HoudiniOptions, readOptions } from "./options.ts" import { defaultPathEnv, defaultPrefsDir, methodsDir, type PathEnv, packageRoot } from "./paths.ts" import { type ProgramName, programCode } from "./programs.ts" +import { isSavedStart, waitSaved } from "./saved-verify.ts" import { addTask, cancelTask, getTask, isRenderStart, taskStatus } from "./tasks.ts" export const APP = "houdini" @@ -357,7 +358,13 @@ export async function setupHoudini(ctx: Plugin.Context, deps: HoudiniDeps = {}) const saved = args.saved !== undefined && args.saved !== false if (saved && args.saved !== true && typeof args.saved !== "string") throw new Error("saved: true (the open file as saved) or a .hip, .hipnc or .hiplc path.") - return withImage(await program(context, saved ? "verify_saved" : "verify", args, VERIFY_TIMEOUT_MS), "verify.png") + if (!saved) return withImage(await program(context, "verify", args, VERIFY_TIMEOUT_MS), "verify.png") + // Houdini only starts the background hython (a moment on its main thread); it is followed here. + const timeout_s = + typeof args.timeout_s === "number" ? Math.min(Math.max(Math.round(args.timeout_s), 5), 3_600) : 540 + const started = (await program(context, "verify_saved", { ...args, timeout_s })) as { started?: unknown } + if (!isSavedStart(started?.started)) throw new Error("houdini_verify saved: the background hython did not start.") + return withImage(await waitSaved(started.started, { signal: context.signal }), "verify.png") }, async houdini_render(args, context) { diff --git a/integrations/houdini/plugin/src/saved-verify.ts b/integrations/houdini/plugin/src/saved-verify.ts new file mode 100644 index 0000000..4126f6a --- /dev/null +++ b/integrations/houdini/plugin/src/saved-verify.ts @@ -0,0 +1,180 @@ +// houdini_verify saved: the verify program runs in a background hython started from the open Houdini, which only +// starts it (programs/verify_saved.py). This module follows it outside Houdini's main thread: its result, its log's +// steps (OPENCODE_STEP: what it does now), its crash, and timeout_s, after which it is stopped and the answer says +// where it was. + +import { execFileSync } from "node:child_process" +import { existsSync, readFileSync, rmSync } from "node:fs" + +import { isAlive } from "./kit.ts" +import { crashOf } from "./tasks.ts" + +export type SavedStart = { + pid: number + work: string + log: string + result: string + file: string + saved_at: string + bytes: number + timeout_s: number +} + +export function isSavedStart(value: unknown): value is SavedStart { + const start = value as Partial | undefined + return ( + typeof start?.pid === "number" && + typeof start.log === "string" && + typeof start.result === "string" && + typeof start.file === "string" && + typeof start.timeout_s === "number" + ) +} + +type Step = { step?: string; at_s?: number; [key: string]: unknown } + +export function stepsOf(text: string): Step[] { + return text + .split(/\r?\n/) + .filter((line) => line.startsWith("OPENCODE_STEP ")) + .flatMap((line) => { + try { + return [JSON.parse(line.slice("OPENCODE_STEP ".length)) as Step] + } catch { + return [] + } + }) +} + +// What a step says it does, in words. +export function describeStep(step: Step | undefined, file: string) { + if (step?.step === undefined) return "starting (hython had not loaded the file yet)" + switch (step.step) { + case "load": + return `loading ${file}` + case "loaded": + return `loaded ${file} in ${step.seconds} s, before the first criterion` + case "criterion": + return `measuring criterion ${step.index}/${step.of} "${step.label}" at frame ${step.frame}` + case "errors": + return "starting the error scan" + case "cook": + return `cooking ${step.node} for the error scan` + case "render": + return "rendering the check image" + case "done": + return "writing its result" + default: + return String(step.step) + } +} + +const read = (file: string) => { + try { + return readFileSync(file, "utf8") + } catch { + return "" + } +} + +export type SavedDeps = { + alive?: (pid: number) => boolean + stop?: (pid: number) => void + now?: () => number + sleep?: (ms: number) => Promise + signal?: AbortSignal +} + +function stopTree(pid: number) { + if (process.platform === "win32") { + // hython may have started its own render hython: the whole tree. + try { + execFileSync("taskkill", ["/pid", String(pid), "/T", "/F"], { + windowsHide: true, + stdio: "ignore", + }) + return + } catch {} + } + try { + process.kill(pid) + } catch {} +} + +// Waits for the background hython's result, at most start.timeout_s from now. +export async function waitSaved(start: SavedStart, deps: SavedDeps = {}): Promise> { + const alive = deps.alive ?? isAlive + const stop = deps.stop ?? stopTree + const now = deps.now ?? Date.now + const sleep = deps.sleep ?? ((ms: number) => new Promise((done) => setTimeout(done, ms))) + const began = now() + const deadline = began + start.timeout_s * 1_000 + const elapsed = () => Math.round((now() - began) / 100) / 10 + // hython holds its log until it has exited (after its result, or after being stopped): removed once it lets go, + // without holding the answer back. + const cleanup = async () => { + for (let attempt = 0; attempt < 100; attempt++) { + try { + rmSync(start.work, { recursive: true, force: true }) + if (!existsSync(start.work)) return + } catch {} + await sleep(200) + } + } + try { + for (;;) { + if (existsSync(start.result)) { + const got = JSON.parse(read(start.result)) as Record + if (typeof got.error === "string") + throw new Error(`verify in the background hython reading ${start.file} failed:\n${got.error}`) + const { load_warnings, load_seconds, ...result } = got + return { + ...result, + read: { + file: start.file, + saved_at: start.saved_at, + bytes: start.bytes, + seconds: elapsed(), + ...(typeof load_seconds === "number" ? { load_seconds } : {}), + by: "a background hython on the file as written on disk, not the open scene", + ...(Array.isArray(load_warnings) && load_warnings.length ? { load_warnings } : {}), + }, + } + } + if (!alive(start.pid)) { + // Its result may land just as it exits. + await sleep(300) + if (existsSync(start.result)) continue + const text = read(start.log) + const crash = crashOf(text) + const last = describeStep(stepsOf(text).at(-1), start.file) + throw new Error( + crash + ? `the background hython reading ${start.file} crashed (${crash}) while ${last}; the open Houdini is not affected. Log: ${start.log}` + : `the background hython reading ${start.file} stopped without a result while ${last}:\n${text.slice(-1200)}`, + ) + } + if (deps.signal?.aborted || now() >= deadline) { + stop(start.pid) + const steps = stepsOf(read(start.log)) + const last = steps.at(-1) + const measured = steps.filter((step) => step.step === "criterion").length + const loaded = steps.find((step) => step.step === "loaded") + throw new Error( + `${deps.signal?.aborted ? "houdini_verify saved was cancelled" : `houdini_verify saved stopped the background hython after ${start.timeout_s} s (timeout_s)`}: it was ${describeStep(last, start.file)}. ${loaded ? `The file loaded in ${loaded.seconds} s; ` : ""}${ + measured + ? `${Math.max(0, measured - (last?.step === "criterion" ? 1 : 0))} criteria were measured before. ` + : "" + }${ + last?.step === "criterion" || last?.step === "cook" + ? "Reading frames of a simulation that is not cached simulates them: read its cache (a File SOP), or " + : "" + }give more time with timeout_s (up to 3600).`, + ) + } + await sleep(500) + } + } finally { + void cleanup() + } +} diff --git a/integrations/houdini/test/host_probe.py b/integrations/houdini/test/host_probe.py index 1bc1f16..3243a54 100644 --- a/integrations/houdini/test/host_probe.py +++ b/integrations/houdini/test/host_probe.py @@ -232,6 +232,83 @@ def test_status_answers_off_the_main_thread_during_a_long_call(self): self.assertEqual((status["busy"], status["running"]), (True, "execute_code")) self.assertEqual(host.dispatch("status", {})["busy"], False) + def test_interface_requests_behind_a_busy_main_thread_are_answered_busy_in_time(self): + # Sol ex03 retrial: in the interface, hou serialises calls across threads; a request's thread calling + # hou.ui.postEventCallback waited for the execute_code the main thread ran (2 and 10 minutes), and the + # QUEUE_WAIT_S answer never came. Here hou calls off the main thread wait for the main thread's request. + main = threading.get_ident() + hom = threading.Lock() + loop = [] + + def guarded(value): + def call(*args): + if threading.get_ident() != main: + with hom: + pass + return value(*args) if callable(value) else value + return call + + ui = types.SimpleNamespace( + addEventLoopCallback=lambda callback: loop.append(callback), + removeEventLoopCallback=lambda callback: loop.remove(callback), + postEventCallback=guarded(lambda callback: loop.append(callback)), + ) + saved = (hou.isUIAvailable, getattr(hou, "ui", None), module.QUEUE_WAIT_S) + hou.isUIAvailable, hou.ui, module.QUEUE_WAIT_S = guarded(True), ui, 0.5 + try: + gate = module.UIThreadGate() + answers = [] + + def ask(): + started = time.monotonic() + try: + answers.append(gate.run(lambda: "ran", "context")) + except module.HostError as error: + answers.append((error.code, str(error), round(time.monotonic() - started, 1))) + + # The main thread runs a request (hou held) and lets no event loop turn for 3 s. + hom.acquire() + gate._running = ("execute_code", time.monotonic()) + asker = threading.Thread(target=ask) + asker.start() + asker.join(2.5) + done_in_time = not asker.is_alive() + time.sleep(0.5) + gate._running = None + hom.release() + asker.join(5) + self.assertTrue(done_in_time, "the request waited for the main thread's request to end") + code, message, seconds = answers[0] + self.assertEqual(code, "ui_timeout") + self.assertLess(seconds, 1.5) + self.assertIn("still runs execute_code", message) + self.assertEqual(loop, [gate._drain]) + # Idle again: a request waits in the queue until the event loop turns. + asker = threading.Thread(target=ask) + asker.start() + time.sleep(0.1) + for callback in list(loop): + callback() + asker.join(2) + self.assertEqual(answers[1], "ran") + # Never one request inside another: a loop turn during a request runs nothing. + gate._running = ("execute_code", time.monotonic()) + ran = [] + gate._jobs.put(lambda: ran.append(1)) + gate._drain() + self.assertEqual(ran, []) + gate._running = None + gate._drain() + self.assertEqual(ran, [1]) + gate.close() + self.assertEqual(loop, []) + finally: + hou.isUIAvailable, module.QUEUE_WAIT_S = saved[0], saved[2] + if saved[1] is None: + del hou.ui + else: + hou.ui = saved[1] + def test_hello_answers_while_the_main_thread_is_busy(self): # OpenCode checks each new offer with hello, one offer at a time: a hello waiting behind a cook held every # offer of the computer back in the Sol trial. It no longer goes through the main thread. diff --git a/integrations/houdini/test/programs_hython.py b/integrations/houdini/test/programs_hython.py index 51c8e97..133cd8f 100644 --- a/integrations/houdini/test/programs_hython.py +++ b/integrations/houdini/test/programs_hython.py @@ -112,6 +112,26 @@ def program(name, params, job=None): return json.loads(json.dumps(namespace.get("result", {}), default=repr)) +def verify_saved(params, timeout=600): + """houdini_verify saved as the plugin runs it: the program starts the background hython and returns at once; + its result is followed from outside (saved-verify.ts). Returns (result, steps of its log, seconds to start).""" + started_at = time.time() + start = program("verify_saved.py", params, job="verify.py")["started"] + start_s = time.time() - started_at + deadline = time.time() + timeout + while time.time() < deadline and not os.path.isfile(start["result"]): + time.sleep(0.2) + with open(start["log"], encoding="utf-8", errors="replace") as fh: + steps = [json.loads(l[14:]) for l in fh.read().splitlines() if l.startswith("OPENCODE_STEP ")] + with open(start["result"], encoding="utf-8") as fh: + result = json.load(fh) + if "error" in result: + raise AssertionError(result["error"]) + result["read"] = {"file": start["file"], "bytes": start["bytes"], "seconds": round(time.time() - started_at, 1)} + shutil.rmtree(start["work"], ignore_errors=True) + return result, steps, start_s + + def failing(name, params, job=None): try: program(name, params, job) @@ -390,16 +410,59 @@ def save_and_verify_saved(): {"expr": "npoints('/obj/geo1')", "op": "==", "value": 8}] live = program("verify.py", {"criteria": criteria, "errors": False}) check(live["failed"] == 1, live["criteria"]) - r = program("verify_saved.py", {"saved": True, "criteria": criteria}, job="verify.py") + r, steps, start_s = verify_saved({"saved": True, "criteria": criteria}) EVIDENCE["verify_saved"] = r check(r["all_passed"] and r["read"]["file"] and r["read"]["bytes"] > 1000, r) check(any(e["node"] == "/obj/bad/broken" for e in r["errors"]), r["errors"]) - r2 = program("verify_saved.py", {"saved": copy, "criteria": criteria[:1], "render": {"size": 128}}, job="verify.py") + # Started, not waited for, on the main thread; its log says each step. + check(start_s < 3, start_s) + check([s["step"] for s in steps][:4] == ["load", "loaded", "criterion", "criterion"] and steps[-1]["step"] == "done", steps) + r2, _, _ = verify_saved({"saved": copy, "criteria": criteria[:1], "render": {"size": 128}}) EVIDENCE["verify_saved_render"] = dict(r2, image_base64=len(r2.get("image_base64") or "")) check(r2["all_passed"] and r2.get("image_base64"), r2) EVIDENCE["verify_saved_seconds"] = {"criteria_and_errors": r["read"]["seconds"], "with_render": r2["read"]["seconds"]} +@test +def verify_saved_says_what_was_read_from_cache(): + # Sol ex03 retrial: a Vellum scene with its 48 cached frames. verify saved says which geometry it read from the + # cache files and which it cooked (a solver read directly simulates), and its error scan cooks no solver inside. + reset_scene() + geo = hou.node("/obj").createNode("geo", "sim") + grid = geo.createNode("grid") + solver = geo.createNode("solver", "solver1") + solver.setInput(0, grid) + cached = geo.createNode("file", "cached") + cached.parm("file").set("$HIP/cache/sim.$F4.bgeo.sc") + cached.setDisplayFlag(True) + cached.setRenderFlag(True) + hip = os.path.join(WORK, "sim", "sim.hipnc") + os.makedirs(os.path.dirname(hip), exist_ok=True) + hou.hipFile.save(hip) + rop = hou.node("/out").createNode("geometry", "cache") + rop.parm("soppath").set(solver.path()) + rop.parm("sopoutput").set("$HIP/cache/sim.$F4.bgeo.sc") + rop.parm("trange").set(1) + rop.parmTuple("f").set((1, 5, 1)) + rop.render() + hou.hipFile.save(hip) + criteria = [ + {"label": "5 cached frames", "expr": "[len(node('/obj/sim/cached').geometryAtFrame(f).points()) for f in range(1, 6)]", + "op": "==", "value": [100] * 5}, + {"label": "the solver itself", "expr": "npoints('/obj/sim/solver1')", "op": "==", "value": 100, "frame": 3}, + ] + r, steps, _ = verify_saved({"saved": True, "criteria": criteria, "frame": 5, "roots": ["/obj/sim"]}) + EVIDENCE["verify_saved_read"] = {"read": r["facts"].get("read"), "cooked": r["facts"]["error_scan"]["cooked"], + "criteria": [(c["label"], c["passed"], c["seconds"]) for c in r["criteria"]]} + check(r["all_passed"], r["criteria"]) + read = {entry["node"]: entry for entry in r["facts"]["read"]} + check(read["/obj/sim/cached"]["from"] == "files on disk" and read["/obj/sim/cached"]["files"] == "$HIP/cache/sim.$F4.bgeo.sc", read) + check(read["/obj/sim/solver1"]["from"].startswith("cooked: simulated"), read) + check(all(c["seconds"] >= 0 for c in r["criteria"]), r["criteria"]) + check(not any(p.startswith("/obj/sim/solver1/") for p in r["facts"]["error_scan"]["cooked"]), r["facts"]["error_scan"]) + check([s.get("label") for s in steps if s["step"] == "criterion"] == ["5 cached frames", "the solver itself"], steps) + + # --- render and PDG -------------------------------------------------------------------------------------------- def wait_job(started, timeout=600): """Follow the log as the plugin's task tracker does: markers, then the process's end.""" @@ -566,6 +629,30 @@ def call(server, method, params): return reply[0] +@test +def host_execute_code_reloading_the_open_file(): + # Sol ex03 retrial: execute_code loaded the open (saved) file again and moved the frame; the main thread stayed + # busy for 14 minutes and every later call, houdini_verify saved: true included, waited behind it. + host, server = served() + try: + geo = hou.node("/obj").createNode("geo", "reloaded") + geo.createNode("box").setDisplayFlag(True) + hip = os.path.join(WORK, "reload", "reload.hipnc").replace("\\", "/") + os.makedirs(os.path.dirname(hip), exist_ok=True) + hou.hipFile.save(hip) + handle = call(server, "hello", {})["result"]["script_handle"] + started = time.time() + reply = call(server, "execute_code", {"script_handle": handle, "code": + "hou.hipFile.load(hou.hipFile.path(), suppress_save_prompt=True)\nhou.setFrame(1)\nhou.setFrame(4)\n" + "result = {'path': hou.hipFile.path(), 'frame': hou.frame()}"}) + EVIDENCE["host_reload"] = {"seconds": round(time.time() - started, 1), "reply": reply} + check(time.time() - started < 30, EVIDENCE["host_reload"]) + check("result" in reply, reply) + finally: + server.close() + host.close() + + @test def host_execute_code_undo_and_hints(): host, server = served() diff --git a/integrations/houdini/test/saved-verify.test.ts b/integrations/houdini/test/saved-verify.test.ts new file mode 100644 index 0000000..ab7c3c6 --- /dev/null +++ b/integrations/houdini/test/saved-verify.test.ts @@ -0,0 +1,110 @@ +// houdini_verify saved follows its background hython outside Houdini's main thread: a result, a crash, or +// timeout_s, after which it says where the hython was (Sol ex03 retrial: a verify waited 10 minutes and said only +// that Houdini was busy). + +import assert from "node:assert/strict" +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { it } from "node:test" + +import { type SavedStart, waitSaved } from "../plugin/src/saved-verify.ts" + +const step = (what: Record) => `OPENCODE_STEP ${JSON.stringify(what)}` + +function started(log: string): SavedStart { + const work = mkdtempSync(join(tmpdir(), "houdini-saved-")) + mkdirSync(work, { recursive: true }) + writeFileSync(join(work, "verify.log"), log) + return { + pid: 4242, + work, + log: join(work, "verify.log"), + result: join(work, "result.json"), + file: "D:/shots/ex03.hipnc", + saved_at: "2026-11-16 07:25:51", + bytes: 630948, + timeout_s: 5, + } +} + +// A clock that advances with each wait: no real waiting. +function clock() { + let t = 1_000_000 + return { + now: () => t, + sleep: async (ms: number) => { + t += ms + }, + } +} + +it("a verify that runs out of time is stopped and says what it was doing", async () => { + const start = started( + [ + step({ step: "load", file: "D:/shots/ex03.hipnc", at_s: 0 }), + step({ step: "loaded", seconds: 0.4, at_s: 0.4 }), + ...Array.from({ length: 11 }, (_, i) => + step({ step: "criterion", index: i + 1, of: 16, label: `check ${i + 1}`, frame: 48, at_s: 0.5 + i * 0.1 }), + ), + ].join("\n"), + ) + const stopped: number[] = [] + try { + await assert.rejects( + waitSaved(start, { ...clock(), alive: () => true, stop: (pid) => stopped.push(pid) }), + (error: Error) => { + assert.match(error.message, /stopped the background hython after 5 s \(timeout_s\)/) + assert.match(error.message, /it was measuring criterion 11\/16 "check 11" at frame 48/) + assert.match(error.message, /The file loaded in 0\.4 s; 10 criteria were measured before\./) + assert.match(error.message, /simulates them: read its cache \(a File SOP\)/) + return true + }, + ) + assert.deepEqual(stopped, [4242]) + assert.equal(existsSync(start.work), false) + } finally { + rmSync(start.work, { recursive: true, force: true }) + } +}) + +it("a verify that finishes gives its result and what it read", async () => { + const start = started(`${step({ step: "load", at_s: 0 })}\n${step({ step: "done", at_s: 1 })}`) + writeFileSync(start.result, JSON.stringify({ passed: 16, failed: 0, load_seconds: 0.4, load_warnings: [] })) + try { + const result = (await waitSaved(start, { ...clock(), alive: () => true })) as { + passed: number + read: Record + } + assert.equal(result.passed, 16) + assert.equal(result.read.file, "D:/shots/ex03.hipnc") + assert.equal(result.read.load_seconds, 0.4) + assert.equal("load_warnings" in result.read, false) + assert.equal(existsSync(start.work), false) + } finally { + rmSync(start.work, { recursive: true, force: true }) + } +}) + +it("a verify whose hython crashed says so, with where it was", async () => { + const start = started( + [ + step({ step: "loaded", seconds: 1, at_s: 1 }), + step({ step: "render", at_s: 2 }), + "20052: Fatal error: Segmentation fault", + "CURRENT THREAD 1", + "+0x1 [RV_VKImage::getInfo] libRV.dll", + ].join("\n"), + ) + try { + await assert.rejects(waitSaved(start, { ...clock(), alive: () => false }), (error: Error) => { + assert.match( + error.message, + /crashed \(Segmentation fault in RV_VKImage::getInfo\) while rendering the check image; the open Houdini is not affected/, + ) + return true + }) + } finally { + rmSync(start.work, { recursive: true, force: true }) + } +})