GeezerVison is showing github.com — fix(core): send execute progress at most every 100 ms (#60120) · jonBoone/opencode@b6f82bf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/codemode/tool.ts +24-3Lines changed: 24 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,7 +10,7 @@ import type { | 10 | 10 | Namespace as ToolNamespace, | 11 | 11 | Result, | 12 | 12 | } from "@opencode/schema/tool" | 13 | | -import { Context, Effect, Ref, Schema, Semaphore } from "effect" | | 13 | +import { Clock, Context, Effect, Ref, Schema, Semaphore } from "effect" | 14 | 14 | import { definition, normalizedName } from "../tool/runtime.js" | 15 | 15 | import { CodeModeCatalog } from "./catalog.js" | 16 | 16 | import { CodeModeWeb } from "./web.js" | @@ -92,9 +92,26 @@ export const create = ( | 92 | 92 | const files = yield* Ref.make<Array<CollectedFiles>>([]) | 93 | 93 | const calls = yield* Ref.make<Array<ExecuteCall>>([]) | 94 | 94 | 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 | + }) | 95 | 105 | const record = (update: (items: Array<ExecuteCall>) => Array<ExecuteCall>) => | 96 | 106 | 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 | + }), | 98 | 115 | ) | 99 | 116 | const result = yield* runtime(inventory) | 100 | 117 | .execute(code) | @@ -117,6 +134,10 @@ export const create = ( | 117 | 134 | }), | 118 | 135 | hooks: progressHooks(record), | 119 | 136 | }), | | 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 | + ), | 120 | 141 | ) | 121 | 142 | const toolCalls = yield* Ref.get(calls) | 122 | 143 | const collected = (yield* Ref.get(files)) | @@ -147,7 +168,7 @@ export const create = ( | 147 | 168 | content, | 148 | 169 | metadata, | 149 | 170 | } | 150 | | - }), | | 171 | + }).pipe(Effect.scoped), | 151 | 172 | } satisfies Info | 152 | 173 | } | 153 | 174 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/tool-execute.test.ts +82-6Lines changed: 82 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,7 +6,9 @@ import { Agent } from "@opencode/schema/agent" | 6 | 6 | import { Session } from "@opencode/schema/session" | 7 | 7 | import { SessionMessage } from "@opencode/schema/session-message" | 8 | 8 | 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" | 10 | 12 |
| 11 | 13 | const context = { | 12 | 14 | sessionID: Session.ID.make("ses_execute"), | @@ -189,14 +191,13 @@ test("executions share the prepared runtime of an inventory while keeping per-ca | 189 | 191 | } | 190 | 192 |
| 191 | 193 | 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 | + ) | 193 | 197 | expect<unknown>(results).toEqual( | 194 | 198 | ["first", "second"].map((id) => ({ | 195 | 199 | 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 } }] }], | 200 | 201 | })), | 201 | 202 | ) | 202 | 203 | expect( | @@ -210,3 +211,78 @@ test("executions share the prepared runtime of an inventory while keeping per-ca | 210 | 211 | ) | 211 | 212 | expect(conversions).toBe(2) | 212 | 213 | }) | | 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 Copy file name to clipboardExpand all lines: packages/core/test/tool-registry.test.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1225,11 +1225,11 @@ describe("Tool", () => { | 1225 | 1225 | }) | 1226 | 1226 |
| 1227 | 1227 | expect(execution).toMatchObject({ content: [{ type: "text" }] }) | | 1228 | + expect(execution.metadata?.toolCalls).toEqual([{ tool: "echo", status: "completed", input: { text: "request" } }]) | 1228 | 1229 | expect(executed).toEqual(["old:request"]) | 1229 | 1230 | expect(progress).toEqual([ | 1230 | 1231 | { toolCalls: [{ tool: "echo", status: "running", input: { text: "request" } }] }, | 1231 | 1232 | { stage: "old" }, | 1232 | | - { toolCalls: [{ tool: "echo", status: "completed", input: { text: "request" } }] }, | 1233 | 1233 | ]) | 1234 | 1234 | }), | 1235 | 1235 | ) | |
|