Skip to content

fix(backends): mapCommonBackendEvent unwraps sandbox-SDK data.part.text natively (closes #35)#36

Merged
drewstone merged 1 commit into
mainfrom
feat/sandbox-sdk-backend-interop
May 23, 2026
Merged

fix(backends): mapCommonBackendEvent unwraps sandbox-SDK data.part.text natively (closes #35)#36
drewstone merged 1 commit into
mainfrom
feat/sandbox-sdk-backend-interop

Conversation

@drewstone

Copy link
Copy Markdown
Contributor

Closes #35.

What

mapCommonBackendEvent now unwraps the canonical @tangle-network/sandbox message.part.updated shape { data: { part: { type: 'text', text } } } natively. Before this fix, the default mapper only looked at data.text / data.delta / record.text — sandbox-SDK text deltas silently dropped out of the canonical stream, forcing every sandbox-SDK consumer to ship a duplicated mapEvent shim.

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) ?? partText

Resolution order is data.textdata.deltarecord.textdata.part.text so any consumer already passing the flat shape keeps winning (back-compat).

Tests

Two new tests in tests/runtime.test.ts:

  1. "unwraps the @tangle-network/sandbox data.part.text shape natively" — fires the real sandbox-SDK event shape through runAgentTaskStream and asserts text_delta events emit ['hello', ' world']. Also asserts a non-text part-kind drops cleanly (does NOT mis-fire).
  2. "prefers explicit data.text when 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.streamTaskrunAgentTaskStream needs this. Without the fix, each product ships its own mapEvent shim — 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

  • blueprint-agent#1758 (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.
  • Every future sandbox-hosted product (gtm-agent's sandbox migration TODO, tax/legal/creative when they go sandbox-hosted) gets the right thing by default.

…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 drewstone merged commit 3adbac9 into main May 23, 2026
1 check failed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createSandboxPromptBackend default mapper doesn't unwrap sandbox-SDK SandboxEvent shape (silent text_delta loss)

1 participant