forked from doctly/switchboard
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremote-watch.js
More file actions
222 lines (201 loc) · 7.32 KB
/
Copy pathremote-watch.js
File metadata and controls
222 lines (201 loc) · 7.32 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
// see .ai/contexts/session-cache.md ("Remote hosts — watch channel")
'use strict';
const { REMOTE_PROJECTS_REL, REMOTE_SESSIONS_REL } = require('./remote-transport');
const { isSafeRelPath } = require('./remote-hosts');
const { backoffDelayMs } = require('./remote-index');
const NOOP_LOG = { info() {}, warn() {}, error() {} };
const NO_INOTIFYWAIT_MARKER = 'SWITCHBOARD-NO-INOTIFYWAIT';
const NO_INOTIFYWAIT_EXIT_CODE = 44;
const SESSION_FILE_RE = /^[0-9]+\.json$/;
const PROJECTS_EVENTS = 'modify,close_write,create,moved_to';
const SESSIONS_EVENTS = 'close_write,create,delete,moved_to';
const COALESCE_MS = 15000;
const RESTART_BASE_MS = 5000;
const HEALTHY_MS = 30000;
function buildWatchCommand() {
return `if ! command -v inotifywait >/dev/null 2>&1; then echo ${NO_INOTIFYWAIT_MARKER}; exit ${NO_INOTIFYWAIT_EXIT_CODE}; fi; ` +
`mkdir -p '${REMOTE_PROJECTS_REL}' '${REMOTE_SESSIONS_REL}'; ` +
`inotifywait -m -r -e ${PROJECTS_EVENTS} --format 'P|%w%f' '${REMOTE_PROJECTS_REL}' & p=$!; ` +
`inotifywait -m -e ${SESSIONS_EVENTS} --format 'S|%w%f' '${REMOTE_SESSIONS_REL}' & s=$!; ` +
`wait $p $s`;
}
function buildSshArgs(alias) {
// see .ai/contexts/session-cache.md ("Remote hosts — watch channel")
return [
'-tt', '-o', 'BatchMode=yes',
'-o', 'ConnectTimeout=10', '-o', 'ServerAliveInterval=30', '-o', 'ServerAliveCountMax=3',
alias, buildWatchCommand(),
];
}
function parseWatchLine(line) {
if (typeof line !== 'string' || line.length < 3 || line[1] !== '|') return null;
const kind = line[0];
const raw = line.slice(2);
if (kind === 'P') {
const prefix = REMOTE_PROJECTS_REL + '/';
if (!raw.startsWith(prefix)) return null;
const rel = raw.slice(prefix.length);
return isSafeRelPath(rel) ? { kind: 'project', rel } : null;
}
if (kind === 'S') {
const prefix = REMOTE_SESSIONS_REL + '/';
if (!raw.startsWith(prefix)) return null;
const rel = raw.slice(prefix.length);
return SESSION_FILE_RE.test(rel) ? { kind: 'session', rel } : null;
}
return null;
}
function createRemoteWatcher(opts = {}) {
const spawnFn = opts.spawn || require('child_process').spawn;
const log = opts.log || NOOP_LOG;
const setT = (opts.timers && opts.timers.setTimeout) || setTimeout;
const clearT = (opts.timers && opts.timers.clearTimeout) || clearTimeout;
const states = new Map();
function killChild(s) {
if (s.restartTimer) { clearT(s.restartTimer); s.restartTimer = null; }
if (s.child) {
const child = s.child;
s.child = null;
try { child.kill(); } catch {}
}
}
function emitCoalesced(s, kind) {
if (s.cooldown[kind]) { s.pending[kind] = true; return; }
s.onEvent(s.alias, kind);
s.cooldown[kind] = true;
const t = setT(() => {
s.cooldownTimer[kind] = null;
s.cooldown[kind] = false;
if (s.pending[kind]) { s.pending[kind] = false; emitCoalesced(s, kind); }
}, COALESCE_MS);
if (t && t.unref) t.unref();
s.cooldownTimer[kind] = t;
}
function handleLine(s, rawLine) {
const line = rawLine.replace(/\r$/, '');
if (!line) return;
if (line.includes(NO_INOTIFYWAIT_MARKER)) {
s.unwatchable = true;
log.warn(`[remote-watch:${s.alias}] inotifywait is not installed on this host — ` +
'watch channel disabled, periodic refresh still covers it');
killChild(s);
return;
}
const parsed = parseWatchLine(line);
if (!parsed) return;
if (parsed.kind === 'project' && s.onActivity) s.onActivity(s.alias, parsed.rel);
emitCoalesced(s, parsed.kind);
}
function onData(s, chunk) {
s.buf += chunk;
let idx;
while ((idx = s.buf.indexOf('\n')) !== -1) {
const line = s.buf.slice(0, idx);
s.buf = s.buf.slice(idx + 1);
handleLine(s, line);
}
}
function scheduleRestart(s) {
const delay = backoffDelayMs(s.failures, RESTART_BASE_MS);
s.restartTimer = setT(() => { s.restartTimer = null; spawnChild(s); }, delay);
if (s.restartTimer && s.restartTimer.unref) s.restartTimer.unref();
}
// identity-guarded: a superseded child's late close must not touch the state — see .ai/contexts/session-cache.md ("watch channel")
function onExit(s, child, code) {
if (s.child !== child) return;
s.child = null;
if (s.stopped || s.unwatchable) return;
const isFailure = (Date.now() - s.spawnedAt) < HEALTHY_MS;
const prevDelay = backoffDelayMs(s.failures, RESTART_BASE_MS);
s.failures = isFailure ? s.failures + 1 : 0;
const delay = backoffDelayMs(s.failures, RESTART_BASE_MS);
if (isFailure && delay !== prevDelay) {
const tail = s.stderrTail ? ` — stderr: ${s.stderrTail}` : '';
log.warn(`[remote-watch:${s.alias}] ssh exited (code ${code}) after ${s.failures} consecutive quick ` +
`failure(s), retrying in ${Math.round(delay / 1000)}s${tail}`);
}
scheduleRestart(s);
}
function spawnChild(s) {
if (s.stopped || s.unwatchable) return;
let child;
try {
child = spawnFn('ssh', buildSshArgs(s.alias), { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
} catch (err) {
log.warn(`[remote-watch:${s.alias}] spawn failed: ${err.message}`);
s.failures += 1;
scheduleRestart(s);
return;
}
s.child = child;
s.buf = '';
s.spawnedAt = Date.now();
s.stderrTail = '';
if (child.stdout) {
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => { if (s.child === child) onData(s, chunk); });
}
if (child.stderr) {
child.stderr.setEncoding('utf8');
child.stderr.on('data', (chunk) => {
if (s.child !== child) return;
s.stderrTail = (s.stderrTail + chunk).slice(-200);
});
}
child.on('error', () => {});
child.on('close', (code) => onExit(s, child, code));
}
function getState(alias) {
let s = states.get(alias);
if (!s) {
s = {
alias, child: null, buf: '', stopped: true, unwatchable: false,
failures: 0, spawnedAt: 0, restartTimer: null, onEvent: null, onActivity: null,
stderrTail: '',
cooldown: { project: false, session: false },
pending: { project: false, session: false },
cooldownTimer: { project: null, session: null },
};
states.set(alias, s);
}
return s;
}
function start(alias, onEvent, onActivity) {
if (typeof alias !== 'string' || !alias || typeof onEvent !== 'function') return;
const s = getState(alias);
if (!s.stopped && !s.unwatchable) return;
s.stopped = false;
s.unwatchable = false;
s.failures = 0;
s.onEvent = onEvent;
s.onActivity = typeof onActivity === 'function' ? onActivity : null;
spawnChild(s);
}
function stop(alias) {
const s = states.get(alias);
if (!s) return;
s.stopped = true;
killChild(s);
for (const kind of Object.keys(s.cooldownTimer)) {
if (s.cooldownTimer[kind]) { clearT(s.cooldownTimer[kind]); s.cooldownTimer[kind] = null; }
s.cooldown[kind] = false;
s.pending[kind] = false;
}
}
function stopAll() {
for (const alias of [...states.keys()]) stop(alias);
}
function isRunning(alias) {
const s = states.get(alias);
return !!(s && !s.stopped && !s.unwatchable);
}
return { start, stop, stopAll, isRunning };
}
module.exports = {
createRemoteWatcher,
parseWatchLine,
buildWatchCommand,
buildSshArgs,
NO_INOTIFYWAIT_MARKER,
NO_INOTIFYWAIT_EXIT_CODE,
};