switchroom 0.14.20 → 0.14.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-scheduler/index.js +2 -3
- package/dist/auth-broker/index.js +2 -3
- package/dist/cli/notion-write-pretool.mjs +2 -3
- package/dist/cli/switchroom.js +16 -8
- package/dist/host-control/main.js +2 -3
- package/dist/vault/approvals/kernel-server.js +2 -3
- package/dist/vault/broker/server.js +2 -3
- package/package.json +3 -3
- package/profiles/_base/start.sh.hbs +11 -24
- package/profiles/_shared/telegram-style.md.hbs +2 -2
- package/profiles/default/CLAUDE.md.hbs +4 -1
- package/skills/switchroom-runtime/SKILL.md +6 -16
- package/telegram-plugin/agent-dir.ts +15 -0
- package/telegram-plugin/dist/gateway/gateway.js +655 -514
- package/telegram-plugin/gateway/coalesce-attachments.ts +9 -0
- package/telegram-plugin/gateway/gateway.ts +246 -83
- package/telegram-plugin/gateway/inbound-spool.ts +15 -0
- package/telegram-plugin/gateway/interrupt-defer.ts +6 -0
- package/telegram-plugin/gateway/resume-inbound-builder.ts +180 -0
- package/telegram-plugin/registry/turns-schema.ts +138 -33
- package/telegram-plugin/stream-reply-handler.ts +1 -11
- package/telegram-plugin/tests/agent-dir.test.ts +25 -0
- package/telegram-plugin/tests/coalesce-attachments.test.ts +24 -6
- package/telegram-plugin/tests/e2e.test.ts +2 -77
- package/telegram-plugin/tests/inbound-spool.test.ts +45 -0
- package/telegram-plugin/tests/interrupt-defer.test.ts +13 -0
- package/telegram-plugin/tests/multi-turn-continuity.test.ts +0 -1
- package/telegram-plugin/tests/outbound-ordering.test.ts +0 -1
- package/telegram-plugin/tests/parse-mode-rotation.test.ts +0 -1
- package/telegram-plugin/tests/permission-verdict-resume-guard.test.ts +86 -0
- package/telegram-plugin/tests/races.test.ts +0 -26
- package/telegram-plugin/tests/registry-turns.test.ts +106 -29
- package/telegram-plugin/tests/resume-inbound-builder.test.ts +182 -0
- package/telegram-plugin/tests/status-accent.test.ts +0 -1
- package/telegram-plugin/tests/stream-reply-error-paths.test.ts +0 -1
- package/telegram-plugin/tests/stream-reply-handler.test.ts +0 -24
- package/telegram-plugin/tests/streaming-e2e.test.ts +0 -1
- package/telegram-plugin/tests/streaming-orchestration.test.ts +0 -1
- package/telegram-plugin/tests/tool-activity-summary.test.ts +44 -0
- package/telegram-plugin/tests/turns-writer.test.ts +16 -6
- package/telegram-plugin/tests/worker-activity-feed.test.ts +14 -0
- package/telegram-plugin/tool-activity-summary.ts +55 -0
- package/telegram-plugin/uat/assertions.ts +53 -0
- package/telegram-plugin/uat/driver.ts +30 -0
- package/telegram-plugin/uat/feed-matcher.test.ts +80 -0
- package/telegram-plugin/uat/fixtures/album/blue.jpg +0 -0
- package/telegram-plugin/uat/fixtures/album/green.jpg +0 -0
- package/telegram-plugin/uat/fixtures/album/red.jpg +0 -0
- package/telegram-plugin/uat/scenarios/jtbd-album-coalescing-dm.test.ts +136 -0
- package/telegram-plugin/uat/scenarios/jtbd-memory-survives-restart-dm.test.ts +17 -2
- package/telegram-plugin/worker-activity-feed.ts +11 -5
- package/telegram-plugin/handoff-continuity.ts +0 -206
- package/telegram-plugin/tests/handoff-continuity.test.ts +0 -262
|
@@ -106,6 +106,51 @@ describe('spoolId — stable dedup key', () => {
|
|
|
106
106
|
// messageId > 0 → legacy m:<chat>:<msgId> still wins.
|
|
107
107
|
expect(a).toBe('m:c1:555')
|
|
108
108
|
})
|
|
109
|
+
// honest-restart-resume: a boot-resume inbound is minted with a fresh
|
|
110
|
+
// ts/messageId every boot, so without a turn-keyed id an operator who
|
|
111
|
+
// restarts twice before the agent drains the first resume would stack
|
|
112
|
+
// N resumes of the same turn. Keying on resume_turn_key collapses them.
|
|
113
|
+
it('resume_interrupted → s:resume:<turn_key>, stable across boots (fresh ts/messageId)', () => {
|
|
114
|
+
const a = spoolId(
|
|
115
|
+
msg({
|
|
116
|
+
messageId: 1700_000_000_000,
|
|
117
|
+
ts: 1700_000_000_000,
|
|
118
|
+
meta: { source: 'resume_interrupted', resume_turn_key: '12345:11' },
|
|
119
|
+
}),
|
|
120
|
+
)
|
|
121
|
+
const b = spoolId(
|
|
122
|
+
msg({
|
|
123
|
+
messageId: 1700_000_999_999,
|
|
124
|
+
ts: 1700_000_999_999,
|
|
125
|
+
meta: { source: 'resume_interrupted', resume_turn_key: '12345:11' },
|
|
126
|
+
}),
|
|
127
|
+
)
|
|
128
|
+
expect(a).toBe('s:resume:12345:11')
|
|
129
|
+
expect(b).toBe(a)
|
|
130
|
+
})
|
|
131
|
+
it('resume_watchdog_timeout shares the s:resume namespace (one turn is one or the other)', () => {
|
|
132
|
+
const interrupted = spoolId(
|
|
133
|
+
msg({ messageId: 0, meta: { source: 'resume_interrupted', resume_turn_key: 'k:1' } }),
|
|
134
|
+
)
|
|
135
|
+
const timeout = spoolId(
|
|
136
|
+
msg({ messageId: 0, meta: { source: 'resume_watchdog_timeout', resume_turn_key: 'k:1' } }),
|
|
137
|
+
)
|
|
138
|
+
expect(timeout).toBe('s:resume:k:1')
|
|
139
|
+
expect(timeout).toBe(interrupted)
|
|
140
|
+
})
|
|
141
|
+
it('resume inbounds for distinct turns stay distinct', () => {
|
|
142
|
+
const a = spoolId(
|
|
143
|
+
msg({ messageId: 0, meta: { source: 'resume_interrupted', resume_turn_key: 'k:1' } }),
|
|
144
|
+
)
|
|
145
|
+
const b = spoolId(
|
|
146
|
+
msg({ messageId: 0, meta: { source: 'resume_interrupted', resume_turn_key: 'k:2' } }),
|
|
147
|
+
)
|
|
148
|
+
expect(a).not.toBe(b)
|
|
149
|
+
})
|
|
150
|
+
it('resume source without a turn_key falls back to legacy id (no crash)', () => {
|
|
151
|
+
const a = spoolId(msg({ messageId: 777, meta: { source: 'resume_interrupted' }, ts: 100 }))
|
|
152
|
+
expect(a).toBe('m:c1:777')
|
|
153
|
+
})
|
|
109
154
|
})
|
|
110
155
|
|
|
111
156
|
describe('inbound-spool — subagent_handback dedup across restart re-build (#1719)', () => {
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
ToolFlightTracker,
|
|
16
16
|
decideInterruptTiming,
|
|
17
17
|
resolveInterruptMaxWaitMs,
|
|
18
|
+
resolveSafeBoundaryEnabled,
|
|
18
19
|
DEFAULT_INTERRUPT_MAX_WAIT_MS,
|
|
19
20
|
} from '../gateway/interrupt-defer.js'
|
|
20
21
|
|
|
@@ -119,6 +120,18 @@ describe('decideInterruptTiming', () => {
|
|
|
119
120
|
})
|
|
120
121
|
})
|
|
121
122
|
|
|
123
|
+
describe('resolveSafeBoundaryEnabled (default ON)', () => {
|
|
124
|
+
it('defaults to true when unset', () => {
|
|
125
|
+
expect(resolveSafeBoundaryEnabled(undefined)).toBe(true)
|
|
126
|
+
})
|
|
127
|
+
it('stays true when explicitly true', () => {
|
|
128
|
+
expect(resolveSafeBoundaryEnabled(true)).toBe(true)
|
|
129
|
+
})
|
|
130
|
+
it('only an explicit false opts out', () => {
|
|
131
|
+
expect(resolveSafeBoundaryEnabled(false)).toBe(false)
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
|
|
122
135
|
describe('resolveInterruptMaxWaitMs', () => {
|
|
123
136
|
it('uses the configured value when positive', () => {
|
|
124
137
|
expect(resolveInterruptMaxWaitMs(3000)).toBe(3000)
|
|
@@ -31,7 +31,6 @@ function makeDeps(bot: FakeBot, overrides?: Partial<StreamReplyDeps>): StreamRep
|
|
|
31
31
|
markdownToHtml: (t) => realMarkdownToHtml(t),
|
|
32
32
|
escapeMarkdownV2: (t) => t,
|
|
33
33
|
repairEscapedWhitespace: (t) => t,
|
|
34
|
-
takeHandoffPrefix: () => '',
|
|
35
34
|
assertAllowedChat: () => {},
|
|
36
35
|
resolveThreadId: (_, explicit) => (explicit != null ? Number(explicit) : undefined),
|
|
37
36
|
disableLinkPreview: true,
|
|
@@ -437,7 +437,6 @@ describe('wrapBot + handleStreamReply + reply ordering', () => {
|
|
|
437
437
|
markdownToHtml: (t) => t,
|
|
438
438
|
escapeMarkdownV2: (t) => t,
|
|
439
439
|
repairEscapedWhitespace: (t) => t,
|
|
440
|
-
takeHandoffPrefix: () => '',
|
|
441
440
|
assertAllowedChat: () => {},
|
|
442
441
|
resolveThreadId: () => undefined,
|
|
443
442
|
disableLinkPreview: true,
|
|
@@ -31,7 +31,6 @@ function makeDeps(bot: FakeBot, overrides?: Partial<StreamReplyDeps>): StreamRep
|
|
|
31
31
|
markdownToHtml: (t) => realMarkdownToHtml(t),
|
|
32
32
|
escapeMarkdownV2: (t) => `ESC(${t})`,
|
|
33
33
|
repairEscapedWhitespace: (t) => t,
|
|
34
|
-
takeHandoffPrefix: () => '',
|
|
35
34
|
assertAllowedChat: () => {},
|
|
36
35
|
resolveThreadId: (_, explicit) => (explicit != null ? Number(explicit) : undefined),
|
|
37
36
|
disableLinkPreview: true,
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural pin for the permission-card resume beat.
|
|
3
|
+
*
|
|
4
|
+
* What broke (and the bug this guards against): when the operator
|
|
5
|
+
* answers a permission card, the suspended `claude` turn un-parks and
|
|
6
|
+
* resumes the SAME turn — the gateway must flip the awaiting glyph
|
|
7
|
+
* (🙏) back to a working glyph so the operator sees progress instead
|
|
8
|
+
* of a stuck card. That flip is `resumeReactionAfterVerdict()`.
|
|
9
|
+
*
|
|
10
|
+
* The verdict can arrive down several independent paths (button tap,
|
|
11
|
+
* always-allow, `/allow`·`/deny`, TTL auto-deny, free-text `y <id>`/
|
|
12
|
+
* `no <id>` reply, …). Every one of them calls
|
|
13
|
+
* `dispatchPermissionVerdict(...)` to un-park the turn — but the resume
|
|
14
|
+
* glyph flip is a *separate* call right next to it. The free-text-reply
|
|
15
|
+
* path shipped the dispatch WITHOUT the resume (fixed in v0.14.19), so
|
|
16
|
+
* answering via a text reply left the card frozen on 🙏 even though the
|
|
17
|
+
* turn was running. The controller-level behaviour is covered by
|
|
18
|
+
* `status-reactions.test.ts` ("setAwaiting" + watchdog re-arm); mtcute
|
|
19
|
+
* UAT cannot observe reactions at all, so this static pin is the only
|
|
20
|
+
* thing that catches a verdict path forgetting the resume.
|
|
21
|
+
*
|
|
22
|
+
* This guard fails loudly if any `dispatchPermissionVerdict(...)`
|
|
23
|
+
* callsite is not paired with a `resumeReactionAfterVerdict()` within a
|
|
24
|
+
* few lines — i.e. a new (or refactored) verdict path drops the resume.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { describe, it, expect } from 'vitest'
|
|
28
|
+
import { readFileSync } from 'node:fs'
|
|
29
|
+
import { fileURLToPath } from 'node:url'
|
|
30
|
+
import { dirname, resolve } from 'node:path'
|
|
31
|
+
|
|
32
|
+
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
33
|
+
const GATEWAY_SRC = readFileSync(
|
|
34
|
+
resolve(__dirname, '..', 'gateway', 'gateway.ts'),
|
|
35
|
+
'utf8',
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
const LINES = GATEWAY_SRC.split('\n')
|
|
39
|
+
|
|
40
|
+
// A `dispatchPermissionVerdict(` occurrence is a CALLSITE unless it's the
|
|
41
|
+
// function definition itself.
|
|
42
|
+
const isDefinition = (line: string) =>
|
|
43
|
+
/\bfunction\s+dispatchPermissionVerdict\b/.test(line)
|
|
44
|
+
|
|
45
|
+
const dispatchCallsites = LINES.flatMap((line, i) =>
|
|
46
|
+
/\bdispatchPermissionVerdict\s*\(/.test(line) && !isDefinition(line)
|
|
47
|
+
? [i]
|
|
48
|
+
: [],
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
// How far below the dispatch the resume call is allowed to live. The
|
|
52
|
+
// widest real gap today is ~9 lines (the slash-command path); 15 gives
|
|
53
|
+
// refactor headroom without letting an unrelated resume "cover" a
|
|
54
|
+
// dispatch from a different block.
|
|
55
|
+
const RESUME_WINDOW = 15
|
|
56
|
+
|
|
57
|
+
describe('permission verdict → resume reaction wiring', () => {
|
|
58
|
+
it('there is at least one verdict-dispatch path to guard', () => {
|
|
59
|
+
expect(dispatchCallsites.length).toBeGreaterThan(0)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('every dispatchPermissionVerdict() callsite flips the awaiting glyph back via resumeReactionAfterVerdict()', () => {
|
|
63
|
+
const unpaired: number[] = []
|
|
64
|
+
for (const idx of dispatchCallsites) {
|
|
65
|
+
const window = LINES.slice(idx, idx + RESUME_WINDOW + 1).join('\n')
|
|
66
|
+
if (!/\bresumeReactionAfterVerdict\s*\(\s*\)/.test(window)) {
|
|
67
|
+
// 1-based line number for a human-readable failure.
|
|
68
|
+
unpaired.push(idx + 1)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
expect(
|
|
72
|
+
unpaired,
|
|
73
|
+
`dispatchPermissionVerdict() at gateway.ts line(s) ` +
|
|
74
|
+
`${unpaired.join(', ')} has no resumeReactionAfterVerdict() within ` +
|
|
75
|
+
`${RESUME_WINDOW} lines — that verdict path leaves the permission ` +
|
|
76
|
+
`card stuck on 🙏 after the operator answers. Add the resume call ` +
|
|
77
|
+
`(see the sibling paths and v0.14.19 / the free-text-reply fix).`,
|
|
78
|
+
).toEqual([])
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('the resume helper still exists (the pairing is meaningless if it was deleted)', () => {
|
|
82
|
+
expect(/function\s+resumeReactionAfterVerdict\s*\(/.test(GATEWAY_SRC)).toBe(
|
|
83
|
+
true,
|
|
84
|
+
)
|
|
85
|
+
})
|
|
86
|
+
})
|
|
@@ -7,9 +7,6 @@
|
|
|
7
7
|
* server.ts directly.
|
|
8
8
|
*/
|
|
9
9
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
10
|
-
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
11
|
-
import { tmpdir } from 'node:os'
|
|
12
|
-
import { join } from 'node:path'
|
|
13
10
|
|
|
14
11
|
import {
|
|
15
12
|
parseQueuePrefix,
|
|
@@ -17,11 +14,6 @@ import {
|
|
|
17
14
|
formatPriorAssistantPreview,
|
|
18
15
|
buildChannelMetaAttributes,
|
|
19
16
|
} from '../steering.js'
|
|
20
|
-
import {
|
|
21
|
-
consumeHandoffTopic,
|
|
22
|
-
formatHandoffLine,
|
|
23
|
-
HANDOFF_TOPIC_FILENAME,
|
|
24
|
-
} from '../handoff-continuity.js'
|
|
25
17
|
|
|
26
18
|
// ---- harness (copy of e2e.test.ts's — intentional; tests stay isolated) ----
|
|
27
19
|
|
|
@@ -213,24 +205,6 @@ describe('Race: reply tool claims PTY preview suppression gate', () => {
|
|
|
213
205
|
})
|
|
214
206
|
})
|
|
215
207
|
|
|
216
|
-
describe('Race: handoff topic consumed once, second reply no-ops', () => {
|
|
217
|
-
let tmp: string
|
|
218
|
-
beforeEach(() => {
|
|
219
|
-
tmp = mkdtempSync(join(tmpdir(), 'handoff-race-'))
|
|
220
|
-
})
|
|
221
|
-
afterEach(() => {
|
|
222
|
-
rmSync(tmp, { recursive: true, force: true })
|
|
223
|
-
})
|
|
224
|
-
|
|
225
|
-
it('topic consumed on first reply; second reply sees null', () => {
|
|
226
|
-
writeFileSync(join(tmp, HANDOFF_TOPIC_FILENAME), 'topic\n', 'utf8')
|
|
227
|
-
const first = consumeHandoffTopic(tmp)
|
|
228
|
-
expect(first).toBe('topic')
|
|
229
|
-
const second = consumeHandoffTopic(tmp)
|
|
230
|
-
expect(second).toBeNull()
|
|
231
|
-
})
|
|
232
|
-
})
|
|
233
|
-
|
|
234
208
|
describe('Race: activeTurnStartedAt cleanup on every exit path', () => {
|
|
235
209
|
// Parameterized test — exercises every code path that deletes a
|
|
236
210
|
// status controller in server.ts and asserts the Map is empty after.
|
|
@@ -17,10 +17,20 @@ import {
|
|
|
17
17
|
recordTurnStart,
|
|
18
18
|
recordTurnEnd,
|
|
19
19
|
findOrphanedTurns,
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
markOrphanedWithTimeoutClassification,
|
|
21
|
+
findLatestTurnIfInterrupted,
|
|
22
22
|
} from '../registry/turns-schema.js'
|
|
23
23
|
|
|
24
|
+
// Convenience: the boot reaper with no live hang marker — every open turn
|
|
25
|
+
// is a clean 'restart' interrupt. Mirrors the gateway's between-turns boot.
|
|
26
|
+
function reapAsRestart(db: Parameters<typeof findOrphanedTurns>[0]) {
|
|
27
|
+
return markOrphanedWithTimeoutClassification(db, {
|
|
28
|
+
markerTurnKey: null,
|
|
29
|
+
markerAgeMs: null,
|
|
30
|
+
hangThresholdMs: 300_000,
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
24
34
|
// ---------------------------------------------------------------------------
|
|
25
35
|
// Helpers
|
|
26
36
|
// ---------------------------------------------------------------------------
|
|
@@ -280,17 +290,18 @@ describe('findOrphanedTurns', () => {
|
|
|
280
290
|
})
|
|
281
291
|
|
|
282
292
|
// ---------------------------------------------------------------------------
|
|
283
|
-
//
|
|
293
|
+
// markOrphanedWithTimeoutClassification — no-hang-marker (clean restart) path
|
|
284
294
|
// ---------------------------------------------------------------------------
|
|
285
295
|
|
|
286
|
-
describe('
|
|
296
|
+
describe('markOrphanedWithTimeoutClassification (restart path)', () => {
|
|
287
297
|
it('stamps all open turns with ended_via=restart and non-null ended_at', () => {
|
|
288
298
|
const db = openTurnsDbInMemory()
|
|
289
299
|
recordTurnStart(db, { turnKey: '555:1', chatId: '555' })
|
|
290
300
|
recordTurnStart(db, { turnKey: '555:2', chatId: '555' })
|
|
291
301
|
|
|
292
|
-
const
|
|
293
|
-
expect(
|
|
302
|
+
const res = reapAsRestart(db)
|
|
303
|
+
expect(res.reaped).toBe(2)
|
|
304
|
+
expect(res.timeoutTurnKey).toBeNull()
|
|
294
305
|
|
|
295
306
|
const rows = db.prepare(
|
|
296
307
|
"SELECT * FROM turns WHERE ended_via = 'restart'",
|
|
@@ -309,7 +320,7 @@ describe('markOrphanedAsRestarted', () => {
|
|
|
309
320
|
recordTurnEnd(db, { turnKey: '555:3', endedVia: 'stop' })
|
|
310
321
|
recordTurnStart(db, { turnKey: '555:4', chatId: '555' })
|
|
311
322
|
|
|
312
|
-
|
|
323
|
+
reapAsRestart(db)
|
|
313
324
|
|
|
314
325
|
const closed = db.prepare(
|
|
315
326
|
"SELECT ended_via FROM turns WHERE turn_key = '555:3'",
|
|
@@ -323,41 +334,99 @@ describe('markOrphanedAsRestarted', () => {
|
|
|
323
334
|
db.close()
|
|
324
335
|
})
|
|
325
336
|
|
|
326
|
-
it('after
|
|
337
|
+
it('after reaping, findOrphanedTurns returns empty', () => {
|
|
327
338
|
const db = openTurnsDbInMemory()
|
|
328
339
|
recordTurnStart(db, { turnKey: '666:1', chatId: '666' })
|
|
329
340
|
recordTurnStart(db, { turnKey: '666:2', chatId: '666' })
|
|
330
341
|
|
|
331
|
-
|
|
342
|
+
reapAsRestart(db)
|
|
332
343
|
|
|
333
344
|
expect(findOrphanedTurns(db, '666')).toHaveLength(0)
|
|
334
345
|
db.close()
|
|
335
346
|
})
|
|
336
347
|
|
|
337
|
-
it('
|
|
348
|
+
it('reaps 0 when there are no open turns', () => {
|
|
338
349
|
const db = openTurnsDbInMemory()
|
|
339
350
|
recordTurnStart(db, { turnKey: '777:1', chatId: '777' })
|
|
340
351
|
recordTurnEnd(db, { turnKey: '777:1', endedVia: 'stop' })
|
|
341
352
|
|
|
342
|
-
expect(
|
|
353
|
+
expect(reapAsRestart(db).reaped).toBe(0)
|
|
343
354
|
db.close()
|
|
344
355
|
})
|
|
345
356
|
|
|
346
|
-
it('is safe to call on an empty DB (
|
|
357
|
+
it('is safe to call on an empty DB (reaps 0, no error)', () => {
|
|
347
358
|
const db = openTurnsDbInMemory()
|
|
348
|
-
expect(
|
|
359
|
+
expect(reapAsRestart(db).reaped).toBe(0)
|
|
349
360
|
db.close()
|
|
350
361
|
})
|
|
351
362
|
})
|
|
352
363
|
|
|
353
364
|
// ---------------------------------------------------------------------------
|
|
354
|
-
//
|
|
365
|
+
// markOrphanedWithTimeoutClassification — hang-marker (timeout) path
|
|
355
366
|
// ---------------------------------------------------------------------------
|
|
356
367
|
|
|
357
|
-
describe('
|
|
368
|
+
describe('markOrphanedWithTimeoutClassification (timeout path)', () => {
|
|
369
|
+
it('stamps the marker turn timeout when its marker is older than the threshold', () => {
|
|
370
|
+
const db = openTurnsDbInMemory()
|
|
371
|
+
recordTurnStart(db, { turnKey: 'hang:1', chatId: 'h' })
|
|
372
|
+
recordTurnStart(db, { turnKey: 'live:2', chatId: 'h' })
|
|
373
|
+
|
|
374
|
+
const res = markOrphanedWithTimeoutClassification(db, {
|
|
375
|
+
markerTurnKey: 'hang:1',
|
|
376
|
+
markerAgeMs: 600_000, // 10 min > 5 min threshold
|
|
377
|
+
hangThresholdMs: 300_000,
|
|
378
|
+
reasonSnapshot: JSON.stringify({ idleMs: 600_000 }),
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
expect(res.timeoutTurnKey).toBe('hang:1')
|
|
382
|
+
expect(res.reaped).toBe(2)
|
|
383
|
+
|
|
384
|
+
const hang = db.prepare("SELECT * FROM turns WHERE turn_key = 'hang:1'").get() as Record<string, unknown>
|
|
385
|
+
expect(hang['ended_via']).toBe('timeout')
|
|
386
|
+
expect(hang['interrupt_reason']).toBe(JSON.stringify({ idleMs: 600_000 }))
|
|
387
|
+
// The other open turn is a clean restart.
|
|
388
|
+
const live = db.prepare("SELECT * FROM turns WHERE turn_key = 'live:2'").get() as Record<string, unknown>
|
|
389
|
+
expect(live['ended_via']).toBe('restart')
|
|
390
|
+
db.close()
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
it('classifies the marker turn as restart when its marker is younger than the threshold', () => {
|
|
394
|
+
const db = openTurnsDbInMemory()
|
|
395
|
+
recordTurnStart(db, { turnKey: 'fresh:1', chatId: 'h' })
|
|
396
|
+
|
|
397
|
+
const res = markOrphanedWithTimeoutClassification(db, {
|
|
398
|
+
markerTurnKey: 'fresh:1',
|
|
399
|
+
markerAgeMs: 5_000, // 5s — was making progress
|
|
400
|
+
hangThresholdMs: 300_000,
|
|
401
|
+
})
|
|
402
|
+
|
|
403
|
+
expect(res.timeoutTurnKey).toBeNull()
|
|
404
|
+
const row = db.prepare("SELECT ended_via FROM turns WHERE turn_key = 'fresh:1'").get() as Record<string, unknown>
|
|
405
|
+
expect(row['ended_via']).toBe('restart')
|
|
406
|
+
db.close()
|
|
407
|
+
})
|
|
408
|
+
|
|
409
|
+
it('does not classify timeout when no marker turn key is present (between-turns boot)', () => {
|
|
410
|
+
const db = openTurnsDbInMemory()
|
|
411
|
+
recordTurnStart(db, { turnKey: 'x:1', chatId: 'h' })
|
|
412
|
+
const res = markOrphanedWithTimeoutClassification(db, {
|
|
413
|
+
markerTurnKey: null,
|
|
414
|
+
markerAgeMs: 999_999,
|
|
415
|
+
hangThresholdMs: 300_000,
|
|
416
|
+
})
|
|
417
|
+
expect(res.timeoutTurnKey).toBeNull()
|
|
418
|
+
db.close()
|
|
419
|
+
})
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
// ---------------------------------------------------------------------------
|
|
423
|
+
// findLatestTurnIfInterrupted — keys on the LATEST turn only
|
|
424
|
+
// ---------------------------------------------------------------------------
|
|
425
|
+
|
|
426
|
+
describe('findLatestTurnIfInterrupted', () => {
|
|
358
427
|
it('returns null when no turns exist', () => {
|
|
359
428
|
const db = openTurnsDbInMemory()
|
|
360
|
-
expect(
|
|
429
|
+
expect(findLatestTurnIfInterrupted(db)).toBeNull()
|
|
361
430
|
db.close()
|
|
362
431
|
})
|
|
363
432
|
|
|
@@ -365,14 +434,14 @@ describe('findMostRecentInterruptedTurn', () => {
|
|
|
365
434
|
const db = openTurnsDbInMemory()
|
|
366
435
|
recordTurnStart(db, { turnKey: '888:1', chatId: '888' })
|
|
367
436
|
recordTurnEnd(db, { turnKey: '888:1', endedVia: 'stop' })
|
|
368
|
-
expect(
|
|
437
|
+
expect(findLatestTurnIfInterrupted(db)).toBeNull()
|
|
369
438
|
db.close()
|
|
370
439
|
})
|
|
371
440
|
|
|
372
441
|
it('returns an open turn (ended_at IS NULL) as interrupted', () => {
|
|
373
442
|
const db = openTurnsDbInMemory()
|
|
374
443
|
recordTurnStart(db, { turnKey: '999:1', chatId: '999', lastUserMsgId: 'msg-1' })
|
|
375
|
-
const t =
|
|
444
|
+
const t = findLatestTurnIfInterrupted(db)
|
|
376
445
|
expect(t).not.toBeNull()
|
|
377
446
|
expect(t!.turn_key).toBe('999:1')
|
|
378
447
|
expect(t!.last_user_msg_id).toBe('msg-1')
|
|
@@ -383,7 +452,7 @@ describe('findMostRecentInterruptedTurn', () => {
|
|
|
383
452
|
const db = openTurnsDbInMemory()
|
|
384
453
|
recordTurnStart(db, { turnKey: 'aaa:1', chatId: 'aaa' })
|
|
385
454
|
recordTurnEnd(db, { turnKey: 'aaa:1', endedVia: 'sigterm' })
|
|
386
|
-
const t =
|
|
455
|
+
const t = findLatestTurnIfInterrupted(db)
|
|
387
456
|
expect(t).not.toBeNull()
|
|
388
457
|
expect(t!.ended_via).toBe('sigterm')
|
|
389
458
|
db.close()
|
|
@@ -393,30 +462,40 @@ describe('findMostRecentInterruptedTurn', () => {
|
|
|
393
462
|
const db = openTurnsDbInMemory()
|
|
394
463
|
recordTurnStart(db, { turnKey: 'bbb:1', chatId: 'bbb' })
|
|
395
464
|
recordTurnEnd(db, { turnKey: 'bbb:1', endedVia: 'restart' })
|
|
396
|
-
const t =
|
|
465
|
+
const t = findLatestTurnIfInterrupted(db)
|
|
397
466
|
expect(t).not.toBeNull()
|
|
398
467
|
expect(t!.ended_via).toBe('restart')
|
|
399
468
|
db.close()
|
|
400
469
|
})
|
|
401
470
|
|
|
402
|
-
it('
|
|
471
|
+
it('returns a timeout-stamped turn as interrupted', () => {
|
|
472
|
+
const db = openTurnsDbInMemory()
|
|
473
|
+
recordTurnStart(db, { turnKey: 'tmo:1', chatId: 'tmo' })
|
|
474
|
+
recordTurnEnd(db, { turnKey: 'tmo:1', endedVia: 'timeout' })
|
|
475
|
+
const t = findLatestTurnIfInterrupted(db)
|
|
476
|
+
expect(t).not.toBeNull()
|
|
477
|
+
expect(t!.ended_via).toBe('timeout')
|
|
478
|
+
db.close()
|
|
479
|
+
})
|
|
480
|
+
|
|
481
|
+
it('picks the most-recently-started turn (latest), not an older interrupted one', () => {
|
|
403
482
|
const db = openTurnsDbInMemory()
|
|
404
483
|
recordTurnStart(db, { turnKey: 'ccc:1', chatId: 'ccc' })
|
|
405
|
-
// Different started_at by waiting one ms; bun:sqlite stores the
|
|
406
|
-
// recordTurnStart call's Date.now() so we use raw insert below to be
|
|
407
|
-
// deterministic.
|
|
408
484
|
db.exec(`UPDATE turns SET started_at = 1000 WHERE turn_key = 'ccc:1'`)
|
|
409
485
|
recordTurnStart(db, { turnKey: 'ccc:2', chatId: 'ccc' })
|
|
410
486
|
db.exec(`UPDATE turns SET started_at = 2000 WHERE turn_key = 'ccc:2'`)
|
|
411
487
|
recordTurnEnd(db, { turnKey: 'ccc:1', endedVia: 'restart' })
|
|
412
488
|
recordTurnEnd(db, { turnKey: 'ccc:2', endedVia: 'sigterm' })
|
|
413
|
-
const t =
|
|
489
|
+
const t = findLatestTurnIfInterrupted(db)
|
|
414
490
|
expect(t).not.toBeNull()
|
|
415
491
|
expect(t!.turn_key).toBe('ccc:2')
|
|
416
492
|
db.close()
|
|
417
493
|
})
|
|
418
494
|
|
|
419
|
-
it('
|
|
495
|
+
it('a clean latest turn SHADOWS an older interrupted one (returns null)', () => {
|
|
496
|
+
// This is the inverse of the old findMostRecentInterruptedTurn bug: a
|
|
497
|
+
// completed resume (latest turn 'stop') must not resurface the stale
|
|
498
|
+
// interrupted turn on the next restart.
|
|
420
499
|
const db = openTurnsDbInMemory()
|
|
421
500
|
recordTurnStart(db, { turnKey: 'ddd:1', chatId: 'ddd' })
|
|
422
501
|
db.exec(`UPDATE turns SET started_at = 1000 WHERE turn_key = 'ddd:1'`)
|
|
@@ -424,9 +503,7 @@ describe('findMostRecentInterruptedTurn', () => {
|
|
|
424
503
|
db.exec(`UPDATE turns SET started_at = 2000 WHERE turn_key = 'ddd:2'`)
|
|
425
504
|
recordTurnEnd(db, { turnKey: 'ddd:1', endedVia: 'sigterm' })
|
|
426
505
|
recordTurnEnd(db, { turnKey: 'ddd:2', endedVia: 'stop' })
|
|
427
|
-
|
|
428
|
-
expect(t).not.toBeNull()
|
|
429
|
-
expect(t!.turn_key).toBe('ddd:1')
|
|
506
|
+
expect(findLatestTurnIfInterrupted(db)).toBeNull()
|
|
430
507
|
db.close()
|
|
431
508
|
})
|
|
432
509
|
})
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for telegram-plugin/gateway/resume-inbound-builder.ts
|
|
3
|
+
*
|
|
4
|
+
* Pure builders — no SQLite, no gateway. They run under bun test alongside
|
|
5
|
+
* the other telegram-plugin tests. The contract under test:
|
|
6
|
+
*
|
|
7
|
+
* - humanizeElapsed bucketing (moments / min / h / days, plus the
|
|
8
|
+
* negative/NaN guard).
|
|
9
|
+
* - buildResumeInterruptedInbound → source='resume_interrupted', resume
|
|
10
|
+
* framing, dedup anchor meta.resume_turn_key, thread routing.
|
|
11
|
+
* - buildResumeWatchdogReportInbound → source='resume_watchdog_timeout',
|
|
12
|
+
* report (not resume) framing, idle_ms passthrough.
|
|
13
|
+
* - selectResumeBuilder policy table.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, it, expect } from 'bun:test'
|
|
17
|
+
import {
|
|
18
|
+
humanizeElapsed,
|
|
19
|
+
buildResumeInterruptedInbound,
|
|
20
|
+
buildResumeWatchdogReportInbound,
|
|
21
|
+
selectResumeBuilder,
|
|
22
|
+
} from '../gateway/resume-inbound-builder.js'
|
|
23
|
+
import type { Turn, TurnEndedVia } from '../registry/turns-schema.js'
|
|
24
|
+
|
|
25
|
+
function makeTurn(overrides: Partial<Turn> = {}): Turn {
|
|
26
|
+
return {
|
|
27
|
+
turn_key: '12345:11',
|
|
28
|
+
chat_id: '12345',
|
|
29
|
+
thread_id: null,
|
|
30
|
+
started_at: 1_000_000,
|
|
31
|
+
ended_at: null,
|
|
32
|
+
ended_via: 'restart',
|
|
33
|
+
last_assistant_msg_id: null,
|
|
34
|
+
last_assistant_done: null,
|
|
35
|
+
last_user_msg_id: null,
|
|
36
|
+
user_prompt_preview: null,
|
|
37
|
+
assistant_reply_preview: null,
|
|
38
|
+
tool_call_count: null,
|
|
39
|
+
interrupt_reason: null,
|
|
40
|
+
created_at: 1_000_000,
|
|
41
|
+
updated_at: 1_000_000,
|
|
42
|
+
...overrides,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe('humanizeElapsed', () => {
|
|
47
|
+
it('returns "moments" under 45s', () => {
|
|
48
|
+
expect(humanizeElapsed(0)).toBe('moments')
|
|
49
|
+
expect(humanizeElapsed(44_000)).toBe('moments')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('buckets minutes under an hour', () => {
|
|
53
|
+
expect(humanizeElapsed(60_000)).toBe('~1 min')
|
|
54
|
+
expect(humanizeElapsed(5 * 60_000)).toBe('~5 min')
|
|
55
|
+
expect(humanizeElapsed(59 * 60_000)).toBe('~59 min')
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('buckets hours under a day', () => {
|
|
59
|
+
expect(humanizeElapsed(60 * 60_000)).toBe('~1h')
|
|
60
|
+
expect(humanizeElapsed(3 * 60 * 60_000)).toBe('~3h')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('buckets days at/over 24h with singular/plural', () => {
|
|
64
|
+
expect(humanizeElapsed(24 * 60 * 60_000)).toBe('~1 day')
|
|
65
|
+
expect(humanizeElapsed(50 * 60 * 60_000)).toBe('~2 days')
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('guards against negative / non-finite input', () => {
|
|
69
|
+
expect(humanizeElapsed(-5)).toBe('an unknown amount of time')
|
|
70
|
+
expect(humanizeElapsed(NaN)).toBe('an unknown amount of time')
|
|
71
|
+
expect(humanizeElapsed(Infinity)).toBe('an unknown amount of time')
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
describe('buildResumeInterruptedInbound', () => {
|
|
76
|
+
it('sets the resume_interrupted source and dedup anchor', () => {
|
|
77
|
+
const turn = makeTurn({ turn_key: 'abc:7', ended_via: 'sigterm' })
|
|
78
|
+
const msg = buildResumeInterruptedInbound({ turn, nowMs: turn.started_at + 3 * 60 * 60_000 })
|
|
79
|
+
expect(msg.type).toBe('inbound')
|
|
80
|
+
expect(msg.meta.source).toBe('resume_interrupted')
|
|
81
|
+
expect(msg.meta.resume_turn_key).toBe('abc:7')
|
|
82
|
+
expect(msg.meta.interrupted_via).toBe('sigterm')
|
|
83
|
+
expect(msg.user).toBe('switchroom')
|
|
84
|
+
expect(msg.userId).toBe(0)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('frames the elapsed time in the body and tells the model to resume, not ask', () => {
|
|
88
|
+
const turn = makeTurn()
|
|
89
|
+
const msg = buildResumeInterruptedInbound({ turn, nowMs: turn.started_at + 3 * 60 * 60_000 })
|
|
90
|
+
expect(msg.text).toContain('~3h')
|
|
91
|
+
expect(msg.text.toLowerCase()).toContain('interrupted')
|
|
92
|
+
expect(msg.text.toLowerCase()).toContain('do')
|
|
93
|
+
expect(msg.text).toContain('not ask whether to resume')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('defaults interrupted_via to restart when ended_via is null', () => {
|
|
97
|
+
const turn = makeTurn({ ended_via: null })
|
|
98
|
+
const msg = buildResumeInterruptedInbound({ turn })
|
|
99
|
+
expect(msg.meta.interrupted_via).toBe('restart')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('includes the prompt preview when present and carries original_prompt meta', () => {
|
|
103
|
+
const turn = makeTurn({ user_prompt_preview: 'refactor the auth module' })
|
|
104
|
+
const msg = buildResumeInterruptedInbound({ turn })
|
|
105
|
+
expect(msg.text).toContain('refactor the auth module')
|
|
106
|
+
expect(msg.meta.original_prompt).toBe('refactor the auth module')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('truncates a long prompt preview in the body', () => {
|
|
110
|
+
const long = 'x'.repeat(300)
|
|
111
|
+
const turn = makeTurn({ user_prompt_preview: long })
|
|
112
|
+
const msg = buildResumeInterruptedInbound({ turn })
|
|
113
|
+
expect(msg.text).toContain('…')
|
|
114
|
+
expect(msg.text).not.toContain('x'.repeat(200))
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('routes to the forum thread when thread_id is numeric', () => {
|
|
118
|
+
const turn = makeTurn({ thread_id: '99' })
|
|
119
|
+
const msg = buildResumeInterruptedInbound({ turn })
|
|
120
|
+
expect(msg.threadId).toBe(99)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('omits threadId for a non-forum (null thread_id) chat', () => {
|
|
124
|
+
const msg = buildResumeInterruptedInbound({ turn: makeTurn({ thread_id: null }) })
|
|
125
|
+
expect(msg.threadId).toBeUndefined()
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
describe('buildResumeWatchdogReportInbound', () => {
|
|
130
|
+
it('sets the resume_watchdog_timeout source and idle_ms passthrough', () => {
|
|
131
|
+
const turn = makeTurn({ ended_via: 'timeout' })
|
|
132
|
+
const msg = buildResumeWatchdogReportInbound({ turn, idleMs: 300_000 })
|
|
133
|
+
expect(msg.meta.source).toBe('resume_watchdog_timeout')
|
|
134
|
+
expect(msg.meta.interrupted_via).toBe('timeout')
|
|
135
|
+
expect(msg.meta.idle_ms).toBe('300000')
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('reports the hang honestly and asks rather than resuming', () => {
|
|
139
|
+
const turn = makeTurn({ ended_via: 'timeout' })
|
|
140
|
+
const msg = buildResumeWatchdogReportInbound({ turn, idleMs: 300_000 })
|
|
141
|
+
expect(msg.text.toLowerCase()).toContain('hang-watchdog')
|
|
142
|
+
expect(msg.text).toContain('no observable progress')
|
|
143
|
+
expect(msg.text).toContain('Do NOT silently resume')
|
|
144
|
+
expect(msg.text.toLowerCase()).toContain('take a different angle')
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('mentions tool-call count when the turn ran tools before stalling', () => {
|
|
148
|
+
const turn = makeTurn({ ended_via: 'timeout', tool_call_count: 4 })
|
|
149
|
+
const msg = buildResumeWatchdogReportInbound({ turn, idleMs: 300_000 })
|
|
150
|
+
expect(msg.text).toContain('4 tool calls')
|
|
151
|
+
expect(msg.meta.tool_call_count).toBe('4')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('singularizes a single tool call', () => {
|
|
155
|
+
const turn = makeTurn({ ended_via: 'timeout', tool_call_count: 1 })
|
|
156
|
+
const msg = buildResumeWatchdogReportInbound({ turn, idleMs: 300_000 })
|
|
157
|
+
expect(msg.text).toContain('1 tool call')
|
|
158
|
+
expect(msg.text).not.toContain('1 tool calls')
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('omits the tool clause when no tools ran', () => {
|
|
162
|
+
const turn = makeTurn({ ended_via: 'timeout', tool_call_count: 0 })
|
|
163
|
+
const msg = buildResumeWatchdogReportInbound({ turn, idleMs: 300_000 })
|
|
164
|
+
expect(msg.text).not.toContain('tool call')
|
|
165
|
+
})
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
describe('selectResumeBuilder', () => {
|
|
169
|
+
const cases: Array<[TurnEndedVia | null, 'resume' | 'report' | null]> = [
|
|
170
|
+
['timeout', 'report'],
|
|
171
|
+
['restart', 'resume'],
|
|
172
|
+
['sigterm', 'resume'],
|
|
173
|
+
['unknown', 'resume'],
|
|
174
|
+
[null, 'resume'],
|
|
175
|
+
['stop', null],
|
|
176
|
+
]
|
|
177
|
+
for (const [endedVia, expected] of cases) {
|
|
178
|
+
it(`maps ended_via=${String(endedVia)} → ${String(expected)}`, () => {
|
|
179
|
+
expect(selectResumeBuilder(endedVia)).toBe(expected)
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
})
|
|
@@ -54,7 +54,6 @@ function makeDeps(
|
|
|
54
54
|
markdownToHtml: (t) => `<b>${t}</b>`,
|
|
55
55
|
escapeMarkdownV2: (t) => `\\${t}\\`,
|
|
56
56
|
repairEscapedWhitespace: (t) => t,
|
|
57
|
-
takeHandoffPrefix: () => '',
|
|
58
57
|
assertAllowedChat: () => {},
|
|
59
58
|
resolveThreadId: (_, explicit) => (explicit != null ? Number(explicit) : undefined),
|
|
60
59
|
disableLinkPreview: true,
|