@silverprotocol/claude-agent-sdk 0.11.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +7 -7
- package/dist/index.js +56 -56
- package/dist/index.js.map +1 -1
- package/package.json +5 -2
- package/src/index.ts +66 -66
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `@silverprotocol/claude-agent-sdk` — the LIVE-path normalizer (stateful facet).
|
|
3
3
|
*
|
|
4
|
-
* Translates the public Claude Agent SDK `SDKMessage` union (the
|
|
5
|
-
*
|
|
4
|
+
* Translates the public Claude Agent SDK `SDKMessage` union (the frames a host's
|
|
5
|
+
* run seam yields from `query()`) into
|
|
6
6
|
* AgJSON events (`AgEvent[]`, spec §4) by driving a shared {@link StreamAssembler}
|
|
7
7
|
* via primitive calls. Claude's assistant turn is a COMPLETE-message structure —
|
|
8
8
|
* `message.content[]` is the whole turn's content, not a stream of deltas — so the
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* block, then tool_use block), so the message lifecycle is keyed on `message.id`
|
|
13
13
|
* and its seal is deferred across contiguous same-id frames: one
|
|
14
14
|
* `message.start`/`message.end` pair per id, never a re-open of a sealed id
|
|
15
|
-
* (INV-MSG — see `PendingMessage` in `createClaudeNormalizer
|
|
15
|
+
* (INV-MSG — see `PendingMessage` in `createClaudeNormalizer`).
|
|
16
16
|
*
|
|
17
17
|
* With `includePartialMessages: true` (workspace#7) the seam ALSO interleaves
|
|
18
18
|
* `stream_event` frames — the raw Anthropic streaming vocabulary — before each
|
|
@@ -447,7 +447,7 @@ function readNonExecutionKinds(frame: unknown): Map<string, string> {
|
|
|
447
447
|
return out;
|
|
448
448
|
}
|
|
449
449
|
|
|
450
|
-
//
|
|
450
|
+
// Ruled 2026-09-24 (no neutral remedy field; fix the carries): each `tool_result_meta`
|
|
451
451
|
// entry, keyed by its `id` (the tool_use_id), verbatim: {id, non_execution_kind?,
|
|
452
452
|
// user_feedback?, remedy?} per the CLI's schema ("@internal Display metadata for
|
|
453
453
|
// this message's tool_result blocks"). Read through the JSON boundary; an entry
|
|
@@ -475,8 +475,8 @@ function isDenialKind(kind: string | undefined): boolean {
|
|
|
475
475
|
return kind === "user-rejected" || kind === "permission-rule" || (kind !== undefined && kind.startsWith("automode-"));
|
|
476
476
|
}
|
|
477
477
|
|
|
478
|
-
// Which assistant `error` codes are transient (retriable).
|
|
479
|
-
// top-level close and a nested frame's non-terminal `error` share it.
|
|
478
|
+
// Which assistant `error` codes are transient (retriable). An API-error turn's
|
|
479
|
+
// stashed top-level close and a nested frame's non-terminal `error` share it.
|
|
480
480
|
// Finding #2 (minor): `overloaded` (transient capacity error, a first
|
|
481
481
|
// cousin of rate_limit/server_error) joins the retriable set.
|
|
482
482
|
// `model_not_found` (a permanent misconfiguration — e.g. a stale/
|
|
@@ -805,7 +805,7 @@ const CARRIED_SYSTEM_SUBTYPES = new Set<string>([
|
|
|
805
805
|
// system/status ping — status:'compacting'|'requesting'|null (+ optional
|
|
806
806
|
// compact_result/compact_error). GENUINE consumer-facing agent-state signal
|
|
807
807
|
// (exactly the ready/thinking/responding vocabulary chat surfaces render —
|
|
808
|
-
//
|
|
808
|
+
// a reference host's status-states request) with no AgJSON home; previously
|
|
809
809
|
// router-plane'd. Carried whole-frame, which also closes the manifest's
|
|
810
810
|
// disclosed `compact_error` residual gap.
|
|
811
811
|
"status",
|
|
@@ -877,7 +877,7 @@ function anthropicFrameKind(msg: SDKMessage): string | undefined {
|
|
|
877
877
|
// A COPY, never the frame's own array: the value is emitted unparsed
|
|
878
878
|
// (result-meta, message.metadata), core's StreamAssembler does not copy, and
|
|
879
879
|
// push() hands a JSON frame through by reference, so returning `v` would let an
|
|
880
|
-
// emitted event share the host's live array (
|
|
880
|
+
// emitted event share the host's live array (found by an aliasing audit).
|
|
881
881
|
function readUserMessageUuids(v: unknown): string[] | undefined {
|
|
882
882
|
return Array.isArray(v) && v.every((s): s is string => typeof s === "string") ? [...v] : undefined;
|
|
883
883
|
}
|
|
@@ -892,7 +892,7 @@ const HOST_ONLY_WRAPPER_KEYS: ReadonlySet<string> = new Set([
|
|
|
892
892
|
"api_error",
|
|
893
893
|
"api_error_params",
|
|
894
894
|
"api_error_code",
|
|
895
|
-
//
|
|
895
|
+
// Ruled 2026-09-24 (defer the field, fix the carries): three more @internal
|
|
896
896
|
// CLI assistant-wrapper strings, carried where the host can read them.
|
|
897
897
|
"error_details",
|
|
898
898
|
"advisor_model",
|
|
@@ -948,7 +948,7 @@ function carryVerbatim(v: unknown): JsonValue {
|
|
|
948
948
|
return withoutCreditTokens(JsonValue.parse(v));
|
|
949
949
|
}
|
|
950
950
|
|
|
951
|
-
//
|
|
951
|
+
// SPEC §13.10: a provider credit or bearer token is never
|
|
952
952
|
// emitted. Anthropic's refusal `stop_details` can hold `fallback_credit_token`
|
|
953
953
|
// (top level and per fallback), so every key of that name is deleted at any
|
|
954
954
|
// depth; everything else is kept verbatim.
|
|
@@ -974,7 +974,7 @@ function toolResultText(content: unknown): string | undefined {
|
|
|
974
974
|
}
|
|
975
975
|
|
|
976
976
|
// The namespace of the facet's own harness keys on a tool.done `_meta` (the
|
|
977
|
-
// subagent report's "anthropic/agentOutput",
|
|
977
|
+
// subagent report's "anthropic/agentOutput", the CLI's "anthropic/toolResultMeta",
|
|
978
978
|
// and since draft.5 the two host records below). SPEC §2.1 *Host records*: a
|
|
979
979
|
// producer carries such a record under a key in a namespace it owns.
|
|
980
980
|
const HARNESS_META_PREFIX = "anthropic/";
|
|
@@ -993,7 +993,7 @@ const PERMISSION_DENIED_META_KEY = `${HARNESS_META_PREFIX}permissionDenied`;
|
|
|
993
993
|
// written, so a tool (a malicious MCP server) can never present a forged
|
|
994
994
|
// harness fact, such as an agent report or a remedy pointing a host at its URL,
|
|
995
995
|
// even for a call the facet writes no harness key for (a review of the
|
|
996
|
-
//
|
|
996
|
+
// tool_result_meta carry; the CLI itself strips its own reserved `com.anthropic/` prefix
|
|
997
997
|
// from server `_meta`). Every other sibling key is kept verbatim. undefined
|
|
998
998
|
// when nothing remains.
|
|
999
999
|
function mergeHarnessMeta(sibling: AgMeta | undefined, harness: { [k: string]: JsonValue }): AgMeta | undefined {
|
|
@@ -1361,8 +1361,8 @@ export interface ClaudeNormalizerOptions {
|
|
|
1361
1361
|
* tool_result frame that carries none, the turn's own id), a placeholder in
|
|
1362
1362
|
* the same spirit as the openai/vercel facets' fixed labels. That placeholder is fine for
|
|
1363
1363
|
* self-contained streams but LEAKS into any consumer that persists
|
|
1364
|
-
* events verbatim under its own thread identity (
|
|
1365
|
-
* events carried the session id while the runtime's session records
|
|
1364
|
+
* events verbatim under its own thread identity (in a reference host,
|
|
1365
|
+
* mid-stream events carried the session id while the runtime's session records
|
|
1366
1366
|
* carried the real thread id). A runtime that owns the real thread
|
|
1367
1367
|
* identity should always pass it here — one id everywhere, stamped at
|
|
1368
1368
|
* construction.
|
|
@@ -1374,8 +1374,8 @@ export interface ClaudeNormalizerOptions {
|
|
|
1374
1374
|
* whose frame carries no usable message id or uuid (the normal path names a
|
|
1375
1375
|
* turn by its SDK message id or frame uuid, unique by construction). A host
|
|
1376
1376
|
* folds every invoke of a conversation into ONE Reducer, so a stem that
|
|
1377
|
-
* restarted with each invoke repeated those ids across invokes (
|
|
1378
|
-
*
|
|
1377
|
+
* restarted with each invoke repeated those ids across invokes (§8.0 "Ids
|
|
1378
|
+
* across invokes"). Absent, each normalizer draws a random
|
|
1379
1379
|
* `claude_<16 hex>` stem at most once, lazily (only when a fallback id is
|
|
1380
1380
|
* first needed, so the common path draws no randomness), and holds it
|
|
1381
1381
|
* OUTSIDE the atomic-push rebuild, so a rebuild reproduces it. Pass a fixed
|
|
@@ -1405,10 +1405,10 @@ function mintInvokeNonce(): string {
|
|
|
1405
1405
|
function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeStem: () => string): Normalizer {
|
|
1406
1406
|
const a = new StreamAssembler();
|
|
1407
1407
|
|
|
1408
|
-
//
|
|
1408
|
+
// A reference host's fold-identity check: the wire-visible `parentTurnId`
|
|
1409
1409
|
// label passed to subagent.start/.done (`turn_${parent_tool_use_id}`) is a
|
|
1410
1410
|
// synthetic cross-ref that was never opened as a real turn — it must stay on
|
|
1411
|
-
// the wire (
|
|
1411
|
+
// the wire (a reference host asserts `parentTurnId === 'turn_<toolCallId>'`)
|
|
1412
1412
|
// but must NOT be reused to route an INNER tool_result's turnId. Track the
|
|
1413
1413
|
// REAL subagent turnId per spawning parent_tool_use_id, derived from the
|
|
1414
1414
|
// sub-session's own assistant arm at subagent.start time, so a later inner
|
|
@@ -1434,7 +1434,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1434
1434
|
// message id opens its turn under its own frame uuid instead;
|
|
1435
1435
|
// - `isSDKMessage` validates only discriminants, so a missing or non-string
|
|
1436
1436
|
// id/uuid falls back to a positional `turn_<stem>_frame_<n>` rather than a
|
|
1437
|
-
// shared `turn_undefined`. The stem is per invoke (
|
|
1437
|
+
// shared `turn_undefined`. The stem is per invoke (§8.0 ids across invokes: a bare
|
|
1438
1438
|
// `turn_frame_<n>` repeated across the invokes one Reducer folds), and the
|
|
1439
1439
|
// counter is deterministic from the wire, so an atomic-push rebuild
|
|
1440
1440
|
// reproduces every delivered id.
|
|
@@ -1477,7 +1477,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1477
1477
|
}
|
|
1478
1478
|
// The result frame closes the open turn (or, for a result-only turn, the one
|
|
1479
1479
|
// it opens itself) and clears it. A RESULT-ONLY turn (no assistant frame,
|
|
1480
|
-
// notice or stream opened it: the
|
|
1480
|
+
// notice or stream opened it: the result-only API-error path, a
|
|
1481
1481
|
// startup-failure result, a local-command result) is OPENED here with an
|
|
1482
1482
|
// explicit turn.start before anything else the result emits (INV-TURN,
|
|
1483
1483
|
// SPEC:743: every turn is opened by exactly one turn.start and closed by
|
|
@@ -1503,7 +1503,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1503
1503
|
// session into one nested turn (and, if a subagent's frames carry the
|
|
1504
1504
|
// parent's session_id, collided with the top-level turn itself). It must
|
|
1505
1505
|
// never equal the synthetic `parentTurnId` label `turn_${parent_tool_use_id}`
|
|
1506
|
-
// (
|
|
1506
|
+
// (a reference host's fold-identity check), and a message id never does.
|
|
1507
1507
|
function nestedTurnId(parentToolUseId: string, firstMessageId: unknown, frameUuid: unknown): string {
|
|
1508
1508
|
const known = subagentTurnByParentToolUseId.get(parentToolUseId);
|
|
1509
1509
|
if (known !== undefined && !closedRuns.has(parentToolUseId)) return known;
|
|
@@ -1646,12 +1646,12 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1646
1646
|
// Every toolCallId that already got its FINAL tool.done in this invoke. The
|
|
1647
1647
|
// result's permission_denials aggregate skips these: re-emitting tool.start +
|
|
1648
1648
|
// tool.done{denied} for a call already closed is a duplicate start and a second
|
|
1649
|
-
// final tool.done (INV-BLOCK
|
|
1649
|
+
// final tool.done (INV-BLOCK: the reducer parks it as a double fold).
|
|
1650
1650
|
const closedToolCallIds = new Set<string>();
|
|
1651
1651
|
// SDK message id → the `diagnostics` value already carried for it (see the
|
|
1652
1652
|
// assistant branch): one carry per response, not one per frame.
|
|
1653
1653
|
const diagnosticsCarried = new Map<string, string>();
|
|
1654
|
-
//
|
|
1654
|
+
// Top-level turnId → the closing response's non-null stop_details (credit
|
|
1655
1655
|
// tokens stripped) and the message it describes, emitted on that turn's turn.done.
|
|
1656
1656
|
const stopDetailsByTurn = new Map<string, { readonly messageId: string; readonly value: JsonValue }>();
|
|
1657
1657
|
|
|
@@ -1677,7 +1677,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1677
1677
|
};
|
|
1678
1678
|
}
|
|
1679
1679
|
|
|
1680
|
-
//
|
|
1680
|
+
// Since 0.6.3: the turn.error close an API-error assistant frame
|
|
1681
1681
|
// (`error` set) decided for its turn, STASHED until the turn's result frame
|
|
1682
1682
|
// (the vercel facet's `stashedError` pattern). An API-error turn reaches this
|
|
1683
1683
|
// facet as TWO frames: the synthetic assistant message carrying `error`, and
|
|
@@ -1703,14 +1703,14 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1703
1703
|
type StashedTurnError = { readonly message: string; readonly code: string; readonly retriable: boolean };
|
|
1704
1704
|
const stashedTurnErrors = new Map<string, StashedTurnError>();
|
|
1705
1705
|
|
|
1706
|
-
/** Remove and return the stashed error close for `turnId`, if any
|
|
1706
|
+
/** Remove and return the stashed error close for `turnId`, if any. */
|
|
1707
1707
|
function takeStashedTurnError(turnId: string): StashedTurnError | undefined {
|
|
1708
1708
|
const stashed = stashedTurnErrors.get(turnId);
|
|
1709
1709
|
stashedTurnErrors.delete(turnId);
|
|
1710
1710
|
return stashed;
|
|
1711
1711
|
}
|
|
1712
1712
|
|
|
1713
|
-
//
|
|
1713
|
+
// INV-MSG — ONE message id ⇒ ONE message lifecycle.
|
|
1714
1714
|
//
|
|
1715
1715
|
// The Claude Agent SDK splits ONE API assistant message across MULTIPLE
|
|
1716
1716
|
// `assistant` frames whenever it has several content blocks: the thinking
|
|
@@ -1812,7 +1812,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1812
1812
|
redacted: string | undefined;
|
|
1813
1813
|
// draft.4 `phase` (§8.0 item 27): whether any non-empty thinking text
|
|
1814
1814
|
// streamed, and whether the complete frame listed this block in
|
|
1815
|
-
// `narration_block_indexes` while it was still open
|
|
1815
|
+
// `narration_block_indexes` while it was still open.
|
|
1816
1816
|
hasText: boolean;
|
|
1817
1817
|
narration: boolean;
|
|
1818
1818
|
}
|
|
@@ -1987,7 +1987,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1987
1987
|
|
|
1988
1988
|
// The stream arm proper. Partials map onto the SAME lifecycles the complete
|
|
1989
1989
|
// arm produces: message_start opens the SAME PendingMessage the complete
|
|
1990
|
-
// frame later joins (
|
|
1990
|
+
// frame later joins (the split-frame continuation test), and block ids reuse the
|
|
1991
1991
|
// stream's own content `index` — which equals the complete arm's cross-frame
|
|
1992
1992
|
// `blockIndex` arithmetic, so ids are identical either way.
|
|
1993
1993
|
function driveStreamEvent(msg: SDKPartial): void {
|
|
@@ -2063,7 +2063,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2063
2063
|
}
|
|
2064
2064
|
|
|
2065
2065
|
if (ev.type === "message_stop") {
|
|
2066
|
-
// The seal stays DEFERRED (
|
|
2066
|
+
// The seal stays DEFERRED (INV-MSG) while the lifecycle is
|
|
2067
2067
|
// open: the complete assistant frame for this id follows and joins it,
|
|
2068
2068
|
// and tool_results may still bind. And when the lifecycle is ALREADY
|
|
2069
2069
|
// sealed — the live wire delivers the tool_result BEFORE the tool-round
|
|
@@ -2219,7 +2219,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2219
2219
|
// on start events, so the estimate is now LIVE-ONLY (it used to fold
|
|
2220
2220
|
// last-value-wins onto the reasoning block) — the same standing as its
|
|
2221
2221
|
// `system/thinking_tokens` twin, which rides `ext.anthropic.frame`. No
|
|
2222
|
-
// consumer read the folded value (
|
|
2222
|
+
// consumer read the folded value (both reference consumers checked).
|
|
2223
2223
|
// `reasoningDelta`'s sugar has no `_meta` option; `a.emit()` is the base
|
|
2224
2224
|
// primitive (the reasoning.start precedent in `emitAssistantBlock`), and
|
|
2225
2225
|
// the sugar's only extra step, de-cumulation, is a pass-through for a
|
|
@@ -2405,7 +2405,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2405
2405
|
const parentTurnId =
|
|
2406
2406
|
msg.parent_tool_use_id !== null ? `turn_${msg.parent_tool_use_id}` : undefined;
|
|
2407
2407
|
|
|
2408
|
-
//
|
|
2408
|
+
// INV-MSG: does this frame CONTINUE the message left open by the previous
|
|
2409
2409
|
// frame? Same SDK message id, same turn, same nesting — anything else is a
|
|
2410
2410
|
// new message and seals the open one first (unchanged emission order).
|
|
2411
2411
|
const continued: PendingMessage | undefined =
|
|
@@ -2551,7 +2551,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2551
2551
|
if (assistantApiErrorCode !== undefined) {
|
|
2552
2552
|
wrapperMetaRaw["api_error_code"] = assistantApiErrorCode;
|
|
2553
2553
|
}
|
|
2554
|
-
//
|
|
2554
|
+
// Ruled 2026-09-24 (defer the neutral remedy field, fix the
|
|
2555
2555
|
// vendor carries; a 0.7.0 carry with zero golden moves). Three more
|
|
2556
2556
|
// @internal CLI 2.1.280 assistant-wrapper strings, UNDECLARED in sdk.d.ts
|
|
2557
2557
|
// 0.3.280 (so read through the JSON boundary), carried verbatim under their
|
|
@@ -2579,7 +2579,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2579
2579
|
// A non-null parent_tool_use_id ⇒ this assistant message belongs to a
|
|
2580
2580
|
// NESTED turn (subagent run). subagent.start is the SOLE nested-turn opener
|
|
2581
2581
|
// (spec §4/§5), emitted once per run by `openRun`, and seeds the turn so
|
|
2582
|
-
// openMessage does NOT synthesize a duplicate turn.start.
|
|
2582
|
+
// openMessage does NOT synthesize a duplicate turn.start. INV-MSG: a
|
|
2583
2583
|
// CONTINUATION frame joins the message the previous frame opened — no
|
|
2584
2584
|
// second message.start.
|
|
2585
2585
|
let open: PendingMessage;
|
|
@@ -2624,7 +2624,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2624
2624
|
});
|
|
2625
2625
|
}
|
|
2626
2626
|
const messageId = open.emittedId;
|
|
2627
|
-
//
|
|
2627
|
+
// A NON-NULL `stop_details` on a response (e.g. a refusal's
|
|
2628
2628
|
// {type: "refusal", category, explanation}, the shape @anthropic-ai/sdk
|
|
2629
2629
|
// 0.93.0 declares; any further member is kept too) is carried verbatim on its
|
|
2630
2630
|
// turn's closing turn.done.messageMetadata, which folds onto the message it
|
|
@@ -2693,9 +2693,9 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2693
2693
|
// `message.metadata` event after the block (the streamed path already
|
|
2694
2694
|
// carries the turn-binding family there).
|
|
2695
2695
|
//
|
|
2696
|
-
// Consumer check (2026-09-23):
|
|
2697
|
-
//
|
|
2698
|
-
// the native frame.
|
|
2696
|
+
// Consumer check (2026-09-23): a reference client has none of these
|
|
2697
|
+
// names; a reference host reads no `providerMetadata`, and its scrub keys
|
|
2698
|
+
// on the native frame. That host asked that the triad move as ONE unit; the
|
|
2699
2699
|
// result frame's `apiErrorCode` stays on `ext.anthropic.result-meta`.
|
|
2700
2700
|
const replayRaw: { [k: string]: JsonValue } = {};
|
|
2701
2701
|
const hostRaw: { [k: string]: JsonValue } = {};
|
|
@@ -2723,7 +2723,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2723
2723
|
const narrationIndexes = new Set(narrationBlockIndexes ?? []);
|
|
2724
2724
|
if (suppressed && narrationIndexes.size > 0) {
|
|
2725
2725
|
// STREAMED: the complete frame precedes its blocks' content_block_stop
|
|
2726
|
-
// (
|
|
2726
|
+
// (thinking-fable51 frames [62] signature → [63] frame → [64]
|
|
2727
2727
|
// stop), so each listed frame-local index names stream block
|
|
2728
2728
|
// `framedThrough + idx` (the frame covers the next blocks after those
|
|
2729
2729
|
// earlier frames covered). It is marked only while it is still OPEN;
|
|
@@ -2738,7 +2738,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2738
2738
|
}
|
|
2739
2739
|
if (suppressed) {
|
|
2740
2740
|
// STREAMED: record each open tool block's `wire_tool_inputs` entry (the
|
|
2741
|
-
// complete frame precedes the block's content_block_stop
|
|
2741
|
+
// complete frame precedes the block's content_block_stop); its
|
|
2742
2742
|
// tool.args.assembled carries it only if it differs from the streamed input.
|
|
2743
2743
|
for (let i = 0; i < m.content.length; i++) {
|
|
2744
2744
|
const frameBlock = m.content[i];
|
|
@@ -2811,7 +2811,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2811
2811
|
a.emit({ type: "message.metadata", messageId, metadata: unanchored });
|
|
2812
2812
|
}
|
|
2813
2813
|
}
|
|
2814
|
-
// The seal is DEFERRED (
|
|
2814
|
+
// The seal is DEFERRED (INV-MSG) — the next frame may continue this same
|
|
2815
2815
|
// message id. Usage is message-level and repeated per frame, so the newest
|
|
2816
2816
|
// frame's copy is the one that rides the eventual `message.end` — field by
|
|
2817
2817
|
// field: `mapMessageUsage` names every field it maps explicitly (absent ⇒
|
|
@@ -2826,7 +2826,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2826
2826
|
|
|
2827
2827
|
// If the assistant turn carries an error signal (rate_limit, billing_error, etc.),
|
|
2828
2828
|
// the turn ends as a turn.error so consumers see the error rather than a
|
|
2829
|
-
// silent empty turn.
|
|
2829
|
+
// silent empty turn. The close is STASHED, not emitted here — the
|
|
2830
2830
|
// turn's result frame (which carries the turn's usage) emits it, or
|
|
2831
2831
|
// `flush()` does if no result ever arrives (see `stashedTurnErrors`).
|
|
2832
2832
|
if (msg.error !== undefined) {
|
|
@@ -2856,13 +2856,13 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2856
2856
|
if (!nestedStashedErrors.has(turnId)) nestedStashedErrors.set(turnId, { message: errCode, code: errCode, retriable });
|
|
2857
2857
|
} else if (!stashedTurnErrors.has(turnId)) {
|
|
2858
2858
|
// First error frame wins: a turn already carrying a stashed close keeps
|
|
2859
|
-
// it (before
|
|
2859
|
+
// it (through 0.6.2, before the stash, the first error frame closed the turn).
|
|
2860
2860
|
stashedTurnErrors.set(turnId, { message: errCode, code: errCode, retriable });
|
|
2861
2861
|
}
|
|
2862
2862
|
}
|
|
2863
2863
|
|
|
2864
2864
|
// `subagent.done` is NOT emitted here: it brackets the RUN (see `openRun`),
|
|
2865
|
-
// and this message's seal is deferred (
|
|
2865
|
+
// and this message's seal is deferred (INV-MSG) until nothing can continue it.
|
|
2866
2866
|
|
|
2867
2867
|
// Record this frame's own uuid → the messageId it produced, so a LATER
|
|
2868
2868
|
// retraction naming this uuid can translate it (Finding #1). The EMITTED
|
|
@@ -2895,7 +2895,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2895
2895
|
// `ext.anthropic.frame{kind:"user"}`. The replay's `tool_use_result`
|
|
2896
2896
|
// sibling, read here for live frames, is no longer read on replays.
|
|
2897
2897
|
if ("isReplay" in msg && msg.isReplay === true) return;
|
|
2898
|
-
//
|
|
2898
|
+
// INV-MSG: a tool_result binds to the fold, and a COMPLETE-mode message
|
|
2899
2899
|
// is sealed first, exactly as the per-frame close used to. A STREAMED
|
|
2900
2900
|
// message is never sealed by a live user frame: its native boundary is its
|
|
2901
2901
|
// own stream (message_stop, with the complete assistant frames that join
|
|
@@ -3002,18 +3002,18 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3002
3002
|
// started mid-run: a background agent spanning invokes, a resume):
|
|
3003
3003
|
// open a fresh run named by this frame, so its tool.done lands in a
|
|
3004
3004
|
// turn that subagent.start opened (INV-TURN) and B-strict closes.
|
|
3005
|
-
// Through 0.7.0
|
|
3006
|
-
//
|
|
3007
|
-
// loudly"): a turn
|
|
3008
|
-
//
|
|
3009
|
-
//
|
|
3005
|
+
// Through 0.7.0 (whose cross-invoke fix covered the fallback stem
|
|
3006
|
+
// only) the never-opened case named the synthetic
|
|
3007
|
+
// `turn_<parent_tool_use_id>` label instead ("park loudly"): a turn
|
|
3008
|
+
// nobody opened, whose id REPEATED across the invokes one Reducer
|
|
3009
|
+
// folds (§8.0 "Ids across invokes"), and it parked.
|
|
3010
3010
|
toolTurnId = nestedTurnId(parentToolUseId, undefined, msg.uuid);
|
|
3011
3011
|
openRun(parentToolUseId, toolTurnId, `turn_${parentToolUseId}`);
|
|
3012
3012
|
}
|
|
3013
3013
|
} else {
|
|
3014
3014
|
toolTurnId = openTopTurnId;
|
|
3015
3015
|
}
|
|
3016
|
-
// tool_use_result sibling (SDK-injected rich MCP result
|
|
3016
|
+
// tool_use_result sibling (SDK-injected rich MCP result): carries
|
|
3017
3017
|
// structuredContent (incl. render-cache markers) + _meta.ui the block-level
|
|
3018
3018
|
// arm never sees. Applies only when the message has exactly ONE tool_result
|
|
3019
3019
|
// block (the sibling is message-level; multi-result attribution is ambiguous —
|
|
@@ -3124,10 +3124,10 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3124
3124
|
outcome,
|
|
3125
3125
|
...(denied ? {} : { isError: block.is_error === true }),
|
|
3126
3126
|
turnId: toolTurnId,
|
|
3127
|
-
// SPEC §5 tool.done adoption
|
|
3127
|
+
// SPEC §5 tool.done adoption: the Claude SDK
|
|
3128
3128
|
// closes the assistant message (message.end) BEFORE this tool_result
|
|
3129
3129
|
// arrives, so a messageId-less toolDone here has no open message to
|
|
3130
|
-
// attach to and parks the fold (
|
|
3130
|
+
// attach to and parks the fold (a reference host's fold-identity check caught
|
|
3131
3131
|
// this on a real claude tool conversation). A stable derived
|
|
3132
3132
|
// messageId engages the reducer's adoption path instead: the result
|
|
3133
3133
|
// lands in its OWN dedicated role:"tool" message.
|
|
@@ -3148,7 +3148,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3148
3148
|
// The facet's harness keys ride the same `_meta`, MERGED into the MCP
|
|
3149
3149
|
// sibling's `_meta` (never replacing it), so they fold with the
|
|
3150
3150
|
// tool-result block: the Agent run report ("anthropic/agentOutput"),
|
|
3151
|
-
//
|
|
3151
|
+
// the CLI's `tool_result_meta` entry ("anthropic/toolResultMeta"),
|
|
3152
3152
|
// and the draft.5 host records, the MCP `resourceLinks` list and the
|
|
3153
3153
|
// live denial's context. The "anthropic/" namespace is the
|
|
3154
3154
|
// harness's own: see `mergeHarnessMeta`. This tool.done carries no
|
|
@@ -3176,14 +3176,14 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3176
3176
|
}
|
|
3177
3177
|
|
|
3178
3178
|
if (msg.type === "result" && msg.subtype === "success") {
|
|
3179
|
-
//
|
|
3179
|
+
// INV-MSG: the turn is closing — seal the open assistant message first
|
|
3180
3180
|
// (message.end has always preceded the turn close).
|
|
3181
3181
|
closePendingMessage();
|
|
3182
3182
|
// B-strict: a subagent run still open (a background or unreported sub-run)
|
|
3183
3183
|
// is NOT closed by its parent's result; it closes when the framework
|
|
3184
3184
|
// reports it, or at flush.
|
|
3185
3185
|
const turnId = closingTopTurnId(msg.uuid, msg.session_id, msg.user_message_uuid);
|
|
3186
|
-
//
|
|
3186
|
+
// Since 0.6.3 (claude-agent-sdk 0.3.280): `subtype: "success"` does NOT mean the turn
|
|
3187
3187
|
// succeeded. Upstream's SDKResultMessage doc: "subtype "success" carries
|
|
3188
3188
|
// the final assistant text in result — or, with is_error true, the error
|
|
3189
3189
|
// text when the turn ended on an API error". The CLI sets is_error from
|
|
@@ -3213,14 +3213,14 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3213
3213
|
msg.structured_output !== undefined ? JsonValue.parse(msg.structured_output) : undefined;
|
|
3214
3214
|
// Emit permission_denials as tool.start + tool.done denied pairs, inside a
|
|
3215
3215
|
// dedicated carrier message: the assistant message is already sealed, and
|
|
3216
|
-
// INV-MSG
|
|
3217
|
-
//
|
|
3216
|
+
// INV-MSG forbids attaching to sealed messages / closed turns.
|
|
3217
|
+
// An API-error turn's denials take this same carrier, at the same
|
|
3218
3218
|
// point — its close is deferred to this frame (below), so the turn is
|
|
3219
3219
|
// still open here, like every other turn.
|
|
3220
3220
|
emitDenialsCarrier(turnId, msg.permission_denials, msg.session_id);
|
|
3221
3221
|
// 0.3.220: fast_mode_disabled_reason + per-model canonicalModel/provider
|
|
3222
3222
|
// ride `ext.anthropic.result-meta` before the close (no core home on
|
|
3223
|
-
// turn.done — see resultMetaPayload's doc).
|
|
3223
|
+
// turn.done — see resultMetaPayload's doc). It is emitted on an
|
|
3224
3224
|
// API-error turn too, before its close like on every other turn.
|
|
3225
3225
|
const resultMeta = resultMetaPayload(msg, stashedError !== undefined || apiErrorTurn);
|
|
3226
3226
|
if (resultMeta !== undefined) {
|
|
@@ -3277,7 +3277,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3277
3277
|
// exactly what "paused" names). The result-meta deferredToolUse carry stays.
|
|
3278
3278
|
// A later invoke that resumes the call opens its own turn, minted from its
|
|
3279
3279
|
// own frames (INV-XINV). Never keyed on terminal_reason. An is_error result
|
|
3280
|
-
// never reaches here: it closed above as turn.error
|
|
3280
|
+
// never reaches here: it closed above as turn.error, and so did a
|
|
3281
3281
|
// turn an API-error frame decided; the deferred tool found unavailable on
|
|
3282
3282
|
// resume ("tool_deferred_unavailable") is such an is_error close.
|
|
3283
3283
|
const deferredCall = readDeferredCall(msg);
|
|
@@ -3312,10 +3312,10 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3312
3312
|
|
|
3313
3313
|
if (msg.type === "result") {
|
|
3314
3314
|
// At this point msg.subtype can only be an error variant (success handled above).
|
|
3315
|
-
//
|
|
3315
|
+
// INV-MSG: seal the open assistant message before the turn close.
|
|
3316
3316
|
closePendingMessage();
|
|
3317
3317
|
const turnId = closingTopTurnId(msg.uuid, msg.session_id);
|
|
3318
|
-
//
|
|
3318
|
+
// Consume any stashed assistant-error close for this turnId — this
|
|
3319
3319
|
// frame emits it (below). Each turn has its own id now, so an entry can
|
|
3320
3320
|
// only ever be this turn's.
|
|
3321
3321
|
const stashedError = takeStashedTurnError(turnId);
|
|
@@ -3325,7 +3325,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3325
3325
|
// not throw (Tenet 6: graceful, never throws).
|
|
3326
3326
|
const errors = Array.isArray(msg.errors) ? msg.errors : [];
|
|
3327
3327
|
const subtype = typeof msg.subtype === "string" ? msg.subtype : "error_unknown";
|
|
3328
|
-
//
|
|
3328
|
+
// Since claude-agent-sdk 0.3.274: a known startup failure says why the CLI refused to
|
|
3329
3329
|
// start, and upstream frames it as "offer the fix instead of a retry" —
|
|
3330
3330
|
// so a PRESENT reason decides retriable (false, except the values upstream
|
|
3331
3331
|
// itself calls retriable; see `startupFailureRetriable`). An ABSENT reason
|
|
@@ -3355,7 +3355,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3355
3355
|
a.emitExt("anthropic", "result-meta", resultMeta);
|
|
3356
3356
|
}
|
|
3357
3357
|
if (stashedError !== undefined) {
|
|
3358
|
-
//
|
|
3358
|
+
// An assistant error frame already decided this turn's close —
|
|
3359
3359
|
// emit THAT one (the first error that ended the turn), once, never a
|
|
3360
3360
|
// second turn.error for the same turnId (INV-TURN), with this frame's
|
|
3361
3361
|
// usage (the success arm's mapping). This arm is only
|
|
@@ -3412,7 +3412,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3412
3412
|
// `retractUuids`'s doc); also the ONLY retraction path when a consumer
|
|
3413
3413
|
// never observed (or a normalizer instance never processed) the
|
|
3414
3414
|
// superseding assistant frame's own `supersedes` field directly.
|
|
3415
|
-
//
|
|
3415
|
+
// INV-MSG: a retraction can name the still-open message — seal it first so
|
|
3416
3416
|
// `message.end` never trails its own `message.remove`.
|
|
3417
3417
|
closePendingMessage();
|
|
3418
3418
|
const uuids = Array.isArray(msg.retracted_message_uuids) ? msg.retracted_message_uuids : [];
|
|
@@ -3549,7 +3549,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3549
3549
|
return a.drain();
|
|
3550
3550
|
},
|
|
3551
3551
|
flush(): AgEvent[] {
|
|
3552
|
-
//
|
|
3552
|
+
// INV-MSG: nothing can continue the deferred message now — seal it with
|
|
3553
3553
|
// its real usage rather than leaving INV-FLUSH to synthesize a bare
|
|
3554
3554
|
// `message.end`. At flush: no content is minted (INV-FLUSH (3), C1).
|
|
3555
3555
|
closePendingMessage(true);
|
|
@@ -3557,7 +3557,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3557
3557
|
// terminal (turn.abort{stream-truncated}, or its stashed API error) and
|
|
3558
3558
|
// its subagent.done, innermost first.
|
|
3559
3559
|
abortOpenRuns();
|
|
3560
|
-
//
|
|
3560
|
+
// A turn whose assistant error frame stashed its close but whose
|
|
3561
3561
|
// result frame never arrived still closes as that turn.error (no usage:
|
|
3562
3562
|
// only a result carries the turn's), never as INV-FLUSH's synthesized
|
|
3563
3563
|
// turn.abort — the outcome the frame's own close used to produce.
|