-
Notifications
You must be signed in to change notification settings - Fork 14
perf(opencode): throttle shell tool metadata streaming #1049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { Duration, Effect, Schedule, Semaphore, type Scope } from "effect" | ||
|
|
||
| export interface MetadataThrottleOptions { | ||
| intervalMillis: number | ||
| byteThreshold: number | ||
| snapshot: () => string | ||
| emit: (output: string) => Effect.Effect<void> | ||
| } | ||
|
|
||
| export interface MetadataThrottle { | ||
| onChunk: (size: number) => Effect.Effect<void> | ||
| flush: (reason: "spill" | "final") => Effect.Effect<void> | ||
| } | ||
|
|
||
| // Coalesces the shell tool's per-chunk metadata pushes. Without this, every | ||
| // decoded chunk fires a full part update + message.part.updated event, which is | ||
| // the dominant per-call cost for chatty commands. Emissions run through a single | ||
| // serialized channel so the timer fiber and the stream consumer never interleave | ||
| // writes — each emit ships the full preview, so an out-of-order write could let a | ||
| // stale preview overwrite a newer one downstream. | ||
| export const makeMetadataThrottle = ( | ||
| options: MetadataThrottleOptions, | ||
| ): Effect.Effect<MetadataThrottle, never, Scope.Scope> => | ||
| Effect.gen(function* () { | ||
| const lock = yield* Semaphore.make(1) | ||
| let dirty = false | ||
| let bytesSinceFlush = 0 | ||
| let firstFlushed = false | ||
|
|
||
| const emit = (force: boolean) => | ||
| lock.withPermits(1)( | ||
| Effect.suspend(() => { | ||
| if (!dirty && !force) return Effect.void | ||
| const output = options.snapshot() | ||
| // Clear before awaiting emit so chunks arriving mid-emit stay marked | ||
| // dirty for the next flush instead of being silently dropped. | ||
| dirty = false | ||
| bytesSinceFlush = 0 | ||
| // Best-effort: a metadata push is a UI/notification side effect. Swallow | ||
| // a defect so it cannot fail the stream consumer (which would stop | ||
| // reading stdout and could hang the process) or orDie the final flush. | ||
| // Interrupts and typed errors still propagate. | ||
| return options.emit(output).pipe(Effect.catchDefect(() => Effect.void)) | ||
| }), | ||
| ) | ||
|
|
||
| const onChunk = (size: number) => | ||
| Effect.suspend(() => { | ||
| // A leading empty decode chunk (TextDecoder can emit "" on a partial | ||
| // multibyte boundary) carries no new preview; skipping it avoids burning | ||
| // the first-flush slot before the first real output arrives. | ||
| if (size <= 0) return Effect.void | ||
| dirty = true | ||
| bytesSinceFlush += size | ||
| // First non-empty chunk is visible immediately: downstream consumers | ||
| // (e.g. the abort-on-output test path) rely on seeing it synchronously. | ||
| if (!firstFlushed) { | ||
| firstFlushed = true | ||
| return emit(true) | ||
| } | ||
| if (bytesSinceFlush >= options.byteThreshold) return emit(true) | ||
| return Effect.void | ||
| }) | ||
|
|
||
| // "spill" forces an emit even under the byte threshold (output just crossed | ||
| // to a tempfile); "final" is dirty-gated so it only pushes a pending tail. | ||
| const flush = (reason: "spill" | "final") => emit(reason === "spill") | ||
|
|
||
| yield* emit(false).pipe( | ||
| Effect.repeat(Schedule.spaced(Duration.millis(options.intervalMillis))), | ||
| Effect.delay(Duration.millis(options.intervalMillis)), | ||
| Effect.forkScoped, | ||
| ) | ||
|
|
||
| return { onChunk, flush } | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
packages/opencode/test/tool/shell-metadata-throttle.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { describe, expect } from "bun:test" | ||
| import { Duration, Effect } from "effect" | ||
| import * as TestClock from "effect/testing/TestClock" | ||
| import { it } from "../lib/effect" | ||
| import { makeMetadataThrottle } from "../../src/tool/shell-metadata-throttle" | ||
|
|
||
| function setup(overrides?: { intervalMillis?: number; byteThreshold?: number }) { | ||
| const emits: string[] = [] | ||
| const state = { last: "" } | ||
| const make = makeMetadataThrottle({ | ||
| intervalMillis: overrides?.intervalMillis ?? 150, | ||
| byteThreshold: overrides?.byteThreshold ?? 4 * 1024, | ||
| snapshot: () => state.last, | ||
| emit: (output) => Effect.sync(() => emits.push(output)), | ||
| }) | ||
| return { emits, state, make } | ||
| } | ||
|
|
||
| describe("tool.shell metadata throttle", () => { | ||
| it.effect("emits the first chunk synchronously without advancing the clock", () => | ||
| Effect.gen(function* () { | ||
| const { emits, state, make } = setup() | ||
| const throttle = yield* make | ||
| state.last = "hello" | ||
| yield* throttle.onChunk(5) | ||
| expect(emits).toEqual(["hello"]) | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("emits immediately when accumulated bytes cross the threshold", () => | ||
| Effect.gen(function* () { | ||
| const { emits, state, make } = setup({ byteThreshold: 100 }) | ||
| const throttle = yield* make | ||
| state.last = "a" | ||
| yield* throttle.onChunk(1) // first chunk flushes immediately | ||
| state.last = "ab" | ||
| yield* throttle.onChunk(50) // 50 < 100, coalesced | ||
| expect(emits).toEqual(["a"]) | ||
| state.last = "abc" | ||
| yield* throttle.onChunk(60) // 50 + 60 >= 100, flushes | ||
| expect(emits).toEqual(["a", "abc"]) | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("flushes coalesced chunks on the interval timer (progressive updates)", () => | ||
| Effect.gen(function* () { | ||
| const { emits, state, make } = setup() | ||
| const throttle = yield* make | ||
| state.last = "1" | ||
| yield* throttle.onChunk(1) // first flush | ||
| state.last = "12" | ||
| yield* throttle.onChunk(1) // coalesced under threshold | ||
| yield* TestClock.adjust(Duration.millis(150)) // timer flush | ||
| state.last = "123" | ||
| yield* throttle.onChunk(1) | ||
| yield* TestClock.adjust(Duration.millis(150)) // timer flush | ||
| state.last = "1234" | ||
| yield* throttle.onChunk(1) | ||
| yield* TestClock.adjust(Duration.millis(150)) // timer flush | ||
| yield* TestClock.adjust(Duration.millis(150)) // nothing dirty, no flush | ||
| expect(emits).toEqual(["1", "12", "123", "1234"]) | ||
| expect(emits.length).toBeGreaterThanOrEqual(3) | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("final flush pushes a pending tail chunk", () => | ||
| Effect.gen(function* () { | ||
| const { emits, state, make } = setup() | ||
| const throttle = yield* make | ||
| state.last = "head" | ||
| yield* throttle.onChunk(1) // first flush | ||
| state.last = "head+tail" | ||
| yield* throttle.onChunk(1) // coalesced, not yet emitted | ||
| yield* throttle.flush("final") | ||
| expect(emits).toEqual(["head", "head+tail"]) | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("spill flush emits immediately even under the byte threshold", () => | ||
| Effect.gen(function* () { | ||
| const { emits, state, make } = setup() | ||
| const throttle = yield* make | ||
| state.last = "x" | ||
| yield* throttle.onChunk(1) // first flush | ||
| state.last = "x-spilled" | ||
| yield* throttle.flush("spill") | ||
| expect(emits).toEqual(["x", "x-spilled"]) | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("timer does not emit when no new output arrived", () => | ||
| Effect.gen(function* () { | ||
| const { emits, state, make } = setup() | ||
| const throttle = yield* make | ||
| state.last = "a" | ||
| yield* throttle.onChunk(1) // first flush clears dirty | ||
| yield* TestClock.adjust(Duration.millis(150)) | ||
| yield* TestClock.adjust(Duration.millis(150)) | ||
| expect(emits).toEqual(["a"]) | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("final flush is a no-op when the latest output was already emitted", () => | ||
| Effect.gen(function* () { | ||
| const { emits, state, make } = setup() | ||
| const throttle = yield* make | ||
| state.last = "done" | ||
| yield* throttle.onChunk(1) // first flush clears dirty | ||
| yield* throttle.flush("final") | ||
| expect(emits).toEqual(["done"]) | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("ignores empty chunks and keeps the first-flush slot for the first real output", () => | ||
| Effect.gen(function* () { | ||
| const { emits, state, make } = setup() | ||
| const throttle = yield* make | ||
| state.last = "" // empty decode chunk: nothing new to preview | ||
| yield* throttle.onChunk(0) | ||
| expect(emits).toEqual([]) // no emit, and first-flush slot not consumed | ||
| state.last = "real" | ||
| yield* throttle.onChunk(4) // first real output still flushes synchronously | ||
| expect(emits).toEqual(["real"]) | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("swallows a defect from emit across first-chunk, timer, and final flush", () => | ||
| Effect.gen(function* () { | ||
| let calls = 0 | ||
| const state = { last: "" } | ||
| const throttle = yield* makeMetadataThrottle({ | ||
| intervalMillis: 150, | ||
| byteThreshold: 4 * 1024, | ||
| snapshot: () => state.last, | ||
| emit: () => { | ||
| calls += 1 | ||
| return Effect.die(new Error("metadata channel boom")) | ||
| }, | ||
| }) | ||
| state.last = "first" | ||
| yield* throttle.onChunk(1) // first-chunk synchronous flush | ||
| state.last = "first+timer" | ||
| yield* throttle.onChunk(1) | ||
| yield* TestClock.adjust(Duration.millis(150)) // timer flush | ||
| state.last = "first+timer+final" | ||
| yield* throttle.onChunk(1) | ||
| yield* throttle.flush("final") // final flush | ||
| // Reaching here proves none of the three flush paths propagated the defect. | ||
| expect(calls).toBeGreaterThanOrEqual(3) | ||
| }), | ||
| ) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.