@revoengine/cli 1.0.31 → 1.0.33

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 (51) hide show
  1. package/README.md +68 -17
  2. package/dist/src/automation-contract.generated.d.ts +55 -21
  3. package/dist/src/automation-contract.generated.js +67 -25
  4. package/dist/src/client.d.ts +32 -2
  5. package/dist/src/client.js +54 -13
  6. package/dist/src/commands/database-tables.js +10 -3
  7. package/dist/src/commands/migrate.js +2 -0
  8. package/dist/src/commands/terminal-browser-central-profile.d.ts +65 -0
  9. package/dist/src/commands/terminal-browser-central-profile.js +203 -0
  10. package/dist/src/commands/terminal-browser-human-input.d.ts +3 -1
  11. package/dist/src/commands/terminal-browser-human-input.js +3 -1
  12. package/dist/src/commands/terminal-browser-interaction.js +1 -1
  13. package/dist/src/commands/terminal-browser-live-protocol.d.ts +34 -2
  14. package/dist/src/commands/terminal-browser-live-protocol.js +28 -12
  15. package/dist/src/commands/terminal-browser-live-transport.d.ts +1 -0
  16. package/dist/src/commands/terminal-browser-live-transport.js +9 -5
  17. package/dist/src/commands/terminal-browser-live.d.ts +24 -3
  18. package/dist/src/commands/terminal-browser-live.js +199 -30
  19. package/dist/src/commands/terminal-browser-page-input.d.ts +4 -1
  20. package/dist/src/commands/terminal-browser-page-input.js +46 -1
  21. package/dist/src/commands/terminal-browser-profile-crypto.d.ts +30 -0
  22. package/dist/src/commands/terminal-browser-profile-crypto.js +101 -0
  23. package/dist/src/commands/terminal-browser-profile-import-runner.d.ts +31 -0
  24. package/dist/src/commands/terminal-browser-profile-import-runner.js +112 -0
  25. package/dist/src/commands/terminal-browser-profile-import.d.ts +28 -0
  26. package/dist/src/commands/terminal-browser-profile-import.js +125 -0
  27. package/dist/src/commands/terminal-browser-profile-import.protocol.d.ts +50 -0
  28. package/dist/src/commands/terminal-browser-profile-import.protocol.js +52 -0
  29. package/dist/src/commands/terminal-browser-profile-preferences.d.ts +1 -0
  30. package/dist/src/commands/terminal-browser-profile-preferences.js +16 -0
  31. package/dist/src/commands/terminal-browser-profile-sync.d.ts +14 -0
  32. package/dist/src/commands/terminal-browser-profile-sync.js +169 -0
  33. package/dist/src/commands/terminal-browser-profile-sync.protocol.d.ts +50 -0
  34. package/dist/src/commands/terminal-browser-profile-sync.protocol.js +19 -0
  35. package/dist/src/commands/terminal-browser-profile.d.ts +10 -1
  36. package/dist/src/commands/terminal-browser-profile.js +30 -4
  37. package/dist/src/commands/terminal-browser-recording.d.ts +39 -0
  38. package/dist/src/commands/terminal-browser-recording.js +74 -0
  39. package/dist/src/commands/terminal-browser-recording.protocol.d.ts +49 -0
  40. package/dist/src/commands/terminal-browser-recording.protocol.js +16 -0
  41. package/dist/src/commands/terminal-browser.d.ts +14 -1
  42. package/dist/src/commands/terminal-browser.js +98 -33
  43. package/dist/src/commands/terminal-output-buffer.d.ts +18 -1
  44. package/dist/src/commands/terminal-output-buffer.js +95 -8
  45. package/dist/src/commands/terminal.d.ts +3 -1
  46. package/dist/src/commands/terminal.js +257 -54
  47. package/dist/src/resource-syncs/database-table-sync.d.ts +8 -1
  48. package/dist/src/resource-syncs/database-table-sync.js +34 -11
  49. package/dist/src/sync-output.d.ts +3 -3
  50. package/dist/src/ui.js +1 -0
  51. package/package.json +5 -5
@@ -33,11 +33,44 @@ export function browserAuthPage(value) {
33
33
  }
34
34
  }
35
35
  export class BrowserLiveHandle {
36
- options;
36
+ baseOptions;
37
+ viewerOptions;
38
+ get options() { return this.viewerOptions ?? this.baseOptions; }
39
+ get sessionOwned() { return this.baseOptions.sessionOwned === true; }
40
+ refreshRecordingAuthority(options) {
41
+ if (!this.sessionOwned || this.closed || ['actorUserId', 'threadId', 'agentTerminalWorkspaceId', 'browserSessionId']
42
+ .some(key => options[key] !== this.baseOptions[key]) || !options.isAuthorized())
43
+ throw new Error('browser_live_scope_invalid');
44
+ this.baseOptions = { ...options, sessionOwned: true };
45
+ }
46
+ attachViewer(options) {
47
+ if (!this.sessionOwned || this.closed || ['actorUserId', 'threadId', 'agentTerminalWorkspaceId', 'connectionId', 'browserSessionId']
48
+ .some(key => options[key] !== this.baseOptions[key]) || !options.isAuthorized())
49
+ throw new Error('browser_live_scope_invalid');
50
+ this.viewerOptions = options;
51
+ }
52
+ async resumeRecordingIfPublic() {
53
+ if (!this.sessionOwned || this.closed || this.mode !== 'observe' || this.recordingMode === 'off')
54
+ return;
55
+ const page = this.port.active().page;
56
+ if (browserAuthPage(page.url()) || await pageHasSecretFocus(page))
57
+ return;
58
+ if (this.private) {
59
+ this.port.control.private = false;
60
+ await this.stopCapture();
61
+ }
62
+ await this.start();
63
+ }
37
64
  port;
38
65
  page;
39
66
  segment;
40
67
  recordingMode = 'off';
68
+ fps = 15;
69
+ settingsApplied = false;
70
+ elapsedMs = 0;
71
+ segmentStartedAt;
72
+ scroll;
73
+ get frameInterval() { return 1000 / this.fps; }
41
74
  sequence = 0;
42
75
  lastFrameAt = 0;
43
76
  generation = 0;
@@ -45,17 +78,23 @@ export class BrowserLiveHandle {
45
78
  stopped = false;
46
79
  stateFingerprint = '';
47
80
  lastStatePoll = 0;
48
- requests = new Set();
81
+ requests = new Map();
49
82
  poll;
50
83
  trailingFrame;
51
84
  trailingCapturePending = false;
52
85
  profileChoicesPending;
53
- constructor(options, port) { this.options = options; this.port = port; }
86
+ constructor(options, port) {
87
+ this.baseOptions = options;
88
+ this.port = port;
89
+ this.fps = options.browserSettings?.fps ?? 15;
90
+ if (options.browserSettings?.alwaysRecord)
91
+ this.recordingMode = options.browserSettings.recordingMode;
92
+ }
54
93
  get mode() { return this.port.control.mode; }
55
94
  get controlEpoch() { return this.port.control.epoch; }
56
95
  get private() { return this.port.control.private; }
57
96
  assertAuthorized() {
58
- if (this.closed || !this.options.isAuthorized() || !this.port.authorized())
97
+ if (this.closed || !this.baseOptions.isAuthorized() || !this.port.authorized())
59
98
  throw new Error('browser_live_scope_expired');
60
99
  }
61
100
  async state() {
@@ -80,13 +119,25 @@ export class BrowserLiveHandle {
80
119
  ? profiles.find(choice => choice.persistent && choice.name === selected.name) || { ...selected, default: false }
81
120
  : profiles[0] : selected;
82
121
  this.assertAuthorized();
83
- return { ...state, profile, profiles, private: this.private, frameSequence: this.sequence, recording: { mode: this.recordingMode } };
122
+ return { ...state, profile, profiles, private: this.private, frameSequence: this.sequence, recording: { mode: this.recordingMode },
123
+ settings: { fps: this.fps, ...this.port.active().page.viewportSize() },
124
+ recordingState: { status: this.segment ? 'recording' : this.recordingMode === 'off' ? 'off' : 'paused',
125
+ elapsedMs: this.elapsedMs + (this.segmentStartedAt === undefined ? 0 : Date.now() - this.segmentStartedAt) } };
84
126
  }
85
127
  async start() {
86
128
  this.assertAuthorized();
87
129
  this.stopped = false;
88
- this.port.control.connected = true;
130
+ this.port.control.connected = !this.sessionOwned || !!this.viewerOptions;
131
+ if (this.sessionOwned && !this.viewerOptions && this.recordingMode === 'off')
132
+ return;
133
+ if (this.options.browserSettings && !this.settingsApplied) {
134
+ await this.port.active().page.setViewportSize({ width: this.options.browserSettings.width, height: this.options.browserSettings.height });
135
+ this.port.observations.invalidate();
136
+ this.settingsApplied = true;
137
+ }
89
138
  await this.sync();
139
+ if (this.sessionOwned && this.viewerOptions)
140
+ await this.refreshFrame();
90
141
  // Website-created popups and model tab switches need no model call to refresh the active stream.
91
142
  if (this.poll)
92
143
  clearInterval(this.poll);
@@ -118,24 +169,38 @@ export class BrowserLiveHandle {
118
169
  const segment = this.segment;
119
170
  this.page = undefined;
120
171
  this.segment = undefined;
172
+ this.scroll = undefined;
121
173
  this.generation++;
122
174
  if (page && !page.isClosed())
123
175
  await page.screencast.stop().catch(() => undefined);
176
+ if (this.segmentStartedAt !== undefined) {
177
+ this.elapsedMs += Date.now() - this.segmentStartedAt;
178
+ this.segmentStartedAt = undefined;
179
+ }
124
180
  if (segment) {
125
181
  const file = fs.lstatSync(segment.path, { throwIfNoEntry: false });
126
182
  if (file?.isFile() && !file.isSymbolicLink() && file.size > 0 && file.size <= 256 * 1024 * 1024) {
127
183
  fs.chmodSync(segment.path, 0o600);
128
- this.options.onRecording({ ...segment, size: file.size });
184
+ if ((this.sessionOwned ? this.baseOptions : this.options).onRecording({ ...segment, size: file.size }) === false)
185
+ this.recordingMode = 'off';
129
186
  }
130
187
  else if (file && file.size === 0)
131
188
  fs.unlinkSync(segment.path);
132
189
  }
133
190
  }
134
- async pauseRecording() {
191
+ async pauseRecording(restartPreview = true) {
135
192
  if (!this.segment && this.recordingMode === 'off')
136
193
  return;
137
194
  this.generation++;
138
195
  this.port.control.private = true;
196
+ if (!restartPreview) {
197
+ // Navigation routes must not wait for a queued DOM evaluation which itself
198
+ // waits for navigation. Stop native media first; finalize after the route resumes.
199
+ if (this.page && !this.page.isClosed())
200
+ await this.page.screencast.stop().catch(() => undefined);
201
+ void this.stopCapture().catch(() => this.block());
202
+ return;
203
+ }
139
204
  await this.stopCapture();
140
205
  if (!this.closed)
141
206
  await this.sync();
@@ -157,7 +222,7 @@ export class BrowserLiveHandle {
157
222
  this.assertAuthorized();
158
223
  if (this.mode !== mode || epoch !== this.controlEpoch)
159
224
  return false;
160
- const fingerprint = JSON.stringify({ mode, epoch, tabs: state.tabs, profile: state.profile, private: state.private });
225
+ const fingerprint = JSON.stringify({ mode, epoch, tabs: state.tabs, profile: state.profile, private: state.private, focusId: state.focusId, settings: state.settings, recordingState: state.recordingState });
161
226
  if (fingerprint !== this.stateFingerprint && this.options.onState({ controlEpoch: epoch, mode, state })) {
162
227
  this.stateFingerprint = fingerprint;
163
228
  return true;
@@ -169,7 +234,7 @@ export class BrowserLiveHandle {
169
234
  return;
170
235
  this.assertAuthorized();
171
236
  const { page, tabId } = this.port.active();
172
- if (page === this.page) {
237
+ if (page === this.page && !(this.sessionOwned && this.segmentStartedAt !== undefined && Date.now() - this.segmentStartedAt >= 30_000)) {
173
238
  const changed = await this.publishCurrentState();
174
239
  if (changed)
175
240
  await this.refreshFrame();
@@ -186,12 +251,14 @@ export class BrowserLiveHandle {
186
251
  if (browserAuthPage(page.url()) || await pageHasSecretFocus(page))
187
252
  this.port.control.private = true;
188
253
  await this.publishCurrentState(true);
254
+ if (this.sessionOwned && !this.viewerOptions && (this.private || this.mode === 'blocked'
255
+ || this.recordingMode === 'off' || this.recordingMode !== 'local' && !this.port.fullNetwork()))
256
+ return;
189
257
  const generation = ++this.generation;
190
258
  this.page = page;
191
259
  let recordingPath;
192
- if (this.recordingMode !== 'off' && !this.private && this.mode !== 'blocked') {
193
- if (this.recordingMode !== 'local' && !this.port.fullNetwork())
194
- throw new Error('browser_recording_network_required');
260
+ if (this.recordingMode !== 'off' && !this.private && this.mode !== 'blocked'
261
+ && (this.recordingMode === 'local' || this.port.fullNetwork())) {
195
262
  const directory = this.options.recordingDirectory;
196
263
  fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
197
264
  const info = fs.lstatSync(directory);
@@ -202,15 +269,28 @@ export class BrowserLiveHandle {
202
269
  const name = `browser-${recordingId}.webm`;
203
270
  recordingPath = path.join(directory, name);
204
271
  fs.closeSync(fs.openSync(recordingPath, 'wx', 0o600));
272
+ this.segmentStartedAt = Date.now();
205
273
  this.segment = { recordingId, name, path: recordingPath, size: 0, mode: this.recordingMode };
206
274
  }
207
- await page.screencast.start({ quality: 70, size: { width: Math.min(page.viewportSize().width, 1280),
208
- height: Math.min(page.viewportSize().height, 900) }, ...(recordingPath ? { path: recordingPath } : {}),
275
+ await page.screencast.start({ quality: 70, size: page.viewportSize(),
276
+ ...(recordingPath ? { path: recordingPath, fps: this.fps } : {}),
209
277
  onFrame: frame => {
210
278
  if (this.closed || generation !== this.generation || !this.options.isAuthorized() || !this.port.authorized()
211
279
  || this.port.active().page !== page)
212
280
  return;
213
- if (Date.now() - this.lastFrameAt < 200) {
281
+ try {
282
+ if (this.segment && fs.statSync(this.segment.path).size > 250 * 1024 * 1024) {
283
+ void this.pauseRecording().catch(() => this.block());
284
+ return;
285
+ }
286
+ }
287
+ catch {
288
+ this.block();
289
+ return;
290
+ }
291
+ if (this.sessionOwned && !this.viewerOptions)
292
+ return;
293
+ if (Date.now() - this.lastFrameAt < this.frameInterval) {
214
294
  this.scheduleTrailingFrame(page, tabId, generation);
215
295
  return;
216
296
  }
@@ -218,12 +298,11 @@ export class BrowserLiveHandle {
218
298
  this.lastFrameAt = Date.now();
219
299
  try {
220
300
  this.emitFrame(frame.data, page, tabId);
221
- if (this.segment && fs.statSync(this.segment.path).size > 250 * 1024 * 1024)
222
- void this.pauseRecording();
223
301
  }
224
302
  catch { /* A destroyed page/frame is discarded, never replayed. */ }
225
303
  } });
226
- await this.refreshFrame();
304
+ if (!this.sessionOwned || this.viewerOptions)
305
+ await this.refreshFrame();
227
306
  }
228
307
  cancelTrailingFrame() {
229
308
  if (this.trailingFrame)
@@ -252,16 +331,18 @@ export class BrowserLiveHandle {
252
331
  this.lastFrameAt = Date.now();
253
332
  this.emitFrame(jpeg, page, tabId);
254
333
  })).catch(() => undefined).finally(() => { this.trailingCapturePending = false; });
255
- }, Math.max(0, 200 - (Date.now() - this.lastFrameAt)));
334
+ }, Math.max(0, this.frameInterval - (Date.now() - this.lastFrameAt)));
256
335
  this.trailingFrame.unref();
257
336
  }
258
337
  emitFrame(data, page, tabId) {
338
+ if (this.sessionOwned && !this.viewerOptions)
339
+ return;
259
340
  const observation = this.port.observations.fresh(page, tabId);
260
341
  const image = jpegDimensions(data);
261
342
  const metadata = { protocol: 1, liveSessionId: this.options.liveSessionId,
262
343
  browserSessionId: this.options.browserSessionId, tabId, epoch: this.controlEpoch, sequence: ++this.sequence,
263
344
  timestamp: Date.now(), observationId: observation.id, width: observation.width, height: observation.height,
264
- imageWidth: image.width, imageHeight: image.height, format: 'jpeg', private: this.private };
345
+ imageWidth: image.width, imageHeight: image.height, format: 'jpeg', private: this.private, ...(this.scroll ? { scroll: this.scroll } : {}) };
265
346
  const header = Buffer.from(JSON.stringify(metadata));
266
347
  if (4 + header.length + data.length > BROWSER_LIVE_MAX_FRAME_BYTES)
267
348
  return;
@@ -284,10 +365,12 @@ export class BrowserLiveHandle {
284
365
  }
285
366
  catch (error) {
286
367
  this.assertAuthorized();
287
- // A closed, replaced page is an obsolete capture, not a failure of the
288
- // still-authorized successor. All errors on the current page remain fatal.
289
- if (!this.closed && !this.stopped && page.isClosed() && this.port.active().page !== page
290
- && epoch === this.controlEpoch && mode === this.mode && privateFrame === this.private)
368
+ // A capture failure belongs to the scope that started it. Discard work
369
+ // superseded by a local privacy/control fence or by a closed replaced tab.
370
+ // Authorization is still checked first; current-page failures stay fatal.
371
+ if (this.stopped || epoch !== this.controlEpoch || mode !== this.mode
372
+ || generation !== this.generation || privateFrame !== this.private
373
+ || page.isClosed() && this.port.active().page !== page)
291
374
  return;
292
375
  throw error;
293
376
  }
@@ -302,6 +385,12 @@ export class BrowserLiveHandle {
302
385
  return;
303
386
  if (this.port.active().page !== page || this.port.active().tabId !== tabId)
304
387
  continue;
388
+ this.scroll = await page.evaluate(() => {
389
+ const root = document.scrollingElement || document.documentElement;
390
+ return { top: Math.max(0, Math.round(root.scrollTop)), height: Math.max(1, Math.round(root.scrollHeight)), viewport: innerHeight };
391
+ }).catch(() => undefined);
392
+ if (epoch !== this.controlEpoch || generation !== this.generation || this.port.active().page !== page)
393
+ return;
305
394
  this.lastFrameAt = Date.now();
306
395
  this.emitFrame(jpeg, page, tabId);
307
396
  return;
@@ -309,24 +398,31 @@ export class BrowserLiveHandle {
309
398
  throw new Error('browser_observation_stale');
310
399
  }
311
400
  async control(frame) {
401
+ let humanProfileTransition = false;
312
402
  const ack = (ok, error, state) => ({
313
403
  type: 'control.result', requestId: frame.requestId, liveSessionId: this.options.liveSessionId,
314
404
  controlEpoch: this.controlEpoch, mode: this.mode, ok, ...(error ? { error } : {}), ...(state ? { state } : {})
315
405
  });
316
406
  try {
317
407
  this.assertAuthorized();
408
+ if (this.sessionOwned && (!this.viewerOptions || !this.viewerOptions.isAuthorized()))
409
+ throw new Error('browser_live_scope_expired');
318
410
  if (!validBrowserLiveControl(frame) || frame.liveSessionId !== this.options.liveSessionId || this.requests.has(frame.requestId))
319
411
  throw new Error('browser_live_control_invalid');
412
+ for (const [id, at] of this.requests)
413
+ if (Date.now() - at > 300000)
414
+ this.requests.delete(id);
320
415
  if (this.requests.size >= 4096)
321
416
  throw new Error('browser_live_reconnect_required');
322
- this.requests.add(frame.requestId);
417
+ this.requests.set(frame.requestId, Date.now());
323
418
  const transition = ['takeover', 'release', 'profile', 'select_tab'].includes(frame.action)
324
419
  || frame.action === 'stop' && this.mode !== 'observe';
325
420
  if (transition ? frame.controlEpoch !== this.controlEpoch + 1
326
421
  : frame.action === 'start' ? frame.controlEpoch > this.controlEpoch : frame.controlEpoch !== this.controlEpoch)
327
422
  throw new Error('browser_live_epoch_stale');
328
423
  const priorMode = this.mode;
329
- if (['input', 'profile', 'select_tab'].includes(frame.action) && priorMode !== 'human'
424
+ humanProfileTransition = frame.action === 'profile' && priorMode === 'human';
425
+ if (['input', 'profile', 'select_tab', 'settings', 'navigate'].includes(frame.action) && priorMode !== 'human'
330
426
  || frame.action === 'recording' && priorMode === 'blocked' && frame.recording?.mode !== 'off')
331
427
  throw new Error('browser_live_human_owner_required');
332
428
  // Fence every queued model operation immediately, before awaiting the shared session queue.
@@ -374,6 +470,25 @@ export class BrowserLiveHandle {
374
470
  await this.port.selectTab(frame.tabId);
375
471
  this.port.control.mode = 'human';
376
472
  }
473
+ else if (frame.action === 'settings') {
474
+ await this.stopCapture();
475
+ this.fps = frame.settings.fps;
476
+ await this.port.active().page.setViewportSize({ width: frame.settings.width, height: frame.settings.height });
477
+ this.port.observations.invalidate();
478
+ }
479
+ else if (frame.action === 'navigate') {
480
+ await this.stopCapture();
481
+ this.port.observations.invalidate();
482
+ const page = this.port.active().page;
483
+ if (frame.navigation === 'back')
484
+ await page.goBack({ waitUntil: 'domcontentloaded', timeout: 10000 });
485
+ else if (frame.navigation === 'forward')
486
+ await page.goForward({ waitUntil: 'domcontentloaded', timeout: 10000 });
487
+ else
488
+ await page.reload({ waitUntil: 'domcontentloaded', timeout: 10000 });
489
+ if (browserAuthPage(page.url()) || await pageHasSecretFocus(page))
490
+ this.port.control.private = true;
491
+ }
377
492
  else if (frame.action === 'recording') {
378
493
  const mode = frame.recording.mode;
379
494
  if (mode !== 'off' && this.private) {
@@ -385,9 +500,15 @@ export class BrowserLiveHandle {
385
500
  if (mode !== 'off' && mode !== 'local' && !this.port.fullNetwork())
386
501
  throw new Error('browser_recording_network_required');
387
502
  await this.stopCapture();
503
+ if (this.recordingMode === 'off' && mode !== 'off')
504
+ this.elapsedMs = 0;
388
505
  this.recordingMode = mode;
389
506
  }
390
507
  else if (frame.action === 'stop') {
508
+ if (this.sessionOwned && priorMode === 'observe') {
509
+ this.port.control.connected = false;
510
+ return ack(true);
511
+ }
391
512
  this.stopped = true;
392
513
  this.port.control.connected = false;
393
514
  if (this.poll)
@@ -413,11 +534,40 @@ export class BrowserLiveHandle {
413
534
  await this.stopCapture();
414
535
  await this.sync();
415
536
  }
416
- return ack(true, undefined, state);
537
+ return ack(true, undefined, frame.action === 'release' ? await this.state() : state);
417
538
  });
418
539
  }
419
540
  catch (error) {
420
541
  const message = error instanceof Error && /^browser_[a-z_]+$/u.test(error.message) ? error.message : 'browser_live_control_failed';
542
+ // Geometry may change during native smooth scrolling. This rejected action had no
543
+ // side effects: refresh its observation without ending the user's control or replaying it.
544
+ if (frame.action === 'recording' && ['browser_recording_private', 'browser_recording_network_required'].includes(message)) {
545
+ try {
546
+ this.assertAuthorized();
547
+ return ack(false, message, await this.state());
548
+ }
549
+ catch { /* Keep ordinary authorization failures fenced. */ }
550
+ }
551
+ if (humanProfileTransition && ['browser_profile_busy', 'browser_profile_exists', 'browser_profile_name_invalid', 'browser_profile_keystore_unsupported'].includes(message)) {
552
+ try {
553
+ this.assertAuthorized();
554
+ this.port.control.mode = 'human';
555
+ this.port.control.private = true;
556
+ await this.sync();
557
+ await this.refreshFrame(this.controlEpoch);
558
+ return ack(false, message, await this.state());
559
+ }
560
+ catch {
561
+ this.port.control.mode = 'blocked';
562
+ }
563
+ }
564
+ if (['browser_observation_stale', 'browser_focus_changed'].includes(message) && frame.action === 'input' && this.mode === 'human') {
565
+ try {
566
+ await this.refreshFrame(this.controlEpoch);
567
+ return ack(false, message, await this.state());
568
+ }
569
+ catch { /* Authorization and capture errors keep the ordinary fail-closed result. */ }
570
+ }
421
571
  return ack(false, message);
422
572
  }
423
573
  }
@@ -432,12 +582,31 @@ export class BrowserLiveHandle {
432
582
  this.port.control.mode = 'blocked';
433
583
  this.port.control.private = true;
434
584
  this.port.observations.invalidate();
435
- void this.stopCapture();
585
+ void this.stopCapture().catch(() => undefined);
436
586
  for (const page of this.port.pages())
437
587
  void releasePageInput(page);
438
588
  }
439
589
  async settled() { await this.captureQueue.catch(() => undefined); }
440
590
  disconnect() {
591
+ if (this.closed)
592
+ return;
593
+ if (this.sessionOwned) {
594
+ if (this.mode === 'human')
595
+ this.block();
596
+ this.viewerOptions = undefined;
597
+ this.port.control.connected = false;
598
+ this.cancelTrailingFrame();
599
+ if (this.recordingMode === 'off') {
600
+ if (this.poll)
601
+ clearInterval(this.poll);
602
+ this.poll = undefined;
603
+ void this.stopCapture().catch(() => this.block());
604
+ }
605
+ return;
606
+ }
607
+ this.close();
608
+ }
609
+ close() {
441
610
  if (this.closed)
442
611
  return;
443
612
  if (this.mode === 'human')
@@ -448,7 +617,7 @@ export class BrowserLiveHandle {
448
617
  this.port.control.connected = false;
449
618
  if (this.poll)
450
619
  clearInterval(this.poll);
451
- void this.stopCapture();
620
+ void this.stopCapture().catch(() => undefined);
452
621
  for (const page of this.port.pages())
453
622
  void releasePageInput(page);
454
623
  }
@@ -1,8 +1,11 @@
1
1
  import type { Page } from 'playwright-core';
2
2
  import { type BrowserInteraction, type BrowserObservation } from './terminal-browser-interaction.ts';
3
+ export declare function describeFocusedField(page: Page): Promise<string | undefined>;
3
4
  /** CAPTCHA answers are model-solvable; explicit password/MFA controls remain private. */
4
5
  export declare function secretControlAttributes(attributes: Array<string | null>): boolean;
5
6
  export declare function pageHasSecretFocus(page: Page): Promise<boolean>;
6
7
  export declare function clearSecretInputs(page: Page): Promise<void>;
7
8
  export declare function releasePageInput(page: Page): Promise<void>;
8
- export declare function applyPageInteraction(page: Page, interaction: BrowserInteraction, observation: BrowserObservation, human?: boolean, onAttempt?: () => void): Promise<void>;
9
+ export declare function applyPageInteraction(page: Page, interaction: BrowserInteraction & {
10
+ focusId?: string;
11
+ }, observation: BrowserObservation, human?: boolean, onAttempt?: () => void): Promise<void>;
@@ -1,4 +1,44 @@
1
+ import { randomUUID } from 'node:crypto';
1
2
  import { validBrowserInteraction } from "./terminal-browser-interaction.js";
3
+ const focusedFields = new WeakMap();
4
+ function isFocusedField(element) {
5
+ let active = document.activeElement;
6
+ while (active?.shadowRoot?.activeElement)
7
+ active = active.shadowRoot.activeElement;
8
+ return document.hasFocus() && active === element;
9
+ }
10
+ export async function describeFocusedField(page) {
11
+ const previous = focusedFields.get(page);
12
+ if (previous && await previous.element.evaluate(isFocusedField).catch(() => false))
13
+ return previous.id;
14
+ if (previous) {
15
+ await previous.element.dispose().catch(() => undefined);
16
+ focusedFields.delete(page);
17
+ }
18
+ for (const frame of page.frames()) {
19
+ const handle = await frame.evaluateHandle(() => {
20
+ let active = document.activeElement;
21
+ while (active?.shadowRoot?.activeElement)
22
+ active = active.shadowRoot.activeElement;
23
+ return document.hasFocus() && (active instanceof HTMLInputElement || active instanceof HTMLTextAreaElement)
24
+ && !active.disabled && !active.readOnly ? active : null;
25
+ }).catch(() => undefined);
26
+ const element = handle?.asElement();
27
+ if (element) {
28
+ const id = randomUUID();
29
+ focusedFields.set(page, { id, element });
30
+ return id;
31
+ }
32
+ await handle?.dispose();
33
+ }
34
+ return undefined;
35
+ }
36
+ async function fillFocusedField(page, id, value) {
37
+ const field = focusedFields.get(page);
38
+ if (!field || field.id !== id || !await field.element.evaluate(isFocusedField).catch(() => false))
39
+ throw new Error('browser_focus_changed');
40
+ await field.element.fill(value, { timeout: 2000 });
41
+ }
2
42
  const NON_TYPING_KEYS = new Set(['Enter', 'Tab', 'Escape', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight',
3
43
  'PageUp', 'PageDown', 'Home', 'End']);
4
44
  /** CAPTCHA answers are model-solvable; explicit password/MFA controls remain private. */
@@ -51,7 +91,10 @@ export async function releasePageInput(page) {
51
91
  await page.keyboard.up(key).catch(() => undefined);
52
92
  }
53
93
  export async function applyPageInteraction(page, interaction, observation, human = false, onAttempt) {
54
- if (!validBrowserInteraction(interaction))
94
+ const { focusId, ...ordinary } = interaction;
95
+ if (focusId !== undefined && (!human || interaction.kind !== 'text'))
96
+ throw new Error('browser_private_input_invalid');
97
+ if (!validBrowserInteraction(ordinary))
55
98
  throw new Error('invalid_browser_interaction');
56
99
  for (const [x, y] of [[interaction.x, interaction.y], [interaction.toX, interaction.toY]])
57
100
  if (x !== undefined && (x >= observation.width || y >= observation.height))
@@ -84,6 +127,8 @@ export async function applyPageInteraction(page, interaction, observation, human
84
127
  await page.mouse.wheel(interaction.deltaX ?? 0, interaction.deltaY ?? 0);
85
128
  else if (interaction.kind === 'key')
86
129
  await page.keyboard.press(interaction.key === 'Space' ? ' ' : interaction.key);
130
+ else if (focusId)
131
+ await fillFocusedField(page, focusId, interaction.text);
87
132
  else
88
133
  await page.keyboard.insertText(interaction.text);
89
134
  }
@@ -0,0 +1,30 @@
1
+ import { type KeyObject } from 'node:crypto';
2
+ export declare const BROWSER_PROFILE_STATE_MAX_BYTES: number;
3
+ export type BrowserProfileKeyScope = {
4
+ instanceId: string;
5
+ browserProfileId: string;
6
+ keyVersion: string;
7
+ browserSessionId: string;
8
+ actorUserId: string;
9
+ assistantThreadId: string;
10
+ agentTerminalId: string;
11
+ agentTerminalWorkspaceId: string;
12
+ connectionId: string;
13
+ generation: number;
14
+ keyId: string;
15
+ };
16
+ export type BrowserProfileSealedKey = {
17
+ ephemeralPublicKey: string;
18
+ iv: string;
19
+ ciphertext: string;
20
+ tag: string;
21
+ };
22
+ export type BrowserProfileStateScope = Pick<BrowserProfileKeyScope, 'instanceId' | 'browserProfileId' | 'keyVersion'> & {
23
+ revision: number;
24
+ };
25
+ /** The deployment Vault key never leaves the server; only a per-profile key is sealed to this connection. */
26
+ export declare function sealBrowserProfileKey(dataKey: Buffer, recipientPublicKey: string, scope: BrowserProfileKeyScope): BrowserProfileSealedKey;
27
+ export declare function openBrowserProfileKey(envelope: BrowserProfileSealedKey, privateKey: KeyObject, scope: BrowserProfileKeyScope): Buffer;
28
+ /** Binary opaque object: 12-byte nonce, 16-byte tag, authenticated ciphertext. No browser values in metadata. */
29
+ export declare function encryptBrowserProfileState(plaintext: Buffer, dataKey: Buffer, scope: BrowserProfileStateScope): Buffer;
30
+ export declare function decryptBrowserProfileState(ciphertext: Buffer, dataKey: Buffer, scope: BrowserProfileStateScope): Buffer;
@@ -0,0 +1,101 @@
1
+ import { createCipheriv, createDecipheriv, createHash, createPublicKey, diffieHellman, generateKeyPairSync, hkdfSync, randomBytes } from 'node:crypto';
2
+ export const BROWSER_PROFILE_STATE_MAX_BYTES = 8 * 1024 * 1024;
3
+ function decode(value, size) {
4
+ if (typeof value !== 'string' || value.length > 256)
5
+ throw new Error('browser_profile_cipher_invalid');
6
+ const result = Buffer.from(value, 'base64');
7
+ if (result.toString('base64') !== value || size !== undefined && result.length !== size)
8
+ throw new Error('browser_profile_cipher_invalid');
9
+ return result;
10
+ }
11
+ function keyAad(scope) {
12
+ return Buffer.from(JSON.stringify(['revo-browser-profile-key-v1', scope.instanceId, scope.browserProfileId, scope.keyVersion,
13
+ scope.browserSessionId, scope.actorUserId, scope.assistantThreadId, scope.agentTerminalId, scope.agentTerminalWorkspaceId,
14
+ scope.connectionId, scope.generation, scope.keyId]));
15
+ }
16
+ function stateAad(scope) {
17
+ if (!Number.isSafeInteger(scope.revision) || scope.revision < 1)
18
+ throw new Error('browser_profile_cipher_invalid');
19
+ return Buffer.from(JSON.stringify(['revo-browser-profile-state-v1', scope.instanceId, scope.browserProfileId, scope.keyVersion, scope.revision]));
20
+ }
21
+ /** The deployment Vault key never leaves the server; only a per-profile key is sealed to this connection. */
22
+ export function sealBrowserProfileKey(dataKey, recipientPublicKey, scope) {
23
+ if (dataKey.length !== 32)
24
+ throw new Error('browser_profile_cipher_invalid');
25
+ let shared;
26
+ let transportKey;
27
+ try {
28
+ const der = decode(recipientPublicKey);
29
+ if (createHash('sha256').update(der).digest('hex') !== scope.keyId)
30
+ throw new Error();
31
+ const recipient = createPublicKey({ key: der, format: 'der', type: 'spki' });
32
+ if (recipient.asymmetricKeyType !== 'x25519')
33
+ throw new Error();
34
+ const ephemeral = generateKeyPairSync('x25519');
35
+ shared = diffieHellman({ privateKey: ephemeral.privateKey, publicKey: recipient });
36
+ transportKey = Buffer.from(hkdfSync('sha256', shared, keyAad(scope), Buffer.from('revo-browser-profile-transport-v1'), 32));
37
+ const iv = randomBytes(12);
38
+ const cipher = createCipheriv('aes-256-gcm', transportKey, iv);
39
+ cipher.setAAD(keyAad(scope));
40
+ const ciphertext = Buffer.concat([cipher.update(dataKey), cipher.final()]);
41
+ return { ephemeralPublicKey: ephemeral.publicKey.export({ format: 'der', type: 'spki' }).toString('base64'),
42
+ iv: iv.toString('base64'), ciphertext: ciphertext.toString('base64'), tag: cipher.getAuthTag().toString('base64') };
43
+ }
44
+ catch {
45
+ throw new Error('browser_profile_cipher_invalid');
46
+ }
47
+ finally {
48
+ shared?.fill(0);
49
+ transportKey?.fill(0);
50
+ }
51
+ }
52
+ export function openBrowserProfileKey(envelope, privateKey, scope) {
53
+ let shared;
54
+ let transportKey;
55
+ try {
56
+ if (privateKey.asymmetricKeyType !== 'x25519')
57
+ throw new Error();
58
+ const ownDer = createPublicKey(privateKey).export({ format: 'der', type: 'spki' });
59
+ if (createHash('sha256').update(ownDer).digest('hex') !== scope.keyId)
60
+ throw new Error();
61
+ const publicKey = createPublicKey({ key: decode(envelope.ephemeralPublicKey), format: 'der', type: 'spki' });
62
+ if (publicKey.asymmetricKeyType !== 'x25519')
63
+ throw new Error();
64
+ shared = diffieHellman({ privateKey, publicKey });
65
+ transportKey = Buffer.from(hkdfSync('sha256', shared, keyAad(scope), Buffer.from('revo-browser-profile-transport-v1'), 32));
66
+ const decipher = createDecipheriv('aes-256-gcm', transportKey, decode(envelope.iv, 12));
67
+ decipher.setAAD(keyAad(scope));
68
+ decipher.setAuthTag(decode(envelope.tag, 16));
69
+ return Buffer.concat([decipher.update(decode(envelope.ciphertext, 32)), decipher.final()]);
70
+ }
71
+ catch {
72
+ throw new Error('browser_profile_cipher_invalid');
73
+ }
74
+ finally {
75
+ shared?.fill(0);
76
+ transportKey?.fill(0);
77
+ }
78
+ }
79
+ /** Binary opaque object: 12-byte nonce, 16-byte tag, authenticated ciphertext. No browser values in metadata. */
80
+ export function encryptBrowserProfileState(plaintext, dataKey, scope) {
81
+ if (plaintext.length > BROWSER_PROFILE_STATE_MAX_BYTES || dataKey.length !== 32)
82
+ throw new Error('browser_profile_state_too_large');
83
+ const iv = randomBytes(12);
84
+ const cipher = createCipheriv('aes-256-gcm', dataKey, iv);
85
+ cipher.setAAD(stateAad(scope));
86
+ const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
87
+ return Buffer.concat([iv, cipher.getAuthTag(), ciphertext]);
88
+ }
89
+ export function decryptBrowserProfileState(ciphertext, dataKey, scope) {
90
+ if (ciphertext.length < 28 || ciphertext.length > BROWSER_PROFILE_STATE_MAX_BYTES + 28 || dataKey.length !== 32)
91
+ throw new Error('browser_profile_cipher_invalid');
92
+ try {
93
+ const decipher = createDecipheriv('aes-256-gcm', dataKey, ciphertext.subarray(0, 12));
94
+ decipher.setAuthTag(ciphertext.subarray(12, 28));
95
+ decipher.setAAD(stateAad(scope));
96
+ return Buffer.concat([decipher.update(ciphertext.subarray(28)), decipher.final()]);
97
+ }
98
+ catch {
99
+ throw new Error('browser_profile_cipher_invalid');
100
+ }
101
+ }