feat(agent-harness): bound and sanitize model streams - #5767
Conversation
| const close = async () => { | ||
| abort.abort(); | ||
| await reader.cancel().catch(() => undefined); | ||
| reader.releaseLock(); |
There was a problem hiding this comment.
WARNING: close() is not idempotent and can throw on releaseLock()
cancel() always calls close(), which aborts signal before cancelling the reader. Aborting the pipeThrough signal rejects a pending reader.read() in pull(), so the catch path also await close(). The second call hits reader.releaseLock() after the lock is already released and throws TypeError, which can surface as an unhandled rejection during client disconnect.
Guard close() with a once-flag (and/or try/catch around releaseLock()) so cleanup is safe to invoke from both cancel and catch.
Reply with @kilocode-bot fix it to have Kilo Code address this issue.
Code Review SummaryStatus: 1 Issue Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
Files Reviewed (2 files)
Fix these issues in Kilo Cloud Reviewed by grok-4.6 · Input: 159.6K · Output: 30.8K · Cached: 465.9K Review guidance: REVIEW.md from base branch |
|
Closing: the owner stopped this workflow section. The branch is retained. |
No new behavior — the new streaming support is not connected to a user-facing feature.
Summary
The server-only
streamHarnessModelforwards Server-Sent Events (SSE) incrementally;boundedBodycaps input at 1 mebibyte before decoding, andmediaTypenormalizes response types.StreamEnvelope,StreamEvent, andStreamErrorvalidate OpenAI-compatible software development kit (SDK) data and errors while preserving provider extensions.StreamContextrequires caller-supplied cancellation signals and sanitized error callbacks; the mirrored, unexported SDK schema must stay aligned with SDK contract changes.Files
apps/web/src/lib/agent-harness/model-stream.ts— Added source, 153 lines. Counts cumulative bytes before strict 8-bit Unicode Transformation Format (UTF-8) decoding and raisesPAYLOAD_TOO_LARGEabove 1,048,576 bytes. Parses multiline SSE and ignores unknown fields or invalid retry values without buffering the complete response. Validates known fields, then re-encodes the original JavaScript Object Notation (JSON) value to retain unknown provider data. Preserves text, both reasoning forms, tool calls and signatures, identifiers, finish reasons, costs, and detailed or separate usage. Retains caller headers, copies only upstreamrequest-id, and setscontent-type: text/event-streampluscontent-encoding: identity. Rejects unsuccessful responses with their original status; wrong media types or missing bodies on successful responses return sanitized 422 JSON. Cancels rejected bodies without forwarding their contents. Structured errors accept integer-compatible codes from 400–599; invalid codes become 422. Errors become terminal SSE events throughfailure; parsing and transport failures useerrorStatuswith 422 as the invalid-data fallback. After streaming starts, errors retain response status 200 and end the stream with sanitized events instead of another[DONE]marker. End-of-stream,[DONE], errors, and cancellation abort upstream work, cancel the reader, and release its lock. Caller signals also carry request cancellation and deadlines; empty and terminal-only output remain valid.apps/web/src/lib/agent-harness/model-stream.test.ts— Added tests, 230 lines. Covers incremental reads through the OpenAI-compatible SDK, multiline events, split UTF-8, usage, reasoning, tool extensions, headers, and completion markers. Covers exact and exceeded byte limits, malformed fields, structured errors, failed reads, invalid responses, empty output, cancellation, deadlines, and cleanup.Tests: 1 file added —
model-stream.test.ts(+230/−0 lines); the supplied report records 34 passing cases.Generated: 0 files changed.
Verification
Manual and end-to-end verification: not run because this level adds an internal helper without an endpoint or complete user journey.
Authenticated proxy composition, billed dispatch, Worker deployment, and cross-client verification remain outside this level.
Visual Changes
Visual Changes: N/A
Reviewer Notes
Human steps
This level requires no human setup before or after merge.
Product pull request merges remain human-owned.
Recorded checks
Notes
Stacked PRs — merge bottom to top. Each level shows only its own diff.
Runtime verification (E2E, user advocacy, simplify) runs on the tip PR over every level.
Every level keeps its own checks, its own bot review, and its own threads; each one is answered on its own PR.
Each level is its own deliverable: it builds and passes its own checks alone.
A finding on a level is repaired on that level, then carried upward with stack.sh forward.
shared-agent-harness-3bb0— chore(agent-harness): register workspaces and enforce CI boundaries #5632shared-agent-harness-3bb0-s2— feat(agent-harness): define portable domain and snapshots #5637shared-agent-harness-3bb0-s3— feat(agent-harness): define commands tools and permission policy #5639shared-agent-harness-3bb0-s4— feat(agent-harness): share client state and cursor recovery #5643shared-agent-harness-3bb0-s5— feat(agent-harness): persist command intents and execution receipts #5647shared-agent-harness-3bb0-s6— feat(db): add harness ingress grants and retirement fences #5655shared-agent-harness-3bb0-s7— feat(agent-harness): deliver legacy history and project durable text #5659shared-agent-harness-3bb0-s8— feat(agent-harness): authorize durable grants and registered clients #5662shared-agent-harness-3bb0-s9— feat(agent-harness): fence retirement and retry payload cleanup #5667shared-agent-harness-3bb0-s10— feat(agent-harness): persist authoritative state in SQLite #5675shared-agent-harness-3bb0-s11— feat(agent-harness): admit durable runs and revisioned commands #5678shared-agent-harness-3bb0-s12— feat(agent-harness): recover queued runs and stream checkpointed steps #5688shared-agent-harness-3bb0-s13— feat(agent-harness): resolve interactions and dispatch tools sequentially #5693shared-agent-harness-3bb0-s14— feat(agent-harness): fence designated client tool execution #5697shared-agent-harness-3bb0-s15— feat(agent-harness): synchronize durable snapshots and legacy history #5701shared-agent-harness-3bb0-s16— feat(agent-harness): reuse authorized invitations with durable replay #5704shared-agent-harness-3bb0-s17— feat(integrations): bound repository transport for harness reads #5710shared-agent-harness-3bb0-s18— feat(integrations): expose bounded authorized repository reads #5714shared-agent-harness-3bb0-s19— feat(agent-harness): expose named authorized resource reads #5718shared-agent-harness-3bb0-s20— feat(sessions): bound history transport for harness reads #5724shared-agent-harness-3bb0-s21— feat(agent-harness): read scoped Cloud Agent context and progress #5726shared-agent-harness-3bb0-s22— fix(agent-harness): preserve ordered Cloud Agent dispatch identity #5731shared-agent-harness-3bb0-s23— feat(agent-harness): hand coding work to authorized Cloud Agent sessions #5733shared-agent-harness-3bb0-s24— feat(agent-harness): authorize scoped MCP gateway connections #5737shared-agent-harness-3bb0-s25— feat(agent-harness): bound MCP gateway transport in the Worker #5740shared-agent-harness-3bb0-s26— feat(agent-harness): execute validated remote MCP tools #5743shared-agent-harness-3bb0-s27— refactor(exa): share provider dispatch and usage recording #5746shared-agent-harness-3bb0-s28— feat(agent-harness): authorize bounded web provider requests #5747shared-agent-harness-3bb0-s29— feat(agent-harness): normalize web sources and preserve citations #5749shared-agent-harness-3bb0-s30— feat(agent-harness): define closed internal operation contracts #5753shared-agent-harness-3bb0-s31— feat(agent-harness): authorize internal maintenance operations #5754shared-agent-harness-3bb0-s32— feat(agent-harness): authorize named Kilo operation dispatch #5755shared-agent-harness-3bb0-s33— feat(agent-harness): authorize internal provider operations #5757shared-agent-harness-3bb0-s34— feat(agent-harness): secure the internal operations endpoint #5758shared-agent-harness-3bb0-s35— feat(agent-harness): bound and sanitize model streams #5767 ← this PRshared-agent-harness-3bb0-s36— feat(agent-harness): secure billed model inference #5776shared-agent-harness-3bb0-s37— test(agent-harness): cover model gateway security boundaries #5777 (tip)