From c22c27012134b54f915dff7f830edf668ba44e10 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Sat, 31 Oct 2026 11:58:50 -0500 Subject: [PATCH] fix(core): stop the JSON Schema codec cache from keeping evicted locations alive (#60800) --- packages/core/src/tool/runtime.ts | 25 +++++++++++++------------ packages/core/test/tool-schema.test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/packages/core/src/tool/runtime.ts b/packages/core/src/tool/runtime.ts index 7a65fa8345b..152b4cd3102 100644 --- a/packages/core/src/tool/runtime.ts +++ b/packages/core/src/tool/runtime.ts @@ -2,21 +2,22 @@ import { emptyInputJsonSchema, type ToolDefinition } from "@opencode/ai/effect" import { isRecord } from "@opencode/util/record" import { Tool } from "@opencode/schema/tool" import type { StandardJSONSchemaV1, StandardSchemaV1 } from "@standard-schema/spec" -import { Cache, Effect, JsonPointer, JsonSchema, Schema, SchemaIssue, SchemaRepresentation } from "effect" +import { Effect, JsonPointer, JsonSchema, Result, Schema, SchemaIssue, SchemaRepresentation } from "effect" import { $ZodType, toJSONSchema } from "zod/v4/core" const formatEffectIssues = SchemaIssue.makeFormatterStandardSchemaV1() -const jsonSchemas = Effect.runSync( - Cache.make | undefined>({ - capacity: 100, - lookup: (schema) => - Effect.try({ - try: () => jsonSchema(schema), - catch: () => undefined, - }).pipe(Effect.orElseSucceed(() => undefined)), - }), -) +// Codecs for raw JSON Schema inputs (mostly MCP tools), built once per schema object; schemas are not edited after +// registration. Not an Effect `Cache`: each entry keeps its lookup fiber, which keeps the caller's stack frames and +// through them the `Effect.fn` closures over its Location's services, so evicted Locations stayed alive. +const jsonSchemas = new WeakMap | undefined>() + +const codecFor = (schema: JsonSchema.JsonSchema) => { + if (jsonSchemas.has(schema)) return jsonSchemas.get(schema) + const codec = Result.try(() => jsonSchema(schema)).pipe(Result.getOrUndefined) + jsonSchemas.set(schema, codec) + return codec +} export const definition = (tool: Tool.Info): ToolDefinition => ({ type: "tool", @@ -74,7 +75,7 @@ const validateInput = ( ): Effect.Effect> => { if (isStandardSchema(schema)) return validateStandard(schema, value) return Effect.gen(function* () { - const codec = Schema.isSchema(schema) ? schema : yield* Cache.get(jsonSchemas, schema) + const codec = Schema.isSchema(schema) ? schema : codecFor(schema) if (codec === undefined) return { value } return yield* Schema.decodeUnknownEffect(codec)(value, { errors: "all" }).pipe( Effect.match({ diff --git a/packages/core/test/tool-schema.test.ts b/packages/core/test/tool-schema.test.ts index 3670e7ac2c3..94003ef2b92 100644 --- a/packages/core/test/tool-schema.test.ts +++ b/packages/core/test/tool-schema.test.ts @@ -531,6 +531,31 @@ test("raw JSON schemas pass input through when they cannot be imported", async ( }) }) +test("raw JSON schema validation does not keep the caller alive", async () => { + // The session runner executes tools from Effect.fn closures over its Location's services. Building the codec must + // not leave anything process-wide that reaches the caller's stack frames once the Location is gone. + const call = async () => { + const services = { name: "location services" } + const tool: Info = { + name: "raw-retention", + description: "Raw retention tool", + input: { type: "object", properties: { value: { type: "string" } }, required: ["value"] }, + execute: () => Effect.succeed({ content: services.name }), + } + const run = Effect.fn("LocationCaller.run")(function* () { + return yield* execute(tool, { value: "ok" }, context) + }) + expect(await Effect.runPromise(run())).toMatchObject({ content: [{ type: "text", text: "location services" }] }) + return new WeakRef(services) + } + const services = await call() + for (let attempt = 0; attempt < 50 && services.deref() !== undefined; attempt++) { + Bun.gc(true) + await Bun.sleep(10) + } + expect(services.deref()).toBeUndefined() +}) + test("missing external input schemas fall back to an empty schema", () => { const tool = { name: "external",