Repository navigation
fix(command-code): avoid double charging live NDJSON buffers #8047
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
lidge-jun
merged 4 commits into
lidge-jun:dev
from
0disoft:task/command-code-buffer-reservation
Nov 3, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a1d6e6a
fix(command-code): reserve only live buffer growth
0disoft ff0004d
test(command-code): cover shared buffer ownership and drain
0disoft f99e215
Merge remote-tracking branch 'origin/dev' into lane/pr-8047
lidge-jun 8ab3bb6
Merge remote-tracking branch 'origin/dev' into lane/pr-8047
lidge-jun 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
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
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
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
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,110 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { createCommandCodeAdapter } from "../../src/adapters/command-code"; | ||
| import { createTestTranslatorBudget } from "../helpers/translator-budget"; | ||
| import type { AdapterEvent } from "../../src/types"; | ||
|
|
||
| const adapter = () => createCommandCodeAdapter({ adapter: "command-code", baseUrl: "https://command.example" }); | ||
| const content = "é한😀".repeat(32); | ||
| const line = JSON.stringify({ type: "text-delta", text: content }); | ||
| const encoder = new TextEncoder(); | ||
|
|
||
| function input(bytes: Uint8Array, chunkSize: number, beforeRead?: (offset: number) => void) { | ||
| let offset = 0; | ||
| let cancelled = false; | ||
| const body = new ReadableStream<Uint8Array>({ | ||
| pull(controller) { | ||
| beforeRead?.(offset); | ||
| if (offset === bytes.length) { controller.close(); return; } | ||
| const end = Math.min(bytes.length, offset + chunkSize); | ||
| controller.enqueue(bytes.subarray(offset, end)); | ||
| offset = end; | ||
| }, | ||
| cancel() { cancelled = true; }, | ||
| }, { highWaterMark: 0 }); | ||
| return { response: new Response(body), cancelled: () => cancelled }; | ||
| } | ||
|
|
||
| for (const ending of ["\n", ""] as const) { | ||
| for (const fragmented of [false, true]) { | ||
| test(`Command Code admits an exact-limit ${fragmented ? "fragmented" : "whole"} record with ${JSON.stringify(ending)} termination`, async () => { | ||
| const bytes = encoder.encode(line + ending); | ||
| const budget = createTestTranslatorBudget({ maxTurnBytes: bytes.length }); | ||
| const source = input(bytes, fragmented ? 1 : bytes.length); | ||
| const events: AdapterEvent[] = []; | ||
| for await (const event of adapter().parseStream(source.response, budget)) events.push(event); | ||
| expect(events).toEqual([ | ||
| { type: "text_delta", text: content }, | ||
| { type: "done", usage: undefined, stopReason: undefined }, | ||
| ]); | ||
| expect(budget.snapshot()).toMatchObject({ currentBytes: 0, highWaterBytes: bytes.length, overflows: 0 }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| for (const fragmented of [false, true]) { | ||
| test(`Command Code refuses a ${fragmented ? "fragmented" : "whole"} record one byte over budget and cleans up`, async () => { | ||
| const bytes = encoder.encode(line); | ||
| const budget = createTestTranslatorBudget({ maxTurnBytes: bytes.length - 1 }); | ||
| const source = input(bytes, fragmented ? 1 : bytes.length); | ||
| const events: AdapterEvent[] = []; | ||
| const consume = async () => { | ||
| for await (const event of adapter().parseStream(source.response, budget)) events.push(event); | ||
| }; | ||
| await expect(consume()).rejects.toMatchObject({ code: "translation_buffer_limit", kind: "live_transient" }); | ||
| expect(events).toHaveLength(0); | ||
| expect(budget.snapshot()).toMatchObject({ currentBytes: 0, overflows: 1 }); | ||
| expect(source.cancelled()).toBe(true); | ||
| }); | ||
| } | ||
|
|
||
| for (const deficit of [0, 1]) { | ||
| test(`Command Code preserves another live_transient owner's bytes on ${deficit ? "refused" : "successful"} growth`, async () => { | ||
| const bytes = encoder.encode(line); | ||
| const budget = createTestTranslatorBudget({ maxTurnBytes: bytes.length + 64 - deficit }); | ||
| budget.chargeRetained(64, { kind: "live_transient" }); | ||
| const source = input(bytes, 1); | ||
| const events: AdapterEvent[] = []; | ||
| const consume = async () => { | ||
| for await (const event of adapter().parseStream(source.response, budget)) events.push(event); | ||
| }; | ||
| if (deficit) { | ||
| await expect(consume()).rejects.toMatchObject({ code: "translation_buffer_limit" }); | ||
| expect(events).toHaveLength(0); | ||
| expect(source.cancelled()).toBe(true); | ||
| } else { | ||
| await consume(); | ||
| expect(events.at(-1)?.type).toBe("done"); | ||
| } | ||
| expect(budget.snapshot()).toMatchObject({ currentBytes: 64, highWaterBytes: bytes.length + 64 - deficit, overflows: deficit }); | ||
| budget.releaseRetained(64, { kind: "live_transient" }); | ||
| expect(budget.snapshot().currentBytes).toBe(0); | ||
| }); | ||
| } | ||
|
|
||
| test("Command Code releases drained bytes before reading the next fragment", async () => { | ||
| const carryBytes = 16; // The second record's ASCII JSON prefix, before any text content. | ||
| const firstReadBytes = encoder.encode(line + "\n").length + carryBytes; | ||
| const budget = createTestTranslatorBudget({ maxTurnBytes: firstReadBytes }); | ||
| const retainedBeforeSecondRead: number[] = []; | ||
| const source = input(encoder.encode(line + "\n" + line), firstReadBytes, offset => { | ||
| if (offset === firstReadBytes) retainedBeforeSecondRead.push(budget.snapshot().currentBytes); | ||
| }); | ||
| const events: AdapterEvent[] = []; | ||
| for await (const event of adapter().parseStream(source.response, budget)) events.push(event); | ||
| expect(retainedBeforeSecondRead).toEqual([carryBytes]); | ||
| expect(events.filter(event => event.type === "text_delta").map(event => event.text)).toEqual([content, content]); | ||
| expect(events.at(-1)?.type).toBe("done"); | ||
| expect(budget.snapshot()).toMatchObject({ currentBytes: 0, highWaterBytes: firstReadBytes, overflows: 0 }); | ||
| }); | ||
|
|
||
| test("Command Code consumer return releases its retained record and cancels upstream", async () => { | ||
| const bytes = encoder.encode(line + "\n"); | ||
| const budget = createTestTranslatorBudget({ maxTurnBytes: bytes.length }); | ||
| const source = input(bytes, bytes.length); | ||
| const events = adapter().parseStream(source.response, budget); | ||
| expect((await events.next()).value).toMatchObject({ type: "text_delta", text: content }); | ||
| expect(budget.snapshot().currentBytes).toBe(bytes.length); | ||
| await events.return(); | ||
| expect(budget.snapshot().currentBytes).toBe(0); | ||
| expect(source.cancelled()).toBe(true); | ||
| }); | ||
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.