switchroom 0.18.3 → 0.18.6

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 (101) hide show
  1. package/dist/agent-scheduler/index.js +2 -1
  2. package/dist/auth-broker/index.js +2 -1
  3. package/dist/cli/notion-write-pretool.mjs +2 -1
  4. package/dist/cli/switchroom.js +119 -74
  5. package/dist/host-control/main.js +3 -2
  6. package/dist/vault/approvals/kernel-server.js +2 -1
  7. package/dist/vault/broker/server.js +37 -8
  8. package/package.json +1 -1
  9. package/skills/switchroom-release/SKILL.md +78 -0
  10. package/telegram-plugin/auth-snapshot-format.ts +15 -1
  11. package/telegram-plugin/dist/gateway/gateway.js +922 -459
  12. package/telegram-plugin/gateway/gateway.ts +260 -21
  13. package/telegram-plugin/gateway/resolve-person.ts +304 -0
  14. package/telegram-plugin/gateway/unhandled-rejection-policy.ts +21 -1
  15. package/telegram-plugin/hooks/silent-end-scan.mjs +164 -40
  16. package/telegram-plugin/operator-events.ts +21 -0
  17. package/telegram-plugin/pending-work-progress.ts +42 -7
  18. package/telegram-plugin/quota-bar-format.ts +360 -0
  19. package/telegram-plugin/subagent-watcher.ts +27 -9
  20. package/telegram-plugin/tests/auth-snapshot-format.test.ts +21 -0
  21. package/telegram-plugin/tests/gateway-boot-marker-clear.test.ts +3 -3
  22. package/telegram-plugin/tests/operator-events.test.ts +16 -0
  23. package/telegram-plugin/tests/pending-work-progress.test.ts +116 -3
  24. package/telegram-plugin/tests/quota-bar-format.test.ts +444 -0
  25. package/telegram-plugin/tests/resolve-person.test.ts +290 -0
  26. package/telegram-plugin/tests/silent-end-interrupt-stop-integration.test.ts +53 -0
  27. package/telegram-plugin/tests/silent-end-interrupt-stop-scan.test.ts +138 -0
  28. package/telegram-plugin/tests/subagent-watcher.test.ts +61 -0
  29. package/telegram-plugin/tests/unhandled-rejection-policy.test.ts +19 -0
  30. package/telegram-plugin/tests/worker-activity-feed.test.ts +97 -0
  31. package/telegram-plugin/worker-activity-feed.ts +220 -15
  32. package/profiles/default/CLAUDE.md +0 -116
  33. package/telegram-plugin/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
  34. package/vendor/hindsight-memory/scripts/__pycache__/directive_verify.cpython-313.pyc +0 -0
  35. package/vendor/hindsight-memory/scripts/__pycache__/drain_pending.cpython-313.pyc +0 -0
  36. package/vendor/hindsight-memory/scripts/__pycache__/recall.cpython-313.pyc +0 -0
  37. package/vendor/hindsight-memory/scripts/__pycache__/retain.cpython-313.pyc +0 -0
  38. package/vendor/hindsight-memory/scripts/__pycache__/session_end.cpython-313.pyc +0 -0
  39. package/vendor/hindsight-memory/scripts/lib/__pycache__/__init__.cpython-313.pyc +0 -0
  40. package/vendor/hindsight-memory/scripts/lib/__pycache__/bank.cpython-313.pyc +0 -0
  41. package/vendor/hindsight-memory/scripts/lib/__pycache__/client.cpython-313.pyc +0 -0
  42. package/vendor/hindsight-memory/scripts/lib/__pycache__/config.cpython-313.pyc +0 -0
  43. package/vendor/hindsight-memory/scripts/lib/__pycache__/content.cpython-313.pyc +0 -0
  44. package/vendor/hindsight-memory/scripts/lib/__pycache__/daemon.cpython-313.pyc +0 -0
  45. package/vendor/hindsight-memory/scripts/lib/__pycache__/directives.cpython-313.pyc +0 -0
  46. package/vendor/hindsight-memory/scripts/lib/__pycache__/gateway_ipc.cpython-313.pyc +0 -0
  47. package/vendor/hindsight-memory/scripts/lib/__pycache__/llm.cpython-313.pyc +0 -0
  48. package/vendor/hindsight-memory/scripts/lib/__pycache__/pending.cpython-313.pyc +0 -0
  49. package/vendor/hindsight-memory/scripts/lib/__pycache__/state.cpython-313.pyc +0 -0
  50. package/vendor/hindsight-memory/scripts/lib/__pycache__/switchroom_envelope.cpython-313.pyc +0 -0
  51. package/vendor/hindsight-memory/scripts/tests/__pycache__/__init__.cpython-313.pyc +0 -0
  52. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_config_client_casts.cpython-313-pytest-9.1.1.pyc +0 -0
  53. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_config_client_casts.cpython-313.pyc +0 -0
  54. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_directive_capture_nudge.cpython-313-pytest-9.1.1.pyc +0 -0
  55. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_directive_capture_nudge.cpython-313.pyc +0 -0
  56. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_directive_verify.cpython-313-pytest-9.1.1.pyc +0 -0
  57. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_directive_verify.cpython-313.pyc +0 -0
  58. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_directives.cpython-313-pytest-9.1.1.pyc +0 -0
  59. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_directives.cpython-313.pyc +0 -0
  60. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_gateway_ipc.cpython-313-pytest-9.1.1.pyc +0 -0
  61. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_gateway_ipc.cpython-313.pyc +0 -0
  62. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_context_slice.cpython-313-pytest-9.1.1.pyc +0 -0
  63. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_context_slice.cpython-313.pyc +0 -0
  64. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_integration.cpython-313-pytest-9.1.1.pyc +0 -0
  65. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_integration.cpython-313.pyc +0 -0
  66. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_tag_filters.cpython-313-pytest-9.1.1.pyc +0 -0
  67. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_tag_filters.cpython-313.pyc +0 -0
  68. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_topic_filter.cpython-313-pytest-9.1.1.pyc +0 -0
  69. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_topic_filter.cpython-313.pyc +0 -0
  70. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_trivial_skip.cpython-313-pytest-9.1.1.pyc +0 -0
  71. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_recall_trivial_skip.cpython-313.pyc +0 -0
  72. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_retain_window.cpython-313-pytest-9.1.1.pyc +0 -0
  73. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_retain_window.cpython-313.pyc +0 -0
  74. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_sender_routing.cpython-313-pytest-9.1.1.pyc +0 -0
  75. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_sender_routing.cpython-313.pyc +0 -0
  76. package/vendor/hindsight-memory/scripts/tests/__pycache__/test_switchroom_envelope.cpython-313-pytest-9.1.1.pyc +0 -0
  77. package/vendor/hindsight-memory/tests/__pycache__/conftest.cpython-313-pytest-9.0.3.pyc +0 -0
  78. package/vendor/hindsight-memory/tests/__pycache__/conftest.cpython-313-pytest-9.1.1.pyc +0 -0
  79. package/vendor/hindsight-memory/tests/__pycache__/test_bank.cpython-313-pytest-9.1.1.pyc +0 -0
  80. package/vendor/hindsight-memory/tests/__pycache__/test_bank.cpython-313.pyc +0 -0
  81. package/vendor/hindsight-memory/tests/__pycache__/test_client.cpython-313-pytest-9.1.1.pyc +0 -0
  82. package/vendor/hindsight-memory/tests/__pycache__/test_client.cpython-313.pyc +0 -0
  83. package/vendor/hindsight-memory/tests/__pycache__/test_config.cpython-313-pytest-9.0.3.pyc +0 -0
  84. package/vendor/hindsight-memory/tests/__pycache__/test_config.cpython-313-pytest-9.1.1.pyc +0 -0
  85. package/vendor/hindsight-memory/tests/__pycache__/test_config.cpython-313.pyc +0 -0
  86. package/vendor/hindsight-memory/tests/__pycache__/test_content.cpython-313-pytest-9.1.1.pyc +0 -0
  87. package/vendor/hindsight-memory/tests/__pycache__/test_content.cpython-313.pyc +0 -0
  88. package/vendor/hindsight-memory/tests/__pycache__/test_drain_pending.cpython-313-pytest-9.1.1.pyc +0 -0
  89. package/vendor/hindsight-memory/tests/__pycache__/test_drain_pending.cpython-313.pyc +0 -0
  90. package/vendor/hindsight-memory/tests/__pycache__/test_hooks.cpython-313-pytest-9.1.1.pyc +0 -0
  91. package/vendor/hindsight-memory/tests/__pycache__/test_hooks.cpython-313.pyc +0 -0
  92. package/vendor/hindsight-memory/tests/__pycache__/test_manifest.cpython-313-pytest-9.1.1.pyc +0 -0
  93. package/vendor/hindsight-memory/tests/__pycache__/test_manifest.cpython-313.pyc +0 -0
  94. package/vendor/hindsight-memory/tests/__pycache__/test_pending.cpython-313-pytest-9.1.1.pyc +0 -0
  95. package/vendor/hindsight-memory/tests/__pycache__/test_pending.cpython-313.pyc +0 -0
  96. package/vendor/hindsight-memory/tests/__pycache__/test_recall_exit_codes.cpython-313-pytest-9.1.1.pyc +0 -0
  97. package/vendor/hindsight-memory/tests/__pycache__/test_recall_exit_codes.cpython-313.pyc +0 -0
  98. package/vendor/hindsight-memory/tests/__pycache__/test_session_end_pending.cpython-313-pytest-9.1.1.pyc +0 -0
  99. package/vendor/hindsight-memory/tests/__pycache__/test_session_end_pending.cpython-313.pyc +0 -0
  100. package/vendor/hindsight-memory/tests/__pycache__/test_state.cpython-313-pytest-9.1.1.pyc +0 -0
  101. package/vendor/hindsight-memory/tests/__pycache__/test_state.cpython-313.pyc +0 -0
@@ -49,9 +49,11 @@ function setup(): Capture {
49
49
  }
50
50
 
51
51
  async function flush(): Promise<void> {
52
- // Allow the fire-and-forget promise chain in tick() to settle.
53
- await Promise.resolve()
54
- await Promise.resolve()
52
+ // Allow the fire-and-forget promise chain in tick() to settle. The chain
53
+ // is `.then(editMessage).then(emitMetric).catch(...)` with an async
54
+ // editMessage, so it spans several microtask hops — a single macrotask
55
+ // boundary (setTimeout 0) drains them all deterministically.
56
+ await new Promise((r) => setTimeout(r, 0))
55
57
  }
56
58
 
57
59
  describe('pending-work-progress', () => {
@@ -558,4 +560,115 @@ describe('pending-work-progress', () => {
558
560
  await flush()
559
561
  expect(cap.edits).toHaveLength(1)
560
562
  })
563
+
564
+ // ─── review finding #2: transport-hiccup classification ────────────────────
565
+ // A best-effort liveness surface must not log a scary "edit failed" warning
566
+ // for recoverable/permanent transport outcomes, and must stop retrying a
567
+ // dead anchor. "not modified" counts as success; "gone" clears state; 429 /
568
+ // transient leave state for the next tick — all silently.
569
+ it('#2: "message is not modified" counts as a successful edit (no warning, metric emitted)', async () => {
570
+ const cap = setup()
571
+ const errs: string[] = []
572
+ const origStderr = process.stderr.write.bind(process.stderr)
573
+ process.stderr.write = ((s: string) => { errs.push(s); return true }) as typeof process.stderr.write
574
+ try {
575
+ let calls = 0
576
+ __setDepsForTests({
577
+ editMessage: async () => {
578
+ calls++
579
+ if (calls === 1) throw new Error('Bad Request: message is not modified')
580
+ cap.edits.push({} as PendingProgressEditCtx)
581
+ },
582
+ emitMetric: (e) => cap.metrics.push(e),
583
+ nowMs: () => cap.now,
584
+ })
585
+ startTurn(KEY)
586
+ noteAsyncDispatch(KEY)
587
+ noteOutbound(KEY, { messageId: 100, text: 'working' })
588
+ noteTurnEnd(KEY)
589
+
590
+ cap.now = EDIT_INTERVAL_MS
591
+ __tickForTests(cap.now)
592
+ // The .catch hop needs an extra microtask drain beyond the 2-await flush.
593
+ await flush()
594
+ await Promise.resolve()
595
+ // The not-modified outcome is success — an `edited` metric was emitted.
596
+ expect(cap.metrics.some((m) => m.kind === 'pending_progress_edited')).toBe(true)
597
+ // No scary "edit failed" stderr for a transport-class outcome.
598
+ expect(errs.some((e) => e.includes('edit failed'))).toBe(false)
599
+ } finally {
600
+ process.stderr.write = origStderr
601
+ }
602
+ })
603
+
604
+ it('#2: a gone anchor (message not found) clears state so the tick stops retrying it, silently', async () => {
605
+ const cap = setup()
606
+ const errs: string[] = []
607
+ const origStderr = process.stderr.write.bind(process.stderr)
608
+ process.stderr.write = ((s: string) => { errs.push(s); return true }) as typeof process.stderr.write
609
+ try {
610
+ let calls = 0
611
+ __setDepsForTests({
612
+ editMessage: async () => {
613
+ calls++
614
+ throw new Error('Bad Request: message to edit not found')
615
+ },
616
+ emitMetric: (e) => cap.metrics.push(e),
617
+ nowMs: () => cap.now,
618
+ })
619
+ startTurn(KEY)
620
+ noteAsyncDispatch(KEY)
621
+ noteOutbound(KEY, { messageId: 100, text: 'working' })
622
+ noteTurnEnd(KEY)
623
+
624
+ cap.now = EDIT_INTERVAL_MS
625
+ __tickForTests(cap.now)
626
+ await flush()
627
+ await Promise.resolve()
628
+ expect(calls).toBe(1)
629
+ // No warning for a transport-gone outcome.
630
+ expect(errs.some((e) => e.includes('edit failed'))).toBe(false)
631
+
632
+ // Next tick — the anchor was cleared, so no retry against the dead id.
633
+ cap.now = EDIT_INTERVAL_MS * 3
634
+ __tickForTests(cap.now)
635
+ await flush()
636
+ await Promise.resolve()
637
+ expect(calls).toBe(1) // no second attempt
638
+ } finally {
639
+ process.stderr.write = origStderr
640
+ }
641
+ })
642
+
643
+ it('#2: a 429 / transient error leaves state intact for the next tick, silently', async () => {
644
+ const cap = setup()
645
+ const errs: string[] = []
646
+ const origStderr = process.stderr.write.bind(process.stderr)
647
+ process.stderr.write = ((s: string) => { errs.push(s); return true }) as typeof process.stderr.write
648
+ try {
649
+ let calls = 0
650
+ __setDepsForTests({
651
+ editMessage: async () => {
652
+ calls++
653
+ throw { error_code: 429, parameters: { retry_after: 1 }, message: 'Too Many Requests' }
654
+ },
655
+ emitMetric: (e) => cap.metrics.push(e),
656
+ nowMs: () => cap.now,
657
+ })
658
+ startTurn(KEY)
659
+ noteAsyncDispatch(KEY)
660
+ noteOutbound(KEY, { messageId: 100, text: 'working' })
661
+ noteTurnEnd(KEY)
662
+
663
+ cap.now = EDIT_INTERVAL_MS
664
+ __tickForTests(cap.now)
665
+ await flush()
666
+ await Promise.resolve()
667
+ // State survives — the anchor is still there to retry next tick.
668
+ expect(__getStateForTests(KEY)?.anchorMessageId).toBe(100)
669
+ expect(errs.some((e) => e.includes('edit failed'))).toBe(false)
670
+ } finally {
671
+ process.stderr.write = origStderr
672
+ }
673
+ })
561
674
  })
@@ -0,0 +1,444 @@
1
+ /**
2
+ * Tests for quota-bar-format.ts — bar-building, time-left formatting, and
3
+ * the full block assembly. Pure functions, frozen-clock fixtures.
4
+ */
5
+ import { describe, it, expect } from 'vitest';
6
+ import {
7
+ pickDot,
8
+ formatTimeLeft,
9
+ elapsedFraction,
10
+ buildBar,
11
+ accountStatus,
12
+ formatWindowRow,
13
+ renderQuotaBarAccount,
14
+ renderQuotaBarBlockFromListState,
15
+ renderUsageCard,
16
+ } from '../quota-bar-format.js';
17
+ import type { AccountSnapshot } from '../auth-snapshot-format.js';
18
+ import type { QuotaUtilization } from '../quota-check.js';
19
+ import type { ListStateData } from '../../src/auth/broker/client.js';
20
+
21
+ const NOW = new Date('2026-07-10T00:00:00.000Z');
22
+
23
+ function quota(part: Partial<QuotaUtilization>): QuotaUtilization {
24
+ return {
25
+ fiveHourUtilizationPct: 0,
26
+ sevenDayUtilizationPct: 0,
27
+ fiveHourResetAt: null,
28
+ sevenDayResetAt: null,
29
+ representativeClaim: null,
30
+ overageStatus: null,
31
+ overageDisabledReason: null,
32
+ ...part,
33
+ };
34
+ }
35
+
36
+ // ── pickDot ──────────────────────────────────────────────────────────
37
+
38
+ describe('pickDot', () => {
39
+ it('green under 50%', () => {
40
+ expect(pickDot(0)).toBe('🟢');
41
+ expect(pickDot(49)).toBe('🟢');
42
+ });
43
+ it('yellow 50-89%', () => {
44
+ expect(pickDot(50)).toBe('🟡');
45
+ expect(pickDot(89)).toBe('🟡');
46
+ });
47
+ it('red 90%+', () => {
48
+ expect(pickDot(90)).toBe('🔴');
49
+ expect(pickDot(100)).toBe('🔴');
50
+ });
51
+ });
52
+
53
+ // ── formatTimeLeft ───────────────────────────────────────────────────
54
+
55
+ describe('formatTimeLeft', () => {
56
+ it('returns "resets now" when the target is null', () => {
57
+ expect(formatTimeLeft(null, NOW)).toBe('resets now');
58
+ });
59
+ it('returns "resets now" when the target has already passed', () => {
60
+ expect(formatTimeLeft(new Date(NOW.getTime() - 1000), NOW)).toBe('resets now');
61
+ });
62
+ it('returns "resets now" when the target is exactly now', () => {
63
+ expect(formatTimeLeft(new Date(NOW.getTime()), NOW)).toBe('resets now');
64
+ });
65
+ it('formats sub-hour deltas as minutes', () => {
66
+ expect(formatTimeLeft(new Date(NOW.getTime() + 45 * 60_000), NOW)).toBe('45m left');
67
+ });
68
+ it('formats sub-day deltas as compact hours+minutes', () => {
69
+ expect(formatTimeLeft(new Date(NOW.getTime() + (4 * 60 + 20) * 60_000), NOW)).toBe('4h20m left');
70
+ });
71
+ it('formats multi-day deltas as compact days+hours', () => {
72
+ expect(formatTimeLeft(new Date(NOW.getTime() + (3 * 24 + 1) * 60 * 60_000), NOW)).toBe('3d1h left');
73
+ });
74
+ it('drops the hours segment when exactly on a day boundary', () => {
75
+ expect(formatTimeLeft(new Date(NOW.getTime() + 2 * 24 * 60 * 60_000), NOW)).toBe('2d left');
76
+ });
77
+ it('returns "resets now" (never "NaNm left") for an invalid reset date', () => {
78
+ expect(formatTimeLeft(new Date('not-a-date'), NOW)).toBe('resets now');
79
+ expect(formatTimeLeft(new Date(NaN), NOW)).toBe('resets now');
80
+ });
81
+ });
82
+
83
+ // ── elapsedFraction ──────────────────────────────────────────────────
84
+
85
+ describe('elapsedFraction', () => {
86
+ const fiveHourMs = 5 * 60 * 60 * 1000;
87
+ it('is 0 when there is no reset timestamp', () => {
88
+ expect(elapsedFraction(null, fiveHourMs, NOW)).toBe(0);
89
+ });
90
+ it('is 0 when the reset has already passed', () => {
91
+ expect(elapsedFraction(new Date(NOW.getTime() - 1000), fiveHourMs, NOW)).toBe(0);
92
+ });
93
+ it('is close to 1 just before reset', () => {
94
+ const frac = elapsedFraction(new Date(NOW.getTime() + 60_000), fiveHourMs, NOW);
95
+ expect(frac).toBeGreaterThan(0.99);
96
+ expect(frac).toBeLessThanOrEqual(1);
97
+ });
98
+ it('is ~0.5 halfway through the window', () => {
99
+ const frac = elapsedFraction(new Date(NOW.getTime() + fiveHourMs / 2), fiveHourMs, NOW);
100
+ expect(frac).toBeCloseTo(0.5, 5);
101
+ });
102
+ it('is 0 (never NaN) for an invalid reset date', () => {
103
+ expect(elapsedFraction(new Date('not-a-date'), fiveHourMs, NOW)).toBe(0);
104
+ expect(elapsedFraction(new Date(NaN), fiveHourMs, NOW)).toBe(0);
105
+ });
106
+ });
107
+
108
+ // ── buildBar ─────────────────────────────────────────────────────────
109
+
110
+ describe('buildBar', () => {
111
+ it('is all empty cells with a tick at 0% pct and 0 elapsed fraction', () => {
112
+ expect(buildBar(0, 0)).toBe('┃░░░░░░░░░');
113
+ });
114
+ it('the tick always overrides the fill, even at 100% pct', () => {
115
+ expect(buildBar(100, 0.5)).toBe('█████┃████');
116
+ });
117
+ it('the tick overrides fill at the very last cell (100% pct, 100% elapsed)', () => {
118
+ expect(buildBar(100, 1)).toBe('█████████┃');
119
+ });
120
+ it('is exactly 10 characters for any pct/fraction combination', () => {
121
+ for (const pct of [0, 1, 33, 47, 89, 99, 100]) {
122
+ for (const frac of [0, 0.25, 0.5, 0.75, 1]) {
123
+ expect(buildBar(pct, frac).length).toBe(10);
124
+ }
125
+ }
126
+ });
127
+ it('places the tick outside the filled region when there is room', () => {
128
+ // 20% filled (2 cells) + tick at fraction 0.8 → tick lands past the fill.
129
+ const bar = buildBar(20, 0.8);
130
+ expect(bar[0]).toBe('█');
131
+ expect(bar[1]).toBe('█');
132
+ expect(bar).toContain('┃');
133
+ });
134
+ it('over-pace: quota fill visibly punches through past the time-elapsed tick', () => {
135
+ // 80% quota used, only 30% of the way through the window — burning
136
+ // faster than the clock. Fill (8 cells) extends past the tick
137
+ // (index round(0.3*9)=3), so filled cells appear on BOTH sides of the
138
+ // tick — that's the "punch through" signal the operator asked for.
139
+ const bar = buildBar(80, 0.3);
140
+ expect(bar).toBe('███┃████░░');
141
+ const tickIndex = bar.indexOf('┃');
142
+ const filledAfterTick = bar.slice(tickIndex + 1).includes('█');
143
+ expect(filledAfterTick).toBe(true); // fill visibly overtakes the pace marker
144
+ });
145
+ });
146
+
147
+ // ── accountStatus ────────────────────────────────────────────────────
148
+
149
+ describe('accountStatus', () => {
150
+ it('active wins even if the broker also flags the account exhausted', () => {
151
+ expect(accountStatus(true, true)).toBe('active');
152
+ });
153
+ it('reports exhausted for a non-active exhausted account', () => {
154
+ expect(accountStatus(false, true)).toBe('exhausted');
155
+ });
156
+ it('reports idle for a non-active, non-exhausted account', () => {
157
+ expect(accountStatus(false, false)).toBe('idle');
158
+ });
159
+ });
160
+
161
+ // ── formatWindowRow / renderQuotaBarAccount ─────────────────────────
162
+
163
+ describe('formatWindowRow', () => {
164
+ it('renders the locked row shape', () => {
165
+ const row = formatWindowRow('5h', 0, null, NOW);
166
+ expect(row).toBe('- 🟢 5h `[┃░░░░░░░░░] 0% / resets now`');
167
+ });
168
+ it('clamps a >100% probe value for display', () => {
169
+ const row = formatWindowRow('7d', 101, null, NOW);
170
+ expect(row).toContain('100%');
171
+ });
172
+ });
173
+
174
+ describe('renderQuotaBarAccount', () => {
175
+ it('renders a title line + two window rows', () => {
176
+ const q = quota({
177
+ fiveHourUtilizationPct: 0,
178
+ sevenDayUtilizationPct: 47,
179
+ sevenDayResetAt: new Date(NOW.getTime() + (3 * 24 + 1) * 60 * 60_000),
180
+ });
181
+ const lines = renderQuotaBarAccount('ken@example.com', true, false, q, NOW);
182
+ expect(lines).toHaveLength(3);
183
+ expect(lines[0]).toBe('- **ken@example.com** (active)');
184
+ expect(lines[1]).toBe('- 🟢 5h `[┃░░░░░░░░░] 0% / resets now`');
185
+ expect(lines[2]).toContain('🟢 7d');
186
+ expect(lines[2]).toContain('47% / 3d1h left');
187
+ });
188
+ it('escapes markdown-significant characters in the label (bold run, not a code span)', () => {
189
+ // Regression: the title line wraps label in **bold**, so a label
190
+ // containing `**` or `[...]` must be backslash-escaped (escapeMarkdown),
191
+ // not defused with codeSpanSafe (which only strips backticks and would
192
+ // leave `*`/`[`/`]` live, breaking the bold run or injecting a link).
193
+ const lines = renderQuotaBarAccount('a**b[x](evil)_c', false, false, null, NOW);
194
+ expect(lines[0]).toBe('- **a\\*\\*b\\[x\\](evil)\\_c** (idle)');
195
+ });
196
+ it('exhausted account at 0% utilization on both windows still shows exhausted status with healthy dots', () => {
197
+ const q = quota({ fiveHourUtilizationPct: 0, sevenDayUtilizationPct: 0 });
198
+ const lines = renderQuotaBarAccount('done@example.com', false, true, q, NOW);
199
+ expect(lines[0]).toBe('- **done@example.com** (exhausted)');
200
+ expect(lines[1]).toContain('🟢 5h');
201
+ expect(lines[2]).toContain('🟢 7d');
202
+ });
203
+ it('renders a distinct probe-failed warning (never a healthy green 0% bar) when quota is null', () => {
204
+ // #2959 blocking bug: a failed probe must NOT be indistinguishable from a
205
+ // fresh account with full headroom (🟢 0%). It must carry a ⚠️ glyph and a
206
+ // "no data" label, with no green dot and no fake 0% bar.
207
+ const lines = renderQuotaBarAccount('nobody@example.com', false, false, null, NOW);
208
+ expect(lines).toEqual([
209
+ '- **nobody@example.com** (idle)',
210
+ '- ⚠️ 5h `no data — probe failed`',
211
+ '- ⚠️ 7d `no data — probe failed`',
212
+ ]);
213
+ const body = lines.join('\n');
214
+ expect(body).not.toContain('🟢');
215
+ expect(body).not.toContain('0%');
216
+ });
217
+
218
+ it('renders a distinct thin-probe warning (never a healthy green 0% bar) when the probe is thin', () => {
219
+ // A thin/headerless probe carries no real utilization — must degrade to the
220
+ // same ⚠️ data-quality gap, not a confident 🟢 0%.
221
+ const thin = quota({ fiveHourUtilPresent: false, sevenDayUtilPresent: false });
222
+ const lines = renderQuotaBarAccount('thin@example.com', false, false, thin, NOW);
223
+ expect(lines).toEqual([
224
+ '- **thin@example.com** (idle)',
225
+ '- ⚠️ 5h `no data — thin probe`',
226
+ '- ⚠️ 7d `no data — thin probe`',
227
+ ]);
228
+ expect(lines.join('\n')).not.toContain('🟢');
229
+ });
230
+ });
231
+
232
+ // ── renderQuotaBarBlockFromListState ────────────────────────────────
233
+
234
+ describe('renderQuotaBarBlockFromListState', () => {
235
+ it('renders the locked multi-account block shape', () => {
236
+ const state: ListStateData = {
237
+ active: 'ken@example.com',
238
+ fallback_order: ['ken@example.com', 'alt@example.com'],
239
+ accounts: [
240
+ {
241
+ label: 'ken@example.com',
242
+ exhausted: false,
243
+ last_quota: {
244
+ fiveHourUtilizationPct: 0,
245
+ sevenDayUtilizationPct: 47,
246
+ fiveHourResetAt: new Date(NOW.getTime() + 60_000).toISOString(),
247
+ sevenDayResetAt: new Date(NOW.getTime() + (3 * 24 + 1) * 60 * 60_000).toISOString(),
248
+ representativeClaim: null,
249
+ overageStatus: null,
250
+ overageDisabledReason: null,
251
+ capturedAt: NOW.getTime(),
252
+ },
253
+ },
254
+ {
255
+ label: 'alt@example.com',
256
+ exhausted: true,
257
+ last_quota: {
258
+ fiveHourUtilizationPct: 0,
259
+ sevenDayUtilizationPct: 100,
260
+ fiveHourResetAt: new Date(NOW.getTime() - 60_000).toISOString(),
261
+ sevenDayResetAt: new Date(NOW.getTime() + 2 * 24 * 60 * 60_000 + 16 * 60 * 60_000).toISOString(),
262
+ representativeClaim: null,
263
+ overageStatus: null,
264
+ overageDisabledReason: null,
265
+ capturedAt: NOW.getTime(),
266
+ },
267
+ },
268
+ ],
269
+ agents: [],
270
+ consumers: [],
271
+ };
272
+ const block = renderQuotaBarBlockFromListState(state, { now: NOW });
273
+ const lines = block.split('\n');
274
+ expect(lines).toEqual([
275
+ '- **ken@example.com** (active)',
276
+ '- 🟢 5h `[░░░░░░░░░┃] 0% / 1m left`',
277
+ '- 🟢 7d `[█████┃░░░░] 47% / 3d1h left`',
278
+ '- **alt@example.com** (exhausted)',
279
+ '- 🟢 5h `[┃░░░░░░░░░] 0% / resets now`',
280
+ '- 🔴 7d `[██████┃███] 100% / 2d16h left`',
281
+ ]);
282
+ });
283
+ });
284
+
285
+ // ── renderUsageCard (bar block only — /usage no longer appends the ─────
286
+ // Format 2 table underneath; see gateway.ts's `bot.command('usage', ...)`)
287
+
288
+ describe('renderUsageCard', () => {
289
+ function snap(part: Partial<AccountSnapshot> & { label: string }): AccountSnapshot {
290
+ return {
291
+ isActive: false,
292
+ quota: null,
293
+ ...part,
294
+ };
295
+ }
296
+
297
+ const snapshots: AccountSnapshot[] = [
298
+ snap({
299
+ label: 'ken@example.com',
300
+ isActive: true,
301
+ quota: quota({
302
+ fiveHourUtilizationPct: 0,
303
+ sevenDayUtilizationPct: 47,
304
+ fiveHourResetAt: new Date(NOW.getTime() + 60 * 60_000),
305
+ sevenDayResetAt: new Date(NOW.getTime() + (3 * 24 + 1) * 60 * 60_000),
306
+ }),
307
+ }),
308
+ ];
309
+ const exhausted = new Map<string, boolean>([['ken@example.com', false]]);
310
+
311
+ it('renders the quota-bar block plus a recommendation + freshness footer — no Format 2 table', () => {
312
+ const out = renderUsageCard(snapshots, exhausted, { now: NOW });
313
+ const lines = out.split('\n');
314
+ // Bar block — account title + two window rows with the ASCII bar.
315
+ expect(lines[0]).toBe('- **ken@example.com** (active)');
316
+ expect(out).toContain('- 🟢 5h `[');
317
+ expect(out).toContain('- 🟢 7d `[');
318
+ // Reset info is carried by the bar rows themselves (relative
319
+ // time-left), not a separate table.
320
+ expect(out).toContain('1h0m left');
321
+ expect(out).toContain('left');
322
+ // The old Format 2 TABLE must be fully gone from this card.
323
+ expect(out).not.toContain('🔋 **Auth — fleet status**');
324
+ expect(out).not.toContain('| State | Account | 5h | 5h resets | 7d | 7d resets |');
325
+ // ...but the actionable recommendation footer is RESTORED (#2959 review).
326
+ expect(out).toContain('Recommendation: stay on ken@example.com.');
327
+ // Freshness marker present (no stamp supplied → bare "Live").
328
+ expect(lines[lines.length - 1]).toBe('_Live_');
329
+ // 3 bar lines + recommendation + freshness = 5.
330
+ expect(lines).toHaveLength(5);
331
+ });
332
+
333
+ it('restores the "switch now" recommendation footer for a BLOCKED active account', () => {
334
+ // Matches the old renderAuthSnapshotFormat2 / recommendation() logic: an
335
+ // active account maxed on a window, with a healthy alternative, yields the
336
+ // most actionable line — "switch to Y now".
337
+ const blocked: AccountSnapshot[] = [
338
+ snap({
339
+ label: 'active@example.com',
340
+ isActive: true,
341
+ quota: quota({ fiveHourUtilizationPct: 100, sevenDayUtilizationPct: 100 }),
342
+ }),
343
+ snap({
344
+ label: 'healthy@example.com',
345
+ isActive: false,
346
+ quota: quota({ fiveHourUtilizationPct: 5, sevenDayUtilizationPct: 10 }),
347
+ }),
348
+ ];
349
+ const ex = new Map<string, boolean>([
350
+ ['active@example.com', true],
351
+ ['healthy@example.com', false],
352
+ ]);
353
+ const out = renderUsageCard(blocked, ex, { now: NOW });
354
+ expect(out).toContain(
355
+ '_Recommendation: active active@example.com is BLOCKED — switch to healthy@example.com now._',
356
+ );
357
+ });
358
+
359
+ it('shows a "⚠ cached Nm ago" marker for stale-cache-served data instead of a live stamp', () => {
360
+ const out = renderUsageCard(snapshots, exhausted, {
361
+ now: NOW,
362
+ staleCachedAtMs: NOW.getTime() - 3 * 60_000,
363
+ });
364
+ const lines = out.split('\n');
365
+ expect(lines[lines.length - 1]).toBe('_⚠ cached 3m ago_');
366
+ expect(out).not.toContain('_Live');
367
+ });
368
+
369
+ it('shows a "Live · refreshed" stamp when the probe was live', () => {
370
+ const out = renderUsageCard(snapshots, exhausted, {
371
+ now: NOW,
372
+ liveProbedAtMs: NOW.getTime() - 5_000,
373
+ });
374
+ const lines = out.split('\n');
375
+ expect(lines[lines.length - 1]).toBe('_Live · refreshed 5s ago_');
376
+ expect(out).not.toContain('cached');
377
+ });
378
+
379
+ it('stale-cache marker takes precedence over a live stamp when both are supplied', () => {
380
+ const out = renderUsageCard(snapshots, exhausted, {
381
+ now: NOW,
382
+ staleCachedAtMs: NOW.getTime() - 60_000,
383
+ liveProbedAtMs: NOW.getTime(),
384
+ });
385
+ expect(out.split('\n').pop()).toBe('_⚠ cached 1m ago_');
386
+ });
387
+
388
+ it('probeFailed renders an explicit "probe failed" marker instead of a false "Live" footer (#2959 honesty fix)', () => {
389
+ // A total probe failure: zero result rows, nothing served from cache.
390
+ // Every account row renders "⚠️ no data — probe failed"; the footer
391
+ // must NOT claim "Live". Pre-fix this rendered a bare `_Live_`.
392
+ const out = renderUsageCard(snapshots, exhausted, { now: NOW, probeFailed: true });
393
+ expect(out.split('\n').pop()).toBe('_⚠ probe failed — no live data_');
394
+ expect(out).not.toContain('_Live');
395
+ expect(out).not.toContain('cached');
396
+ });
397
+
398
+ it('probeFailed does not override real cached data (cache takes precedence)', () => {
399
+ // If some rows WERE served from cache, there IS real (stale) data —
400
+ // the cache marker must win over the probe-failed marker.
401
+ const out = renderUsageCard(snapshots, exhausted, {
402
+ now: NOW,
403
+ staleCachedAtMs: NOW.getTime() - 2 * 60_000,
404
+ probeFailed: true,
405
+ });
406
+ expect(out.split('\n').pop()).toBe('_⚠ cached 2m ago_');
407
+ expect(out).not.toContain('probe failed');
408
+ });
409
+
410
+ it('handles an invalid/NaN reset date without emitting "NaN" or corrupting the bar', () => {
411
+ const bad: AccountSnapshot[] = [
412
+ snap({
413
+ label: 'ken@example.com',
414
+ isActive: true,
415
+ quota: quota({
416
+ fiveHourUtilizationPct: 10,
417
+ sevenDayUtilizationPct: 20,
418
+ fiveHourResetAt: new Date('not-a-date'),
419
+ sevenDayResetAt: new Date(NaN),
420
+ }),
421
+ }),
422
+ ];
423
+ const out = renderUsageCard(bad, exhausted, { now: NOW });
424
+ expect(out).not.toContain('NaN');
425
+ // Malformed reset → treated as "resets now", tick at index 0.
426
+ expect(out).toContain('/ resets now`');
427
+ for (const line of out.split('\n')) {
428
+ const m = line.match(/\[(.+?)\]/);
429
+ if (m) expect(m[1].length).toBe(10); // bar stays exactly 10 cells
430
+ }
431
+ });
432
+
433
+ it('masks the account-email label in demo mode', () => {
434
+ const out = renderUsageCard(snapshots, exhausted, { now: NOW, demo: true });
435
+ expect(out).not.toContain('ken@example.com');
436
+ // Bar rows are unaffected; only the title-line label is masked.
437
+ expect(out).toContain('5h `[');
438
+ });
439
+
440
+ it('does not mask the label when demo is omitted', () => {
441
+ const out = renderUsageCard(snapshots, exhausted, { now: NOW });
442
+ expect(out).toContain('- **ken@example.com** (active)');
443
+ });
444
+ });