mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(core): stop the JSON Schema codec cache from keeping evicted locations alive (#60800)
This commit is contained in:
committed by
GitHub
Unverified
parent
3960ebb5b9
commit
c22c270121
@@ -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<JsonSchema.JsonSchema, Schema.Codec<unknown> | 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<object, Schema.Codec<unknown> | 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<any, any>): ToolDefinition => ({
|
||||
type: "tool",
|
||||
@@ -74,7 +75,7 @@ const validateInput = (
|
||||
): Effect.Effect<StandardSchemaV1.Result<unknown>> => {
|
||||
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({
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user