dsh-ntr 0.3.2 → 0.3.3

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/src/bridge.ts CHANGED
@@ -20,6 +20,7 @@ import {
20
20
  PROTOCOL_VERSION,
21
21
  RequestError,
22
22
  type ClientConnection,
23
+ type ContentBlock,
23
24
  type InitializeResponse,
24
25
  type McpServer,
25
26
  type McpServerHttp,
@@ -52,6 +53,7 @@ import {
52
53
  resolveFsPath,
53
54
  resolveSessionCwd,
54
55
  selectForwardPrompt,
56
+ isAbsolutePath,
55
57
  acpPresetStatusFromRoster,
56
58
  splitAdapterModelId,
57
59
  updateContentToText,
@@ -74,6 +76,7 @@ import type {
74
76
  AcpIdleRelease, ApprovalDeadlineKey, ApprovalDeadlinePatch, PermissionOutcome, PermissionOptionView, PermissionTimeoutMode, PluginUpdateState, PreparedAdapterCall, SessionView, StreamChunk,
75
77
  } from './protocol.ts'
76
78
  import { DEFAULT_IDLE_TIMEOUT_MS, DEFAULT_TAG_HALO, IDLE_RELEASE_MS } from './protocol.ts'
79
+ import { OUTPUT_CHARS_PER_TOKEN, TurnTpsTracker } from './tps.ts'
77
80
  import { UpdateService } from './update.ts'
78
81
 
79
82
  /**
@@ -134,7 +137,30 @@ interface LiveSession {
134
137
  availableCommands?: SessionView['availableCommands']
135
138
  plan?: unknown[]
136
139
  usage?: SessionView['usage']
140
+ /**
141
+ * The plugin's own measurable burst aggregate over this ACP session's turns
142
+ * (see `src/tps.ts`). Persisted with the chat-session record so the dock's
143
+ * rate survives a host restart; absent until a turn contributes a measurable
144
+ * burst (>= 1 inter-chunk gap).
145
+ */
146
+ burst?: SessionView['burst']
137
147
  running: boolean
148
+ /**
149
+ * The HOST's prompt slot for this session: true from the moment a turn
150
+ * starts until the agent answers it (or the host aborts the turn, or the
151
+ * turn is discarded). Together with the {@link settled} gate — which STAYS
152
+ * claimed after an abort until the agent acknowledges the cancel — this is
153
+ * what `claimTurn` arbitrates, NOT `running`.
154
+ *
155
+ * The three are deliberately different:
156
+ * - an ambient wait keeps `running` true (the background drain is still the
157
+ * session's work, so nothing may release the process) while this slot is
158
+ * already FREE (the agent answered; only the drain keeps talking);
159
+ * - an aborted turn drops THIS flag at once (our face of the turn ends) but
160
+ * leaves `settled` in place, so the next prompt still waits out
161
+ * `turnSettleMs` for the agent instead of racing a concurrent prompt.
162
+ */
163
+ promptInFlight?: boolean
138
164
  closed: boolean
139
165
  /** The agent process died; the session awaits a `session/load` resume. */
140
166
  released: boolean
@@ -244,9 +270,73 @@ interface LiveSession {
244
270
  * the controller's own `retire` when it still points at it.
245
271
  */
246
272
  turnController?: TurnStreamController
273
+ /**
274
+ * A background turn the agent started on its OWN initiative (CodeBuddy's
275
+ * multitask drains: a sub-agent completion notice starts a run the client
276
+ * never asked for). Its first `session_info_update` announces the request id
277
+ * every later update of that run carries in `_meta`; a `session/prompt`
278
+ * answered instantly with no conversation request id was folded into this
279
+ * drain and its real answer arrives as the drain's updates.
280
+ *
281
+ * Only lifetime events clear it (close / release / resume / stop): a drain
282
+ * may outlive the turn that was steered into it, and clearing on a normal
283
+ * settle would misclassify the NEXT steered prompt.
284
+ */
285
+ ambient?: AmbientTurn
286
+ /**
287
+ * Request ids whose `agent/notice` already went out, so a repeated
288
+ * announcement of the same background run is not logged twice.
289
+ */
290
+ ambientNotices?: Set<string>
291
+ /**
292
+ * Background drains this session already STOPPED waiting on (an ambient wait
293
+ * that closed on silence, or a record that outlived `ambientStaleMs`). The
294
+ * record is gone from {@link ambient}, but the drain itself may still be
295
+ * running: a queued message or a long tool makes it speak again while the
296
+ * NEXT turn is on air. Without this list the late text would teach that turn
297
+ * the drain's id (`noteTurnRequestId` / `adoptTurnRequestId`) and fold the
298
+ * background run's answer into the user's answer (a double answer).
299
+ *
300
+ * An update or re-announcement carrying a listed id REVIVES it
301
+ * (`reviveAmbientId` + a rebuilt {@link ambient}): the run is alive, so its
302
+ * next steered prompt must receive the drain's text. Only while the record
303
+ * stays retired is its text transcript-only. Timestamps let the zero-output
304
+ * report recognise a prompt folded into a recently quieted drain
305
+ * (`hasRecentRetiredAmbient`). Bounded ({@link RETIRED_AMBIENT_MAX}), oldest
306
+ * evicted first; every lifecycle boundary clears the list
307
+ * (`clearAmbientState`).
308
+ */
309
+ retiredAmbients?: RetiredAmbient[]
247
310
  transcript: Transcript
248
311
  }
249
312
 
313
+ /** One unsolicited (background) turn the agent is running, as announced in `_meta`. */
314
+ interface AmbientTurn {
315
+ /** `codebuddy.ai/requestId` of the drain; every update of the run carries it. */
316
+ requestId: string
317
+ startedAt: number
318
+ /**
319
+ * When this drain last showed any sign of life (announcement or an update
320
+ * carrying its request id). {@link BridgeTuning.ambientStaleMs} expires the
321
+ * record from here, so a drain that died silently does not capture a later
322
+ * normal turn as steered.
323
+ */
324
+ lastActivityAt: number
325
+ /** Whether the drain already streamed prose (text or thought) into the transcript. */
326
+ sawOutput: boolean
327
+ }
328
+
329
+ /** A drain id the session stopped waiting on, and when it was retired. */
330
+ interface RetiredAmbient {
331
+ requestId: string
332
+ /**
333
+ * When the wait was retired. `hasRecentRetiredAmbient` uses it to tell a
334
+ * prompt folded into a just-quieted drain from one that ended on its own;
335
+ * `adoptTurnRequestId` / `noteTurnRequestId` ignore the id at any age.
336
+ */
337
+ retiredAt: number
338
+ }
339
+
250
340
  /** Reliability tuning, threaded from `AcpPluginConfig` through the manager. */
251
341
  export interface BridgeTuning {
252
342
  startupTimeoutMs?: number
@@ -341,6 +431,65 @@ export interface BridgeTuning {
341
431
  * probe-only process has no stake in. `0` disables it.
342
432
  */
343
433
  probeIdleTimeoutMs?: number
434
+ /**
435
+ * How long an ambient wait (see `TurnStreamController.awaitAmbient`) stays
436
+ * open after the LAST session update before the turn closes itself.
437
+ *
438
+ * A drain's end is its silence — the SDK's schema rejects the `session_end`
439
+ * CodeBuddy never sends anyway — but "silence" must be measured in the
440
+ * agent's own cadence, not in a chat-app-paced one. Measured against
441
+ * CodeBuddy 2.179.0: the gaps between drain ticks (the
442
+ * `model_requesting`→`model_streaming` hand-off of a real model call) run
443
+ * 1.0–1.4s, so any window below that closes the turn BEFORE the answer
444
+ * exists. 10s leaves room for two slow hand-offs without holding the turn
445
+ * open for a drain that actually finished.
446
+ *
447
+ * Deliberately NOT keyed on an `agentPhase` idle flag: a drain goes idle
448
+ * between queued messages too, so that signal closes the turn in the middle
449
+ * of its own work.
450
+ */
451
+ ambientQuietMs?: number
452
+ /**
453
+ * The hard ceiling on ONE ambient wait: a background run that keeps ticking
454
+ * for minutes without finishing must not hold the conversation's turn open
455
+ * forever. At the ceiling the turn says where the rest of the output will
456
+ * land and closes.
457
+ */
458
+ ambientMaxMs?: number
459
+ /**
460
+ * The hard ceiling on the WORKER phase of a detached-worker hold (see
461
+ * `TurnStreamController.holdForBackgroundDispatch`): the turn dispatched one
462
+ * or more background workers through `Agent` tool calls, the agent answered
463
+ * the prompt, and at least one worker has not reported back yet. The worker
464
+ * phase has NO quiet window on purpose — measured against CodeBuddy 2.179.0,
465
+ * a detached worker's own updates are 13–24s apart while it is perfectly
466
+ * alive — so only EVERY known dispatch reporting (completed / failed) or
467
+ * this ceiling can end it; a drain announcement is recorded but arms no
468
+ * clock before then. Once they have all reported, the drain's own
469
+ * `ambientQuietMs` / `ambientMaxMs` windows take over.
470
+ *
471
+ * Default 600000 ms (10 minutes).
472
+ */
473
+ backgroundTaskMaxMs?: number
474
+ /**
475
+ * How long a PARKED detached-worker hold may wait for the host to open the
476
+ * driver step that carries the worker's report. The prompt response landed
477
+ * between two steps (the host is about to execute the `Agent` card), and a
478
+ * host that never opens the continuation step must not hang the turn. The
479
+ * host normally opens it at once, so this is a safety bound, not a cadence —
480
+ * deliberately far larger than one model hand-off.
481
+ *
482
+ * Default 30000 ms (30 seconds).
483
+ */
484
+ backgroundParkedGraceMs?: number
485
+ /**
486
+ * How long an announced background drain may go without ANY update before a
487
+ * later instant prompt stops treating itself as folded into it. A drain that
488
+ * went silent for minutes is over even though nothing told us; keeping the
489
+ * record would misclassify the next normal turn as steered. Checked lazily
490
+ * when a prompt response arrives (see `streamPrompt`), never on a timer.
491
+ */
492
+ ambientStaleMs?: number
344
493
  }
345
494
 
346
495
  /**
@@ -426,6 +575,11 @@ const DEFAULT_TUNING: Required<BridgeTuning> = {
426
575
  turnStallTickMs: 30_000,
427
576
  idleTimeoutMs: DEFAULT_IDLE_TIMEOUT_MS,
428
577
  probeIdleTimeoutMs: 300_000,
578
+ ambientQuietMs: 10_000,
579
+ ambientMaxMs: 120_000,
580
+ backgroundTaskMaxMs: 600_000,
581
+ backgroundParkedGraceMs: 30_000,
582
+ ambientStaleMs: 300_000,
429
583
  }
430
584
 
431
585
  type BridgeState = 'stopped' | 'starting' | 'ready' | 'error'
@@ -698,11 +852,16 @@ export class AgentBridge {
698
852
  * same session — forbidden by the protocol and rejected (or worse, mishandled)
699
853
  * by the agent. Waiting turns a protocol error into a short delay.
700
854
  *
855
+ * The gate is {@link LiveSession.promptInFlight} + the settle promise, NOT
856
+ * {@link LiveSession.running}: an ambient turn keeps `running` true for the
857
+ * whole drain while its prompt slot is already free, so `running` would
858
+ * refuse every message the user sends during a background task.
859
+ *
701
860
  * @param session - the session asking for the slot.
702
861
  * @returns whether the slot was claimed, and why not when it was not.
703
862
  */
704
863
  async claimTurn(session: LiveSession): Promise<{ ok: true } | { ok: false; reason: 'busy' | 'unsettled' }> {
705
- if (session.running) return { ok: false, reason: 'busy' }
864
+ if (this.promptSlotHeld(session)) return { ok: false, reason: 'busy' }
706
865
  const pending = session.settled
707
866
  if (pending === undefined) return { ok: true }
708
867
  let timer: ReturnType<typeof setTimeout> | undefined
@@ -720,13 +879,21 @@ export class AgentBridge {
720
879
  // The agent never acknowledged: sending now would start a concurrent turn,
721
880
  // so report it honestly instead of handing the agent two prompts at once.
722
881
  if (session.settled !== undefined) return { ok: false, reason: 'unsettled' }
723
- if (session.running) return { ok: false, reason: 'busy' }
882
+ // Re-checked after the wait: another turn may have claimed the slot while
883
+ // this one was waiting on the gate.
884
+ if (this.promptSlotHeld(session)) return { ok: false, reason: 'busy' }
724
885
  return { ok: true }
725
886
  }
726
887
 
888
+ /** Whether a turn currently holds the agent prompt slot (see `promptInFlight`). */
889
+ private promptSlotHeld(session: LiveSession): boolean {
890
+ return session.promptInFlight === true
891
+ }
892
+
727
893
  /**
728
- * Mark the agent-side turn as in flight and hand back the settler its stream
729
- * owner must call once the agent is actually done with it.
894
+ * Mark the agent-side turn as in flight — claim the agent prompt slot and
895
+ * open the settle gate — and hand back the settler its stream owner must
896
+ * call once the agent is actually done with it.
730
897
  *
731
898
  * @param session - the session whose turn is starting.
732
899
  * @returns the settler (idempotent; also used by connection loss).
@@ -736,11 +903,16 @@ export class AgentBridge {
736
903
  const settled = new Promise<void>(resolve => { settle = resolve })
737
904
  session.settled = settled
738
905
  session.settledResolve = settle
906
+ session.promptInFlight = true
739
907
  this.touch()
740
908
  return () => {
909
+ // Identity-guarded: only THIS turn's settler may release the slot. A
910
+ // superseded turn's late settler must not clear the flag a NEWER turn
911
+ // has already claimed (its settle gate was replaced with a new promise).
741
912
  if (session.settled === settled) {
742
913
  session.settled = undefined
743
914
  session.settledResolve = undefined
915
+ session.promptInFlight = false
744
916
  }
745
917
  settle()
746
918
  // The turn is over: a fresh window starts here rather than at the prompt
@@ -752,12 +924,14 @@ export class AgentBridge {
752
924
  /**
753
925
  * Open a session's settle gate without owning its turn stream: the turn's
754
926
  * stream is gone (a resume, a release) but the next prompt still waits on
755
- * this promise, and nothing is left that could ever resolve it.
927
+ * this promise, and nothing is left that could ever resolve it. The prompt
928
+ * slot itself goes with it: whatever held it is gone too.
756
929
  */
757
930
  private openSettleGate(session: LiveSession): void {
758
931
  const settle = session.settledResolve
759
932
  session.settled = undefined
760
933
  session.settledResolve = undefined
934
+ session.promptInFlight = false
761
935
  settle?.()
762
936
  }
763
937
 
@@ -1257,25 +1431,67 @@ export class AgentBridge {
1257
1431
  * {@link BridgeTuning.staleCancelRetryDelayMs}, out-waiting the cleanup
1258
1432
  * window, bounded by {@link BridgeTuning.staleCancelMaxRetries}; a genuinely
1259
1433
  * requested cancel (`cancelRequestedAtTurn` still names this generation)
1260
- * returns as-is.
1434
+ * returns as-is — checked BOTH before the delay and after it, so a second
1435
+ * stop click inside the retry window does not resend the prompt the user
1436
+ * just stopped.
1437
+ *
1438
+ * `turnGen` is the {@link LiveSession.turnSeq} this prompt was sent for. A
1439
+ * stale-cancel resend is only allowed while that generation still owns the
1440
+ * session: once a successor turn began (the cancel fallback finalized this
1441
+ * one and the user moved on), resending would race a second `session/prompt`
1442
+ * into the successor's turn. The late answer is returned as-is instead.
1443
+ *
1444
+ * The RESPONSE's `_meta` travels with the stop reason: it is the only place
1445
+ * an agent says whether the prompt actually queued a conversation turn (see
1446
+ * `CodebuddyMeta.conversationRequestId`), which decides whether an instant
1447
+ * answer was the turn ending or a fold into a background drain.
1261
1448
  */
1262
- async runPrompt(session: LiveSession, text: string, turnId: string): Promise<string> {
1449
+ async runPrompt(session: LiveSession, prompt: string | ContentBlock[], turnId: string, turnGen: number): Promise<{ stopReason: string; meta?: Record<string, unknown> }> {
1263
1450
  const conn = this.conn
1264
1451
  if (conn === undefined || session.closed || session.released) throw new AcpError('session_closed', 'acp: session is closed')
1265
1452
  this.touch()
1453
+
1454
+ const blocks: ContentBlock[] = typeof prompt === 'string'
1455
+ ? [{ type: 'text', text: prompt }]
1456
+ : prompt
1457
+
1458
+ if (blocks.length === 0) {
1459
+ throw new AcpError('empty_prompt', 'acp: prompt cannot be empty')
1460
+ }
1461
+
1462
+ const hasImages = blocks.some(b => b.type === 'image')
1463
+ if (hasImages && this.initResult?.agentCapabilities?.promptCapabilities?.image !== true) {
1464
+ throw new AcpError('image_unsupported', `acp: agent "${this.def.id}" does not support image input`)
1465
+ }
1466
+
1266
1467
  for (let attempt = 0; ; attempt += 1) {
1267
1468
  // The response is activity too: the idle window must start at the turn's
1268
1469
  // END, not at the prompt that may have started a long time ago.
1269
1470
  const result = await conn.agent.request(methods.agent.session.prompt, {
1270
1471
  sessionId: session.acpSessionId,
1271
- prompt: [{ type: 'text', text }],
1472
+ prompt: blocks,
1272
1473
  }).finally(() => { this.touch() })
1474
+ const answered = () => ({
1475
+ stopReason: result.stopReason,
1476
+ ...(result._meta !== undefined && result._meta !== null ? { meta: result._meta } : {}),
1477
+ })
1273
1478
  if (result.stopReason !== 'cancelled' || session.cancelRequestedAtTurn === session.turnSeq) {
1274
- return result.stopReason
1479
+ return answered()
1275
1480
  }
1276
- if (attempt >= this.staleCancelMaxRetries) return result.stopReason
1481
+ // The turn this answer belongs to no longer owns the session (a cancel
1482
+ // fallback finalized it and a successor began): resending would fire a
1483
+ // second `session/prompt` into the successor's turn.
1484
+ if (session.turnSeq !== turnGen) return answered()
1485
+ if (attempt >= this.staleCancelMaxRetries) return answered()
1277
1486
  await new Promise(resolve => setTimeout(resolve, this.staleCancelRetryDelayMs))
1278
1487
  if (session.closed || this.conn !== conn) throw new AcpError('session_closed', 'acp: session is closed')
1488
+ // Re-check after the delay: the successor may have started while waiting.
1489
+ if (session.turnSeq !== turnGen) return answered()
1490
+ // ...and a SECOND stop during the delay names this same generation. The
1491
+ // user already said stop, so the stale-cancel answer is not a swallow to
1492
+ // out-wait anymore: resending would make the agent run the prompt the
1493
+ // user just stopped (the double-click case). Return as-is.
1494
+ if (session.cancelRequestedAtTurn === session.turnSeq) return answered()
1279
1495
  this.touch()
1280
1496
  }
1281
1497
  }
@@ -1326,16 +1542,30 @@ export class AgentBridge {
1326
1542
 
1327
1543
  /**
1328
1544
  * Force the local end of one turn whose agent never acknowledged it: clear
1329
- * `running`, open the settle gate, and broadcast the turn end with `turnId:
1330
- * ''` (there is no agent response to attribute it to). Shared by the cancel
1545
+ * `running`, open the settle gate, and end the turn. Shared by the cancel
1331
1546
  * fallback and the stall watchdog.
1332
1547
  *
1548
+ * A live controller owns the turn's streaming face — its segment queue, its
1549
+ * ambient clocks and its `turn/end` id. Ending the turn behind its back
1550
+ * would leave the driver step parked on an open queue, and the controller's
1551
+ * own timers would later broadcast a SECOND `turn/end` for a turn already
1552
+ * closed. So when the session still points at a current controller, the
1553
+ * force goes THROUGH it (`forceFinalize`), which runs the same closing work
1554
+ * as a normal settle exactly once. Only a turn with no live controller (an
1555
+ * `/acp`-RPC turn, or one already retired) falls back to the local broadcast
1556
+ * with `turnId: ''` — there is no stream whose id could be named.
1557
+ *
1333
1558
  * Generation-guarded: a turn that already ended — or that a later turn
1334
1559
  * superseded — is not this call's to finalize. Returns whether it acted; a
1335
1560
  * `false` means the caller was late and must leave the session alone.
1336
1561
  */
1337
1562
  finalizeTurn(session: LiveSession, turnGen: number, message: string): boolean {
1338
1563
  if (session.closed || session.turnSeq !== turnGen) return false
1564
+ const controller = session.turnController
1565
+ if (controller !== undefined && !controller.stale) {
1566
+ controller.forceFinalize({ kind: 'error', message })
1567
+ return true
1568
+ }
1339
1569
  // Nothing is in flight anymore (the turn settled on its own): nothing to
1340
1570
  // finalize. A still-pending settle gate counts as in flight even though
1341
1571
  // the stream is gone — that gate is what the next prompt waits on.
@@ -1403,10 +1633,12 @@ export class AgentBridge {
1403
1633
  session.closed = false
1404
1634
  session.released = false
1405
1635
  // A resume is a fresh agent-side state: nothing from the dead process's
1406
- // turn survives it, including the settle gate and the stall markers.
1636
+ // turn survives it, including the settle gate and the stall markers. A
1637
+ // background drain belonged to that process too.
1407
1638
  session.orphanPromptTurnSeq = undefined
1408
1639
  session.reclaimedTurnSeq = undefined
1409
1640
  session.activeToolCalls.clear()
1641
+ clearAmbientState(session)
1410
1642
  session.lastAgentActivityAt = Date.now()
1411
1643
  this.openSettleGate(session)
1412
1644
  // Route replay updates into the fresh transcript while the load request
@@ -1469,8 +1701,10 @@ export class AgentBridge {
1469
1701
  // would make the next prompt wait for an answer that can no longer come.
1470
1702
  this.openSettleGate(session)
1471
1703
  // No call from the dead process can still be running: drop the tool set
1472
- // so a resumed turn starts on the plain window.
1704
+ // so a resumed turn starts on the plain window. A background drain died
1705
+ // with it, and a stale ambient would misclassify the NEXT prompt.
1473
1706
  session.activeToolCalls.clear()
1707
+ clearAmbientState(session)
1474
1708
  session.released = true
1475
1709
  this.settlePermissions(session)
1476
1710
  this.released.set(session.sessionId, session)
@@ -1581,6 +1815,12 @@ export class AgentBridge {
1581
1815
  async closeSession(session: LiveSession, reason: 'closed' | 'agent-stopped' | 'agent-died'): Promise<void> {
1582
1816
  if (session.closed) return
1583
1817
  session.closed = true
1818
+ // The session is going away: whatever background drain it announced ends
1819
+ // with it (a re-created session must not inherit a stale ambient).
1820
+ clearAmbientState(session)
1821
+ // ...and so does its prompt slot: nothing may keep rejecting a new prompt
1822
+ // for a record that is being deleted.
1823
+ session.promptInFlight = false
1584
1824
  // Closing a session is activity of its own: restart the idle window here
1585
1825
  // rather than at the prompt that opened it.
1586
1826
  this.touch()
@@ -1655,6 +1895,7 @@ export class AgentBridge {
1655
1895
  this.settlePermissions(session)
1656
1896
  this.openSettleGate(session)
1657
1897
  session.activeToolCalls.clear()
1898
+ clearAmbientState(session)
1658
1899
  this.sessions.delete(session.sessionId)
1659
1900
  this.acpIndex.delete(session.acpSessionId)
1660
1901
  this.hub.broadcast({ type: 'session/closed', sessionId: session.sessionId, reason })
@@ -1937,6 +2178,60 @@ export class AgentBridge {
1937
2178
  // tool, which earns the wider watchdog window.
1938
2179
  session.lastAgentActivityAt = Date.now()
1939
2180
  const kind = params.update.sessionUpdate
2181
+ const meta = codebuddyMetaOf(params.update)
2182
+ // A RETIRED drain that speaks again is alive, not retired. Un-retire it
2183
+ // BEFORE this update is classified and rebuild the ambient record: the run
2184
+ // may never re-announce itself, yet the next prompt folded into it must
2185
+ // still be recognised as steered and receive the drain's text. A
2186
+ // re-announcement below does the same for its id.
2187
+ if (meta?.requestId !== undefined && isRetiredAmbientId(session, meta.requestId)) {
2188
+ reviveAmbientId(session, meta.requestId)
2189
+ session.ambient = {
2190
+ requestId: meta.requestId,
2191
+ startedAt: session.ambient?.startedAt ?? Date.now(),
2192
+ lastActivityAt: Date.now(),
2193
+ sawOutput: session.ambient?.sawOutput ?? false,
2194
+ }
2195
+ }
2196
+ if (meta?.unsolicitedTurn !== undefined) {
2197
+ // The agent started a background turn on its own: remember it so a
2198
+ // prompt the agent folds into it (answered instantly, no conversation
2199
+ // request id) can be kept open until the drain speaks. Announced once per
2200
+ // request id — a repeated announcement is the same run, not news.
2201
+ const run = meta.unsolicitedTurn
2202
+ // A re-announcement also revives a drain whose wait already closed: the
2203
+ // same run is alive, and the notice de-dupe set keeps it silent.
2204
+ reviveAmbientId(session, run.requestId)
2205
+ session.ambient = { requestId: run.requestId, startedAt: Date.now(), lastActivityAt: Date.now(), sawOutput: false }
2206
+ const announced = session.ambientNotices ?? new Set<string>()
2207
+ session.ambientNotices = announced
2208
+ if (!announced.has(run.requestId)) {
2209
+ announced.add(run.requestId)
2210
+ // The de-dupe set outlives individual turns (a hard ceiling keeps the
2211
+ // drain record), so it is bounded: evict the oldest ids first.
2212
+ while (announced.size > AMBIENT_NOTICE_MAX) {
2213
+ const oldest = announced.values().next().value
2214
+ if (oldest === undefined) break
2215
+ announced.delete(oldest)
2216
+ }
2217
+ this.hub.broadcast({
2218
+ type: 'agent/notice',
2219
+ agentId: session.agentId,
2220
+ level: 'info',
2221
+ text: `acp: agent "${this.def.id}" started a background task turn (${run.reason ?? 'unsolicited'}); its output will appear in the ACP transcript view, or in the turn that was waiting for it.`,
2222
+ })
2223
+ }
2224
+ }
2225
+ if (session.ambient !== undefined
2226
+ && meta?.requestId === session.ambient.requestId) {
2227
+ // The run is alive: refresh the staleness clock on ANY update it owns,
2228
+ // not just prose (a tool-heavy stretch is still the drain working).
2229
+ session.ambient.lastActivityAt = Date.now()
2230
+ if ((kind === 'agent_message_chunk' || kind === 'agent_thought_chunk')
2231
+ && chunkTextOf((params.update as { content?: unknown }).content) !== '') {
2232
+ session.ambient.sawOutput = true
2233
+ }
2234
+ }
1940
2235
  if (kind === 'tool_call' || kind === 'tool_call_update') {
1941
2236
  const { toolCallId, status } = params.update as { toolCallId: string; status?: string | null }
1942
2237
  if (status === 'completed' || status === 'failed') session.activeToolCalls.delete(toolCallId)
@@ -1994,6 +2289,178 @@ export class AgentBridge {
1994
2289
  }
1995
2290
  }
1996
2291
 
2292
+ /**
2293
+ * The CodeBuddy `_meta` facts this bridge reads, when the object carries them.
2294
+ *
2295
+ * CodeBuddy runs "background drains": a sub-agent completion notice starts a
2296
+ * turn the client never prompted, announced by a `session_info_update` whose
2297
+ * `_meta["codebuddy.ai/unsolicitedTurn"]` names the run. Every update of that
2298
+ * run then carries `_meta["codebuddy.ai/requestId"]`, and a `session/prompt`
2299
+ * answered instantly (with no `codebuddy.ai/conversationRequestId` on the
2300
+ * RESPONSE) had its message folded into the drain — the real answer streams
2301
+ * afterwards, under the drain's request id.
2302
+ *
2303
+ * The same run also explains a DETACHED WORKER: `_meta["codebuddy.ai/toolName"]`
2304
+ * names the `Agent` call that dispatched it, the call is re-sent with
2305
+ * `_meta["codebuddy.ai/isBackground"] = true` once the worker runs off-turn, and
2306
+ * every update the worker emits carries
2307
+ * `_meta["codebuddy.ai/parentToolCallId"]` naming that call. Those updates are
2308
+ * the worker's own turn — they belong in the transcript, not in the session's
2309
+ * assistant message. The completion of the dispatch call itself carries NO
2310
+ * `_meta` at all, so a known call's status is read from its id.
2311
+ *
2312
+ * No other agent sends these keys: an object without them yields `undefined`,
2313
+ * so every existing path is untouched. Values are only read when they have the
2314
+ * declared shape (never guessed from a message body).
2315
+ */
2316
+ interface CodebuddyMeta {
2317
+ /** The run this update belongs to. */
2318
+ requestId?: string
2319
+ /** Set on the `session_info_update` that STARTS a background turn. */
2320
+ unsolicitedTurn?: { requestId: string; reason?: string }
2321
+ /** The agent's own phase heartbeat (`{ phase }`), a liveness tick. */
2322
+ agentPhase?: { phase: string }
2323
+ /** Set on a `session/prompt` RESPONSE that really queued a conversation turn. */
2324
+ conversationRequestId?: string
2325
+ /**
2326
+ * The tool a `tool_call` names, when the agent omits `title`. `Agent` (or
2327
+ * `Task`) means the call DISPATCHED a detached worker: the call stays in
2328
+ * progress while the worker runs on its own.
2329
+ */
2330
+ toolName?: string
2331
+ /**
2332
+ * Set on every update a DETACHED WORKER emits (prose, tools, its report):
2333
+ * it names the `Agent` tool call that dispatched the worker. Such an update
2334
+ * belongs to the worker's own turn, never to this session's answer.
2335
+ */
2336
+ parentToolCallId?: string
2337
+ /**
2338
+ * Set on the dispatch call RE-SENT after the worker started
2339
+ * (`codebuddy.ai/isBackground: true`): the call handed its work to a worker
2340
+ * that keeps running off-turn, which is the structural "detached" signal.
2341
+ * The first send of the same call does not carry it.
2342
+ */
2343
+ isBackground?: boolean
2344
+ /** `codebuddy.ai/isSubagent` — the same re-send declares the worker a sub-agent. */
2345
+ isSubagent?: boolean
2346
+ }
2347
+
2348
+ /** Read the CodeBuddy `_meta` keys off an update or a `{ _meta }` wrapper. */
2349
+ function codebuddyMetaOf(source: unknown): CodebuddyMeta | undefined {
2350
+ if (typeof source !== 'object' || source === null) return undefined
2351
+ const row = source as Record<string, unknown>
2352
+ const rawMeta = row._meta
2353
+ if (typeof rawMeta !== 'object' || rawMeta === null) return undefined
2354
+ const meta = rawMeta as Record<string, unknown>
2355
+ const out: CodebuddyMeta = {}
2356
+ const requestId = meta['codebuddy.ai/requestId']
2357
+ if (typeof requestId === 'string' && requestId !== '') out.requestId = requestId
2358
+ const conversationRequestId = meta['codebuddy.ai/conversationRequestId']
2359
+ if (typeof conversationRequestId === 'string' && conversationRequestId !== '') {
2360
+ out.conversationRequestId = conversationRequestId
2361
+ }
2362
+ const toolName = meta['codebuddy.ai/toolName']
2363
+ if (typeof toolName === 'string' && toolName !== '') out.toolName = toolName
2364
+ const parentToolCallId = meta['codebuddy.ai/parentToolCallId']
2365
+ if (typeof parentToolCallId === 'string' && parentToolCallId !== '') out.parentToolCallId = parentToolCallId
2366
+ // Booleans, read as booleans only: a string "true" is not the declared shape
2367
+ // and guessing from it would make another agent's unrelated key look detached.
2368
+ if (meta['codebuddy.ai/isBackground'] === true) out.isBackground = true
2369
+ if (meta['codebuddy.ai/isSubagent'] === true) out.isSubagent = true
2370
+ const unsolicited = meta['codebuddy.ai/unsolicitedTurn']
2371
+ if (typeof unsolicited === 'object' && unsolicited !== null) {
2372
+ const run = unsolicited as Record<string, unknown>
2373
+ if (typeof run.requestId === 'string' && run.requestId !== '') {
2374
+ out.unsolicitedTurn = {
2375
+ requestId: run.requestId,
2376
+ ...(typeof run.reason === 'string' && run.reason !== '' ? { reason: run.reason } : {}),
2377
+ }
2378
+ }
2379
+ }
2380
+ const phase = meta['codebuddy.ai/agentPhase']
2381
+ if (typeof phase === 'object' && phase !== null) {
2382
+ const tick = phase as Record<string, unknown>
2383
+ if (typeof tick.phase === 'string' && tick.phase !== '') out.agentPhase = { phase: tick.phase }
2384
+ }
2385
+ return Object.keys(out).length === 0 ? undefined : out
2386
+ }
2387
+
2388
+ /**
2389
+ * Remember a background drain this session stopped waiting on. A quiet close
2390
+ * drops the drain record (so an ordinary next turn is not held open for it),
2391
+ * but the run may revive afterwards; keeping its id lets every later update be
2392
+ * recognized as the drain's (`isRetiredAmbientId`) instead of being adopted as
2393
+ * the next turn's own. Bounded, oldest first — an id this far back names a run
2394
+ * no late update can plausibly still carry. Re-retiring an id refreshes its
2395
+ * timestamp and moves it to the end.
2396
+ */
2397
+ function retireAmbientId(session: LiveSession, requestId: string): void {
2398
+ const retired = session.retiredAmbients ?? []
2399
+ const existing = retired.findIndex(entry => entry.requestId === requestId)
2400
+ if (existing >= 0) retired.splice(existing, 1)
2401
+ retired.push({ requestId, retiredAt: Date.now() })
2402
+ while (retired.length > RETIRED_AMBIENT_MAX) retired.shift()
2403
+ session.retiredAmbients = retired
2404
+ }
2405
+
2406
+ /** Whether this request id names a drain the session already stopped waiting on. */
2407
+ function isRetiredAmbientId(session: LiveSession, requestId: string | undefined): boolean {
2408
+ return requestId !== undefined
2409
+ && session.retiredAmbients?.some(entry => entry.requestId === requestId) === true
2410
+ }
2411
+
2412
+ /**
2413
+ * Whether the session retired a drain recently enough that a prompt answered
2414
+ * with no ambient record and no conversation id may have been folded into it.
2415
+ * A quiet close is only evidence WE stopped waiting, so the next instant
2416
+ * answer is ambiguous; past this horizon the drain is treated as gone and the
2417
+ * ordinary zero-output warning applies.
2418
+ */
2419
+ function hasRecentRetiredAmbient(session: LiveSession, withinMs: number): boolean {
2420
+ const retired = session.retiredAmbients
2421
+ if (retired === undefined || retired.length === 0) return false
2422
+ const cutoff = Date.now() - withinMs
2423
+ return retired.some(entry => entry.retiredAt >= cutoff)
2424
+ }
2425
+
2426
+ /**
2427
+ * The retired drain spoke again (an update or a re-announcement carried its
2428
+ * id): it is alive, not retired. The caller rebuilds {@link LiveSession.ambient}
2429
+ * so the next prompt folded into it is recognised as steered and its text can
2430
+ * reach the user's turn; only while the id stays retired does its text remain
2431
+ * transcript-only.
2432
+ */
2433
+ function reviveAmbientId(session: LiveSession, requestId: string): void {
2434
+ const retired = session.retiredAmbients
2435
+ if (retired === undefined) return
2436
+ const index = retired.findIndex(entry => entry.requestId === requestId)
2437
+ if (index < 0) return
2438
+ retired.splice(index, 1)
2439
+ if (retired.length === 0) session.retiredAmbients = undefined
2440
+ }
2441
+
2442
+ /**
2443
+ * Forget every background-drain trace a session carried. Called at each
2444
+ * lifecycle boundary (close / release / resume / force reset): a fresh
2445
+ * agent-side state neither inherits a stale drain nor the ids of drains that
2446
+ * belonged to the old one.
2447
+ */
2448
+ function clearAmbientState(session: LiveSession): void {
2449
+ session.ambient = undefined
2450
+ session.ambientNotices?.clear()
2451
+ session.retiredAmbients = undefined
2452
+ }
2453
+
2454
+ /**
2455
+ * The closing line of a turn the agent deferred to its OWN background run: the
2456
+ * answer exists, but in the ACP transcript view rather than in this bubble.
2457
+ * Shared by the ambient wait and the detached-worker hold, so both say the
2458
+ * same thing in the same words.
2459
+ */
2460
+ function backgroundDeferralLine(agentId: string): string {
2461
+ return `\n[dsh-ntr] agent "${agentId}" deferred this turn to a background task run; its output is in the ACP transcript view. Send again after the background run finishes.\n`
2462
+ }
2463
+
1997
2464
  /** Human-facing identity of an ACP tool call (the approval headline). */
1998
2465
  /**
1999
2466
  * Human-facing name of the tool an ask is about, for the chat line and the
@@ -2085,6 +2552,7 @@ function snapshot(session: LiveSession): SessionView {
2085
2552
  ...(session.availableCommands !== undefined ? { availableCommands: session.availableCommands } : {}),
2086
2553
  ...(session.plan !== undefined ? { plan: session.plan } : {}),
2087
2554
  ...(session.usage !== undefined ? { usage: session.usage } : {}),
2555
+ ...(session.burst !== undefined ? { burst: session.burst } : {}),
2088
2556
  running: session.running,
2089
2557
  closed: session.closed,
2090
2558
  ...(session.released ? { released: true } : {}),
@@ -2127,6 +2595,7 @@ interface PersistedChatSession {
2127
2595
  reasoningEffort?: string
2128
2596
  modes?: SessionView['modes']
2129
2597
  usage?: SessionView['usage']
2598
+ burst?: SessionView['burst']
2130
2599
  hasHistory?: boolean
2131
2600
  }
2132
2601
 
@@ -2142,6 +2611,7 @@ function persistedChatSession(session: LiveSession): PersistedChatSession {
2142
2611
  ...(session.reasoningEffort === undefined ? {} : { reasoningEffort: session.reasoningEffort }),
2143
2612
  ...(session.modes === undefined ? {} : { modes: session.modes }),
2144
2613
  ...(session.usage === undefined ? {} : { usage: session.usage }),
2614
+ ...(session.burst === undefined ? {} : { burst: session.burst }),
2145
2615
  ...(session.hasHistory === true || session.transcript.since(0).length > 0 ? { hasHistory: true } : {}),
2146
2616
  }
2147
2617
  }
@@ -2160,6 +2630,14 @@ function isPersistedUsage(value: unknown): value is SessionView['usage'] {
2160
2630
  return typeof usage.used === 'number' && typeof usage.size === 'number'
2161
2631
  }
2162
2632
 
2633
+ /** Tolerant shape check for a `burst` aggregate read back from disk. */
2634
+ function isPersistedBurst(value: unknown): value is NonNullable<SessionView['burst']> {
2635
+ if (value === null || typeof value !== 'object') return false
2636
+ const burst = value as { outputTokens?: unknown; burstMs?: unknown }
2637
+ return typeof burst.outputTokens === 'number' && burst.outputTokens >= 0
2638
+ && typeof burst.burstMs === 'number' && burst.burstMs >= 0
2639
+ }
2640
+
2163
2641
  /**
2164
2642
  * The DSH chat session's OWN workspace, when one is resolvable: the directory
2165
2643
  * the user selected in the DSH sidebar, which the host reports back as the
@@ -2925,12 +3403,17 @@ interface SegmentState {
2925
3403
  activeToolCall: AcpToolCallSnapshot | undefined
2926
3404
  /** Whether this segment opened at least one tool-call block. */
2927
3405
  hasCard: boolean
3406
+ /**
3407
+ * The segment belongs to an ambient wait: the agent folded our prompt into a
3408
+ * background drain it started itself. Tool traffic is recorded (so the
3409
+ * transcript and any parked tool-output waiter get it) but opens NO card —
3410
+ * a card would make the host loop execute a tool for a turn whose answer has
3411
+ * not even started streaming yet.
3412
+ */
3413
+ ambient: boolean
2928
3414
  finished: boolean
2929
3415
  }
2930
3416
 
2931
- /** ACP `usage_update` is context occupancy, not request tokens; host usage is a 4-chars/token OUTPUT estimate (tool args excluded). */
2932
- const OUTPUT_CHARS_PER_TOKEN = 4
2933
-
2934
3417
  /** How one segment's stream ends, in `StreamChunk.finish` terms. */
2935
3418
  type SegmentFinishReason =
2936
3419
  | { kind: 'stop' }
@@ -2943,6 +3426,153 @@ type TurnSettle =
2943
3426
  | { kind: 'cancelled' }
2944
3427
  | { kind: 'error'; message: string; code?: string }
2945
3428
 
3429
+ /** How often a running turn's live burst aggregate is pushed to the browser. */
3430
+ const TPS_PUSH_INTERVAL_MS = 1000
3431
+
3432
+ /**
3433
+ * Bounds on the updates withheld while a turn's own request id is still
3434
+ * unknown (see `TurnStreamController.bufferAmbientUpdate`). The buffer exists
3435
+ * only for the response window of one prompt, but a chatty drain could stream
3436
+ * for a while inside it; over the bound the OLDEST entries are dropped — the
3437
+ * transcript still has every one of them.
3438
+ */
3439
+ const PENDING_AMBIENT_MAX_ENTRIES = 200
3440
+ const PENDING_AMBIENT_MAX_CHARS = 64 * 1024
3441
+
3442
+ /**
3443
+ * Bound on {@link LiveSession.ambientNotices}. The set now outlives the turn
3444
+ * that announced a drain (a hard ceiling keeps the record, and a repeated
3445
+ * announcement of the same run must stay silent), so it needs a ceiling of its
3446
+ * own for a chatty agent. Oldest ids are evicted first; an id that far back is
3447
+ * a run nobody is waiting on anymore.
3448
+ */
3449
+ const AMBIENT_NOTICE_MAX = 64
3450
+
3451
+ /**
3452
+ * Bound on {@link LiveSession.retiredAmbients}. The ids only exist to catch a
3453
+ * drain that speaks again after its wait closed, and the timestamp lets the
3454
+ * next silent close recognise a recent fold — a short-horizon concern, so a
3455
+ * small ring is enough; the oldest is evicted first.
3456
+ */
3457
+ const RETIRED_AMBIENT_MAX = 32
3458
+
3459
+ /**
3460
+ * Floor for the "recently retired" lookback the zero-output fallback uses
3461
+ * (twice `ambientQuietMs`). Only test-sized quiet windows fall below it; a
3462
+ * production window (10s) always wins. Without the floor a 250ms quiet window
3463
+ * would leave a 500ms lookback, too tight to survive a loaded CI scheduler
3464
+ * between one turn ending and the next prompt's response.
3465
+ */
3466
+ const RETIRED_AMBIENT_RECENT_MIN_MS = 1_000
3467
+
3468
+ /** One ambient wait's clocks and the stop reason it will close the turn with. */
3469
+ interface AmbientWait {
3470
+ /** The drain's request id: the turn is waiting on this run, not on its own. */
3471
+ requestId: string
3472
+ quietTimer: ReturnType<typeof setTimeout>
3473
+ hardTimer: ReturnType<typeof setTimeout>
3474
+ /** The stop reason the agent's `session/prompt` response carried. */
3475
+ stopReason: string
3476
+ /**
3477
+ * Whether the CEILING closed this wait (the drain was still ticking) rather
3478
+ * than silence. A hard ending must keep the session's drain record: the next
3479
+ * prompt may be folded into the same run, and it is still alive.
3480
+ */
3481
+ hard?: boolean
3482
+ }
3483
+
3484
+ /** An update withheld from the segment until the turn's own request id is known. */
3485
+ interface PendingAmbientUpdate {
3486
+ seq: number
3487
+ update: Record<string, unknown>
3488
+ /** `JSON.stringify(update).length`, kept so eviction need not re-serialize. */
3489
+ chars: number
3490
+ }
3491
+
3492
+ /**
3493
+ * The `Agent` tool call that handed work to a DETACHED worker.
3494
+ *
3495
+ * CodeBuddy 2.179.0 with multitask on: the coordinator opens an `Agent` call
3496
+ * (`_meta["codebuddy.ai/toolName"] = "Agent"`), says "已派…", and ENDS ITS OWN
3497
+ * PROMPT normally while the worker keeps running off-turn. The call itself
3498
+ * stays `in_progress` until the worker reports, and only then does the agent
3499
+ * start the background drain that carries the worker's report into the
3500
+ * session. A turn that closed at its prompt response therefore lost the only
3501
+ * answer it was ever going to produce; this record is what the controller
3502
+ * holds the turn open for.
3503
+ */
3504
+ interface BackgroundDispatch {
3505
+ /** The `Agent` call's id — the card that stays on the plane. */
3506
+ toolCallId: string
3507
+ /** The last status the agent declared for the dispatch call itself. */
3508
+ status: string
3509
+ /** Whether the call already reported completed / failed. */
3510
+ completed: boolean
3511
+ /**
3512
+ * Whether the work runs DETACHED, read from the wire's own structure: the
3513
+ * call re-sent with `_meta["codebuddy.ai/isBackground"] = true`, or any
3514
+ * update naming this call as its `parentToolCallId`. A synchronous
3515
+ * `Agent` / `Task` call carries neither, so it can never make the turn wait
3516
+ * after its own answer.
3517
+ */
3518
+ detached: boolean
3519
+ /** Whether any update came FROM the worker (`codebuddy.ai/parentToolCallId`). */
3520
+ sawWorkerUpdate: boolean
3521
+ }
3522
+
3523
+ /**
3524
+ * A turn held open for the detached worker its `Agent` call dispatched.
3525
+ *
3526
+ * Two phases, because their liveness signals differ:
3527
+ * - `worker`: at least one worker runs off-turn and may be silent for tens of
3528
+ * seconds between model calls — NO quiet timer, only
3529
+ * {@link BridgeTuning.backgroundTaskMaxMs}. It ends when EVERY known dispatch
3530
+ * reported (completed / failed); a drain announcement is recorded meanwhile
3531
+ * but arms no clock.
3532
+ * - `drain`: every dispatch reported and the background turn that carries
3533
+ * their report is (or is about to start) — the existing ambient windows
3534
+ * apply (`ambientQuietMs` quiet, reset by every update; `ambientMaxMs`
3535
+ * ceiling from the switch).
3536
+ */
3537
+ interface BackgroundHold {
3538
+ /** The stop reason the prompt RESPONSE carried (what the turn closes with). */
3539
+ stopReason: string
3540
+ /** The run this hold currently waits on: the response's id or the last announcement. */
3541
+ requestId?: string
3542
+ /**
3543
+ * Every drain id this hold may fold text from: the response's
3544
+ * `conversationRequestId` / `requestId` plus every announced id. A run
3545
+ * announced before the response may name a different id than the response —
3546
+ * both are this turn's answer, and anything else stays background noise.
3547
+ */
3548
+ acceptedIds: Set<string>
3549
+ phase: 'worker' | 'drain'
3550
+ /**
3551
+ * Agent MESSAGE text folded into this hold. The parent's interim
3552
+ * acknowledgement ("已派…") landed before the hold and is not the drained
3553
+ * run's answer: a hold that never received that answer must still say where
3554
+ * it will appear.
3555
+ */
3556
+ answerChars: number
3557
+ /**
3558
+ * The transcript head when the hold began. Text at or below it arrived
3559
+ * before the hold — a parked hold replays the parent's ack from there — so
3560
+ * it is not counted in {@link answerChars}; the drain updates withheld for
3561
+ * the response are the exception and are counted explicitly when flushed.
3562
+ */
3563
+ floorSeq: number
3564
+ quietTimer?: ReturnType<typeof setTimeout>
3565
+ hardTimer?: ReturnType<typeof setTimeout>
3566
+ /**
3567
+ * The bounded wait for a driver step to speak into. Only while the hold is
3568
+ * PARKED (`pendingBackgroundHold`): the response landed between steps, and
3569
+ * the host is expected to open the continuation step for the Agent card.
3570
+ */
3571
+ graceTimer?: ReturnType<typeof setTimeout>
3572
+ /** Whether the run's ids were already released (a close, cancel or teardown). */
3573
+ retired: boolean
3574
+ }
3575
+
2946
3576
  /**
2947
3577
  * The state of ONE ACP prompt, shared across every driver step of the turn.
2948
3578
  *
@@ -2956,13 +3586,111 @@ type TurnSettle =
2956
3586
  class TurnStreamController {
2957
3587
  private segment: SegmentState | undefined
2958
3588
  private settled = false
3589
+ /** Whether the turn's closing work already ran (see `closeTurn`). */
3590
+ private closedTurn = false
2959
3591
  private disposed = false
2960
3592
  private readonly seenCalls = new Set<string>()
2961
3593
  private readonly announcedKinds = new Map<string, string>()
2962
3594
  /** Narrative lines (permission asks) that arrived while no segment was open. */
2963
3595
  private readonly pendingProse: string[] = []
2964
3596
  sawVisibleBlock = false
3597
+ /**
3598
+ * Whether THIS turn streamed its own prose (text or reasoning), i.e. an
3599
+ * answer that is not the background drain's. Tool cards do not count — a
3600
+ * drain that only ran tools delivered no words to the conversation.
3601
+ */
3602
+ private sawAgentProse = false
3603
+ /**
3604
+ * Whether any `agent_message_chunk` text reached the plane. Reasoning does
3605
+ * not count: a steer whose drain only thought is still a turn with no answer
3606
+ * text, and must say so when it closes.
3607
+ */
3608
+ private sawVisibleText = false
3609
+ /**
3610
+ * The CodeBuddy request id this turn has adopted as its own (from the
3611
+ * prompt RESPONSE's `conversationRequestId`, or from the first update that
3612
+ * named one). Used to keep a background drain's prose out of the user's
3613
+ * answer; `undefined` means "unknown" and nothing is split (fail-open).
3614
+ */
3615
+ private turnRequestId?: string
3616
+ /**
3617
+ * Updates the session's background drain already streamed while this turn's
3618
+ * OWN request id was still unknown. They are NOT folded on arrival: a drain
3619
+ * tick that lands before the prompt response could otherwise teach the turn
3620
+ * the drain's id (`noteTurnRequestId`) and mix the drain's text into the
3621
+ * user's answer. Withheld here (cursor already advanced — the transcript
3622
+ * keeps a copy) until the response decides what this turn is:
3623
+ * - a normal turn (response carries a conversation id) discards them — they
3624
+ * were never this turn's answer;
3625
+ * - a steered turn flushes them into the segment, in order, once
3626
+ * `awaitAmbient` marks the segment ambient (tool cards suppressed).
3627
+ */
3628
+ private readonly pendingAmbientUpdates: PendingAmbientUpdate[] = []
3629
+ private pendingAmbientChars = 0
3630
+ /**
3631
+ * Whether the bounded buffer dropped at least one update. The flush then
3632
+ * says so in the conversation instead of silently losing the front of the
3633
+ * drain's output (constraint 29: ACP payloads are never dropped silently).
3634
+ */
3635
+ private pendingAmbientTruncated = false
3636
+ /** The ambient wait in progress, if the agent folded this prompt into a drain. */
3637
+ private awaitingAmbient?: AmbientWait
3638
+ /**
3639
+ * The prompt response named no conversation turn while a drain this session
3640
+ * retired RECENTLY may still be running without re-announcing. The turn still
3641
+ * settles at once (a drain record that no update revived is not waited on),
3642
+ * but its zero-output report says the turn may have been folded into that run
3643
+ * instead of blaming the agent and suggesting a resend.
3644
+ */
3645
+ private recentRetiredDrain = false
3646
+ /** The outcome of a settle whose closing work is waiting one macrotask (see `settle`). */
3647
+ private pendingOutcome?: TurnSettle
3648
+ private closeScheduled = false
2965
3649
  private cursorSeq: number
3650
+ /**
3651
+ * Burst tracker for this turn, fed from the LIVE hub frames only. Segment
3652
+ * replay must not touch it: every event is already counted once when it
3653
+ * arrived, and replay would double the numerator and the gaps.
3654
+ */
3655
+ private readonly tps = new TurnTpsTracker()
3656
+ private tpsSeq: number
3657
+ private lastTpsPushAt = 0
3658
+ /**
3659
+ * The ambient quiet/ceiling windows, read once from the manager: they are
3660
+ * deployment tuning, not per-tick facts, and small test values must be stable
3661
+ * for the whole turn (see {@link BridgeTuning.ambientQuietMs} for the
3662
+ * measured CodeBuddy tick cadence these defaults come from).
3663
+ */
3664
+ private readonly ambientTuning: { quietMs: number; maxMs: number }
3665
+ /**
3666
+ * The detached workers this turn dispatched (see {@link BackgroundDispatch}),
3667
+ * by `toolCallId`, recorded from the wire while the turn is still streaming —
3668
+ * typically in the first step, before the prompt response lands. A turn may
3669
+ * dispatch several, and the hold waits for ALL of them.
3670
+ */
3671
+ private readonly dispatches = new Map<string, BackgroundDispatch>()
3672
+ /** The detached-worker hold in progress, once a segment carries it. */
3673
+ private backgroundHold?: BackgroundHold
3674
+ /**
3675
+ * A hold parked because the prompt response landed while NO driver segment
3676
+ * was open (the host is between steps, about to execute the Agent card). It
3677
+ * begins when the next `startSegment` opens — or closes after a bounded
3678
+ * grace so a host that never opens one does not hang the turn.
3679
+ */
3680
+ private pendingBackgroundHold?: BackgroundHold
3681
+ /**
3682
+ * The hold's own windows, read once at construction like the ambient pair:
3683
+ * `taskMaxMs` is the worker phase's ceiling, `graceMs` the parked hold's
3684
+ * bounded wait for a driver step (see {@link BridgeTuning.backgroundTaskMaxMs}
3685
+ * and {@link BridgeTuning.backgroundParkedGraceMs}).
3686
+ */
3687
+ private readonly backgroundTuning: { taskMaxMs: number; graceMs: number }
3688
+ /**
3689
+ * When this controller was built. Only a drain announced AFTER this moment
3690
+ * may be adopted by the hold: an `ambient` record that predates the turn is
3691
+ * some earlier run's, and folding its text would mix two answers.
3692
+ */
3693
+ private readonly constructedAt: number
2966
3694
 
2967
3695
  constructor(
2968
3696
  private readonly manager: BridgeManager,
@@ -2975,6 +3703,10 @@ class TurnStreamController {
2975
3703
  ) {
2976
3704
  this.turnSeqAtStart = session.turnSeq
2977
3705
  this.cursorSeq = session.transcript.head
3706
+ this.tpsSeq = session.transcript.head
3707
+ this.constructedAt = Date.now()
3708
+ this.ambientTuning = manager.ambientTuning
3709
+ this.backgroundTuning = manager.backgroundTuning
2978
3710
  this.unsubscribe = hub.subscribe(event => this.onEvent(event))
2979
3711
  }
2980
3712
 
@@ -3010,6 +3742,12 @@ class TurnStreamController {
3010
3742
  * consuming it, so the next segment replays it again.
3011
3743
  */
3012
3744
  startSegment(): SegmentState {
3745
+ // A step opening on an already settled turn (the response landed a tick
3746
+ // ago and the tail drain is still scheduled) closes that turn FIRST: the
3747
+ // step must read a settled controller and the session must not stay
3748
+ // `running` across the macrotask boundary — the host's next prompt would be
3749
+ // refused as busy for no reason.
3750
+ this.flushScheduledClose()
3013
3751
  // A step opening is the HOST handing the turn back to the agent — this
3014
3752
  // runs for the first step and for each continuation after the host
3015
3753
  // executed a card. The watchdog measures the stretch the AGENT holds the
@@ -3025,13 +3763,31 @@ class TurnStreamController {
3025
3763
  outputChars: 0,
3026
3764
  activeToolCall: undefined,
3027
3765
  hasCard: false,
3766
+ // A segment opened DURING an ambient wait (never the steered first step,
3767
+ // which marks its own) inherits the suppression: the drain's tools still
3768
+ // stay transcript-only until the turn closes. A detached-worker hold
3769
+ // suppresses its cards for the same reason, and a PARKED hold must
3770
+ // suppress from the first replayed update on — the drain's TaskOutput
3771
+ // arrives in exactly that step.
3772
+ ambient: this.awaitingAmbient !== undefined
3773
+ || this.backgroundHold !== undefined
3774
+ || this.pendingBackgroundHold !== undefined,
3028
3775
  finished: false,
3029
3776
  }
3030
3777
  this.segment = seg
3031
3778
  const entries = this.session.transcript.since(this.cursorSeq)
3032
3779
  for (const entry of entries) {
3033
3780
  if (seg.finished) break
3034
- if (!this.foldInto(seg, entry.update as Record<string, unknown>)) break
3781
+ const update = entry.update as Record<string, unknown>
3782
+ // The same split the live path makes: a background drain's prose is not
3783
+ // this turn's answer, even when it landed while no step was open. The
3784
+ // record (tool outputs) still goes in; the cursor moves past it.
3785
+ if (this.isForeignAmbientText(update)) {
3786
+ this.recordWithoutSegment(update)
3787
+ this.cursorSeq = entry.seq
3788
+ continue
3789
+ }
3790
+ if (!this.foldInto(seg, update, entry.seq)) break
3035
3791
  this.cursorSeq = entry.seq
3036
3792
  }
3037
3793
  if (!seg.finished && this.pendingProse.length > 0) {
@@ -3040,6 +3796,12 @@ class TurnStreamController {
3040
3796
  const lines = this.pendingProse.splice(0, this.pendingProse.length).join('')
3041
3797
  this.emitProse(seg, 'reasoning', lines)
3042
3798
  }
3799
+ const parkedHold = this.pendingBackgroundHold
3800
+ if (parkedHold !== undefined && !seg.finished) {
3801
+ // The host opened the continuation step for the Agent card: the hold
3802
+ // finally has a driver step to speak the drain's answer into.
3803
+ this.adoptBackgroundHold(seg, parkedHold)
3804
+ }
3043
3805
  if (this.settled) this.finishAfterSettle(seg)
3044
3806
  return seg
3045
3807
  }
@@ -3049,10 +3811,745 @@ class TurnStreamController {
3049
3811
  if (this.segment !== undefined) this.finishSegment(this.segment, reason)
3050
3812
  }
3051
3813
 
3814
+ /** Whether this turn already streamed prose of its own (not a drain's). */
3815
+ get hasAgentProse(): boolean {
3816
+ return this.sawAgentProse
3817
+ }
3818
+
3819
+ /**
3820
+ * Keep the turn open for a background drain the agent folded our prompt into.
3821
+ *
3822
+ * THE SHAPE: the agent answered `session/prompt` at once (CodeBuddy returns
3823
+ * `end_turn` in ~14ms when it merges the message into a drain it is already
3824
+ * running) and the real answer streams afterwards under the DRAIN's request
3825
+ * id. Closing on the response would cut the turn before its text exists —
3826
+ * which is exactly the bug this state exists for. So:
3827
+ *
3828
+ * - the agent-side prompt slot is released (`endTurn`: the response HAS
3829
+ * landed) but the turn is NOT settled: `settled` means "the turn is over",
3830
+ * and it is not — no `turn/end`, no `running = false`, no segment close;
3831
+ * - the segment keeps folding the session's updates, with cards suppressed
3832
+ * (see `SegmentState.ambient`): the host must not execute a background
3833
+ * tool for a turn whose answer has not begun;
3834
+ * - any update on the session resets the quiet clock, and the drain's end is
3835
+ * its silence (a `session_end` is rejected by the SDK's schema and never
3836
+ * reaches us — never wait for one); a hard ceiling closes what never ends.
3837
+ */
3838
+ awaitAmbient(requestId: string, stopReason: string): void {
3839
+ if (this.settled || this.disposed || this.awaitingAmbient !== undefined) return
3840
+ const seg = this.segment
3841
+ if (seg === undefined || seg.finished) {
3842
+ // No live driver step to speak into: nothing can be folded, so close the
3843
+ // turn the ordinary way rather than holding state that renders nothing.
3844
+ this.clearPendingAmbient()
3845
+ this.settle({ kind: 'stop', stopReason })
3846
+ return
3847
+ }
3848
+ seg.ambient = true
3849
+ // The response landed: the agent no longer holds a prompt for this turn.
3850
+ this.endTurn()
3851
+ // The drain may have spoken BEFORE the response was judged. Those updates
3852
+ // were withheld while this turn's own request id was unknowable (see
3853
+ // `pendingAmbientUpdates`); the turn IS the drain, so they are its answer —
3854
+ // folded in arrival order, now that `seg.ambient` suppresses their cards.
3855
+ this.flushPendingAmbient(seg)
3856
+ if (seg.finished) {
3857
+ this.settle({ kind: 'stop', stopReason })
3858
+ return
3859
+ }
3860
+ const wait: AmbientWait = {
3861
+ requestId,
3862
+ quietTimer: setTimeout(() => { this.onAmbientQuiet(wait) }, this.ambientTuning.quietMs),
3863
+ hardTimer: setTimeout(() => { this.onAmbientHard(wait) }, this.ambientTuning.maxMs),
3864
+ stopReason,
3865
+ }
3866
+ wait.quietTimer.unref?.()
3867
+ wait.hardTimer.unref?.()
3868
+ this.awaitingAmbient = wait
3869
+ }
3870
+
3871
+ /**
3872
+ * The prompt response named no conversation turn while a drain the session
3873
+ * retired recently may still be running (and did not re-announce): the turn
3874
+ * cannot be held open, but a zero-output report must not blame the agent.
3875
+ * See `hasRecentRetiredAmbient` for the window.
3876
+ */
3877
+ noteRecentRetiredDrain(): void {
3878
+ this.recentRetiredDrain = true
3879
+ }
3880
+
3881
+ /**
3882
+ * Whether this turn dispatched a detached worker whose answer is still
3883
+ * outstanding. Read when the prompt response lands: such a turn is NOT over
3884
+ * even though the agent answered it (see {@link holdForBackgroundDispatch}).
3885
+ *
3886
+ * Two ways a dispatch keeps the turn open:
3887
+ * - it has not reported back yet (the worker is still running), or
3888
+ * - it DID report — before the response, even — but it was structurally
3889
+ * detached and emitted worker updates: the drain that carries its report
3890
+ * is still coming.
3891
+ * A synchronous `Agent` / `Task` call (completed, no `isBackground`, no
3892
+ * worker updates) matches neither and closes normally.
3893
+ */
3894
+ get dispatchedDetachedWorker(): boolean {
3895
+ for (const dispatch of this.dispatches.values()) {
3896
+ if (!dispatch.completed) return true
3897
+ if (dispatch.detached && dispatch.sawWorkerUpdate) return true
3898
+ }
3899
+ return false
3900
+ }
3901
+
3902
+ /** The hold this turn is inside — adopted, or parked waiting for a step. */
3903
+ private activeBackgroundHold(): BackgroundHold | undefined {
3904
+ return this.backgroundHold ?? this.pendingBackgroundHold
3905
+ }
3906
+
3907
+ /** Whether every KNOWN dispatch has reported (no dispatch at all counts as done). */
3908
+ private allDispatchesCompleted(): boolean {
3909
+ for (const dispatch of this.dispatches.values()) {
3910
+ if (!dispatch.completed) return false
3911
+ }
3912
+ return true
3913
+ }
3914
+
3915
+ /**
3916
+ * Hold the turn open for the detached worker(s) its `Agent` tool calls
3917
+ * dispatched — the second controlled ambient-family exception (constraint
3918
+ * 23), not a new idle watchdog.
3919
+ *
3920
+ * THE SHAPE (measured against CodeBuddy 2.179.0, multitask on): the
3921
+ * coordinator opens an `Agent` call that hands work to a background worker,
3922
+ * says "已派…", and the `session/prompt` RESPONSE arrives normally while the
3923
+ * worker keeps running. The worker's own updates (requestId B +
3924
+ * `parentToolCallId`) never touch this session's message; when it finishes,
3925
+ * the agent starts a background DRAIN (requestId A — the response's own
3926
+ * `conversationRequestId`) whose steps carry the report and the final answer.
3927
+ * Nothing tells the client the worker is running: no `session_end` (the SDK's
3928
+ * schema rejects it) and an `agentPhase` idle tick even fires BETWEEN the
3929
+ * worker and the drain. So the response cannot close the turn.
3930
+ *
3931
+ * The hold:
3932
+ * - releases the agent-side prompt slot (`endTurn`: the response landed)
3933
+ * while keeping `running` and the driver step open;
3934
+ * - marks the segment background, so EVERY tool call for the rest of the
3935
+ * hold — the workers', the drain's TaskOutput — stays transcript-only and
3936
+ * the final message carries text with no card in front of it;
3937
+ * - waits in two phases: the worker phase only has the
3938
+ * {@link BridgeTuning.backgroundTaskMaxMs} ceiling (a detached worker goes
3939
+ * silent for tens of seconds while it works) and ends only when ALL known
3940
+ * dispatches reported, after which the drain phase uses the ambient
3941
+ * windows.
3942
+ */
3943
+ holdForBackgroundDispatch(stopReason: string, requestId?: string): void {
3944
+ if (this.settled || this.disposed || this.closedTurn || this.stale) return
3945
+ if (this.activeBackgroundHold() !== undefined) return
3946
+ const hold: BackgroundHold = {
3947
+ stopReason,
3948
+ requestId,
3949
+ acceptedIds: new Set<string>(),
3950
+ phase: 'worker',
3951
+ answerChars: 0,
3952
+ floorSeq: this.session.transcript.head,
3953
+ retired: false,
3954
+ }
3955
+ if (requestId !== undefined) hold.acceptedIds.add(requestId)
3956
+ // A drain announced while this turn was ALREADY on air belongs to it (the
3957
+ // announcement may have named a different id than the response). One that
3958
+ // predates the turn is some earlier run's, and folding its text would mix
3959
+ // two answers.
3960
+ const announced = this.session.ambient
3961
+ if (announced !== undefined && announced.startedAt >= this.constructedAt) {
3962
+ hold.acceptedIds.add(announced.requestId)
3963
+ if (hold.requestId === undefined) hold.requestId = announced.requestId
3964
+ }
3965
+ // The response landed: the agent no longer holds a prompt for this turn.
3966
+ this.endTurn()
3967
+ hold.hardTimer = setTimeout(() => { this.onBackgroundHard(hold) }, this.backgroundTuning.taskMaxMs)
3968
+ hold.hardTimer.unref?.()
3969
+ const seg = this.segment
3970
+ if (seg !== undefined && !seg.finished) {
3971
+ this.adoptBackgroundHold(seg, hold)
3972
+ } else {
3973
+ // The response landed BETWEEN driver steps: the host is about to execute
3974
+ // the Agent card and open its continuation step. Park the hold for that
3975
+ // step, with a bounded wait — a host that never opens one must not hang
3976
+ // the turn forever.
3977
+ hold.graceTimer = setTimeout(() => { this.onBackgroundGraceExpired(hold) }, this.backgroundTuning.graceMs)
3978
+ hold.graceTimer.unref?.()
3979
+ this.pendingBackgroundHold = hold
3980
+ }
3981
+ // A FAST detached worker may already have reported (even before the
3982
+ // response): with every known dispatch complete there is no worker phase
3983
+ // left to wait out, so the drain phase starts now.
3984
+ if (this.allDispatchesCompleted()) this.startBackgroundDrain(hold)
3985
+ }
3986
+
3987
+ /** Give a (possibly parked) hold a driver segment: cards off for the rest of it. */
3988
+ private adoptBackgroundHold(seg: SegmentState, hold: BackgroundHold): void {
3989
+ if (hold.graceTimer !== undefined) {
3990
+ clearTimeout(hold.graceTimer)
3991
+ hold.graceTimer = undefined
3992
+ }
3993
+ if (this.pendingBackgroundHold === hold) this.pendingBackgroundHold = undefined
3994
+ seg.ambient = true
3995
+ this.backgroundHold = hold
3996
+ // The drain may have spoken BEFORE the response was judged. Those updates
3997
+ // were withheld while this turn's own id was unknown
3998
+ // (`pendingAmbientUpdates`); only the runs THIS hold owns are its answer —
3999
+ // another run's withheld text is dropped (the transcript keeps it).
4000
+ this.flushBackgroundHoldUpdates(seg, hold)
4001
+ // The parked grace owns the wait while no step is on air; now that one is,
4002
+ // a hold whose drain phase already began arms its quiet clock.
4003
+ if (hold.phase === 'drain') this.armBackgroundQuiet(hold)
4004
+ }
4005
+
4006
+ /**
4007
+ * The worker phase is over once EVERY known dispatch reported: the drain
4008
+ * that carries their reports is (about to be) running, so the existing
4009
+ * ambient windows take over, measured from HERE.
4010
+ *
4011
+ * An announced run is accepted immediately, but while any dispatch is still
4012
+ * incomplete it only records the id and switches the hold's primary run —
4013
+ * arming the quiet clock there would let a drain that spoke early cut the
4014
+ * turn off before its other worker ever reports.
4015
+ */
4016
+ private startBackgroundDrain(hold: BackgroundHold, requestId?: string): void {
4017
+ if (hold !== this.backgroundHold && hold !== this.pendingBackgroundHold) return
4018
+ if (requestId !== undefined) {
4019
+ hold.acceptedIds.add(requestId)
4020
+ hold.requestId = requestId
4021
+ }
4022
+ if (!this.allDispatchesCompleted()) return
4023
+ if (hold.phase === 'drain') {
4024
+ if (this.backgroundHold === hold) this.armBackgroundQuiet(hold)
4025
+ return
4026
+ }
4027
+ hold.phase = 'drain'
4028
+ if (hold.hardTimer !== undefined) clearTimeout(hold.hardTimer)
4029
+ hold.hardTimer = setTimeout(() => { this.onBackgroundHard(hold) }, this.ambientTuning.maxMs)
4030
+ hold.hardTimer.unref?.()
4031
+ // A parked hold belongs to the grace until a driver step opens; a hold
4032
+ // with a step arms its quiet clock now.
4033
+ if (this.backgroundHold === hold) this.armBackgroundQuiet(hold)
4034
+ }
4035
+
4036
+ /**
4037
+ * A dispatch that lands while the drain phase already began means the
4038
+ * coordinator handed out NEW work: the worker phase is back, and with it the
4039
+ * absence of a quiet window.
4040
+ */
4041
+ private enterBackgroundWorkerPhase(hold: BackgroundHold): void {
4042
+ if (hold.phase === 'worker') return
4043
+ hold.phase = 'worker'
4044
+ if (hold.quietTimer !== undefined) {
4045
+ clearTimeout(hold.quietTimer)
4046
+ hold.quietTimer = undefined
4047
+ }
4048
+ if (hold.hardTimer !== undefined) clearTimeout(hold.hardTimer)
4049
+ hold.hardTimer = setTimeout(() => { this.onBackgroundHard(hold) }, this.backgroundTuning.taskMaxMs)
4050
+ hold.hardTimer.unref?.()
4051
+ }
4052
+
4053
+ /** Push the drain phase's quiet clock out: every update is evidence of work. */
4054
+ private armBackgroundQuiet(hold: BackgroundHold): void {
4055
+ if (hold.quietTimer !== undefined) clearTimeout(hold.quietTimer)
4056
+ hold.quietTimer = setTimeout(() => { this.onBackgroundQuiet(hold) }, this.ambientTuning.quietMs)
4057
+ hold.quietTimer.unref?.()
4058
+ }
4059
+
4060
+ /**
4061
+ * Any session update restarts the drain phase's quiet clock. Only an
4062
+ * ADOPTED hold has one: a parked hold's wait belongs to its grace timer, and
4063
+ * the worker phase never has a clock at all.
4064
+ */
4065
+ private noteBackgroundHoldActivity(): void {
4066
+ const hold = this.backgroundHold
4067
+ if (hold === undefined || hold.phase !== 'drain') return
4068
+ this.armBackgroundQuiet(hold)
4069
+ }
4070
+
4071
+ /** The drained run went quiet: the turn ends with the stop reason the response gave. */
4072
+ private onBackgroundQuiet(hold: BackgroundHold): void {
4073
+ if (this.backgroundHold !== hold || hold.phase !== 'drain') return
4074
+ this.closeBackgroundHold(hold, { hard: false })
4075
+ }
4076
+
4077
+ /** The hold outlived its ceiling (worker or drain): the run is still going. */
4078
+ private onBackgroundHard(hold: BackgroundHold): void {
4079
+ if (hold !== this.backgroundHold && hold !== this.pendingBackgroundHold) return
4080
+ this.closeBackgroundHold(hold, { hard: true })
4081
+ }
4082
+
4083
+ /**
4084
+ * No driver step opened inside the grace: the host is not coming back for
4085
+ * this turn's Agent card. Close it the way a quiet ambient ending does — the
4086
+ * deferral line, never the zero-output blame — and keep ONE drain record:
4087
+ * the worker may still report into a run no turn is waiting on.
4088
+ *
4089
+ * A parked hold has NO segment by definition, so the chat plane has nowhere
4090
+ * to put the deferral line: it goes to the settings log (`agent/notice`)
4091
+ * instead of being parked on prose nobody will read.
4092
+ */
4093
+ private onBackgroundGraceExpired(hold: BackgroundHold): void {
4094
+ if (this.pendingBackgroundHold !== hold) return
4095
+ this.pendingBackgroundHold = undefined
4096
+ this.clearBackgroundHoldTimers(hold)
4097
+ const line = hold.answerChars > 0 ? undefined : backgroundDeferralLine(this.session.agentId)
4098
+ this.releaseBackgroundHoldIds(hold, { hard: true })
4099
+ if (line !== undefined) this.broadcastBackgroundDeferral(line)
4100
+ this.closeTurn({ kind: 'stop', stopReason: hold.stopReason }, { line, keepAmbient: true, requestId: hold.requestId })
4101
+ }
4102
+
4103
+ /**
4104
+ * Close a detached-worker hold exactly once. A turn that folded NO answer
4105
+ * text from its held runs says where the rest of the output will land (the
4106
+ * deferral wording, never the silent-failure blame); the parent's interim
4107
+ * acknowledgement does not count as that answer.
4108
+ *
4109
+ * Ownership of the drain ids at close (`releaseBackgroundHoldIds`): a quiet
4110
+ * close retires every accepted id and drops the session record; a hard or
4111
+ * grace close keeps exactly ONE record, because the run is provably still
4112
+ * going and a later update or re-announcement must revive it normally.
4113
+ */
4114
+ private closeBackgroundHold(hold: BackgroundHold, options: { hard: boolean }): void {
4115
+ if (this.pendingBackgroundHold === hold) this.pendingBackgroundHold = undefined
4116
+ else if (this.backgroundHold === hold) this.backgroundHold = undefined
4117
+ else return
4118
+ this.clearBackgroundHoldTimers(hold)
4119
+ const line = hold.answerChars > 0 ? undefined : backgroundDeferralLine(this.session.agentId)
4120
+ this.releaseBackgroundHoldIds(hold, { hard: options.hard })
4121
+ const onAir = this.segment !== undefined && !this.segment.finished
4122
+ // With no segment on air the closing line has no chat bubble to land in:
4123
+ // log it, so the fact is recorded rather than silently dropped.
4124
+ if (line !== undefined && !onAir) this.broadcastBackgroundDeferral(line)
4125
+ this.closeTurn(
4126
+ { kind: 'stop', stopReason: hold.stopReason },
4127
+ { line, keepAmbient: options.hard, requestId: hold.requestId },
4128
+ )
4129
+ }
4130
+
4131
+ /** Write a hold's deferral line into the settings log (`agent/notice`). */
4132
+ private broadcastBackgroundDeferral(line: string): void {
4133
+ this.hub.broadcast({
4134
+ type: 'agent/notice',
4135
+ agentId: this.session.agentId,
4136
+ level: 'info',
4137
+ text: line.trim(),
4138
+ })
4139
+ }
4140
+
4141
+ /** Drop a hold's clocks. The hold object itself stays for its `retired` mark. */
4142
+ private clearBackgroundHoldTimers(hold: BackgroundHold): void {
4143
+ if (hold.quietTimer !== undefined) clearTimeout(hold.quietTimer)
4144
+ if (hold.hardTimer !== undefined) clearTimeout(hold.hardTimer)
4145
+ if (hold.graceTimer !== undefined) clearTimeout(hold.graceTimer)
4146
+ hold.quietTimer = undefined
4147
+ hold.hardTimer = undefined
4148
+ hold.graceTimer = undefined
4149
+ }
4150
+
4151
+ /**
4152
+ * Drop every hold clock (teardown / a forced ending). `retire` also gives up
4153
+ * every accepted run id — a SUCCESSOR turn must never fold the abandoned
4154
+ * hold's drain or worker text into its own answer.
4155
+ */
4156
+ private clearBackgroundHold(options?: { retire?: boolean }): void {
4157
+ const pending = this.pendingBackgroundHold
4158
+ if (pending !== undefined) {
4159
+ this.pendingBackgroundHold = undefined
4160
+ this.clearBackgroundHoldTimers(pending)
4161
+ if (options?.retire === true) this.releaseBackgroundHoldIds(pending, { hard: false })
4162
+ }
4163
+ const hold = this.backgroundHold
4164
+ if (hold === undefined) return
4165
+ this.backgroundHold = undefined
4166
+ this.clearBackgroundHoldTimers(hold)
4167
+ if (options?.retire === true) this.releaseBackgroundHoldIds(hold, { hard: false })
4168
+ }
4169
+
4170
+ /**
4171
+ * Settle what happens to the session's drain records when a hold closes.
4172
+ *
4173
+ * `hard: false` (quiet close, cancel, teardown): the run is no longer waited
4174
+ * on, so EVERY accepted id is retired — its late text stays transcript-only
4175
+ * for whatever turn runs next — and the session record is dropped if it
4176
+ * names one of them.
4177
+ *
4178
+ * `hard: true` (ceiling / parked grace): the run is provably still going.
4179
+ * Exactly ONE record survives — the session's current drain if it has one,
4180
+ * else the hold's primary run — with its activity clock refreshed; the other
4181
+ * accepted ids are retired. A hold whose primary id had no session record
4182
+ * CREATES one, so a successor's `noteTurnRequestId` cannot adopt that id.
4183
+ * Every accepted id therefore ends up either retired or owned by
4184
+ * `session.ambient`; never both unowned and unretired.
4185
+ */
4186
+ private releaseBackgroundHoldIds(hold: BackgroundHold, options: { hard: boolean }): void {
4187
+ if (hold.retired) return
4188
+ hold.retired = true
4189
+ if (!options.hard) {
4190
+ for (const id of hold.acceptedIds) retireAmbientId(this.session, id)
4191
+ if (this.session.ambient !== undefined && hold.acceptedIds.has(this.session.ambient.requestId)) {
4192
+ this.session.ambient = undefined
4193
+ }
4194
+ return
4195
+ }
4196
+ const survivor = this.session.ambient?.requestId ?? hold.requestId
4197
+ for (const id of hold.acceptedIds) {
4198
+ if (id === survivor) continue
4199
+ retireAmbientId(this.session, id)
4200
+ }
4201
+ if (survivor === undefined) return
4202
+ const now = Date.now()
4203
+ if (this.session.ambient?.requestId === survivor) {
4204
+ this.session.ambient.lastActivityAt = now
4205
+ } else {
4206
+ this.session.ambient = {
4207
+ requestId: survivor,
4208
+ startedAt: now,
4209
+ lastActivityAt: now,
4210
+ sawOutput: hold.answerChars > 0,
4211
+ }
4212
+ }
4213
+ // The surviving run is alive, not retired (an earlier wait of this turn
4214
+ // may have retired it before it spoke again).
4215
+ reviveAmbientId(this.session, survivor)
4216
+ }
4217
+
4218
+ /**
4219
+ * Fold the withheld drain updates that belong to this hold into its segment.
4220
+ * The rest of the buffer was some OTHER run's text: it is dropped here (the
4221
+ * transcript keeps it), never leaked into this turn's answer.
4222
+ */
4223
+ private flushBackgroundHoldUpdates(seg: SegmentState, hold: BackgroundHold): void {
4224
+ if (this.pendingAmbientUpdates.length === 0) {
4225
+ this.pendingAmbientTruncated = false
4226
+ return
4227
+ }
4228
+ const pending = this.pendingAmbientUpdates.splice(0, this.pendingAmbientUpdates.length)
4229
+ this.pendingAmbientChars = 0
4230
+ const truncated = this.pendingAmbientTruncated
4231
+ this.pendingAmbientTruncated = false
4232
+ const accepted = pending.filter(entry => {
4233
+ const requestId = codebuddyMetaOf(entry.update)?.requestId
4234
+ return requestId !== undefined && hold.acceptedIds.has(requestId)
4235
+ })
4236
+ if (accepted.length === 0) return
4237
+ if (truncated && !seg.finished) {
4238
+ // The front of the drain's output was evicted from the bounded buffer:
4239
+ // say so rather than letting the turn quietly start mid-story.
4240
+ this.emitProse(seg, 'text', '\n[dsh-ntr] background output was truncated; the ACP transcript view still has every update.\n')
4241
+ }
4242
+ for (const entry of accepted) {
4243
+ if (seg.finished) break
4244
+ this.foldInto(seg, entry.update, entry.seq)
4245
+ // The buffered drain text arrived BEFORE the hold, but it IS the held
4246
+ // run's answer: below the hold's floor it was not counted by `foldInto`,
4247
+ // so count it here (the floor exists to skip the parent's earlier ack,
4248
+ // not the drain's own words).
4249
+ if (entry.seq <= hold.floorSeq
4250
+ && String(entry.update.sessionUpdate ?? '') === 'agent_message_chunk') {
4251
+ hold.answerChars += chunkTextOf(entry.update.content).length
4252
+ }
4253
+ }
4254
+ }
4255
+
4256
+ /**
4257
+ * Follow the detached-worker signals of one live update.
4258
+ *
4259
+ * Only CodeBuddy sends them (`codebuddyMetaOf` answers `undefined` for every
4260
+ * other agent, so nothing here can fire for them):
4261
+ * - an `Agent` / `Task` `tool_call` records the dispatch — the FIRST send
4262
+ * needs that CodeBuddy shape;
4263
+ * - the SAME call re-sent with `isBackground: true`, or any update naming it
4264
+ * as its `parentToolCallId`, marks the work DETACHED;
4265
+ * - that call reporting completed / failed is read BY ID: the real
4266
+ * completion frame carries NO `_meta` at all, so requiring the CodeBuddy
4267
+ * shape here would miss every real completion;
4268
+ * - any update carrying `parentToolCallId` is the worker's OWN turn — it
4269
+ * proves the worker alive but never belongs to this session's answer;
4270
+ * - an `unsolicitedTurn` announcement is the drain that carries the report.
4271
+ */
4272
+ private trackBackgroundDispatch(update: Record<string, unknown>): void {
4273
+ if (this.closedTurn) return
4274
+ const meta = codebuddyMetaOf(update)
4275
+ const kind = String(update.sessionUpdate ?? '')
4276
+ const row = update as { toolCallId?: unknown; status?: unknown }
4277
+ const toolCallId = typeof row.toolCallId === 'string' ? row.toolCallId.trim() : ''
4278
+ const status = typeof row.status === 'string' ? row.status : ''
4279
+ const completedStatus = status === 'completed' || status === 'failed'
4280
+ if ((kind === 'tool_call' || kind === 'tool_call_update') && toolCallId !== '') {
4281
+ const known = this.dispatches.get(toolCallId)
4282
+ if (known !== undefined) {
4283
+ // A known dispatch: the id is the whole evidence (see the doc above).
4284
+ if (status !== '') known.status = status
4285
+ if (meta?.isBackground === true) known.detached = true
4286
+ if (completedStatus && !known.completed) {
4287
+ known.completed = true
4288
+ this.noteDispatchProgress()
4289
+ }
4290
+ } else if (meta !== undefined
4291
+ && (meta.toolName === 'Agent' || meta.toolName === 'Task')
4292
+ && (kind === 'tool_call' || meta.isBackground === true)) {
4293
+ // A NEW dispatch needs the CodeBuddy shape. A turn may dispatch more
4294
+ // than one worker; the hold waits for all of them.
4295
+ const dispatch: BackgroundDispatch = {
4296
+ toolCallId,
4297
+ status,
4298
+ completed: completedStatus,
4299
+ detached: meta.isBackground === true,
4300
+ sawWorkerUpdate: false,
4301
+ }
4302
+ this.dispatches.set(toolCallId, dispatch)
4303
+ if (!dispatch.completed) {
4304
+ const held = this.activeBackgroundHold()
4305
+ // A dispatch registered after the drain phase began means new work:
4306
+ // the worker phase (and its absence of a quiet clock) is back.
4307
+ if (held !== undefined) this.enterBackgroundWorkerPhase(held)
4308
+ }
4309
+ }
4310
+ }
4311
+ if (meta?.parentToolCallId !== undefined) {
4312
+ const parent = this.dispatches.get(meta.parentToolCallId)
4313
+ if (parent !== undefined) {
4314
+ parent.detached = true
4315
+ parent.sawWorkerUpdate = true
4316
+ }
4317
+ }
4318
+ if (meta?.unsolicitedTurn !== undefined) {
4319
+ const held = this.activeBackgroundHold()
4320
+ if (held !== undefined) this.startBackgroundDrain(held, meta.unsolicitedTurn.requestId)
4321
+ }
4322
+ }
4323
+
4324
+ /**
4325
+ * A dispatch just reported: once the LAST one has, the drain phase starts.
4326
+ * Until then the turn stays in its worker phase, with no quiet clock.
4327
+ */
4328
+ private noteDispatchProgress(): void {
4329
+ if (!this.allDispatchesCompleted()) return
4330
+ const held = this.activeBackgroundHold()
4331
+ if (held !== undefined) this.startBackgroundDrain(held)
4332
+ }
4333
+
4334
+ /** Any session update is evidence the drain still works: push the quiet clock out. */
4335
+ private noteAmbientActivity(): void {
4336
+ const wait = this.awaitingAmbient
4337
+ if (wait === undefined) return
4338
+ clearTimeout(wait.quietTimer)
4339
+ wait.quietTimer = setTimeout(() => { this.onAmbientQuiet(wait) }, this.ambientTuning.quietMs)
4340
+ wait.quietTimer.unref?.()
4341
+ }
4342
+
4343
+ /** The drain went quiet: the turn ends with the stop reason the response gave. */
4344
+ private onAmbientQuiet(wait: AmbientWait): void {
4345
+ if (this.awaitingAmbient !== wait) return
4346
+ this.finishAmbient(wait)
4347
+ }
4348
+
4349
+ /** The drain outlived the ceiling: say where the rest will land, then close. */
4350
+ private onAmbientHard(wait: AmbientWait): void {
4351
+ if (this.awaitingAmbient !== wait) return
4352
+ const seg = this.segment
4353
+ if (seg !== undefined && !seg.finished) {
4354
+ this.emitProse(seg, 'text', `\n[dsh-ntr] agent "${this.session.agentId}" is still running a background task; the rest of its output will appear in the ACP transcript view.\n`)
4355
+ }
4356
+ // The ceiling is evidence the drain is STILL RUNNING (unlike a quiet close),
4357
+ // so the ending keeps the session's drain record; see `closeTurn`.
4358
+ wait.hard = true
4359
+ // The hard explanation already says the turn was cut short; the deferred
4360
+ // line would only repeat it in different words.
4361
+ this.finishAmbient(wait, { announceDeferral: false })
4362
+ }
4363
+
4364
+ /**
4365
+ * Close an ambient wait exactly once. A turn that produced no answer TEXT
4366
+ * says so: the drain's output is in the ACP transcript view, and this
4367
+ * bubble must not be empty (the zero-output report, reworded for a turn that
4368
+ * was deferred rather than failed).
4369
+ */
4370
+ private finishAmbient(wait: AmbientWait, options?: { announceDeferral?: boolean }): void {
4371
+ if (this.awaitingAmbient !== wait) return
4372
+ const line = options?.announceDeferral === false || this.sawVisibleText
4373
+ ? undefined
4374
+ : backgroundDeferralLine(this.session.agentId)
4375
+ this.closeTurn(
4376
+ { kind: 'stop', stopReason: wait.stopReason },
4377
+ { line, keepAmbient: wait.hard === true, requestId: wait.requestId },
4378
+ )
4379
+ }
4380
+
4381
+ /** Drop the ambient wait's clocks (the turn is being closed by someone else). */
4382
+ private clearAmbientWait(): void {
4383
+ const wait = this.awaitingAmbient
4384
+ if (wait === undefined) return
4385
+ this.awaitingAmbient = undefined
4386
+ clearTimeout(wait.quietTimer)
4387
+ clearTimeout(wait.hardTimer)
4388
+ }
4389
+
4390
+ /**
4391
+ * Withhold one background-drain update until the prompt response says what
4392
+ * this turn is. The cursor advances past it anyway: it is already in the
4393
+ * transcript, and replaying it into a later segment would emit it twice.
4394
+ * Bounded (see `PENDING_AMBIENT_MAX_*`) — the transcript still keeps every
4395
+ * entry the buffer drops.
4396
+ */
4397
+ private bufferAmbientUpdate(seq: number, update: Record<string, unknown>): void {
4398
+ const chars = JSON.stringify(update).length
4399
+ this.pendingAmbientUpdates.push({ seq, update, chars })
4400
+ this.pendingAmbientChars += chars
4401
+ while (this.pendingAmbientUpdates.length > PENDING_AMBIENT_MAX_ENTRIES
4402
+ || (this.pendingAmbientChars > PENDING_AMBIENT_MAX_CHARS && this.pendingAmbientUpdates.length > 1)) {
4403
+ const dropped = this.pendingAmbientUpdates.shift()
4404
+ if (dropped === undefined) break
4405
+ this.pendingAmbientChars -= dropped.chars
4406
+ this.pendingAmbientTruncated = true
4407
+ }
4408
+ }
4409
+
4410
+ /** Fold the withheld drain updates into the now-ambient segment, once, in order. */
4411
+ private flushPendingAmbient(seg: SegmentState): void {
4412
+ if (this.pendingAmbientUpdates.length === 0) {
4413
+ this.pendingAmbientTruncated = false
4414
+ return
4415
+ }
4416
+ const truncated = this.pendingAmbientTruncated
4417
+ this.pendingAmbientTruncated = false
4418
+ const pending = this.pendingAmbientUpdates.splice(0, this.pendingAmbientUpdates.length)
4419
+ this.pendingAmbientChars = 0
4420
+ if (truncated && !seg.finished) {
4421
+ // The front of the drain's output was evicted from the bounded buffer:
4422
+ // say so rather than letting the turn quietly start mid-story.
4423
+ this.emitProse(seg, 'text', '\n[dsh-ntr] background output was truncated; the ACP transcript view still has every update.\n')
4424
+ }
4425
+ for (const entry of pending) {
4426
+ if (seg.finished) break
4427
+ this.foldInto(seg, entry.update, entry.seq)
4428
+ }
4429
+ }
4430
+
4431
+ /** Drop withheld drain updates: this turn is not the drain, or it is closing. */
4432
+ private clearPendingAmbient(): void {
4433
+ this.pendingAmbientUpdates.length = 0
4434
+ this.pendingAmbientChars = 0
4435
+ this.pendingAmbientTruncated = false
4436
+ }
4437
+
3052
4438
  /** The agent settled its `session/prompt`. Ends the turn's streaming face. */
3053
4439
  settle(outcome: TurnSettle): void {
3054
4440
  if (this.settled) return
4441
+ // `settled` is claimed NOW (a second settle, a cancel or a watchdog that
4442
+ // races the tail drain must find the turn already spoken for), but the
4443
+ // CLOSING work waits one macrotask: the SDK dispatches `session/update`
4444
+ // notifications independently of the `session/prompt` RESPONSE, so the
4445
+ // agent's last chunks can still be queued when this lands (see
4446
+ // `settleTranscript`). Only what already reached the transcript is read —
4447
+ // one macrotask, never an open-ended wait.
3055
4448
  this.settled = true
4449
+ this.clearAmbientWait()
4450
+ this.clearPendingAmbient()
4451
+ // An outside ending (watchdog, cancel) abandons a detached-worker hold: its
4452
+ // run is no longer this turn's, so its id is retired before the close.
4453
+ this.clearBackgroundHold({ retire: true })
4454
+ this.pendingOutcome = outcome
4455
+ this.closeScheduled = true
4456
+ setImmediate(() => { this.flushScheduledClose() })
4457
+ }
4458
+
4459
+ /**
4460
+ * Force the local end of this turn — the cancel fallback and any watchdog
4461
+ * that reached the controller. Runs the SAME closing work as a normal settle
4462
+ * (`closeTurn`): the ambient clocks stop, the on-air segment closes (so the
4463
+ * driver step parked on its queue ends), exactly one `turn/end` goes out
4464
+ * with THIS turn's id, the session stops running and the prompt slot is
4465
+ * released. Idempotent: a turn that already closed ignores the call.
4466
+ */
4467
+ forceFinalize(outcome: TurnSettle): void {
4468
+ if (this.stale || this.closedTurn) return
4469
+ this.clearAmbientWait()
4470
+ // A forced ending abandons a detached-worker hold: retire its run's id so
4471
+ // its late words stay transcript-only for the next turn.
4472
+ this.clearBackgroundHold({ retire: true })
4473
+ // A settle's deferred tail read is moot once this close runs: the turn is
4474
+ // over now, and the pending macrotask must not drain anything into it.
4475
+ this.pendingOutcome = undefined
4476
+ this.closeScheduled = false
4477
+ this.closeTurn(outcome)
4478
+ }
4479
+
4480
+ /**
4481
+ * Run the deferred closing work now, when a driver step is about to open on
4482
+ * an already settled turn: the continuation step must see the closed turn
4483
+ * (and the session NOT left `running` across the macrotask boundary).
4484
+ * Idempotent — the scheduled tick then finds nothing left to do.
4485
+ */
4486
+ private flushScheduledClose(): void {
4487
+ if (!this.closeScheduled) return
4488
+ this.closeScheduled = false
4489
+ const outcome = this.pendingOutcome
4490
+ this.pendingOutcome = undefined
4491
+ if (outcome === undefined) return
4492
+ // A DISPOSE that raced this queued close cleared it (see `dispose`), so a
4493
+ // close still scheduled here belongs to a settle that landed AFTER the
4494
+ // stream was retired (a crash rejects its prompt only after the teardown).
4495
+ // That turn still ends through its own queue and real id — the teardown's
4496
+ // releaseAll frame carried an empty id.
4497
+ this.drainTail()
4498
+ this.closeTurn(outcome)
4499
+ }
4500
+
4501
+ /**
4502
+ * Fold the notifications that reached the transcript but not the segment:
4503
+ * the tail a response raced past. Stops AT a cutting event without consuming
4504
+ * it (same rule as `startSegment`) — its next segment is not this turn's.
4505
+ */
4506
+ private drainTail(): void {
4507
+ // The deferred read belongs to the turn that scheduled it. If the turn was
4508
+ // retired or a NEWER turn already began (turnSeq moved on) in this one
4509
+ // macrotask window, everything past the cursor is the successor's: folding
4510
+ // it here would leak its text into this turn's driver step.
4511
+ if (this.stale) return
4512
+ const seg = this.segment
4513
+ if (seg === undefined || seg.finished) return
4514
+ for (const entry of this.session.transcript.since(this.cursorSeq)) {
4515
+ if (seg.finished) break
4516
+ const update = entry.update as Record<string, unknown>
4517
+ // The same split the live path makes: a background drain's text in the
4518
+ // tail is not this turn's answer. Record what the registry needs (tool
4519
+ // outputs), move the cursor past it, and do NOT fold it — otherwise the
4520
+ // deferred read smuggles the drain's prose into a normal turn.
4521
+ if (this.isForeignAmbientText(update)) {
4522
+ this.recordWithoutSegment(update)
4523
+ this.cursorSeq = entry.seq
4524
+ continue
4525
+ }
4526
+ if (!this.foldInto(seg, update, entry.seq)) break
4527
+ this.cursorSeq = entry.seq
4528
+ }
4529
+ }
4530
+
4531
+ /**
4532
+ * End the turn exactly once: burst metrics, ONE `turn/end`, the provisional
4533
+ * lock, the closing text, the segment, the session flags, the prompt slot.
4534
+ *
4535
+ * `ambient.line` marks a controlled ambient ending: the given line closes
4536
+ * the segment when the turn carried no answer text, instead of the generic
4537
+ * zero-output warning (which would blame the agent for a turn the drain
4538
+ * simply had not finished yet). `ambient.keepAmbient` says the ending was the
4539
+ * CEILING, not silence: see the drain-record comment below.
4540
+ */
4541
+ private closeTurn(outcome: TurnSettle, ambient?: { line?: string; keepAmbient?: boolean; requestId?: string }): void {
4542
+ if (this.closedTurn) return
4543
+ this.closedTurn = true
4544
+ // A closed turn is a settled turn, whichever path got here first (an
4545
+ // ambient ending does not go through `settle`).
4546
+ this.settled = true
4547
+ this.clearAmbientWait()
4548
+ this.clearPendingAmbient()
4549
+ // Whoever closed this turn already decided what happens to the held
4550
+ // run's record (`closeBackgroundHold` retired or kept it); only the clocks
4551
+ // are still ours to drop.
4552
+ this.clearBackgroundHold()
3056
4553
  // A late answer to a SUPERSEDED turn still has to close this controller's
3057
4554
  // queue (a continuation step may be parked on it) and its settle gate —
3058
4555
  // but it must not touch the session the NEWER turn now owns, nor release
@@ -3064,6 +4561,38 @@ class TurnStreamController {
3064
4561
  ? { kind: 'error', failure: { message: outcome.message, code: outcome.code ?? 'INTERNAL' } }
3065
4562
  : { kind: 'stop' }
3066
4563
  if (current) {
4564
+ // An ambient ending that came from SILENCE retires the drain record with
4565
+ // its turn: a record that outlives the wait would make the NEXT instant
4566
+ // answer with no conversation request id look steered (see
4567
+ // `awaitAmbient`), holding a normal turn open for a drain this one
4568
+ // already finished waiting on. A HARD ceiling ending is the opposite
4569
+ // evidence — the drain was still ticking when the turn was cut — so the
4570
+ // record SURVIVES (activity clock refreshed; `ambientStaleMs` expires it
4571
+ // lazily) precisely so the next prompt folded into the same drain is
4572
+ // still recognised as steered. `ambientNotices` is never cleared here:
4573
+ // a repeated announcement of the same run must stay silent, and the set
4574
+ // is bounded where entries are added.
4575
+ if (ambient !== undefined) {
4576
+ if (ambient.keepAmbient === true) {
4577
+ if (this.session.ambient !== undefined) this.session.ambient.lastActivityAt = Date.now()
4578
+ } else {
4579
+ // Silence closed this wait, but silence is not proof the run ended: a
4580
+ // queued message or a long tool can make the drain speak again. So
4581
+ // RETIRE its id before dropping the record — a revived run's late
4582
+ // text must stay in the ACP transcript view instead of teaching the
4583
+ // NEXT turn the drain's id and mixing the drain's answer into the
4584
+ // user's (a double answer). The trade-off is deliberate: a next
4585
+ // prompt the agent folds into that same revived drain may not receive
4586
+ // the drain's text on the chat plane (transcript-only); a double
4587
+ // answer is worse.
4588
+ if (ambient.requestId !== undefined) retireAmbientId(this.session, ambient.requestId)
4589
+ this.session.ambient = undefined
4590
+ }
4591
+ }
4592
+ // The turn's burst aggregate must be visible before the turn is marked
4593
+ // over: the final `turn/metrics` lands first, then `turn/end` flips the
4594
+ // session/pill state. Persisting and broadcasting share one value.
4595
+ this.finalizeTps()
3067
4596
  if (outcome.kind === 'cancelled') {
3068
4597
  this.hub.broadcast({ type: 'turn/end', sessionId: this.session.sessionId, turnId: this.turnId, stopReason: 'cancelled' })
3069
4598
  } else if (outcome.kind === 'error') {
@@ -3073,19 +4602,28 @@ class TurnStreamController {
3073
4602
  }
3074
4603
  // The provisional lock settles BEFORE any synthesized warning below: the
3075
4604
  // warning is our text, not evidence that the first turn delivered anything.
4605
+ //
4606
+ // An AMBIENT ending ends the provisional phase WITHOUT a rollback: a
4607
+ // deferral to a background drain is not a failed first turn, the drain's
4608
+ // text still lands in the ACP transcript view, and rolling the lock back
4609
+ // would close the session under it — dropping exactly the output the
4610
+ // deferred turn promised. The lock therefore becomes permanent.
3076
4611
  if (this.session.provisionalLock === true) {
3077
4612
  this.session.provisionalLock = false
3078
- if (!this.sawVisibleBlock) this.rollbackFirstTurn(this.session)
4613
+ if (!this.sawVisibleBlock && ambient === undefined) this.rollbackFirstTurn(this.session)
3079
4614
  }
3080
4615
  }
3081
4616
  const seg = this.segment
3082
4617
  if (seg !== undefined) {
3083
- if (current && outcome.kind === 'stop' && !this.sawVisibleBlock) {
4618
+ if (ambient !== undefined) {
4619
+ // The ambient line is OUR report, so it only appears when the drain
4620
+ // delivered no answer text of its own; reasoning alone does not count.
4621
+ if (ambient.line !== undefined && current) this.emitProse(seg, 'text', ambient.line)
4622
+ } else if (current && outcome.kind === 'stop' && !this.sawVisibleBlock) {
3084
4623
  // The silent-failure report: a normal stop with nothing the chat
3085
4624
  // plane renders. Aborted or failing turns need no extra hint; this
3086
4625
  // is specifically "protocol says success, the bubble is empty".
3087
- const line = `\n[dsh-ntr] agent "${this.session.agentId}" ended the turn with no visible output (stop reason: ${outcome.stopReason}). Some agents fold upstream failures — auth, billing, rate limits — into a normal stop; try resending, and check the agent process logs if it repeats.\n`
3088
- this.emitProse(seg, 'text', line)
4626
+ this.emitProse(seg, 'text', this.zeroOutputLine(outcome.stopReason))
3089
4627
  }
3090
4628
  this.finishSegment(seg, reason)
3091
4629
  }
@@ -3100,23 +4638,58 @@ class TurnStreamController {
3100
4638
  }
3101
4639
  this.endTurn()
3102
4640
  // The controller stays indexed only while a continuation step can still
3103
- // come for its cards. A cancelled, failed, or card-less turn has none:
3104
- // retire it now; the next turn's `rememberTurnStream` would anyway.
4641
+ // come for its cards. A cancelled, failed, or card-less turn has none: retire
4642
+ // it now; the next turn's `rememberTurnStream` would anyway. An ambient turn
4643
+ // ALWAYS retires: its suppressed calls never open cards, so keeping the index
4644
+ // would pin the controller (and its session id) to a turn nobody can continue.
3105
4645
  if (!current) this.retire()
3106
- else if (outcome.kind !== 'stop' || this.seenCalls.size === 0) this.dispose()
4646
+ else if (ambient !== undefined || outcome.kind !== 'stop' || this.seenCalls.size === 0) this.dispose()
3107
4647
  }
3108
4648
 
3109
- /** Cancelled by the user: finish the on-air segment and release the turn's slots. */
4649
+ /**
4650
+ * Cancelled by the user: finish the on-air segment and release the turn's
4651
+ * slots. An AMBIENT turn has already released the prompt slot (its response
4652
+ * landed) and holds no segment the cancel fallback could close, so the
4653
+ * cancel closes the turn itself with the cancelled stop.
4654
+ */
3110
4655
  abortActiveSegment(): void {
3111
4656
  if (this.stale) return
4657
+ this.clearPendingAmbient()
4658
+ if (this.closeScheduled) {
4659
+ // `settle` claimed the turn but queued its closing work for the next
4660
+ // macrotask. An abort landing inside that window must not let the
4661
+ // scheduled close broadcast the RESPONSE's stop reason while the segment
4662
+ // already finished `aborted`: drop the queued outcome and close NOW with
4663
+ // the cancelled one, so `turn/end` and the segment's finish agree.
4664
+ this.closeScheduled = false
4665
+ this.pendingOutcome = undefined
4666
+ this.closeTurn({ kind: 'cancelled' })
4667
+ return
4668
+ }
4669
+ if (this.awaitingAmbient !== undefined) {
4670
+ this.settle({ kind: 'cancelled' })
4671
+ return
4672
+ }
4673
+ if (this.backgroundHold !== undefined || this.pendingBackgroundHold !== undefined) {
4674
+ // A detached-worker hold is a turn whose prompt response already landed
4675
+ // (the slot is free) and whose segment is still open on purpose: the
4676
+ // cancel ends OUR face of it now, once, and the held run's id is retired
4677
+ // so its late text cannot join the next turn.
4678
+ this.clearBackgroundHold({ retire: true })
4679
+ this.settle({ kind: 'cancelled' })
4680
+ return
4681
+ }
3112
4682
  this.finishActive({ kind: 'aborted', failure: { message: 'aborted', code: 'ABORTED' } })
3113
4683
  // The agent may take its time acknowledging the cancel, but OUR face of
3114
- // the turn ends now: the session stops running and a provisional first
3115
- // lock settles against what was actually shown. The turn SLOT stays
3116
- // claimed, though: the agent-side prompt is still in flight, and a second
4684
+ // the turn ends now: the session stops running, the HOST's prompt slot is
4685
+ // released (so a new prompt may claim it), and a provisional first lock
4686
+ // settles against what was actually shown. The AGENT-side gate (`settled`)
4687
+ // stays claimed: the prompt is still in flight over there, and a second
3117
4688
  // prompt sent into that window would be a concurrent `session/prompt` the
3118
- // protocol forbids — `settle` (or the cancel fallback) releases the slot.
4689
+ // protocol forbids — `claimTurn` waits it out, `settle` (or the cancel
4690
+ // fallback) opens it.
3119
4691
  this.session.running = false
4692
+ this.session.promptInFlight = false
3120
4693
  if (this.session.provisionalLock === true) {
3121
4694
  this.session.provisionalLock = false
3122
4695
  if (!this.sawVisibleBlock) this.rollbackFirstTurn(this.session)
@@ -3124,9 +4697,29 @@ class TurnStreamController {
3124
4697
  releaseAllToolOutputWaiters()
3125
4698
  }
3126
4699
 
3127
- /** Stop observing and release the manager's index. Idempotent. */
4700
+ /**
4701
+ * Stop observing and release the manager's index. Idempotent.
4702
+ *
4703
+ * The segment is CLOSED here (aborted) before unsubscribing: a driver step
4704
+ * parked on its queue (`yield* segment.queue`) only ends when the queue
4705
+ * closes, and a controller replaced by the conversation's next turn would
4706
+ * otherwise leave that step hanging forever. An already finished segment is a
4707
+ * no-op, and the session's flags are left alone — a superseded turn must not
4708
+ * mark the newer turn's session idle.
4709
+ */
3128
4710
  dispose(): void {
3129
4711
  if (this.disposed) return
4712
+ // A settle may have queued its closing work for the next macrotask: this
4713
+ // close owns the turn now, so that tick must not broadcast a second
4714
+ // `turn/end` (with this turn's real id) over the teardown.
4715
+ this.closeScheduled = false
4716
+ this.pendingOutcome = undefined
4717
+ this.finishActive({ kind: 'aborted', failure: { message: 'turn stream retired', code: 'RETIRED' } })
4718
+ this.clearAmbientWait()
4719
+ this.clearPendingAmbient()
4720
+ // A successor turn must never fold this hold's drain or worker text: the
4721
+ // abandoned run's id is retired before the subscription goes away.
4722
+ this.clearBackgroundHold({ retire: true })
3130
4723
  this.retire()
3131
4724
  releaseAllToolOutputWaiters()
3132
4725
  }
@@ -3187,20 +4780,73 @@ class TurnStreamController {
3187
4780
  } else {
3188
4781
  // A settled turn that never showed anything: the zero-output warning the
3189
4782
  // plane would have gotten from a single-stream turn (constraint 23).
3190
- const line = `\n[dsh-ntr] agent "${this.session.agentId}" ended the turn with no visible output. Some agents fold upstream failures — auth, billing, rate limits — into a normal stop; try resending, and check the agent process logs if it repeats.\n`
4783
+ const line = this.zeroOutputLine()
3191
4784
  if (seg.index < 0) this.emitProse(seg, 'text', line)
3192
4785
  this.finishSegment(seg, { kind: 'stop' })
3193
4786
  }
3194
4787
  }
3195
4788
 
4789
+ /**
4790
+ * The "protocol says success, the bubble is empty" line. When a recently
4791
+ * retired drain may have taken the prompt without re-announcing itself, say
4792
+ * that instead of blaming the agent: the output exists in the transcript.
4793
+ */
4794
+ private zeroOutputLine(stopReason?: string): string {
4795
+ if (this.recentRetiredDrain) {
4796
+ return `\n[dsh-ntr] agent "${this.session.agentId}" produced no visible output this turn; it may have been folded into a background run; its output is in the ACP transcript view.\n`
4797
+ }
4798
+ const reason = stopReason === undefined ? '' : ` (stop reason: ${stopReason})`
4799
+ return `\n[dsh-ntr] agent "${this.session.agentId}" ended the turn with no visible output${reason}. Some agents fold upstream failures — auth, billing, rate limits — into a normal stop; try resending, and check the agent process logs if it repeats.\n`
4800
+ }
4801
+
3196
4802
  private onEvent(event: unknown): void {
3197
4803
  if (this.disposed) return
3198
4804
  const row = event as { type?: string; sessionId?: string }
3199
4805
  if (row.sessionId !== undefined && row.sessionId !== this.session.sessionId) return
3200
4806
  if (row.type === 'session/update') {
3201
4807
  const typed = event as { sessionId: string; seq: number; update: unknown }
3202
- if (typed.seq <= this.cursorSeq) return
4808
+ // Any update — a `usage_update` / phase tick included — says the
4809
+ // background drain is still working, so the ambient quiet clock restarts.
4810
+ this.noteAmbientActivity()
4811
+ // A detached-worker hold's drain phase has the same rule; its worker
4812
+ // phase never does (see `BridgeTuning.backgroundTaskMaxMs`).
4813
+ this.noteBackgroundHoldActivity()
3203
4814
  const update = typed.update as Record<string, unknown>
4815
+ // The dispatch / worker / announcement bookkeeping runs BEFORE the cursor
4816
+ // guard: those are turn-level facts, not segment content, and an update
4817
+ // withheld by the ambient buffer must still keep them.
4818
+ this.trackBackgroundDispatch(update)
4819
+ if (typed.seq <= this.cursorSeq) return
4820
+ // A background drain's prose is not this turn's answer: keep it out of
4821
+ // the plane (the transcript still has it). Fail-open — nothing is split
4822
+ // until this turn has a request id of its own to compare against.
4823
+ this.noteTurnRequestId(update)
4824
+ if (this.isForeignAmbientText(update)) {
4825
+ this.recordWithoutSegment(update)
4826
+ // Advance past it only when nothing is left unconsumed behind it: the
4827
+ // event that CUT the previous segment is parked for the next one, and
4828
+ // jumping the cursor over it would lose its text for good. The replay
4829
+ // applies the same foreign split, so leaving this one behind is safe.
4830
+ if (typed.seq === this.cursorSeq + 1) this.cursorSeq = typed.seq
4831
+ return
4832
+ }
4833
+ // The turn's OWN request id is still unknown and this update belongs to
4834
+ // the background drain: folding it now could make the drain's tick teach
4835
+ // `noteTurnRequestId` the drain's id, mixing the drain's text into the
4836
+ // user's answer. Withhold it until the prompt response decides what this
4837
+ // turn is (see `pendingAmbientUpdates`); the cursor moves on because the
4838
+ // transcript already holds the entry.
4839
+ if (this.turnRequestId === undefined
4840
+ && this.awaitingAmbient === undefined
4841
+ && this.isAmbientOwned(update)) {
4842
+ this.bufferAmbientUpdate(typed.seq, update)
4843
+ this.cursorSeq = typed.seq
4844
+ return
4845
+ }
4846
+ // Live-only burst accounting runs BEFORE the segment cursor: prose that
4847
+ // arrives between segments still belongs to this turn's output, and its
4848
+ // gaps are real. Replay never reaches this path.
4849
+ this.trackTps(typed)
3204
4850
  const seg = this.segment
3205
4851
  if (seg === undefined || seg.finished) {
3206
4852
  // Between segments: tool outputs still reach the shared registry (the
@@ -3213,16 +4859,75 @@ class TurnStreamController {
3213
4859
  }
3214
4860
  // The cursor advances only past events fully consumed into this
3215
4861
  // segment; the event that CUTS the segment belongs to the next one.
3216
- if (this.foldInto(seg, update)) this.cursorSeq = typed.seq
4862
+ if (this.foldInto(seg, update, typed.seq)) this.cursorSeq = typed.seq
3217
4863
  } else if (row.type === 'permission/request') {
4864
+ this.tps.onBreak()
3218
4865
  const typed = event as Extract<AcpEvent, { type: 'permission/request' }>
3219
4866
  this.pushProse(this.permissionLine(typed))
3220
4867
  } else if (row.type === 'permission/expired') {
4868
+ this.tps.onBreak()
3221
4869
  const typed = event as { toolCall: unknown; timeoutMs: number; outcome: PermissionOutcome; optionName?: string }
3222
4870
  this.pushProse(this.expiredLine(typed))
3223
4871
  }
3224
4872
  }
3225
4873
 
4874
+ /** Feed one live update into the turn's burst tracker. Never called by replay. */
4875
+ private trackTps(typed: { seq: number; update: unknown }): void {
4876
+ if (typed.seq <= this.tpsSeq) return
4877
+ this.tpsSeq = typed.seq
4878
+ const update = typed.update as Record<string, unknown>
4879
+ const kind = String(update.sessionUpdate ?? '')
4880
+ if (kind === 'agent_thought_chunk' || kind === 'agent_message_chunk') {
4881
+ const text = chunkTextOf(update.content)
4882
+ if (text === '') return
4883
+ this.tps.onProse(text, Date.now())
4884
+ this.pushLiveTps()
4885
+ } else if (kind === 'tool_call' || kind === 'tool_call_update') {
4886
+ this.tps.onBreak()
4887
+ }
4888
+ }
4889
+
4890
+ /** Throttled live aggregate so the dock's speed moves during a long turn. */
4891
+ private pushLiveTps(): void {
4892
+ const dshSessionId = this.session.dshSessionId
4893
+ if (dshSessionId === undefined) return
4894
+ const now = Date.now()
4895
+ if (now - this.lastTpsPushAt < TPS_PUSH_INTERVAL_MS) return
4896
+ const snap = this.tps.snapshot()
4897
+ if (snap.outputTokens <= 0 || snap.burstMs <= 0) return
4898
+ this.lastTpsPushAt = now
4899
+ const base = this.session.burst ?? { outputTokens: 0, burstMs: 0 }
4900
+ this.hub.broadcast({
4901
+ type: 'turn/metrics',
4902
+ sessionId: this.session.sessionId,
4903
+ dshSessionId,
4904
+ turnId: this.turnId,
4905
+ outputTokens: base.outputTokens + snap.outputTokens,
4906
+ burstMs: base.burstMs + snap.burstMs,
4907
+ final: false,
4908
+ })
4909
+ }
4910
+
4911
+ /** Close the turn's metrics: persist the measurable part, then broadcast it. */
4912
+ private finalizeTps(): void {
4913
+ const dshSessionId = this.session.dshSessionId
4914
+ if (dshSessionId === undefined) return
4915
+ const snap = this.tps.snapshot()
4916
+ if (snap.outputTokens > 0 && snap.burstMs > 0) {
4917
+ this.manager.noteSessionBurst(this.session, snap.outputTokens, snap.burstMs)
4918
+ }
4919
+ const aggregate = this.session.burst ?? { outputTokens: 0, burstMs: 0 }
4920
+ this.hub.broadcast({
4921
+ type: 'turn/metrics',
4922
+ sessionId: this.session.sessionId,
4923
+ dshSessionId,
4924
+ turnId: this.turnId,
4925
+ outputTokens: aggregate.outputTokens,
4926
+ burstMs: aggregate.burstMs,
4927
+ final: true,
4928
+ })
4929
+ }
4930
+
3226
4931
  /**
3227
4932
  * Put one narrative line into the turn's prose stream.
3228
4933
  *
@@ -3259,15 +4964,115 @@ class TurnStreamController {
3259
4964
  recordAcpToolOutput(callId, updateContentToText(update.content))
3260
4965
  }
3261
4966
 
4967
+ /**
4968
+ * Whether this update belongs to a background drain — the one the session is
4969
+ * waiting on, or one whose wait already closed (retired) but whose run can
4970
+ * still speak.
4971
+ */
4972
+ private isAmbientOwned(update: Record<string, unknown>): boolean {
4973
+ const requestId = codebuddyMetaOf(update)?.requestId
4974
+ if (requestId === undefined) return false
4975
+ return requestId === this.session.ambient?.requestId
4976
+ || isRetiredAmbientId(this.session, requestId)
4977
+ }
4978
+
4979
+ /** Learn this turn's own CodeBuddy request id from the first update naming one. */
4980
+ private noteTurnRequestId(update: Record<string, unknown>): void {
4981
+ if (this.turnRequestId !== undefined) return
4982
+ const meta = codebuddyMetaOf(update)
4983
+ // A detached worker's update names A WORKER, never this turn: adopting it
4984
+ // would let the worker's own turn pass as the session's answer.
4985
+ if (meta?.parentToolCallId !== undefined) return
4986
+ // While a hold runs, the only ids this turn owns are the drain ids it
4987
+ // accepted (see `BackgroundHold.acceptedIds`) — and those belong to the
4988
+ // background run, never to a second "own turn id" that would outlive the
4989
+ // hold. Nothing else may be adopted.
4990
+ if (this.activeBackgroundHold() !== undefined) return
4991
+ const requestId = meta?.requestId
4992
+ // A background drain's update names THE DRAIN, never this turn: adopting it
4993
+ // would make `isForeignAmbientText` pass the drain's own text through. A
4994
+ // RETIRED drain (its wait closed on silence, the run revived) is the same
4995
+ // trap — that id belongs to the background run whichever turn is on air.
4996
+ if (requestId === undefined || requestId === this.session.ambient?.requestId) return
4997
+ if (isRetiredAmbientId(this.session, requestId)) return
4998
+ this.turnRequestId = requestId
4999
+ }
5000
+
5001
+ /** Adopt the request id the prompt RESPONSE declared for this turn. */
5002
+ adoptTurnRequestId(requestId: string): void {
5003
+ // A retired drain id is never this turn's own: adopting it would make the
5004
+ // background run's prose pass `isForeignAmbientText` as the answer. The
5005
+ // response still ends a normal turn, so the withheld drain buffer is
5006
+ // dropped either way (the transcript keeps it).
5007
+ if (!isRetiredAmbientId(this.session, requestId)) this.turnRequestId = requestId
5008
+ // A normal turn: whatever the drain streamed while the id was unknown was
5009
+ // never this turn's answer. Drop it (the transcript keeps it) — it must not
5010
+ // surface when a later segment replays or when the turn flushes anything.
5011
+ this.clearPendingAmbient()
5012
+ }
5013
+
5014
+ /**
5015
+ * Prose of the session's background drain while this turn runs on its OWN
5016
+ * request id: it belongs to the background run, not to the user's answer.
5017
+ * Fail-open by design — an unknown turn id splits nothing, and a turn that
5018
+ * was itself folded into the drain keeps its prose.
5019
+ */
5020
+ private isForeignAmbientText(update: Record<string, unknown>): boolean {
5021
+ const meta = codebuddyMetaOf(update)
5022
+ const requestId = meta?.requestId
5023
+ // A detached worker's own turn is never this session's answer, whichever
5024
+ // turn is on air (the fold-level guard lives in `foldInto` as well, for
5025
+ // the replay paths that never ask this question). This includes a worker
5026
+ // update that also names a drain id: its prose is the WORKER's.
5027
+ if (meta?.parentToolCallId !== undefined) return true
5028
+ // A detached-worker hold owns the drain phase: any run it accepted (the
5029
+ // response's id, an announcement that named a different one) is this
5030
+ // turn's answer, and every OTHER id is background noise — whatever the
5031
+ // turn's own id says. Parked holds count: the response already owns them.
5032
+ const hold = this.activeBackgroundHold()
5033
+ if (hold !== undefined) {
5034
+ if (requestId === undefined) return false
5035
+ return !hold.acceptedIds.has(requestId)
5036
+ }
5037
+ // The drain THIS turn is waiting on owns its text unconditionally: the
5038
+ // wait was opened for this id (a revived drain may still carry a retired
5039
+ // mark if the update raced the revival), so the whole steered answer must
5040
+ // reach the user instead of being filtered as background noise.
5041
+ if (requestId !== undefined && this.awaitingAmbient?.requestId === requestId) return false
5042
+ // A RETIRED drain is foreign even though its record is gone: the quiet
5043
+ // close only means WE stopped waiting, not that the run ended. Its late
5044
+ // text must never join the turn now on air — even before that turn has
5045
+ // learned a request id of its own (the fail-open below does not apply to
5046
+ // an id known to be a background run).
5047
+ if (isRetiredAmbientId(this.session, requestId)) return true
5048
+ if (this.turnRequestId === undefined) return false
5049
+ if (requestId === undefined || requestId === this.turnRequestId) return false
5050
+ const ambient = this.session.ambient
5051
+ return ambient !== undefined && requestId === ambient.requestId
5052
+ }
5053
+
3262
5054
  /**
3263
5055
  * The turn's whole fold, shared by live events and segment replay. Visible
3264
5056
  * non-tool events END a card-bearing segment instead of joining it — that is
3265
5057
  * the segmentation rule that puts each card between two assistant messages.
3266
5058
  * Returns whether the event was consumed into this segment: the cutting
3267
5059
  * event is left unconsumed (and thus behind the cursor) for the next one.
5060
+ *
5061
+ * `seq` is the update's transcript position when the caller has one: the
5062
+ * detached-worker hold uses it to count only the answer text that arrived
5063
+ * AFTER the hold began (a parked hold replays the parent's earlier ack).
3268
5064
  */
3269
- private foldInto(seg: SegmentState, update: Record<string, unknown>): boolean {
5065
+ private foldInto(seg: SegmentState, update: Record<string, unknown>, seq?: number): boolean {
3270
5066
  const kind = String(update.sessionUpdate ?? '')
5067
+ // A DETACHED WORKER's own turn: its words and tools are the worker's, not
5068
+ // this session's answer. The transcript keeps them (the ACP view is where
5069
+ // the user can read them) and the shared tool-output registry still sees
5070
+ // the call, but nothing reaches this turn's message and no card opens for
5071
+ // the host to execute. This applies to EVERY turn, not just a hold.
5072
+ if (codebuddyMetaOf(update)?.parentToolCallId !== undefined) {
5073
+ this.noteToolRecord(update)
5074
+ return true
5075
+ }
3271
5076
  if (kind === 'agent_thought_chunk' || kind === 'agent_message_chunk') {
3272
5077
  const text = chunkTextOf(update.content)
3273
5078
  if (text === '') return true
@@ -3276,10 +5081,34 @@ class TurnStreamController {
3276
5081
  return false
3277
5082
  }
3278
5083
  seg.outputChars += text.length
5084
+ // "The turn's own answer", as opposed to a background drain's prose that
5085
+ // happens to stream through the same session (see `isForeignAmbientText`).
5086
+ if (!this.isAmbientOwned(update)) this.sawAgentProse = true
5087
+ if (kind === 'agent_message_chunk') {
5088
+ this.sawVisibleText = true
5089
+ // A hold's closing line asks whether ITS runs delivered answer text.
5090
+ // The parent's interim acknowledgement arrived before the hold and
5091
+ // already set `sawVisibleText`, so the hold keeps its own counter —
5092
+ // and only text that arrived after the hold began counts (a parked
5093
+ // hold replays that earlier ack from the transcript).
5094
+ const hold = this.activeBackgroundHold()
5095
+ if (hold !== undefined && (seq === undefined || seq > hold.floorSeq)) {
5096
+ hold.answerChars += text.length
5097
+ }
5098
+ }
3279
5099
  this.emitProse(seg, kind === 'agent_thought_chunk' ? 'reasoning' : 'text', text)
3280
5100
  return true
3281
5101
  } else if (kind === 'tool_call' || kind === 'tool_call_update') {
3282
5102
  this.noteToolRecord(update)
5103
+ if (seg.ambient || this.isAmbientOwned(update)) {
5104
+ // A background drain's tools stay in the transcript: opening a card
5105
+ // would make the host execute a tool and propose a continuation step
5106
+ // for a turn whose answer has not started yet. The ownership check
5107
+ // covers a NORMAL turn too — its own segment is not ambient, but a
5108
+ // drain can stream a tool call on the same session while it runs, and
5109
+ // that call is not this turn's work.
5110
+ return true
5111
+ }
3283
5112
  const snapshot = acpToolCallFromUpdate(update)
3284
5113
  if (snapshot === undefined) return true
3285
5114
  if (seg.activeType === 'tool-call' && seg.activeToolCall?.callId === snapshot.callId) {
@@ -3291,6 +5120,12 @@ class TurnStreamController {
3291
5120
  return true
3292
5121
  }
3293
5122
  if (this.seenCalls.has(snapshot.callId)) return true // a restatement of a card already settled
5123
+ // A BARE status-only update for a call this turn never announced is not
5124
+ // a card. The real wire sends the detached dispatch's completion exactly
5125
+ // that way (no `_meta`, no title, no input); a turn that begins after the
5126
+ // dispatch belongs to another turn's card, and opening one here would
5127
+ // make the host execute a phantom tool.
5128
+ if (kind === 'tool_call_update' && snapshot.name === '' && snapshot.argsRaw === '') return true
3294
5129
  this.seenCalls.add(snapshot.callId)
3295
5130
  this.openToolCallBlock(seg, snapshot)
3296
5131
  return true
@@ -3524,6 +5359,34 @@ export class BridgeManager {
3524
5359
  return this.turnStreams.get(dshSessionId)
3525
5360
  }
3526
5361
 
5362
+ /**
5363
+ * The ambient windows a turn's controller reads once at construction (see
5364
+ * {@link BridgeTuning.ambientQuietMs}). Exposed as a plain snapshot so the
5365
+ * controller never pokes at the manager's tuning object, and tests can tune
5366
+ * the cadence through the constructor like every other window.
5367
+ */
5368
+ get ambientTuning(): { quietMs: number; maxMs: number } {
5369
+ return {
5370
+ quietMs: this.tuning.ambientQuietMs,
5371
+ maxMs: this.tuning.ambientMaxMs,
5372
+ }
5373
+ }
5374
+
5375
+ /**
5376
+ * The detached-worker hold's windows, read once at controller construction
5377
+ * for the same reason as {@link ambientTuning}: `taskMaxMs` is the worker
5378
+ * phase's only clock (see {@link BridgeTuning.backgroundTaskMaxMs}) and
5379
+ * `graceMs` the bounded wait a PARKED hold gives the host to open its
5380
+ * continuation step (see {@link BridgeTuning.backgroundParkedGraceMs}). The
5381
+ * drain phase reuses {@link ambientTuning}.
5382
+ */
5383
+ get backgroundTuning(): { taskMaxMs: number; graceMs: number } {
5384
+ return {
5385
+ taskMaxMs: this.tuning.backgroundTaskMaxMs,
5386
+ graceMs: this.tuning.backgroundParkedGraceMs,
5387
+ }
5388
+ }
5389
+
3527
5390
  /** Index a turn's controller, retiring whatever turn it supersedes. */
3528
5391
  rememberTurnStream(dshSessionId: string, controller: TurnStreamController): void {
3529
5392
  const previous = this.turnStreams.get(dshSessionId)
@@ -3722,6 +5585,13 @@ export class BridgeManager {
3722
5585
  this.sessionWorkspaceResolver = resolver
3723
5586
  }
3724
5587
 
5588
+ /**
5589
+ * Set the attachment service or context used to resolve image attachments in user prompts.
5590
+ */
5591
+ setAttachmentResolver(resolver?: unknown): void {
5592
+ this.attachmentResolver = resolver
5593
+ }
5594
+
3725
5595
  /**
3726
5596
  * Wire the ACP-mode probe: given a DSH session id, report whether that
3727
5597
  * session runs the ACP agent preset. The host half reads it back from the
@@ -3836,6 +5706,7 @@ export class BridgeManager {
3836
5706
  ...(session.modes === undefined ? {} : { modes: session.modes }),
3837
5707
  ...(session.availableCommands === undefined ? {} : { availableCommands: session.availableCommands }),
3838
5708
  ...(session.usage === undefined ? {} : { usage: session.usage }),
5709
+ ...(session.burst === undefined ? {} : { burst: session.burst }),
3839
5710
  ...(session.released ? { released: true } : {}),
3840
5711
  ...(session.running ? { running: true } : {}),
3841
5712
  ...forkRowInfo(session, this.canForkNow(session.agentId)),
@@ -4733,6 +6604,21 @@ export class BridgeManager {
4733
6604
  for (const dshSessionId of this.chatAgentIntent.keys()) this.syncConversationRoute(dshSessionId)
4734
6605
  }
4735
6606
 
6607
+ /**
6608
+ * Fold one settled ACP turn's measurable burst into the session aggregate and
6609
+ * persist it. A turn with no measured inter-chunk gap contributes neither
6610
+ * side: adding its tokens with zero time would inflate the session rate.
6611
+ */
6612
+ noteSessionBurst(session: LiveSession, outputTokens: number, burstMs: number): void {
6613
+ if (outputTokens <= 0 || burstMs <= 0) return
6614
+ const base = session.burst ?? { outputTokens: 0, burstMs: 0 }
6615
+ session.burst = {
6616
+ outputTokens: base.outputTokens + outputTokens,
6617
+ burstMs: base.burstMs + burstMs,
6618
+ }
6619
+ this.persistAgents()
6620
+ }
6621
+
4736
6622
  private persistAgents(): void {
4737
6623
  const filePath = this.getStoragePath()
4738
6624
  if (!filePath) return
@@ -4798,8 +6684,8 @@ export class BridgeManager {
4798
6684
  if (typeof dshSessionId !== 'string' || dshSessionId === '') continue
4799
6685
  if (typeof cwd !== 'string') continue
4800
6686
  if (!this.defsMap.has(agentId)) continue
4801
- const { title, modelId, reasoningEffort, modes, usage, hasHistory } = record as {
4802
- title?: unknown; modelId?: unknown; reasoningEffort?: unknown; modes?: unknown; usage?: unknown; hasHistory?: unknown
6687
+ const { title, modelId, reasoningEffort, modes, usage, burst, hasHistory } = record as {
6688
+ title?: unknown; modelId?: unknown; reasoningEffort?: unknown; modes?: unknown; usage?: unknown; burst?: unknown; hasHistory?: unknown
4803
6689
  }
4804
6690
  const session: LiveSession = {
4805
6691
  sessionId,
@@ -4814,6 +6700,7 @@ export class BridgeManager {
4814
6700
  ...(typeof reasoningEffort === 'string' && reasoningEffort !== '' ? { reasoningEffort } : {}),
4815
6701
  ...(isPersistedModes(modes) ? { modes } : {}),
4816
6702
  ...(isPersistedUsage(usage) ? { usage } : {}),
6703
+ ...(isPersistedBurst(burst) ? { burst } : {}),
4817
6704
  ...(hasHistory === true ? { hasHistory: true } : {}),
4818
6705
  running: false,
4819
6706
  turnSeq: 0,
@@ -5712,7 +7599,7 @@ export class BridgeManager {
5712
7599
  return { sessionId: session.sessionId }
5713
7600
  }
5714
7601
 
5715
- async prompt(sessionId: string, text: string): Promise<{ turnId: string }> {
7602
+ async prompt(sessionId: string, prompt: string | ContentBlock[]): Promise<{ turnId: string }> {
5716
7603
  let located = await this.locateOrResume(sessionId)
5717
7604
  // The watchdog abandoned a previous turn here and the agent may still be
5718
7605
  // holding its prompt slot on the old process: reclaim BEFORE sending, or
@@ -5730,7 +7617,40 @@ export class BridgeManager {
5730
7617
  located = await this.locateOrResume(sessionId)
5731
7618
  }
5732
7619
  const { bridge, session } = located
5733
- if (session.running) throw new AcpError('turn_in_flight', 'acp: this session already has a prompt in flight')
7620
+ // The agent prompt slot is SHARED with chat turns: `running` alone misses
7621
+ // a chat turn whose cancellation already released it (`running` false,
7622
+ // `promptInFlight` still naming the agent-side turn), and a live chat
7623
+ // controller means a continuation step may resume the SAME agent turn.
7624
+ // A controller that already SETTLED (kept only so its card's continuation
7625
+ // can still stream) has given the agent slot back: `/acp` may use it, and
7626
+ // advancing `turnSeq` retires it — the behaviour the stale-controller
7627
+ // watchdog case pins.
7628
+ const chatController = session.turnController
7629
+ if (session.running
7630
+ || session.promptInFlight === true
7631
+ || (chatController !== undefined && !chatController.stale && !chatController.hasSettled)) {
7632
+ throw new AcpError('turn_in_flight', 'acp: this session already has a prompt in flight')
7633
+ }
7634
+
7635
+ const blocks: ContentBlock[] = typeof prompt === 'string'
7636
+ ? [{ type: 'text', text: prompt }]
7637
+ : prompt
7638
+
7639
+ if (blocks.length === 0) {
7640
+ throw new AcpError('empty_prompt', 'acp: prompt cannot be empty')
7641
+ }
7642
+
7643
+ const hasImages = blocks.some(b => b.type === 'image')
7644
+ if (hasImages && bridge.initResult?.agentCapabilities?.promptCapabilities?.image !== true) {
7645
+ throw new AcpError('image_unsupported', `acp: agent "${bridge.def.id}" does not support image input`)
7646
+ }
7647
+
7648
+ // Claim the SAME slot the chat path claims (`claimTurn` + `beginTurn`): an
7649
+ // `/acp` turn in flight must make the next chat prompt wait, and vice
7650
+ // versa. The settler is idempotent and identity-guarded; the turn still
7651
+ // ends through `turn/end` below, so chat and RPC turns share one gate
7652
+ // without sharing a stream controller.
7653
+ const endTurn = bridge.beginTurn(session)
5734
7654
  session.running = true
5735
7655
  session.turnSeq += 1
5736
7656
  const turnGen = session.turnSeq
@@ -5741,9 +7661,12 @@ export class BridgeManager {
5741
7661
  session.reclaimedTurnSeq = undefined
5742
7662
  this.armStallTimer()
5743
7663
  const turnId = randomUUID()
5744
- this.hub.broadcast({ type: 'turn/start', sessionId, turnId, text })
5745
- void bridge.runPrompt(session, text, turnId)
5746
- .then(stopReason => {
7664
+ const promptText = typeof prompt === 'string'
7665
+ ? prompt
7666
+ : blocks.filter(b => b.type === 'text').map(b => (b as { text: string }).text).join('\n')
7667
+ this.hub.broadcast({ type: 'turn/start', sessionId, turnId, text: promptText })
7668
+ void bridge.runPrompt(session, blocks, turnId, turnGen)
7669
+ .then(result => {
5747
7670
  // The reclaim already ended this turn locally; the answer it caused is
5748
7671
  // that same turn reporting back, not a second turn end for the caller.
5749
7672
  if (session.reclaimedTurnSeq === turnGen) {
@@ -5753,8 +7676,11 @@ export class BridgeManager {
5753
7676
  // A superseded turn's late answer must not end the turn now on air.
5754
7677
  if (session.turnSeq !== turnGen) return
5755
7678
  this.clearOrphanPrompt(session, turnGen)
7679
+ // Release the shared slot BEFORE the terminal frame: a listener that
7680
+ // sends the next prompt on `turn/end` must not read a stale claim.
7681
+ endTurn()
5756
7682
  session.running = false
5757
- this.hub.broadcast({ type: 'turn/end', sessionId, turnId, stopReason })
7683
+ this.hub.broadcast({ type: 'turn/end', sessionId, turnId, stopReason: result.stopReason })
5758
7684
  })
5759
7685
  .catch((error: unknown) => {
5760
7686
  if (session.reclaimedTurnSeq === turnGen) {
@@ -5771,6 +7697,7 @@ export class BridgeManager {
5771
7697
  return
5772
7698
  }
5773
7699
  this.clearOrphanPrompt(session, turnGen)
7700
+ endTurn()
5774
7701
  session.running = false
5775
7702
  this.hub.broadcast({
5776
7703
  type: 'turn/end',
@@ -5779,6 +7706,9 @@ export class BridgeManager {
5779
7706
  error: describeError(error),
5780
7707
  })
5781
7708
  })
7709
+ // Every outcome — normal answer, failure, reclaim early-return, or a
7710
+ // close's rejection — releases the shared prompt slot exactly once.
7711
+ .finally(() => { endTurn() })
5782
7712
  return { turnId }
5783
7713
  }
5784
7714
 
@@ -6104,6 +8034,7 @@ export class BridgeManager {
6104
8034
  }
6105
8035
  private sessionWorkspaceResolver?: SessionWorkspaceResolver
6106
8036
  private acpModeResolver?: AcpModeResolver
8037
+ private attachmentResolver?: unknown
6107
8038
  /** Cached `chatSessionAcpMode` answers, keyed by DSH chat session id. */
6108
8039
  private readonly acpModeCache = new Map<string, boolean>()
6109
8040
  /** In-flight cold reads behind `chatSessionAcpModeAnswer`, keyed by session id. */
@@ -6469,8 +8400,36 @@ export class BridgeManager {
6469
8400
  // Forward the person's own words: the host folds its context sections
6470
8401
  // (AGENTS.md digest, skill catalog, runtime snapshot) after the claimed user
6471
8402
  // batch precisely so a consumer can tell them apart by `source` — see
6472
- // `selectForwardPrompt` and AGENTS.md rule 16.
6473
- const selection = selectForwardPrompt(options.messages ?? [])
8403
+ const workspaceRoot = chatWorkspaceRoot(this.sessionWorkspaceResolver, options.sessionId)
8404
+ const defaultReadFile = (p: string) => {
8405
+ try {
8406
+ if (existsSync(p)) return readFileSync(p)
8407
+ if (workspaceRoot !== undefined && !isAbsolutePath(p)) {
8408
+ const joined = join(workspaceRoot, p)
8409
+ if (existsSync(joined)) return readFileSync(joined)
8410
+ }
8411
+ } catch {}
8412
+ return undefined
8413
+ }
8414
+
8415
+ const resolver = this.attachmentResolver
8416
+ const attachmentContext = resolver !== undefined && typeof resolver === 'object' && resolver !== null
8417
+ ? Object.assign(Object.create(resolver), {
8418
+ readFile: (p: string) => {
8419
+ if (typeof (resolver as { readFile?: unknown }).readFile === 'function') {
8420
+ try {
8421
+ const res = (resolver as { readFile: (p: string) => unknown }).readFile(p)
8422
+ if (res !== undefined) return res
8423
+ } catch {}
8424
+ }
8425
+ return defaultReadFile(p)
8426
+ },
8427
+ })
8428
+ : {
8429
+ readFile: defaultReadFile,
8430
+ }
8431
+
8432
+ const selection = selectForwardPrompt(options.messages ?? [], attachmentContext)
6474
8433
  if (selection.kind === 'no-user-input') {
6475
8434
  // An injected-context wake: nothing the user said, nothing to send.
6476
8435
  yield { type: 'finish', reason: { kind: 'stop' } }
@@ -6537,14 +8496,31 @@ export class BridgeManager {
6537
8496
  return
6538
8497
  }
6539
8498
 
8499
+ const hasImages = (selection.images?.length ?? 0) > 0 || (selection.content?.some(b => b.type === 'image') ?? false)
8500
+ if (hasImages && bridge.initResult?.agentCapabilities?.promptCapabilities?.image !== true) {
8501
+ if (session.provisionalLock === true) this.rollbackFirstTurn(session)
8502
+ yield {
8503
+ type: 'finish',
8504
+ reason: {
8505
+ kind: 'error',
8506
+ failure: {
8507
+ message: `acp: agent "${agentId}" does not support image input`,
8508
+ code: 'image_unsupported',
8509
+ },
8510
+ },
8511
+ }
8512
+ return
8513
+ }
8514
+
6540
8515
  // One prompt slot per session, and a turn the user aborted still occupies
6541
8516
  // the AGENT's slot until it acknowledges the cancel (see `claimTurn`).
6542
8517
  const claim = await bridge.claimTurn(session)
6543
8518
  if (!claim.ok) {
6544
- // A first turn that never got the prompt slot delivered nothing either:
6545
- // give the conversation back instead of leaving it locked to an agent
6546
- // that refused the only message it was asked to serve.
6547
- if (session.provisionalLock === true) this.rollbackFirstTurn(session)
8519
+ // Never roll the provisional lock back here: a failed claim means ANOTHER
8520
+ // prompt holds the slot (or the agent has not settled it), and that turn
8521
+ // owns the conversation's lock. Closing the session would kill a turn
8522
+ // that is actively streaming — the refusal already tells the user to
8523
+ // retry.
6548
8524
  yield {
6549
8525
  type: 'finish',
6550
8526
  reason: {
@@ -6682,8 +8658,12 @@ export class BridgeManager {
6682
8658
  // next turn has to respect. Settlement reaches the controller, which
6683
8659
  // releases the slot (`endTurn`) whether the segment on air is the first
6684
8660
  // or a continuation one.
6685
- bridge.runPrompt(session, promptText, turnId)
6686
- .then(stopReason => {
8661
+ const promptBlocks: ContentBlock[] = selection.content && selection.content.length > 0
8662
+ ? selection.content as ContentBlock[]
8663
+ : [{ type: 'text', text: promptText }]
8664
+
8665
+ bridge.runPrompt(session, promptBlocks, turnId, turnGen)
8666
+ .then(result => {
6687
8667
  // The reclaim already ended this turn locally, and the close it
6688
8668
  // performed rejected this very request: the late answer is that same
6689
8669
  // turn, not a second one to broadcast.
@@ -6691,9 +8671,79 @@ export class BridgeManager {
6691
8671
  session.reclaimedTurnSeq = undefined
6692
8672
  return
6693
8673
  }
6694
- controller.settle(stopReason === 'cancelled'
8674
+ const responseMeta = codebuddyMetaOf({ _meta: result.meta })
8675
+ // A DETACHED-WORKER HOLD: this turn's `Agent` call handed real work
8676
+ // to a background worker that has not produced its answer yet, yet
8677
+ // the agent answered the prompt normally (CodeBuddy says "已派…" and
8678
+ // ends its own turn). Closing here loses the only answer the turn
8679
+ // will ever produce — the worker's report streams into this same
8680
+ // session later, as a background drain. Hold the turn open until
8681
+ // every dispatch reports and the drain goes quiet (see
8682
+ // `holdForBackgroundDispatch`); the prompt slot is released there.
8683
+ //
8684
+ // The decision comes BEFORE `adoptTurnRequestId`: adopting the id
8685
+ // would clear the updates the drain streamed while this turn's own id
8686
+ // was unknown, and those are exactly the ones the hold must fold.
8687
+ const holdDispatch = controller.dispatchedDetachedWorker
8688
+ && !session.released
8689
+ && !session.closed
8690
+ && result.stopReason !== 'cancelled'
8691
+ if (!holdDispatch && responseMeta?.conversationRequestId !== undefined) {
8692
+ // The response names the conversation turn it queued: an instant
8693
+ // answer that still carries one is a real (if silent) turn, and its
8694
+ // prose can be told apart from a background drain's.
8695
+ controller.adoptTurnRequestId(responseMeta.conversationRequestId)
8696
+ }
8697
+ // An announced drain that has shown NO sign of life for the staleness
8698
+ // window is over, even though nothing said so (CodeBuddy never sends
8699
+ // `session_end`). Keeping the record would make a later instant answer
8700
+ // with no conversation request id look steered, and hold a normal turn
8701
+ // open against a drain that is never coming back. RETIRE the id
8702
+ // first, so a late update or a response naming it is never adopted as
8703
+ // this session's own turn; then forget the record (the notice de-dupe
8704
+ // set with it) so this response settles normally.
8705
+ if (session.ambient !== undefined
8706
+ && Date.now() - session.ambient.lastActivityAt > this.tuning.ambientStaleMs) {
8707
+ retireAmbientId(session, session.ambient.requestId)
8708
+ session.ambient = undefined
8709
+ session.ambientNotices?.clear()
8710
+ }
8711
+ if (holdDispatch) {
8712
+ // The response may name one run while an announcement named
8713
+ // another; the hold accepts both (see `BackgroundHold.acceptedIds`).
8714
+ controller.holdForBackgroundDispatch(
8715
+ result.stopReason,
8716
+ responseMeta?.conversationRequestId ?? responseMeta?.requestId,
8717
+ )
8718
+ return
8719
+ }
8720
+ // An instant answer with NO conversation request id, while the agent
8721
+ // announced a background drain this turn has not spoken into: the
8722
+ // prompt was folded into that drain (CodeBuddy's multitask). The
8723
+ // response is a formality — keep the turn open until the drain speaks.
8724
+ if (session.ambient !== undefined
8725
+ && !session.released
8726
+ && !session.closed
8727
+ && result.stopReason !== 'cancelled'
8728
+ && responseMeta?.conversationRequestId === undefined
8729
+ && !controller.hasAgentProse) {
8730
+ controller.awaitAmbient(session.ambient.requestId, result.stopReason)
8731
+ return
8732
+ }
8733
+ // No ambient record to wait on, but a drain this session stopped
8734
+ // waiting on was retired moments ago: the run may have taken this
8735
+ // prompt WITHOUT re-announcing itself. The turn cannot be held open
8736
+ // (nothing names the drain), but a silent close must say the output
8737
+ // may be in the transcript instead of blaming the agent.
8738
+ if (session.ambient === undefined
8739
+ && result.stopReason !== 'cancelled'
8740
+ && responseMeta?.conversationRequestId === undefined
8741
+ && hasRecentRetiredAmbient(session, Math.max(this.tuning.ambientQuietMs * 2, RETIRED_AMBIENT_RECENT_MIN_MS))) {
8742
+ controller.noteRecentRetiredDrain()
8743
+ }
8744
+ controller.settle(result.stopReason === 'cancelled'
6695
8745
  ? { kind: 'cancelled' }
6696
- : { kind: 'stop', stopReason })
8746
+ : { kind: 'stop', stopReason: result.stopReason })
6697
8747
  })
6698
8748
  .catch((err: unknown) => {
6699
8749
  if (session.reclaimedTurnSeq === turnGen) {