@silverprotocol/claude-agent-sdk 0.12.5 → 1.0.0-rc.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/README.md +28 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +162 -64
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +165 -68
package/src/index.ts
CHANGED
|
@@ -417,7 +417,7 @@ function toolResultContentToAgBlocks(content: NonNullable<ToolResultContent>): A
|
|
|
417
417
|
|
|
418
418
|
// Assistant-side mcp_tool_result content → AgBlock[] (distinct shape from the
|
|
419
419
|
// user-side tool_result content mapped by `toolResultContentToAgBlocks` above).
|
|
420
|
-
// A `for...of` loop, not `.map` —
|
|
420
|
+
// A `for...of` loop, not `.map` — the 2026-07-03 SDK-bump adaptation: the
|
|
421
421
|
// 0.3.199 `SDKMessage` union widened enough that `.map`/`.forEach`/`.filter`
|
|
422
422
|
// callback PARAMETER inference silently degrades to implicit `any` on this
|
|
423
423
|
// content shape (a known TS limitation calling an array method on a value typed
|
|
@@ -484,12 +484,12 @@ function isDenialKind(kind: string | undefined): boolean {
|
|
|
484
484
|
|
|
485
485
|
// Which assistant `error` codes are transient (retriable). An API-error turn's
|
|
486
486
|
// stashed top-level close and a nested frame's non-terminal `error` share it.
|
|
487
|
-
//
|
|
487
|
+
// `overloaded` (transient capacity error, a first
|
|
488
488
|
// cousin of rate_limit/server_error) joins the retriable set.
|
|
489
489
|
// `model_not_found` (a permanent misconfiguration — e.g. a stale/
|
|
490
490
|
// decommissioned model id) is deliberately EXCLUDED: explicit
|
|
491
|
-
// false-by-omission, not an oversight (
|
|
492
|
-
// adaptation
|
|
491
|
+
// false-by-omission, not an oversight (the 2026-07-03 SDK-bump
|
|
492
|
+
// adaptation). `account_on_hold` (0.3.258) is likewise a
|
|
493
493
|
// deliberate non-retriable: a billing-class code (the account is on
|
|
494
494
|
// hold — a first cousin of `billing_error`, cleared by the account
|
|
495
495
|
// holder, never by re-sending the turn).
|
|
@@ -521,7 +521,7 @@ function assistantErrorRetriable(errCode: NonNullable<SDKAssistantError>): boole
|
|
|
521
521
|
// default content.block) never apply it, so those facts drop there (a
|
|
522
522
|
// disclosed, pre-existing gap; a retraction itself still executes via
|
|
523
523
|
// `message.remove` regardless, see `drive()`).
|
|
524
|
-
// - `blockMeta
|
|
524
|
+
// - `blockMeta`: the HOST-only wrapper facts (HOST_ONLY_WRAPPER_KEYS).
|
|
525
525
|
// Only text.start / reasoning.start get it, because `reduce()` folds their
|
|
526
526
|
// `_meta` onto the block.
|
|
527
527
|
// Returns which halves ANCHORED on a folded start event: `replay` when the
|
|
@@ -554,7 +554,7 @@ function emitAssistantBlock(
|
|
|
554
554
|
case "text": {
|
|
555
555
|
// Claude's assistant message is a COMPLETE structure (not a live stream), so
|
|
556
556
|
// citations are already known at the point text.end fires — they ride the
|
|
557
|
-
// STREAMED-text citations carrier (
|
|
557
|
+
// STREAMED-text citations carrier (text.end.citations), never as
|
|
558
558
|
// a duplicate id-less supplement block.
|
|
559
559
|
const id = `${messageId}:text:${blockIndex}`;
|
|
560
560
|
const citations =
|
|
@@ -750,15 +750,17 @@ function isSDKMessage(v: unknown): v is SDKMessage {
|
|
|
750
750
|
// Task* subagent-progress family) with NO existing AgJSON vocabulary home.
|
|
751
751
|
//
|
|
752
752
|
// The Task* family (task_started/task_progress/task_updated/task_notification)
|
|
753
|
-
//
|
|
754
|
-
//
|
|
755
|
-
//
|
|
756
|
-
//
|
|
757
|
-
//
|
|
758
|
-
// subagent adoption), and task_progress/task_notification's own
|
|
753
|
+
// rides this carry whole. Its content is not folded into the subagent
|
|
754
|
+
// lifecycle: `tool_use_id` is OPTIONAL on every Task* arm (a
|
|
755
|
+
// `task_type:"local_workflow"` background task carries NONE at all — this
|
|
756
|
+
// family is a broader "tasks panel" superset, not 1:1 with Task-tool subagent
|
|
757
|
+
// adoption), and task_progress/task_notification's own
|
|
759
758
|
// `summary`/`last_tool_name` would DUPLICATE content the nested subagent's
|
|
760
|
-
// own tool_use/tool_result stream already conveys
|
|
761
|
-
//
|
|
759
|
+
// own tool_use/tool_result stream already conveys — a double fold. One part
|
|
760
|
+
// IS mapped: a task_notification correlated to an open run (by tool_use_id,
|
|
761
|
+
// or by task_id through its task_started) closes that run with its nested
|
|
762
|
+
// terminal and subagent.done (B-strict, SPEC §8.0 item 29), before the
|
|
763
|
+
// frame's carry (see `drive()`). `description` enrichment onto
|
|
762
764
|
// subagent.start was also considered and rejected: `subagent.start`'s
|
|
763
765
|
// schema carries no providerMetadata slot (only agentId/agentName), and
|
|
764
766
|
// reaching it would require bypassing the `subagentStart()` sugar
|
|
@@ -775,14 +777,14 @@ function isSDKMessage(v: unknown): v is SDKMessage {
|
|
|
775
777
|
// carried, not forced onto an unrelated event.
|
|
776
778
|
//
|
|
777
779
|
// SDKPermissionDeniedMessage is the ONE arm that DOES map onto an existing
|
|
778
|
-
// home (the
|
|
780
|
+
// home (the `<turnId>:denials` carrier) — see the dedicated
|
|
779
781
|
// branch + `deniedLiveByToolUseId` in `createClaudeNormalizer()` below; it
|
|
780
782
|
// is deliberately EXCLUDED from the carried sets here.
|
|
781
783
|
//
|
|
782
784
|
// SDKModelRefusalFallbackMessage is HALF-mapped: its retraction goes to
|
|
783
785
|
// `message.remove` (§8 item 19) and the rest of the frame has no home. It is
|
|
784
786
|
// NOT in the sets below either. Its dedicated branch does the retraction and
|
|
785
|
-
// then emits the same `ext.anthropic.frame` carry itself (
|
|
787
|
+
// then emits the same `ext.anthropic.frame` carry itself (since 2026-09-23, on
|
|
786
788
|
// the item-22 reading for half-mapped frames — see that branch).
|
|
787
789
|
//
|
|
788
790
|
// ONE uniform key — `ext.anthropic.frame{kind, frame}` (SPEC §8 item 22 /
|
|
@@ -889,7 +891,7 @@ function readUserMessageUuids(v: unknown): string[] | undefined {
|
|
|
889
891
|
return Array.isArray(v) && v.every((s): s is string => typeof s === "string") ? [...v] : undefined;
|
|
890
892
|
}
|
|
891
893
|
|
|
892
|
-
//
|
|
894
|
+
// Since 2026-09-23: the assistant-wrapper keys that ride a block's HOST-ONLY
|
|
893
895
|
// `_meta` rather than replay-load-bearing `providerMetadata` (SPEC §12). They
|
|
894
896
|
// are CLI-wrapper facts the Messages API never consumes. See the split in
|
|
895
897
|
// drive()'s assistant branch.
|
|
@@ -1099,10 +1101,13 @@ function readStartupFailureReason(v: unknown): string | undefined {
|
|
|
1099
1101
|
// The type's own lead sentence frames the whole enum as a refusal to start:
|
|
1100
1102
|
// "Why Claude Code refused to start, so a host can offer the fix instead of a
|
|
1101
1103
|
// retry." So a PRESENT reason means NOT retriable, except for the values
|
|
1102
|
-
// upstream itself EXPLICITLY describes as retriable. Through 0.3.
|
|
1103
|
-
// exactly
|
|
1104
|
+
// upstream itself EXPLICITLY describes as retriable. Through 0.3.296 that set is
|
|
1105
|
+
// exactly two values:
|
|
1104
1106
|
// - `worktree_unverified`: "the session's worktree could not be verified
|
|
1105
1107
|
// right now; retrying may succeed."
|
|
1108
|
+
// - `org_config_required_unavailable` (0.3.296): the organization's required
|
|
1109
|
+
// policy limits and managed settings "could not be loaded (network, proxy,
|
|
1110
|
+
// or an Anthropic error); retrying may succeed."
|
|
1106
1111
|
// Three more values read as if they might clear on their own, but upstream
|
|
1107
1112
|
// never says a retry helps, so each is a deliberate non-retriable — explicit
|
|
1108
1113
|
// false-by-omission (the model_not_found / account_on_hold voice in the
|
|
@@ -1118,12 +1123,13 @@ function readStartupFailureReason(v: unknown): string | undefined {
|
|
|
1118
1123
|
// Every other documented value (org_pin_api_key_conflict, provider_not_allowed,
|
|
1119
1124
|
// org_pin_mismatch, managed_settings_invalid, gateway_signin_required,
|
|
1120
1125
|
// gateway_access_denied, proxy_invalid, temp_dir_unusable, cwd_unavailable, shell_tool_missing,
|
|
1121
|
-
// worktree_resume_refused, cli_version_too_old, bypass_root
|
|
1122
|
-
//
|
|
1126
|
+
// worktree_resume_refused, cli_version_too_old, bypass_root, and 0.3.296's
|
|
1127
|
+
// org_config_refused: "signing in again or an admin's change is the fix, not a
|
|
1128
|
+
// retry") names a configuration, policy or environment fix, and an unknown future value
|
|
1123
1129
|
// inherits the enum's "offer the fix instead of a retry" framing. An ABSENT
|
|
1124
1130
|
// reason never reaches this function: the error arm keeps its old rule, so
|
|
1125
1131
|
// older producers are byte-identical.
|
|
1126
|
-
const RETRIABLE_STARTUP_FAILURE_REASONS = new Set<string>(["worktree_unverified"]);
|
|
1132
|
+
const RETRIABLE_STARTUP_FAILURE_REASONS = new Set<string>(["worktree_unverified", "org_config_required_unavailable"]);
|
|
1127
1133
|
function startupFailureRetriable(reason: string): boolean {
|
|
1128
1134
|
return RETRIABLE_STARTUP_FAILURE_REASONS.has(reason);
|
|
1129
1135
|
}
|
|
@@ -1140,6 +1146,22 @@ function apiErrorStatusRetriable(status: unknown): boolean {
|
|
|
1140
1146
|
return typeof status === "number" && (status === 429 || status >= 500);
|
|
1141
1147
|
}
|
|
1142
1148
|
|
|
1149
|
+
// The error fields an is_error success result carries by itself, when no
|
|
1150
|
+
// assistant error frame decided its close first: code = the server's
|
|
1151
|
+
// api_error_code when the CLI copied one through; else the CLI's own
|
|
1152
|
+
// terminal_reason when it names a cause other than a normal completion; else
|
|
1153
|
+
// the generic "api_error". message = the result text (on an API error, its
|
|
1154
|
+
// text), or the code when that text is empty. retriable = the HTTP status says
|
|
1155
|
+
// rate limit or server failure (see `apiErrorStatusRetriable`).
|
|
1156
|
+
function resultErrorFields(msg: SDKResultSuccessMsg): { readonly message: string; readonly code: string; readonly retriable: boolean } {
|
|
1157
|
+
const code = readApiErrorCode(msg) ?? closeCauseTerminalReason(msg.terminal_reason) ?? "api_error";
|
|
1158
|
+
return {
|
|
1159
|
+
message: typeof msg.result === "string" && msg.result !== "" ? msg.result : code,
|
|
1160
|
+
code,
|
|
1161
|
+
retriable: apiErrorStatusRetriable(msg.api_error_status),
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1143
1165
|
// ─── 0.3.220 result-frame enrichment: fast mode + per-model serving identity ──
|
|
1144
1166
|
// `fast_mode_disabled_reason` (on BOTH result arms — why fast mode was blocked)
|
|
1145
1167
|
// and `ModelUsage.canonicalModel`/`provider` (the pricing-lookup identity behind
|
|
@@ -1423,7 +1445,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1423
1445
|
// tool_result (same non-null parent_tool_use_id) can route to it instead.
|
|
1424
1446
|
const subagentTurnByParentToolUseId = new Map<string, string>();
|
|
1425
1447
|
|
|
1426
|
-
// INV-TURN (SPEC:743;
|
|
1448
|
+
// INV-TURN (SPEC:743; ruled 2026-09-23): ONE turnId names
|
|
1427
1449
|
// exactly ONE turn. A top-level turn is `turn_` + the id of the frame that
|
|
1428
1450
|
// OPENS it: the first assistant message's id (`m.id`, from a complete frame
|
|
1429
1451
|
// or a stream `message_start`) in the ordinary case, or the frame's own
|
|
@@ -1548,7 +1570,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1548
1570
|
// the user branch), and a nested one for a run this invoke never opened opens
|
|
1549
1571
|
// a fresh run the same way.
|
|
1550
1572
|
//
|
|
1551
|
-
// B-STRICT NESTED TERMINALS (draft.4, the nested-turn ruling
|
|
1573
|
+
// B-STRICT NESTED TERMINALS (draft.4, the nested-turn ruling,
|
|
1552
1574
|
// 2026-09-24). A nested turn opens
|
|
1553
1575
|
// with exactly one subagent.start and closes with exactly one turn.done |
|
|
1554
1576
|
// turn.error | turn.abort carrying its turnId, IMMEDIATELY followed by its
|
|
@@ -1584,6 +1606,10 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1584
1606
|
const closedNestedTurnIds = new Set<string>();
|
|
1585
1607
|
// Nested turnId → its FIRST API error frame's close (released at run close).
|
|
1586
1608
|
const nestedStashedErrors = new Map<string, StashedTurnError>();
|
|
1609
|
+
// Nested turnIds whose stashed close came from a refusal frame (see the
|
|
1610
|
+
// top-level `refusedTurns`): closed by the framework, the run closes as
|
|
1611
|
+
// turn.done{finishReason:"refusal"}; at flush it stays turn.error.
|
|
1612
|
+
const refusedNestedTurns = new Set<string>();
|
|
1587
1613
|
// task_started: task_id → the spawning tool_use_id, for a task_notification
|
|
1588
1614
|
// that carries only its task_id.
|
|
1589
1615
|
const toolUseIdByTaskId = new Map<string, string>();
|
|
@@ -1597,7 +1623,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1597
1623
|
openRuns.set(parentToolUseId, { turnId, parentTurnId });
|
|
1598
1624
|
a.subagentStart(turnId, parentTurnId);
|
|
1599
1625
|
}
|
|
1600
|
-
function closeRun(parentToolUseId: string, close: NestedClose): void {
|
|
1626
|
+
function closeRun(parentToolUseId: string, close: NestedClose, atFlush = false): void {
|
|
1601
1627
|
const run = openRuns.get(parentToolUseId);
|
|
1602
1628
|
if (run === undefined) return;
|
|
1603
1629
|
openRuns.delete(parentToolUseId);
|
|
@@ -1605,7 +1631,18 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1605
1631
|
closedNestedTurnIds.add(run.turnId);
|
|
1606
1632
|
const stashed = nestedStashedErrors.get(run.turnId);
|
|
1607
1633
|
nestedStashedErrors.delete(run.turnId);
|
|
1608
|
-
|
|
1634
|
+
const refused = refusedNestedTurns.delete(run.turnId);
|
|
1635
|
+
if (stashed !== undefined && refused && !atFlush) {
|
|
1636
|
+
// A refusal frame decided this nested close, and the framework ended the
|
|
1637
|
+
// run: the block signal rides finishReason (as the top-level refusal
|
|
1638
|
+
// close), with no usage (a nested terminal carries none, SPEC §8.0 item
|
|
1639
|
+
// 29). At flush it stays turn.error below (INV-FLUSH).
|
|
1640
|
+
a.closeTurnDone(run.turnId, {
|
|
1641
|
+
outcome: { type: "error", message: stashed.message, code: stashed.code, retriable: stashed.retriable },
|
|
1642
|
+
finishReason: "refusal",
|
|
1643
|
+
safety: [{ category: "refusal", blocked: true }],
|
|
1644
|
+
});
|
|
1645
|
+
} else if (stashed !== undefined) {
|
|
1609
1646
|
a.closeTurnError(run.turnId, { message: stashed.message, code: stashed.code, retriable: stashed.retriable });
|
|
1610
1647
|
} else if (close.kind === "success") {
|
|
1611
1648
|
a.closeTurnDone(run.turnId, { outcome: { type: "success" }, finishReason: "unknown" });
|
|
@@ -1620,12 +1657,12 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1620
1657
|
// INV-FLUSH orders its own closes.
|
|
1621
1658
|
function abortOpenRuns(): void {
|
|
1622
1659
|
for (const parentToolUseId of [...openRuns.keys()].reverse()) {
|
|
1623
|
-
closeRun(parentToolUseId, { kind: "aborted", reason: "stream-truncated" });
|
|
1660
|
+
closeRun(parentToolUseId, { kind: "aborted", reason: "stream-truncated" }, true);
|
|
1624
1661
|
}
|
|
1625
1662
|
}
|
|
1626
1663
|
|
|
1627
|
-
//
|
|
1628
|
-
//
|
|
1664
|
+
// Refusal-fallback retraction (§8 item 19; the 2026-07-03 SDK-bump
|
|
1665
|
+
// adaptation). `supersedes` / `retracted_message_uuids`
|
|
1629
1666
|
// name PRIOR DELIVERED MESSAGES by their wire-frame `uuid` (`msg.uuid`) — a
|
|
1630
1667
|
// DIFFERENT id space from the messageIds this facet actually emits (`m.id`
|
|
1631
1668
|
// for an assistant frame's `message.start`, `${tool_use_id}:result` for an
|
|
@@ -1639,10 +1676,10 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1639
1676
|
// home mapping): live per-denial diagnostic recorded by the standalone
|
|
1640
1677
|
// `permission_denied` frame (fires DURING the turn, before the terminal
|
|
1641
1678
|
// result) — keyed by tool_use_id so the terminal `permission_denials[]`
|
|
1642
|
-
// aggregate (the ALREADY-handled
|
|
1679
|
+
// aggregate (the ALREADY-handled `<turnId>:denials` carrier)
|
|
1643
1680
|
// can enrich its tool.done with the actual rejection text + decision-reason
|
|
1644
1681
|
// context, instead of emitting a SECOND tool.start/tool.done pair for the
|
|
1645
|
-
// same denial (
|
|
1682
|
+
// same denial (a double fold).
|
|
1646
1683
|
type LiveDenial = {
|
|
1647
1684
|
message: string;
|
|
1648
1685
|
decisionReasonType?: string;
|
|
@@ -1710,6 +1747,14 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1710
1747
|
// be its own turn's, so a stale one can no longer leak into the next turn.
|
|
1711
1748
|
type StashedTurnError = { readonly message: string; readonly code: string; readonly retriable: boolean };
|
|
1712
1749
|
const stashedTurnErrors = new Map<string, StashedTurnError>();
|
|
1750
|
+
// Top-level turnIds whose stashed close came from a refusal frame: an
|
|
1751
|
+
// assistant error frame whose message.stop_reason is "refusal". CLI 2.1.280
|
|
1752
|
+
// sends a refusal it does not retry as such a frame (error
|
|
1753
|
+
// "invalid_request"), then a result with is_error true. Its result closes
|
|
1754
|
+
// the turn as turn.done{outcome error, finishReason "refusal", safety}, so
|
|
1755
|
+
// the block signal reaches the fold (SPEC §4); a flush with no result keeps
|
|
1756
|
+
// the stashed turn.error (INV-TURN lists the flush closes).
|
|
1757
|
+
const refusedTurns = new Set<string>();
|
|
1713
1758
|
|
|
1714
1759
|
/** Remove and return the stashed error close for `turnId`, if any. */
|
|
1715
1760
|
function takeStashedTurnError(turnId: string): StashedTurnError | undefined {
|
|
@@ -1915,12 +1960,12 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
1915
1960
|
});
|
|
1916
1961
|
}
|
|
1917
1962
|
|
|
1918
|
-
// Emit what the block's content_block_stop produces — text.end (with the
|
|
1963
|
+
// Emit what the block's content_block_stop produces — text.end (with the
|
|
1919
1964
|
// streamed-citations carrier), reasoning.end + the replay-load-bearing opaque
|
|
1920
1965
|
// (same end-then-opaque order as the complete arm), the MANDATORY
|
|
1921
1966
|
// tool.args.assembled (spec §4/§8.1), or the buffered compaction block.
|
|
1922
1967
|
// `atFlush` (INV-FLUSH (3), draft.4; the fold/flush ruling, "snapshot
|
|
1923
|
-
// fold + honest flush; opaque at flush forbidden"
|
|
1968
|
+
// fold + honest flush; opaque at flush forbidden"): the
|
|
1924
1969
|
// stream ENDED with this block open, so the close lands NO new content. The
|
|
1925
1970
|
// lifecycle closes stay (text.end with its already-received citations,
|
|
1926
1971
|
// reasoning.end), and the block's scratch is DROPPED: no tool.args.assembled
|
|
@@ -2225,7 +2270,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2225
2270
|
// name kept; null kept: "no estimate yet" is a real value). Absent key ⇒
|
|
2226
2271
|
// no bag, byte-identical.
|
|
2227
2272
|
//
|
|
2228
|
-
//
|
|
2273
|
+
// Since 2026-09-23: it rides the delta's host-only `_meta`, not
|
|
2229
2274
|
// `providerMetadata`. The latter is REPLAY-LOAD-BEARING (SPEC §12: values
|
|
2230
2275
|
// that must round-trip to the provider), and the Messages API never
|
|
2231
2276
|
// consumes this CLI estimate. Consequence: `reduce()` folds `_meta` only
|
|
@@ -2257,7 +2302,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2257
2302
|
return;
|
|
2258
2303
|
}
|
|
2259
2304
|
if (d.type === "citations_delta" && b.kind === "text") {
|
|
2260
|
-
// Buffered — the
|
|
2305
|
+
// Buffered — the streamed-citations carrier is text.end, at stop.
|
|
2261
2306
|
b.citations.push(...mapCitations([d.citation]));
|
|
2262
2307
|
return;
|
|
2263
2308
|
}
|
|
@@ -2358,7 +2403,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2358
2403
|
|
|
2359
2404
|
// Emit a result's permission_denials as tool.start + tool.done denied pairs,
|
|
2360
2405
|
// inside a dedicated carrier message: the assistant message is already
|
|
2361
|
-
// sealed, and INV-MSG
|
|
2406
|
+
// sealed, and INV-MSG forbids attaching to sealed messages or
|
|
2362
2407
|
// closed turns. BOTH result arms call it, at the same point (after the seal
|
|
2363
2408
|
// and the run closes, before result-meta and the close). Through 0.6.4 only
|
|
2364
2409
|
// the success arm did, so an error-subtype result (SDKResultError declares
|
|
@@ -2431,7 +2476,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2431
2476
|
: undefined;
|
|
2432
2477
|
if (continued === undefined) closePendingMessage();
|
|
2433
2478
|
|
|
2434
|
-
//
|
|
2479
|
+
// Refusal-fallback retraction: this frame supersedes prior delivered messages
|
|
2435
2480
|
// (refusal-fallback retry) — evict them "on arrival", per the field's own
|
|
2436
2481
|
// doc, before opening the canonical replacement below. Idempotent with
|
|
2437
2482
|
// the end-of-turn `model_refusal_fallback` notice (see the `system`
|
|
@@ -2573,7 +2618,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2573
2618
|
// vendor carries; a 0.7.0 carry with zero golden moves). Three more
|
|
2574
2619
|
// @internal CLI 2.1.280 assistant-wrapper strings, UNDECLARED in sdk.d.ts
|
|
2575
2620
|
// 0.3.280 (so read through the JSON boundary), carried verbatim under their
|
|
2576
|
-
// wire names on the frame's first block's host-only `_meta` (the
|
|
2621
|
+
// wire names on the frame's first block's host-only `_meta` (the wrapper-key route,
|
|
2577
2622
|
// which folds onto block._meta; never providerMetadata, SPEC §12):
|
|
2578
2623
|
// - `error_details`: "Raw API error message: preserves details (e.g.
|
|
2579
2624
|
// prompt-too-long token counts) that user-facing content discards"
|
|
@@ -2650,8 +2695,9 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2650
2695
|
// response of the turn without one clears it. Every `fallback_credit_token`,
|
|
2651
2696
|
// at any depth, is deleted first (a provider credit token is never
|
|
2652
2697
|
// emitted). Null (every committed golden) carries nothing. Keyed by turn,
|
|
2653
|
-
// so a nested (subagent) response never touches its parent's entry
|
|
2654
|
-
//
|
|
2698
|
+
// so a nested (subagent) response never touches its parent's entry; only a
|
|
2699
|
+
// top-level turn.done carries it (a nested terminal carries no
|
|
2700
|
+
// messageMetadata).
|
|
2655
2701
|
const stopDetails: unknown = m.stop_details;
|
|
2656
2702
|
if (isJsonObject(stopDetails)) {
|
|
2657
2703
|
stopDetailsByTurn.set(turnId, { messageId, value: withoutCreditTokens(JsonValue.parse(stopDetails)) });
|
|
@@ -2696,7 +2742,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2696
2742
|
if (resumeReason !== undefined) wrapperMetaRaw["resume_reason"] = resumeReason;
|
|
2697
2743
|
}
|
|
2698
2744
|
}
|
|
2699
|
-
//
|
|
2745
|
+
// The 2026-09-23 re-cut: the bag mixes two kinds of fact. SPEC
|
|
2700
2746
|
// §12 makes `providerMetadata` REPLAY-LOAD-BEARING (values that must
|
|
2701
2747
|
// round-trip to the provider) and `_meta` host-only side metadata.
|
|
2702
2748
|
// Every key in HOST_ONLY_WRAPPER_KEYS is a CLI-wrapper fact the Messages
|
|
@@ -2737,7 +2783,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2737
2783
|
// reasoning block. A listed empty block (display "omitted") gets none.
|
|
2738
2784
|
// The index is FRAME-local. Never inferred from position, text or turn
|
|
2739
2785
|
// end; never decoded from the signature; the vendor list itself still
|
|
2740
|
-
// rides host-only `_meta` verbatim
|
|
2786
|
+
// rides host-only `_meta` verbatim.
|
|
2741
2787
|
const narrationIndexes = new Set(narrationBlockIndexes ?? []);
|
|
2742
2788
|
if (suppressed && narrationIndexes.size > 0) {
|
|
2743
2789
|
// STREAMED: the complete frame precedes its blocks' content_block_stop
|
|
@@ -2848,6 +2894,21 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2848
2894
|
// turn's result frame (which carries the turn's usage) emits it, or
|
|
2849
2895
|
// `flush()` does if no result ever arrives (see `stashedTurnErrors`).
|
|
2850
2896
|
if (msg.error !== undefined) {
|
|
2897
|
+
// A reported fix is never silently dropped (SPEC §8.0 item 33). An
|
|
2898
|
+
// error frame's turn can close without a turn.done (the stashed
|
|
2899
|
+
// turn.error, a flush, a nested run), where the turn.done
|
|
2900
|
+
// messageMetadata carry of `stop_details` above never reaches the wire:
|
|
2901
|
+
// the CLI's refusal message (error "invalid_request", stop_reason
|
|
2902
|
+
// "refusal") carries the refusal's category, explanation and
|
|
2903
|
+
// recommended model there. So it rides `message.metadata` on this
|
|
2904
|
+
// still-open message whatever the close, verbatim under its wire name,
|
|
2905
|
+
// less every provider credit token (§13.10), and folds into the
|
|
2906
|
+
// message's `metadata`. A refusal its result closes as turn.done also
|
|
2907
|
+
// carries it on that turn.done's messageMetadata. Null carries nothing.
|
|
2908
|
+
if (isJsonObject(stopDetails)) {
|
|
2909
|
+
const metadata: AgMeta = { stop_details: withoutCreditTokens(JsonValue.parse(stopDetails)) };
|
|
2910
|
+
a.emit({ type: "message.metadata", messageId, metadata });
|
|
2911
|
+
}
|
|
2851
2912
|
// The API error ended the model's output — nothing can continue this
|
|
2852
2913
|
// message. Seal it now, so `message.end` still precedes the (deferred)
|
|
2853
2914
|
// close.
|
|
@@ -2855,7 +2916,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2855
2916
|
const errCode: NonNullable<SDKAssistantError> = msg.error;
|
|
2856
2917
|
const retriable = assistantErrorRetriable(errCode);
|
|
2857
2918
|
if (msg.parent_tool_use_id !== null) {
|
|
2858
|
-
// A NESTED error frame (
|
|
2919
|
+
// A NESTED error frame (ruled 2026-09-23): the error is
|
|
2859
2920
|
// owned by the NESTED turn (SPEC:97 a subagent is a full turn with its
|
|
2860
2921
|
// own turnId; INV-OWNER, SPEC:753), so it never closes the parent. The
|
|
2861
2922
|
// parent closes as an error only when its OWN result frame says so.
|
|
@@ -2869,13 +2930,18 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2869
2930
|
// ("a nested turn has no failure outcome") is an open spec question.
|
|
2870
2931
|
a.emit({ type: "error", turnId, message: errCode, code: errCode, retriable });
|
|
2871
2932
|
// B-strict: the nested turn now HAS a failure outcome. The FIRST error
|
|
2872
|
-
// frame's close is stashed and released as the nested
|
|
2873
|
-
// its run closes (
|
|
2874
|
-
|
|
2933
|
+
// frame's close is stashed and released as the nested terminal when
|
|
2934
|
+
// its run closes (turn.done{finishReason refusal} for a refusal frame,
|
|
2935
|
+
// else turn.error; see `closeRun`), whatever the Task result reports.
|
|
2936
|
+
if (!nestedStashedErrors.has(turnId)) {
|
|
2937
|
+
nestedStashedErrors.set(turnId, { message: errCode, code: errCode, retriable });
|
|
2938
|
+
if (m.stop_reason === "refusal") refusedNestedTurns.add(turnId);
|
|
2939
|
+
}
|
|
2875
2940
|
} else if (!stashedTurnErrors.has(turnId)) {
|
|
2876
2941
|
// First error frame wins: a turn already carrying a stashed close keeps
|
|
2877
2942
|
// it (through 0.6.2, before the stash, the first error frame closed the turn).
|
|
2878
2943
|
stashedTurnErrors.set(turnId, { message: errCode, code: errCode, retriable });
|
|
2944
|
+
if (m.stop_reason === "refusal") refusedTurns.add(turnId);
|
|
2879
2945
|
}
|
|
2880
2946
|
}
|
|
2881
2947
|
|
|
@@ -2883,7 +2949,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2883
2949
|
// and this message's seal is deferred (INV-MSG) until nothing can continue it.
|
|
2884
2950
|
|
|
2885
2951
|
// Record this frame's own uuid → the messageId it produced, so a LATER
|
|
2886
|
-
// retraction naming this uuid can translate it (
|
|
2952
|
+
// retraction naming this uuid can translate it (§8 item 19). The EMITTED
|
|
2887
2953
|
// id, so a retraction still names the message that actually exists on the
|
|
2888
2954
|
// wire when this frame rode a derived carrier.
|
|
2889
2955
|
registerUuid(msg.uuid, [messageId], turnId);
|
|
@@ -2979,10 +3045,10 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
2979
3045
|
// subagent reduce() correctly routes the result.
|
|
2980
3046
|
const content = msg.message.content;
|
|
2981
3047
|
if (typeof content !== "string") {
|
|
2982
|
-
//
|
|
3048
|
+
// Prefer the REAL subagent turnId recorded at
|
|
2983
3049
|
// subagent.start time. Fall back to the old synthetic label only when
|
|
2984
3050
|
// it is unknown (e.g. a result delivered before its subagent.start
|
|
2985
|
-
// was ever observed) — defensive;
|
|
3051
|
+
// was ever observed) — defensive; the never-opened-turn guard now
|
|
2986
3052
|
// parks loudly on that label instead of fabricating a phantom turn.
|
|
2987
3053
|
// Top-level: the OPEN top-level turn, explicitly (overlapping subagent
|
|
2988
3054
|
// runs make the assembler's last-turn backfill unreliable). With no turn
|
|
@@ -3089,7 +3155,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3089
3155
|
let toolResultCount = 0;
|
|
3090
3156
|
for (const b of content) if (b.type === "tool_result") toolResultCount++;
|
|
3091
3157
|
const applySibling = sibling !== undefined && toolResultCount === 1;
|
|
3092
|
-
//
|
|
3158
|
+
// Retraction (§8 item 19): this frame's own uuid may later be named by a retraction
|
|
3093
3159
|
// (a refused leg's tombstoned tool_results, per the field's own doc) —
|
|
3094
3160
|
// collect every adopted messageId this frame produces below.
|
|
3095
3161
|
const resultMessageIds: string[] = [];
|
|
@@ -3186,7 +3252,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3186
3252
|
});
|
|
3187
3253
|
}
|
|
3188
3254
|
}
|
|
3189
|
-
//
|
|
3255
|
+
// Retraction (§8 item 19): record this frame's uuid → the adopted messageId(s) it
|
|
3190
3256
|
// produced, so a later retraction naming this uuid can translate it.
|
|
3191
3257
|
registerUuid(msg.uuid, resultMessageIds, toolTurnId);
|
|
3192
3258
|
}
|
|
@@ -3240,10 +3306,36 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3240
3306
|
// ride `ext.anthropic.result-meta` before the close (no core home on
|
|
3241
3307
|
// turn.done — see resultMetaPayload's doc). It is emitted on an
|
|
3242
3308
|
// API-error turn too, before its close like on every other turn.
|
|
3243
|
-
|
|
3309
|
+
// A refusal close: the stash came from a refusal frame, or (no frame
|
|
3310
|
+
// decided it) this is_error result itself says stop_reason "refusal".
|
|
3311
|
+
const refusedByFrame = stashedError !== undefined && refusedTurns.delete(turnId);
|
|
3312
|
+
const refusedByResult = stashedError === undefined && apiErrorTurn && msg.stop_reason === "refusal";
|
|
3313
|
+
const refusalClose = refusedByFrame || refusedByResult;
|
|
3314
|
+
const resultMeta = resultMetaPayload(msg, !refusalClose && (stashedError !== undefined || apiErrorTurn));
|
|
3244
3315
|
if (resultMeta !== undefined) {
|
|
3245
3316
|
a.emitExt("anthropic", "result-meta", resultMeta);
|
|
3246
3317
|
}
|
|
3318
|
+
if (refusalClose) {
|
|
3319
|
+
// The refusal close: turn.done, whose finishReason carries the block
|
|
3320
|
+
// signal (SPEC §4) and whose outcome keeps the error the CLI reports
|
|
3321
|
+
// (outcome.type is the facet's choice). The fields are the frame's
|
|
3322
|
+
// stashed ones, or on a result-only refusal this result's own (as its
|
|
3323
|
+
// turn.error close would carry), never an invented code. The closing
|
|
3324
|
+
// response's stop_details rides messageMetadata, as on every turn.done.
|
|
3325
|
+
const fields = stashedError ?? resultErrorFields(msg);
|
|
3326
|
+
const refusalDetails = stopDetailsByTurn.get(turnId);
|
|
3327
|
+
stopDetailsByTurn.delete(turnId);
|
|
3328
|
+
a.closeTurnDone(turnId, {
|
|
3329
|
+
outcome: { type: "error", message: fields.message, code: fields.code, retriable: fields.retriable },
|
|
3330
|
+
finishReason: "refusal",
|
|
3331
|
+
...(turnUsage !== undefined ? { usage: turnUsage } : {}),
|
|
3332
|
+
safety: [{ category: "refusal", blocked: true }],
|
|
3333
|
+
...(refusalDetails !== undefined
|
|
3334
|
+
? { messageId: refusalDetails.messageId, messageMetadata: { stop_details: refusalDetails.value } }
|
|
3335
|
+
: {}),
|
|
3336
|
+
});
|
|
3337
|
+
return;
|
|
3338
|
+
}
|
|
3247
3339
|
if (stashedError !== undefined) {
|
|
3248
3340
|
// The assistant error frame decided this close; this frame adds the
|
|
3249
3341
|
// turn's usage, the same mapping turn.done would have carried (SPEC §4:
|
|
@@ -3275,11 +3367,11 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3275
3367
|
// that never happened. `code` is free-form (SPEC :618), so this fills
|
|
3276
3368
|
// existing fields from the frame's own value; a live API error
|
|
3277
3369
|
// (terminal_reason "api_error", api-error-auth) is unchanged.
|
|
3278
|
-
const
|
|
3370
|
+
const fields = resultErrorFields(msg);
|
|
3279
3371
|
a.closeTurnError(turnId, {
|
|
3280
|
-
message:
|
|
3281
|
-
code,
|
|
3282
|
-
retriable:
|
|
3372
|
+
message: fields.message,
|
|
3373
|
+
code: fields.code,
|
|
3374
|
+
retriable: fields.retriable,
|
|
3283
3375
|
...(turnUsage !== undefined ? { usage: turnUsage } : {}),
|
|
3284
3376
|
});
|
|
3285
3377
|
return;
|
|
@@ -3295,9 +3387,10 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3295
3387
|
// exactly what "paused" names). The result-meta deferredToolUse carry stays.
|
|
3296
3388
|
// A later invoke that resumes the call opens its own turn, minted from its
|
|
3297
3389
|
// own frames (INV-XINV). Never keyed on terminal_reason. An is_error result
|
|
3298
|
-
// never reaches here: it closed above as turn.error,
|
|
3299
|
-
// turn an API-error frame decided; the deferred
|
|
3300
|
-
// resume ("tool_deferred_unavailable") is such
|
|
3390
|
+
// never reaches here: it closed above, as turn.error or, for a refusal, as
|
|
3391
|
+
// turn.done, and so did a turn an API-error frame decided; the deferred
|
|
3392
|
+
// tool found unavailable on resume ("tool_deferred_unavailable") is such
|
|
3393
|
+
// an is_error close.
|
|
3301
3394
|
const deferredCall = readDeferredCall(msg);
|
|
3302
3395
|
const stop: unknown = msg.stop_reason;
|
|
3303
3396
|
if (deferredCall !== undefined && stop === "tool_deferred") {
|
|
@@ -3337,6 +3430,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3337
3430
|
// frame emits it (below). Each turn has its own id now, so an entry can
|
|
3338
3431
|
// only ever be this turn's.
|
|
3339
3432
|
const stashedError = takeStashedTurnError(turnId);
|
|
3433
|
+
refusedTurns.delete(turnId);
|
|
3340
3434
|
// Guard `errors` and `subtype` defensively: `isSDKMessage` only checks
|
|
3341
3435
|
// `typeof v.subtype === "string"` for the result arm — it does NOT validate
|
|
3342
3436
|
// the `errors` array. A malformed message (missing errors, wrong subtype) must
|
|
@@ -3424,7 +3518,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3424
3518
|
}
|
|
3425
3519
|
|
|
3426
3520
|
if (msg.type === "system" && msg.subtype === "model_refusal_fallback") {
|
|
3427
|
-
//
|
|
3521
|
+
// Refusal-fallback retraction: the end-of-turn authoritative eviction record —
|
|
3428
3522
|
// "the complete audit record for the turn" per the field's own doc.
|
|
3429
3523
|
// Idempotent with the earlier `supersedes`-triggered eviction above (see
|
|
3430
3524
|
// `retractUuids`'s doc); also the ONLY retraction path when a consumer
|
|
@@ -3435,7 +3529,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3435
3529
|
closePendingMessage();
|
|
3436
3530
|
const uuids = Array.isArray(msg.retracted_message_uuids) ? msg.retracted_message_uuids : [];
|
|
3437
3531
|
retractUuids(uuids);
|
|
3438
|
-
//
|
|
3532
|
+
// The 2026-09-23 re-cut: only the retraction has a core home.
|
|
3439
3533
|
// A notice yields 0..N `message.remove`s, so there is no single event to
|
|
3440
3534
|
// hang the rest on. The rest of the frame is the switch itself: `trigger`, `direction`
|
|
3441
3535
|
// ('retry'; 'revert'/'sticky' are "no longer emitted" per 0.3.280),
|
|
@@ -3451,10 +3545,10 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3451
3545
|
// whose content that home already conveys, and `message.remove` conveys
|
|
3452
3546
|
// only the retraction. So the residual fields fall under item 22's
|
|
3453
3547
|
// no-drop MUST, and the whole frame rides. (A clarifying
|
|
3454
|
-
// line for item 22 is queued for
|
|
3548
|
+
// line for item 22 is queued for review.) `ext.*` is live-only
|
|
3455
3549
|
// and non-folding, so the second copy of `retracted_message_uuids`
|
|
3456
|
-
// cannot double-fold
|
|
3457
|
-
//
|
|
3550
|
+
// cannot double-fold. This is the only producer for the candidate
|
|
3551
|
+
// `turn.model-switch` event. Synthetic-only (needs a classifier refusal).
|
|
3458
3552
|
a.emitExt("anthropic", "frame", { kind: msg.subtype, frame: carryVerbatim(msg) });
|
|
3459
3553
|
return;
|
|
3460
3554
|
}
|
|
@@ -3463,8 +3557,8 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3463
3557
|
// Fixture-drift ratchet finding: this standalone LIVE denial notice is
|
|
3464
3558
|
// the SAME fact the already-handled `SDKResultMessage.permission_denials[]`
|
|
3465
3559
|
// aggregate turns into a tool.start+tool.done{denied} pair inside the
|
|
3466
|
-
//
|
|
3467
|
-
// second pair here (
|
|
3560
|
+
// `<turnId>:denials` carrier (above) — do NOT emit a
|
|
3561
|
+
// second pair here (a double fold). Record this frame's
|
|
3468
3562
|
// richer diagnostic fields only; the aggregate handler consumes them.
|
|
3469
3563
|
//
|
|
3470
3564
|
// `decision_reason_code` (CLI 2.1.280, @internal — UNDECLARED in sdk.d.ts
|
|
@@ -3507,7 +3601,6 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3507
3601
|
// comment above for the full per-arm reasoning, SPEC §8 item 22).
|
|
3508
3602
|
const carriedKind = anthropicFrameKind(msg);
|
|
3509
3603
|
if (carriedKind !== undefined) {
|
|
3510
|
-
a.emitExt("anthropic", "frame", { kind: carriedKind, frame: carryVerbatim(msg) });
|
|
3511
3604
|
// B-strict: a background sub-run's outcome arrives as task_notification,
|
|
3512
3605
|
// correlated to its run by tool_use_id (or by task_id through the
|
|
3513
3606
|
// task_started frame that named it).
|
|
@@ -3526,6 +3619,9 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3526
3619
|
}
|
|
3527
3620
|
}
|
|
3528
3621
|
}
|
|
3622
|
+
// The carry rides after the home events this frame produced (a correlated
|
|
3623
|
+
// task_notification's nested terminal and subagent.done), SPEC §8.0 item 22.
|
|
3624
|
+
a.emitExt("anthropic", "frame", { kind: carriedKind, frame: carryVerbatim(msg) });
|
|
3529
3625
|
return;
|
|
3530
3626
|
}
|
|
3531
3627
|
|
|
@@ -3569,7 +3665,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3569
3665
|
flush(): AgEvent[] {
|
|
3570
3666
|
// INV-MSG: nothing can continue the deferred message now — seal it with
|
|
3571
3667
|
// its real usage rather than leaving INV-FLUSH to synthesize a bare
|
|
3572
|
-
// `message.end`. At flush: no content is minted (INV-FLUSH (3)
|
|
3668
|
+
// `message.end`. At flush: no content is minted (INV-FLUSH (3)).
|
|
3573
3669
|
closePendingMessage(true);
|
|
3574
3670
|
// B-strict: every subagent run still open closes here with its nested
|
|
3575
3671
|
// terminal (turn.abort{stream-truncated}, or its stashed API error) and
|
|
@@ -3583,6 +3679,7 @@ function createInnerClaudeNormalizer(options: ClaudeNormalizerOptions, invokeSte
|
|
|
3583
3679
|
a.closeTurnError(turnId, { ...stashed });
|
|
3584
3680
|
}
|
|
3585
3681
|
stashedTurnErrors.clear();
|
|
3682
|
+
refusedTurns.clear();
|
|
3586
3683
|
return a.flush();
|
|
3587
3684
|
},
|
|
3588
3685
|
};
|