switchroom 0.16.47 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/dist/agent-scheduler/index.js +3 -1
  2. package/dist/auth-broker/index.js +24 -8
  3. package/dist/cli/drive-write-pretool.mjs +0 -5
  4. package/dist/cli/notion-write-pretool.mjs +3 -1
  5. package/dist/cli/switchroom.js +1359 -1030
  6. package/dist/cli/ui/index.html +84 -12
  7. package/dist/host-control/main.js +53 -17
  8. package/dist/vault/approvals/kernel-server.js +4 -1
  9. package/dist/vault/broker/server.js +201 -56
  10. package/package.json +3 -3
  11. package/profiles/_base/cron-session.sh.hbs +1 -1
  12. package/profiles/_base/start.sh.hbs +54 -3
  13. package/skills/switchroom-architecture/telegram.md +8 -15
  14. package/skills/switchroom-cli/SKILL.md +4 -5
  15. package/skills/telegram-test-harness/SKILL.md +1 -1
  16. package/telegram-plugin/README.md +18 -29
  17. package/telegram-plugin/bridge/bridge.ts +1 -41
  18. package/telegram-plugin/bridge/tool-filter.ts +3 -4
  19. package/telegram-plugin/dist/bridge/bridge.js +8 -43
  20. package/telegram-plugin/dist/gateway/gateway.js +842 -832
  21. package/telegram-plugin/dist/server.js +8 -43
  22. package/telegram-plugin/format.ts +119 -17
  23. package/telegram-plugin/gateway/approvals-commands.ts +6 -2
  24. package/telegram-plugin/gateway/busy-key-reaper.ts +113 -0
  25. package/telegram-plugin/gateway/disconnect-flush.ts +11 -0
  26. package/telegram-plugin/gateway/escalation-bridge-gate.ts +46 -0
  27. package/telegram-plugin/gateway/gate-parity-probe.ts +102 -0
  28. package/telegram-plugin/gateway/gateway.ts +535 -627
  29. package/telegram-plugin/gateway/inbound-delivery-confirm.ts +89 -7
  30. package/telegram-plugin/gateway/inbound-spool.ts +108 -10
  31. package/telegram-plugin/gateway/model-command.ts +51 -3
  32. package/telegram-plugin/gateway/ms365-write-approval.test.ts +13 -0
  33. package/telegram-plugin/gateway/ms365-write-approval.ts +5 -1
  34. package/telegram-plugin/gateway/pending-inbound-buffer.ts +26 -0
  35. package/telegram-plugin/gateway/represent-guard.ts +28 -11
  36. package/telegram-plugin/gateway/status-pin-store.ts +124 -45
  37. package/telegram-plugin/gateway/vault-request-access-card.ts +5 -1
  38. package/telegram-plugin/gateway/worker-feed-dispatch.ts +19 -0
  39. package/telegram-plugin/history.ts +5 -0
  40. package/telegram-plugin/hooks/silent-end-interrupt-stop.mjs +1 -2
  41. package/telegram-plugin/hooks/subagent-tracker-pretool.mjs +9 -1
  42. package/telegram-plugin/registry/subagents-schema.ts +126 -1
  43. package/telegram-plugin/registry/turns-schema.ts +65 -1
  44. package/telegram-plugin/session-tail.ts +26 -4
  45. package/telegram-plugin/slot-banner-driver.ts +42 -2
  46. package/telegram-plugin/status-query-telemetry.ts +100 -0
  47. package/telegram-plugin/stream-reply-handler.ts +15 -16
  48. package/telegram-plugin/subagent-watcher.ts +182 -30
  49. package/telegram-plugin/tests/buffer-gate-broadened.test.ts +4 -10
  50. package/telegram-plugin/tests/busy-key-reaper.test.ts +191 -0
  51. package/telegram-plugin/tests/emission-authority-facade.test.ts +11 -17
  52. package/telegram-plugin/tests/emission-determinism-wiring.test.ts +5 -26
  53. package/telegram-plugin/tests/escalation-bridge-gate.test.ts +38 -0
  54. package/telegram-plugin/tests/format-consistency.test.ts +79 -0
  55. package/telegram-plugin/tests/gate-parity-probe.test.ts +171 -0
  56. package/telegram-plugin/tests/gateway-disconnect-flush.test.ts +13 -0
  57. package/telegram-plugin/tests/gateway-outbound-redact.test.ts +14 -11
  58. package/telegram-plugin/tests/inbound-delivery-confirm.test.ts +146 -0
  59. package/telegram-plugin/tests/inbound-spool.test.ts +143 -0
  60. package/telegram-plugin/tests/model-command.test.ts +54 -1
  61. package/telegram-plugin/tests/multitopic-routing-wiring.test.ts +5 -11
  62. package/telegram-plugin/tests/nested-worker-visibility-harness.test.ts +329 -0
  63. package/telegram-plugin/tests/pending-inbound-buffer.test.ts +53 -0
  64. package/telegram-plugin/tests/progress-update-redact.test.ts +99 -0
  65. package/telegram-plugin/tests/registry-turns.test.ts +67 -0
  66. package/telegram-plugin/tests/represent-guard.test.ts +42 -6
  67. package/telegram-plugin/tests/resume-inbound-builder.test.ts +1 -0
  68. package/telegram-plugin/tests/session-tail.test.ts +10 -1
  69. package/telegram-plugin/tests/slot-banner-boot-recovery.test.ts +246 -0
  70. package/telegram-plugin/tests/status-pin-boot-recovery.test.ts +0 -14
  71. package/telegram-plugin/tests/status-pin-store.test.ts +220 -5
  72. package/telegram-plugin/tests/status-query-telemetry.test.ts +115 -0
  73. package/telegram-plugin/tests/subagent-nested-dispatch.test.ts +209 -0
  74. package/telegram-plugin/tests/subagent-tracker-hooks.test.ts +37 -0
  75. package/telegram-plugin/tests/subagent-watcher-boot-promotion-replay.test.ts +167 -0
  76. package/telegram-plugin/tests/subagent-watcher-env-thresholds.test.ts +46 -3
  77. package/telegram-plugin/tests/subagent-watcher-stall-notification.test.ts +70 -0
  78. package/telegram-plugin/tests/tool-activity-summary.test.ts +16 -0
  79. package/telegram-plugin/tests/tool-filter.test.ts +1 -3
  80. package/telegram-plugin/tests/tool-label-pretool.test.ts +1 -4
  81. package/telegram-plugin/tests/turn-flush-safety.test.ts +222 -1
  82. package/telegram-plugin/tests/vault-request-access-card.test.ts +17 -0
  83. package/telegram-plugin/tests/welcome-text.test.ts +64 -0
  84. package/telegram-plugin/tests/worker-activity-feed.test.ts +202 -9
  85. package/telegram-plugin/tests/worker-feed-dispatch.test.ts +25 -0
  86. package/telegram-plugin/tests/worker-visibility-prose-silent-harness.test.ts +295 -0
  87. package/telegram-plugin/tool-activity-summary.ts +19 -0
  88. package/telegram-plugin/turn-flush-safety.ts +16 -1
  89. package/telegram-plugin/uat/scenarios/jtbd-answer-pings.test.ts +8 -9
  90. package/telegram-plugin/uat/scenarios/jtbd-foreground-feed-visibility-dm.test.ts +1 -1
  91. package/telegram-plugin/uat/scenarios/jtbd-narration-intent-dm.test.ts +1 -1
  92. package/telegram-plugin/welcome-text.ts +13 -9
  93. package/telegram-plugin/worker-activity-feed.ts +75 -15
  94. package/vendor/hindsight-memory/CHANGELOG.md +66 -0
  95. package/vendor/hindsight-memory/README.md +5 -0
  96. package/vendor/hindsight-memory/scripts/lib/client.py +31 -1
  97. package/vendor/hindsight-memory/scripts/lib/config.py +41 -2
  98. package/vendor/hindsight-memory/scripts/lib/content.py +43 -4
  99. package/vendor/hindsight-memory/scripts/lib/daemon.py +11 -2
  100. package/vendor/hindsight-memory/scripts/recall.py +74 -1
  101. package/vendor/hindsight-memory/scripts/retain.py +79 -11
  102. package/vendor/hindsight-memory/scripts/tests/test_config_client_casts.py +111 -0
  103. package/vendor/hindsight-memory/scripts/tests/test_recall_context_slice.py +126 -0
  104. package/vendor/hindsight-memory/scripts/tests/test_recall_integration.py +85 -1
  105. package/vendor/hindsight-memory/scripts/tests/test_recall_tag_filters.py +107 -0
  106. package/vendor/hindsight-memory/scripts/tests/test_retain_window.py +261 -0
  107. package/vendor/hindsight-memory/settings.json +4 -0
  108. package/vendor/hindsight-memory/tests/test_client.py +130 -0
  109. package/vendor/hindsight-memory/tests/test_config.py +47 -0
  110. package/vendor/hindsight-memory/tests/test_content.py +123 -0
  111. package/vendor/hindsight-memory/tests/test_hooks.py +62 -0
  112. package/telegram-plugin/gateway/error-envelope-card.ts +0 -64
  113. package/telegram-plugin/gateway/resolve-calling-subagent.ts +0 -78
  114. package/telegram-plugin/silent-reply.ts +0 -58
  115. package/telegram-plugin/tests/error-envelope-unlock-card.test.ts +0 -79
  116. package/telegram-plugin/tests/resolve-calling-subagent.test.ts +0 -269
  117. package/telegram-plugin/tests/silent-reply-guard.test.ts +0 -122
@@ -0,0 +1,191 @@
1
+ /**
2
+ * #2787 Mechanism B — gateway-glue coverage for the `claudeBusyKeys` orphan
3
+ * reaper and its lockstep shadow timestamp map.
4
+ *
5
+ * `gateway.ts` cannot be imported by a unit test — it boots the Telegram bot and
6
+ * the IPC listener on module load. So this harness wires the SAME containers the
7
+ * gateway owns (`claudeBusyKeys: Set`, `claudeBusyKeySince: Map`), the SAME
8
+ * delivery-confirm queue used as the reaper's proof gate, and the REAL
9
+ * `flushOnAgentDisconnect` used at bridge death — exactly as the gateway does
10
+ * (see `markClaudeBusyForInbound`, `reapOrphanBusyKeysNow`, the delivery-confirm
11
+ * sweep, and the `flushOnAgentDisconnect({ claudeBusyKeys, claudeBusyKeySince })`
12
+ * call). It reproduces the two real defects the pure-helper tests could not:
13
+ *
14
+ * 1. Lockstep across a disconnect → reconnect → re-mark: a stale
15
+ * pre-disconnect timestamp must NOT survive to make a freshly-marked key
16
+ * look orphan-old.
17
+ * 2. Slow-delivery safety: a merely-slow (up to ~5-min, #1922) delivery whose
18
+ * key is still tracked in the confirm queue must NOT be reaped even long
19
+ * past the grace, because `currentTurn == null` during the eager-mark→
20
+ * enqueue window is not proof of idle.
21
+ */
22
+
23
+ import { describe, it, expect, vi } from 'vitest'
24
+ import {
25
+ markBusyKeyLockstep,
26
+ reapOrphanBusyKeys,
27
+ clearBusyKeyLockstep,
28
+ } from '../gateway/busy-key-reaper.js'
29
+ import { flushOnAgentDisconnect } from '../gateway/disconnect-flush.js'
30
+ import {
31
+ createDeliveryQueue,
32
+ trackDelivery,
33
+ ackDelivery,
34
+ } from '../gateway/inbound-delivery-confirm.js'
35
+
36
+ const TTL = 30_000
37
+
38
+ /** The gateway's `reapOrphanBusyKeysNow` glue, reproduced verbatim: no turn in
39
+ * flight, reap keyed on set membership + a delivery-queue proof gate. */
40
+ function reap(
41
+ keys: Set<string>,
42
+ since: Map<string, number>,
43
+ q: ReturnType<typeof createDeliveryQueue>,
44
+ now: number,
45
+ ttlMs = TTL,
46
+ ): string[] {
47
+ return reapOrphanBusyKeys(keys, since, now, {
48
+ ttlMs,
49
+ hasPendingDelivery: (key) => q.pending.has(key),
50
+ log: () => {},
51
+ })
52
+ }
53
+
54
+ /** Minimal disconnect-flush deps carrying the two containers under test — the
55
+ * real bridge-death path that clears busy keys. Mirrors the gateway's call. */
56
+ function flushDeps(claudeBusyKeys: Set<string>, claudeBusyKeySince: Map<string, number>) {
57
+ return {
58
+ agentName: 'clerk',
59
+ activeStatusReactions: new Map(),
60
+ activeReactionMsgIds: new Map(),
61
+ activeTurnStartedAt: new Map<string, number>(),
62
+ claudeBusyKeys,
63
+ claudeBusyKeySince,
64
+ activeDraftStreams: new Map(),
65
+ clearActiveReactions: vi.fn(),
66
+ disposeProgressDriver: vi.fn(),
67
+ log: vi.fn(),
68
+ }
69
+ }
70
+
71
+ describe('#2787 Mechanism B — orphan reaper (gateway glue)', () => {
72
+ it('does NOT reap a slow-but-real delivery still tracked in the confirm queue, even far past the grace', () => {
73
+ const keys = new Set<string>()
74
+ const since = new Map<string, number>()
75
+ const q = createDeliveryQueue<{ text: string }>()
76
+
77
+ // Eager mark at delivery + track for ack (the normal user-inbound glue).
78
+ markBusyKeyLockstep(keys, since, '-100:4', 0)
79
+ trackDelivery(q, '-100:4', { text: 'slow but real' }, 0, '9001')
80
+
81
+ // currentTurn is still null 6 MINUTES later (the #1922 tail) — but the
82
+ // delivery is still pending its enqueue ack. The proof gate must protect it.
83
+ expect(reap(keys, since, q, 360_000)).toEqual([])
84
+ expect(keys.has('-100:4')).toBe(true)
85
+
86
+ // Once claude finally acks (enqueue lands), the queue entry clears; the
87
+ // gateway would then hold currentTurn and clear busy at turn_end.
88
+ expect(ackDelivery(q, '-100:4', '9001')).toBe(true)
89
+ })
90
+
91
+ it('reaps a TRUE orphan — busy-marked, no pending delivery, past the grace', () => {
92
+ const keys = new Set<string>()
93
+ const since = new Map<string, number>()
94
+ const q = createDeliveryQueue()
95
+
96
+ // A steer/interrupt inbound marks busy but is excluded from tracking
97
+ // (shouldTrackDelivery=false) — no queue entry. Its turn vanished without
98
+ // clearing busy: a genuine stuck marker.
99
+ markBusyKeyLockstep(keys, since, '555:_', 0)
100
+ expect(reap(keys, since, q, TTL)).toEqual(['555:_'])
101
+ expect(keys.has('555:_')).toBe(false)
102
+ expect(since.has('555:_')).toBe(false)
103
+ })
104
+
105
+ it('does not reap before the grace elapses', () => {
106
+ const keys = new Set<string>()
107
+ const since = new Map<string, number>()
108
+ const q = createDeliveryQueue()
109
+ markBusyKeyLockstep(keys, since, '555:_', 0)
110
+ expect(reap(keys, since, q, TTL - 1)).toEqual([])
111
+ expect(keys.has('555:_')).toBe(true)
112
+ })
113
+
114
+ it('disconnect → reconnect → re-mark: fresh timestamp, key NOT reaped as stale (the lockstep fix)', () => {
115
+ const keys = new Set<string>()
116
+ const since = new Map<string, number>()
117
+ const q = createDeliveryQueue()
118
+
119
+ // t=0 first mark.
120
+ markBusyKeyLockstep(keys, since, '-100:4', 0)
121
+
122
+ // Bridge dies at t=1000 — the REAL disconnect flush clears BOTH containers
123
+ // in lockstep (belt-and-suspenders to the set-membership guard).
124
+ flushOnAgentDisconnect(flushDeps(keys, since))
125
+ expect(keys.size).toBe(0)
126
+ expect(since.size).toBe(0)
127
+
128
+ // Reconnect and re-mark the SAME key at t=300_000 (well after the old stamp).
129
+ markBusyKeyLockstep(keys, since, '-100:4', 300_000)
130
+
131
+ // Pre-fix (stale t=0 stamp surviving the disconnect + a `!since.has` guard
132
+ // declining to re-stamp) the age would read 300_000ms ≫ grace and the key
133
+ // would be reaped THE INSTANT it was re-marked — re-opening the idle-drain
134
+ // while claude is about to process this very inbound. With the fix the stamp
135
+ // is fresh (t=300_000), so at t just past the re-mark it is NOT reaped.
136
+ expect(reap(keys, since, q, 305_000)).toEqual([]) // age 5s < 30s grace
137
+ expect(keys.has('-100:4')).toBe(true)
138
+
139
+ // ...and it DOES reap once the fresh grace genuinely elapses.
140
+ expect(reap(keys, since, q, 300_000 + TTL)).toEqual(['-100:4'])
141
+ })
142
+
143
+ it('disconnect while a delivery is pending: reconnect re-mark stays protected by the proof gate', () => {
144
+ const keys = new Set<string>()
145
+ const since = new Map<string, number>()
146
+ const q = createDeliveryQueue<{ text: string }>()
147
+
148
+ markBusyKeyLockstep(keys, since, '-100:4', 0)
149
+ trackDelivery(q, '-100:4', { text: 'inflight' }, 0, '42')
150
+
151
+ // Bridge flap: disconnect clears busy state; the offline buffer / re-deliver
152
+ // loop still owns the inbound. Reconnect re-marks + re-tracks it.
153
+ flushOnAgentDisconnect(flushDeps(keys, since))
154
+ markBusyKeyLockstep(keys, since, '-100:4', 5_000)
155
+ trackDelivery(q, '-100:4', { text: 'inflight' }, 5_000, '42')
156
+
157
+ // Even minutes later, still pending its ack → never reaped.
158
+ expect(reap(keys, since, q, 5_000 + 400_000)).toEqual([])
159
+ expect(keys.has('-100:4')).toBe(true)
160
+ })
161
+
162
+ it('prunes shadow-map entries whose key already left the set (no unbounded growth)', () => {
163
+ const keys = new Set<string>()
164
+ const since = new Map<string, number>()
165
+ const q = createDeliveryQueue()
166
+ // Simulate a direct clear that (hypothetically) left the map behind.
167
+ since.set('ghost:_', 0)
168
+ reap(keys, since, q, 999_999)
169
+ expect(since.has('ghost:_')).toBe(false)
170
+ })
171
+
172
+ it('re-mark of an already-busy key does NOT reset its timestamp (measures the true dangle)', () => {
173
+ const keys = new Set<string>()
174
+ const since = new Map<string, number>()
175
+ const q = createDeliveryQueue()
176
+ markBusyKeyLockstep(keys, since, 'k:_', 0)
177
+ markBusyKeyLockstep(keys, since, 'k:_', 20_000) // still busy → no re-stamp
178
+ expect(since.get('k:_')).toBe(0)
179
+ // Orphaned (no pending delivery) → reaps on the ORIGINAL stamp's grace.
180
+ expect(reap(keys, since, q, TTL)).toEqual(['k:_'])
181
+ })
182
+
183
+ it('clearBusyKeyLockstep removes from both containers', () => {
184
+ const keys = new Set<string>()
185
+ const since = new Map<string, number>()
186
+ markBusyKeyLockstep(keys, since, 'k:_', 0)
187
+ clearBusyKeyLockstep(keys, since, 'k:_')
188
+ expect(keys.has('k:_')).toBe(false)
189
+ expect(since.has('k:_')).toBe(false)
190
+ })
191
+ })
@@ -263,20 +263,21 @@ describe('claimOrDowngradePing — PR-4c over-ping decision moves into the faça
263
263
  expect(blockCode).not.toMatch(/\bawait\b/)
264
264
  })
265
265
 
266
- it('claimOrDowngradePing appears EXACTLY ONCE in the gateway, inside the executeReply window (stream path untouched)', () => {
267
- // The over-ping net exists ONLY in executeReply. executeStreamReply has no
268
- // decideOverPing / firstPingAt / wasOverPingSuppressed and never calls
269
- // claimOrDowngradePing — PR-4c does not touch the stream path.
266
+ it('claimOrDowngradePing appears EXACTLY ONCE in the gateway, inside the executeReply window', () => {
267
+ // The over-ping net exists ONLY in executeReply. The retired stream_reply
268
+ // tool (executeStreamReply) had no decideOverPing / firstPingAt /
269
+ // wasOverPingSuppressed and never called claimOrDowngradePing.
270
270
  const calls = [...gatewaySrc.matchAll(/\.claimOrDowngradePing\(/g)]
271
271
  expect(calls).toHaveLength(1)
272
272
  const callIdx = gatewaySrc.indexOf('.claimOrDowngradePing(')
273
273
  const execReplyIdx = gatewaySrc.indexOf('async function executeReply(')
274
- const execStreamIdx = gatewaySrc.indexOf('async function executeStreamReply(')
274
+ // Upper bound: the next top-level function after executeReply.
275
+ const nextFnIdx = gatewaySrc.indexOf('\nasync function ', execReplyIdx + 1)
275
276
  expect(execReplyIdx).toBeGreaterThan(-1)
276
- expect(execStreamIdx).toBeGreaterThan(execReplyIdx)
277
- // The single call is inside executeReply (before executeStreamReply starts).
277
+ expect(nextFnIdx).toBeGreaterThan(execReplyIdx)
278
+ // The single call is inside executeReply.
278
279
  expect(callIdx).toBeGreaterThan(execReplyIdx)
279
- expect(callIdx).toBeLessThan(execStreamIdx)
280
+ expect(callIdx).toBeLessThan(nextFnIdx)
280
281
  })
281
282
  })
282
283
 
@@ -370,20 +371,13 @@ describe('the 7 drain sites route through the façade with producers preserved v
370
371
  })
371
372
  })
372
373
 
373
- describe('the 2 lever-2 finalize blocks route through the façade', () => {
374
+ describe('the lever-2 finalize block routes through the façade', () => {
374
375
  it('executeReply finalize routes via markSubstantiveFinalDelivered + finalizeCard (latch + clear preserved)', () => {
375
376
  const after = gatewaySrc.split('async function executeReply(')[1] ?? ''
376
- const body = after.split('async function executeStreamReply(')[0] ?? after
377
+ const body = after.split('\nasync function ')[0]?.split('\nfunction ')[0] ?? after
377
378
  expect(body).toMatch(/markSubstantiveFinalDelivered\(\(\) => \{\s*\n\s*finalizeTurn\.finalAnswerEverDelivered = true/)
378
379
  expect(body).toMatch(/finalizeCard\(\(\) => \{\s*\n\s*clearActivitySummary\(finalizeTurn\)/)
379
380
  })
380
-
381
- it('executeStreamReply finalize routes via markSubstantiveFinalDelivered + finalizeCard (latch + clear preserved)', () => {
382
- const after = gatewaySrc.split('async function executeStreamReply(')[1] ?? ''
383
- const body = after.split('\nasync function ')[0]?.split('\nfunction ')[0] ?? after
384
- expect(body).toMatch(/markSubstantiveFinalDelivered\(\(\) => \{\s*\n\s*turn\.finalAnswerEverDelivered = true/)
385
- expect(body).toMatch(/finalizeCard\(\(\) => \{\s*\n\s*clearActivitySummary\(turn\)/)
386
- })
387
381
  })
388
382
 
389
383
  describe('per-turn construction — one façade per turn, explicit chat/thread key (PR-4e seam)', () => {
@@ -52,8 +52,8 @@ describe('sticky finalAnswerEverDelivered latch (lever 1 precondition / R0)', ()
52
52
  // The latch is set true only at the points that set finalAnswerDelivered=true,
53
53
  // and only when the reply was substantive — so an ack never latches it.
54
54
  const setTrue = [...gatewaySrc.matchAll(/finalAnswerEverDelivered\s*=\s*true/g)]
55
- // executeReply, executeStreamReply, silent-anchor merge, + the two lever-2
56
- // finalize blocks (which are themselves substantive-gated).
55
+ // executeReply, silent-anchor merge, + the lever-2 finalize block
56
+ // (which is itself substantive-gated).
57
57
  expect(setTrue.length).toBeGreaterThanOrEqual(3)
58
58
  // Each `finalAnswerEverDelivered = true` must sit in a substantive context:
59
59
  // either guarded by `if (turn.finalAnswerSubstantive)` or inside an
@@ -125,14 +125,9 @@ describe('drain producers — narrative may not OPEN, liveness + tool may', () =
125
125
  })
126
126
 
127
127
  describe('lever 2 — finalize the card BEFORE a substantive reply send', () => {
128
- /** executeReply body up to executeStreamReply. */
128
+ /** executeReply body up to the next top-level function. */
129
129
  function executeReplySrc(): string {
130
130
  const after = gatewaySrc.split('async function executeReply(')[1] ?? ''
131
- return after.split('async function executeStreamReply(')[0] ?? after
132
- }
133
- /** executeStreamReply body up to the next top-level function. */
134
- function executeStreamReplySrc(): string {
135
- const after = gatewaySrc.split('async function executeStreamReply(')[1] ?? ''
136
131
  return after.split('\nasync function ')[0]?.split('\nfunction ')[0] ?? after
137
132
  }
138
133
 
@@ -148,27 +143,11 @@ describe('lever 2 — finalize the card BEFORE a substantive reply send', () =>
148
143
  expect(window).toMatch(/isSubstantiveFinalReply/)
149
144
  })
150
145
 
151
- it('executeStreamReply finalizes before handleStreamReply, gated on substantive', () => {
152
- const src = executeStreamReplySrc()
153
- const clearIdx = src.indexOf('clearActivitySummary(')
154
- const sendIdx = src.indexOf('const result = await handleStreamReply(')
155
- expect(clearIdx).toBeGreaterThan(-1)
156
- expect(sendIdx).toBeGreaterThan(-1)
157
- expect(clearIdx).toBeLessThan(sendIdx)
158
- // Window extended to 600 chars to account for the finalAnswerDeliveredAt stamp
159
- // added inside the markSubstantiveFinalDelivered callback (Fix 2 / #2587).
160
- const window = src.slice(Math.max(0, clearIdx - 600), clearIdx)
161
- expect(window).toMatch(/isSubstantiveFinalReply/)
162
- })
163
-
164
146
  it('acks do NOT finalize early — no unconditional clearActivitySummary before the reply send', () => {
165
- // Both lever-2 finalize sites sit inside an isSubstantiveFinalReply guard.
147
+ // The lever-2 finalize site sits inside an isSubstantiveFinalReply guard.
166
148
  // An ack (non-substantive) falls through and never finalizes early, so the
167
149
  // reopen path keeps owning the card (the #2141 ack-then-work feed).
168
- const replySrc = (() => {
169
- const after = gatewaySrc.split('async function executeReply(')[1] ?? ''
170
- return after.split('async function executeStreamReply(')[0] ?? after
171
- })()
150
+ const replySrc = executeReplySrc()
172
151
  // The pre-loop clearActivitySummary must be the substantive-gated one.
173
152
  const preLoop = replySrc.split('for (let i = 0; i < chunks.length')[0] ?? ''
174
153
  const clears = [...preLoop.matchAll(/clearActivitySummary\(/g)]
@@ -0,0 +1,38 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { shouldDeferEscalationForBridge } from "../gateway/escalation-bridge-gate.js";
3
+
4
+ // Executable verification of #2788 Gap A: the obligation sweep's escalate branch
5
+ // DIRECT-SENDS (bypassing the bridge), so during a transient bridge outage it
6
+ // could fire a false "I may have missed this" even though the real reply is just
7
+ // queued behind the flap. The gate defers the nudge while the bridge is down;
8
+ // obligations survive bridge death, so the deferral is safe and self-healing.
9
+
10
+ describe("shouldDeferEscalationForBridge — #2788 Gap A bridge-flap gate", () => {
11
+ it("DEFERS escalation while the bridge is down (no false 'I may have missed this')", () => {
12
+ expect(shouldDeferEscalationForBridge({ bridgeAlive: false })).toBe(true);
13
+ });
14
+
15
+ it("proceeds with escalation when the bridge is alive", () => {
16
+ expect(shouldDeferEscalationForBridge({ bridgeAlive: true })).toBe(false);
17
+ });
18
+
19
+ it("models the gateway wiring: getClient(agent)?.isAlive() === true drives the gate", () => {
20
+ // Bridge registered + alive → do not defer.
21
+ const aliveClient = { isAlive: () => true };
22
+ expect(
23
+ shouldDeferEscalationForBridge({ bridgeAlive: aliveClient.isAlive() === true }),
24
+ ).toBe(false);
25
+
26
+ // Bridge registered but a stale/dead socket → defer.
27
+ const deadClient = { isAlive: () => false };
28
+ expect(
29
+ shouldDeferEscalationForBridge({ bridgeAlive: deadClient.isAlive() === true }),
30
+ ).toBe(true);
31
+
32
+ // No client registered at all (getClient returns undefined) → defer.
33
+ const noClient: { isAlive: () => boolean } | undefined = undefined;
34
+ expect(
35
+ shouldDeferEscalationForBridge({ bridgeAlive: noClient?.isAlive() === true }),
36
+ ).toBe(true);
37
+ });
38
+ });
@@ -16,11 +16,90 @@ import {
16
16
  normalizePunctuation,
17
17
  stripExcessBold,
18
18
  splitMarkdownChunks,
19
+ hardenCardBreaks,
19
20
  PARAGRAPH_SPACER,
20
21
  } from '../format.js'
21
22
 
22
23
  const SP = PARAGRAPH_SPACER // U+00A0
23
24
 
25
+ describe('hardenCardBreaks — deterministic card line-break hardener', () => {
26
+ test('promotes lone field breaks to GFM hard breaks (the blob fix)', () => {
27
+ const out = hardenCardBreaks('Agent: assistant\nAuth: Max\nStatus: running')
28
+ expect(out).toBe('Agent: assistant \nAuth: Max \nStatus: running')
29
+ })
30
+
31
+ test('preserves `\\n\\n` block gaps (not promoted)', () => {
32
+ const out = hardenCardBreaks('**Header**\nfield one\n\n**Next**\nfield two')
33
+ expect(out).toBe('**Header** \nfield one\n\n**Next** \nfield two')
34
+ })
35
+
36
+ test('leaves GFM list items on their native single `\\n`', () => {
37
+ const out = hardenCardBreaks('- one\n- two\n- three')
38
+ expect(out).toBe('- one\n- two\n- three')
39
+ })
40
+
41
+ test('leaves GFM table rows untouched', () => {
42
+ const src = '| a | b |\n| - | - |\n| 1 | 2 |'
43
+ expect(hardenCardBreaks(src)).toBe(src)
44
+ })
45
+
46
+ test('never touches a fenced code block interior', () => {
47
+ const src = '**Accounts**\n```\nalice ok\nbob ok\n```\n**Agents**'
48
+ // The fenced monospace table keeps its single `\n`s; the header lines that
49
+ // face the fence are not hard-broken (fence line is a block construct).
50
+ expect(hardenCardBreaks(src)).toBe(src)
51
+ })
52
+
53
+ test('does not hard-break across a heading or blockquote', () => {
54
+ expect(hardenCardBreaks('# Title\nbody')).toBe('# Title\nbody')
55
+ expect(hardenCardBreaks('> quote\nbody')).toBe('> quote\nbody')
56
+ })
57
+
58
+ test('collapses 3+ newline runs to a single `\\n\\n` gap', () => {
59
+ expect(hardenCardBreaks('a\n\n\n\nb')).toBe('a\n\nb')
60
+ })
61
+
62
+ test('is idempotent', () => {
63
+ const src = 'Agent: x\nAuth: y\n\n**H**\n🟢 Broker running\n🟢 Kernel up'
64
+ const once = hardenCardBreaks(src)
65
+ expect(hardenCardBreaks(once)).toBe(once)
66
+ })
67
+
68
+ test('no-op for single-line text', () => {
69
+ expect(hardenCardBreaks('just one line')).toBe('just one line')
70
+ })
71
+
72
+ // Regression (reviewer nit): a field line that STARTS with an inline code
73
+ // span used to be misclassified as a masked fenced-block open (fenced +
74
+ // inline masks shared one placeholder prefix), so its lone `\n` was never
75
+ // hardened and the card collapsed. Real victim: `/vault get` rendering
76
+ // `` `key` = `value` `` on one line.
77
+ test('hardens a line that STARTS with an inline code span', () => {
78
+ const out = hardenCardBreaks('`key` = `value`\n`k2` = `v2`')
79
+ expect(out).toBe('`key` = `value` \n`k2` = `v2`')
80
+ })
81
+
82
+ test('/vault get shape (`key` =\\n`value`) hard-breaks onto two lines', () => {
83
+ const out = hardenCardBreaks('`sk-key` =\n`hunter2`')
84
+ expect(out).toBe('`sk-key` = \n`hunter2`')
85
+ })
86
+
87
+ test('inline-span-leading fix does NOT disturb a real fenced block', () => {
88
+ // A genuine ``` fence between two inline-span-leading field lines: the
89
+ // field lines harden, the fence interior stays byte-for-byte intact.
90
+ const src = '`a` = 1\n```\nx = 1\ny = 2\n```\n`b` = 2'
91
+ const out = hardenCardBreaks(src)
92
+ expect(out).toContain('```\nx = 1\ny = 2\n```') // fence interior untouched
93
+ expect(out).not.toContain('x = 1 \n') // no hard break injected inside fence
94
+ })
95
+
96
+ test('mid-line inline spans still harden (unchanged behaviour)', () => {
97
+ expect(hardenCardBreaks('Model: `opus`\nAuth: `Max`')).toBe(
98
+ 'Model: `opus` \nAuth: `Max`',
99
+ )
100
+ })
101
+ })
102
+
24
103
  describe('addParagraphSpacers — uniform block spacing', () => {
25
104
  test('still spaces prose→prose (existing behaviour)', () => {
26
105
  const out = addParagraphSpacers('Alpha.\n\nBravo.')
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Drift-catching scaffolding for the inbound-delivery state-machine
3
+ * cutover (#2794). The turn-in-flight gate is now machine-authoritative
4
+ * (`isMachineInTurn`), but the legacy imperative `claudeBusyKeys` set is
5
+ * still maintained in parallel. These tests assert the two AGREE on every
6
+ * well-formed turn schedule, and pin the ONE intended divergence (the
7
+ * orphan-dangle the machine self-heals) so future edits can't silently
8
+ * introduce a dangerous `machine_over_holds` drift without turning a test
9
+ * red.
10
+ *
11
+ * This is the "assertion that machine and shadow agree" from the #2794
12
+ * time-box plan: it turns the standing triple-maintenance drift risk into
13
+ * a CI gate.
14
+ */
15
+
16
+ import { describe, expect, it, beforeEach } from 'vitest'
17
+ import {
18
+ shadowEmit,
19
+ isMachineInTurn,
20
+ __shadowResetForTests,
21
+ } from '../gateway/inbound-delivery-machine-shadow.js'
22
+ import { TURN_TTL_MS, type ChatKey } from '../gateway/inbound-delivery-machine.js'
23
+ import {
24
+ gateParityDivergence,
25
+ isDangerousGateDivergence,
26
+ probeGateParity,
27
+ } from '../gateway/gate-parity-probe.js'
28
+
29
+ const KEY_A = '111:_' as ChatKey
30
+ const KEY_B = '222:_' as ChatKey
31
+
32
+ /**
33
+ * Reference model of the imperative `claudeBusyKeys` set — a per-delivery
34
+ * Set stamped on a fresh-turn delivery and deleted on the matching
35
+ * turnEnd. This mirrors gateway.ts's markBusyKeyLockstep / claudeBusyKeys.delete
36
+ * lifecycle closely enough to detect real drift in the gate view.
37
+ */
38
+ class BusyKeysModel {
39
+ private readonly keys = new Set<string>()
40
+ stampFreshTurn(key: string) {
41
+ this.keys.add(key)
42
+ }
43
+ endTurn(key: string) {
44
+ this.keys.delete(key)
45
+ }
46
+ get size() {
47
+ return this.keys.size
48
+ }
49
+ }
50
+
51
+ describe('gateParityDivergence classifier', () => {
52
+ it('agree → none (both idle, both busy)', () => {
53
+ expect(gateParityDivergence(false, 0)).toBe('none')
54
+ expect(gateParityDivergence(true, 1)).toBe('none')
55
+ expect(gateParityDivergence(true, 3)).toBe('none')
56
+ })
57
+
58
+ it('machine idle, busyKeys held → busykeys_dangle (benign self-heal)', () => {
59
+ expect(gateParityDivergence(false, 1)).toBe('busykeys_dangle')
60
+ expect(isDangerousGateDivergence('busykeys_dangle')).toBe(false)
61
+ })
62
+
63
+ it('machine in-flight, busyKeys empty → machine_over_holds (dangerous)', () => {
64
+ expect(gateParityDivergence(true, 0)).toBe('machine_over_holds')
65
+ expect(isDangerousGateDivergence('machine_over_holds')).toBe(true)
66
+ })
67
+
68
+ it('probeGateParity returns the machine value unchanged (no behaviour change)', () => {
69
+ const lines: string[] = []
70
+ const log = (l: string) => lines.push(l)
71
+ expect(probeGateParity(true, 1, log)).toBe(true)
72
+ expect(probeGateParity(false, 0, log)).toBe(false)
73
+ expect(probeGateParity(false, 1, log)).toBe(false)
74
+ expect(probeGateParity(true, 0, log)).toBe(true)
75
+ // Only the dangerous over-hold emits a drift line.
76
+ expect(lines).toHaveLength(1)
77
+ expect(lines[0]).toContain('gw-trace gate-drift kind=machine_over_holds')
78
+ })
79
+ })
80
+
81
+ describe('machine ↔ claudeBusyKeys parity on well-formed schedules', () => {
82
+ beforeEach(() => __shadowResetForTests())
83
+
84
+ it('single turn: agree at every step', () => {
85
+ const busy = new BusyKeysModel()
86
+ shadowEmit({ kind: 'bridgeUp', at: 1000 })
87
+ expect(gateParityDivergence(isMachineInTurn(), busy.size)).toBe('none')
88
+
89
+ // Fresh inbound → machine goes in_turn; imperative stamps the key.
90
+ shadowEmit({ kind: 'inbound', key: KEY_A, msg: { msgId: 1, isSteering: false, payload: null }, at: 2000 })
91
+ busy.stampFreshTurn(KEY_A)
92
+ expect(gateParityDivergence(isMachineInTurn(), busy.size)).toBe('none')
93
+ expect(isMachineInTurn()).toBe(true)
94
+
95
+ // turnEnd → both reopen.
96
+ shadowEmit({ kind: 'turnEnd', key: KEY_A, at: 3000, outboundEmitted: true })
97
+ busy.endTurn(KEY_A)
98
+ expect(gateParityDivergence(isMachineInTurn(), busy.size)).toBe('none')
99
+ expect(isMachineInTurn()).toBe(false)
100
+ })
101
+
102
+ it('sequential turns across two chats: no drift', () => {
103
+ const busy = new BusyKeysModel()
104
+ shadowEmit({ kind: 'bridgeUp', at: 1000 })
105
+
106
+ const schedule: Array<[ChatKey, number]> = [
107
+ [KEY_A, 2000],
108
+ [KEY_B, 4000],
109
+ [KEY_A, 6000],
110
+ ]
111
+ let t = 2000
112
+ for (const [key, start] of schedule) {
113
+ shadowEmit({ kind: 'inbound', key, msg: { msgId: t, isSteering: false, payload: null }, at: start })
114
+ busy.stampFreshTurn(key)
115
+ expect(gateParityDivergence(isMachineInTurn(), busy.size)).toBe('none')
116
+ shadowEmit({ kind: 'turnEnd', key, at: start + 500, outboundEmitted: true })
117
+ busy.endTurn(key)
118
+ expect(gateParityDivergence(isMachineInTurn(), busy.size)).toBe('none')
119
+ t = start + 500
120
+ }
121
+ })
122
+
123
+ it('mid-turn sibling inbound is buffered, not a new turn: parity holds', () => {
124
+ const busy = new BusyKeysModel()
125
+ shadowEmit({ kind: 'bridgeUp', at: 1000 })
126
+
127
+ shadowEmit({ kind: 'inbound', key: KEY_A, msg: { msgId: 1, isSteering: false, payload: null }, at: 2000 })
128
+ busy.stampFreshTurn(KEY_A)
129
+ // Sibling arrives mid-turn: machine buffers (no new activeTurn); the
130
+ // imperative gate is already busy so it does NOT stamp a fresh key.
131
+ shadowEmit({ kind: 'inbound', key: KEY_B, msg: { msgId: 2, isSteering: false, payload: null }, at: 2500 })
132
+ expect(gateParityDivergence(isMachineInTurn(), busy.size)).toBe('none')
133
+
134
+ shadowEmit({ kind: 'turnEnd', key: KEY_A, at: 3000, outboundEmitted: true })
135
+ busy.endTurn(KEY_A)
136
+ expect(gateParityDivergence(isMachineInTurn(), busy.size)).toBe('none')
137
+ })
138
+ })
139
+
140
+ describe('the ONE intended divergence: orphaned turnStart', () => {
141
+ beforeEach(() => __shadowResetForTests())
142
+
143
+ it('machine self-heals the dangle; imperative set holds the orphan (busykeys_dangle, benign)', () => {
144
+ const busy = new BusyKeysModel()
145
+ shadowEmit({ kind: 'bridgeUp', at: 1000 })
146
+
147
+ // Turn A opens; turn B opens before A's turnEnd ever lands. The
148
+ // imperative set stamps BOTH; only B's turnEnd arrives, leaving A as
149
+ // an orphan key — the gymbro/clerk 5-min dangle.
150
+ shadowEmit({ kind: 'turnStart', key: KEY_A, at: 2000 })
151
+ busy.stampFreshTurn(KEY_A)
152
+ shadowEmit({ kind: 'turnStart', key: KEY_B, at: 3000 })
153
+ busy.stampFreshTurn(KEY_B)
154
+ shadowEmit({ kind: 'turnEnd', key: KEY_B, at: 4000, outboundEmitted: true })
155
+ busy.endTurn(KEY_B)
156
+
157
+ // Machine still holds activeTurn=A (single-activeTurn); imperative
158
+ // holds A too → they agree here.
159
+ expect(gateParityDivergence(isMachineInTurn(), busy.size)).toBe('none')
160
+
161
+ // TTL tick past A's start: the machine self-heals to idle, but the
162
+ // imperative set NEVER gets A's turnEnd → orphan dangles forever.
163
+ shadowEmit({ kind: 'tick', now: 2000 + TURN_TTL_MS + 1 })
164
+ expect(isMachineInTurn()).toBe(false)
165
+ const d = gateParityDivergence(isMachineInTurn(), busy.size)
166
+ expect(d).toBe('busykeys_dangle')
167
+ // Benign: this is exactly the wedge the machine was made authoritative
168
+ // to kill — it must NOT trip the drift alarm.
169
+ expect(isDangerousGateDivergence(d)).toBe(false)
170
+ })
171
+ })