@peakflo/agent-harness 0.2.20-rc.1 → 0.2.20-rc.2
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/README.md +78 -0
- package/dist/browser-broker/agent-actions.mjs +799 -0
- package/dist/browser-broker/browsers.mjs +1699 -0
- package/dist/browser-broker/credentials.mjs +211 -0
- package/dist/browser-broker/disk-guard.mjs +84 -0
- package/dist/browser-broker/egress.mjs +183 -0
- package/dist/browser-broker/input.mjs +270 -0
- package/dist/browser-broker/lease.mjs +547 -0
- package/dist/browser-broker/local-network.mjs +173 -0
- package/dist/browser-broker/local.mjs +158 -0
- package/dist/browser-broker/log.mjs +28 -0
- package/dist/browser-broker/network-policy.mjs +144 -0
- package/dist/browser-broker/os-users.mjs +493 -0
- package/dist/browser-broker/package.json +1 -0
- package/dist/browser-broker/protocol.mjs +321 -0
- package/dist/browser-broker/queue.mjs +37 -0
- package/dist/browser-broker/recorder-scripts.mjs +221 -0
- package/dist/browser-broker/recorder.mjs +438 -0
- package/dist/browser-broker/recording-store.mjs +493 -0
- package/dist/browser-broker/relay.mjs +399 -0
- package/dist/browser-broker/screencast.mjs +198 -0
- package/dist/browser-broker/state-store.mjs +112 -0
- package/dist/browser-broker/transfers.mjs +460 -0
- package/dist/control-plane.d.ts +6 -0
- package/dist/control-plane.js +10 -0
- package/dist/github-workspace.d.ts +2 -0
- package/dist/github-workspace.js +4 -0
- package/dist/harness.d.ts +1 -0
- package/dist/harness.js +24 -0
- package/dist/runtime/knowledge-tools.d.ts +1 -0
- package/dist/runtime/knowledge-tools.js +3 -0
- package/dist/runtime/local-browser.d.ts +33 -0
- package/dist/runtime/local-browser.js +135 -0
- package/package.json +5 -3
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The broker's only link to the world: ONE outbound WebSocket to the workflow
|
|
3
|
+
* API, with the broker JWT as a bearer. Nothing in the sandbox listens for the
|
|
4
|
+
* browser, and (protocol v2) the broker makes no HTTP calls at all: recordings
|
|
5
|
+
* and downloads stay in the sandbox, file bytes move through the Daytona
|
|
6
|
+
* toolbox.
|
|
7
|
+
*
|
|
8
|
+
* wss://<control plane>/api/coding-agents/browser/broker
|
|
9
|
+
*
|
|
10
|
+
* On connect the broker sends `hello` first. Requests carry an `id` and are
|
|
11
|
+
* answered with a `result` of the same id. Text messages are capped at
|
|
12
|
+
* 256 KiB both ways; binary messages are screencast frames. The broker pings
|
|
13
|
+
* every 20 s and drops a connection that misses two pongs, then reconnects
|
|
14
|
+
* with backoff 1 s -> 30 s (with jitter) and sends hello again.
|
|
15
|
+
*
|
|
16
|
+
* Protocol v3 adds the OTHER direction on the same socket: `call()` sends a
|
|
17
|
+
* broker-initiated request (`cred_resolve`, `cred_hosts`, `cred_audit`) with
|
|
18
|
+
* a fresh `rid`, and the API answers with a `cred_result` push of that rid.
|
|
19
|
+
* This is how a secret value reaches the root broker without the harness
|
|
20
|
+
* ever seeing it, so `call()` never logs a message body or a result value:
|
|
21
|
+
* kinds, rids and codes only.
|
|
22
|
+
*/
|
|
23
|
+
import { randomUUID } from 'node:crypto';
|
|
24
|
+
import WebSocket from 'ws';
|
|
25
|
+
import {
|
|
26
|
+
BROKER_FEATURES,
|
|
27
|
+
BROWSER_BROKER_PROTOCOL_VERSION,
|
|
28
|
+
BROWSER_BROKER_PUSHES,
|
|
29
|
+
BROWSER_BROKER_REQUESTS,
|
|
30
|
+
BROWSER_BROKER_SOCKET_PATH,
|
|
31
|
+
BROWSER_MAX_FRAME_BYTES,
|
|
32
|
+
BROWSER_MAX_TEXT_MESSAGE_BYTES,
|
|
33
|
+
BrokerError
|
|
34
|
+
} from './protocol.mjs';
|
|
35
|
+
import { log, errorText } from './log.mjs';
|
|
36
|
+
|
|
37
|
+
const REQUESTS = new Set(BROWSER_BROKER_REQUESTS);
|
|
38
|
+
const PUSHES = new Set(BROWSER_BROKER_PUSHES);
|
|
39
|
+
|
|
40
|
+
export const PING_INTERVAL_MS = 20_000;
|
|
41
|
+
export const BACKOFF_MIN_MS = 1_000;
|
|
42
|
+
export const BACKOFF_MAX_MS = 30_000;
|
|
43
|
+
/** Contract: the broker gives the API 15 s to answer a broker-initiated request. */
|
|
44
|
+
export const CALL_TIMEOUT_MS = 15_000;
|
|
45
|
+
/** Bounded: a stuck API must not grow the pending map without limit. */
|
|
46
|
+
export const MAX_PENDING_CALLS = 256;
|
|
47
|
+
|
|
48
|
+
/** A refused/failed `call()`: `code` is the API's code or `unavailable`/`timeout`. */
|
|
49
|
+
export class RelayCallError extends Error {
|
|
50
|
+
constructor(code, message) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.code =
|
|
53
|
+
typeof code === 'string' && code ? code.slice(0, 64) : 'unavailable';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function nextBackoff(previous) {
|
|
58
|
+
const base = previous
|
|
59
|
+
? Math.min(previous * 2, BACKOFF_MAX_MS)
|
|
60
|
+
: BACKOFF_MIN_MS;
|
|
61
|
+
return base;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function socketUrl(controlPlaneUrl) {
|
|
65
|
+
const url = new URL(BROWSER_BROKER_SOCKET_PATH, controlPlaneUrl);
|
|
66
|
+
url.protocol = url.protocol === 'http:' ? 'ws:' : 'wss:';
|
|
67
|
+
return url.toString();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class Relay {
|
|
71
|
+
#ws = null;
|
|
72
|
+
#token;
|
|
73
|
+
#backoff = 0;
|
|
74
|
+
#reconnectTimer = null;
|
|
75
|
+
#pingTimer = null;
|
|
76
|
+
#missedPongs = 0;
|
|
77
|
+
#stopped = false;
|
|
78
|
+
#helloBuilder;
|
|
79
|
+
#handlers;
|
|
80
|
+
/** rid -> { resolve, reject, timer, t } for broker-initiated requests. */
|
|
81
|
+
#pending = new Map();
|
|
82
|
+
connected = false;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {{ controlPlaneUrl: string, token: string, brokerVersion: string,
|
|
86
|
+
* sandboxId: string|null, hello: () => Promise<{ browsers: object[] }>,
|
|
87
|
+
* onRequest: (msg) => Promise<unknown>, onPush: (msg) => void,
|
|
88
|
+
* onDisconnect: () => void }} options
|
|
89
|
+
*/
|
|
90
|
+
constructor(options) {
|
|
91
|
+
this.options = options;
|
|
92
|
+
this.#token = options.token;
|
|
93
|
+
this.#helloBuilder = options.hello;
|
|
94
|
+
this.#handlers = options;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** New token from a SIGHUP hand-off: reconnect with it at once. */
|
|
98
|
+
setToken(token) {
|
|
99
|
+
if (!token || token === this.#token) return false;
|
|
100
|
+
this.#token = token;
|
|
101
|
+
this.#backoff = 0;
|
|
102
|
+
this.#ws?.terminate();
|
|
103
|
+
if (!this.#ws) this.connect();
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
connect() {
|
|
108
|
+
if (this.#stopped) return;
|
|
109
|
+
clearTimeout(this.#reconnectTimer);
|
|
110
|
+
const ws = new WebSocket(socketUrl(this.options.controlPlaneUrl), {
|
|
111
|
+
headers: { authorization: `Bearer ${this.#token}` },
|
|
112
|
+
maxPayload: Math.max(
|
|
113
|
+
BROWSER_MAX_TEXT_MESSAGE_BYTES,
|
|
114
|
+
BROWSER_MAX_FRAME_BYTES
|
|
115
|
+
),
|
|
116
|
+
handshakeTimeout: 15_000,
|
|
117
|
+
perMessageDeflate: false
|
|
118
|
+
});
|
|
119
|
+
this.#ws = ws;
|
|
120
|
+
ws.on('open', () => void this.#onOpen(ws));
|
|
121
|
+
ws.on(
|
|
122
|
+
'message',
|
|
123
|
+
(data, isBinary) => void this.#onMessage(ws, data, isBinary)
|
|
124
|
+
);
|
|
125
|
+
ws.on('pong', () => {
|
|
126
|
+
this.#missedPongs = 0;
|
|
127
|
+
});
|
|
128
|
+
ws.on('unexpected-response', (_req, res) => {
|
|
129
|
+
log.warn('broker socket refused', { status: res.statusCode });
|
|
130
|
+
ws.terminate();
|
|
131
|
+
});
|
|
132
|
+
ws.on('error', (error) =>
|
|
133
|
+
log.warn('broker socket error', { error: errorText(error) })
|
|
134
|
+
);
|
|
135
|
+
ws.on('close', (code) => this.#onClose(ws, code));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async #onOpen(ws) {
|
|
139
|
+
if (ws !== this.#ws) return;
|
|
140
|
+
this.connected = true;
|
|
141
|
+
this.#backoff = 0;
|
|
142
|
+
this.#missedPongs = 0;
|
|
143
|
+
const hello = await this.#helloBuilder();
|
|
144
|
+
this.#sendOn(ws, {
|
|
145
|
+
t: 'hello',
|
|
146
|
+
protocolVersion: BROWSER_BROKER_PROTOCOL_VERSION,
|
|
147
|
+
brokerVersion: this.options.brokerVersion,
|
|
148
|
+
sandboxId: this.options.sandboxId,
|
|
149
|
+
// v3. The daemon narrows this when the credential proxy did not start.
|
|
150
|
+
features: [...BROKER_FEATURES],
|
|
151
|
+
...hello
|
|
152
|
+
});
|
|
153
|
+
clearInterval(this.#pingTimer);
|
|
154
|
+
this.#pingTimer = setInterval(() => {
|
|
155
|
+
if (this.#missedPongs >= 2) {
|
|
156
|
+
log.warn('broker socket missed pongs; reconnecting');
|
|
157
|
+
ws.terminate();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this.#missedPongs++;
|
|
161
|
+
try {
|
|
162
|
+
ws.ping();
|
|
163
|
+
} catch {
|
|
164
|
+
/* closing */
|
|
165
|
+
}
|
|
166
|
+
}, PING_INTERVAL_MS);
|
|
167
|
+
log.info('broker socket connected');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
#onClose(ws, code) {
|
|
171
|
+
if (ws !== this.#ws) return;
|
|
172
|
+
this.connected = false;
|
|
173
|
+
this.#ws = null;
|
|
174
|
+
clearInterval(this.#pingTimer);
|
|
175
|
+
// An answer can only arrive on the socket that carried the request.
|
|
176
|
+
this.#rejectPending('unavailable', 'The control plane connection closed.');
|
|
177
|
+
this.#handlers.onDisconnect?.();
|
|
178
|
+
if (this.#stopped) return;
|
|
179
|
+
this.#backoff = nextBackoff(this.#backoff);
|
|
180
|
+
const delay = Math.round(this.#backoff * (0.8 + Math.random() * 0.4));
|
|
181
|
+
log.info('broker socket closed; reconnecting', { code, delayMs: delay });
|
|
182
|
+
this.#reconnectTimer = setTimeout(() => this.connect(), delay);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async #onMessage(ws, data, isBinary) {
|
|
186
|
+
if (isBinary) return; // the API never sends frames to the broker
|
|
187
|
+
if (data.length > BROWSER_MAX_TEXT_MESSAGE_BYTES) {
|
|
188
|
+
log.warn('broker socket message too large', { bytes: data.length });
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
let msg;
|
|
192
|
+
try {
|
|
193
|
+
msg = JSON.parse(data.toString('utf8'));
|
|
194
|
+
} catch {
|
|
195
|
+
log.warn('broker socket message is not JSON');
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (!msg || typeof msg.t !== 'string') return;
|
|
199
|
+
if (msg.t === 'ping') {
|
|
200
|
+
this.#sendOn(ws, { t: 'pong', at: msg.at });
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (msg.t === 'cred_result') {
|
|
204
|
+
this.#settle(msg);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (PUSHES.has(msg.t)) {
|
|
208
|
+
try {
|
|
209
|
+
this.#handlers.onPush(msg);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
log.warn('push failed', { t: msg.t, error: errorText(error) });
|
|
212
|
+
}
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (!REQUESTS.has(msg.t) || typeof msg.id !== 'string') {
|
|
216
|
+
if (typeof msg.id === 'string') {
|
|
217
|
+
this.#sendOn(ws, {
|
|
218
|
+
t: 'result',
|
|
219
|
+
id: msg.id,
|
|
220
|
+
ok: false,
|
|
221
|
+
error: { code: 'invalid_args', message: 'Unknown request' }
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const started = Date.now();
|
|
227
|
+
let reply;
|
|
228
|
+
try {
|
|
229
|
+
const value = await this.#handlers.onRequest(msg);
|
|
230
|
+
reply = { t: 'result', id: msg.id, ok: true, value: value ?? null };
|
|
231
|
+
} catch (error) {
|
|
232
|
+
const brokerError =
|
|
233
|
+
error instanceof BrokerError
|
|
234
|
+
? error
|
|
235
|
+
: new BrokerError(
|
|
236
|
+
'internal',
|
|
237
|
+
'The broker could not run this request.'
|
|
238
|
+
);
|
|
239
|
+
if (!(error instanceof BrokerError)) {
|
|
240
|
+
log.error('request failed', { t: msg.t, error: errorText(error) });
|
|
241
|
+
}
|
|
242
|
+
reply = {
|
|
243
|
+
t: 'result',
|
|
244
|
+
id: msg.id,
|
|
245
|
+
ok: false,
|
|
246
|
+
error: brokerError.toJSON()
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
// Ids, kinds and codes only: never args, text or keys.
|
|
250
|
+
log.info('request', {
|
|
251
|
+
t: msg.t,
|
|
252
|
+
name: msg.t === 'command' ? msg.name : undefined,
|
|
253
|
+
op:
|
|
254
|
+
msg.t === 'control' || msg.t === 'recording_read' ? msg.op : undefined,
|
|
255
|
+
sessionId: msg.sessionId,
|
|
256
|
+
ok: reply.ok,
|
|
257
|
+
code: reply.ok ? undefined : reply.error.code,
|
|
258
|
+
ms: Date.now() - started
|
|
259
|
+
});
|
|
260
|
+
this.send(reply);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
#sendOn(ws, msg) {
|
|
264
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) return false;
|
|
265
|
+
let text = JSON.stringify(msg);
|
|
266
|
+
if (Buffer.byteLength(text) > BROWSER_MAX_TEXT_MESSAGE_BYTES) {
|
|
267
|
+
if (msg.t !== 'result') {
|
|
268
|
+
log.warn('dropped an oversize message', { t: msg.t });
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
text = JSON.stringify({
|
|
272
|
+
t: 'result',
|
|
273
|
+
id: msg.id,
|
|
274
|
+
ok: false,
|
|
275
|
+
error: {
|
|
276
|
+
code: 'too_large',
|
|
277
|
+
message: 'The result was too large to send. Ask for a smaller page.'
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
ws.send(text);
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* A broker-initiated request (v3): sends `{ ...msg, rid }` and resolves
|
|
287
|
+
* with the `value` of the API's `cred_result` for that rid, or rejects with
|
|
288
|
+
* a RelayCallError (the API's error code, `timeout`, or `unavailable` when
|
|
289
|
+
* there is no socket or it closes first). The value may carry a secret: the
|
|
290
|
+
* caller owns it, nothing here logs or keeps it.
|
|
291
|
+
*/
|
|
292
|
+
call(msg, timeoutMs = CALL_TIMEOUT_MS) {
|
|
293
|
+
if (!this.connected || !this.#ws) {
|
|
294
|
+
return Promise.reject(
|
|
295
|
+
new RelayCallError('unavailable', 'The control plane is not connected.')
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
if (this.#pending.size >= MAX_PENDING_CALLS) {
|
|
299
|
+
return Promise.reject(
|
|
300
|
+
new RelayCallError(
|
|
301
|
+
'unavailable',
|
|
302
|
+
'Too many pending control plane requests.'
|
|
303
|
+
)
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const rid = randomUUID();
|
|
307
|
+
return new Promise((resolve, reject) => {
|
|
308
|
+
const timer = setTimeout(
|
|
309
|
+
() => {
|
|
310
|
+
if (!this.#pending.delete(rid)) return;
|
|
311
|
+
log.warn('broker call timed out', { t: msg.t, rid });
|
|
312
|
+
reject(
|
|
313
|
+
new RelayCallError(
|
|
314
|
+
'timeout',
|
|
315
|
+
'The control plane did not answer in time.'
|
|
316
|
+
)
|
|
317
|
+
);
|
|
318
|
+
},
|
|
319
|
+
Math.max(1, timeoutMs)
|
|
320
|
+
);
|
|
321
|
+
timer.unref?.();
|
|
322
|
+
this.#pending.set(rid, { resolve, reject, timer, t: msg.t });
|
|
323
|
+
if (!this.#sendOn(this.#ws, { ...msg, rid })) {
|
|
324
|
+
clearTimeout(timer);
|
|
325
|
+
this.#pending.delete(rid);
|
|
326
|
+
reject(
|
|
327
|
+
new RelayCallError(
|
|
328
|
+
'unavailable',
|
|
329
|
+
'The control plane is not connected.'
|
|
330
|
+
)
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Pending broker-initiated requests (tests, diagnostics). */
|
|
337
|
+
get pendingCalls() {
|
|
338
|
+
return this.#pending.size;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
#settle(msg) {
|
|
342
|
+
const rid = typeof msg.rid === 'string' ? msg.rid : '';
|
|
343
|
+
const entry = this.#pending.get(rid);
|
|
344
|
+
if (!entry) {
|
|
345
|
+
log.warn('cred_result for an unknown request', { rid: rid.slice(0, 64) });
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
this.#pending.delete(rid);
|
|
349
|
+
clearTimeout(entry.timer);
|
|
350
|
+
if (msg.ok === true) {
|
|
351
|
+
entry.resolve(msg.value ?? null);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const code =
|
|
355
|
+
typeof msg.error?.code === 'string' ? msg.error.code : 'unavailable';
|
|
356
|
+
// Ids, kinds and codes only: never a value.
|
|
357
|
+
log.info('broker call refused', { t: entry.t, rid, code });
|
|
358
|
+
entry.reject(
|
|
359
|
+
new RelayCallError(
|
|
360
|
+
code,
|
|
361
|
+
typeof msg.error?.message === 'string'
|
|
362
|
+
? msg.error.message.slice(0, 300)
|
|
363
|
+
: 'The control plane refused the request.'
|
|
364
|
+
)
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#rejectPending(code, message) {
|
|
369
|
+
for (const [rid, entry] of this.#pending) {
|
|
370
|
+
clearTimeout(entry.timer);
|
|
371
|
+
entry.reject(new RelayCallError(code, message));
|
|
372
|
+
this.#pending.delete(rid);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Send a JSON message on the current socket; false when disconnected. */
|
|
377
|
+
send(msg) {
|
|
378
|
+
return this.#sendOn(this.#ws, msg);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
sendBinary(buffer) {
|
|
382
|
+
const ws = this.#ws;
|
|
383
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) return false;
|
|
384
|
+
ws.send(buffer, { binary: true });
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
bufferedAmount() {
|
|
389
|
+
return this.#ws?.bufferedAmount ?? Number.MAX_SAFE_INTEGER;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
stop() {
|
|
393
|
+
this.#stopped = true;
|
|
394
|
+
this.#rejectPending('unavailable', 'The broker is stopping.');
|
|
395
|
+
clearTimeout(this.#reconnectTimer);
|
|
396
|
+
clearInterval(this.#pingTimer);
|
|
397
|
+
this.#ws?.close(1001, 'broker stopping');
|
|
398
|
+
}
|
|
399
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live frames for viewers: CDP Page.startScreencast on the active page.
|
|
3
|
+
*
|
|
4
|
+
* Runs ONLY while the API says someone is watching (`screencast on`), in one
|
|
5
|
+
* of two profiles the API picks:
|
|
6
|
+
*
|
|
7
|
+
* local JPEG q60, at most 1280x800, at most 10 fps (viewers on the API
|
|
8
|
+
* instance that holds the broker socket)
|
|
9
|
+
* remote JPEG q40, at most 960x600, at most 2 fps (a viewer on another API
|
|
10
|
+
* instance: frames cross the Postgres NOTIFY bus)
|
|
11
|
+
*
|
|
12
|
+
* The rate is a HARD cap: a frame that arrives inside the interval is held
|
|
13
|
+
* as the latest and sent when the interval ends (newer frames replace it);
|
|
14
|
+
* the ack to Chrome is delayed too, so Chrome itself slows down. A frame is
|
|
15
|
+
* DROPPED (still acked) when the socket already holds more
|
|
16
|
+
* than 2 MiB unsent: a slow viewer link must not grow memory without bound.
|
|
17
|
+
*/
|
|
18
|
+
import { BROWSER_MAX_FRAME_BYTES, encodeBrowserFrame } from './protocol.mjs';
|
|
19
|
+
import { log } from './log.mjs';
|
|
20
|
+
|
|
21
|
+
export const SCREENCAST_PROFILES = Object.freeze({
|
|
22
|
+
local: Object.freeze({
|
|
23
|
+
quality: 60,
|
|
24
|
+
maxWidth: 1280,
|
|
25
|
+
maxHeight: 800,
|
|
26
|
+
minIntervalMs: 100
|
|
27
|
+
}),
|
|
28
|
+
remote: Object.freeze({
|
|
29
|
+
quality: 40,
|
|
30
|
+
maxWidth: 960,
|
|
31
|
+
maxHeight: 600,
|
|
32
|
+
minIntervalMs: 500
|
|
33
|
+
})
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export function screencastOptions(profile) {
|
|
37
|
+
const p = SCREENCAST_PROFILES[profile] ?? SCREENCAST_PROFILES.local;
|
|
38
|
+
return {
|
|
39
|
+
format: 'jpeg',
|
|
40
|
+
quality: p.quality,
|
|
41
|
+
maxWidth: p.maxWidth,
|
|
42
|
+
maxHeight: p.maxHeight,
|
|
43
|
+
everyNthFrame: 1
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const SCREENCAST_OPTIONS = Object.freeze(screencastOptions('local'));
|
|
48
|
+
export const MIN_FRAME_INTERVAL_MS = SCREENCAST_PROFILES.local.minIntervalMs;
|
|
49
|
+
export const MAX_BUFFERED_BYTES = 2 * 1024 * 1024;
|
|
50
|
+
|
|
51
|
+
/** Pixel size from a JPEG's SOF marker; null if not found. */
|
|
52
|
+
export function jpegSize(buffer) {
|
|
53
|
+
let offset = 2;
|
|
54
|
+
while (offset + 9 < buffer.length) {
|
|
55
|
+
if (buffer[offset] !== 0xff) return null;
|
|
56
|
+
const marker = buffer[offset + 1];
|
|
57
|
+
const length = buffer.readUInt16BE(offset + 2);
|
|
58
|
+
if (
|
|
59
|
+
marker >= 0xc0 &&
|
|
60
|
+
marker <= 0xcf &&
|
|
61
|
+
![0xc4, 0xc8, 0xcc].includes(marker)
|
|
62
|
+
) {
|
|
63
|
+
return {
|
|
64
|
+
height: buffer.readUInt16BE(offset + 5),
|
|
65
|
+
width: buffer.readUInt16BE(offset + 7)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
offset += 2 + length;
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class Screencast {
|
|
74
|
+
#cdp = null;
|
|
75
|
+
#page = null;
|
|
76
|
+
#seq = 0;
|
|
77
|
+
#lastSent = 0;
|
|
78
|
+
#onFrame = null;
|
|
79
|
+
#active = false;
|
|
80
|
+
#profile = 'local';
|
|
81
|
+
#held = null;
|
|
82
|
+
#heldTimer = null;
|
|
83
|
+
dropped = 0;
|
|
84
|
+
sent = 0;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {{ sessionId: string, send: (buf: Buffer) => boolean,
|
|
88
|
+
* bufferedAmount: () => number, openCdp: (page) => Promise<CDPSession> }} io
|
|
89
|
+
*/
|
|
90
|
+
constructor(io) {
|
|
91
|
+
this.io = io;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
get active() {
|
|
95
|
+
return this.#active;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
get profile() {
|
|
99
|
+
return this.#profile;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async start(page, profile = this.#profile) {
|
|
103
|
+
const next = SCREENCAST_PROFILES[profile] ? profile : 'local';
|
|
104
|
+
if (this.#active && this.#page === page && this.#profile === next) return;
|
|
105
|
+
await this.stop();
|
|
106
|
+
this.#active = true;
|
|
107
|
+
this.#page = page;
|
|
108
|
+
this.#profile = next;
|
|
109
|
+
const cdp = await this.io.openCdp(page);
|
|
110
|
+
this.#cdp = cdp;
|
|
111
|
+
this.#onFrame = (event) => this.#frame(cdp, event);
|
|
112
|
+
cdp.on('Page.screencastFrame', this.#onFrame);
|
|
113
|
+
await cdp.send('Page.startScreencast', screencastOptions(next));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async stop() {
|
|
117
|
+
const cdp = this.#cdp;
|
|
118
|
+
clearTimeout(this.#heldTimer);
|
|
119
|
+
this.#heldTimer = null;
|
|
120
|
+
this.#held = null;
|
|
121
|
+
this.#active = false;
|
|
122
|
+
this.#cdp = null;
|
|
123
|
+
this.#page = null;
|
|
124
|
+
if (!cdp) return;
|
|
125
|
+
cdp.off('Page.screencastFrame', this.#onFrame);
|
|
126
|
+
await cdp.send('Page.stopScreencast').catch(() => undefined);
|
|
127
|
+
await cdp.detach().catch(() => undefined);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
#frame(cdp, { data, metadata, sessionId }) {
|
|
131
|
+
const ack = () =>
|
|
132
|
+
cdp.send('Page.screencastFrameAck', { sessionId }).catch(() => undefined);
|
|
133
|
+
if (!this.#active || this.#cdp !== cdp) return void ack();
|
|
134
|
+
const interval = SCREENCAST_PROFILES[this.#profile].minIntervalMs;
|
|
135
|
+
const wait = Math.max(0, interval - (Date.now() - this.#lastSent));
|
|
136
|
+
// Hard cap: Chrome keeps more than one frame in flight, so a delayed ack
|
|
137
|
+
// alone does not hold the rate. A frame inside the interval is held as the
|
|
138
|
+
// LATEST and sent when the interval ends (a newer one replaces it), so the
|
|
139
|
+
// viewer never misses the page's last paint.
|
|
140
|
+
if (wait > 0) {
|
|
141
|
+
if (this.#held) this.dropped++;
|
|
142
|
+
this.#held = { data, metadata };
|
|
143
|
+
if (!this.#heldTimer) {
|
|
144
|
+
this.#heldTimer = setTimeout(() => {
|
|
145
|
+
this.#heldTimer = null;
|
|
146
|
+
const held = this.#held;
|
|
147
|
+
this.#held = null;
|
|
148
|
+
if (held && this.#active && this.#cdp === cdp)
|
|
149
|
+
this.#send(held.data, held.metadata);
|
|
150
|
+
}, wait);
|
|
151
|
+
}
|
|
152
|
+
setTimeout(ack, wait);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
this.#send(data, metadata);
|
|
156
|
+
// Delaying the ack also slows Chrome itself down.
|
|
157
|
+
setTimeout(ack, interval);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
#send(data, metadata) {
|
|
161
|
+
if (this.io.bufferedAmount() > MAX_BUFFERED_BYTES) {
|
|
162
|
+
this.dropped++;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const now = Date.now();
|
|
166
|
+
const jpeg = Buffer.from(data, 'base64');
|
|
167
|
+
const size = jpegSize(jpeg) ?? {
|
|
168
|
+
width: metadata.deviceWidth,
|
|
169
|
+
height: metadata.deviceHeight
|
|
170
|
+
};
|
|
171
|
+
const frame = encodeBrowserFrame(
|
|
172
|
+
{
|
|
173
|
+
sessionId: this.io.sessionId,
|
|
174
|
+
seq: ++this.#seq,
|
|
175
|
+
width: size.width,
|
|
176
|
+
height: size.height,
|
|
177
|
+
viewportWidth: Math.round(metadata.deviceWidth),
|
|
178
|
+
viewportHeight: Math.round(metadata.deviceHeight),
|
|
179
|
+
at: now
|
|
180
|
+
},
|
|
181
|
+
jpeg
|
|
182
|
+
);
|
|
183
|
+
if (frame.length > BROWSER_MAX_FRAME_BYTES) {
|
|
184
|
+
this.dropped++;
|
|
185
|
+
} else if (this.io.send(frame)) {
|
|
186
|
+
this.sent++;
|
|
187
|
+
this.#lastSent = now;
|
|
188
|
+
log.debug('frame', {
|
|
189
|
+
profile: this.#profile,
|
|
190
|
+
bytes: frame.length,
|
|
191
|
+
width: size.width,
|
|
192
|
+
height: size.height
|
|
193
|
+
});
|
|
194
|
+
} else {
|
|
195
|
+
this.dropped++;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Root-only persistence of each chat browser's lease and state, so a broker
|
|
3
|
+
* restart continues ABOVE the last fencing epoch (protocol v2: the broker is
|
|
4
|
+
* the source of truth; there is no database copy).
|
|
5
|
+
*
|
|
6
|
+
* /var/pf/browser-state/ root 0700
|
|
7
|
+
* /var/pf/browser-state/<user>.json root 0600, one per chat (<user> = pfb-<8 hex>)
|
|
8
|
+
*
|
|
9
|
+
* Every write is atomic and durable: a 0600 temp file in the same directory,
|
|
10
|
+
* fsync, rename, then fsync of the directory.
|
|
11
|
+
* A record holds { sessionId, user, epoch, leaseId, controller, startedBy, url,
|
|
12
|
+
* title, lastError, updatedAt } and never typed text or URLs with queries
|
|
13
|
+
* (url is stored as recordingUrl(url): origin + path).
|
|
14
|
+
*
|
|
15
|
+
* A wiped chat keeps a tombstone ({ sessionId, epoch, wiped: true }) so a
|
|
16
|
+
* browser opened again later still starts above every epoch ever issued.
|
|
17
|
+
*/
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
|
|
21
|
+
export const STATE_DIR = '/var/pf/browser-state';
|
|
22
|
+
const NAME = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
23
|
+
|
|
24
|
+
/** fsync a directory so a rename inside it survives a crash. */
|
|
25
|
+
export function syncDir(dir) {
|
|
26
|
+
const fd = fs.openSync(dir, fs.constants.O_RDONLY | fs.constants.O_DIRECTORY);
|
|
27
|
+
try {
|
|
28
|
+
fs.fsyncSync(fd);
|
|
29
|
+
} finally {
|
|
30
|
+
fs.closeSync(fd);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class StateStore {
|
|
35
|
+
constructor(dir = STATE_DIR) {
|
|
36
|
+
this.dir = dir;
|
|
37
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
38
|
+
fs.chmodSync(dir, 0o700);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
#file(user) {
|
|
42
|
+
if (!NAME.test(String(user))) throw new Error('Invalid state file name');
|
|
43
|
+
return path.join(this.dir, `${user}.json`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
write(user, record) {
|
|
47
|
+
const file = this.#file(user);
|
|
48
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
49
|
+
const fd = fs.openSync(
|
|
50
|
+
tmp,
|
|
51
|
+
fs.constants.O_WRONLY |
|
|
52
|
+
fs.constants.O_CREAT |
|
|
53
|
+
fs.constants.O_TRUNC |
|
|
54
|
+
fs.constants.O_NOFOLLOW,
|
|
55
|
+
0o600
|
|
56
|
+
);
|
|
57
|
+
try {
|
|
58
|
+
fs.writeSync(fd, JSON.stringify({ ...record, user }));
|
|
59
|
+
fs.fchmodSync(fd, 0o600);
|
|
60
|
+
fs.fsyncSync(fd);
|
|
61
|
+
} finally {
|
|
62
|
+
fs.closeSync(fd);
|
|
63
|
+
}
|
|
64
|
+
fs.renameSync(tmp, file);
|
|
65
|
+
// The rename is durable only once the directory entry is: fsync the dir.
|
|
66
|
+
syncDir(this.dir);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Every readable record, keyed by sessionId (the highest epoch wins on a duplicate). */
|
|
70
|
+
loadAll() {
|
|
71
|
+
const out = new Map();
|
|
72
|
+
let names = [];
|
|
73
|
+
try {
|
|
74
|
+
names = fs.readdirSync(this.dir);
|
|
75
|
+
} catch {
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
for (const name of names) {
|
|
79
|
+
if (!name.endsWith('.json')) {
|
|
80
|
+
if (name.endsWith('.tmp'))
|
|
81
|
+
fs.rmSync(path.join(this.dir, name), { force: true });
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const record = JSON.parse(
|
|
86
|
+
fs.readFileSync(path.join(this.dir, name), 'utf8')
|
|
87
|
+
);
|
|
88
|
+
if (
|
|
89
|
+
typeof record?.sessionId !== 'string' ||
|
|
90
|
+
!Number.isSafeInteger(record.epoch)
|
|
91
|
+
)
|
|
92
|
+
continue;
|
|
93
|
+
const previous = out.get(record.sessionId);
|
|
94
|
+
if (!previous || previous.epoch < record.epoch)
|
|
95
|
+
out.set(record.sessionId, record);
|
|
96
|
+
} catch {
|
|
97
|
+
/* unreadable: ignore */
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Replace a chat's record with a tombstone that keeps only its epoch. */
|
|
104
|
+
tombstone(user, sessionId, epoch) {
|
|
105
|
+
this.write(user, {
|
|
106
|
+
sessionId,
|
|
107
|
+
epoch,
|
|
108
|
+
wiped: true,
|
|
109
|
+
updatedAt: new Date().toISOString()
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|