forked from doctly/switchboard
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrigger-watcher.js
More file actions
1384 lines (1255 loc) · 58.1 KB
/
Copy pathtrigger-watcher.js
File metadata and controls
1384 lines (1255 loc) · 58.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// trigger-watcher.js — File-based input injection for harness scripts.
//
// Drop a JSON trigger file into SWITCHBOARD_TRIGGERS_DIR (default
// ~/.switchboard/triggers/<uuid>.json) and this module writes the command into
// the matching PTY session's stdin. The result is written to
// SWITCHBOARD_TRIGGERS_DIR/processed/<uuid>.result.json; the trigger file is
// then deleted.
//
// Exports: start(ctx) where ctx = { getPtyForSession, isSessionBusy,
// getComposerState, log }
//
// Security limits (defense-in-depth):
// - Max trigger file size: 64 KB (C1)
// - Symlinks rejected via lstat (C2)
// - Max command length: 4 KB (W2)
// - Forbidden control chars in command: \r \n \0 \x1b (W3)
// - Max concurrent in-flight triggers: 8 (W4)
// - Per-trigger timeout_ms capped at 600 000 ms (W6)
// - Child-process liveness check before write (W7)
// - Max chain length: 20 steps (W8)
// - No write into a composer holding unsubmitted input (see docs/automation.md)
// - Optional expectedCwd target guard, fails closed (see .ai/contexts/trigger-watcher.md)
//
// Startup scan: start() also enumerates whatever *.json already sits at the
// root of the triggers dir (never processed/) and feeds it through the same
// path a live fs.watch 'rename' event would — a trigger written while the app
// was closed is otherwise never seen (fs.watch only reports changes after it
// is installed). A trigger older than SWITCHBOARD_TRIGGER_MAX_AGE_MS is
// refused, not run — see .ai/contexts/trigger-watcher.md, "Startup scan".
//
// Platform note: fs.watch is Linux-only reliable (I2). On macOS, inotify
// events may be coalesced or delayed; not blocked but not tested.
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const { createLocalSessionHandle } = require('./trigger-context');
const DEFAULT_TRIGGERS_DIR = path.join(os.homedir(), '.switchboard', 'triggers');
// Default idle-wait timeout: 5 minutes.
// Rationale: agentic Claude CLI turns can run 10-20 min between idle states.
// 30 s (the original default) was too short and would time-out healthy long
// turns. 300 000 ms (5 min) is the practical upper bound for a genuine wait;
// anything longer than that without going idle is considered stuck and the
// harness should escalate instead. The env var and per-trigger timeout_ms
// field both override this default (precedence: timeout_ms > env var > default).
const DEFAULT_IDLE_TIMEOUT = 300_000; // ms — 5 minutes
const MAX_TRIGGER_TIMEOUT = 600_000; // ms — hard cap for per-trigger timeout_ms (W6)
const IDLE_POLL_INTERVAL = 100; // ms
const MAX_TRIGGER_SIZE = 64 * 1024; // 64 KB (C1)
const MAX_COMMAND_LEN = 4 * 1024; // 4 KB (W2)
const MAX_INFLIGHT = 8; // concurrency cap (W4)
const MAX_CHAIN_LENGTH = 20; // max steps per chain (W8)
// Max time to spend looking for busy=true after injecting a command.
// see .ai/contexts/trigger-watcher.md ("submitted") for what this does and does
// not prove.
const BUSY_OBSERVE_TIMEOUT_MS = 2000; // ms
// Delay (ms) between writing a command's text and the Enter keypress that
// submits it. The Enter MUST arrive as a discrete PTY read — concatenated onto
// the text in a single write, Claude Code (kitty keyboard protocol) absorbs it
// as a literal newline in the composer and the command never submits. xterm.js
// sends every keypress as its own write, which is why the web terminal submits
// correctly; we mirror that. Reproduced 2026-06-02: free-text trigger commands
// landed in the composer but did not submit; only the short menu-driven
// /compact path submitted. Override via SWITCHBOARD_SUBMIT_ENTER_DELAY_MS.
const DEFAULT_SUBMIT_ENTER_DELAY_MS = 50; // ms
// Control chars forbidden in command: CR, LF, NUL, ESC (W3)
const FORBIDDEN_COMMAND_RE = /[\r\n\0\x1b]/;
// Politeness guard — see docs/automation.md and .ai/contexts/trigger-watcher.md.
const DEFAULT_QUIET_MS = 3000;
// see .ai/contexts/trigger-watcher.md ("submitted") for what each value
// promises and what it does not.
const SUBMITTED_NO = 'no';
const SUBMITTED_ASSUMED = 'assumed';
const SUBMITTED_ACTIVITY = 'activity';
const SUBMITTED_CONFIRMED = 'confirmed';
const SUBMITTED_RANK = {
[SUBMITTED_NO]: 0,
[SUBMITTED_ASSUMED]: 1,
[SUBMITTED_ACTIVITY]: 2,
[SUBMITTED_CONFIRMED]: 3,
};
const ERROR_NOT_SENT = 'not sent';
const ERROR_CHAIN_TIMEOUT = 'chain timeout';
const ACCEPTED_WAITS = ['idle', 'none'];
function weakestSubmitted(a, b) {
return SUBMITTED_RANK[a] <= SUBMITTED_RANK[b] ? a : b;
}
// Single source of truth for turning a submitWithVerify() result into one of
// the four `submitted` values -- used for the single-command result, the
// per-chain-step field, and the chain's own fold (weakestSubmitted over this).
// Kept as one function so all three read the same classification instead of
// three copies of the same ternary drifting apart.
function classifySubmitted(composerConfirmed, sawBusy) {
return composerConfirmed ? SUBMITTED_CONFIRMED : (sawBusy ? SUBMITTED_ACTIVITY : SUBMITTED_ASSUMED);
}
// W7 liveness probe — see .ai/contexts/trigger-watcher.md, "Session handle".
function resolveHandle(entry) {
return entry.handle || createLocalSessionHandle(entry.ptyProcess);
}
function isEntryAlive(ctx, entry, handle) {
return ctx.isPtyAlive ? ctx.isPtyAlive(entry.ptyProcess) : handle.isAlive();
}
// Poll interval (ms) for `delayWithBusyPoll` below. Deliberately finer than
// IDLE_POLL_INTERVAL (100ms): the text->Enter delay this polls across is only
// DEFAULT_SUBMIT_ENTER_DELAY_MS (50ms) long in production, so a 100ms poll
// interval would sample it once at best -- no better than the single sample
// this replaces.
const MID_BUSY_POLL_INTERVAL_MS = 5; // ms
// Poll `ctx.isSessionBusy` at MID_BUSY_POLL_INTERVAL_MS granularity for the
// full `ms` duration, returning true if busy was observed at ANY point in the
// window -- not just at the start or the end of it. Total elapsed time is
// bounded to exactly `ms`, same as a plain `delay(ms)`; this only changes
// what happens during the wait, not how long it lasts.
function delayWithBusyPoll(ms, sessionId, ctx) {
const deadline = Date.now() + ms;
return new Promise((resolve) => {
let sawBusy = false;
function tick() {
if (ctx.isSessionBusy(sessionId)) sawBusy = true;
const remaining = deadline - Date.now();
if (remaining <= 0) return resolve(sawBusy);
setTimeout(tick, Math.min(MID_BUSY_POLL_INTERVAL_MS, remaining)).unref();
}
tick();
});
}
// Submit a command to a PTY the way a human terminal does: write the text,
// then send Enter as a SEPARATE write so it is read as a discrete "submit"
// keypress rather than a trailing newline. See DEFAULT_SUBMIT_ENTER_DELAY_MS.
//
// Returns `midBusy`: busy polled continuously between the text write and the
// Enter write, true if observed at any point in that window. A single sample
// -- at the start, or at the end -- can miss a busy that rises and falls
// entirely inside the window, which is a real, reproduced case (see
// .ai/contexts/trigger-watcher.md, "submitted"), not a hypothetical one:
// busy observed anywhere here cannot be attributed to an Enter that had not
// been sent yet. See the "composerConfirmed" gate in submitWithVerify, which
// this narrows.
async function submitToPty(handle, command, sessionId, ctx) {
handle.write(command);
const envMs = envNumber('SWITCHBOARD_SUBMIT_ENTER_DELAY_MS');
const delayMs = envMs !== undefined ? envMs : DEFAULT_SUBMIT_ENTER_DELAY_MS;
const midBusy = await delayWithBusyPoll(delayMs, sessionId, ctx);
handle.write('\r');
return midBusy;
}
// A variable set to the empty string is a launcher artefact, not a value:
// Number('') is 0, which would silently drop the window to nothing.
function envNumber(name) {
const raw = process.env[name];
if (raw === undefined || raw.trim() === '') return undefined;
const v = Number(raw);
return Number.isFinite(v) && v >= 0 ? v : undefined;
}
// Normalizes a cwd for the target guard's comparison — see
// .ai/contexts/trigger-watcher.md, "Target guard" for what this does and does not paper over.
function normalizeCwd(p) {
if (typeof p !== 'string' || !p) return null;
let n;
try {
n = path.normalize(p);
} catch (_) {
return null;
}
if (process.platform === 'win32') {
// \\?\UNC\server\share\... -> \\server\share\...
if (n.toLowerCase().startsWith('\\\\?\\unc\\')) {
n = '\\\\' + n.slice(8);
} else if (n.toLowerCase().startsWith('\\\\?\\')) {
// \\?\C:\foo -> C:\foo
n = n.slice(4);
}
// NTFS/ReFS are case-insensitive; fold so "C:\Foo" === "c:\foo".
n = n.toLowerCase();
}
// Strip one trailing separator, but never collapse a bare root ("C:\", "/").
// see .ai/contexts/trigger-watcher.md, "Target guard" for why this is an
// explicit n !== path.sep check rather than a length threshold.
if (n.endsWith(path.sep) && n !== path.sep &&
!(process.platform === 'win32' && /^[a-z]:\\$/.test(n))) {
n = n.slice(0, -1);
}
return n;
}
function getQuietMs() {
const v = envNumber('SWITCHBOARD_TRIGGER_QUIET_MS');
return v !== undefined ? v : DEFAULT_QUIET_MS;
}
function describeBusyComposer(state, quietMs, now) {
if (!state) {
return 'the composer of this session cannot be observed, and doubt resolves to busy';
}
if (state.pending > 0) {
return `${state.pending} byte(s) of input are sitting unsubmitted in the composer`;
}
return `the last keystroke landed ${now - (state.lastInputAt || 0)} ms ago, ` +
`inside the ${quietMs} ms quiet window`;
}
/**
* Runs `check(resolve, scheduleNext)`, re-invoking it on every recursive
* `scheduleNext()`; any throw from `check`, on any tick, becomes this
* promise's rejection. See .ai/contexts/trigger-watcher.md, "Poll loops must
* reject, not throw".
*/
function pollLoop(check) {
return new Promise((resolve, reject) => {
function tick() {
try {
// .unref(): an in-flight poll must not keep the process alive alone.
check(resolve, () => setTimeout(tick, IDLE_POLL_INTERVAL).unref());
} catch (err) {
reject(err);
}
}
tick();
});
}
/**
* Poll until the target composer is free — empty AND quiet — bounded by the
* absolute `deadlineMs`.
*
* A ctx that cannot answer, or a session it does not know, is NOT free: an
* unobservable composer is treated exactly like a full one.
*
* Returns { free, waited_ms, reason }.
*/
function waitForComposerFree(sessionId, ctx, deadlineMs) {
const start = Date.now();
const quietMs = getQuietMs();
const limit = (deadlineMs !== undefined) ? deadlineMs : Infinity;
return pollLoop((resolve, scheduleNext) => {
const now = Date.now();
const state = (typeof ctx.getComposerState === 'function')
? ctx.getComposerState(sessionId)
: null;
if (state && state.pending === 0 && (now - (state.lastInputAt || 0)) >= quietMs) {
return resolve({ free: true, waited_ms: now - start, reason: null });
}
if (now >= limit) {
return resolve({
free: false,
waited_ms: now - start,
reason: describeBusyComposer(state, quietMs, now),
});
}
scheduleNext();
});
}
// Window (ms) to look for busy=true when verifying a submission.
// Defaults to BUSY_OBSERVE_TIMEOUT_MS; override via SWITCHBOARD_SUBMIT_VERIFY_MS.
function getSubmitVerifyMs() {
const v = envNumber('SWITCHBOARD_SUBMIT_VERIFY_MS');
return v !== undefined ? v : BUSY_OBSERVE_TIMEOUT_MS;
}
// How long busy must read false, CONTINUOUSLY, before waitForBusyFall treats a
// turn as finished. A single false sample is not enough: a command's own tail
// activity (e.g. /compact still writing its summary after the visible turn
// ends) can produce a brief busy->false->true flicker. Exiting on the first
// false sample lets the very next chain step get written while that flicker
// is still running, and the re-assertion of busy then lands inside THAT
// step's submit-verify window — read as proof the next step's own Enter
// started a turn, when it did not (root cause of the 2026-09-04 "Enter
// inserted a newline instead of submitting" incident: submitted:"confirmed",
// waited_ms:0, on the step written right after a /compact busy-fall).
// Override via SWITCHBOARD_BUSY_FALL_SETTLE_MS.
const DEFAULT_BUSY_FALL_SETTLE_MS = 300; // ms
function getBusyFallSettleMs() {
const v = envNumber('SWITCHBOARD_BUSY_FALL_SETTLE_MS');
return v !== undefined ? v : DEFAULT_BUSY_FALL_SETTLE_MS;
}
// see .ai/contexts/trigger-watcher.md, "waitForBusyFall waits for the rise too"
function getBusyRiseWaitMs() {
const v = envNumber('SWITCHBOARD_BUSY_RISE_WAIT_MS');
return v !== undefined ? v : getSubmitVerifyMs();
}
/**
* Poll ctx.isSessionBusy(sessionId) for busy=true up to `windowMs`, bounded
* by the absolute `deadlineMs`. Stops early if the PTY disappears.
*
* This is a LEVEL probe, not an edge detector: a session already busy on
* entry answers on the first tick, and nothing here ties the busy state to
* our own write. See .ai/contexts/trigger-watcher.md ("submitted").
*
* Returns { sawBusy, timedOut, sessionExited, waited_ms }.
* - sawBusy: busy=true was observed at some point in the window
* - timedOut: global deadline fired before any observation
* - sessionExited: PTY vanished during the poll
*/
function pollForBusyObserved(sessionId, ctx, windowMs, deadlineMs) {
const start = Date.now();
const windowEnd = start + windowMs;
return pollLoop((resolve, scheduleNext) => {
const now = Date.now();
if (now >= deadlineMs) {
return resolve({ sawBusy: false, timedOut: true, sessionExited: false, waited_ms: now - start });
}
if (!ctx.getPtyForSession(sessionId)) {
return resolve({ sawBusy: false, timedOut: false, sessionExited: true, waited_ms: now - start });
}
if (ctx.isSessionBusy(sessionId)) {
return resolve({ sawBusy: true, timedOut: false, sessionExited: false, waited_ms: now - start });
}
if (now >= windowEnd) {
// Verify window elapsed without seeing busy — caller decides.
return resolve({ sawBusy: false, timedOut: false, sessionExited: false, waited_ms: now - start });
}
scheduleNext();
});
}
/**
* Submit a command, then verify it.
*
* 1. submitToPty(text + discrete Enter)
* 2. Read the composer back — unconditionally, never skipped by an activity
* sample. See .ai/contexts/trigger-watcher.md ("submitted") for why this
* reading alone still cannot be trusted on a session that was already
* mid-turn when we wrote.
* 3. Poll for busy=true within SWITCHBOARD_SUBMIT_VERIFY_MS.
* 4. Busy observed → done (submit_retries: 0). `composerConfirmed` is set only
* when the composer read back empty AND the session was not already busy
* the instant we wrote, AND not busy in the gap between the text write and
* the Enter write (`midBusy`) — busy observed there cannot be attributed to
* an Enter that had not been sent yet. Neither gate proves our Enter caused
* the busy the poll later sees; both only rule out cases where it provably
* could not have. See .ai/contexts/trigger-watcher.md ("submitted"),
* "What confirmed still does not claim" for the causality gap that remains.
* 5. Nothing observed → write a SINGLE bare '\r' (a no-op on an empty composer, so it is
* harmless if the first submit actually worked; if the text is still sitting
* in the composer because the first Enter was absorbed, this submits it) and
* poll the same window again (submit_retries: 1). A retry never yields
* `composerConfirmed` — the first attempt already needed rescuing.
*
* The observation IS the equivalent of waitForTurnComplete's Phase 1; callers
* MUST NOT then wait for busy again — they proceed straight to busy-fall.
*
* Returns { submit_retries, sawBusy, composerConfirmed, sessionExited, timedOut, waited_ms }.
* - waited_ms is the total time spent polling (both windows + retry).
*
* If sessionExited/timedOut fire, the caller short-circuits with the usual
* error result. If neither poll observes busy (and no deadline),
* the caller keeps the legacy instant-reply semantics — submit_retries traces
* that the verification could not confirm a turn started.
*/
async function submitWithVerify(handle, sessionId, command, ctx, deadlineMs) {
// Sampled before the write — see .ai/contexts/trigger-watcher.md ("submitted").
const preBusy = ctx.isSessionBusy(sessionId);
const midBusy = await submitToPty(handle, command, sessionId, ctx);
// Composer read-back: unconditional, immediate, never gated on activity.
const postWriteState = (typeof ctx.getComposerState === 'function')
? ctx.getComposerState(sessionId)
: null;
const composerEmptyAfterWrite = !!postWriteState && postWriteState.pending === 0;
const windowMs = getSubmitVerifyMs();
// No explicit (global) deadline → the verify window alone governs; the retry
// must fire on window expiry, so the deadline must NOT coincide with it.
const effectiveDeadline = (deadlineMs !== undefined) ? deadlineMs : Infinity;
const first = await pollForBusyObserved(sessionId, ctx, windowMs, effectiveDeadline);
if (first.sawBusy || first.sessionExited || first.timedOut) {
return {
submit_retries: 0,
sawBusy: first.sawBusy,
composerConfirmed: first.sawBusy && preBusy === false && midBusy === false && composerEmptyAfterWrite,
sessionExited: first.sessionExited,
timedOut: first.timedOut,
waited_ms: first.waited_ms,
};
}
// Nothing observed in the window — retry the Enter ONCE (bare '\r', never the text),
// and only into a free composer. See docs/automation.md.
const recoveryDeadline = Math.min(effectiveDeadline, Date.now() + windowMs);
const polite = await waitForComposerFree(sessionId, ctx, recoveryDeadline);
if (!polite.free) {
return {
submit_retries: 0,
sawBusy: false,
composerConfirmed: false,
sessionExited: false,
timedOut: false,
recoverySkipped: true,
recoveryReason: polite.reason,
waited_ms: first.waited_ms + polite.waited_ms,
};
}
try {
handle.write('\r');
} catch (err) {
// Surface as a sessionExited-like failure; caller maps to an error result.
return {
submit_retries: 1,
sawBusy: false,
composerConfirmed: false,
sessionExited: false,
timedOut: false,
writeError: err,
waited_ms: first.waited_ms,
};
}
const second = await pollForBusyObserved(sessionId, ctx, windowMs, effectiveDeadline);
return {
submit_retries: 1,
sawBusy: second.sawBusy,
composerConfirmed: false,
sessionExited: second.sessionExited,
timedOut: second.timedOut,
waited_ms: first.waited_ms + second.waited_ms,
};
}
/**
* Wait for the busy RISING edge, then the FALLING edge (busy → true → false):
* the turn actually starting, then finishing. Used after submitWithVerify has
* already tried (and possibly failed) to observe the turn starting.
*
* Checks run in priority order — deadline, rise-wait bound, settle window — so
* a wait in progress when `deadlineMs` arrives resolves as `timedOut`, never
* as success. A rise that never arrives within `getBusyRiseWaitMs()` is not an
* error but a turn never observed; a session already busy on the first tick
* satisfies the rise immediately.
*
* see .ai/contexts/trigger-watcher.md, "waitForBusyFall waits for the rise too"
*
* Returns { timedOut, sessionExited, waited_ms }.
*/
function waitForBusyFall(sessionId, ctx, deadlineMs) {
const start = Date.now();
const settleMs = getBusyFallSettleMs();
const riseDeadline = start + getBusyRiseWaitMs();
// Until busy has read true once, a run of `false` proves nothing.
let hasRisen = false;
// Set the instant busy first reads false (after having risen); reset to
// null on every re-assertion.
let idleSince = null;
return pollLoop((resolve, scheduleNext) => {
const now = Date.now();
if (now >= deadlineMs) {
return resolve({ timedOut: true, sessionExited: false, waited_ms: now - start });
}
if (!ctx.getPtyForSession(sessionId)) {
return resolve({ timedOut: false, sessionExited: true, waited_ms: now - start });
}
if (ctx.isSessionBusy(sessionId)) {
hasRisen = true;
idleSince = null;
} else if (hasRisen) {
if (idleSince === null) idleSince = now;
if (now - idleSince >= settleMs) {
return resolve({ timedOut: false, sessionExited: false, waited_ms: now - start });
}
} else if (now >= riseDeadline) {
// Never rose within the bound: turn never observed, not an error.
return resolve({ timedOut: false, sessionExited: false, waited_ms: now - start });
}
scheduleNext();
});
}
function getTriggersDir() {
return process.env.SWITCHBOARD_TRIGGERS_DIR || DEFAULT_TRIGGERS_DIR;
}
function getIdleTimeout() {
const v = process.env.SWITCHBOARD_TRIGGER_IDLE_TIMEOUT_MS;
if (v !== undefined) {
const parsed = parseInt(v, 10);
return Number.isFinite(parsed) ? parsed : DEFAULT_IDLE_TIMEOUT; // I4: NaN guard
}
return DEFAULT_IDLE_TIMEOUT;
}
// Default staleness threshold for the startup scan (see .ai/contexts/
// trigger-watcher.md, "Startup scan"). A trigger written more than this long
// ago is refused rather than run: it was written for a session state that may
// no longer exist by the time the app comes back up.
//
// Rationale for 5 minutes: the only two "legitimate wait" durations ever
// measured on this transport are 66s and 102s (chain step 0, five real
// /compact incidents, see .ai/contexts/trigger-watcher.md). 300_000 ms clears
// the larger of those by ~3x, so a trigger that is merely slow to be picked up
// is never mistaken for a stale one — while staying far short of "hours",
// which is the case JB's own example (a /compact issued 6h earlier no longer
// targets the same session) treats as unambiguously stale. It also reuses
// DEFAULT_IDLE_TIMEOUT's already-established number and rationale above
// ("the practical upper bound for a genuine wait") rather than inventing a
// second one. Configurable via SWITCHBOARD_TRIGGER_MAX_AGE_MS.
const DEFAULT_TRIGGER_MAX_AGE_MS = 300_000; // ms — 5 minutes
// Read live, like getIdleTimeout() above — NOT cached at module load. A
// module-load-time constant would miss every env var set after require()
// (the exact trap documented at the top of this file's test suite).
function getTriggerMaxAgeMs() {
const v = process.env.SWITCHBOARD_TRIGGER_MAX_AGE_MS;
if (v !== undefined) {
const parsed = parseInt(v, 10);
return Number.isFinite(parsed) ? parsed : DEFAULT_TRIGGER_MAX_AGE_MS; // NaN guard
}
return DEFAULT_TRIGGER_MAX_AGE_MS;
}
/**
* Poll until isSessionBusy(sessionId) returns false, or the timeout expires,
* or the session exits (PTY no longer available).
*
* @param {string} sessionId
* @param {object} ctx
* @param {number} [timeoutMs] explicit timeout in ms; falls back to
* getIdleTimeout() (env var → default) when absent.
* Returns { timedOut: boolean, sessionExited: boolean, waited_ms: number }.
*/
function waitForIdle(sessionId, ctx, timeoutMs) {
const timeout = (timeoutMs !== undefined) ? timeoutMs : getIdleTimeout();
const start = Date.now();
return pollLoop((resolve, scheduleNext) => {
const waited_ms = Date.now() - start;
// W5: detect PTY closure during wait
if (!ctx.getPtyForSession(sessionId)) {
return resolve({ timedOut: false, sessionExited: true, waited_ms });
}
if (!ctx.isSessionBusy(sessionId)) {
return resolve({ timedOut: false, sessionExited: false, waited_ms });
}
if (waited_ms >= timeout) {
return resolve({ timedOut: true, sessionExited: false, waited_ms });
}
scheduleNext();
});
}
// NOTE: the previous combined-phase waiter (waitForTurnComplete: busy-rise then
// busy-fall) was split into submitWithVerify (Phase 1, busy-rise + Enter retry)
// and waitForBusyFall (Phase 2, busy-fall) so the chain path can verify each
// submission and retry the Enter once if the turn never starts (2026-06-04
// "text stuck in composer" incident). The instant-reply semantics are preserved:
// when no rise is confirmed, submitWithVerify still returns and waitForBusyFall
// returns immediately on an already-idle session.
/**
* Validate a single timeout_ms value (for top-level or per-step).
* Returns null if valid, or an error string if invalid.
*/
function validateTimeoutMs(value) {
if (
typeof value !== 'number' ||
!Number.isInteger(value) ||
value <= 0 ||
value > MAX_TRIGGER_TIMEOUT
) {
return 'invalid timeout_ms';
}
return null;
}
// A logger call that cannot throw. See .ai/contexts/trigger-watcher.md,
// "Removing the entry".
function safeLogError(ctx, ...args) {
try {
ctx.log.error(...args);
} catch (_) { /* swallow */ }
}
/**
* Process a single trigger file (by basename, e.g. "abc-123.json").
* Never throws — all errors land in the result file.
*
* @param {function} [acquireSessionLock] (sessionId) => Promise<releaseFn>;
* awaited right after sessionId is known valid, released in the
* `finally` below so a second trigger for the same session cannot
* touch its PTY/composer until this one is fully done.
*/
async function processTriggerFile(name, ctx, triggersDir, processedDir, onEntryRetained, acquireSessionLock) {
// Only handle *.json files, ignore the processed/ subdir itself and
// any stray files.
if (!name.endsWith('.json')) return;
const triggerPath = path.join(triggersDir, name);
const uuid = name.slice(0, -5); // strip ".json"
const resultPath = path.join(processedDir, uuid + '.result.json');
const resultTmp = resultPath + '.tmp'; // I1: atomic write temp path
// I1: atomic result write — write to .tmp then rename so pollers never
// observe a partial JSON file.
async function writeResult(result) {
// Every result carries `submitted`; an unstated one is the safe value.
if (result.submitted === undefined) result.submitted = SUBMITTED_NO;
// see .ai/contexts/trigger-watcher.md, "steps_total"
if (result.steps_total === undefined) result.steps_total = stepsTotal;
try {
fs.writeFileSync(resultTmp, JSON.stringify(result) + '\n', 'utf8');
fs.renameSync(resultTmp, resultPath);
} catch (err) {
safeLogError(ctx, '[trigger-watcher] Failed to write result file:', err.message);
}
try {
fs.unlinkSync(triggerPath);
} catch (err) {
if (err.code !== 'ENOENT') {
// onEntryRetained() first: the guarantee it records must not depend
// on whether the log call after it succeeds.
if (onEntryRetained) onEntryRetained();
safeLogError(ctx, '[trigger-watcher] Trigger file survived processing, will not be run again:',
name, err.message);
}
}
}
let releaseSessionLock;
let stepsTotal = 0;
try {
// ── 1. lstat + size guard (C1 + C2) ──────────────────────────────────────
let stat;
try {
stat = fs.lstatSync(triggerPath); // C2: lstat does NOT follow symlinks
} catch (err) {
if (err.code === 'ENOENT') return; // gone before we could inspect it
ctx.log.error('[trigger-watcher] Trigger file could not be inspected:', name, err.message);
await writeResult({ ok: false, error: 'trigger could not be inspected: ' + err.message });
return;
}
if (!stat.isFile()) {
// C2: reject symlinks, directories, device nodes, etc.
ctx.log.warn('[trigger-watcher] Non-regular-file trigger rejected:', name);
await writeResult({ ok: false, error: 'trigger must be a regular file' });
return;
}
if (stat.size > MAX_TRIGGER_SIZE) {
// C1: reject oversized files before reading
ctx.log.warn('[trigger-watcher] Oversized trigger rejected:', name, stat.size);
await writeResult({ ok: false, error: 'trigger too large (max 64 KB)' });
return;
}
// ── 1b. Staleness guard — see .ai/contexts/trigger-watcher.md, "Startup scan" ──
// Applies uniformly to every trigger, scan-found or live-arriving: mtime is
// the file's write time regardless of how this run came to look at it.
const ageMs = Date.now() - stat.mtimeMs;
const maxAgeMs = getTriggerMaxAgeMs();
if (ageMs > maxAgeMs) {
ctx.log.warn('[trigger-watcher] Stale trigger refused:', name,
Math.round(ageMs) + 'ms old, max ' + maxAgeMs + 'ms');
await writeResult({
ok: false,
submitted: SUBMITTED_NO,
error: ERROR_NOT_SENT,
reason: `trigger is ${Math.round(ageMs)}ms old, older than the ${maxAgeMs}ms ` +
'staleness limit; refused rather than act on a request that may no ' +
'longer target the intended session state',
});
return;
}
// ── 2. Read + parse (with SyntaxError retry for W1 partial-write race) ───
let trigger;
let lastParseErr;
for (let attempt = 0; attempt < 2; attempt++) {
// W1: on second attempt, wait 50 ms (partial-write window) then retry
if (attempt === 1) {
await new Promise(r => setTimeout(r, 50));
}
try {
const raw = fs.readFileSync(triggerPath, 'utf8');
trigger = JSON.parse(raw);
lastParseErr = null;
break; // success
} catch (err) {
lastParseErr = err;
if (!(err instanceof SyntaxError)) {
// ENOENT or other I/O error — no retry useful
break;
}
// SyntaxError on attempt 0: retry once after 50 ms (W1)
}
}
if (lastParseErr) {
ctx.log.warn('[trigger-watcher] Unreadable/unparseable trigger:', name, lastParseErr.message);
await writeResult({ ok: false, error: 'invalid JSON: ' + lastParseErr.message });
return;
}
// ── 3. Validate shape ─────────────────────────────────────────────────────
const { sessionId, command, chain, wait = 'none', timeout_ms, expectedCwd } = trigger;
if (typeof sessionId !== 'string' || !sessionId) {
await writeResult({ ok: false, error: 'missing required field: sessionId', sessionId: sessionId || null });
return;
}
// see .ai/contexts/trigger-watcher.md, "Session serialization"
if (acquireSessionLock) {
releaseSessionLock = await acquireSessionLock(sessionId);
}
// An unknown `wait` is refused before anything is written: falling back to
// 'none' would submit immediately into a session that asked to be waited for.
if (trigger.wait !== undefined && !ACCEPTED_WAITS.includes(trigger.wait)) {
await writeResult({
ok: false,
submitted: SUBMITTED_NO,
error: ERROR_NOT_SENT,
reason: `wait ${JSON.stringify(String(trigger.wait))} is not a value this ` +
`transport knows: only "idle" and "none", and an absent field means "none"`,
sessionId,
});
return;
}
// Mutual exclusion: command and chain cannot both be present
if (command !== undefined && chain !== undefined) {
await writeResult({ ok: false, error: 'command and chain are mutually exclusive', sessionId });
return;
}
// Must have either command or chain
if (command === undefined && chain === undefined) {
await writeResult({ ok: false, error: 'missing required field: command or chain', sessionId });
return;
}
// ── 3a. Validate single-command path ─────────────────────────────────────
if (command !== undefined) {
if (typeof command !== 'string' || !command) {
await writeResult({ ok: false, error: 'missing required field: command', sessionId });
return;
}
stepsTotal = 1;
// W2: command length cap
if (command.length > MAX_COMMAND_LEN) {
await writeResult({ ok: false, error: 'command too long (max 4 KB)', sessionId });
return;
}
// W3: reject forbidden control characters
if (FORBIDDEN_COMMAND_RE.test(command)) {
await writeResult({ ok: false, error: 'command contains forbidden control characters (\\r \\n \\0 \\x1b)', sessionId });
return;
}
}
// ── 3b. Validate chain path ───────────────────────────────────────────────
if (chain !== undefined) {
// W8: chain must be a non-empty array, length ≤ MAX_CHAIN_LENGTH
if (!Array.isArray(chain) || chain.length === 0) {
await writeResult({ ok: false, error: 'chain must be a non-empty array', sessionId });
return;
}
stepsTotal = chain.length;
if (chain.length > MAX_CHAIN_LENGTH) {
await writeResult({ ok: false, error: `chain too long (max ${MAX_CHAIN_LENGTH} steps)`, sessionId });
return;
}
// Validate each step
for (let i = 0; i < chain.length; i++) {
const step = chain[i];
if (typeof step.command !== 'string' || !step.command) {
await writeResult({ ok: false, error: `step[${i}]: missing required command string`, sessionId });
return;
}
// W2: step command length cap
if (step.command.length > MAX_COMMAND_LEN) {
await writeResult({ ok: false, error: `step[${i}]: command too long (max 4 KB)`, sessionId });
return;
}
// W3: reject forbidden control characters in step command
if (FORBIDDEN_COMMAND_RE.test(step.command)) {
await writeResult({ ok: false, error: `step[${i}]: command contains forbidden control characters (\\r \\n \\0 \\x1b)`, sessionId });
return;
}
// W6: validate optional per-step timeout_ms
if (step.timeout_ms !== undefined) {
const err = validateTimeoutMs(step.timeout_ms);
if (err) {
await writeResult({ ok: false, error: `step[${i}]: invalid step timeout_ms`, sessionId });
return;
}
}
}
}
// W6: validate optional top-level timeout_ms
let resolvedTimeoutMs;
if (timeout_ms !== undefined) {
const err = validateTimeoutMs(timeout_ms);
if (err) {
await writeResult({ ok: false, error: 'invalid timeout_ms', sessionId });
return;
}
resolvedTimeoutMs = timeout_ms;
}
// When timeout_ms is absent, resolvedTimeoutMs stays undefined and waitForIdle
// falls back to getIdleTimeout() (env var → compiled default).
// ── 3c. Validate optional expectedCwd — see .ai/contexts/trigger-watcher.md, "Target guard" ──
if (expectedCwd !== undefined && (typeof expectedCwd !== 'string' || !expectedCwd)) {
await writeResult({
ok: false,
submitted: SUBMITTED_NO,
error: ERROR_NOT_SENT,
reason: 'expectedCwd must be a non-empty string when present',
sessionId,
});
return;
}
// ── 4. Look up session ────────────────────────────────────────────────────
const sessionEntry = ctx.getPtyForSession(sessionId);
if (!sessionEntry) {
ctx.log.warn('[trigger-watcher] Session not found:', sessionId);
await writeResult({ ok: false, error: 'session not found', sessionId });
return;
}
const handle = resolveHandle(sessionEntry);
// W7 — pre-flight liveness check. main.js may keep a stale entry in its
// activeSessions map after a Claude process exited "cleanly" (Ctrl+D, /exit)
// without the Switchboard window closing. Without this guard we'd wait the
// full idle-timeout for a busy flag that will never flip, then write into a
// dead PTY and report ok:true.
if (!isEntryAlive(ctx, sessionEntry, handle)) {
ctx.log.warn('[trigger-watcher] Target process not running:', sessionId);
await writeResult({ ok: false, error: 'target process not running', sessionId });
return;
}
// ── Target guard (optional) — see .ai/contexts/trigger-watcher.md, "Target guard" ──
if (expectedCwd !== undefined) {
const observedCwd = normalizeCwd(sessionEntry.cwd);
if (observedCwd === null) {
ctx.log.warn('[trigger-watcher] Target guard: session cwd could not be determined, refusing:', sessionId);
await writeResult({
ok: false,
submitted: SUBMITTED_NO,
error: ERROR_NOT_SENT,
reason: "this session's cwd could not be determined; refusing rather than risk targeting the wrong session",
targetCwdUnknown: true,
expectedCwd,
observedCwd: null,
sessionId,
});
return;
}
if (normalizeCwd(expectedCwd) !== observedCwd) {
ctx.log.warn('[trigger-watcher] Target guard: expectedCwd does not match session cwd, refusing:', sessionId);
await writeResult({
ok: false,
submitted: SUBMITTED_NO,
error: ERROR_NOT_SENT,
reason: "expectedCwd does not match this session's actual cwd",
targetMismatch: true,
expectedCwd,
observedCwd: sessionEntry.cwd,
sessionId,
});
return;
}
}
// ── 5. Single-command path ────────────────────────────────────────────────
if (command !== undefined) {
let waited_ms = 0;
const commandDeadline = Date.now() +
((resolvedTimeoutMs !== undefined) ? resolvedTimeoutMs : getIdleTimeout());
if (wait === 'idle') {
const result = await waitForIdle(sessionId, ctx, resolvedTimeoutMs);
waited_ms = result.waited_ms;
// Nothing is written yet — see .ai/contexts/trigger-watcher.md.
// W5: session exited during wait
if (result.sessionExited) {
ctx.log.warn('[trigger-watcher] Session exited during wait:', sessionId);
await writeResult({
ok: false,
submitted: SUBMITTED_NO,
error: 'session exited during wait',
reason: 'the session exited while waiting for it to go idle; nothing was written',
sessionId, waited_ms,
});
return;
}
if (result.timedOut) {
ctx.log.warn('[trigger-watcher] Idle timeout for session, nothing sent:', sessionId);
await writeResult({
ok: false,
submitted: SUBMITTED_NO,
error: ERROR_NOT_SENT,
reason: 'timeout waiting for idle; nothing was written',
sessionId, waited_ms,
});
return;
}
}
// Politeness — never write over input the user typed and did not submit.
const polite = await waitForComposerFree(sessionId, ctx, commandDeadline);
waited_ms += polite.waited_ms;
if (!polite.free) {
ctx.log.warn('[trigger-watcher] Composer never free, nothing sent:', sessionId, polite.reason);
await writeResult({
ok: false,
submitted: SUBMITTED_NO,
error: ERROR_NOT_SENT,
reason: polite.reason,
sessionId,
waited_ms,
});
return;
}
// W7 — re-check liveness right before writing. The idle wait can be up to
// 10 min (MAX_TRIGGER_TIMEOUT) and the politeness wait runs to the same
// deadline; the child may have exited during either while busy-was-true
// never flipped. This probe belongs AFTER both waits: run before them it
// proves nothing about the moment of the write.
if (!isEntryAlive(ctx, sessionEntry, handle)) {
ctx.log.warn('[trigger-watcher] Target process exited during wait:', sessionId);
await writeResult({ ok: false, error: 'target process not running', sessionId, waited_ms });
return;
}
// Write to PTY: text, then Enter as a discrete keypress (see submitToPty),
// then verify the submission actually started a turn — retrying the Enter
// once if busy is never observed (the 2026-06-04 "text stuck in
// composer, Enter absorbed" incident).
let submitRetries = 0;
let sawBusy = false;
let composerConfirmed = false;
let recoverySkipped = false;
let recoveryReason = null;
try {
const v = await submitWithVerify(handle, sessionId, command, ctx);
submitRetries = v.submit_retries;
sawBusy = v.sawBusy;
composerConfirmed = !!v.composerConfirmed;
recoverySkipped = !!v.recoverySkipped;
recoveryReason = v.recoveryReason || null;
// The submit-verify poll (and its retry, if any) is time this trigger
// spent too -- see "waited_ms" in docs/automation.md.
waited_ms += v.waited_ms;
if (v.writeError) throw v.writeError;
} catch (err) {
ctx.log.error('[trigger-watcher] PTY write failed:', err.message);
await writeResult({ ok: false, error: 'pty write failed: ' + err.message, sessionId });
return;
}
if (recoverySkipped) {