switchroom 0.17.10 → 0.18.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/bin/workspace-dynamic-hook.sh +12 -13
- package/dist/agent-scheduler/index.js +27 -1
- package/dist/auth-broker/index.js +6161 -151
- package/dist/cli/notion-write-pretool.mjs +29 -2
- package/dist/cli/switchroom.js +578 -454
- package/dist/host-control/main.js +6182 -172
- package/dist/vault/approvals/kernel-server.js +5891 -164
- package/dist/vault/broker/server.js +6597 -881
- package/package.json +1 -1
- package/profiles/_base/settings.json.hbs +2 -2
- package/profiles/_base/start.sh.hbs +170 -21
- package/profiles/coding/CLAUDE.md.hbs +1 -1
- package/profiles/default/CLAUDE.md +2 -2
- package/profiles/default/CLAUDE.md.hbs +2 -2
- package/profiles/executive-assistant/CLAUDE.md.hbs +1 -1
- package/profiles/health-coach/CLAUDE.md.hbs +1 -1
- package/telegram-plugin/auth-snapshot-format.ts +22 -24
- package/telegram-plugin/context-exhaustion.ts +124 -0
- package/telegram-plugin/dist/gateway/gateway.js +24086 -8727
- package/telegram-plugin/gateway/activity-card-store.ts +76 -0
- package/telegram-plugin/gateway/gateway.ts +480 -85
- package/telegram-plugin/gateway/inbound-delivery-gate.ts +26 -0
- package/telegram-plugin/gateway/model-command.ts +70 -10
- package/telegram-plugin/package.json +6 -0
- package/telegram-plugin/quota-watch.ts +4 -6
- package/telegram-plugin/registry/turns-schema.test.ts +97 -0
- package/telegram-plugin/registry/turns-schema.ts +78 -0
- package/telegram-plugin/render/ir.ts +209 -0
- package/telegram-plugin/render/parse.ts +363 -0
- package/telegram-plugin/render/render.ts +440 -0
- package/telegram-plugin/render/rich-render.ts +72 -0
- package/telegram-plugin/stream-controller.ts +14 -3
- package/telegram-plugin/tests/activity-card-store.test.ts +94 -0
- package/telegram-plugin/tests/auth-command-format2.test.ts +1 -1
- package/telegram-plugin/tests/auth-snapshot-format.test.ts +30 -16
- package/telegram-plugin/tests/claude-code-event-contract.test.ts +48 -0
- package/telegram-plugin/tests/feed-heartbeat-liveness-open.test.ts +11 -0
- package/telegram-plugin/tests/feed-survival.test.ts +39 -0
- package/telegram-plugin/tests/gateway-session-model-relaunch.test.ts +81 -0
- package/telegram-plugin/tests/inbound-emit-after-intercepts.test.ts +82 -0
- package/telegram-plugin/tests/liveness-tracker.test.ts +228 -0
- package/telegram-plugin/tests/model-command.test.ts +193 -16
- package/telegram-plugin/tests/narrative-render.test.ts +125 -0
- package/telegram-plugin/tests/orphaned-reply-rearm.test.ts +123 -163
- package/telegram-plugin/tests/quota-watch.test.ts +1 -4
- package/telegram-plugin/tests/rapid-fire-delivery-ordering.test.ts +149 -0
- package/telegram-plugin/tests/render/parse-torture.test.ts +136 -0
- package/telegram-plugin/tests/render/parse.test.ts +393 -0
- package/telegram-plugin/tests/render/render.test.ts +436 -0
- package/telegram-plugin/tests/render/rich-render.test.ts +85 -0
- package/telegram-plugin/tests/telegram-activity-visibility-integration.test.ts +155 -1
- package/telegram-plugin/tests/worktree-watch-cwds.test.ts +98 -3
- package/telegram-plugin/turn-liveness-floor.ts +35 -1
- package/telegram-plugin/uat/scenarios/jtbd-rich-formatting-render-dm.test.ts +99 -7
- package/telegram-plugin/worktree-watch-cwds.ts +92 -17
- package/vendor/hindsight-memory/scripts/lib/client.py +11 -1
- package/vendor/hindsight-memory/scripts/lib/config.py +9 -2
- package/vendor/hindsight-memory/scripts/recall.py +64 -6
- package/vendor/hindsight-memory/scripts/tests/test_recall_integration.py +1 -0
- package/vendor/hindsight-memory/tests/test_client.py +43 -0
- package/vendor/hindsight-memory/tests/test_recall_precision.py +114 -0
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Unit tests for the
|
|
2
|
+
* Unit tests for the orphaned-reply backstop, repointed at the REAL
|
|
3
|
+
* LivenessTracker (the thinking-pause fix seam) — no replica decision helpers.
|
|
3
4
|
*
|
|
4
5
|
* Root cause: the orphaned-reply backstop fired a synthetic turn_end
|
|
5
|
-
* (`durationMs: -1`) after 30 s of silence, even mid-
|
|
6
|
+
* (`durationMs: -1`) after 30 s of silence, even mid-work. That nulled
|
|
6
7
|
* `currentTurn` and dropped every subsequent `tool_label`, darkening the live
|
|
7
8
|
* activity feed for the rest of the turn.
|
|
8
9
|
*
|
|
9
|
-
* Fix
|
|
10
|
-
* PRIMARY — fuse fires mid-
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
10
|
+
* Fix layers (all now expressed through `LivenessTracker`):
|
|
11
|
+
* PRIMARY — the fuse fires mid-work → re-arm instead. `decideOnExpiry`
|
|
12
|
+
* re-arms while working OR recently-streaming OR human-waiting,
|
|
13
|
+
* bounded by ORPHANED_REPLY_MAX_REARMS for the working /
|
|
14
|
+
* recently-streaming cases (human-wait is uncapped).
|
|
15
|
+
* SECONDARY — a genuine stream event (`onStreamEvent`) re-stamps liveness and
|
|
16
|
+
* zeroes the rearm counter, so an actively-progressing turn never
|
|
17
|
+
* hits the cap.
|
|
18
|
+
* DEFENSIVE — the gateway rejects a synthetic turn_end while the turn is
|
|
19
|
+
* legitimately working OR recently streaming (`recentlyStreaming`).
|
|
19
20
|
*/
|
|
20
21
|
|
|
21
22
|
import { describe, it, expect } from 'vitest'
|
|
@@ -23,37 +24,24 @@ import {
|
|
|
23
24
|
shouldArmOrphanedReplyTimeout,
|
|
24
25
|
ORPHANED_REPLY_TIMEOUT_MS,
|
|
25
26
|
ORPHANED_REPLY_MAX_REARMS,
|
|
27
|
+
ORPHANED_REPLY_STREAM_WINDOW_MS,
|
|
28
|
+
LivenessTracker,
|
|
26
29
|
} from '../context-exhaustion.js'
|
|
27
30
|
import { ToolFlightTracker } from '../gateway/interrupt-defer.js'
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
// These extract the discriminable parts of the fix so they are unit-testable
|
|
32
|
-
// without instantiating the full gateway.
|
|
33
|
-
// ---------------------------------------------------------------------------
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Mirrors the PRIMARY fix decision inside the setTimeout callback:
|
|
37
|
-
* should the backstop re-arm (true) or fire turn_end (false)?
|
|
38
|
-
*/
|
|
39
|
-
function shouldRearmInsteadOfFire(opts: {
|
|
40
|
-
midToolCall: boolean
|
|
41
|
-
rearmCount: number
|
|
42
|
-
maxRearms: number
|
|
43
|
-
}): boolean {
|
|
44
|
-
return opts.midToolCall && opts.rearmCount < opts.maxRearms
|
|
45
|
-
}
|
|
32
|
+
const W = ORPHANED_REPLY_STREAM_WINDOW_MS
|
|
33
|
+
const MAX = ORPHANED_REPLY_MAX_REARMS
|
|
46
34
|
|
|
47
35
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
36
|
+
* A tracker whose last stream event is far enough in the past that
|
|
37
|
+
* `recentlyStreaming` is false at `now`, so the ONLY thing keeping the turn
|
|
38
|
+
* alive in a `decideOnExpiry` call is the `working` / `humanWaiting` inputs.
|
|
39
|
+
* Lets us test the "no liveness except the tool" decisions in isolation.
|
|
50
40
|
*/
|
|
51
|
-
function
|
|
52
|
-
|
|
53
|
-
midToolCall: boolean
|
|
54
|
-
}): boolean {
|
|
55
|
-
return opts.durationMs === -1 && opts.midToolCall
|
|
41
|
+
function staleTracker(): LivenessTracker {
|
|
42
|
+
return new LivenessTracker(0)
|
|
56
43
|
}
|
|
44
|
+
const STALE_NOW = W + 10_000 // gap > window → recentlyStreaming false
|
|
57
45
|
|
|
58
46
|
// ---------------------------------------------------------------------------
|
|
59
47
|
// Tests: ORPHANED_REPLY_MAX_REARMS constant
|
|
@@ -66,7 +54,6 @@ describe('ORPHANED_REPLY_MAX_REARMS', () => {
|
|
|
66
54
|
|
|
67
55
|
it('combined with ORPHANED_REPLY_TIMEOUT_MS covers at least 10 min of tool activity', () => {
|
|
68
56
|
const coverageMs = ORPHANED_REPLY_MAX_REARMS * ORPHANED_REPLY_TIMEOUT_MS
|
|
69
|
-
// 20 × 30 000 ms = 600 000 ms = 10 min
|
|
70
57
|
expect(coverageMs).toBeGreaterThanOrEqual(10 * 60 * 1000)
|
|
71
58
|
})
|
|
72
59
|
|
|
@@ -76,165 +63,138 @@ describe('ORPHANED_REPLY_MAX_REARMS', () => {
|
|
|
76
63
|
})
|
|
77
64
|
|
|
78
65
|
// ---------------------------------------------------------------------------
|
|
79
|
-
// Tests: PRIMARY fix — re-arm
|
|
66
|
+
// Tests: PRIMARY fix — re-arm via decideOnExpiry (REAL tracker)
|
|
80
67
|
// ---------------------------------------------------------------------------
|
|
81
68
|
|
|
82
|
-
describe('PRIMARY fix: re-arm guard (
|
|
83
|
-
it('re-arms
|
|
84
|
-
|
|
85
|
-
|
|
69
|
+
describe('PRIMARY fix: re-arm guard (LivenessTracker.decideOnExpiry)', () => {
|
|
70
|
+
it('re-arms while working and under the cap (recentlyStreaming false)', () => {
|
|
71
|
+
const t = staleTracker()
|
|
72
|
+
const d = t.decideOnExpiry({ working: true, humanWaiting: false, now: STALE_NOW, windowMs: W, maxRearms: MAX })
|
|
73
|
+
expect(d).toEqual({ rearm: true, countsAgainstCap: true })
|
|
86
74
|
})
|
|
87
75
|
|
|
88
|
-
it('fires once rearm count reaches the cap
|
|
89
|
-
|
|
90
|
-
|
|
76
|
+
it('fires once the working rearm count reaches the cap (recentlyStreaming false)', () => {
|
|
77
|
+
const t = staleTracker()
|
|
78
|
+
let last: { rearm: boolean } | undefined
|
|
79
|
+
for (let k = 0; k <= MAX; k++) {
|
|
80
|
+
last = t.decideOnExpiry({ working: true, humanWaiting: false, now: STALE_NOW, windowMs: W, maxRearms: MAX })
|
|
81
|
+
}
|
|
82
|
+
// MAX re-arms proceeded (counter 1..MAX); the (MAX+1)th fires.
|
|
83
|
+
expect(last!.rearm).toBe(false)
|
|
84
|
+
expect(t.orphanedReplyRearmCount).toBe(MAX)
|
|
91
85
|
})
|
|
92
86
|
|
|
93
|
-
it('fires immediately when
|
|
94
|
-
|
|
95
|
-
|
|
87
|
+
it('fires immediately when nothing keeps the turn alive (idle, not recently streaming)', () => {
|
|
88
|
+
const t = staleTracker()
|
|
89
|
+
const d = t.decideOnExpiry({ working: false, humanWaiting: false, now: STALE_NOW, windowMs: W, maxRearms: MAX })
|
|
90
|
+
expect(d).toEqual({ rearm: false, countsAgainstCap: false })
|
|
96
91
|
})
|
|
97
92
|
|
|
98
|
-
it('
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
// At exactly the cap: fire
|
|
104
|
-
expect(shouldRearmInsteadOfFire({ midToolCall: true, rearmCount: max, maxRearms: max })).toBe(false)
|
|
93
|
+
it('re-arms while recently streaming even when NOT working (thinking-pause survival)', () => {
|
|
94
|
+
const t = new LivenessTracker(0)
|
|
95
|
+
t.onStreamEvent('text', undefined, 0)
|
|
96
|
+
const d = t.decideOnExpiry({ working: false, humanWaiting: false, now: 30_000, windowMs: W, maxRearms: MAX })
|
|
97
|
+
expect(d).toEqual({ rearm: true, countsAgainstCap: true })
|
|
105
98
|
})
|
|
106
99
|
})
|
|
107
100
|
|
|
108
101
|
// ---------------------------------------------------------------------------
|
|
109
|
-
// Tests: DEFENSIVE fix — synthetic turn_end
|
|
102
|
+
// Tests: DEFENSIVE fix — recentlyStreaming keeps the synthetic turn_end at bay
|
|
110
103
|
// ---------------------------------------------------------------------------
|
|
111
104
|
|
|
112
|
-
describe('DEFENSIVE fix:
|
|
113
|
-
it('
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
// No tools → the backstop should fire normally (turn is genuinely orphaned)
|
|
119
|
-
expect(shouldSuppressSyntheticTurnEnd({ durationMs: -1, midToolCall: false })).toBe(false)
|
|
105
|
+
describe('DEFENSIVE fix: recentlyStreaming suppression window', () => {
|
|
106
|
+
it('is true within the window of the last genuine stream event', () => {
|
|
107
|
+
const t = new LivenessTracker(0)
|
|
108
|
+
t.onStreamEvent('tool_use', undefined, 5_000)
|
|
109
|
+
expect(t.recentlyStreaming(5_000, W)).toBe(true)
|
|
110
|
+
expect(t.recentlyStreaming(5_000 + W - 1, W)).toBe(true)
|
|
120
111
|
})
|
|
121
112
|
|
|
122
|
-
it('
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
expect(
|
|
126
|
-
expect(shouldSuppressSyntheticTurnEnd({ durationMs: 0, midToolCall: false })).toBe(false)
|
|
113
|
+
it('is false once the window lapses (genuine hang, teardown proceeds)', () => {
|
|
114
|
+
const t = new LivenessTracker(0)
|
|
115
|
+
t.onStreamEvent('tool_use', undefined, 5_000)
|
|
116
|
+
expect(t.recentlyStreaming(5_000 + W, W)).toBe(false)
|
|
127
117
|
})
|
|
128
118
|
|
|
129
|
-
it('
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
119
|
+
it('a synthetic turn_end (-1) does not extend the window', () => {
|
|
120
|
+
const t = new LivenessTracker(0)
|
|
121
|
+
t.onStreamEvent('tool_use', undefined, 5_000)
|
|
122
|
+
t.onStreamEvent('turn_end', -1, 5_500) // ignored
|
|
123
|
+
expect(t.lastStreamEventAt).toBe(5_000)
|
|
124
|
+
expect(t.recentlyStreaming(5_000 + W, W)).toBe(false)
|
|
133
125
|
})
|
|
134
126
|
})
|
|
135
127
|
|
|
136
128
|
// ---------------------------------------------------------------------------
|
|
137
|
-
// Tests: ToolFlightTracker
|
|
129
|
+
// Tests: ToolFlightTracker → working input → decideOnExpiry
|
|
138
130
|
// ---------------------------------------------------------------------------
|
|
139
131
|
|
|
140
|
-
describe('ToolFlightTracker
|
|
141
|
-
it('re-arm fires when a Bash tool is in flight', () => {
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
expect(shouldSuppressSyntheticTurnEnd({
|
|
169
|
-
durationMs: -1,
|
|
170
|
-
midToolCall: tracker.isMidToolCall(),
|
|
171
|
-
})).toBe(true)
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
it('defensive guard allows synthetic turn_end after all tools complete', () => {
|
|
175
|
-
const tracker = new ToolFlightTracker()
|
|
176
|
-
tracker.onEvent({ kind: 'tool_use', toolUseId: 'bash_2' })
|
|
177
|
-
tracker.onEvent({ kind: 'tool_result', toolUseId: 'bash_2' })
|
|
178
|
-
|
|
179
|
-
expect(shouldSuppressSyntheticTurnEnd({
|
|
180
|
-
durationMs: -1,
|
|
181
|
-
midToolCall: tracker.isMidToolCall(),
|
|
182
|
-
})).toBe(false)
|
|
132
|
+
describe('ToolFlightTracker drives the `working` input', () => {
|
|
133
|
+
it('re-arm fires when a Bash tool is in flight (working=true)', () => {
|
|
134
|
+
const flight = new ToolFlightTracker()
|
|
135
|
+
flight.onEvent({ kind: 'tool_use', toolUseId: 'bash_1' })
|
|
136
|
+
const t = staleTracker()
|
|
137
|
+
const d = t.decideOnExpiry({
|
|
138
|
+
working: flight.isMidToolCall(),
|
|
139
|
+
humanWaiting: false,
|
|
140
|
+
now: STALE_NOW,
|
|
141
|
+
windowMs: W,
|
|
142
|
+
maxRearms: MAX,
|
|
143
|
+
})
|
|
144
|
+
expect(d.rearm).toBe(true)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('fires normally after tool_result completes the tool (working=false, not recently streaming)', () => {
|
|
148
|
+
const flight = new ToolFlightTracker()
|
|
149
|
+
flight.onEvent({ kind: 'tool_use', toolUseId: 'bash_1' })
|
|
150
|
+
flight.onEvent({ kind: 'tool_result', toolUseId: 'bash_1' })
|
|
151
|
+
const t = staleTracker()
|
|
152
|
+
const d = t.decideOnExpiry({
|
|
153
|
+
working: flight.isMidToolCall(),
|
|
154
|
+
humanWaiting: false,
|
|
155
|
+
now: STALE_NOW,
|
|
156
|
+
windowMs: W,
|
|
157
|
+
maxRearms: MAX,
|
|
158
|
+
})
|
|
159
|
+
expect(d.rearm).toBe(false)
|
|
183
160
|
})
|
|
184
161
|
|
|
185
162
|
it('parallel tools: re-arm persists while ANY tool is in flight', () => {
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
rearmCount: 0,
|
|
195
|
-
maxRearms: ORPHANED_REPLY_MAX_REARMS,
|
|
196
|
-
})).toBe(true)
|
|
197
|
-
|
|
198
|
-
// Two complete
|
|
199
|
-
tracker.onEvent({ kind: 'tool_result', toolUseId: 'read_1' })
|
|
200
|
-
tracker.onEvent({ kind: 'tool_result', toolUseId: 'read_2' })
|
|
201
|
-
|
|
202
|
-
// Still re-arming: edit_1 open
|
|
203
|
-
expect(shouldRearmInsteadOfFire({
|
|
204
|
-
midToolCall: tracker.isMidToolCall(),
|
|
205
|
-
rearmCount: 1,
|
|
206
|
-
maxRearms: ORPHANED_REPLY_MAX_REARMS,
|
|
207
|
-
})).toBe(true)
|
|
163
|
+
const flight = new ToolFlightTracker()
|
|
164
|
+
flight.onEvent({ kind: 'tool_use', toolUseId: 'read_1' })
|
|
165
|
+
flight.onEvent({ kind: 'tool_use', toolUseId: 'read_2' })
|
|
166
|
+
flight.onEvent({ kind: 'tool_use', toolUseId: 'edit_1' })
|
|
167
|
+
const t = staleTracker()
|
|
168
|
+
expect(
|
|
169
|
+
t.decideOnExpiry({ working: flight.isMidToolCall(), humanWaiting: false, now: STALE_NOW, windowMs: W, maxRearms: MAX }).rearm,
|
|
170
|
+
).toBe(true)
|
|
208
171
|
|
|
209
|
-
|
|
210
|
-
|
|
172
|
+
flight.onEvent({ kind: 'tool_result', toolUseId: 'read_1' })
|
|
173
|
+
flight.onEvent({ kind: 'tool_result', toolUseId: 'read_2' })
|
|
174
|
+
expect(
|
|
175
|
+
t.decideOnExpiry({ working: flight.isMidToolCall(), humanWaiting: false, now: STALE_NOW, windowMs: W, maxRearms: MAX }).rearm,
|
|
176
|
+
).toBe(true) // edit_1 still open
|
|
211
177
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
maxRearms:
|
|
216
|
-
|
|
178
|
+
flight.onEvent({ kind: 'tool_result', toolUseId: 'edit_1' })
|
|
179
|
+
const t2 = staleTracker() // fresh so the earlier rearms don't confound the cap
|
|
180
|
+
expect(
|
|
181
|
+
t2.decideOnExpiry({ working: flight.isMidToolCall(), humanWaiting: false, now: STALE_NOW, windowMs: W, maxRearms: MAX }).rearm,
|
|
182
|
+
).toBe(false) // all complete, not recently streaming → fire
|
|
217
183
|
})
|
|
218
184
|
|
|
219
185
|
it('cap fires even mid-tool after 20 re-arms (wedged tool surfaces)', () => {
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
rearmCount: i,
|
|
228
|
-
maxRearms: ORPHANED_REPLY_MAX_REARMS,
|
|
229
|
-
})).toBe(true)
|
|
186
|
+
const flight = new ToolFlightTracker()
|
|
187
|
+
flight.onEvent({ kind: 'tool_use', toolUseId: 'hung_bash' })
|
|
188
|
+
const t = staleTracker()
|
|
189
|
+
for (let i = 0; i < MAX; i++) {
|
|
190
|
+
expect(
|
|
191
|
+
t.decideOnExpiry({ working: flight.isMidToolCall(), humanWaiting: false, now: STALE_NOW, windowMs: W, maxRearms: MAX }).rearm,
|
|
192
|
+
).toBe(true)
|
|
230
193
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
rearmCount: ORPHANED_REPLY_MAX_REARMS,
|
|
236
|
-
maxRearms: ORPHANED_REPLY_MAX_REARMS,
|
|
237
|
-
})).toBe(false)
|
|
194
|
+
// Cap exceeded — fire despite in-flight.
|
|
195
|
+
expect(
|
|
196
|
+
t.decideOnExpiry({ working: flight.isMidToolCall(), humanWaiting: false, now: STALE_NOW, windowMs: W, maxRearms: MAX }).rearm,
|
|
197
|
+
).toBe(false)
|
|
238
198
|
})
|
|
239
199
|
})
|
|
240
200
|
|
|
@@ -696,12 +696,11 @@ import {
|
|
|
696
696
|
} from "../quota-watch.js";
|
|
697
697
|
|
|
698
698
|
describe("resolveQuotaWatchTuning", () => {
|
|
699
|
-
it("defaults: stale gate 60min, late-recovery 6h, dedup on
|
|
699
|
+
it("defaults: stale gate 60min, late-recovery 6h, dedup on", () => {
|
|
700
700
|
const t = resolveQuotaWatchTuning({});
|
|
701
701
|
expect(t.maxStaleMs).toBe(DEFAULT_QUOTA_WATCH_MAX_STALE_MS);
|
|
702
702
|
expect(t.lateRecoveryMs).toBe(DEFAULT_QUOTA_WATCH_LATE_RECOVERY_MS);
|
|
703
703
|
expect(t.fleetDedup).toBe(true);
|
|
704
|
-
expect(t.sendOnProbeFail).toBe(false);
|
|
705
704
|
});
|
|
706
705
|
|
|
707
706
|
it("each knob is individually kill-switchable", () => {
|
|
@@ -709,12 +708,10 @@ describe("resolveQuotaWatchTuning", () => {
|
|
|
709
708
|
SWITCHROOM_QUOTA_WATCH_MAX_STALE_MS: "0",
|
|
710
709
|
SWITCHROOM_QUOTA_WATCH_LATE_RECOVERY_MS: "0",
|
|
711
710
|
SWITCHROOM_QUOTA_WATCH_FLEET_DEDUP: "0",
|
|
712
|
-
SWITCHROOM_QUOTA_WATCH_SEND_ON_PROBE_FAIL: "1",
|
|
713
711
|
});
|
|
714
712
|
expect(t.maxStaleMs).toBe(0);
|
|
715
713
|
expect(t.lateRecoveryMs).toBe(0);
|
|
716
714
|
expect(t.fleetDedup).toBe(false);
|
|
717
|
-
expect(t.sendOnProbeFail).toBe(true);
|
|
718
715
|
});
|
|
719
716
|
|
|
720
717
|
it("garbage numeric values fall back to defaults", () => {
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
decideInboundDelivery,
|
|
5
|
+
reserveInboundDelivery,
|
|
6
|
+
} from '../gateway/inbound-delivery-gate.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* #2917 — rapid-fire DM: replies emitted late & out of order.
|
|
10
|
+
*
|
|
11
|
+
* Root cause: the #1556 delivery gate is documented as a LIVE read
|
|
12
|
+
* ("evaluated at delivery time — not a receipt-time snapshot"), but on the
|
|
13
|
+
* concurrent `handleInbound` path the gate decision and the busy-mark that
|
|
14
|
+
* records "a turn is in flight for this chat" were separated by an `await`
|
|
15
|
+
* (attachment download, composer-clear). Two same-chat inbounds each read
|
|
16
|
+
* "idle" during the other's async lead-in, both passed the gate, and both
|
|
17
|
+
* were delivered to the bridge — so the replies came back reordered.
|
|
18
|
+
*
|
|
19
|
+
* The fix couples the deliver decision with a synchronous RESERVE
|
|
20
|
+
* (`reserveInboundDelivery`): a fresh-turn deliver reserves the chat's busy
|
|
21
|
+
* key BEFORE any await, so a concurrent same-chat inbound observes the
|
|
22
|
+
* reservation on the live gate and buffers behind it — restoring per-chat
|
|
23
|
+
* FIFO without dropping anything (a buffered inbound drains on turn-complete
|
|
24
|
+
* / idle exactly as before).
|
|
25
|
+
*/
|
|
26
|
+
describe('reserveInboundDelivery — fresh-turn reservation flag', () => {
|
|
27
|
+
it('reserves a fresh-turn deliver (idle, non-steering, non-interrupt)', () => {
|
|
28
|
+
const r = reserveInboundDelivery({ turnInFlight: false, isSteering: false, isInterrupt: false })
|
|
29
|
+
expect(r.decision).toBe('deliver')
|
|
30
|
+
expect(r.reserve).toBe(true)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('does NOT reserve a buffered (mid-turn) inbound', () => {
|
|
34
|
+
const r = reserveInboundDelivery({ turnInFlight: true, isSteering: false, isInterrupt: false })
|
|
35
|
+
expect(r.decision).toBe('buffer-until-idle')
|
|
36
|
+
expect(r.reserve).toBe(false)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('does NOT reserve a steering deliver (amends the running turn)', () => {
|
|
40
|
+
const r = reserveInboundDelivery({ turnInFlight: true, isSteering: true, isInterrupt: false })
|
|
41
|
+
expect(r.decision).toBe('deliver')
|
|
42
|
+
expect(r.reserve).toBe(false)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('does NOT reserve an interrupt deliver (amends the running turn)', () => {
|
|
46
|
+
const r = reserveInboundDelivery({ turnInFlight: true, isSteering: false, isInterrupt: true })
|
|
47
|
+
expect(r.decision).toBe('deliver')
|
|
48
|
+
expect(r.reserve).toBe(false)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('decision stays byte-identical to decideInboundDelivery across the matrix', () => {
|
|
52
|
+
for (const turnInFlight of [false, true]) {
|
|
53
|
+
for (const isSteering of [false, true]) {
|
|
54
|
+
for (const isInterrupt of [false, true]) {
|
|
55
|
+
const input = { turnInFlight, isSteering, isInterrupt }
|
|
56
|
+
expect(reserveInboundDelivery(input).decision).toBe(decideInboundDelivery(input))
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A minimal model of the gateway's concurrent same-chat delivery path. Each
|
|
65
|
+
* inbound handler:
|
|
66
|
+
* 1. reads the LIVE gate off the shared busy set,
|
|
67
|
+
* 2. yields at an `await` (the composer-clear / attachment lead-in),
|
|
68
|
+
* 3. on a reserving deliver, marks the chat busy and appends to the wire.
|
|
69
|
+
*
|
|
70
|
+
* This reproduces the ordering race deterministically: two same-chat handlers
|
|
71
|
+
* whose async lead-ins overlap. The `syncReserve` flag toggles the fix — when
|
|
72
|
+
* off (legacy: mark busy only AFTER the await), both handlers deliver and the
|
|
73
|
+
* wire order is decided by the await race; when on (#2917: reserve BEFORE the
|
|
74
|
+
* await), the second handler sees the reservation and buffers, preserving FIFO.
|
|
75
|
+
*/
|
|
76
|
+
interface WireResult {
|
|
77
|
+
delivered: string[]
|
|
78
|
+
buffered: string[]
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function runTwoConcurrentInbounds(opts: {
|
|
82
|
+
syncReserve: boolean
|
|
83
|
+
/** ms the first handler's lead-in await takes vs the second's. First is
|
|
84
|
+
* SLOWER so, without the fix, the second overtakes it on the wire. */
|
|
85
|
+
firstLeadInMs: number
|
|
86
|
+
secondLeadInMs: number
|
|
87
|
+
}): Promise<WireResult> {
|
|
88
|
+
const busy = new Set<string>()
|
|
89
|
+
const key = 'chatDM:_'
|
|
90
|
+
const delivered: string[] = []
|
|
91
|
+
const buffered: string[] = []
|
|
92
|
+
|
|
93
|
+
const handle = async (id: string, leadInMs: number): Promise<void> => {
|
|
94
|
+
// Step 1 — live gate read.
|
|
95
|
+
const gate = reserveInboundDelivery({
|
|
96
|
+
turnInFlight: busy.size > 0,
|
|
97
|
+
isSteering: false,
|
|
98
|
+
isInterrupt: false,
|
|
99
|
+
})
|
|
100
|
+
if (gate.decision === 'buffer-until-idle') {
|
|
101
|
+
buffered.push(id)
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
// Step 2 — the fix reserves synchronously BEFORE the await.
|
|
105
|
+
let reserved = false
|
|
106
|
+
if (opts.syncReserve && gate.reserve) {
|
|
107
|
+
busy.add(key)
|
|
108
|
+
reserved = true
|
|
109
|
+
}
|
|
110
|
+
// Step 3 — the async lead-in (composer-clear / attachment download).
|
|
111
|
+
await new Promise((r) => setTimeout(r, leadInMs))
|
|
112
|
+
// Step 4 — legacy marks busy only now, after the await.
|
|
113
|
+
if (!reserved) busy.add(key)
|
|
114
|
+
delivered.push(id)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
await Promise.all([
|
|
118
|
+
handle('msg1', opts.firstLeadInMs),
|
|
119
|
+
handle('msg2', opts.secondLeadInMs),
|
|
120
|
+
])
|
|
121
|
+
return { delivered, buffered }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
describe('rapid-fire same-chat delivery ordering (#2917)', () => {
|
|
125
|
+
it('LEGACY (mark-after-await): the faster second inbound overtakes the first — reorder', async () => {
|
|
126
|
+
// msg1's lead-in is slower, so with the busy-mark deferred until after the
|
|
127
|
+
// await BOTH pass the idle gate and BOTH deliver, with msg2 landing first.
|
|
128
|
+
const { delivered, buffered } = await runTwoConcurrentInbounds({
|
|
129
|
+
syncReserve: false,
|
|
130
|
+
firstLeadInMs: 20,
|
|
131
|
+
secondLeadInMs: 1,
|
|
132
|
+
})
|
|
133
|
+
expect(buffered).toEqual([]) // nothing buffered — the bug
|
|
134
|
+
expect(delivered).toEqual(['msg2', 'msg1']) // out of order
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('FIX (sync reserve): the second inbound sees the reservation and buffers — FIFO preserved', async () => {
|
|
138
|
+
const { delivered, buffered } = await runTwoConcurrentInbounds({
|
|
139
|
+
syncReserve: true,
|
|
140
|
+
firstLeadInMs: 20,
|
|
141
|
+
secondLeadInMs: 1,
|
|
142
|
+
})
|
|
143
|
+
// msg1 reserved synchronously; msg2's live gate sees busy → buffers.
|
|
144
|
+
expect(delivered).toEqual(['msg1'])
|
|
145
|
+
expect(buffered).toEqual(['msg2'])
|
|
146
|
+
// msg2 is not lost — it is held for the turn-complete / idle drain, which
|
|
147
|
+
// re-delivers it in order after msg1's turn ends.
|
|
148
|
+
})
|
|
149
|
+
})
|