GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(core): send execute progress at most every 100 ms (#60120) · jonBoone/opencode@b6f82bf

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/core
    • src/codemode
      • tool.ts
    • test
      • tool-execute.test.ts
      • tool-registry.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/core/src/codemode/tool.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/codemode/tool.ts
+24-3Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
Namespace as ToolNamespace,
1111
Result,
1212
} from "@opencode/schema/tool"
13-
import { Context, Effect, Ref, Schema, Semaphore } from "effect"
13+
import { Clock, Context, Effect, Ref, Schema, Semaphore } from "effect"
1414
import { definition, normalizedName } from "../tool/runtime.js"
1515
import { CodeModeCatalog } from "./catalog.js"
1616
import { CodeModeWeb } from "./web.js"
@@ -92,9 +92,26 @@ export const create = (
9292
const files = yield* Ref.make<Array<CollectedFiles>>([])
9393
const calls = yield* Ref.make<Array<ExecuteCall>>([])
9494
const lock = Semaphore.makeUnsafe(1)
95+
// Each update replaces the last, so send at most one per 100 ms; a trailing one carries the latest rows.
96+
// A success drops a pending one when the scope closes, since the result carries the final rows.
97+
const scope = yield* Effect.scope
98+
const publishedAt = yield* Ref.make(Number.NEGATIVE_INFINITY)
99+
const trailing = yield* Ref.make(false)
100+
const publish = Effect.gen(function* () {
101+
yield* Ref.set(trailing, false)
102+
yield* Ref.set(publishedAt, yield* Clock.currentTimeMillis)
103+
yield* context.progress({ toolCalls: yield* Ref.get(calls) })
104+
})
95105
const record = (update: (items: Array<ExecuteCall>) => Array<ExecuteCall>) =>
96106
lock.withPermit(
97-
Ref.updateAndGet(calls, update).pipe(Effect.tap((toolCalls) => context.progress({ toolCalls }))),
107+
Effect.gen(function* () {
108+
yield* Ref.update(calls, update)
109+
if (yield* Ref.get(trailing)) return
110+
const wait = (yield* Ref.get(publishedAt)) + 100 - (yield* Clock.currentTimeMillis)
111+
if (wait <= 0) return yield* publish
112+
yield* Ref.set(trailing, true)
113+
yield* Effect.sleep(wait).pipe(Effect.andThen(lock.withPermit(publish)), Effect.forkIn(scope))
114+
}),
98115
)
99116
const result = yield* runtime(inventory)
100117
.execute(code)
@@ -117,6 +134,10 @@ export const create = (
117134
}),
118135
hooks: progressHooks(record),
119136
}),
137+
// A failed or interrupted call keeps its last progress, so send pending rows before it settles.
138+
Effect.onError(() =>
139+
lock.withPermit(Ref.get(trailing).pipe(Effect.flatMap((pending) => (pending ? publish : Effect.void)))),
140+
),
120141
)
121142
const toolCalls = yield* Ref.get(calls)
122143
const collected = (yield* Ref.get(files))
@@ -147,7 +168,7 @@ export const create = (
147168
content,
148169
metadata,
149170
}
150-
}),
171+
}).pipe(Effect.scoped),
151172
} satisfies Info
152173
}
153174

Collapse file

‎packages/core/test/tool-execute.test.ts‎

Copy file name to clipboardExpand all lines: packages/core/test/tool-execute.test.ts
+82-6Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { Agent } from "@opencode/schema/agent"
66
import { Session } from "@opencode/schema/session"
77
import { SessionMessage } from "@opencode/schema/session-message"
88
import type { Info } from "@opencode/schema/tool"
9-
import { Effect, Schema } from "effect"
9+
import { Deferred, Effect, Fiber, Schema } from "effect"
10+
import { TestClock } from "effect/testing"
11+
import { it } from "./lib/effect"
1012

1113
const context = {
1214
sessionID: Session.ID.make("ses_execute"),
@@ -189,14 +191,13 @@ test("executions share the prepared runtime of an inventory while keeping per-ca
189191
}
190192

191193
expect(CodeModeTool.catalog(inventory).tools).toHaveLength(1)
192-
const results = await Effect.runPromise(Effect.all([run("first"), run("second")], { concurrency: "unbounded" }))
194+
const results = await Effect.runPromise(
195+
Effect.all([run("first"), run("second")], { concurrency: "unbounded" }).pipe(Effect.provide(TestClock.layer())),
196+
)
193197
expect<unknown>(results).toEqual(
194198
["first", "second"].map((id) => ({
195199
output: `${id}:${id}`,
196-
progress: [
197-
{ toolCalls: [{ tool: "echo", status: "running", input: { value: id } }] },
198-
{ toolCalls: [{ tool: "echo", status: "completed", input: { value: id } }] },
199-
],
200+
progress: [{ toolCalls: [{ tool: "echo", status: "running", input: { value: id } }] }],
200201
})),
201202
)
202203
expect(
@@ -210,3 +211,78 @@ test("executions share the prepared runtime of an inventory while keeping per-ca
210211
)
211212
expect(conversions).toBe(2)
212213
})
214+
215+
const progressCodeMode = (started: Deferred.Deferred<void>, wait: Effect.Effect<void>) =>
216+
createCodeMode(
217+
new Map<string, Info>([
218+
[
219+
"step",
220+
{
221+
name: "step",
222+
description: "Step",
223+
input: Schema.Struct({}),
224+
output: Schema.String,
225+
execute: () => Effect.succeed({ output: "ok" }),
226+
},
227+
],
228+
[
229+
"wait",
230+
{
231+
name: "wait",
232+
description: "Wait",
233+
input: Schema.Struct({}),
234+
output: Schema.String,
235+
execute: () => Deferred.succeed(started, undefined).pipe(Effect.andThen(wait), Effect.as({ output: "ok" })),
236+
},
237+
],
238+
]),
239+
)
240+
241+
const row = (tool: string, status: string) => ({ tool, status })
242+
243+
it.effect("execute sends at most one progress update per 100 ms with the latest rows", () =>
244+
Effect.gen(function* () {
245+
const started = yield* Deferred.make<void>()
246+
const progress: Array<Tool.Metadata> = []
247+
const fiber = yield* progressCodeMode(started, Effect.sleep("250 millis"))
248+
.execute(
249+
{ code: "for (const i of [1, 2, 3]) await tools.step({}); await tools.wait({}); return await tools.step({})" },
250+
{ ...context, progress: (update) => Effect.sync(() => progress.push(update)) },
251+
)
252+
.pipe(Effect.forkChild({ startImmediately: true }))
253+
yield* Deferred.await(started)
254+
yield* Effect.yieldNow
255+
expect(progress).toEqual([{ toolCalls: [row("step", "running")] }])
256+
257+
yield* TestClock.adjust("100 millis")
258+
const steps = [row("step", "completed"), row("step", "completed"), row("step", "completed")]
259+
expect(progress.slice(1)).toEqual([{ toolCalls: [...steps, row("wait", "running")] }])
260+
261+
// The wait ends 150 ms after the trailing update, so its row goes out at once; the last step's never does.
262+
yield* TestClock.adjust("150 millis")
263+
const result = yield* Fiber.join(fiber)
264+
yield* TestClock.adjust("1 second")
265+
expect(progress.slice(2)).toEqual([{ toolCalls: [...steps, row("wait", "completed")] }])
266+
expect(result.metadata).toEqual({ toolCalls: [...steps, row("wait", "completed"), row("step", "completed")] })
267+
}),
268+
)
269+
270+
it.effect("interrupted execute sends its latest progress rows before it settles", () =>
271+
Effect.gen(function* () {
272+
const started = yield* Deferred.make<void>()
273+
const progress: Array<Tool.Metadata> = []
274+
const fiber = yield* progressCodeMode(started, Effect.never)
275+
.execute(
276+
{ code: "await tools.step({}); await tools.step({}); return await tools.wait({})" },
277+
{ ...context, progress: (update) => Effect.sync(() => progress.push(update)) },
278+
)
279+
.pipe(Effect.forkChild({ startImmediately: true }))
280+
yield* Deferred.await(started)
281+
yield* Fiber.interrupt(fiber)
282+
yield* TestClock.adjust("1 second")
283+
expect(progress).toEqual([
284+
{ toolCalls: [row("step", "running")] },
285+
{ toolCalls: [row("step", "completed"), row("step", "completed"), row("wait", "error")] },
286+
])
287+
}),
288+
)
Collapse file

‎packages/core/test/tool-registry.test.ts‎

Copy file name to clipboardExpand all lines: packages/core/test/tool-registry.test.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,11 +1225,11 @@ describe("Tool", () => {
12251225
})
12261226

12271227
expect(execution).toMatchObject({ content: [{ type: "text" }] })
1228+
expect(execution.metadata?.toolCalls).toEqual([{ tool: "echo", status: "completed", input: { text: "request" } }])
12281229
expect(executed).toEqual(["old:request"])
12291230
expect(progress).toEqual([
12301231
{ toolCalls: [{ tool: "echo", status: "running", input: { text: "request" } }] },
12311232
{ stage: "old" },
1232-
{ toolCalls: [{ tool: "echo", status: "completed", input: { text: "request" } }] },
12331233
])
12341234
}),
12351235
)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.