fix(backends): mapCommonBackendEvent unwraps sandbox-SDK data.part.text natively (closes #35)#36
Merged
Merged
Conversation
…ext` natively Closes #35. The canonical `@tangle-network/sandbox` `box.streamTask` emits `message.part.updated` with the text nested under `data.part.text`: { type: 'message.part.updated', data: { part: { type: 'text', text: '…' } } } Before this fix, the default `mapCommonBackendEvent` only walked `data.text` / `data.delta` / `record.text` — sandbox-SDK text deltas silently dropped out of the canonical stream, forcing every sandbox-SDK consumer (blueprint-agent#1758, future gtm-agent sandbox path, every sandbox-hosted product downstream) to ship a duplicated `mapEvent` shim that walked `data.part.text` themselves. Fix: the `message.part.updated` branch now ALSO walks `data.part.text` when `data.part.type === 'text'` (or undefined for forward-compat). Resolution order is `data.text` → `data.delta` → `record.text` → `data.part.text` so any consumer already passing the flat shape keeps working unchanged (covered by the new precedence test). Two new tests pin the behaviour: - "unwraps the @tangle-network/sandbox `data.part.text` shape natively" — fires the real sandbox-SDK shape through `runAgentTaskStream` and asserts the canonical `text_delta` stream contains `['hello', ' world']`, plus that a non-text part-kind drops cleanly (does NOT mis-fire as a text_delta). - "prefers explicit `data.text` when both shapes present" — back-compat: any existing call site sending `{ data: { text } }` keeps winning over the nested shape. Result: products consuming the sandbox SDK can wire it straight into `createSandboxPromptBackend` without writing a `mapEvent` shim — the default does the right thing. Blueprint's pinned contract test (blueprint-agent#1758, `Claude-Code → RuntimeStreamEvent migration seam`) that documented this gap as a finding now becomes the regression guard on this fix. Verification ──────────── - `pnpm test` — 181/181 pass (+2 new). - `pnpm typecheck` — clean.
drewstone
added a commit
that referenced
this pull request
May 23, 2026
drewstone
added a commit
that referenced
this pull request
May 23, 2026
…boxEvent shape (post-#36) (#37) The example previously sidestepped the actual sandbox-SDK event shape by emitting flat `{ type: 'text_delta', text }` — which the default mapper trivially passes through. That dodged the load-bearing question products consuming the real SDK actually hit: how does `createSandboxPromptBackend` handle the canonical shape `{ type: 'message.part.updated', data: { part: { type: 'text', text } } }`? After #36 (closes #35), the default `mapCommonBackendEvent` handles this natively. This example now demonstrates the canonical pattern verbatim so consumers can copy it into their product code without guessing: - `streamPrompt` yields `SandboxEvent`s (the actual SDK shape). - Text deltas arrive as `message.part.updated` with `data.part.text` nested — no per-product `mapEvent` shim required. - Tool turns arrive as `tool_call` + `tool_result` with `data.name` + `data.input` / `data.output` — also handled by the default. Header comment + inline comments call out exactly which event variants the default mapper handles, and explicitly note that `mapEvent` is optional and only needed for product-specific shapes outside the canonical vocabulary. Verified by running the example end-to-end: pnpm exec tsx examples/sandbox-stream-backend/sandbox-stream-backend.ts emits the canonical RuntimeStreamEvent sequence — `task_start`, `readiness_*`, `session_created`, `backend_start`, **`text_delta`** (x3, from the nested `data.part.text` shape — the bit that was broken pre-#36), `tool_call`, `tool_result`, `backend_end`, `task_end`, `final`. The full vocabulary the README documents.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #35.
What
mapCommonBackendEventnow unwraps the canonical@tangle-network/sandboxmessage.part.updatedshape{ data: { part: { type: 'text', text } } }natively. Before this fix, the default mapper only looked atdata.text/data.delta/record.text— sandbox-SDK text deltas silently dropped out of the canonical stream, forcing every sandbox-SDK consumer to ship a duplicatedmapEventshim.How
if (type === 'message.part.updated' || type === 'text_delta' || type === 'delta') { - const text = stringValue(data.text) ?? stringValue(data.delta) ?? stringValue(record.text) + const part = data.part as Record<string, unknown> | undefined + const partText = + part !== undefined && typeof part === 'object' && (part.type === 'text' || part.type === undefined) + ? stringValue(part.text) + : undefined + const text = stringValue(data.text) ?? stringValue(data.delta) ?? stringValue(record.text) ?? partTextResolution order is
data.text→data.delta→record.text→data.part.textso any consumer already passing the flat shape keeps winning (back-compat).Tests
Two new tests in
tests/runtime.test.ts:data.part.textshape natively" — fires the real sandbox-SDK event shape throughrunAgentTaskStreamand assertstext_deltaevents emit['hello', ' world']. Also asserts a non-text part-kind drops cleanly (does NOT mis-fire).data.textwhen both shapes present" — back-compat guard: existing call sites sending the flat shape continue to win.181/181 full suite passes. Typecheck clean.
Why this matters
The sandbox SDK is the canonical sandbox interface for the Tangle ecosystem. Every sandbox-hosted agent product that bridges
box.streamTask→runAgentTaskStreamneeds this. Without the fix, each product ships its ownmapEventshim — exactly the duplication class agent-eval#82/#86 closed for cell helpers, but at the more load-bearing event-mapping seam (every chat-turn event passes through here).Downstream
Claude-Code → RuntimeStreamEvent migration seam) ships a pinned contract test that asserted the BUG (default mapper drops nested shape) as a finding. Once this lands + publishes, that test flips to assert the FIX — instant regression guard.