mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 21:15:57 +03:00
feat(core): run Code Mode on the bytecode VM behind experimental.codemodeVm (#62358)
This commit is contained in:
committed by
GitHub
Unverified
parent
714d653fbb
commit
7f321ab0b4
+1
@@ -2552,6 +2552,7 @@ export type ConfigEntry =
|
||||
schedules?: boolean
|
||||
artifacts?: boolean
|
||||
part_rows?: boolean
|
||||
codemode_vm?: boolean
|
||||
image_generate?: boolean | { model?: string | { providerID: string; model: string; variant?: string } }
|
||||
subagent_depth?: number
|
||||
location_idle_timeout?: string | false
|
||||
|
||||
@@ -11,7 +11,9 @@ import type {
|
||||
Namespace as ToolNamespace,
|
||||
Result,
|
||||
} from "@opencode/schema/tool"
|
||||
import { Clock, Context, Effect, Ref, Schema, Semaphore } from "effect"
|
||||
import { Clock, Context, Effect, Option, Ref, Schema, Semaphore } from "effect"
|
||||
import { Config } from "../config.js"
|
||||
import { Flag } from "../flag.js"
|
||||
import { toSessionError } from "../session/to-session-error.js"
|
||||
import { definition, normalizedName } from "../tool/runtime.js"
|
||||
import { Permission } from "../permission.js"
|
||||
@@ -71,6 +73,22 @@ export type Inventory = {
|
||||
readonly namespaces?: ReadonlyMap<string, ToolNamespace>
|
||||
}
|
||||
|
||||
/** Reads `experimental.codemode_vm` (or `OPENCODE_FLAGS`) on every call; without one, it is off. */
|
||||
const vmEnabled = Effect.gen(function* () {
|
||||
const overrides = (yield* Flag.Overrides) ?? ""
|
||||
for (const item of overrides
|
||||
.split(",")
|
||||
.map((part) => part.trim())
|
||||
.filter(Boolean)
|
||||
.toReversed()) {
|
||||
if (item === "codemode_vm" || item === "codemodeVm") return true
|
||||
if (item === "-codemode_vm" || item === "-codemodeVm") return false
|
||||
}
|
||||
const config = yield* Effect.serviceOption(Config.Service)
|
||||
if (Option.isNone(config)) return false
|
||||
return Config.latest(yield* config.value.entries(), "experimental")?.codemode_vm === true
|
||||
})
|
||||
|
||||
// Invariant model-facing guidance; the changing tool catalog is delivered through Instructions.
|
||||
const description = [
|
||||
"Run JavaScript in a confined Code Mode runtime to script tool calls and HTTP requests and compose their results.",
|
||||
@@ -118,7 +136,7 @@ export const create = (
|
||||
yield* Effect.sleep(wait).pipe(Effect.andThen(lock.withPermit(publish)), Effect.forkIn(scope))
|
||||
}),
|
||||
)
|
||||
const result = yield* runtime(inventory)
|
||||
const result = yield* runtime(inventory, (yield* vmEnabled) ? "vm" : "legacy")
|
||||
.execute(code)
|
||||
.pipe(
|
||||
Effect.provideService(Run, {
|
||||
@@ -225,7 +243,8 @@ export const catalog = (inventory: Inventory) => {
|
||||
)
|
||||
const root: CatalogNode = { children: new Map() }
|
||||
for (const namespace of inventory.namespaces?.values() ?? []) getNode(root, namespace.name).namespace = namespace
|
||||
for (const tool of runtime(inventory).catalog)
|
||||
// Either engine's runtime has the same catalog; reuse whichever exists rather than preparing the tools again.
|
||||
for (const tool of (runtimes.legacy.get(inventory) ?? runtimes.vm.get(inventory) ?? runtime(inventory)).catalog)
|
||||
getNode(root, tool.path).tool = {
|
||||
type: "tool",
|
||||
name: tool.path.split(".").at(-1) ?? tool.path,
|
||||
@@ -261,19 +280,23 @@ function renderCatalog(root: CatalogNode): ReadonlyArray<CodeModeCatalog.Tool |
|
||||
}
|
||||
|
||||
// Inventories are immutable snapshots, so tool definitions, schemas, and the search index are built
|
||||
// once per inventory and shared by its catalog and every execution.
|
||||
const runtimes = new WeakMap<Inventory, CodeMode.Runtime<Run | CodeModeExecution.Service | Permission.Service>>()
|
||||
// once per inventory and engine, and shared by its catalog and every execution.
|
||||
const runtimes = {
|
||||
legacy: new WeakMap<Inventory, CodeMode.Runtime<Run | CodeModeExecution.Service | Permission.Service>>(),
|
||||
vm: new WeakMap<Inventory, CodeMode.Runtime<Run | CodeModeExecution.Service | Permission.Service>>(),
|
||||
}
|
||||
|
||||
function runtime(inventory: Inventory) {
|
||||
const cached = runtimes.get(inventory)
|
||||
function runtime(inventory: Inventory, engine: "legacy" | "vm" = "legacy") {
|
||||
const cached = runtimes[engine].get(inventory)
|
||||
if (cached) return cached
|
||||
const tools = inventoryTools(inventory)
|
||||
const created = CodeMode.make<typeof tools, [typeof CodeModeFetch.extension]>({
|
||||
tools,
|
||||
extensions: [CodeModeFetch.extension],
|
||||
hooks: runHooks,
|
||||
engine,
|
||||
})
|
||||
runtimes.set(inventory, created)
|
||||
runtimes[engine].set(inventory, created)
|
||||
return created
|
||||
}
|
||||
|
||||
@@ -287,14 +310,20 @@ export const check = (
|
||||
limits: CodeMode.ExecutionLimits,
|
||||
executeTool: (name: string, tool: Info, input: unknown) => Effect.Effect<Result, Error>,
|
||||
) =>
|
||||
CodeMode.make({ tools: inventoryTools(inventory), hooks: runHooks, limits })
|
||||
.execute(code, bindings)
|
||||
.pipe(
|
||||
Effect.provideService(Run, {
|
||||
executeTool: (name, tool, input) => executeTool(name, tool, input).pipe(Effect.map(resultValue)),
|
||||
hooks: {},
|
||||
}),
|
||||
)
|
||||
vmEnabled.pipe(
|
||||
Effect.flatMap((vm) =>
|
||||
CodeMode.make({
|
||||
tools: inventoryTools(inventory),
|
||||
hooks: runHooks,
|
||||
limits,
|
||||
engine: vm ? "vm" : "legacy",
|
||||
}).execute(code, bindings),
|
||||
),
|
||||
Effect.provideService(Run, {
|
||||
executeTool: (name, tool, input) => executeTool(name, tool, input).pipe(Effect.map(resultValue)),
|
||||
hooks: {},
|
||||
}),
|
||||
)
|
||||
|
||||
function inventoryTools(inventory: Inventory) {
|
||||
// A path may carry namespace metadata, a callable tool, child tools, or all three.
|
||||
|
||||
@@ -465,6 +465,15 @@ function normalizeExperimental(
|
||||
const value = decodeEncoded(schema, experimental[key], ["experimental", key], diagnostics)
|
||||
if (value !== undefined) result[key] = value
|
||||
})
|
||||
if (!own(experimental, "codemode_vm") && own(experimental, "codemodeVm")) {
|
||||
const value = decodeEncoded(
|
||||
ConfigExperimental.Info.fields.codemode_vm,
|
||||
experimental.codemodeVm,
|
||||
["experimental", "codemodeVm"],
|
||||
diagnostics,
|
||||
)
|
||||
if (value !== undefined) result.codemode_vm = value
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Object.keys(result).length || (isRecord(input.experimental) && !Object.keys(input.experimental).length))
|
||||
|
||||
@@ -554,6 +554,7 @@ describe("ConfigNormalize", () => {
|
||||
background_compaction: true,
|
||||
media_blob: true,
|
||||
part_rows: true,
|
||||
codemode_vm: true,
|
||||
image_generate: { model: "openai/gpt-image-2" },
|
||||
subagent_depth: 0,
|
||||
location_idle_timeout: "2 hours",
|
||||
@@ -567,6 +568,7 @@ describe("ConfigNormalize", () => {
|
||||
background_compaction: true,
|
||||
media_blob: true,
|
||||
part_rows: true,
|
||||
codemode_vm: true,
|
||||
image_generate: { model: { providerID: "openai", model: "gpt-image-2" } },
|
||||
subagent_depth: 0,
|
||||
location_idle_timeout: "7200000 millis",
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { afterAll, beforeAll, describe, expect, test } from "bun:test"
|
||||
import { CodeModeTool } from "@opencode/core/codemode/tool"
|
||||
import { Config } from "@opencode/core/config"
|
||||
import { Permission } from "@opencode/core/permission"
|
||||
import { Tool } from "@opencode/core/tool"
|
||||
import { execute } from "@opencode/core/tool/runtime"
|
||||
import { Agent } from "@opencode/schema/agent"
|
||||
import { Document, Info as ConfigInfo } from "@opencode/schema/config"
|
||||
import { ConfigExperimental } from "@opencode/schema/config/experimental"
|
||||
import { Session } from "@opencode/schema/session"
|
||||
import { SessionMessage } from "@opencode/schema/session-message"
|
||||
import type { Info } from "@opencode/schema/tool"
|
||||
@@ -25,6 +28,14 @@ const allowAll = permissionService({ assert: () => Effect.void })
|
||||
const createCodeMode = (tools: ReadonlyMap<string, Info>, permission = allowAll) =>
|
||||
CodeModeTool.create({ tools }, (_, tool, input, context) => execute(tool, input, context), permission)
|
||||
|
||||
const vmConfig = (codemode_vm: boolean) =>
|
||||
Config.testLayer([
|
||||
new Document({
|
||||
type: "document",
|
||||
info: new ConfigInfo({ experimental: new ConfigExperimental.Info({ codemode_vm }) }),
|
||||
}),
|
||||
])
|
||||
|
||||
test("execute describes invariant Code Mode behavior", () => {
|
||||
expect(createCodeMode(new Map()).description).toBe(
|
||||
[
|
||||
@@ -38,131 +49,6 @@ test("execute describes invariant Code Mode behavior", () => {
|
||||
)
|
||||
})
|
||||
|
||||
test("execute rejects filesystem fetches", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
createCodeMode(new Map()).execute({ code: 'return await fetch("file:///blocked.txt")' }, context),
|
||||
)
|
||||
|
||||
expect(result.output.error).toBe(true)
|
||||
expect(result.output.output).toContain("fetch cannot be used for filesystem access.")
|
||||
})
|
||||
|
||||
describe("fetch permissions", () => {
|
||||
const hits: Array<string> = []
|
||||
let server: ReturnType<typeof Bun.serve>
|
||||
beforeAll(() => {
|
||||
server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
const url = new URL(request.url)
|
||||
hits.push(`${request.method} ${url.pathname}`)
|
||||
if (url.pathname === "/redirect") return Response.redirect(`${url.origin}/blocked`, 302)
|
||||
return new Response("ok")
|
||||
},
|
||||
})
|
||||
})
|
||||
afterAll(() => server.stop(true))
|
||||
|
||||
test("fetch asks the webfetch permission for its URL as the execute call", async () => {
|
||||
hits.length = 0
|
||||
const asked: Array<Permission.AssertInput> = []
|
||||
const url = `${server.url.origin}/allowed`
|
||||
const result = await Effect.runPromise(
|
||||
createCodeMode(
|
||||
new Map(),
|
||||
permissionService({ assert: (input) => Effect.sync(() => void asked.push(input)) }),
|
||||
).execute({ code: `return await (await fetch("${url}", { method: "POST" })).text()` }, context),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe("ok")
|
||||
expect(hits).toEqual(["POST /allowed"])
|
||||
expect(asked).toEqual([
|
||||
{
|
||||
action: "webfetch",
|
||||
resources: [url],
|
||||
save: ["*"],
|
||||
metadata: { method: "POST", url },
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
test("a redirect to a URL denied for webfetch is never requested", async () => {
|
||||
hits.length = 0
|
||||
const denied = permissionService({
|
||||
assert: () => Effect.void,
|
||||
assertNotDenied: (input) =>
|
||||
Effect.fail(new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources })),
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
createCodeMode(new Map(), denied).execute(
|
||||
{ code: `try { await fetch("${server.url.origin}/redirect") } catch (e) { return e.message }` },
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toContain("Permission denied")
|
||||
expect(hits).toEqual(["GET /redirect"])
|
||||
})
|
||||
|
||||
test("fetch matches rules without the fragment and rejects credentials in the URL", async () => {
|
||||
hits.length = 0
|
||||
const asked: Array<ReadonlyArray<string>> = []
|
||||
const result = await Effect.runPromise(
|
||||
createCodeMode(
|
||||
new Map(),
|
||||
permissionService({ assert: (input) => Effect.sync(() => void asked.push(input.resources)) }),
|
||||
).execute(
|
||||
{
|
||||
code: `await fetch("${server.url.origin}/allowed#top"); try { await fetch("${server.url.protocol}//user@${server.url.host}/") } catch (e) { return e.message }`,
|
||||
},
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe("fetch cannot be used with credentials in the URL.")
|
||||
expect(asked).toEqual([[`${server.url.origin}/allowed`]])
|
||||
expect(hits).toEqual(["GET /allowed"])
|
||||
})
|
||||
|
||||
test("a rejection with feedback reaches the program as the error message", async () => {
|
||||
hits.length = 0
|
||||
const url = `${server.url.origin}/corrected`
|
||||
const corrected = permissionService({
|
||||
assert: () => Effect.fail(new Permission.CorrectedError({ feedback: "use the docs site instead" })),
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
createCodeMode(new Map(), corrected).execute(
|
||||
{ code: `try { await fetch("${url}") } catch (e) { return e.message }` },
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe(`The user rejected fetching ${url}: use the docs site instead`)
|
||||
expect(hits).toEqual([])
|
||||
})
|
||||
|
||||
test("a URL denied for webfetch is never requested and rejects like a failed fetch", async () => {
|
||||
hits.length = 0
|
||||
const url = `${server.url.origin}/denied`
|
||||
const denied = permissionService({
|
||||
assert: (input) =>
|
||||
Effect.fail(new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources })),
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
createCodeMode(new Map(), denied).execute(
|
||||
{ code: `const [denied] = await Promise.allSettled([fetch("${url}")]); return denied.status` },
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe("rejected")
|
||||
expect(hits).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
test("canonical execution distinguishes declared, model-only, and raw schema outputs", async () => {
|
||||
const declared: Info = {
|
||||
name: "declared",
|
||||
@@ -241,43 +127,311 @@ test("foreign typed failures settle as Tool.Error at the untrusted boundary", as
|
||||
expect(error.message).toBe("transport died")
|
||||
})
|
||||
|
||||
test("execute supports callable namespace tools", async () => {
|
||||
const callable: Info = {
|
||||
name: "admin",
|
||||
description: "Administer Slack",
|
||||
input: Schema.Struct({}),
|
||||
output: Schema.String,
|
||||
options: { namespace: "slack" },
|
||||
execute: () => Effect.succeed({ output: "admin" }),
|
||||
}
|
||||
const child: Info = {
|
||||
name: "create",
|
||||
description: "Create a Slack resource",
|
||||
input: Schema.Struct({}),
|
||||
output: Schema.String,
|
||||
options: { namespace: "slack.admin" },
|
||||
execute: () => Effect.succeed({ output: "created" }),
|
||||
}
|
||||
const codeMode = createCodeMode(
|
||||
new Map([
|
||||
["slack_admin", callable],
|
||||
["slack_admin_create", child],
|
||||
]),
|
||||
)
|
||||
const result = await Effect.runPromise(
|
||||
codeMode.execute({ code: "return [await tools.slack.admin({}), await tools.slack.admin.create({})]" }, context),
|
||||
describe.each([
|
||||
["experimental.codemode_vm off (legacy)", false],
|
||||
["experimental.codemode_vm on (vm)", true],
|
||||
] as const)("%s", (_, vm) => {
|
||||
const provideFlag = <A, E, R>(effect: Effect.Effect<A, E, R>) => effect.pipe(Effect.provide(vmConfig(vm)))
|
||||
const runFlagged = <A, E>(effect: Effect.Effect<A, E>) => Effect.runPromise(provideFlag(effect))
|
||||
|
||||
test("execute rejects filesystem fetches", async () => {
|
||||
const result = await runFlagged(
|
||||
createCodeMode(new Map()).execute({ code: 'return await fetch("file:///blocked.txt")' }, context),
|
||||
)
|
||||
|
||||
expect(result.output.error).toBe(true)
|
||||
expect(result.output.output).toContain("fetch cannot be used for filesystem access.")
|
||||
})
|
||||
|
||||
describe("fetch permissions", () => {
|
||||
const hits: Array<string> = []
|
||||
let server: ReturnType<typeof Bun.serve>
|
||||
beforeAll(() => {
|
||||
server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
const url = new URL(request.url)
|
||||
hits.push(`${request.method} ${url.pathname}`)
|
||||
if (url.pathname === "/redirect") return Response.redirect(`${url.origin}/blocked`, 302)
|
||||
return new Response("ok")
|
||||
},
|
||||
})
|
||||
})
|
||||
afterAll(() => server.stop(true))
|
||||
|
||||
test("fetch asks the webfetch permission for its URL as the execute call", async () => {
|
||||
hits.length = 0
|
||||
const asked: Array<Permission.AssertInput> = []
|
||||
const url = `${server.url.origin}/allowed`
|
||||
const result = await runFlagged(
|
||||
createCodeMode(
|
||||
new Map(),
|
||||
permissionService({ assert: (input) => Effect.sync(() => void asked.push(input)) }),
|
||||
).execute({ code: `return await (await fetch("${url}", { method: "POST" })).text()` }, context),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe("ok")
|
||||
expect(hits).toEqual(["POST /allowed"])
|
||||
expect(asked).toEqual([
|
||||
{
|
||||
action: "webfetch",
|
||||
resources: [url],
|
||||
save: ["*"],
|
||||
metadata: { method: "POST", url },
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
test("a redirect to a URL denied for webfetch is never requested", async () => {
|
||||
hits.length = 0
|
||||
const denied = permissionService({
|
||||
assert: () => Effect.void,
|
||||
assertNotDenied: (input) =>
|
||||
Effect.fail(new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources })),
|
||||
})
|
||||
const result = await runFlagged(
|
||||
createCodeMode(new Map(), denied).execute(
|
||||
{ code: `try { await fetch("${server.url.origin}/redirect") } catch (e) { return e.message }` },
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toContain("Permission denied")
|
||||
expect(hits).toEqual(["GET /redirect"])
|
||||
})
|
||||
|
||||
test("fetch matches rules without the fragment and rejects credentials in the URL", async () => {
|
||||
hits.length = 0
|
||||
const asked: Array<ReadonlyArray<string>> = []
|
||||
const result = await runFlagged(
|
||||
createCodeMode(
|
||||
new Map(),
|
||||
permissionService({ assert: (input) => Effect.sync(() => void asked.push(input.resources)) }),
|
||||
).execute(
|
||||
{
|
||||
code: `await fetch("${server.url.origin}/allowed#top"); try { await fetch("${server.url.protocol}//user@${server.url.host}/") } catch (e) { return e.message }`,
|
||||
},
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe("fetch cannot be used with credentials in the URL.")
|
||||
expect(asked).toEqual([[`${server.url.origin}/allowed`]])
|
||||
expect(hits).toEqual(["GET /allowed"])
|
||||
})
|
||||
|
||||
test("a rejection with feedback reaches the program as the error message", async () => {
|
||||
hits.length = 0
|
||||
const url = `${server.url.origin}/corrected`
|
||||
const corrected = permissionService({
|
||||
assert: () => Effect.fail(new Permission.CorrectedError({ feedback: "use the docs site instead" })),
|
||||
})
|
||||
const result = await runFlagged(
|
||||
createCodeMode(new Map(), corrected).execute(
|
||||
{ code: `try { await fetch("${url}") } catch (e) { return e.message }` },
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe(`The user rejected fetching ${url}: use the docs site instead`)
|
||||
expect(hits).toEqual([])
|
||||
})
|
||||
|
||||
test("a URL denied for webfetch is never requested and rejects like a failed fetch", async () => {
|
||||
hits.length = 0
|
||||
const url = `${server.url.origin}/denied`
|
||||
const denied = permissionService({
|
||||
assert: (input) =>
|
||||
Effect.fail(new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources })),
|
||||
})
|
||||
const result = await runFlagged(
|
||||
createCodeMode(new Map(), denied).execute(
|
||||
{ code: `const [denied] = await Promise.allSettled([fetch("${url}")]); return denied.status` },
|
||||
context,
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.output.output).toBe("rejected")
|
||||
expect(hits).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
test("execute supports callable namespace tools", async () => {
|
||||
const callable: Info = {
|
||||
name: "admin",
|
||||
description: "Administer Slack",
|
||||
input: Schema.Struct({}),
|
||||
output: Schema.String,
|
||||
options: { namespace: "slack" },
|
||||
execute: () => Effect.succeed({ output: "admin" }),
|
||||
}
|
||||
const child: Info = {
|
||||
name: "create",
|
||||
description: "Create a Slack resource",
|
||||
input: Schema.Struct({}),
|
||||
output: Schema.String,
|
||||
options: { namespace: "slack.admin" },
|
||||
execute: () => Effect.succeed({ output: "created" }),
|
||||
}
|
||||
const codeMode = createCodeMode(
|
||||
new Map([
|
||||
["slack_admin", callable],
|
||||
["slack_admin_create", child],
|
||||
]),
|
||||
)
|
||||
const result = await runFlagged(
|
||||
codeMode.execute({ code: "return [await tools.slack.admin({}), await tools.slack.admin.create({})]" }, context),
|
||||
)
|
||||
|
||||
expect(result.metadata).toEqual({
|
||||
toolCalls: [
|
||||
{ tool: "slack.admin", status: "completed" },
|
||||
{ tool: "slack.admin.create", status: "completed" },
|
||||
],
|
||||
})
|
||||
expect(result.content).toEqual([{ type: "text", text: '[\n "admin",\n "created"\n]' }])
|
||||
})
|
||||
|
||||
test("executions share the prepared runtime of an inventory while keeping per-call state", async () => {
|
||||
let conversions = 0
|
||||
const echo: Info = {
|
||||
name: "echo",
|
||||
description: "Echo a value",
|
||||
input: {
|
||||
"~standard": {
|
||||
version: 1,
|
||||
vendor: "test",
|
||||
validate: (value: unknown) => ({ value }),
|
||||
jsonSchema: {
|
||||
input: () => {
|
||||
conversions++
|
||||
return { type: "object", properties: { value: { type: "string" } }, required: ["value"] }
|
||||
},
|
||||
output: () => ({}),
|
||||
},
|
||||
},
|
||||
},
|
||||
execute: (input, context) => Effect.succeed({ content: `${context.id}:${(input as { value: string }).value}` }),
|
||||
}
|
||||
const inventory = { tools: new Map([["echo", echo]]) }
|
||||
const codeMode = CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context), allowAll)
|
||||
const run = (id: string) => {
|
||||
const progress: Array<Tool.Metadata> = []
|
||||
return CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context), allowAll)
|
||||
.execute(
|
||||
{ code: `return await tools.echo({ value: "${id}" })` },
|
||||
{ ...context, id: Tool.CallID.make(id), progress: (update) => Effect.sync(() => progress.push(update)) },
|
||||
)
|
||||
.pipe(Effect.map((result) => ({ output: result.output.output, progress })))
|
||||
}
|
||||
|
||||
const results = await runFlagged(
|
||||
Effect.all([run("first"), run("second")], { concurrency: "unbounded" }).pipe(Effect.provide(TestClock.layer())),
|
||||
)
|
||||
expect(CodeModeTool.catalog(inventory).tools).toHaveLength(1)
|
||||
expect<unknown>(results).toEqual(
|
||||
["first", "second"].map((id) => ({
|
||||
output: `${id}:${id}`,
|
||||
progress: [{ toolCalls: [{ tool: "echo", status: "running", input: { value: id } }] }],
|
||||
})),
|
||||
)
|
||||
expect(
|
||||
(await runFlagged(codeMode.execute({ code: 'return await tools.echo({ value: "x" })' }, context))).output.output,
|
||||
).toBe("call_execute:x")
|
||||
expect(conversions).toBe(1)
|
||||
|
||||
await runFlagged(
|
||||
createCodeMode(new Map(inventory.tools)).execute({ code: 'return await tools.echo({ value: "next" })' }, context),
|
||||
)
|
||||
expect(conversions).toBe(2)
|
||||
})
|
||||
|
||||
const progressCodeMode = (started: Deferred.Deferred<void>, wait: Effect.Effect<void>) =>
|
||||
createCodeMode(
|
||||
new Map<string, Info>([
|
||||
[
|
||||
"step",
|
||||
{
|
||||
name: "step",
|
||||
description: "Step",
|
||||
input: Schema.Struct({}),
|
||||
output: Schema.String,
|
||||
execute: () => Effect.succeed({ output: "ok" }),
|
||||
},
|
||||
],
|
||||
[
|
||||
"wait",
|
||||
{
|
||||
name: "wait",
|
||||
description: "Wait",
|
||||
input: Schema.Struct({}),
|
||||
output: Schema.String,
|
||||
execute: () => Deferred.succeed(started, undefined).pipe(Effect.andThen(wait), Effect.as({ output: "ok" })),
|
||||
},
|
||||
],
|
||||
]),
|
||||
)
|
||||
|
||||
const row = (tool: string, status: string) => ({ tool, status })
|
||||
|
||||
it.effect("execute sends at most one progress update per 100 ms with the latest rows", () =>
|
||||
provideFlag(
|
||||
Effect.gen(function* () {
|
||||
const started = yield* Deferred.make<void>()
|
||||
const progress: Array<Tool.Metadata> = []
|
||||
const fiber = yield* progressCodeMode(started, Effect.sleep("250 millis"))
|
||||
.execute(
|
||||
{
|
||||
code: "for (const i of [1, 2, 3]) await tools.step({}); await tools.wait({}); return await tools.step({})",
|
||||
},
|
||||
{ ...context, progress: (update) => Effect.sync(() => progress.push(update)) },
|
||||
)
|
||||
.pipe(Effect.forkChild({ startImmediately: true }))
|
||||
yield* Deferred.await(started)
|
||||
yield* Effect.yieldNow
|
||||
expect(progress).toEqual([{ toolCalls: [row("step", "running")] }])
|
||||
|
||||
yield* TestClock.adjust("100 millis")
|
||||
const steps = [row("step", "completed"), row("step", "completed"), row("step", "completed")]
|
||||
expect(progress.slice(1)).toEqual([{ toolCalls: [...steps, row("wait", "running")] }])
|
||||
|
||||
// The wait ends 150 ms after the trailing update, so its row goes out at once; the last step's never does.
|
||||
yield* TestClock.adjust("150 millis")
|
||||
const result = yield* Fiber.join(fiber)
|
||||
yield* TestClock.adjust("1 second")
|
||||
expect(progress.slice(2)).toEqual([{ toolCalls: [...steps, row("wait", "completed")] }])
|
||||
expect(result.metadata).toEqual({ toolCalls: [...steps, row("wait", "completed"), row("step", "completed")] })
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
expect(result.metadata).toEqual({
|
||||
toolCalls: [
|
||||
{ tool: "slack.admin", status: "completed" },
|
||||
{ tool: "slack.admin.create", status: "completed" },
|
||||
],
|
||||
})
|
||||
expect(result.content).toEqual([{ type: "text", text: '[\n "admin",\n "created"\n]' }])
|
||||
it.effect("interrupted execute sends its latest progress rows before it settles", () =>
|
||||
provideFlag(
|
||||
Effect.gen(function* () {
|
||||
const started = yield* Deferred.make<void>()
|
||||
const progress: Array<Tool.Metadata> = []
|
||||
const fiber = yield* progressCodeMode(started, Effect.never)
|
||||
.execute(
|
||||
{ code: "await tools.step({}); await tools.step({}); return await tools.wait({})" },
|
||||
{ ...context, progress: (update) => Effect.sync(() => progress.push(update)) },
|
||||
)
|
||||
.pipe(Effect.forkChild({ startImmediately: true }))
|
||||
yield* Deferred.await(started)
|
||||
yield* Fiber.interrupt(fiber)
|
||||
yield* TestClock.adjust("1 second")
|
||||
expect(progress).toEqual([
|
||||
{ toolCalls: [row("step", "running")] },
|
||||
{ toolCalls: [row("step", "completed"), row("step", "completed"), row("wait", "error")] },
|
||||
])
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
test("executions share the prepared runtime of an inventory while keeping per-call state", async () => {
|
||||
test("runtime cache is keyed per engine when experimental.codemode_vm toggles", async () => {
|
||||
let conversions = 0
|
||||
const echo: Info = {
|
||||
name: "echo",
|
||||
@@ -296,113 +450,30 @@ test("executions share the prepared runtime of an inventory while keeping per-ca
|
||||
},
|
||||
},
|
||||
},
|
||||
execute: (input, context) => Effect.succeed({ content: `${context.id}:${(input as { value: string }).value}` }),
|
||||
execute: (input) => Effect.succeed({ content: (input as { value: string }).value }),
|
||||
}
|
||||
const inventory = { tools: new Map([["echo", echo]]) }
|
||||
const codeMode = CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context), allowAll)
|
||||
const run = (id: string) => {
|
||||
const progress: Array<Tool.Metadata> = []
|
||||
return CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context), allowAll)
|
||||
.execute(
|
||||
{ code: `return await tools.echo({ value: "${id}" })` },
|
||||
{ ...context, id: Tool.CallID.make(id), progress: (update) => Effect.sync(() => progress.push(update)) },
|
||||
)
|
||||
.pipe(Effect.map((result) => ({ output: result.output.output, progress })))
|
||||
}
|
||||
|
||||
expect(CodeModeTool.catalog(inventory).tools).toHaveLength(1)
|
||||
const results = await Effect.runPromise(
|
||||
Effect.all([run("first"), run("second")], { concurrency: "unbounded" }).pipe(Effect.provide(TestClock.layer())),
|
||||
// Legacy engine (flag off) builds and reuses the legacy runtime
|
||||
await Effect.runPromise(
|
||||
codeMode.execute({ code: 'return await tools.echo({ value: "a" })' }, context).pipe(Effect.provide(vmConfig(false))),
|
||||
)
|
||||
expect<unknown>(results).toEqual(
|
||||
["first", "second"].map((id) => ({
|
||||
output: `${id}:${id}`,
|
||||
progress: [{ toolCalls: [{ tool: "echo", status: "running", input: { value: id } }] }],
|
||||
})),
|
||||
await Effect.runPromise(
|
||||
codeMode.execute({ code: 'return await tools.echo({ value: "b" })' }, context).pipe(Effect.provide(vmConfig(false))),
|
||||
)
|
||||
expect(
|
||||
(await Effect.runPromise(codeMode.execute({ code: 'return await tools.echo({ value: "x" })' }, context))).output
|
||||
.output,
|
||||
).toBe("call_execute:x")
|
||||
expect(conversions).toBe(1)
|
||||
|
||||
// VM engine (flag on) builds and reuses its own runtime instance for the same inventory
|
||||
await Effect.runPromise(
|
||||
createCodeMode(new Map(inventory.tools)).execute({ code: 'return await tools.echo({ value: "next" })' }, context),
|
||||
codeMode.execute({ code: 'return await tools.echo({ value: "c" })' }, context).pipe(Effect.provide(vmConfig(true))),
|
||||
)
|
||||
await Effect.runPromise(
|
||||
codeMode.execute({ code: 'return await tools.echo({ value: "d" })' }, context).pipe(Effect.provide(vmConfig(true))),
|
||||
)
|
||||
expect(conversions).toBe(2)
|
||||
|
||||
// Reading the catalog reuses the cached runtime without rebuilding
|
||||
expect(CodeModeTool.catalog(inventory).tools).toHaveLength(1)
|
||||
expect(conversions).toBe(2)
|
||||
})
|
||||
|
||||
const progressCodeMode = (started: Deferred.Deferred<void>, wait: Effect.Effect<void>) =>
|
||||
createCodeMode(
|
||||
new Map<string, Info>([
|
||||
[
|
||||
"step",
|
||||
{
|
||||
name: "step",
|
||||
description: "Step",
|
||||
input: Schema.Struct({}),
|
||||
output: Schema.String,
|
||||
execute: () => Effect.succeed({ output: "ok" }),
|
||||
},
|
||||
],
|
||||
[
|
||||
"wait",
|
||||
{
|
||||
name: "wait",
|
||||
description: "Wait",
|
||||
input: Schema.Struct({}),
|
||||
output: Schema.String,
|
||||
execute: () => Deferred.succeed(started, undefined).pipe(Effect.andThen(wait), Effect.as({ output: "ok" })),
|
||||
},
|
||||
],
|
||||
]),
|
||||
)
|
||||
|
||||
const row = (tool: string, status: string) => ({ tool, status })
|
||||
|
||||
it.effect("execute sends at most one progress update per 100 ms with the latest rows", () =>
|
||||
Effect.gen(function* () {
|
||||
const started = yield* Deferred.make<void>()
|
||||
const progress: Array<Tool.Metadata> = []
|
||||
const fiber = yield* progressCodeMode(started, Effect.sleep("250 millis"))
|
||||
.execute(
|
||||
{ code: "for (const i of [1, 2, 3]) await tools.step({}); await tools.wait({}); return await tools.step({})" },
|
||||
{ ...context, progress: (update) => Effect.sync(() => progress.push(update)) },
|
||||
)
|
||||
.pipe(Effect.forkChild({ startImmediately: true }))
|
||||
yield* Deferred.await(started)
|
||||
yield* Effect.yieldNow
|
||||
expect(progress).toEqual([{ toolCalls: [row("step", "running")] }])
|
||||
|
||||
yield* TestClock.adjust("100 millis")
|
||||
const steps = [row("step", "completed"), row("step", "completed"), row("step", "completed")]
|
||||
expect(progress.slice(1)).toEqual([{ toolCalls: [...steps, row("wait", "running")] }])
|
||||
|
||||
// The wait ends 150 ms after the trailing update, so its row goes out at once; the last step's never does.
|
||||
yield* TestClock.adjust("150 millis")
|
||||
const result = yield* Fiber.join(fiber)
|
||||
yield* TestClock.adjust("1 second")
|
||||
expect(progress.slice(2)).toEqual([{ toolCalls: [...steps, row("wait", "completed")] }])
|
||||
expect(result.metadata).toEqual({ toolCalls: [...steps, row("wait", "completed"), row("step", "completed")] })
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("interrupted execute sends its latest progress rows before it settles", () =>
|
||||
Effect.gen(function* () {
|
||||
const started = yield* Deferred.make<void>()
|
||||
const progress: Array<Tool.Metadata> = []
|
||||
const fiber = yield* progressCodeMode(started, Effect.never)
|
||||
.execute(
|
||||
{ code: "await tools.step({}); await tools.step({}); return await tools.wait({})" },
|
||||
{ ...context, progress: (update) => Effect.sync(() => progress.push(update)) },
|
||||
)
|
||||
.pipe(Effect.forkChild({ startImmediately: true }))
|
||||
yield* Deferred.await(started)
|
||||
yield* Fiber.interrupt(fiber)
|
||||
yield* TestClock.adjust("1 second")
|
||||
expect(progress).toEqual([
|
||||
{ toolCalls: [row("step", "running")] },
|
||||
{ toolCalls: [row("step", "completed"), row("step", "completed"), row("wait", "error")] },
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
Generated
+3
@@ -16676,6 +16676,9 @@
|
||||
"part_rows": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"codemode_vm": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"image_generate": {
|
||||
"anyOf": [
|
||||
{
|
||||
|
||||
@@ -23,6 +23,10 @@ export class Info extends Schema.Class<Info>("ConfigExperimental.Info")({
|
||||
description:
|
||||
"Write each assistant part as its own session_part row instead of rewriting the whole message on every event. Defaults to false.",
|
||||
}),
|
||||
codemode_vm: Schema.Boolean.pipe(optional).annotate({
|
||||
description:
|
||||
"Run the Code Mode execute tool on the experimental bytecode VM instead of the legacy interpreter. Defaults to false.",
|
||||
}),
|
||||
image_generate: Schema.Union([
|
||||
Schema.Boolean,
|
||||
Schema.Struct({
|
||||
|
||||
Reference in New Issue
Block a user