Skip to content

Commit c80548e

Browse files
authored
fix(core): honor plugin tool repairs in Code Mode (#63444)
1 parent 9070bff commit c80548e

17 files changed

Lines changed: 1211 additions & 275 deletions

‎packages/codemode/README.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,26 @@ runtime.execute(source) // Effect<CodeMode.Result, never, ToolServices>
9494
```
9595

9696
The Effect environment is inferred from the supplied tools. `hooks` surround every call the program makes into the
97-
host: `tool.before`/`tool.after` receive `{ name, input, key }` with decoded input and the call's `<runId>:<callId>`
97+
host: `tool.before`/`tool.after` receive `{ name, input, key }` with the call's `<runId>:<callId>`
9898
idempotency key, and `extension.before`/`extension.after` receive `{ extension, name, args }`. An `after` hook also
9999
receives how the call ended (`success` with its value, `failure` with its error, or `interrupted`). A `before` hook
100100
runs before the call reaches the host, and a failing one denies it: the host function is never called, and the program
101101
catches the failure as a thrown error. A denied call fails the way the call itself would: a tool call or asynchronous
102102
global rejects its promise, and a synchronous global throws.
103103

104+
The runtime counts a tool attempt toward `maxToolCalls` under its requested name before lookup or validation.
105+
The optional `tool.resolve` hook can rewrite the name and input, including an unknown path. Return the original
106+
name to keep the catalog's lookup and suggestions. The built-in `search` skips resolution. `tool.before` sees the
107+
decoded input. `tool.after` sees the same call object even if lookup, validation, or a hook failed; in that case
108+
it carries the last name and input reached. A veto from `tool.resolve` or `tool.before` rejects the script's call
109+
with `ToolFailure`.
110+
111+
A redirect must preserve the execution mode (plain, idempotent, or durable) because the driver chooses how to
112+
suspend the call from its requested path. Unknown paths can redirect to plain tools. For resumable calls the
113+
runtime saves the resolved name and JSON input before schema decoding. Resume reuses a completed resolution;
114+
if suspension interrupted an idempotent call during resolution, the hook runs again with the same key.
115+
Detaching to suspend leaves `tool.after` pending until the call settles.
116+
104117
### Suspending and resuming a run
105118

106119
`CodeMode.make` returns a `CodeMode.Runtime`, which can also run a program step by step:

‎packages/codemode/src/runtime/driver.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,18 @@ export class Driver<R> {
164164
call.status = "running"
165165
const request = call.request
166166
if (request.kind === "tool") {
167+
const options = {
168+
key: call.key,
169+
admit: (name: string) => this.admit(call.id, name),
170+
detaching: () => this.suspending,
171+
resolved: request.resolved,
172+
save: (name: string, input: Json | undefined) => {
173+
const current = this.vm.call(call.id)
174+
if (current) current.request = { kind: "tool", path: name.split("."), args: [input], resolved: true }
175+
},
176+
}
167177
if (this.tools.mode(request.path)?._tag === "CodeModeDurable") {
168-
const started = this.tools.start(request.path, request.args, call.key, (name) => this.admit(call.id, name))
178+
const started = this.tools.start(request.path, request.args, options)
169179
return this.fork(call.id, started, (data) => {
170180
const current = this.vm.call(call.id)
171181
if (current === undefined) return
@@ -175,14 +185,7 @@ export class Driver<R> {
175185
this.wait(current)
176186
})
177187
}
178-
return this.fork(
179-
call.id,
180-
this.tools.execute(request.path, request.args, {
181-
key: call.key,
182-
admit: (name) => this.admit(call.id, name),
183-
detaching: () => this.suspending,
184-
}),
185-
)
188+
return this.fork(call.id, this.tools.execute(request.path, request.args, options))
186189
}
187190
if (request.kind === "search") {
188191
return this.fork(

‎packages/codemode/src/scheduler.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ export type Job =
7171
* extension function's call (see `ExtensionRequest`).
7272
*/
7373
export type HostRequest =
74-
| { readonly kind: "tool"; readonly path: ReadonlyArray<string>; readonly args: Array<Json | undefined> }
74+
| {
75+
readonly kind: "tool"
76+
readonly path: ReadonlyArray<string>
77+
readonly args: Array<Json | undefined>
78+
readonly resolved?: true
79+
}
7580
| { readonly kind: "search"; readonly args: Array<Json | undefined> }
7681
| { readonly kind: "extension"; readonly call: ExtensionRequest }
7782

@@ -110,7 +115,7 @@ export class HostCall {
110115
readonly id: number,
111116
/** The call's stable idempotency key, `<runId>:<callId>`. */
112117
readonly key: string,
113-
readonly request: HostRequest,
118+
public request: HostRequest,
114119
/** What runs: a tool path, or an extension function's label. */
115120
readonly label: string,
116121
/** The running job waits for it: its outcome continues the frame that asked, before any other job. */

‎packages/codemode/src/tool-runtime.ts‎

Lines changed: 119 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export type ToolCall = {
3434
readonly name: string
3535
}
3636

37-
/** A tool call the program is making, with its decoded input. */
37+
/** A tool call the program is making. Host resolution updates its path and input; `tool.before` sees decoded input. */
3838
export type ToolInvocation = {
3939
readonly name: string
4040
readonly input: unknown
@@ -58,8 +58,16 @@ export type CallResult =
5858
| { readonly status: "failure"; readonly error: unknown }
5959
| { readonly status: "interrupted" }
6060

61+
function callResult(exit: Exit.Exit<unknown, unknown>): CallResult {
62+
if (Exit.isSuccess(exit)) return { status: "success", value: exit.value }
63+
if (Cause.hasInterruptsOnly(exit.cause)) return { status: "interrupted" }
64+
return { status: "failure", error: Cause.squash(exit.cause) }
65+
}
66+
6167
/** Hooks around every call the program makes into the host. A failing `before` denies the call. */
6268
export type Hooks<R = never> = {
69+
/** Resolve a registered or unknown tool path before validation. Redirects must preserve the call's execution mode. */
70+
readonly "tool.resolve"?: (call: ToolInvocation) => Effect.Effect<Pick<ToolInvocation, "name" | "input">, unknown, R>
6371
readonly "tool.before"?: ((call: ToolInvocation) => Effect.Effect<void, unknown, R>) | undefined
6472
readonly "tool.after"?: ((call: ToolInvocation, result: CallResult) => Effect.Effect<void, never, R>) | undefined
6573
readonly "extension.before"?: ((call: ExtensionInvocation) => Effect.Effect<void, unknown, R>) | undefined
@@ -81,9 +89,7 @@ export const hooked = <Call, A, R>(
8189
? run
8290
: Effect.onExit(run, (exit) => {
8391
if (detaching?.()) return Effect.void
84-
if (Exit.isSuccess(exit)) return after(call, { status: "success", value: exit.value })
85-
if (Cause.hasInterruptsOnly(exit.cause)) return after(call, { status: "interrupted" })
86-
return after(call, { status: "failure", error: Cause.squash(exit.cause) })
92+
return after(call, callResult(exit))
8793
})
8894
return before === undefined ? observed : Effect.andThen(before(call), observed)
8995
}
@@ -525,10 +531,12 @@ const namespaceKeys = <R>(root: ToolNode<R>, path: ReadonlyArray<string>): Reado
525531
return Array.from(node.children.keys())
526532
}
527533

528-
const resolve = <R>(root: ToolNode<R>, path: ReadonlyArray<string>, index: ReadonlyArray<SearchEntry>): Tool<R> => {
534+
const resolve = <R>(prepared: Prepared<R>, path: ReadonlyArray<string>): Tool<R> => {
535+
const root = prepared.root
529536
const segments = canonicalSegments(path)
530537
const node = lookup(root, segments)
531538
if (node === undefined) {
539+
const index = prepared.searchIndex
532540
const name = segments.join(".")
533541
const ns = segments.length > 1 && root.children.has(segments[0]) ? segments[0] : undefined
534542
const closest = rank(
@@ -568,6 +576,9 @@ export type ExecuteCallOptions = {
568576
readonly key: string
569577
readonly admit: (name: string) => void
570578
readonly detaching?: (() => boolean) | undefined
579+
/** A completed resolution saved for replay, before schema decoding. */
580+
readonly resolved?: boolean | undefined
581+
readonly save?: (name: string, input: Json | undefined) => void
571582
}
572583

573584
/** The tool bridge of one execution. Arguments arrive and results leave as JSON; program values never enter. */
@@ -594,8 +605,7 @@ export type ToolRuntime<R = never> = {
594605
readonly start: (
595606
path: ReadonlyArray<string>,
596607
args: Array<Json | undefined>,
597-
key: string,
598-
admit: (name: string) => void,
608+
options: ExecuteCallOptions,
599609
) => Effect.Effect<Json, unknown, R>
600610
/**
601611
* Waits for a durable tool's running work: its output, as `execute` gives it. `detaching` says whether an
@@ -630,22 +640,22 @@ export const make = <R>(
630640
calls.push(call)
631641
}
632642

633-
const decode = (name: string, tool: Tool<R>, args: Array<Json | undefined>) =>
634-
Effect.gen(function* () {
635-
const normalized = args.length === 0 ? [{}] : args
636-
if (normalized.length !== 1)
637-
throw new ToolRuntimeError("InvalidToolInput", `Tool '${name}' expects at most one input object.`)
638-
const input = normalized[0]
639-
const field = typeof input === "string" ? stringParameter(tool)?.name : undefined
640-
return yield* Effect.try({
641-
try: () => decodeToolInput(tool, field === undefined ? input : { [field]: input }),
642-
catch: (cause) =>
643-
new ToolRuntimeError(
644-
"InvalidToolInput",
645-
`Invalid input for tool '${name}': ${String(cause)}`,
646-
name === "search" ? [] : ["The signature may have changed. Use search to get the current signature."],
647-
),
648-
})
643+
const argument = (args: Array<Json | undefined>) => (args.length === 0 ? {} : args[0])
644+
645+
const wrap = (tool: Tool<R> | undefined, input: unknown) => {
646+
const field = tool && typeof input === "string" ? stringParameter(tool)?.name : undefined
647+
return field === undefined ? input : { [field]: input }
648+
}
649+
650+
const decode = (name: string, tool: Tool<R>, input: unknown) =>
651+
Effect.try({
652+
try: () => decodeToolInput(tool, wrap(tool, input)),
653+
catch: (cause) =>
654+
new ToolRuntimeError(
655+
"InvalidToolInput",
656+
`Invalid input for tool '${name}': ${String(cause)}`,
657+
name === "search" ? [] : ["The signature may have changed. Use search to get the current signature."],
658+
),
649659
})
650660

651661
// A tool's own failure becomes a ToolError; interruption passes through.
@@ -673,26 +683,76 @@ export const make = <R>(
673683
catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid output from tool '${name}': ${cause}`),
674684
})
675685

676-
const executeTool = (name: string, tool: Tool<R>, args: Array<Json | undefined>, options: ExecuteCallOptions) =>
686+
const invoke = (tool: Tool<R>, call: ToolInvocation) =>
677687
Effect.gen(function* () {
678688
if ("_tag" in tool.execute && tool.execute._tag === "CodeModeDurable") {
679-
throw new TypeError(`Tool '${name}' is a durable tool; start it with ToolRuntime.start.`)
689+
throw new TypeError(`Tool '${call.name}' is a durable tool; start it with ToolRuntime.start.`)
680690
}
681691
const run = "_tag" in tool.execute ? tool.execute.execute : tool.execute
682-
const key = options.key
683-
const input = yield* decode(name, tool, args)
684-
yield* Effect.sync(() => options.admit(name))
685-
return yield* hooked(
686-
{ name, input, key },
687-
hooks["tool.before"],
688-
hooks["tool.after"],
692+
const raw = yield* failure(Effect.suspend(() => run(call.input, { key: call.key })))
693+
return yield* output(call.name, tool, raw)
694+
})
695+
696+
const mode = (tool: Tool<R> | undefined) => (tool && "_tag" in tool.execute ? tool.execute : undefined)
697+
698+
const attempt = <A>(
699+
path: ReadonlyArray<string>,
700+
args: Array<Json | undefined>,
701+
options: ExecuteCallOptions,
702+
run: (tool: Tool<R>, call: ToolInvocation) => Effect.Effect<A, unknown, R>,
703+
search = false,
704+
) => {
705+
const name = path.join(".")
706+
const original = search ? makeSearchTool(prepared.searchIndex) : lookup(root, path)?.tool
707+
const execution = mode(original)
708+
const rewrite = search || options.resolved ? undefined : hooks["tool.resolve"]
709+
return Effect.acquireUseRelease(
710+
Effect.sync(() => {
711+
options.admit(name)
712+
const input: unknown = args.length > 1 ? args : argument(args)
713+
return { name, key: options.key, input }
714+
}),
715+
(call) =>
689716
Effect.gen(function* () {
690-
const raw = yield* failure(Effect.suspend(() => run(input, { key })))
691-
return yield* output(name, tool, raw)
717+
if (args.length > 1)
718+
throw new ToolRuntimeError("InvalidToolInput", `Tool '${name}' expects at most one input object.`)
719+
// A replay must read the saved JSON even if a hook or the executor edits its input in place.
720+
if (execution) call.input = structuredClone(call.input)
721+
if (rewrite) {
722+
const target = yield* failure(Effect.suspend(() => rewrite(call)))
723+
call.name = target.name
724+
call.input = target.input
725+
}
726+
const tool = (call.name === name ? original : undefined) ?? resolve(prepared, [call.name])
727+
if (execution?._tag !== mode(tool)?._tag)
728+
throw new ToolRuntimeError(
729+
"InvalidToolInput",
730+
`Cannot redirect '${name}' to '${call.name}' across execution modes.`,
731+
)
732+
if (rewrite && execution) {
733+
if (call.input !== undefined && !Schema.is(Schema.Json)(call.input))
734+
throw new ToolRuntimeError(
735+
"InvalidToolInput",
736+
`Resolved input for resumable tool '${call.name}' must be JSON.`,
737+
)
738+
options.save?.(call.name, structuredClone(call.input))
739+
}
740+
call.input = yield* decode(call.name, tool, call.input)
741+
const before = hooks["tool.before"]
742+
if (before) yield* failure(Effect.suspend(() => before(call)))
743+
return yield* run(tool, call)
692744
}),
693-
options.detaching,
694-
)
695-
})
745+
(call, exit) => {
746+
if (options.detaching?.()) return Effect.void
747+
// A durable start hands settlement to wait, including after resume.
748+
if (execution?._tag === "CodeModeDurable" && Exit.isSuccess(exit)) {
749+
if (hooks["tool.after"]) invocations.set(call.key, call)
750+
return Effect.void
751+
}
752+
return hooks["tool.after"]?.(call, callResult(exit)) ?? Effect.void
753+
},
754+
)
755+
}
696756

697757
const durableTool = (
698758
path: ReadonlyArray<string>,
@@ -716,55 +776,31 @@ export const make = <R>(
716776
invocations,
717777
record: (name) => recordCall({ name }),
718778
keys: (path) => namespaceKeys(root, path),
719-
search: (args, options) =>
720-
Effect.suspend(() => executeTool("search", makeSearchTool(prepared.searchIndex), args, options)),
779+
search: (args, options) => attempt(["search"], args, options, invoke, true),
721780
execute: (path, args, options) =>
722781
Effect.suspend(() => {
723782
const segments = canonicalSegments(path)
724783
// Models often write `tools.search(...)` for the bare `search(...)`; honor it unless a tool owns that path.
725-
if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined)
726-
return executeTool("search", makeSearchTool(prepared.searchIndex), args, options)
727-
return executeTool(segments.join("."), resolve(root, path, prepared.searchIndex), args, options)
728-
}),
729-
mode: (path) => {
730-
const segments = canonicalSegments(path)
731-
if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined) return undefined
732-
const execute = lookup(root, segments)?.tool?.execute
733-
return execute !== undefined && "_tag" in execute ? execute : undefined
734-
},
735-
start: (path, args, key, admit) =>
736-
Effect.suspend(() => {
737-
const found = durableTool(path)
738-
return Effect.gen(function* () {
739-
const input = yield* decode(found.name, found.tool, args)
740-
yield* Effect.sync(() => admit(found.name))
741-
const invocation: ToolInvocation = { name: found.name, input, key }
742-
invocations.set(key, invocation)
743-
const before = hooks["tool.before"]
744-
const after = hooks["tool.after"]
745-
const run = failure(Effect.suspend(() => found.durable.start(input, { key }))).pipe(
746-
Effect.flatMap((raw) =>
747-
Effect.try({
748-
try: (): Json => {
749-
const text = JSON.stringify(raw)
750-
return text === undefined ? null : JSON.parse(text)
751-
},
752-
catch: (cause) =>
753-
new ToolRuntimeError("InvalidToolOutput", `Invalid start data from tool '${found.name}': ${cause}`),
754-
}),
755-
),
756-
)
757-
const observed =
758-
after === undefined
759-
? run
760-
: Effect.onError(run, (cause) => {
761-
invocations.delete(key)
762-
if (Cause.hasInterruptsOnly(cause)) return after(invocation, { status: "interrupted" })
763-
return after(invocation, { status: "failure", error: Cause.squash(cause) })
764-
})
765-
return yield* before === undefined ? observed : Effect.andThen(before(invocation), observed)
766-
})
784+
const search = segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined
785+
return attempt(segments, args, options, invoke, search)
767786
}),
787+
mode: (path) => mode(lookup(root, canonicalSegments(path))?.tool),
788+
start: (path, args, options) =>
789+
attempt(canonicalSegments(path), args, options, (tool, call) =>
790+
Effect.gen(function* () {
791+
const durable = mode(tool)
792+
if (durable?._tag !== "CodeModeDurable") throw new TypeError(`Tool '${call.name}' is not a durable tool.`)
793+
const raw = yield* failure(Effect.suspend(() => durable.start(call.input, { key: call.key })))
794+
return yield* Effect.try({
795+
try: (): Json => {
796+
const text = JSON.stringify(raw)
797+
return text === undefined ? null : JSON.parse(text)
798+
},
799+
catch: (cause) =>
800+
new ToolRuntimeError("InvalidToolOutput", `Invalid start data from tool '${call.name}': ${cause}`),
801+
})
802+
}),
803+
),
768804
wait: (path, args, key, data, detaching) =>
769805
Effect.suspend(() => {
770806
const found = durableTool(path)
@@ -780,7 +816,7 @@ export const make = <R>(
780816
known !== undefined
781817
? Effect.succeed(known)
782818
: Effect.map(
783-
decode(found.name, found.tool, args),
819+
decode(found.name, found.tool, argument(args)),
784820
(input): ToolInvocation => ({
785821
name: found.name,
786822
input,
@@ -791,9 +827,7 @@ export const make = <R>(
791827
Effect.onExit(run, (exit) => {
792828
if (detaching()) return Effect.void
793829
invocations.delete(key)
794-
if (Exit.isSuccess(exit)) return after(call, { status: "success", value: exit.value })
795-
if (Cause.hasInterruptsOnly(exit.cause)) return after(call, { status: "interrupted" })
796-
return after(call, { status: "failure", error: Cause.squash(exit.cause) })
830+
return after(call, callResult(exit))
797831
}),
798832
)
799833
}),

0 commit comments

Comments
 (0)