feat(core): save fetch responses across a suspended Code Mode run with snapshot hooks

This commit is contained in:
James Long
2026-11-05 07:40:02 +00:00
Unverified
parent bc4367e979
commit e3ddb76a26
2 changed files with 167 additions and 7 deletions
+39 -7
View File
@@ -111,15 +111,40 @@ const redirected = (request: Init, status: number, from: string, to: URL): Init
const read = async (result: { response: Response; url: string; redirects: number }) => {
const response = result.response
const bytes = await response.bytes()
const headers = Object.fromEntries(response.headers)
const text = () => new TextDecoder().decode(bytes)
return {
return made({
url: result.url,
status: response.status,
statusText: response.statusText,
ok: response.ok,
redirected: result.redirects > 0,
headers: Array.from(response.headers),
body: await response.bytes(),
})
}
/** A response as plain data, which a suspended run saves it as. */
type Saved = {
readonly url: string
readonly status: number
readonly statusText: string
readonly redirected: boolean
readonly headers: ReadonlyArray<readonly [string, string]>
readonly body: Uint8Array
}
// The data a response was made from, out of the program's sight: it copies only string keys.
const saved = Symbol("saved")
// The response the program holds, made from plain data, after a fetch and again when a suspended run resumes.
const made = (data: Saved) => {
const headers = Object.fromEntries(data.headers)
const text = () => new TextDecoder().decode(data.body)
return {
[saved]: data,
url: data.url,
status: data.status,
statusText: data.statusText,
ok: data.status >= 200 && data.status <= 299,
redirected: data.redirected,
headers: {
get: (name: string) => headers[name.toLowerCase()] ?? null,
has: (name: string) => name.toLowerCase() in headers,
@@ -127,11 +152,18 @@ const read = async (result: { response: Response; url: string; redirects: number
},
text: async () => text(),
json: async () => JSON.parse(text()) as unknown,
bytes: async () => bytes,
bytes: async () => data.body,
}
}
export const extension = Extension.make({ name: "fetch", globals: { fetch: Extension.async(fetch) } })
export const extension = Extension.make({
name: "fetch",
globals: { fetch: Extension.async(fetch) },
snapshot: {
encode: (value) => (value as { readonly [saved]: Saved })[saved],
decode: (data) => made(data as Saved),
},
})
/** What to show for a fetch call: its method and URL. */
export const display = (args: ReadonlyArray<unknown>) => {
+128
View File
@@ -0,0 +1,128 @@
import { afterAll, beforeAll, expect, test } from "bun:test"
import { CodeMode, Tool } from "@opencode/codemode"
import { CodeModeExecution } from "@opencode/core/codemode/execution"
import { CodeModeFetch } from "@opencode/core/codemode/fetch"
import { Permission } from "@opencode/core/permission"
import { Agent } from "@opencode/schema/agent"
import { Session } from "@opencode/schema/session"
import { SessionMessage } from "@opencode/schema/session-message"
import { CallID } from "@opencode/schema/tool"
import { Effect, Schema } from "effect"
import { permissionService } from "../lib/permission"
// A response the program holds is saved through the fetch extension's snapshot hooks.
const context = {
sessionID: Session.ID.make("ses_fetch"),
agent: Agent.ID.make("build"),
messageID: SessionMessage.ID.make("msg_fetch"),
id: CallID.make("call_fetch"),
progress: () => Effect.void,
}
let server: ReturnType<typeof Bun.serve>
let release = Promise.withResolvers<void>()
beforeAll(() => {
server = Bun.serve({
port: 0,
fetch: async (request) => {
if (new URL(request.url).pathname === "/slow") await release.promise
return new Response(`{"page":1}`, { status: 201, headers: { "x-id": "7" } })
},
})
})
afterAll(() => server.stop(true))
// A durable tool whose work never ends on its own, so a run can suspend while it runs.
let begun = Promise.withResolvers<void>()
const job = Tool.make({
description: "Runs a job",
input: Schema.Struct({}),
output: Schema.Number,
durable: {
start: () => Effect.sync(() => begun.resolve()).pipe(Effect.as("job")),
attach: () => Effect.never,
cancel: () => Effect.void,
},
})
const runtime = CodeMode.make({ tools: { job }, extensions: [CodeModeFetch.extension] })
const services = <A, E>(effect: Effect.Effect<A, E, CodeModeExecution.Service | Permission.Service>) =>
effect.pipe(
Effect.provideService(CodeModeExecution.Service, context),
Effect.provideService(Permission.Service, permissionService({ assert: () => Effect.void })),
)
test("a response the program holds survives a suspend and a resume from bytes", async () => {
begun = Promise.withResolvers<void>()
const code = `
const res = await fetch("${server.url.origin}/page")
const text = res.text
await tools.job({})
return [res.status, res.statusText, res.ok, res.redirected, res.url, res.headers.get("x-id"), res.headers.has("x-id"),
await text(), (await res.json()).page, (await res.bytes()).length, Object.keys(res)]`
const suspended = await Effect.runPromise(
services(
Effect.scoped(
Effect.gen(function* () {
const execution = yield* runtime.execute(code, undefined, { runId: "f" })
yield* Effect.promise(() => begun.promise)
yield* Effect.sleep(1)
return yield* execution.suspend
}),
),
),
)
if (suspended.ok !== "suspended") throw new Error(JSON.stringify(suspended))
const resumed = await Effect.runPromise(
services(
Effect.scoped(
Effect.flatMap(
runtime.resume(CodeMode.toBytes(suspended.state), { [suspended.pending[0]!.key]: { ok: true, value: 1 } }),
(execution) => execution.result,
),
),
),
)
expect(resumed).toEqual({
ok: true,
value: [
201,
"Created",
true,
false,
`${server.url.origin}/page`,
"7",
true,
`{"page":1}`,
1,
10,
["url", "status", "statusText", "ok", "redirected", "headers", "text", "json", "bytes"],
],
toolCalls: [{ name: "job" }],
})
})
test("a fetch in flight blocks a suspend", async () => {
release = Promise.withResolvers<void>()
const error = await Effect.runPromise(
services(
Effect.scoped(
Effect.gen(function* () {
const execution = yield* runtime.execute(
`return (await fetch("${server.url.origin}/slow")).status`,
undefined,
{
runId: "g",
},
)
yield* Effect.sleep(20)
const refused = yield* Effect.flip(execution.suspend)
release.resolve()
expect(yield* execution.result).toMatchObject({ ok: true, value: 201 })
return refused
}),
),
),
)
expect(error.running).toEqual([{ key: "g:0", path: "fetch" }])
})