fix(core): send execute progress at most every 100 ms (#60120)

This commit is contained in:
Aiden Cline
2026-10-29 13:41:26 -05:00
committed by GitHub
Unverified
parent b2bd02ee71
commit b6f82bf15e
3 changed files with 107 additions and 10 deletions
+24 -3
View File
@@ -10,7 +10,7 @@ import type {
Namespace as ToolNamespace,
Result,
} from "@opencode/schema/tool"
import { Context, Effect, Ref, Schema, Semaphore } from "effect"
import { Clock, Context, Effect, Ref, Schema, Semaphore } from "effect"
import { definition, normalizedName } from "../tool/runtime.js"
import { CodeModeCatalog } from "./catalog.js"
import { CodeModeWeb } from "./web.js"
@@ -92,9 +92,26 @@ export const create = (
const files = yield* Ref.make<Array<CollectedFiles>>([])
const calls = yield* Ref.make<Array<ExecuteCall>>([])
const lock = Semaphore.makeUnsafe(1)
// Each update replaces the last, so send at most one per 100 ms; a trailing one carries the latest rows.
// A success drops a pending one when the scope closes, since the result carries the final rows.
const scope = yield* Effect.scope
const publishedAt = yield* Ref.make(Number.NEGATIVE_INFINITY)
const trailing = yield* Ref.make(false)
const publish = Effect.gen(function* () {
yield* Ref.set(trailing, false)
yield* Ref.set(publishedAt, yield* Clock.currentTimeMillis)
yield* context.progress({ toolCalls: yield* Ref.get(calls) })
})
const record = (update: (items: Array<ExecuteCall>) => Array<ExecuteCall>) =>
lock.withPermit(
Ref.updateAndGet(calls, update).pipe(Effect.tap((toolCalls) => context.progress({ toolCalls }))),
Effect.gen(function* () {
yield* Ref.update(calls, update)
if (yield* Ref.get(trailing)) return
const wait = (yield* Ref.get(publishedAt)) + 100 - (yield* Clock.currentTimeMillis)
if (wait <= 0) return yield* publish
yield* Ref.set(trailing, true)
yield* Effect.sleep(wait).pipe(Effect.andThen(lock.withPermit(publish)), Effect.forkIn(scope))
}),
)
const result = yield* runtime(inventory)
.execute(code)
@@ -117,6 +134,10 @@ export const create = (
}),
hooks: progressHooks(record),
}),
// A failed or interrupted call keeps its last progress, so send pending rows before it settles.
Effect.onError(() =>
lock.withPermit(Ref.get(trailing).pipe(Effect.flatMap((pending) => (pending ? publish : Effect.void)))),
),
)
const toolCalls = yield* Ref.get(calls)
const collected = (yield* Ref.get(files))
@@ -147,7 +168,7 @@ export const create = (
content,
metadata,
}
}),
}).pipe(Effect.scoped),
} satisfies Info
}
+82 -6
View File
@@ -6,7 +6,9 @@ import { Agent } from "@opencode/schema/agent"
import { Session } from "@opencode/schema/session"
import { SessionMessage } from "@opencode/schema/session-message"
import type { Info } from "@opencode/schema/tool"
import { Effect, Schema } from "effect"
import { Deferred, Effect, Fiber, Schema } from "effect"
import { TestClock } from "effect/testing"
import { it } from "./lib/effect"
const context = {
sessionID: Session.ID.make("ses_execute"),
@@ -189,14 +191,13 @@ test("executions share the prepared runtime of an inventory while keeping per-ca
}
expect(CodeModeTool.catalog(inventory).tools).toHaveLength(1)
const results = await Effect.runPromise(Effect.all([run("first"), run("second")], { concurrency: "unbounded" }))
const results = await Effect.runPromise(
Effect.all([run("first"), run("second")], { concurrency: "unbounded" }).pipe(Effect.provide(TestClock.layer())),
)
expect<unknown>(results).toEqual(
["first", "second"].map((id) => ({
output: `${id}:${id}`,
progress: [
{ toolCalls: [{ tool: "echo", status: "running", input: { value: id } }] },
{ toolCalls: [{ tool: "echo", status: "completed", input: { value: id } }] },
],
progress: [{ toolCalls: [{ tool: "echo", status: "running", input: { value: id } }] }],
})),
)
expect(
@@ -210,3 +211,78 @@ test("executions share the prepared runtime of an inventory while keeping per-ca
)
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")] },
])
}),
)
+1 -1
View File
@@ -1225,11 +1225,11 @@ describe("Tool", () => {
})
expect(execution).toMatchObject({ content: [{ type: "text" }] })
expect(execution.metadata?.toolCalls).toEqual([{ tool: "echo", status: "completed", input: { text: "request" } }])
expect(executed).toEqual(["old:request"])
expect(progress).toEqual([
{ toolCalls: [{ tool: "echo", status: "running", input: { text: "request" } }] },
{ stage: "old" },
{ toolCalls: [{ tool: "echo", status: "completed", input: { text: "request" } }] },
])
}),
)