Skip to content

Commit 12a04fa

Browse files
mliottaclaude
andcommitted
feat(opencode): wire registerToolSchema with feature detection
CAR upstream issue anomalyco#20 (Parslee-ai/car-releases#20) was fixed at car/673fd07 — registerToolSchema(schemaJson) on the runtime takes a serialized ToolSchema, and car-validator runs full JSON Schema validation in verifyProposal when populated. The fix is awaiting an npm release; opencode adds the wiring now behind a typeof check so: - on car-runtime 0.5.1 (current published): typeof check is false, fall through to schemaless registerTool — identical behavior to before - on a future release that exposes registerToolSchema: opencode passes the JSON Schema (built via EffectZod.toJsonSchema for built-in tools, MCP-provided inputSchema for MCP tools), and CAR validates parameter shape during pre-flight verify README "Coming next" updated. Declarative permission policies removed from the list — CAR maintainer closed the per-session scoping issue (anomalyco#19) recommending per-runtime isolation, but opencode keeps the per-instance model for cross-session memory continuity, so permissions stay inline via ctx.ask. 122/123 session tests pass; the single failure is a pre-existing flaky timing test (cancel finalizes interrupted bash tool output) that fails ~2/3 of runs on the parent commit too — not introduced here. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 4d49c5d commit 12a04fa

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎packages/opencode/src/car/index.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ type State = {
1717
memoryPath: string
1818
}
1919

20+
export interface ToolSchemaShape {
21+
readonly description: string
22+
readonly parameters: object
23+
readonly idempotent?: boolean
24+
}
25+
2026
export interface ExecuteActionInput<T> {
2127
readonly action: {
2228
readonly id: string
2329
readonly tool: string
2430
readonly parameters: Record<string, unknown>
31+
readonly schema?: ToolSchemaShape
2532
}
2633
readonly dispatch: (params: Record<string, unknown>) => Promise<T>
2734
}
@@ -128,19 +135,35 @@ export const layer = Layer.effect(
128135
}),
129136
)
130137

131-
const registerTool = Effect.fn("Car.registerTool")(function* (name: string) {
138+
const registerTool = Effect.fn("Car.registerTool")(function* (name: string, schema?: ToolSchemaShape) {
132139
const s = yield* InstanceState.get(state)
133140
if (s.registered.has(name)) return
134-
yield* Effect.tryPromise({
135-
try: () => s.rt.registerTool(name),
136-
catch: (e) => new Error(`car: registerTool ${name}: ${String(e)}`),
137-
}).pipe(Effect.orDie)
141+
const rtAny = s.rt as unknown as {
142+
registerToolSchema?: (json: string) => Promise<void>
143+
}
144+
if (schema && typeof rtAny.registerToolSchema === "function") {
145+
const schemaJson = JSON.stringify({
146+
name,
147+
description: schema.description,
148+
parameters: schema.parameters,
149+
idempotent: schema.idempotent ?? false,
150+
})
151+
yield* Effect.tryPromise({
152+
try: () => rtAny.registerToolSchema!(schemaJson),
153+
catch: (e) => new Error(`car: registerToolSchema ${name}: ${String(e)}`),
154+
}).pipe(Effect.orDie)
155+
} else {
156+
yield* Effect.tryPromise({
157+
try: () => s.rt.registerTool(name),
158+
catch: (e) => new Error(`car: registerTool ${name}: ${String(e)}`),
159+
}).pipe(Effect.orDie)
160+
}
138161
s.registered.add(name)
139162
})
140163

141164
const executeAction = Effect.fn("Car.executeAction")(function* <T>(input: ExecuteActionInput<T>) {
142165
const s = yield* InstanceState.get(state)
143-
yield* registerTool(input.action.tool)
166+
yield* registerTool(input.action.tool, input.action.schema)
144167

145168
const proposalJson = JSON.stringify({
146169
source: "opencode",

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,12 @@ NOTE: At any point in time through this workflow you should feel free to ask the
434434
{ args },
435435
)
436436
const result = yield* car.executeAction({
437-
action: { id: options.toolCallId, tool: item.id, parameters: args },
437+
action: {
438+
id: options.toolCallId,
439+
tool: item.id,
440+
parameters: args,
441+
schema: { description: item.description, parameters: schema },
442+
},
438443
dispatch: (params) => run.promise(item.execute(params as typeof args, ctx)),
439444
})
440445
const output = {
@@ -480,7 +485,15 @@ NOTE: At any point in time through this workflow you should feel free to ask the
480485
const result: Awaited<ReturnType<NonNullable<typeof execute>>> = yield* Effect.gen(function* () {
481486
yield* ctx.ask({ permission: key, metadata: {}, patterns: ["*"], always: ["*"] })
482487
return yield* car.executeAction({
483-
action: { id: opts.toolCallId, tool: key, parameters: args as Record<string, unknown> },
488+
action: {
489+
id: opts.toolCallId,
490+
tool: key,
491+
parameters: args as Record<string, unknown>,
492+
schema: {
493+
description: item.description ?? "",
494+
parameters: transformed as object,
495+
},
496+
},
484497
dispatch: (params) => execute(params as typeof args, opts),
485498
})
486499
}).pipe(

0 commit comments

Comments
 (0)