@agentclientprotocol/claude-agent-acp 0.58.1 → 0.59.0

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/acp-agent.js CHANGED
@@ -48,6 +48,47 @@ const DEFAULT_FORCE_CANCEL_GRACE_MS = 30_000;
48
48
  * completed/stalled without the host turn resolving (issue #825). */
49
49
  const TURN_NO_RESULT_MESSAGE = "The turn ended without a result: the agent went idle while this prompt was still in flight " +
50
50
  "(e.g. the model stream dropped mid-turn). Any partial output may be incomplete; please retry.";
51
+ /** Result-message origin kinds that mark an AUTONOMOUS cycle — work the
52
+ * model did on its own (a task-notification followup, a peer/coordinator/
53
+ * observer message it handled) rather than the user's prompt. Absent,
54
+ * `human`, and `channel` origins are the user's own turn (this adapter's
55
+ * prompts arrive as the ACP channel on some CLI configurations — ALL
56
+ * channel servers are treated as user, so a foreign channel integration's
57
+ * autonomously-handled result is misclassified as the user's; accepted,
58
+ * see below), and `auto-continuation` continues the user's turn, so its
59
+ * result is the turn's real terminal.
60
+ *
61
+ * Deliberately fail-OPEN: an unknown future kind defaults to the user
62
+ * lane. Misrouting a USER result into the autonomous lane hangs the
63
+ * prompt un-detectably (the result is skipped, its trailing idle absorbed
64
+ * as owed, so the #825 detector can't fire); misrouting an autonomous
65
+ * result into the user lane is the bounded misattribution class this set
66
+ * exists to reduce. */
67
+ const AUTONOMOUS_RESULT_ORIGINS = new Set([
68
+ "task-notification",
69
+ "peer",
70
+ "coordinator",
71
+ "observer",
72
+ "observer-activity",
73
+ ]);
74
+ /** Whether this turn's terminal result arrived but its settlement is being
75
+ * held for background subagents it spawned (see Turn.deferredSettle). The
76
+ * single spelling of the hold predicate, shared by the consumer's settle
77
+ * lanes and cancel(). */
78
+ function isHeldOpen(turn) {
79
+ return turn != null && turn.deferredSettle !== undefined && !turn.settled;
80
+ }
81
+ /** Disarm the force-cancel backstop (see Session.forceCancelTimer). Every
82
+ * path that settles the active turn must run this so a timer can never fire
83
+ * on an already-settled turn — and must leave the field undefined, or the
84
+ * arm site's !forceCancelTimer guard would refuse to arm the backstop for
85
+ * the NEXT turn's cancel. */
86
+ function disarmForceCancel(session) {
87
+ if (session.forceCancelTimer) {
88
+ clearTimeout(session.forceCancelTimer);
89
+ session.forceCancelTimer = undefined;
90
+ }
91
+ }
51
92
  /** Compute a stable fingerprint of the session-defining params so we can
52
93
  * detect when a loadSession/resumeSession call requires tearing down and
53
94
  * recreating the underlying Query process. MCP servers are sorted by name
@@ -56,6 +97,67 @@ function computeSessionFingerprint(params) {
56
97
  const servers = [...(params.mcpServers ?? [])].sort((a, b) => a.name.localeCompare(b.name));
57
98
  return JSON.stringify({ cwd: params.cwd, mcpServers: servers });
58
99
  }
100
+ /**
101
+ * Advance the lexer state across the fragment appended since the last delta:
102
+ * just enough JSON awareness (string/escape, nesting depth) to spot commas
103
+ * that sit at the top level of the input object — everything before such a
104
+ * comma is a set of complete fields. Returns true once the input object's
105
+ * closing brace arrives.
106
+ */
107
+ function scanStreamedToolInput(state) {
108
+ let complete = false;
109
+ for (let index = state.scannedTo; index < state.partialJson.length; index++) {
110
+ const character = state.partialJson[index];
111
+ if (state.inString) {
112
+ if (state.escaped) {
113
+ state.escaped = false;
114
+ }
115
+ else if (character === "\\") {
116
+ state.escaped = true;
117
+ }
118
+ else if (character === '"') {
119
+ state.inString = false;
120
+ }
121
+ continue;
122
+ }
123
+ if (character === '"') {
124
+ state.inString = true;
125
+ }
126
+ else if (character === "{") {
127
+ state.objectDepth++;
128
+ }
129
+ else if (character === "}") {
130
+ state.objectDepth--;
131
+ if (state.objectDepth === 0) {
132
+ complete = true;
133
+ }
134
+ }
135
+ else if (character === "[") {
136
+ state.arrayDepth++;
137
+ }
138
+ else if (character === "]") {
139
+ state.arrayDepth--;
140
+ }
141
+ else if (character === "," && state.objectDepth === 1 && state.arrayDepth === 0) {
142
+ state.lastTopLevelComma = index;
143
+ }
144
+ }
145
+ state.scannedTo = state.partialJson.length;
146
+ return complete;
147
+ }
148
+ /** Parse the complete top-level fields before a top-level comma by closing the
149
+ * object at that boundary. */
150
+ function recoveredToolInput(prefix) {
151
+ try {
152
+ const value = JSON.parse(prefix + "}");
153
+ return value !== null && typeof value === "object" && !Array.isArray(value)
154
+ ? value
155
+ : undefined;
156
+ }
157
+ catch {
158
+ return undefined;
159
+ }
160
+ }
59
161
  export async function claudeCliPath() {
60
162
  if (process.env.CLAUDE_CODE_EXECUTABLE) {
61
163
  return process.env.CLAUDE_CODE_EXECUTABLE;
@@ -188,6 +290,36 @@ export function stripLocalCommandMetadata(content) {
188
290
  export function isLocalCommandMetadata(content) {
189
291
  return stripLocalCommandMetadata(content) === null;
190
292
  }
293
+ /**
294
+ * True for the synthetic assistant message the CLI injects into the transcript
295
+ * when a turn fails authentication (e.g. "Not logged in · Please run /login",
296
+ * "Session expired. Please run /login to sign in again."). The `/login`
297
+ * instruction is Claude Code TUI-specific and meaningless to ACP clients
298
+ * (issue #863). The live prompt loop suppresses the text and fails the turn
299
+ * with `authRequired` so the client can run its own auth flow; replay must
300
+ * skip it too — both for parity with what the client saw live and because the
301
+ * message stays in the transcript forever, so it would resurface on every
302
+ * session/load even after the user has logged back in.
303
+ *
304
+ * Takes the API message (`message.message`), which replay only knows as
305
+ * `unknown`. The persisted record's structured `error: "authentication_failed"`
306
+ * marker is stripped by `getSessionMessages`, so the synthetic model + text is
307
+ * all both paths have to match on.
308
+ */
309
+ export function isSyntheticLoginMessage(apiMessage) {
310
+ if (!apiMessage || typeof apiMessage !== "object") {
311
+ return false;
312
+ }
313
+ const { model, content } = apiMessage;
314
+ if (model !== "<synthetic>" || !Array.isArray(content) || content.length !== 1) {
315
+ return false;
316
+ }
317
+ const block = content[0];
318
+ return (!!block &&
319
+ block.type === "text" &&
320
+ typeof block.text === "string" &&
321
+ block.text.includes("Please run /login"));
322
+ }
191
323
  const PERMISSION_MODE_ALIASES = {
192
324
  auto: "auto",
193
325
  default: "default",
@@ -666,23 +798,34 @@ export class ClaudeAcpAgent {
666
798
  // gateways that don't carry a stable/matching id across the stream and the
667
799
  // consolidated message. Reset after each consolidated message consumes it.
668
800
  const streamedBlocks = [];
801
+ // Tool-use blocks start streaming before their JSON input. Keep the
802
+ // partial input per parent message and block index so completed top-level
803
+ // fields can refine the pending tool call while it streams. Entries are
804
+ // dropped at block/message boundaries; the whole map is swept when a turn
805
+ // settles, since an interrupted subagent stream (keyed by a
806
+ // parent_tool_use_id that never recurs) has no boundary event of its own.
807
+ const streamedToolInputs = new Map();
669
808
  // Stop reason accumulated for the active turn (result subtype, refusal,
670
809
  // max_tokens, …). Reset per turn; read when the turn settles at idle.
671
810
  let stopReason = "end_turn";
672
- // How many trailing `session_state_changed: idle` messages are already
673
- // accounted for: every user-turn result that terminates a turn (settle,
674
- // reject, or orphan skip) is followed by one, as is a cancelled turn
675
- // settled by the next turn's echo hand-off. The idle handler absorbs owed
676
- // idles; an idle that arrives when NONE is owed while the active turn is
677
- // still unsettled means the SDK ended the turn without ever emitting its
678
- // result, so the turn will never settle on its own (issue #825).
679
- // Stream-level debt, deliberately NOT reset per turn: a lagged idle can
680
- // arrive after the next turn has already activated (issue #773), and the
681
- // debt is what attributes it to the turn that owed it. Over-counting (an
682
- // idle the SDK never emits, e.g. CLI binaries without session-state
683
- // events — issue #497) is benign: the counter just absorbs one future
684
- // idle, and detection degrades to the status quo rather than misfiring.
685
- let owedTrailingIdles = 0;
811
+ /** The consumer's single send chokepoint: every `sessionUpdate` in this
812
+ * loop goes through here (never `this.client.sessionUpdate` directly) so
813
+ * answer-delivery tracking is a property of sending, not something each
814
+ * emission site must remember. A top-level `agent_message_chunk` marks
815
+ * the stretch's answer as delivered; subagent-attributed chunks are
816
+ * recognizable by the `parentToolUseId` meta that toAcpNotifications
817
+ * stamps from `parent_tool_use_id`, and never reach the top-level feed
818
+ * as the turn's answer. */
819
+ const sendUpdate = async (notification) => {
820
+ const { update } = notification;
821
+ if (update.sessionUpdate === "agent_message_chunk") {
822
+ const claudeMeta = update._meta?.claudeCode;
823
+ if (!claudeMeta?.parentToolUseId) {
824
+ session.emittedAssistantText = true;
825
+ }
826
+ }
827
+ await this.client.sessionUpdate(notification);
828
+ };
686
829
  const resetTurnScratch = () => {
687
830
  lastAssistantTotalUsage = null;
688
831
  lastAssistantUsage = null;
@@ -718,6 +861,31 @@ export class ClaudeAcpAgent {
718
861
  session.activeTurn = turn;
719
862
  session.cancelled = false;
720
863
  session.pendingOrphanResults = 0;
864
+ session.orphanCommands?.clear();
865
+ // Two-phase sweep of registry entries the level signal ended (see
866
+ // the endedPerLevel field doc): armed at the first activation,
867
+ // deleted at the second — the same activation-time self-heal as the
868
+ // orphan lanes, and the growth bound for leaked entries whose settle
869
+ // bookends never arrive. The one-activation grace lets a corrective
870
+ // inclusive level rescue a live async agent that a racing payload
871
+ // absent-marked (deletion is irreversible: levels never ADD entries).
872
+ // Local-only commands don't advance the clock: two quick /context
873
+ // calls would otherwise burn the whole grace in seconds of wall time
874
+ // while the corrective level is still in flight, and they interact
875
+ // with no tasks — a later real turn still bounds growth.
876
+ if (!turn.isLocalOnlyCommand) {
877
+ for (const [taskId, record] of session.liveBackgroundTasks) {
878
+ if (!record.endedPerLevel) {
879
+ continue;
880
+ }
881
+ if (record.endedPerLevel === "sweep-armed") {
882
+ session.liveBackgroundTasks.delete(taskId);
883
+ }
884
+ else {
885
+ record.endedPerLevel = "sweep-armed";
886
+ }
887
+ }
888
+ }
721
889
  resetTurnScratch();
722
890
  };
723
891
  /** Ensure there is an active turn before a user-turn result that carries no
@@ -737,18 +905,165 @@ export class ClaudeAcpAgent {
737
905
  * result), so we skip those and only promote once the count is drained. */
738
906
  const ensureActiveTurn = () => {
739
907
  if (session.activeTurn) {
740
- return;
741
- }
742
- const head = (session.turnQueue ?? []).find((t) => !t.settled);
743
- if (!head) {
744
- return;
908
+ if (!isHeldOpen(session.activeTurn)) {
909
+ return;
910
+ }
911
+ // A held turn (Turn.deferredSettle) already produced its result, so
912
+ // this incoming user-turn result cannot be its — it belongs to the
913
+ // next queued command (an echo-less one, e.g. `/context` sent while
914
+ // the hold drains; a normal prompt's echo would have handed the held
915
+ // turn off before its result). Settle the held turn with its
916
+ // recorded outcome — the user moving on outranks the hold, same
917
+ // contract as the echo hand-off — and fall through to promote the
918
+ // queue head, which this result belongs to. Without this, the head
919
+ // would never be promoted (echo-less turns have no other promotion
920
+ // path) and its prompt would hang, while this result's outcome
921
+ // overwrote the held turn's. Orphan lanes below are necessarily
922
+ // empty while a turn is held: orphans are seeded by cancel(), which
923
+ // inline-settles a held turn, and activation cleared older ones.
924
+ // settleActive also closes the held turn's delivery stretch, so the
925
+ // promoted command's own delivery decision is not judged against the
926
+ // held turn's followup text (issue #453) — the caller snapshots the
927
+ // flag AFTER this runs.
928
+ settleActive(session.activeTurn.deferredSettle);
745
929
  }
930
+ // Orphan accounting runs BEFORE the head check: an orphan's echo-less
931
+ // result can arrive with an EMPTY queue (the common post-cancel
932
+ // timeline — the active turn settled at the interrupt's idle and the
933
+ // user hasn't typed yet), and it must still be consumed here. Skipping
934
+ // the bookkeeping when there is nothing to promote would leave a
935
+ // phantom entry/count that swallows the next live echo-less result
936
+ // (e.g. /compact) instead.
746
937
  if ((session.pendingOrphanResults ?? 0) > 0) {
747
938
  session.pendingOrphanResults--;
748
939
  return;
749
940
  }
941
+ // msg_lifecycle_v1 lane. Attribute this echo-less result using the
942
+ // entries' states — turns run sequentially and frames arrive in stream
943
+ // order, so at any result: every "zombie" is from an already-dead turn
944
+ // whose own result already passed before the frame that created the
945
+ // newest entry (or never existed), every "started" entry was dispatched
946
+ // into THE turn that emitted this result (an older turn's entries got
947
+ // their terminal frames before a newer turn's "started" frames), and a
948
+ // "pending" entry was not dispatched before it. One result therefore
949
+ // covers ALL started and zombie entries at once (N coalesced commands
950
+ // share ONE result); their outstanding terminal frames then no-op on
951
+ // the missing entries. NOTE this ordering argument is asserted from
952
+ // observed CLI behavior, not a documented wire contract — if a dead
953
+ // turn's late result could lag past the NEXT turn's dispatch frames,
954
+ // deleting a zombie and a started entry on one result would
955
+ // double-consume it. The unexpected-transition logging in the frame
956
+ // handler is the tripwire for that class of drift.
957
+ if (session.orphanCommands?.size) {
958
+ let consumedOrphanResult = false;
959
+ let oldestPending;
960
+ for (const [uuid, state] of session.orphanCommands) {
961
+ if (state === "started" || state === "zombie") {
962
+ consumedOrphanResult = true;
963
+ session.orphanCommands.delete(uuid);
964
+ }
965
+ else {
966
+ oldestPending ??= uuid;
967
+ }
968
+ }
969
+ if (consumedOrphanResult) {
970
+ return;
971
+ }
972
+ if (oldestPending !== undefined) {
973
+ // No dispatch was seen before this result, so it is very likely a
974
+ // live turn's — but a lost "started" frame would mean it IS the
975
+ // orphan's (dup-over-loss: prefer one wrong skip over
976
+ // misattributing a dead turn's outcome to a live prompt). Grant
977
+ // each pending entry exactly one skip, like the count lane did.
978
+ session.orphanCommands.delete(oldestPending);
979
+ return;
980
+ }
981
+ }
982
+ const head = firstUnsettledQueuedTurn();
983
+ if (!head) {
984
+ return;
985
+ }
750
986
  activateTurn(head);
751
987
  };
988
+ /** Result-time bookkeeping that must run whether or not the result can be
989
+ * attributed to a turn. (1) Latch `commandResultSeen` on every queued
990
+ * turn whose command is known dispatched with no terminal frame yet —
991
+ * the emitting turn is the one it was dispatched (possibly folded) into,
992
+ * so its result has now passed; a later cancel() must not seed an orphan
993
+ * entry that waits for it (see Turn.commandResultSeen). (2) When a turn
994
+ * is ACTIVE, the result is attributed to it and never reaches
995
+ * ensureActiveTurn — but it still covers the map's started entries
996
+ * (commands folded into the active turn share its result) and zombies
997
+ * (their late results have already passed or never existed), so drain
998
+ * them here or they would zombify/linger and swallow a later live
999
+ * echo-less result. */
1000
+ const recordResultForOrphanCommands = () => {
1001
+ for (const turn of session.turnQueue ?? []) {
1002
+ if (!turn.settled && turn.commandStarted && !turn.commandFinished) {
1003
+ turn.commandResultSeen = true;
1004
+ }
1005
+ }
1006
+ if (session.activeTurn && session.orphanCommands?.size) {
1007
+ for (const [uuid, state] of session.orphanCommands) {
1008
+ if (state === "started" || state === "zombie") {
1009
+ session.orphanCommands.delete(uuid);
1010
+ }
1011
+ }
1012
+ }
1013
+ };
1014
+ /** The unsettled in-flight turn owning this prompt uuid, if any. */
1015
+ const findUnsettledTurn = (uuid) => (session.turnQueue ?? []).find((t) => t.promptUuid === uuid && !t.settled);
1016
+ /** The first queued turn still awaiting its outcome, if any — the single
1017
+ * spelling of "a prompt is pending" shared by the head promotion and
1018
+ * the autonomous stretch-close guard. */
1019
+ const firstUnsettledQueuedTurn = () => (session.turnQueue ?? []).find((t) => !t.settled);
1020
+ /** Whether any background subagent this turn spawned is still live —
1021
+ * while true, the turn's settlement stays deferred so the subagent's
1022
+ * output and permission requests land inside it (see
1023
+ * Turn.deferredSettle). */
1024
+ const turnAwaitingSubagents = (turn) => {
1025
+ if (!turn.spawnedTaskIds?.size) {
1026
+ return false;
1027
+ }
1028
+ for (const taskId of turn.spawnedTaskIds) {
1029
+ const record = session.liveBackgroundTasks.get(taskId);
1030
+ // The isSubagent read is defense in depth for the shells-never-defer
1031
+ // contract: spawnedTaskIds only ever holds subagent ids today, but a
1032
+ // future add site must not silently let a long-lived shell hold a
1033
+ // prompt open. endedPerLevel entries are kept for attribution only —
1034
+ // the level signal says the task is gone (or its bookends were
1035
+ // lost), so a hold must not wait on them.
1036
+ if (record?.isSubagent && !record.endedPerLevel) {
1037
+ return true;
1038
+ }
1039
+ }
1040
+ return false;
1041
+ };
1042
+ /** Settle the active turn's stored deferred outcome once none of its
1043
+ * spawned subagents is live. The single drain rule shared by the
1044
+ * followup-result and idle settle sites, so the two lanes can't drift. */
1045
+ const settleDeferredIfDrained = () => {
1046
+ const turn = session.activeTurn;
1047
+ if (isHeldOpen(turn) && !turnAwaitingSubagents(turn)) {
1048
+ settleActive(turn.deferredSettle);
1049
+ }
1050
+ };
1051
+ /** Settle the active turn with `outcome` now — unless subagents it
1052
+ * spawned are still live, in which case store the outcome and hold the
1053
+ * turn open (see Turn.deferredSettle). Every result-time settle of a
1054
+ * turn that can have spawned subagents must route through here: a site
1055
+ * calling settleActive directly bypasses the hold and re-opens the
1056
+ * out-of-turn permission deadlock (issue #866) through its lane. */
1057
+ const settleOrDefer = (outcome) => {
1058
+ if (session.activeTurn &&
1059
+ !session.activeTurn.settled &&
1060
+ turnAwaitingSubagents(session.activeTurn)) {
1061
+ session.activeTurn.deferredSettle = outcome;
1062
+ }
1063
+ else {
1064
+ settleActive(outcome);
1065
+ }
1066
+ };
752
1067
  /** Settle the active turn's deferred exactly once, disarm the force-cancel
753
1068
  * backstop (the turn is over), and drop it from the queue. */
754
1069
  const settleActive = (result) => {
@@ -756,22 +1071,34 @@ export class ClaudeAcpAgent {
756
1071
  if (!turn || turn.settled) {
757
1072
  return;
758
1073
  }
1074
+ // Captured before the settled flip below (isHeldOpen tests !settled).
1075
+ const wasHeld = isHeldOpen(turn);
759
1076
  turn.settled = true;
760
- if (session.forceCancelTimer) {
761
- clearTimeout(session.forceCancelTimer);
762
- session.forceCancelTimer = undefined;
763
- }
1077
+ disarmForceCancel(session);
764
1078
  session.turnQueue = (session.turnQueue ?? []).filter((t) => t !== turn);
765
1079
  session.activeTurn = null;
1080
+ streamedToolInputs.clear();
1081
+ if (wasHeld) {
1082
+ // Settling a held turn is its delivery-stretch boundary: the turn's
1083
+ // answer finished long ago, so text streamed since the last boundary
1084
+ // is normally its followups' — left latched it would suppress a
1085
+ // following replayed turn's issue-#453 result-text fallback (the
1086
+ // common post-hold sequence). Known trade: at the echo hand-off an
1087
+ // incoming turn's pre-echo deltas share this one boolean, so a
1088
+ // STREAMING replay on a usage-omitting backend could re-emit its
1089
+ // answer — the flag cannot attribute text to a turn before its
1090
+ // echo, and the suppression direction is the common one, so the
1091
+ // clear wins. Every held-settle lane inherits this: the drain
1092
+ // settle, both hand-offs, and stream-done; cancel()'s inline mirror
1093
+ // carries its own copy.
1094
+ session.emittedAssistantText = false;
1095
+ }
766
1096
  turn.resolve(result);
767
1097
  };
768
1098
  /** Reject the active turn (auth required, error result, …) without tearing
769
1099
  * down the consumer: the stream continues to idle and later turns proceed. */
770
1100
  const failActive = (error) => {
771
- if (session.forceCancelTimer) {
772
- clearTimeout(session.forceCancelTimer);
773
- session.forceCancelTimer = undefined;
774
- }
1101
+ disarmForceCancel(session);
775
1102
  const turn = session.activeTurn;
776
1103
  if (!turn || turn.settled) {
777
1104
  return;
@@ -779,14 +1106,17 @@ export class ClaudeAcpAgent {
779
1106
  turn.settled = true;
780
1107
  session.turnQueue = (session.turnQueue ?? []).filter((t) => t !== turn);
781
1108
  session.activeTurn = null;
1109
+ streamedToolInputs.clear();
1110
+ // A failed turn's stretch is over, and some failure lanes (the issue
1111
+ // #825 idle-fail) never see the result whose `finally` would close it —
1112
+ // start the next stretch clean, or its stale delivery record would
1113
+ // suppress the next turn's issue-#453 result-text fallback.
1114
+ session.emittedAssistantText = false;
782
1115
  turn.reject(error);
783
1116
  };
784
1117
  /** Reject every in-flight turn — used when the stream dies. */
785
1118
  const failAllTurns = (error) => {
786
- if (session.forceCancelTimer) {
787
- clearTimeout(session.forceCancelTimer);
788
- session.forceCancelTimer = undefined;
789
- }
1119
+ disarmForceCancel(session);
790
1120
  const turns = session.activeTurn
791
1121
  ? [session.activeTurn, ...(session.turnQueue ?? []).filter((t) => t !== session.activeTurn)]
792
1122
  : [...(session.turnQueue ?? [])];
@@ -794,8 +1124,18 @@ export class ClaudeAcpAgent {
794
1124
  session.turnQueue = [];
795
1125
  for (const turn of turns) {
796
1126
  if (!turn.settled) {
1127
+ const wasHeld = isHeldOpen(turn);
797
1128
  turn.settled = true;
798
- turn.reject(error);
1129
+ if (wasHeld) {
1130
+ // A held turn's answer already streamed and its outcome is
1131
+ // recorded — a stream death during the post-answer hold is a
1132
+ // background failure, not the turn's. Resolve with the real
1133
+ // outcome, mirroring the stream-done path.
1134
+ turn.resolve(turn.deferredSettle);
1135
+ }
1136
+ else {
1137
+ turn.reject(error);
1138
+ }
799
1139
  }
800
1140
  }
801
1141
  };
@@ -836,9 +1176,45 @@ export class ClaudeAcpAgent {
836
1176
  // turn being abandoned. Stale counts self-heal: activation resets
837
1177
  // them (see activateTurn).
838
1178
  if (session.activeTurn && !session.activeTurn.settled) {
839
- session.pendingOrphanResults = (session.pendingOrphanResults ?? 0) + 1;
1179
+ // Seed by what the frames already told us, mirroring cancel()'s
1180
+ // queued-turn sweep — the consumer may have drained the wedged
1181
+ // turn's result and/or terminal frame before the backstop fired,
1182
+ // and an entry seeded for a result or frame that is already
1183
+ // spent would never drain (it would swallow an unrelated later
1184
+ // echo-less result instead).
1185
+ const active = session.activeTurn;
1186
+ if (active.commandFinished === "completed" || active.commandFinished === "discarded") {
1187
+ // Finished SDK-side; any result already passed. Nothing to
1188
+ // track.
1189
+ }
1190
+ else if (active.commandFinished === "cancelled") {
1191
+ // Aborted after dispatch: its late result may still come —
1192
+ // unless it already did.
1193
+ if (!active.commandResultSeen) {
1194
+ this.trackOrphanCommand(session, active.promptUuid, "zombie");
1195
+ }
1196
+ }
1197
+ else if (active.commandResultSeen) {
1198
+ // Its result was already consumed (dropped at the cancelled
1199
+ // guard); only the terminal frame is outstanding, which no-ops
1200
+ // with no entry. Nothing to track.
1201
+ }
1202
+ else {
1203
+ // The wedged turn WAS dispatched (it's active), so track it
1204
+ // "started": its late result (if the SDK recovers) is skipped
1205
+ // echo-less, and its terminal frame — or that skip plus
1206
+ // activation's clear when the frame is lost to the wedge — is
1207
+ // what drains it.
1208
+ this.trackOrphanCommand(session, active.promptUuid, "started");
1209
+ }
840
1210
  }
841
1211
  settleActive({ stopReason: "cancelled", usage: sessionUsage(session) });
1212
+ // The cancelled turn's result may never come (that's why the
1213
+ // backstop fired) — close its delivery stretch here so partial
1214
+ // streamed text can't suppress the next turn's issue-#453 fallback.
1215
+ // If a late orphan result does arrive, its `finally` clears again;
1216
+ // FIFO ordering means no live turn's text can have streamed yet.
1217
+ session.emittedAssistantText = false;
842
1218
  // If the session is being torn down, abandon the in-flight next()
843
1219
  // (swallowing any later rejection so it can't surface as unhandled)
844
1220
  // and stop; otherwise re-arm and keep consuming — `pendingNext`
@@ -862,11 +1238,15 @@ export class ClaudeAcpAgent {
862
1238
  // turn's real outcome.
863
1239
  //
864
1240
  // Settle the turn that was in flight so its prompt() doesn't hang:
865
- // cancelled if a cancel is pending, otherwise the accumulated outcome.
866
- settleActive({
867
- stopReason: session.cancelled ? "cancelled" : stopReason,
868
- usage: sessionUsage(session),
869
- });
1241
+ // cancelled if a cancel is pending, otherwise the outcome a
1242
+ // deferred turn already recorded (see Turn.deferredSettle) or the
1243
+ // accumulated scratch outcome. The scratch currently still equals
1244
+ // a deferred turn's stored outcome (followup results never mutate
1245
+ // it), but the stored one is the authoritative source.
1246
+ const inFlight = session.activeTurn;
1247
+ settleActive(session.cancelled
1248
+ ? { stopReason: "cancelled", usage: sessionUsage(session) }
1249
+ : (inFlight?.deferredSettle ?? { stopReason, usage: sessionUsage(session) }));
870
1250
  // Queued turns the SDK never started never ran, so reject them rather
871
1251
  // than reporting a success (end_turn) — or a misleading "cancelled" —
872
1252
  // for a prompt that produced no output. (A cancel already settled the
@@ -893,19 +1273,116 @@ export class ClaudeAcpAgent {
893
1273
  message: message,
894
1274
  });
895
1275
  }
1276
+ // CLIs 2.1.206+ (capability msg_lifecycle_v1) report the fate of every
1277
+ // uuid-stamped queued command (queued/started/completed/cancelled/
1278
+ // discarded) as `command_lifecycle` frames — 2-3 per prompt, since
1279
+ // prompt() stamps a uuid on every message. The frame is @internal and
1280
+ // absent from the SDKMessage union, so handle it BEFORE the exhaustive
1281
+ // switch: it must not reach `unreachable`'s error log, and a `case`
1282
+ // for it wouldn't typecheck. It feeds only the orphan accounting (see
1283
+ // Session.orphanCommands); turn settlement stays driven by
1284
+ // echoes/results/idle. (Raw-mode emission above still forwards these
1285
+ // frames.)
1286
+ if (message.type === "command_lifecycle") {
1287
+ const frame = message;
1288
+ switch (frame.state) {
1289
+ case "started": {
1290
+ // Remember dispatch on the live turn so a cancel() that orphans
1291
+ // it seeds the right state (see Turn.commandStarted)...
1292
+ const queued = findUnsettledTurn(frame.command_uuid);
1293
+ if (queued) {
1294
+ queued.commandStarted = true;
1295
+ }
1296
+ // ...and promote an already-orphaned command: once dispatched,
1297
+ // a bare `cancelled` no longer means "dropped without running".
1298
+ const state = session.orphanCommands?.get(frame.command_uuid);
1299
+ if (state === "pending") {
1300
+ session.orphanCommands.set(frame.command_uuid, "started");
1301
+ }
1302
+ else if (state === "zombie") {
1303
+ // "started" after the command's terminal frame: the ordering
1304
+ // the whole lane rests on has been violated (frames are
1305
+ // per-uuid FIFO). Surface it — a silent drift here degrades
1306
+ // into swallowed or misattributed results.
1307
+ this.logger.error(`Session ${params.sessionId}: command_lifecycle "started" for ${frame.command_uuid} after its terminal frame; orphan accounting may be off for this cancel.`);
1308
+ }
1309
+ break;
1310
+ }
1311
+ case "completed":
1312
+ case "discarded":
1313
+ case "cancelled": {
1314
+ // Terminal frames. Latch the fate on a still-queued turn so a
1315
+ // later cancel() doesn't seed an orphan entry for a command
1316
+ // whose one-and-only terminal frame has already been consumed
1317
+ // (nothing would ever drain that entry).
1318
+ const queued = findUnsettledTurn(frame.command_uuid);
1319
+ if (queued) {
1320
+ queued.commandFinished = frame.state;
1321
+ }
1322
+ if (frame.state === "cancelled") {
1323
+ // Ambiguous by design (dup-over-loss): dropped before
1324
+ // dispatch (no result will ever come — safe to forget) vs
1325
+ // consumed into a turn that was aborted/failed. For the
1326
+ // latter, any result the dead turn managed to emit has
1327
+ // already deleted the entry (see
1328
+ // recordResultForOrphanCommands / ensureActiveTurn), so a
1329
+ // still-"started" entry means no result was seen since
1330
+ // dispatch — it becomes a zombie for the next
1331
+ // echo-less-result skip.
1332
+ const state = session.orphanCommands?.get(frame.command_uuid);
1333
+ if (state === "pending") {
1334
+ session.orphanCommands?.delete(frame.command_uuid);
1335
+ }
1336
+ else if (state === "started") {
1337
+ session.orphanCommands?.set(frame.command_uuid, "zombie");
1338
+ }
1339
+ break;
1340
+ }
1341
+ // Exactly-one-terminal: the command is finished. "completed" is
1342
+ // emitted after any result its turn produced (fresh turn) or the
1343
+ // command folded into another turn whose result is attributed
1344
+ // elsewhere — either way no echo-less result remains to skip.
1345
+ // "discarded" = session ended with it still queued; no result.
1346
+ session.orphanCommands?.delete(frame.command_uuid);
1347
+ break;
1348
+ }
1349
+ default:
1350
+ // "queued" carries no fate information. Anything else is a
1351
+ // state this adapter doesn't know — likely a CLI that grew the
1352
+ // v1 vocabulary. The entry still drains by result coverage or
1353
+ // activation's clear (bounded damage), but log it so the
1354
+ // degradation is visible instead of silent.
1355
+ if (frame.state !== "queued") {
1356
+ this.logger.error(`Session ${params.sessionId}: unknown command_lifecycle state "${frame.state}" for ${frame.command_uuid}; treating as uninformative.`);
1357
+ }
1358
+ break;
1359
+ }
1360
+ continue;
1361
+ }
896
1362
  switch (message.type) {
897
1363
  case "system":
898
1364
  switch (message.subtype) {
899
1365
  case "init":
1366
+ // Latch the lifecycle capability so cancel() routes orphan
1367
+ // accounting through `orphanCommands` (per-uuid, exact)
1368
+ // instead of the coalescing-blind count. Never unlatch: init
1369
+ // re-emits per turn and the capability can't be lost mid-CLI.
1370
+ if (message.capabilities?.includes("msg_lifecycle_v1")) {
1371
+ session.msgLifecycleV1 = true;
1372
+ }
900
1373
  // A fresh `system`/init (e.g. after reinitialize) can carry an
901
1374
  // updated Fast mode state; reconcile it with what we seeded at
902
1375
  // session creation.
903
1376
  await this.syncFastModeState(message.session_id, session, message.fast_mode_state);
904
1377
  break;
905
1378
  case "status": {
1379
+ // These banners count as delivered text (via sendUpdate), so
1380
+ // an echo-less turn that only ever emits them (e.g. `/compact`,
1381
+ // promoted at its own result) doesn't have its result text
1382
+ // re-emitted by the issue-#453 fallback.
906
1383
  if (message.status === "compacting") {
907
1384
  compactionInProgress = true;
908
- await this.client.sessionUpdate({
1385
+ await sendUpdate({
909
1386
  sessionId: message.session_id,
910
1387
  update: {
911
1388
  sessionUpdate: "agent_message_chunk",
@@ -918,7 +1395,7 @@ export class ClaudeAcpAgent {
918
1395
  // message carrying `compact_result`, not the `compact_boundary`
919
1396
  // message (which only fires when there's content to compact).
920
1397
  compactionInProgress = false;
921
- await this.client.sessionUpdate({
1398
+ await sendUpdate({
922
1399
  sessionId: message.session_id,
923
1400
  update: {
924
1401
  sessionUpdate: "agent_message_chunk",
@@ -929,7 +1406,7 @@ export class ClaudeAcpAgent {
929
1406
  else if (message.compact_result === "failed" && compactionInProgress) {
930
1407
  compactionInProgress = false;
931
1408
  const reason = message.compact_error ? `: ${message.compact_error}` : ".";
932
- await this.client.sessionUpdate({
1409
+ await sendUpdate({
933
1410
  sessionId: message.session_id,
934
1411
  update: {
935
1412
  sessionUpdate: "agent_message_chunk",
@@ -954,9 +1431,9 @@ export class ClaudeAcpAgent {
954
1431
  // dropped dramatically) and replaced within seconds by the next
955
1432
  // result message.
956
1433
  //
957
- // `size` keeps coming from session.contextWindowSize (learned
958
- // from modelUsage / the model heuristic) — getContextUsage's
959
- // window field under-reports extended 1M windows.
1434
+ // `size` keeps coming from session.contextWindowSize —
1435
+ // compaction frees occupancy, it doesn't change the model's
1436
+ // window.
960
1437
  //
961
1438
  // The "Compacting completed." text is emitted from the `status`
962
1439
  // handler (keyed on `compact_result`), not here, so the failure
@@ -964,7 +1441,7 @@ export class ClaudeAcpAgent {
964
1441
  const usedTokens = await fetchContextUsedTokens(session.query, this.logger);
965
1442
  lastAssistantUsage = null;
966
1443
  lastAssistantTotalUsage = usedTokens ?? 0;
967
- await this.client.sessionUpdate({
1444
+ await sendUpdate({
968
1445
  sessionId: message.session_id,
969
1446
  update: {
970
1447
  sessionUpdate: "usage_update",
@@ -975,7 +1452,7 @@ export class ClaudeAcpAgent {
975
1452
  break;
976
1453
  }
977
1454
  case "local_command_output": {
978
- await this.client.sessionUpdate({
1455
+ await sendUpdate({
979
1456
  sessionId: message.session_id,
980
1457
  update: {
981
1458
  sessionUpdate: "agent_message_chunk",
@@ -985,6 +1462,7 @@ export class ClaudeAcpAgent {
985
1462
  break;
986
1463
  }
987
1464
  case "session_state_changed": {
1465
+ session.lastSessionState = message.state;
988
1466
  if (message.state === "idle") {
989
1467
  // A non-cancelled turn normally settled at its terminal
990
1468
  // `result` already (issue #773), and that result recorded an
@@ -1017,14 +1495,38 @@ export class ClaudeAcpAgent {
1017
1495
  // when the cancel pre-empted the result (wedge/force-cancel).
1018
1496
  if (session.cancelled && session.activeTurn && !session.activeTurn.settled) {
1019
1497
  settleActive({ stopReason: "cancelled", usage: sessionUsage(session) });
1498
+ // An interrupt can pre-empt the turn's result entirely
1499
+ // (nothing ran the result-case `finally`), so close the
1500
+ // delivery stretch here: idle is the SDK's authoritative
1501
+ // turn-over signal, and stale partial-text state would
1502
+ // suppress the next turn's issue-#453 fallback.
1503
+ session.emittedAssistantText = false;
1504
+ }
1505
+ else if (isHeldOpen(session.activeTurn)) {
1506
+ // A turn held open for its background subagents (see
1507
+ // Turn.deferredSettle). Idles keep their normal cadence
1508
+ // during the hold — the CLI emits one per processing
1509
+ // cycle (the turn's own trailer, then one per followup),
1510
+ // NOT one final "all drained" signal — so each one
1511
+ // absorbs an outstanding trailer debt, and the turn only
1512
+ // settles once none of its spawned subagents is left
1513
+ // (the followup-result settle usually got there first;
1514
+ // this is the fallback when no followup came). Mid-hold
1515
+ // idles never fall through: a held turn HAS its result,
1516
+ // so reading its idle as "turn abandoned without a
1517
+ // result" (issue #825) would fail a healthy prompt.
1518
+ if (session.owedTrailingIdles > 0) {
1519
+ session.owedTrailingIdles--;
1520
+ }
1521
+ settleDeferredIfDrained();
1020
1522
  }
1021
- else if (owedTrailingIdles > 0) {
1523
+ else if (session.owedTrailingIdles > 0) {
1022
1524
  // Absorb a settled turn's trailing idle. Also covers a
1023
1525
  // cancel that landed between a turn's counted result and
1024
1526
  // this lagged idle (no active turn to settle): the idle
1025
1527
  // still belongs to that settled turn, and skipping the
1026
1528
  // decrement would leak the debt permanently.
1027
- owedTrailingIdles--;
1529
+ session.owedTrailingIdles--;
1028
1530
  }
1029
1531
  else if (!session.cancelled &&
1030
1532
  session.activeTurn &&
@@ -1066,7 +1568,7 @@ export class ClaudeAcpAgent {
1066
1568
  const title = isSynthesis
1067
1569
  ? "Recalled synthesized memory"
1068
1570
  : `Recalled ${count} ${count === 1 ? "memory" : "memories"}`;
1069
- await this.client.sessionUpdate({
1571
+ await sendUpdate({
1070
1572
  sessionId: message.session_id,
1071
1573
  update: {
1072
1574
  sessionUpdate: "tool_call",
@@ -1093,7 +1595,7 @@ export class ClaudeAcpAgent {
1093
1595
  // list with this payload: supportedCommands() is captured once
1094
1596
  // at initialize and never reflects mid-session changes, so we
1095
1597
  // forward message.commands directly rather than re-querying.
1096
- await this.client.sessionUpdate({
1598
+ await sendUpdate({
1097
1599
  sessionId: message.session_id,
1098
1600
  update: {
1099
1601
  sessionUpdate: "available_commands_update",
@@ -1117,7 +1619,7 @@ export class ClaudeAcpAgent {
1117
1619
  // rejection reason — otherwise the client shows a tool call
1118
1620
  // that silently never resolves.
1119
1621
  const reason = message.decision_reason ?? message.message;
1120
- await this.client.sessionUpdate({
1622
+ await sendUpdate({
1121
1623
  sessionId: message.session_id,
1122
1624
  update: {
1123
1625
  sessionUpdate: "tool_call_update",
@@ -1149,10 +1651,14 @@ export class ClaudeAcpAgent {
1149
1651
  // instead of a silent stop. ACP's agent_message_chunk has no
1150
1652
  // severity field, so fold the level into the text for the more
1151
1653
  // prominent levels ('info' is transcript-only noise — leave plain).
1654
+ // Sending via sendUpdate also marks the notice as this stretch's
1655
+ // delivered text: a hook-blocked turn's result repeats the block
1656
+ // reason with zero output tokens, and the issue-#453 fallback
1657
+ // must not emit it a second time.
1152
1658
  const text = message.level === "info"
1153
1659
  ? message.content
1154
1660
  : `**${message.level[0].toUpperCase()}${message.level.slice(1)}:** ${message.content}`;
1155
- await this.client.sessionUpdate({
1661
+ await sendUpdate({
1156
1662
  sessionId: message.session_id,
1157
1663
  update: {
1158
1664
  sessionUpdate: "agent_message_chunk",
@@ -1165,10 +1671,51 @@ export class ClaudeAcpAgent {
1165
1671
  case "hook_progress":
1166
1672
  case "hook_response":
1167
1673
  case "files_persisted":
1674
+ case "task_progress":
1675
+ break;
1168
1676
  case "task_started":
1677
+ // For subagent tasks `task_id` is the subagent's agent id (the
1678
+ // SDK keys its task registry by agent id) and `tool_use_id` is
1679
+ // the Agent/Task tool_use that spawned it — recorded so the
1680
+ // subagent's permission requests, which reach canUseTool with
1681
+ // only `agentID`, can attribute their eagerly-emitted
1682
+ // tool_call to the parent tool call. Non-subagent tasks (e.g.
1683
+ // background Bash) land here too; their task_ids never match
1684
+ // an agentID, so those entries are inert for attribution.
1685
+ //
1686
+ // `isSubagent` marks Task/Agent-tool subagents — the tasks
1687
+ // whose completion wakes the model for a followup, so the
1688
+ // ones worth deferring turn settlement for. A sync subagent
1689
+ // is pruned (terminal task_updated) before its turn's result
1690
+ // can arrive, so registry membership at result time means an
1691
+ // async subagent. Their spawn is also recorded on the active
1692
+ // turn: a turn only ever waits on its own subagents, and a
1693
+ // spawn during a held-open drain window (an agent chain)
1694
+ // extends that turn's hold.
1695
+ session.liveBackgroundTasks.set(message.task_id, {
1696
+ parentToolUseId: message.tool_use_id,
1697
+ isSubagent: !!message.subagent_type,
1698
+ });
1699
+ if (message.subagent_type && session.activeTurn && !session.activeTurn.settled) {
1700
+ (session.activeTurn.spawnedTaskIds ??= new Set()).add(message.task_id);
1701
+ }
1702
+ break;
1169
1703
  case "task_notification":
1170
- case "task_progress":
1704
+ // The task settled — no further tool calls can originate
1705
+ // from it, so its registry entry can be dropped.
1706
+ session.liveBackgroundTasks.delete(message.task_id);
1707
+ break;
1171
1708
  case "task_updated":
1709
+ // terminal-status task_updated patch and a (deduplicated)
1710
+ // task_notification when a task settles, but only the patch is
1711
+ // guaranteed per transition — prune on it too so the registry
1712
+ // can't grow for the session's lifetime if a notification is
1713
+ // skipped.
1714
+ if (message.patch.status === "completed" ||
1715
+ message.patch.status === "failed" ||
1716
+ message.patch.status === "killed") {
1717
+ session.liveBackgroundTasks.delete(message.task_id);
1718
+ }
1172
1719
  break;
1173
1720
  case "worker_shutting_down":
1174
1721
  // A Remote Control worker announced a graceful teardown. This is a
@@ -1220,7 +1767,7 @@ export class ClaudeAcpAgent {
1220
1767
  const outcome = persistent
1221
1768
  ? `The session will continue on ${message.fallback_model}.`
1222
1769
  : `The session stays on ${message.original_model}.`;
1223
- await this.client.sessionUpdate({
1770
+ await sendUpdate({
1224
1771
  sessionId: message.session_id,
1225
1772
  update: {
1226
1773
  sessionUpdate: "agent_message_chunk",
@@ -1256,12 +1803,50 @@ export class ClaudeAcpAgent {
1256
1803
  break;
1257
1804
  // `control_request_progress` only reports on side_question
1258
1805
  // control requests, which this adapter never issues.
1259
- // `background_tasks_changed` is a level signal (the full live
1260
- // background-task set on every membership change) for surfaces
1261
- // that render a background-activity indicator; turn lifecycle
1262
- // here is driven by results/idle, so there is nothing to track.
1263
1806
  case "control_request_progress":
1807
+ break;
1264
1808
  case "background_tasks_changed":
1809
+ // A level signal: the full live background-task set on every
1810
+ // membership change, with REPLACE semantics. Used only to
1811
+ // reconcile `liveBackgroundTasks` — dropping (or, for
1812
+ // subagent entries, unpinning) any entry whose settle
1813
+ // bookend (task_notification / terminal task_updated) was
1814
+ // lost, so a leaked subagent entry can't defer its spawning
1815
+ // turn's settlement forever. Growth of retained
1816
+ // (endedPerLevel) subagent entries is bounded by the
1817
+ // activation-time sweep in activateTurn, not here. It never
1818
+ // ADDS entries (the payload carries no attribution or
1819
+ // subagent marker), so the unspecified ordering vs. the edge
1820
+ // bookends is safe: a level that precedes its task_started
1821
+ // simply no-ops here.
1822
+ if (session.liveBackgroundTasks.size > 0) {
1823
+ const live = new Set(message.tasks.map((t) => t.task_id));
1824
+ for (const [taskId, record] of session.liveBackgroundTasks) {
1825
+ if (live.has(taskId)) {
1826
+ // The level proves the task live in the background
1827
+ // universe (e.g. a foreground agent was backgrounded
1828
+ // after an earlier absent-marking, or that marking was
1829
+ // a racing payload built before the task registered) —
1830
+ // un-end it so a hold waits on it again, and disarm
1831
+ // the activation sweep.
1832
+ record.endedPerLevel = undefined;
1833
+ continue;
1834
+ }
1835
+ if (record.isSubagent) {
1836
+ // The level's universe is BACKGROUND tasks only, so a
1837
+ // live sync (foreground) subagent is legitimately
1838
+ // absent — deleting its entry would strand its
1839
+ // permission attribution (#859). Keep the entry but
1840
+ // stop any hold from waiting on the id: an absent id
1841
+ // can equally be a leaked async entry whose settle
1842
+ // bookends were lost.
1843
+ record.endedPerLevel ??= "ended";
1844
+ }
1845
+ else {
1846
+ session.liveBackgroundTasks.delete(taskId);
1847
+ }
1848
+ }
1849
+ }
1265
1850
  break;
1266
1851
  default:
1267
1852
  unreachable(message, this.logger);
@@ -1269,177 +1854,281 @@ export class ClaudeAcpAgent {
1269
1854
  }
1270
1855
  break;
1271
1856
  case "result": {
1272
- // Task-notification followups are autonomous work triggered by a
1273
- // task-notification system message, not by the user's prompt.
1274
- // They should not influence the user-turn lifecycle (stop reason,
1275
- // slash-command output forwarding) but their cost is real.
1276
- const isTaskNotification = message.origin?.kind === "task-notification";
1277
- // Reconcile the Fast mode toggle with the SDK's reported state.
1278
- // Gated to user-driven turns like every other side effect below; a
1279
- // background followup's state lands on the next user turn's result.
1280
- // Runs even when the turn errors or was cancelled.
1281
- if (!isTaskNotification) {
1282
- await this.syncFastModeState(params.sessionId, session, message.fast_mode_state);
1283
- }
1284
- // A user-turn result needs an active turn so its stop reason is
1285
- // attributed and the turn settles at idle. Local-only commands carry
1286
- // no user-message echo to promote them, so do it here from the head.
1287
- // Promote BEFORE accumulating usage, since activation resets the
1288
- // accumulator — promoting after would discard this result's tokens.
1289
- if (!isTaskNotification) {
1290
- ensureActiveTurn();
1291
- }
1292
- // Every user-turn result terminates a turn (settle, reject, or
1293
- // orphan skip) and the SDK follows it with a trailing
1294
- // `session_state_changed: idle` — record the debt so the idle
1295
- // handler absorbs that idle rather than reading it as a turn the
1296
- // SDK abandoned (issue #825). One exclusion: the cancelled ACTIVE
1297
- // turn's own result. It is dropped at the `session.cancelled`
1298
- // guard, and either the idle itself settles the turn (consuming
1299
- // the trailer) or the next echo's hand-off does (which records
1300
- // the debt there instead) — counting here too would double it.
1301
- // Results skipped while cancelled with NO active turn — orphaned
1302
- // queued turns the SDK still ran, or a force-cancelled turn's
1303
- // late result after the backstop settled it — get no such settle,
1304
- // so their trailers must be counted here or they'd later be read
1305
- // as the next healthy turn being abandoned and false-fail it.
1306
- if (!isTaskNotification && (!session.cancelled || !session.activeTurn)) {
1307
- owedTrailingIdles++;
1308
- }
1309
- // Accumulate usage into the user turn's tally. Skip task-notification
1310
- // followups: their cost is real but is reported separately via the
1311
- // usage_update below, and `session.accumulatedUsage` is only reset on
1312
- // turn activation — so folding a task-notification result that lands
1313
- // after the next turn is active (but before it settles) would leak
1314
- // those tokens into that turn's PromptResponse.usage.
1315
- if (!isTaskNotification) {
1316
- session.accumulatedUsage.inputTokens += message.usage.input_tokens;
1317
- session.accumulatedUsage.outputTokens += message.usage.output_tokens;
1318
- session.accumulatedUsage.cachedReadTokens += message.usage.cache_read_input_tokens;
1319
- session.accumulatedUsage.cachedWriteTokens +=
1320
- message.usage.cache_creation_input_tokens;
1321
- }
1322
- const matchingModelUsage = lastAssistantModel
1323
- ? getMatchingModelUsage(message.modelUsage, lastAssistantModel)
1324
- : null;
1325
- // Only overwrite when we have an authoritative value — a miss
1326
- // (e.g. a turn with no top-level assistant message) would
1327
- // otherwise discard the window learned on a prior turn and
1328
- // leave the next prompt's mid-stream updates reporting 200k.
1329
- if (matchingModelUsage) {
1330
- session.contextWindowSize = matchingModelUsage.contextWindow;
1331
- }
1332
- // Send usage_update notification
1333
- if (lastAssistantTotalUsage !== null) {
1334
- await this.client.sessionUpdate({
1335
- sessionId: params.sessionId,
1336
- update: {
1337
- sessionUpdate: "usage_update",
1338
- used: lastAssistantTotalUsage,
1339
- size: session.contextWindowSize,
1340
- cost: {
1341
- amount: message.total_cost_usd,
1342
- currency: "USD",
1343
- },
1344
- ...(message.origin && {
1345
- _meta: { "_claude/origin": message.origin },
1346
- }),
1347
- },
1348
- });
1349
- }
1350
- if (session.cancelled) {
1351
- if (!isTaskNotification) {
1352
- stopReason = "cancelled";
1857
+ // A result from an autonomous cycle — a task-notification
1858
+ // followup, or a peer/coordinator/observer message the model
1859
+ // handled on its own (see AUTONOMOUS_RESULT_ORIGINS) — is not
1860
+ // the user's prompt's. Autonomous results must never touch the
1861
+ // user-turn lifecycle (stop reason, settles, failActive,
1862
+ // slash-command output forwarding), though their cost is real.
1863
+ const isAutonomousResult = message.origin != null && AUTONOMOUS_RESULT_ORIGINS.has(message.origin.kind);
1864
+ try {
1865
+ // Reconcile the Fast mode toggle with the SDK's reported state.
1866
+ // Gated to user-driven turns like every other side effect below;
1867
+ // an autonomous cycle's state lands on the next user turn's
1868
+ // result. Runs even when the turn errors or was cancelled.
1869
+ if (!isAutonomousResult) {
1870
+ await this.syncFastModeState(params.sessionId, session, message.fast_mode_state);
1353
1871
  }
1354
- break;
1355
- }
1356
- // A refusal can arrive on any result subtype (and may even set
1357
- // is_error), so handle it before the subtype switch — otherwise the
1358
- // is_error throw below would surface it as an internal error. The
1359
- // refused assistant message carries no visible content, so surface
1360
- // the classifier's explanation (when available) and report ACP's
1361
- // dedicated `refusal` stop reason.
1362
- if (message.stop_reason === "refusal" && !isTaskNotification) {
1363
- if (lastRefusalExplanation) {
1364
- await this.client.sessionUpdate({
1872
+ // A user-turn result needs an active turn so its stop reason is
1873
+ // attributed and the turn settles at idle. Local-only commands carry
1874
+ // no user-message echo to promote them, so do it here from the head.
1875
+ // Promote BEFORE accumulating usage, since activation resets the
1876
+ // accumulator — promoting after would discard this result's tokens.
1877
+ // The orphan bookkeeping runs first: it covers folded/zombie
1878
+ // commands whose shared or late result this is, even when the
1879
+ // result is the ACTIVE turn's (ensureActiveTurn never looks at
1880
+ // the map in that case).
1881
+ if (!isAutonomousResult) {
1882
+ recordResultForOrphanCommands();
1883
+ ensureActiveTurn();
1884
+ }
1885
+ // A result closes the stretch of output it terminates: snapshot
1886
+ // the delivery record — AFTER ensureActiveTurn, whose held-turn
1887
+ // hand-off closes the held stretch, so an echo-less command
1888
+ // promoted here is judged on its own delivery, not on the held
1889
+ // turn's followup text — and before the handling below can emit
1890
+ // anything of its own; the `finally` then clears it so every
1891
+ // exit from this case (the cancelled-guard and refusal breaks
1892
+ // included) starts the next stretch clean. Clearing up front
1893
+ // instead would let result-time emissions (refusal explanation,
1894
+ // result-text forwarding) taint the next stretch and suppress a
1895
+ // following replayed turn's fallback. Autonomous cycles run
1896
+ // alongside a user turn and must not clear its flag (they exit
1897
+ // through the early break below, which the gated `finally`
1898
+ // leaves alone).
1899
+ const deliveredAssistantText = session.emittedAssistantText;
1900
+ // Every user-turn result terminates a turn (settle, reject, or
1901
+ // orphan skip) and the SDK follows it with a trailing
1902
+ // `session_state_changed: idle` — record the debt so the idle
1903
+ // handler absorbs that idle rather than reading it as a turn the
1904
+ // SDK abandoned (issue #825). One exclusion: the cancelled ACTIVE
1905
+ // turn's own result. It is dropped at the `session.cancelled`
1906
+ // guard, and either the idle itself settles the turn (consuming
1907
+ // the trailer) or the next echo's hand-off does (which records
1908
+ // the debt there instead) — counting here too would double it.
1909
+ // Results skipped while cancelled with NO active turn — orphaned
1910
+ // queued turns the SDK still ran, or a force-cancelled turn's
1911
+ // late result after the backstop settled it — get no such settle,
1912
+ // so their trailers must be counted here or they'd later be read
1913
+ // as the next healthy turn being abandoned and false-fail it.
1914
+ // Autonomous results (followups, peer/channel/coordinator
1915
+ // cycles) are counted too: each is its own processing cycle with
1916
+ // its own trailing idle, and that idle can lag past the next
1917
+ // prompt's echo — which, un-owed, would be read as the fresh
1918
+ // turn being abandoned (#825 false-fail). That lag was mostly
1919
+ // unreachable when such cycles only ran with no pending turn,
1920
+ // but a held turn settling AT a followup result unblocks the
1921
+ // client at exactly that point, making the race the common
1922
+ // case.
1923
+ // The cancelled-ACTIVE-turn exclusion applies only to that
1924
+ // turn's OWN result — a followup result arriving inside the
1925
+ // cancel window still gets its own trailer and must be counted,
1926
+ // or that idle would later false-fail the next prompt.
1927
+ if (isAutonomousResult || !session.cancelled || !session.activeTurn) {
1928
+ session.owedTrailingIdles++;
1929
+ }
1930
+ // Accumulate usage into the user turn's tally. Skip autonomous
1931
+ // results: their cost is real but is reported separately via the
1932
+ // usage_update below, and `session.accumulatedUsage` is only reset on
1933
+ // turn activation — so folding an autonomous result that lands
1934
+ // after the next turn is active (but before it settles) would leak
1935
+ // those tokens into that turn's PromptResponse.usage.
1936
+ if (!isAutonomousResult) {
1937
+ session.accumulatedUsage.inputTokens += message.usage.input_tokens;
1938
+ session.accumulatedUsage.outputTokens += message.usage.output_tokens;
1939
+ session.accumulatedUsage.cachedReadTokens += message.usage.cache_read_input_tokens;
1940
+ session.accumulatedUsage.cachedWriteTokens +=
1941
+ message.usage.cache_creation_input_tokens;
1942
+ }
1943
+ const matchingModelUsage = lastAssistantModel
1944
+ ? getMatchingModelUsage(message.modelUsage, lastAssistantModel)
1945
+ : null;
1946
+ // Only overwrite when we have an authoritative value — a miss
1947
+ // (e.g. a turn with no top-level assistant message) would
1948
+ // otherwise discard the window learned on a prior turn and
1949
+ // leave the next prompt's mid-stream updates reporting 200k.
1950
+ if (matchingModelUsage) {
1951
+ session.contextWindowSize = matchingModelUsage.contextWindow;
1952
+ }
1953
+ // Send usage_update notification
1954
+ if (lastAssistantTotalUsage !== null) {
1955
+ await sendUpdate({
1365
1956
  sessionId: params.sessionId,
1366
1957
  update: {
1367
- sessionUpdate: "agent_message_chunk",
1368
- content: { type: "text", text: lastRefusalExplanation },
1958
+ sessionUpdate: "usage_update",
1959
+ used: lastAssistantTotalUsage,
1960
+ size: session.contextWindowSize,
1961
+ cost: {
1962
+ amount: message.total_cost_usd,
1963
+ currency: "USD",
1964
+ },
1965
+ ...(message.origin && {
1966
+ _meta: { "_claude/origin": message.origin },
1967
+ }),
1369
1968
  },
1370
1969
  });
1371
1970
  }
1372
- stopReason = "refusal";
1373
- settleActive({ stopReason: "refusal", usage: sessionUsage(session) });
1374
- break;
1375
- }
1376
- switch (message.subtype) {
1377
- case "success": {
1378
- if (message.result.includes("Please run /login")) {
1379
- failActive(RequestError.authRequired());
1380
- break;
1971
+ if (session.cancelled) {
1972
+ if (!isAutonomousResult) {
1973
+ stopReason = "cancelled";
1381
1974
  }
1382
- if (message.stop_reason === "max_tokens") {
1383
- if (!isTaskNotification) {
1384
- stopReason = "max_tokens";
1385
- }
1386
- break;
1387
- }
1388
- if (message.is_error) {
1389
- failActive(RequestError.internalError(errorKindData(lastAssistantError), message.result));
1390
- break;
1975
+ break;
1976
+ }
1977
+ // A held turn (see Turn.deferredSettle) settles at its
1978
+ // followup's terminal result: this is the earliest point at which
1979
+ // the promised summary has fully streamed — the trailing idle
1980
+ // would work too, but a client should not wait out another idle
1981
+ // round-trip for a response whose content is already complete.
1982
+ // (While the turn still awaits another of its subagents —
1983
+ // parallel spawns — the helper holds; the next notification's
1984
+ // followup settles it instead. Other autonomous origins — peer/
1985
+ // channel/coordinator cycles — reach here too: settling a
1986
+ // drained hold at their results is as good as the idle
1987
+ // fallback.) Then stop: everything below is user-turn
1988
+ // lifecycle, and an autonomous outcome must never touch it —
1989
+ // its is_error or "Please run /login" text would otherwise
1990
+ // failActive a live turn (the held one, or the user's next
1991
+ // prompt) whose own result recorded a different outcome.
1992
+ if (isAutonomousResult) {
1993
+ settleDeferredIfDrained();
1994
+ // With no turn in flight OR QUEUED (also after the settle
1995
+ // above), the stretch holds only autonomous prose — close
1996
+ // it, so a replayed next prompt isn't silently suppressed by
1997
+ // the issue-#453 delivery check. A live turn's flag may
1998
+ // guard the USER's already-streamed text — and so may a
1999
+ // QUEUED turn's: with mid-message echo lag its deltas stream
2000
+ // before the echo activates it (activeTurn still null), and
2001
+ // clearing then would re-emit that answer via the fallback,
2002
+ // the duplicate direction the flag's doc forbids.
2003
+ if (!session.activeTurn && !firstUnsettledQueuedTurn()) {
2004
+ session.emittedAssistantText = false;
1391
2005
  }
1392
- // For local-only commands (no model invocation), the result
1393
- // text is the command output — forward it to the client.
1394
- // Task-notification followups never originate from a user
1395
- // slash command, so skip the forwarding for them.
1396
- if (session.activeTurn?.isLocalOnlyCommand && !isTaskNotification) {
1397
- for (const notification of toAcpNotifications(message.result, "assistant", params.sessionId, session.toolUseCache, this.client, this.logger)) {
1398
- await this.client.sessionUpdate(notification);
1399
- }
2006
+ break;
2007
+ }
2008
+ // A refusal can arrive on any result subtype (and may even set
2009
+ // is_error), so handle it before the subtype switch — otherwise the
2010
+ // is_error throw below would surface it as an internal error. The
2011
+ // refused assistant message carries no visible content, so surface
2012
+ // the classifier's explanation (when available) and report ACP's
2013
+ // dedicated `refusal` stop reason.
2014
+ if (message.stop_reason === "refusal") {
2015
+ if (lastRefusalExplanation) {
2016
+ await sendUpdate({
2017
+ sessionId: params.sessionId,
2018
+ update: {
2019
+ sessionUpdate: "agent_message_chunk",
2020
+ content: { type: "text", text: lastRefusalExplanation },
2021
+ },
2022
+ });
1400
2023
  }
2024
+ stopReason = "refusal";
2025
+ // Through the deferral gate, not settleActive: a refusal can
2026
+ // land on a turn whose spawned subagents are still live, and
2027
+ // settling it out from under them would strand their output
2028
+ // and permission requests out-of-turn (issue #866's deadlock,
2029
+ // through the refusal lane).
2030
+ settleOrDefer({ stopReason: "refusal", usage: sessionUsage(session) });
1401
2031
  break;
1402
2032
  }
1403
- case "error_during_execution": {
1404
- if (message.stop_reason === "max_tokens") {
1405
- if (!isTaskNotification) {
2033
+ switch (message.subtype) {
2034
+ case "success": {
2035
+ if (message.result.includes("Please run /login")) {
2036
+ failActive(RequestError.authRequired());
2037
+ break;
2038
+ }
2039
+ if (message.stop_reason === "max_tokens") {
1406
2040
  stopReason = "max_tokens";
2041
+ break;
2042
+ }
2043
+ if (message.is_error) {
2044
+ failActive(RequestError.internalError(errorKindData(lastAssistantError), message.result));
2045
+ break;
2046
+ }
2047
+ // The result text is forwarded in two cases. Local-only
2048
+ // commands (no model invocation): the result IS the command
2049
+ // output. Otherwise the result is normally a trailing copy of
2050
+ // text that already streamed — but a cache-replayed turn
2051
+ // generates no tokens, and some CLIs then skip streaming
2052
+ // entirely and answer on the `result` alone: no `stream_event`
2053
+ // deltas, no consolidated `assistant` message (issue #453).
2054
+ // Forward it rather than end the turn silently:
2055
+ // `deliveredAssistantText` covers whatever already reached the
2056
+ // client (a turn that showed its answer cannot emit it twice),
2057
+ // and the output-token check keeps the fallback to the
2058
+ // replayed turns it was reported for. `?? 0`: typed non-null,
2059
+ // but third-party backends have been observed omitting usage
2060
+ // token fields (see snapshotFromUsage), and the replay lane
2061
+ // was reported from exactly such a backend — treat a missing
2062
+ // count as the replay signature rather than silently disabling
2063
+ // the fallback there. (Autonomous results never get here —
2064
+ // they exit at the early break above — so no background
2065
+ // prose can be injected into the feed.)
2066
+ if (session.activeTurn?.isLocalOnlyCommand ||
2067
+ (!deliveredAssistantText && (message.usage.output_tokens ?? 0) === 0)) {
2068
+ for (const notification of toAcpNotifications(message.result, "assistant", params.sessionId, session.toolUseCache, this.client, this.logger)) {
2069
+ await sendUpdate(notification);
2070
+ }
1407
2071
  }
1408
2072
  break;
1409
2073
  }
1410
- if (message.is_error) {
1411
- failActive(RequestError.internalError(errorKindData(lastAssistantError), message.errors.join(", ") || message.subtype));
1412
- break;
1413
- }
1414
- if (!isTaskNotification) {
2074
+ case "error_during_execution": {
2075
+ if (message.stop_reason === "max_tokens") {
2076
+ stopReason = "max_tokens";
2077
+ break;
2078
+ }
2079
+ if (message.is_error) {
2080
+ failActive(RequestError.internalError(errorKindData(lastAssistantError), message.errors.join(", ") || message.subtype));
2081
+ break;
2082
+ }
1415
2083
  stopReason = "end_turn";
1416
- }
1417
- break;
1418
- }
1419
- case "error_max_budget_usd":
1420
- case "error_max_turns":
1421
- case "error_max_structured_output_retries":
1422
- if (message.is_error) {
1423
- failActive(RequestError.internalError(errorKindData(lastAssistantError), message.errors.join(", ") || message.subtype));
1424
2084
  break;
1425
2085
  }
1426
- if (!isTaskNotification) {
2086
+ case "error_max_budget_usd":
2087
+ case "error_max_turns":
2088
+ case "error_max_structured_output_retries":
2089
+ if (message.is_error) {
2090
+ failActive(RequestError.internalError(errorKindData(lastAssistantError), message.errors.join(", ") || message.subtype));
2091
+ break;
2092
+ }
1427
2093
  stopReason = "max_turn_requests";
1428
- }
1429
- break;
1430
- default:
1431
- unreachable(message, this.logger);
1432
- break;
2094
+ break;
2095
+ default:
2096
+ unreachable(message, this.logger);
2097
+ break;
2098
+ }
2099
+ // Settle the user turn at its terminal result so the client unlocks
2100
+ // as soon as the answer is done, rather than waiting for the SDK's
2101
+ // trailing `idle` (which can lag while background work runs — issue
2102
+ // #773). The consumer keeps draining afterward (absorbing idle and
2103
+ // forwarding any background output).
2104
+ //
2105
+ // One exception: while background subagents this turn spawned are
2106
+ // still live, settling now would strand their remaining work
2107
+ // outside any turn — ACP allows out-of-turn session/update, but
2108
+ // many clients stop consuming at the prompt response, and a
2109
+ // subagent's permission request would block on an RPC nobody
2110
+ // answers (issues #864/#866). Hold the turn open instead: store
2111
+ // the outcome and settle with it once the subagents are done —
2112
+ // at their followup's terminal result (see the deferred-settle
2113
+ // block above the subtype switch) or at an idle with none of
2114
+ // them left — so the subagents' streamed output, their
2115
+ // permission requests, and the model's promised summary all land
2116
+ // inside the turn. `session/cancel` and the next prompt's echo
2117
+ // hand-off still settle a deferred turn early, so a long-running
2118
+ // subagent never holds the prompt hostage.
2119
+ //
2120
+ // is_error/auth already settled via failActive (activeTurn is null
2121
+ // then, so both branches no-op); cancellation is left to the
2122
+ // idle/abort path. settleActive is idempotent, so a duplicate
2123
+ // idle is a no-op.
2124
+ if (!session.cancelled) {
2125
+ settleOrDefer({ stopReason, usage: sessionUsage(session) });
2126
+ }
1433
2127
  }
1434
- // Settle the user turn at its terminal result so the client unlocks
1435
- // as soon as the answer is done, rather than waiting for the SDK's
1436
- // trailing `idle` (which can lag while background work runs — issue
1437
- // #773). The consumer keeps draining afterward (absorbing idle and
1438
- // forwarding any background output). is_error/auth already settled
1439
- // via failActive; cancellation is left to the idle/abort path.
1440
- // settleActive is idempotent, so a duplicate idle is a no-op.
1441
- if (!isTaskNotification && !session.cancelled) {
1442
- settleActive({ stopReason, usage: sessionUsage(session) });
2128
+ finally {
2129
+ if (!isAutonomousResult) {
2130
+ session.emittedAssistantText = false;
2131
+ }
1443
2132
  }
1444
2133
  break;
1445
2134
  }
@@ -1502,11 +2191,10 @@ export class ClaudeAcpAgent {
1502
2191
  const model = message.event.message.model;
1503
2192
  if (model && model !== "<synthetic>") {
1504
2193
  lastAssistantModel = model;
1505
- // Only upgrade from the default — once a `result` has given
1506
- // us an authoritative window, trust it over the heuristic.
1507
- // Model switches invalidate the cached window via
1508
- // `syncSessionConfigState`, which resets us back to the
1509
- // default so this branch runs again for the new model.
2194
+ // Only upgrade from the default — once the SDK has given us
2195
+ // an authoritative window (seeded at session creation,
2196
+ // refreshed on model switches in `applyConfigOptionValue`,
2197
+ // confirmed by each `result`), trust it over the heuristic.
1510
2198
  if (session.contextWindowSize === DEFAULT_CONTEXT_WINDOW) {
1511
2199
  const inferred = inferContextWindowFromModel(model);
1512
2200
  if (inferred !== null) {
@@ -1532,7 +2220,7 @@ export class ClaudeAcpAgent {
1532
2220
  const nextUsage = totalTokens(lastAssistantUsage);
1533
2221
  if (nextUsage !== lastAssistantTotalUsage) {
1534
2222
  lastAssistantTotalUsage = nextUsage;
1535
- await this.client.sessionUpdate({
2223
+ await sendUpdate({
1536
2224
  sessionId: params.sessionId,
1537
2225
  update: {
1538
2226
  sessionUpdate: "usage_update",
@@ -1548,8 +2236,11 @@ export class ClaudeAcpAgent {
1548
2236
  taskState: session.taskState,
1549
2237
  emittedToolCalls: session.emittedToolCalls,
1550
2238
  messageId: currentStreamMessageId,
2239
+ streamedToolInputs,
1551
2240
  })) {
1552
- await this.client.sessionUpdate(notification);
2241
+ // sendUpdate records delivery; a subagent stream's chunks carry
2242
+ // the stamped parentToolUseId meta and are excluded there.
2243
+ await sendUpdate(notification);
1553
2244
  }
1554
2245
  break;
1555
2246
  }
@@ -1570,7 +2261,7 @@ export class ClaudeAcpAgent {
1570
2261
  // is still promoted — activateTurn() clears the flag. The turn's own
1571
2262
  // echo is then dropped from the feed (the client already shows it).
1572
2263
  if (message.type === "user" && "uuid" in message && message.uuid) {
1573
- const queued = (session.turnQueue ?? []).find((t) => t.promptUuid === message.uuid && !t.settled);
2264
+ const queued = findUnsettledTurn(message.uuid);
1574
2265
  if (queued) {
1575
2266
  // Only (re)activate if this isn't already the active turn — a
1576
2267
  // turn promoted early (e.g. by a result that preceded its echo)
@@ -1588,15 +2279,38 @@ export class ClaudeAcpAgent {
1588
2279
  // debt so that lagged idle is absorbed rather than read
1589
2280
  // as the freshly-activated turn ending without a result
1590
2281
  // (which would false-fail a healthy turn — issue #825).
1591
- owedTrailingIdles++;
2282
+ // Counted for a DEFERRED turn too, even though its own
2283
+ // result already recorded a debt that may still be
2284
+ // outstanding: the interrupt can produce a trailer of
2285
+ // its own, and over-counting is benign (absorbs one
2286
+ // future idle) while under-counting risks the false
2287
+ // fail this debt exists to prevent.
2288
+ session.owedTrailingIdles++;
1592
2289
  // Before activateTurn resets the accumulator, so the
1593
2290
  // usage still belongs to the cancelled turn.
1594
2291
  settleActive({ stopReason: "cancelled", usage: sessionUsage(session) });
1595
2292
  }
2293
+ else if (isHeldOpen(session.activeTurn)) {
2294
+ // A turn held open for its background subagents (see
2295
+ // Turn.deferredSettle) hands off with the real outcome
2296
+ // its result recorded, not a guessed end_turn — the
2297
+ // user moving on must not block behind a long-running
2298
+ // subagent, but it must not rewrite the stop reason
2299
+ // either. Its trailing-idle debt stands and is absorbed
2300
+ // when the drain idle eventually arrives.
2301
+ settleActive(session.activeTurn.deferredSettle);
2302
+ }
1596
2303
  else {
1597
2304
  settleActive({ stopReason: "end_turn", usage: sessionUsage(session) });
1598
2305
  }
1599
2306
  }
2307
+ // Unlike the no-result teardown lanes, this hand-off must
2308
+ // NOT clear emittedAssistantText for a NON-held previous
2309
+ // turn (a held one's settleActive above closes its own
2310
+ // stretch): the echo can land
2311
+ // mid-message, so deltas already streamed belong to the turn
2312
+ // being activated — clearing would forget them and let its
2313
+ // result re-emit the answer.
1600
2314
  activateTurn(queued);
1601
2315
  }
1602
2316
  break;
@@ -1649,7 +2363,7 @@ export class ClaudeAcpAgent {
1649
2363
  taskState: session.taskState,
1650
2364
  messageId: messageIdForGrouping(message),
1651
2365
  })) {
1652
- await this.client.sessionUpdate(notification);
2366
+ await sendUpdate(notification);
1653
2367
  }
1654
2368
  }
1655
2369
  else {
@@ -1673,12 +2387,7 @@ export class ClaudeAcpAgent {
1673
2387
  if (message.message.role === "system") {
1674
2388
  break;
1675
2389
  }
1676
- if (message.type === "assistant" &&
1677
- message.message.model === "<synthetic>" &&
1678
- Array.isArray(message.message.content) &&
1679
- message.message.content.length === 1 &&
1680
- message.message.content[0].type === "text" &&
1681
- message.message.content[0].text.includes("Please run /login")) {
2390
+ if (message.type === "assistant" && isSyntheticLoginMessage(message.message)) {
1682
2391
  failActive(RequestError.authRequired());
1683
2392
  break;
1684
2393
  }
@@ -1761,13 +2470,18 @@ export class ClaudeAcpAgent {
1761
2470
  taskState: session.taskState,
1762
2471
  emittedToolCalls: session.emittedToolCalls,
1763
2472
  messageId: messageIdForGrouping(message),
2473
+ toolUseResult: message.type === "user" ? message.tool_use_result : undefined,
1764
2474
  })) {
1765
- await this.client.sessionUpdate(notification);
2475
+ // sendUpdate records delivery. Subagent text/thinking is
2476
+ // filtered out of `content` above; blocks that do pass through
2477
+ // (e.g. a subagent image) carry the stamped parentToolUseId
2478
+ // meta and are excluded there.
2479
+ await sendUpdate(notification);
1766
2480
  }
1767
2481
  break;
1768
2482
  }
1769
2483
  case "tool_progress": {
1770
- await this.client.sessionUpdate({
2484
+ await sendUpdate({
1771
2485
  sessionId: message.session_id,
1772
2486
  update: {
1773
2487
  sessionUpdate: "tool_call_update",
@@ -1785,7 +2499,7 @@ export class ClaudeAcpAgent {
1785
2499
  }
1786
2500
  case "rate_limit_event": {
1787
2501
  if (lastAssistantTotalUsage !== null) {
1788
- await this.client.sessionUpdate({
2502
+ await sendUpdate({
1789
2503
  sessionId: message.session_id,
1790
2504
  update: {
1791
2505
  sessionUpdate: "usage_update",
@@ -1806,7 +2520,7 @@ export class ClaudeAcpAgent {
1806
2520
  case "conversation_reset":
1807
2521
  break;
1808
2522
  default:
1809
- unreachable(message);
2523
+ unreachable(message, this.logger);
1810
2524
  break;
1811
2525
  }
1812
2526
  }
@@ -1843,6 +2557,30 @@ export class ClaudeAcpAgent {
1843
2557
  }
1844
2558
  }
1845
2559
  }
2560
+ /** Route one orphaned command into the session's orphan-accounting lane:
2561
+ * the per-uuid map on msg_lifecycle_v1 CLIs (drained by the command's own
2562
+ * terminal lifecycle frame and the echo-less-result skip), the plain count
2563
+ * elsewhere (the count lane can't express per-command states, so `state`
2564
+ * only matters on the map lane). Both orphan-producing paths — cancel()'s
2565
+ * queued-turn sweep and the consumer's force-cancel wedge path — must seed
2566
+ * through here so the lane split stays a single mechanism.
2567
+ *
2568
+ * Known window: `msgLifecycleV1` is only learnable from the stream's first
2569
+ * `system`/init (the control-channel initialize carries no capabilities),
2570
+ * so a cancel that beats that drain seeds the COUNT lane on a
2571
+ * lifecycle-capable CLI — where command coalescing can leave the count
2572
+ * stale by N-1 (the pre-map bug, confined to this sub-second window and
2573
+ * still healed by the next activation's reset). Structural until the SDK
2574
+ * exposes capabilities before the stream starts. */
2575
+ trackOrphanCommand(session, uuid, state) {
2576
+ if (session.msgLifecycleV1) {
2577
+ session.orphanCommands ??= new Map();
2578
+ session.orphanCommands.set(uuid, state);
2579
+ }
2580
+ else {
2581
+ session.pendingOrphanResults = (session.pendingOrphanResults ?? 0) + 1;
2582
+ }
2583
+ }
1846
2584
  async cancel(params) {
1847
2585
  const session = this.sessions[params.sessionId];
1848
2586
  if (!session) {
@@ -1856,11 +2594,18 @@ export class ClaudeAcpAgent {
1856
2594
  return;
1857
2595
  }
1858
2596
  session.cancelled = true;
2597
+ // Capture the orphan-accounting lane before anything can await: the
2598
+ // consumer latches msgLifecycleV1 when it drains the first system/init,
2599
+ // which can happen DURING the awaited interrupt() below — the receipt
2600
+ // reconciliation must act on the same lane the seeding used, or a
2601
+ // count-lane orphan would be left for the map-lane receipt path (which
2602
+ // never decrements the count) to miss.
2603
+ const lifecycleLane = session.msgLifecycleV1 === true;
1859
2604
  // Settle queued turns that haven't started yet (no echo seen) right away —
1860
2605
  // they have no in-flight SDK work to interrupt. The active turn is settled
1861
2606
  // by the consumer when it observes the interrupt's trailing idle (or via the
1862
2607
  // backstop below). Mirrors the old pendingMessages cancellation.
1863
- const orphanedUuids = [];
2608
+ const orphanedTurns = [];
1864
2609
  if (session.turnQueue) {
1865
2610
  for (const turn of session.turnQueue) {
1866
2611
  if (turn !== session.activeTurn && !turn.settled) {
@@ -1868,16 +2613,98 @@ export class ClaudeAcpAgent {
1868
2613
  // Deliberately no `usage`: a queued turn never ran, so the session
1869
2614
  // accumulator (the active turn's tally) is not its spend.
1870
2615
  turn.resolve({ stopReason: "cancelled" });
1871
- orphanedUuids.push(turn.promptUuid);
2616
+ orphanedTurns.push(turn);
1872
2617
  }
1873
2618
  }
1874
2619
  // Each removed queued turn's user message was already pushed to the SDK,
1875
2620
  // which processes input FIFO and will still emit a result for it with no
1876
- // uuid to match. Count those so the consumer skips them (see
2621
+ // uuid to match. Track those so the consumer skips them (see
1877
2622
  // ensureActiveTurn) rather than misattributing them to the head.
1878
- session.pendingOrphanResults = (session.pendingOrphanResults ?? 0) + orphanedUuids.length;
2623
+ // msg_lifecycle_v1 CLIs get per-uuid tracking drained by the command's
2624
+ // own terminal lifecycle frame — exact under command coalescing, where
2625
+ // N queued commands fold into ONE turn emitting one result and a plain
2626
+ // count would go stale by N-1 and swallow a later echo-less result.
2627
+ // Older CLIs keep the count and its activation-time self-heal (they
2628
+ // never see lifecycle frames, so commandStarted/commandFinished stay
2629
+ // unset and every turn takes the plain-seed path below).
2630
+ for (const turn of orphanedTurns) {
2631
+ if (turn.commandFinished === "completed" || turn.commandFinished === "discarded") {
2632
+ // The command already finished SDK-side and its terminal frame was
2633
+ // consumed while the turn sat queued — nothing is left to skip, and
2634
+ // a seeded entry would never drain.
2635
+ continue;
2636
+ }
2637
+ if (turn.commandFinished === "cancelled") {
2638
+ // Terminal frame already consumed. Dispatched-then-aborted: the
2639
+ // dead turn's late result may still come — seed the zombie the
2640
+ // frame handler would have made — unless that result already
2641
+ // passed pre-cancel (commandResultSeen: e.g. the command folded
2642
+ // into the active turn and their shared result was attributed
2643
+ // there), in which case a zombie would be a phantom that swallows
2644
+ // an unrelated later result. Never dispatched: dropped, no result
2645
+ // coming, nothing to track.
2646
+ if (turn.commandStarted && !turn.commandResultSeen) {
2647
+ this.trackOrphanCommand(session, turn.promptUuid, "zombie");
2648
+ }
2649
+ continue;
2650
+ }
2651
+ if (turn.commandStarted && turn.commandResultSeen) {
2652
+ // Dispatched and its turn's result already passed; only its
2653
+ // terminal frame is outstanding, which no-ops with no entry.
2654
+ continue;
2655
+ }
2656
+ this.trackOrphanCommand(session, turn.promptUuid, turn.commandStarted ? "started" : "pending");
2657
+ }
1879
2658
  session.turnQueue = session.turnQueue.filter((turn) => turn === session.activeTurn && !turn.settled);
1880
2659
  }
2660
+ // A deferred active turn (see Turn.deferredSettle) already has its
2661
+ // result — it is only held open for its background subagents, which the
2662
+ // interrupt below tears down. Settle it "cancelled" NOW: during the hold
2663
+ // the session is typically already in state idle (the CLI's trailer
2664
+ // fired at the result), so the interrupt may produce no fresh idle for
2665
+ // the consumer's cancelled-settle path to run on, and the cancel would
2666
+ // otherwise stall until the force-cancel backstop. Any outstanding
2667
+ // trailer debt is absorbed by the idle handler when its idle does come.
2668
+ // The turn's own usage snapshot is reported per the cancelled-usage
2669
+ // contract (issue #844).
2670
+ {
2671
+ const active = session.activeTurn;
2672
+ if (isHeldOpen(active)) {
2673
+ active.settled = true;
2674
+ // Mirror settleActive's invariants (it is consumer-scoped and
2675
+ // unreachable from here): disarm the backstop — none should be
2676
+ // armed for a held turn, but a drift here must not leave a timer
2677
+ // firing on a settled turn — and drop the turn from the queue.
2678
+ disarmForceCancel(session);
2679
+ session.turnQueue = (session.turnQueue ?? []).filter((t) => t !== active);
2680
+ session.activeTurn = null;
2681
+ // Settling a held turn closes its delivery stretch: any streamed
2682
+ // text since the last boundary was its followups', and left latched
2683
+ // it would suppress a following replayed turn's issue-#453 fallback.
2684
+ session.emittedAssistantText = false;
2685
+ // When the interrupt below pre-empts a live cycle — running, or
2686
+ // blocked on a permission request (requires_action, the #866 shape
2687
+ // users cancel out of) — it produces a trailer idle with no counted
2688
+ // result; with the hold's own trailer typically already absorbed,
2689
+ // that idle would be un-owed and could lag past the next prompt's
2690
+ // echo — read as the fresh turn ending without a result (issue #825
2691
+ // false-fail). Pre-count it unless the session sits idle: there the
2692
+ // interrupt emits nothing, and a debt that never drains would mask
2693
+ // one future #825 detection. (lastSessionState is last-CONSUMED, so
2694
+ // both stale reads exist and both are accepted one-cycle windows: a
2695
+ // running transition still in the backlog reads as stale idle and
2696
+ // under-counts — that false-fail additionally needs the trailer to
2697
+ // lag past the next echo — while a cycle already completed into the
2698
+ // backlog reads as stale non-idle and over-counts, masking one
2699
+ // future #825 detection. Undefined — no state event consumed —
2700
+ // pre-counts; that only occurs on CLIs whose missing idle events
2701
+ // also disable the detector the debt could mask.)
2702
+ if (session.lastSessionState !== "idle") {
2703
+ session.owedTrailingIdles++;
2704
+ }
2705
+ active.resolve({ stopReason: "cancelled", usage: active.deferredSettle.usage });
2706
+ }
2707
+ }
1881
2708
  // Arm a backstop before interrupting: if a turn is actively consuming the
1882
2709
  // query and interrupt() doesn't make the SDK yield (e.g. a wedged TaskOutput
1883
2710
  // block — issue #680), force the consumer to settle the active turn
@@ -1915,11 +2742,30 @@ export class ClaudeAcpAgent {
1915
2742
  // receipt, so a bare `{}` success from a gateway can't read as "everything
1916
2743
  // was dropped") — keep the count-everything behavior and its
1917
2744
  // activation-time self-heal.
1918
- if (Array.isArray(receipt?.still_queued) && orphanedUuids.length > 0) {
2745
+ if (Array.isArray(receipt?.still_queued) && orphanedTurns.length > 0) {
1919
2746
  const stillQueued = new Set(receipt.still_queued);
1920
- const dropped = orphanedUuids.filter((uuid) => !stillQueued.has(uuid)).length;
1921
- if (dropped > 0) {
1922
- session.pendingOrphanResults = Math.max(0, (session.pendingOrphanResults ?? 0) - dropped);
2747
+ if (lifecycleLane) {
2748
+ // Lifecycle lane: forget dropped orphans by uuid. Only entries still
2749
+ // "pending" — an orphan absent from `still_queued` because it was
2750
+ // DISPATCHED before the interrupt (not dropped) has usually been
2751
+ // promoted to "started" by its lifecycle frame by now, and its own
2752
+ // terminal frame must stay in charge of its fate. (If that frame is
2753
+ // still in the consumer's backlog we mis-forget — the same exposure
2754
+ // the count lane has always had for a dropped-then-run command.)
2755
+ // Mostly redundant with the "cancelled"-frame removal, but a receipt
2756
+ // survives paths where that frame was never emitted.
2757
+ for (const turn of orphanedTurns) {
2758
+ if (!stillQueued.has(turn.promptUuid) &&
2759
+ session.orphanCommands?.get(turn.promptUuid) === "pending") {
2760
+ session.orphanCommands.delete(turn.promptUuid);
2761
+ }
2762
+ }
2763
+ }
2764
+ else {
2765
+ const dropped = orphanedTurns.filter((turn) => !stillQueued.has(turn.promptUuid)).length;
2766
+ if (dropped > 0) {
2767
+ session.pendingOrphanResults = Math.max(0, (session.pendingOrphanResults ?? 0) - dropped);
2768
+ }
1923
2769
  }
1924
2770
  }
1925
2771
  }
@@ -1967,10 +2813,7 @@ export class ClaudeAcpAgent {
1967
2813
  // after the floor, and clear the timer so it can't outlive the deleted
1968
2814
  // session (it isn't unref'd and would otherwise keep the event loop alive
1969
2815
  // until it fires).
1970
- if (session.forceCancelTimer) {
1971
- clearTimeout(session.forceCancelTimer);
1972
- session.forceCancelTimer = undefined;
1973
- }
2816
+ disarmForceCancel(session);
1974
2817
  session.cancelController?.abort();
1975
2818
  this.closeQueryStream(session);
1976
2819
  // Abort the SDK abort signal only on explicit destroy. closeQueryStream
@@ -2150,6 +2993,12 @@ export class ClaudeAcpAgent {
2150
2993
  if (replaySession && replayMessageId && message.uuid) {
2151
2994
  replaySession.messageIdToUuid.set(replayMessageId, message.uuid);
2152
2995
  }
2996
+ // The live prompt loop converts the synthetic "Please run /login"
2997
+ // assistant message into an authRequired error instead of showing its
2998
+ // TUI-specific text; skip it on replay too (issue #863).
2999
+ if (message.type === "assistant" && isSyntheticLoginMessage(message.message)) {
3000
+ continue;
3001
+ }
2153
3002
  // @ts-expect-error - untyped in SDK but we handle all of these
2154
3003
  let content = message.message.content;
2155
3004
  // @ts-expect-error - untyped in SDK but we handle all of these
@@ -2188,14 +3037,14 @@ export class ClaudeAcpAgent {
2188
3037
  * outcome or a `requestCancelled` rejection). Either way we surface the same
2189
3038
  * "Tool use aborted" the callers already expect, so a cancelled dialog no
2190
3039
  * longer leaves the `await` hanging. */
2191
- async requestPermissionFromClient(params, toolName, signal) {
3040
+ async requestPermissionFromClient(params, toolName, signal, parentToolUseId) {
2192
3041
  // The SDK may invoke `canUseTool` (and therefore this permission request)
2193
3042
  // before the assistant message's tool_use block streams to us. Some ACP clients
2194
3043
  // expect the `tool_call` a permission request references to already exist,
2195
3044
  // so emit it now if it hasn't been sent yet. The streamed tool_use chunk
2196
3045
  // later refines it with a `tool_call_update` rather than emitting a
2197
3046
  // duplicate (see `emittedToolCalls` in `toAcpNotifications`).
2198
- await this.ensureToolCallEmitted(params.sessionId, toolName, params.toolCall.toolCallId, params.toolCall.rawInput);
3047
+ await this.ensureToolCallEmitted(params.sessionId, toolName, params.toolCall.toolCallId, params.toolCall.rawInput, parentToolUseId);
2199
3048
  try {
2200
3049
  return await this.client.requestPermission(params, signal);
2201
3050
  }
@@ -2212,10 +3061,15 @@ export class ClaudeAcpAgent {
2212
3061
  * instead of emitting a duplicate (see `emittedToolCalls`). Built via the same
2213
3062
  * `toolCallNotification` helper as the streamed path so the two are identical.
2214
3063
  * Tools the stream renders as a plan (TodoWrite) or suppresses (Task*) are
2215
- * skipped so a permission prompt for them never surfaces a stray tool_call. */
2216
- async ensureToolCallEmitted(sessionId, toolName, toolCallId, toolInput) {
3064
+ * emitted too: a permission request referencing a tool call the client has
3065
+ * never seen can trip strict clients (issue #851), so the reference must
3066
+ * always resolve. Since the streamed path never completes those calls, they
3067
+ * are resolved at tool_result time instead (see `toAcpNotifications`).
3068
+ * `parentToolUseId` attributes a subagent's tool call to the Agent/Task call
3069
+ * that spawned it, matching the streamed path's `_meta`. */
3070
+ async ensureToolCallEmitted(sessionId, toolName, toolCallId, toolInput, parentToolUseId) {
2217
3071
  const session = this.sessions[sessionId];
2218
- if (!session || !shouldEmitToolCall(toolName)) {
3072
+ if (!session) {
2219
3073
  return;
2220
3074
  }
2221
3075
  if (session.emittedToolCalls.has(toolCallId)) {
@@ -2223,13 +3077,20 @@ export class ClaudeAcpAgent {
2223
3077
  }
2224
3078
  session.emittedToolCalls.add(toolCallId);
2225
3079
  const supportsTerminalOutput = this.clientCapabilities?._meta?.["terminal_output"] === true;
2226
- await this.client.sessionUpdate({
2227
- sessionId,
2228
- update: toolCallNotification({ id: toolCallId, name: toolName, input: toolInput }, toolInput, supportsTerminalOutput, session.cwd),
2229
- });
3080
+ const update = toolCallNotification({ id: toolCallId, name: toolName, input: toolInput }, toolInput, supportsTerminalOutput, session.cwd);
3081
+ if (parentToolUseId) {
3082
+ update._meta = {
3083
+ ...update._meta,
3084
+ claudeCode: {
3085
+ ...(update._meta?.claudeCode || {}),
3086
+ parentToolUseId,
3087
+ },
3088
+ };
3089
+ }
3090
+ await this.client.sessionUpdate({ sessionId, update });
2230
3091
  }
2231
3092
  canUseTool(sessionId) {
2232
- return async (toolName, toolInput, { signal, suggestions, toolUseID }) => {
3093
+ return async (toolName, toolInput, { signal, suggestions, toolUseID, agentID }) => {
2233
3094
  const alwaysAllowLabel = describeAlwaysAllow(suggestions, toolName);
2234
3095
  const supportsTerminalOutput = this.clientCapabilities?._meta?.["terminal_output"] === true;
2235
3096
  const session = this.sessions[sessionId];
@@ -2239,6 +3100,23 @@ export class ClaudeAcpAgent {
2239
3100
  message: "Session not found",
2240
3101
  };
2241
3102
  }
3103
+ // When the tool call originates inside a subagent, attribute the eagerly
3104
+ // emitted tool_call (and the permission request itself) to the Agent/Task
3105
+ // tool call that spawned the subagent, mirroring the streamed subagent
3106
+ // path's `_meta.claudeCode.parentToolUseId` (see `liveBackgroundTasks`).
3107
+ const parentToolUseId = agentID
3108
+ ? session.liveBackgroundTasks.get(agentID)?.parentToolUseId
3109
+ : undefined;
3110
+ if (agentID && !parentToolUseId) {
3111
+ // The attribution rests on an undocumented SDK invariant
3112
+ // (task_started.task_id === canUseTool's agentID for subagent tasks;
3113
+ // verified against the bundled CLI). Should an SDK bump break it — or
3114
+ // the consumer lose the race with task_started — the lookup misses and
3115
+ // the request goes out unattributed; log it so the regression is
3116
+ // observable rather than silent.
3117
+ this.logger.log(`[claude-agent-acp] No parent tool_use recorded for subagent ${agentID}; ` +
3118
+ `sending the ${toolName} permission request unattributed`);
3119
+ }
2242
3120
  // AskUserQuestion is surfaced to us as a normal permission check (the SDK
2243
3121
  // routes it through canUseTool whenever a callback is registered, rather
2244
3122
  // than the interactive dialog). Present it as an ACP form elicitation and
@@ -2246,7 +3124,7 @@ export class ClaudeAcpAgent {
2246
3124
  if (toolName === "AskUserQuestion" && this.clientCapabilities?.elicitation?.form) {
2247
3125
  // Like permission requests, the elicitation references this toolUseID, so
2248
3126
  // make sure the tool_call has surfaced to the client before we send it.
2249
- await this.ensureToolCallEmitted(sessionId, toolName, toolUseID, toolInput);
3127
+ await this.ensureToolCallEmitted(sessionId, toolName, toolUseID, toolInput, parentToolUseId);
2250
3128
  return this.handleAskUserQuestion(sessionId, toolInput, toolUseID, signal);
2251
3129
  }
2252
3130
  if (toolName === "ExitPlanMode") {
@@ -2280,8 +3158,13 @@ export class ClaudeAcpAgent {
2280
3158
  toolCallId: toolUseID,
2281
3159
  rawInput: toolInput,
2282
3160
  ...toolInfoFromToolUse({ name: toolName, input: toolInput, id: toolUseID }, supportsTerminalOutput, session?.cwd),
3161
+ // `claudeCode` metas always carry `toolName` (see ToolUpdateMeta),
3162
+ // so clients can rely on one shape everywhere.
3163
+ ...(parentToolUseId
3164
+ ? { _meta: { claudeCode: { toolName, parentToolUseId } } }
3165
+ : {}),
2283
3166
  },
2284
- }, toolName, signal);
3167
+ }, toolName, signal, parentToolUseId);
2285
3168
  if (signal.aborted || response.outcome?.outcome === "cancelled") {
2286
3169
  throw new Error("Tool use aborted");
2287
3170
  }
@@ -2339,8 +3222,13 @@ export class ClaudeAcpAgent {
2339
3222
  toolCallId: toolUseID,
2340
3223
  rawInput: toolInput,
2341
3224
  ...toolInfoFromToolUse({ name: toolName, input: toolInput, id: toolUseID }, supportsTerminalOutput, session?.cwd),
3225
+ // `claudeCode` metas always carry `toolName` (see ToolUpdateMeta),
3226
+ // so clients can rely on one shape everywhere.
3227
+ ...(parentToolUseId
3228
+ ? { _meta: { claudeCode: { toolName, parentToolUseId } } }
3229
+ : {}),
2342
3230
  },
2343
- }, toolName, signal);
3231
+ }, toolName, signal, parentToolUseId);
2344
3232
  if (signal.aborted || response.outcome?.outcome === "cancelled") {
2345
3233
  throw new Error("Tool use aborted");
2346
3234
  }
@@ -2516,12 +3404,17 @@ export class ClaudeAcpAgent {
2516
3404
  // carries no "1m" token.
2517
3405
  const newModelInfo = session.modelInfos.find((m) => m.value === value);
2518
3406
  if (session.models.currentModelId !== value) {
2519
- // The cached context window was learned for the previous model; reset
2520
- // to the new model's heuristic so mid-stream updates between now and
2521
- // the next `result` reflect the user's selection instead of the old
2522
- // model's window.
3407
+ // The cached context window was learned for the previous model. The
3408
+ // SDK is already running the new model here (user-driven switches call
3409
+ // `query.setModel` before this, and the refusal-fallback sync only
3410
+ // reconciles a switch the SDK already made), so ask it for the new
3411
+ // window; fall back to the text heuristic so mid-stream updates
3412
+ // between now and the next `result` reflect the user's selection
3413
+ // instead of the old model's window.
2523
3414
  session.contextWindowSize =
2524
- inferContextWindowFromModel(value, newModelInfo?.displayName, newModelInfo?.description) ?? DEFAULT_CONTEXT_WINDOW;
3415
+ (await fetchContextWindowSize(session.query, this.logger)) ??
3416
+ inferContextWindowFromModel(value, newModelInfo?.resolvedModel, newModelInfo?.displayName, newModelInfo?.description) ??
3417
+ DEFAULT_CONTEXT_WINDOW;
2525
3418
  }
2526
3419
  session.models = { ...session.models, currentModelId: value };
2527
3420
  // Recompute availableModes for the new model and clamp the current
@@ -3122,6 +4015,22 @@ export class ClaudeAcpAgent {
3122
4015
  effortLevel: initialEffort.currentValue,
3123
4016
  });
3124
4017
  }
4018
+ // Seed the context window from the SDK's authoritative report. Text
4019
+ // inference alone misses aliases that resolve to extended-context models
4020
+ // with no "1m" token anywhere in their id or description (e.g. `sonnet` →
4021
+ // claude-sonnet-5, natively ~1M): those streamed `usage_update.size:
4022
+ // 200000` until the first result's modelUsage corrected it — again on
4023
+ // every process restart or session re-creation, since the learned window
4024
+ // lives only on the Session (issue #596).
4025
+ //
4026
+ // The inference fallback is deliberately keyed to the allowlisted entry: a
4027
+ // fallback-resolved sibling's resolvedModel/displayName/description can
4028
+ // describe a different context lane than the verbatim live id (e.g. an
4029
+ // "opus[1m]" row matched for a bare 200k id), so on the fallback path only
4030
+ // the id itself is a trustworthy window signal.
4031
+ const contextWindowSize = (await fetchContextWindowSize(q, this.logger)) ??
4032
+ inferContextWindowFromModel(models.currentModelId, allowlistedModelInfo?.resolvedModel, allowlistedModelInfo?.displayName, allowlistedModelInfo?.description) ??
4033
+ DEFAULT_CONTEXT_WINDOW;
3125
4034
  this.sessions[sessionId] = {
3126
4035
  query: q,
3127
4036
  input: input,
@@ -3144,16 +4053,13 @@ export class ClaudeAcpAgent {
3144
4053
  fastModeEnabled,
3145
4054
  abortController,
3146
4055
  emitRawSDKMessages: sessionMeta?.claudeCode?.emitRawSDKMessages ?? false,
3147
- contextWindowSize:
3148
- // Deliberately keyed to the allowlisted entry: a fallback-resolved
3149
- // sibling's displayName/description can describe a different context
3150
- // lane than the verbatim live id (e.g. an "opus[1m]" row matched for
3151
- // a bare 200k id), so on the fallback path only the id itself is a
3152
- // trustworthy window signal.
3153
- inferContextWindowFromModel(models.currentModelId, allowlistedModelInfo?.displayName, allowlistedModelInfo?.description) ?? DEFAULT_CONTEXT_WINDOW,
4056
+ contextWindowSize,
3154
4057
  taskState,
3155
4058
  toolUseCache: {},
3156
4059
  emittedToolCalls: new Set(),
4060
+ liveBackgroundTasks: new Map(),
4061
+ emittedAssistantText: false,
4062
+ owedTrailingIdles: 0,
3157
4063
  messageIdToUuid: new Map(),
3158
4064
  };
3159
4065
  return {
@@ -4005,10 +4911,12 @@ function isTaskTool(toolName) {
4005
4911
  toolName === "TaskList" ||
4006
4912
  toolName === "TaskGet");
4007
4913
  }
4008
- /** Whether a tool's tool_use surfaces to the client as a standalone
4914
+ /** Whether the streamed tool_use path surfaces this tool as a standalone
4009
4915
  * `tool_call`. TodoWrite is rendered as a `plan` and Task* tools are
4010
4916
  * suppressed (their plan snapshot is emitted at tool_result time), so neither
4011
- * produces a tool_call. */
4917
+ * produces a streamed tool_call/tool_call_update — which means a
4918
+ * permission-surfaced tool_call for them (see `ensureToolCallEmitted`) must be
4919
+ * resolved explicitly at tool_result time. */
4012
4920
  function shouldEmitToolCall(toolName) {
4013
4921
  return toolName !== "TodoWrite" && !isTaskTool(toolName);
4014
4922
  }
@@ -4043,6 +4951,28 @@ function toolCallNotification(toolUse, rawInput, supportsTerminalOutput, cwd, re
4043
4951
  ...toolInfoFromToolUse(toolUse, supportsTerminalOutput, cwd),
4044
4952
  };
4045
4953
  }
4954
+ /** Refine a pending tool call from the complete top-level fields recovered
4955
+ * from its still-streaming input. Shares `toolInfoFromToolUse` with the
4956
+ * consolidated path but never carries `content`: content built from partial
4957
+ * input is misleading (an Edit missing its `new_string` renders as a pure
4958
+ * deletion) or invalid (a Write diff without `content` lacks the required
4959
+ * `newText`), and the consolidated message supplies it moments later. */
4960
+ function streamedInputRefinement(toolUse, input, supportsTerminalOutput, cwd) {
4961
+ // TodoWrite/Task* never surfaced a tool_call to refine (plan lane).
4962
+ if (!shouldEmitToolCall(toolUse.name)) {
4963
+ return undefined;
4964
+ }
4965
+ const { title, kind, locations } = toolInfoFromToolUse({ ...toolUse, input }, supportsTerminalOutput, cwd);
4966
+ return {
4967
+ _meta: { claudeCode: { toolName: toolUse.name } },
4968
+ toolCallId: toolUse.id,
4969
+ sessionUpdate: "tool_call_update",
4970
+ rawInput: input,
4971
+ title,
4972
+ kind,
4973
+ ...(locations ? { locations } : {}),
4974
+ };
4975
+ }
4046
4976
  /**
4047
4977
  * Convert an SDKAssistantMessage (Claude) to a SessionNotification (ACP).
4048
4978
  * Only handles text, image, and thinking chunks for now.
@@ -4074,6 +5004,15 @@ export function toAcpNotifications(content, role, sessionId, toolUseCache, clien
4074
5004
  }
4075
5005
  return [{ sessionId, update }];
4076
5006
  }
5007
+ // `tool_use_result` is message-level and carries no tool_use_id of its own:
5008
+ // it describes "the" tool_result block of the message it rode in on. If
5009
+ // several tool_result blocks were ever batched into one message it couldn't
5010
+ // be attributed, so it is only honored when the message carries exactly one.
5011
+ const toolUseResult = options?.toolUseResult !== undefined &&
5012
+ content.filter((c) => typeof c === "object" && c !== null && c.type === "tool_result")
5013
+ .length === 1
5014
+ ? options.toolUseResult
5015
+ : undefined;
4077
5016
  const output = [];
4078
5017
  // Only handle the first chunk for streaming; extend as needed for batching
4079
5018
  for (const chunk of content) {
@@ -4215,12 +5154,59 @@ export function toAcpNotifications(content, role, sessionId, toolUseCache, clien
4215
5154
  case "bash_code_execution_tool_result":
4216
5155
  case "text_editor_code_execution_tool_result":
4217
5156
  case "mcp_tool_result": {
5157
+ const wasEmitted = options?.emittedToolCalls?.has(chunk.tool_use_id) === true;
4218
5158
  options?.emittedToolCalls?.delete(chunk.tool_use_id);
4219
5159
  const toolUse = toolUseCache[chunk.tool_use_id];
4220
5160
  if (!toolUse) {
5161
+ // The permission flow may have surfaced this tool_call even though
5162
+ // its tool_use never reached the cache (e.g. the assistant message
5163
+ // carrying it was dropped by the cancelled-turn guard and a straggler
5164
+ // result landed later). Resolve the surfaced call anyway so it can't
5165
+ // stay pending in the client forever; without the cache entry the
5166
+ // tool name is unknown, so no claudeCode meta is attached.
5167
+ if (wasEmitted) {
5168
+ output.push({
5169
+ sessionId,
5170
+ update: {
5171
+ toolCallId: chunk.tool_use_id,
5172
+ sessionUpdate: "tool_call_update",
5173
+ status: "is_error" in chunk && chunk.is_error
5174
+ ? "failed"
5175
+ : "completed",
5176
+ rawOutput: chunk.content,
5177
+ },
5178
+ });
5179
+ }
4221
5180
  logger.error(`[claude-agent-acp] Got a tool result for tool use that wasn't tracked: ${chunk.tool_use_id}`);
4222
5181
  break;
4223
5182
  }
5183
+ // A permission request may have surfaced a plan-rendered (TodoWrite) or
5184
+ // suppressed (Task*) tool as a real tool_call so the request referenced
5185
+ // a tool call the client knows about (see `ensureToolCallEmitted`,
5186
+ // issue #851). The branches below never emit a tool_call_update for
5187
+ // those tools, which would leave the surfaced call pending in the
5188
+ // client forever — resolve it here. `wasEmitted` is only ever true for
5189
+ // these tools via the permission flow: the streamed plan/suppressed
5190
+ // branches don't record emissions.
5191
+ if (wasEmitted && !shouldEmitToolCall(toolUse.name)) {
5192
+ output.push({
5193
+ sessionId,
5194
+ update: {
5195
+ _meta: {
5196
+ claudeCode: {
5197
+ toolName: toolUse.name,
5198
+ ...(options?.parentToolUseId ? { parentToolUseId: options.parentToolUseId } : {}),
5199
+ },
5200
+ },
5201
+ toolCallId: chunk.tool_use_id,
5202
+ sessionUpdate: "tool_call_update",
5203
+ status: "is_error" in chunk && chunk.is_error
5204
+ ? "failed"
5205
+ : "completed",
5206
+ rawOutput: chunk.content,
5207
+ },
5208
+ });
5209
+ }
4224
5210
  if (isTaskTool(toolUse.name)) {
4225
5211
  // Headless/SDK sessions emit Task* tools instead of TodoWrite.
4226
5212
  // TaskCreate / TaskUpdate mutate the accumulated task list; TaskList
@@ -4244,7 +5230,7 @@ export function toAcpNotifications(content, role, sessionId, toolUseCache, clien
4244
5230
  }
4245
5231
  }
4246
5232
  else if (toolUse.name !== "TodoWrite") {
4247
- const { _meta: toolMeta, ...toolUpdate } = toolUpdateFromToolResult(chunk, toolUseCache[chunk.tool_use_id], supportsTerminalOutput);
5233
+ const { _meta: toolMeta, ...toolUpdate } = toolUpdateFromToolResult(chunk, toolUseCache[chunk.tool_use_id], supportsTerminalOutput, toolUseResult);
4248
5234
  // When terminal output is supported, send terminal_output as a
4249
5235
  // separate notification to match codex-acp's streaming lifecycle:
4250
5236
  // 1. tool_call → _meta.terminal_info (already sent above)
@@ -4321,35 +5307,105 @@ export function toAcpNotifications(content, role, sessionId, toolUseCache, clien
4321
5307
  }
4322
5308
  export function streamEventToAcpNotifications(message, sessionId, toolUseCache, client, logger, options) {
4323
5309
  const event = message.event;
5310
+ const streamKey = message.parent_tool_use_id ?? "";
5311
+ const streamedToolInputs = options?.streamedToolInputs;
5312
+ const forwardedOptions = {
5313
+ clientCapabilities: options?.clientCapabilities,
5314
+ parentToolUseId: message.parent_tool_use_id,
5315
+ cwd: options?.cwd,
5316
+ taskState: options?.taskState,
5317
+ emittedToolCalls: options?.emittedToolCalls,
5318
+ messageId: options?.messageId,
5319
+ };
4324
5320
  switch (event.type) {
4325
- case "content_block_start":
4326
- return toAcpNotifications([event.content_block], "assistant", sessionId, toolUseCache, client, logger, {
4327
- clientCapabilities: options?.clientCapabilities,
4328
- parentToolUseId: message.parent_tool_use_id,
4329
- cwd: options?.cwd,
4330
- taskState: options?.taskState,
4331
- emittedToolCalls: options?.emittedToolCalls,
4332
- messageId: options?.messageId,
4333
- });
4334
- case "content_block_delta":
4335
- return toAcpNotifications([event.delta], "assistant", sessionId, toolUseCache, client, logger, {
4336
- clientCapabilities: options?.clientCapabilities,
4337
- parentToolUseId: message.parent_tool_use_id,
4338
- cwd: options?.cwd,
4339
- taskState: options?.taskState,
4340
- emittedToolCalls: options?.emittedToolCalls,
4341
- messageId: options?.messageId,
4342
- });
5321
+ case "content_block_start": {
5322
+ const block = event.content_block;
5323
+ if (streamedToolInputs &&
5324
+ (block.type === "tool_use" ||
5325
+ block.type === "server_tool_use" ||
5326
+ block.type === "mcp_tool_use")) {
5327
+ let inputsForMessage = streamedToolInputs.get(streamKey);
5328
+ if (!inputsForMessage) {
5329
+ inputsForMessage = new Map();
5330
+ streamedToolInputs.set(streamKey, inputsForMessage);
5331
+ }
5332
+ inputsForMessage.set(event.index, {
5333
+ id: block.id,
5334
+ name: block.name,
5335
+ partialJson: "",
5336
+ scannedTo: 0,
5337
+ inString: false,
5338
+ escaped: false,
5339
+ objectDepth: 0,
5340
+ arrayDepth: 0,
5341
+ lastTopLevelComma: -1,
5342
+ emittedThroughComma: -1,
5343
+ });
5344
+ }
5345
+ return toAcpNotifications([block], "assistant", sessionId, toolUseCache, client, logger, forwardedOptions);
5346
+ }
5347
+ case "content_block_delta": {
5348
+ if (event.delta.type === "input_json_delta") {
5349
+ const streamedInput = streamedToolInputs?.get(streamKey)?.get(event.index);
5350
+ if (!streamedInput)
5351
+ return [];
5352
+ streamedInput.partialJson += event.delta.partial_json;
5353
+ if (scanStreamedToolInput(streamedInput)) {
5354
+ // Input complete: the consolidated assistant message replays the
5355
+ // block with its full input and refines the call there; emitting
5356
+ // here too would send a duplicate identical update.
5357
+ const inputsForMessage = streamedToolInputs?.get(streamKey);
5358
+ inputsForMessage?.delete(event.index);
5359
+ if (inputsForMessage?.size === 0)
5360
+ streamedToolInputs?.delete(streamKey);
5361
+ return [];
5362
+ }
5363
+ if (streamedInput.lastTopLevelComma <= streamedInput.emittedThroughComma) {
5364
+ return [];
5365
+ }
5366
+ streamedInput.emittedThroughComma = streamedInput.lastTopLevelComma;
5367
+ const input = recoveredToolInput(streamedInput.partialJson.slice(0, streamedInput.lastTopLevelComma));
5368
+ if (!input)
5369
+ return [];
5370
+ const supportsTerminalOutput = options?.clientCapabilities?._meta?.["terminal_output"] === true;
5371
+ const update = streamedInputRefinement(streamedInput, input, supportsTerminalOutput, options?.cwd);
5372
+ if (!update)
5373
+ return [];
5374
+ if (message.parent_tool_use_id) {
5375
+ update._meta = {
5376
+ ...update._meta,
5377
+ claudeCode: {
5378
+ ...(update._meta?.claudeCode || {}),
5379
+ parentToolUseId: message.parent_tool_use_id,
5380
+ },
5381
+ };
5382
+ }
5383
+ applyMessageId(update, options?.messageId);
5384
+ return [{ sessionId, update }];
5385
+ }
5386
+ return toAcpNotifications([event.delta], "assistant", sessionId, toolUseCache, client, logger, forwardedOptions);
5387
+ }
4343
5388
  // No content. `ping` is a Messages-API keep-alive event that the SDK's
4344
5389
  // `BetaRawMessageStreamEvent` union doesn't include even though the
4345
5390
  // wire format emits it; the `as never` cast lets us no-op it here
4346
5391
  // instead of letting it fall through to `unreachable`.
4347
5392
  case "ping":
4348
- case "message_start":
4349
5393
  case "message_delta":
5394
+ return [];
5395
+ // A message boundary ends every input stream on this lane: message_stop is
5396
+ // the normal end, and a message_start clears anything a prior message on
5397
+ // the lane left behind (e.g. a stream cut short mid-block).
5398
+ case "message_start":
4350
5399
  case "message_stop":
4351
- case "content_block_stop":
5400
+ streamedToolInputs?.delete(streamKey);
4352
5401
  return [];
5402
+ case "content_block_stop": {
5403
+ const inputsForMessage = streamedToolInputs?.get(streamKey);
5404
+ inputsForMessage?.delete(event.index);
5405
+ if (inputsForMessage?.size === 0)
5406
+ streamedToolInputs?.delete(streamKey);
5407
+ return [];
5408
+ }
4353
5409
  default:
4354
5410
  unreachable(event, logger);
4355
5411
  return [];
@@ -4428,12 +5484,14 @@ function commonPrefixLength(a, b) {
4428
5484
  * Anthropic 1M-context variants encode "1m" as a distinct token in the SDK
4429
5485
  * model ID (e.g., "claude-opus-4-6-1m"), which `\b1m\b` catches without also
4430
5486
  * matching things like "10m" or embedded substrings. Semantic aliases like
4431
- * `default` carry no such token in the ID, but the SDK's human-facing
4432
- * `displayName`/`description` do (e.g. "Opus 4.7 (1M context)"), so callers
4433
- * pass those too — the same `\b1m\b` token appears in "1M context". The SDK's
4434
- * `ModelInfo` exposes no structured context-window field, so this text scan is
4435
- * the only pre-`result` signal available. A miss falls back to the default
4436
- * window and is corrected by `result.modelUsage` within one turn. */
5487
+ * `default` carry no such token in the ID, but their `resolvedModel` and the
5488
+ * SDK's human-facing `displayName`/`description` can (e.g.
5489
+ * "claude-opus-4-8[1m]", "Opus 4.7 (1M context)"), so callers pass those too.
5490
+ * This text scan can't catch every model — some resolve to extended-context
5491
+ * models with no "1m" anywhere (e.g. `sonnet` → claude-sonnet-5, natively
5492
+ * ~1M) — which is why `fetchContextWindowSize` is preferred wherever a live
5493
+ * query is available. A miss falls back to the default window and is
5494
+ * corrected by `result.modelUsage` within one turn. */
4437
5495
  function inferContextWindowFromModel(...texts) {
4438
5496
  if (texts.some((text) => text != null && /\b1m\b/i.test(text)))
4439
5497
  return 1_000_000;
@@ -4443,12 +5501,7 @@ function inferContextWindowFromModel(...texts) {
4443
5501
  * `getContextUsage` control request. Unlike the per-message API usage numbers
4444
5502
  * (which only count message tokens), this `totalTokens` includes the system
4445
5503
  * prompt, tool schemas, MCP tools, and memory-file overhead — the real
4446
- * occupancy the user sees. Returns `null` on any control-request failure.
4447
- *
4448
- * Note: we deliberately do NOT use this response's window fields for `size`.
4449
- * They have been observed to under-report extended (1M) context windows, so
4450
- * the window keeps coming from `modelUsage` / `inferContextWindowFromModel`,
4451
- * which handle the 1M variants correctly. */
5504
+ * occupancy the user sees. Returns `null` on any control-request failure. */
4452
5505
  async function fetchContextUsedTokens(query, logger) {
4453
5506
  try {
4454
5507
  const usage = await query.getContextUsage();
@@ -4459,6 +5512,28 @@ async function fetchContextUsedTokens(query, logger) {
4459
5512
  return null;
4460
5513
  }
4461
5514
  }
5515
+ /** Fetch the current model's full context window (`rawMaxTokens`) via the
5516
+ * `getContextUsage` control request — the same source `/context` prints.
5517
+ * This is the only pre-`result` signal that covers semantic aliases whose
5518
+ * text carries no "1m" token (e.g. `sonnet` → claude-sonnet-5, natively
5519
+ * ~1M), so it's the primary window source at session creation and on model
5520
+ * switches; `inferContextWindowFromModel` remains the fallback. A returned
5521
+ * window is still superseded by each `result.modelUsage.contextWindow`.
5522
+ *
5523
+ * (Older CLIs under-reported extended 1M windows here — commit 20ef663
5524
+ * dropped the field for that reason — but the CLI vendored by the pinned
5525
+ * SDK reports them correctly again.) Returns `null` on any control-request
5526
+ * failure or a nonsensical (non-positive) window. */
5527
+ async function fetchContextWindowSize(query, logger) {
5528
+ try {
5529
+ const usage = await query.getContextUsage();
5530
+ return usage.rawMaxTokens > 0 ? usage.rawMaxTokens : null;
5531
+ }
5532
+ catch (error) {
5533
+ logger.error("Failed to fetch context window size from SDK:", error);
5534
+ return null;
5535
+ }
5536
+ }
4462
5537
  /** Translate the legacy `MAX_THINKING_TOKENS` env var into the SDK's `thinking`
4463
5538
  * option. The `maxThinkingTokens` option it used to feed is deprecated and
4464
5539
  * reduced to on/off on current models, so map the value to explicit thinking