@testdriverai/runner 7.8.0-canary.15 → 7.8.0-test.39
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 +1 -187
- package/lib/ably-service.js +20 -164
- package/lib/automation.js +5 -10
- package/package.json +1 -1
- package/scripts-desktop/start-agent.sh +0 -105
package/README.md
CHANGED
|
@@ -1,187 +1 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
The TestDriver Runner is a desktop automation agent that connects to the TestDriver API via [Ably](https://ably.com/) realtime messaging. It receives commands from the SDK (click, type, find, screenshot, etc.) and executes them on a desktop environment using PyAutoGUI and Sharp.
|
|
4
|
-
|
|
5
|
-
## Architecture
|
|
6
|
-
|
|
7
|
-
The runner operates in two modes:
|
|
8
|
-
|
|
9
|
-
| Mode | Binary | Use Case |
|
|
10
|
-
|------|--------|----------|
|
|
11
|
-
| **Presence Runner** | `testdriver-runner` | Self-registers with the API, enters Ably presence, and waits for SDK sessions to claim it. Used for persistent/pooled runners. |
|
|
12
|
-
| **Sandbox Agent** | `testdriver-sandbox-agent` | Reads pre-provisioned credentials from a config file or environment variables. Used for ephemeral cloud sandboxes (E2B, AWS EC2). |
|
|
13
|
-
|
|
14
|
-
## Prerequisites
|
|
15
|
-
|
|
16
|
-
### System Requirements
|
|
17
|
-
|
|
18
|
-
- **Node.js** >= 18
|
|
19
|
-
- **Python 3** with `pyautogui` and `Pillow`
|
|
20
|
-
- A desktop environment (physical display, VNC, or virtual framebuffer)
|
|
21
|
-
|
|
22
|
-
### Desktop Environment (Linux)
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
# Virtual display + desktop
|
|
26
|
-
apt-get install -y xvfb xfce4 xfce4-terminal dbus-x11 wmctrl
|
|
27
|
-
|
|
28
|
-
# VNC access (optional, for debugging)
|
|
29
|
-
apt-get install -y tigervnc-standalone-server novnc websockify
|
|
30
|
-
|
|
31
|
-
# Python automation
|
|
32
|
-
pip3 install pyautogui python-xlib Pillow
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
### Desktop Environment (Windows)
|
|
36
|
-
|
|
37
|
-
- Standard Windows desktop (RDP or console session)
|
|
38
|
-
- Python 3 with `pyautogui` and `Pillow`:
|
|
39
|
-
```powershell
|
|
40
|
-
pip install pyautogui Pillow
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### Chrome
|
|
44
|
-
|
|
45
|
-
Google Chrome or Chrome for Testing must be installed and accessible on `PATH`.
|
|
46
|
-
|
|
47
|
-
## Installation
|
|
48
|
-
|
|
49
|
-
### From the TestDriver API (recommended)
|
|
50
|
-
|
|
51
|
-
```bash
|
|
52
|
-
curl -fSL -H "x-api-key: $TD_API_KEY" \
|
|
53
|
-
https://api.testdriver.ai/api/v7/runner/download \
|
|
54
|
-
-o /tmp/testdriverai-runner.tgz && \
|
|
55
|
-
npm install -g /tmp/testdriverai-runner.tgz && \
|
|
56
|
-
rm /tmp/testdriverai-runner.tgz
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
### From source (development)
|
|
60
|
-
|
|
61
|
-
```bash
|
|
62
|
-
cd runner
|
|
63
|
-
npm install
|
|
64
|
-
npm start
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
## Quick Start
|
|
68
|
-
|
|
69
|
-
### Presence Runner
|
|
70
|
-
|
|
71
|
-
```bash
|
|
72
|
-
export TD_API_KEY="your-team-api-key"
|
|
73
|
-
testdriver-runner
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
The runner will:
|
|
77
|
-
1. Register with the API at `/api/v7/runner/register`
|
|
78
|
-
2. Receive an Ably token and channel
|
|
79
|
-
3. Enter presence on the runner channel
|
|
80
|
-
4. Wait for SDK sessions to claim it
|
|
81
|
-
|
|
82
|
-
### Sandbox Agent
|
|
83
|
-
|
|
84
|
-
The sandbox agent reads credentials from a JSON config file that the API provisions (via SSM, cloud-init, etc.):
|
|
85
|
-
|
|
86
|
-
**Linux:** `/tmp/testdriver-agent.json`
|
|
87
|
-
**Windows:** `C:\Windows\Temp\testdriver-agent.json`
|
|
88
|
-
|
|
89
|
-
```json
|
|
90
|
-
{
|
|
91
|
-
"sandboxId": "sb-abc123",
|
|
92
|
-
"ably": {
|
|
93
|
-
"token": "ably-token-string",
|
|
94
|
-
"channel": "testdriver:env:team:sandbox"
|
|
95
|
-
},
|
|
96
|
-
"apiRoot": "https://api.testdriver.ai",
|
|
97
|
-
"apiKey": "team-api-key"
|
|
98
|
-
}
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
Start the agent (it will wait up to 5 minutes for the config file to appear):
|
|
102
|
-
|
|
103
|
-
```bash
|
|
104
|
-
testdriver-sandbox-agent
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
Or pass credentials via environment variables instead:
|
|
108
|
-
|
|
109
|
-
```bash
|
|
110
|
-
export SANDBOX_ID="my-sandbox"
|
|
111
|
-
export ABLY_TOKEN='{"token":"..."}'
|
|
112
|
-
export ABLY_CHANNEL="testdriver:env:team:sandbox"
|
|
113
|
-
testdriver-sandbox-agent
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## Environment Variables
|
|
117
|
-
|
|
118
|
-
### Required
|
|
119
|
-
|
|
120
|
-
| Variable | Description |
|
|
121
|
-
|----------|-------------|
|
|
122
|
-
| `TD_API_KEY` | Team API key (presence runner mode) |
|
|
123
|
-
|
|
124
|
-
### Optional
|
|
125
|
-
|
|
126
|
-
| Variable | Default | Description |
|
|
127
|
-
|----------|---------|-------------|
|
|
128
|
-
| `TD_API_ROOT` | Per `TD_ENV` | API server URL |
|
|
129
|
-
| `TD_ENV` | `stable` | Environment (`dev` / `test` / `canary` / `stable`) |
|
|
130
|
-
| `TD_RUNNER_ID` | Auto-generated UUID | Fixed runner identifier |
|
|
131
|
-
| `TD_RUNNER_SINGLE` | `false` | Exit after one session |
|
|
132
|
-
| `TD_RUNNER_OS` | Auto-detected | OS capability advertised to API |
|
|
133
|
-
| `TD_VNC_URL` | Auto-detected | Public VNC URL override |
|
|
134
|
-
| `TD_NOVNC_PORT` | Auto-detected | noVNC WebSocket proxy port |
|
|
135
|
-
| `SANDBOX_ID` | Auto-generated | Sandbox identifier (agent mode) |
|
|
136
|
-
| `ABLY_TOKEN` | From config file | Ably auth token JSON (agent mode) |
|
|
137
|
-
| `ABLY_CHANNEL` | From config file | Ably channel name (agent mode) |
|
|
138
|
-
| `CONFIG_PATH` | `/tmp/testdriver-agent.json` | Config file path override (agent mode) |
|
|
139
|
-
| `SCREEN_WIDTH` | `1366` | Virtual display width (Linux) |
|
|
140
|
-
| `SCREEN_HEIGHT` | `768` | Virtual display height (Linux) |
|
|
141
|
-
| `DISPLAY` | `:0` | X11 display (Linux) |
|
|
142
|
-
|
|
143
|
-
## Logs
|
|
144
|
-
|
|
145
|
-
| Platform | Runner Log | Agent Log |
|
|
146
|
-
|----------|-----------|-----------|
|
|
147
|
-
| Linux/macOS | `/tmp/testdriver-runner.log` | `/tmp/testdriver-agent.log` |
|
|
148
|
-
| Windows | `C:\Windows\Temp\testdriver-runner.log` | `C:\Windows\Temp\testdriver-agent.log` |
|
|
149
|
-
|
|
150
|
-
## Desktop Scripts
|
|
151
|
-
|
|
152
|
-
Helper scripts in `scripts-desktop/` for managing the Linux desktop environment:
|
|
153
|
-
|
|
154
|
-
| Script | Purpose |
|
|
155
|
-
|--------|---------|
|
|
156
|
-
| `start-desktop.sh` | Starts Xvfb, XFCE, D-Bus, disables screen blanking |
|
|
157
|
-
| `launch_chrome.sh` | Launches Chrome with standard flags |
|
|
158
|
-
| `launch_chrome_for_testing.sh` | Launches Chrome for Testing with remote debugging (port 9222) |
|
|
159
|
-
| `control_window.sh` | Window management (minimize, restore, focus) via wmctrl |
|
|
160
|
-
|
|
161
|
-
## Deployment
|
|
162
|
-
|
|
163
|
-
### AWS AMI (Packer)
|
|
164
|
-
|
|
165
|
-
See `packer/` for Packer templates that build AMIs with the runner pre-installed. The AMI includes the full desktop stack, Chrome, Python, and the runner.
|
|
166
|
-
|
|
167
|
-
### E2B Sandboxes
|
|
168
|
-
|
|
169
|
-
The E2B template installs the runner in a Dockerfile. See `sdk/setup/e2b/` for the recommended setup.
|
|
170
|
-
|
|
171
|
-
### Docker
|
|
172
|
-
|
|
173
|
-
```bash
|
|
174
|
-
TD_API_KEY=your-key docker compose up --build
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
## Updating
|
|
178
|
-
|
|
179
|
-
Re-download and reinstall:
|
|
180
|
-
|
|
181
|
-
```bash
|
|
182
|
-
curl -fSL -H "x-api-key: $TD_API_KEY" \
|
|
183
|
-
https://api.testdriver.ai/api/v7/runner/download \
|
|
184
|
-
-o /tmp/testdriverai-runner.tgz && \
|
|
185
|
-
npm install -g /tmp/testdriverai-runner.tgz && \
|
|
186
|
-
rm /tmp/testdriverai-runner.tgz
|
|
187
|
-
```
|
|
1
|
+
# runner
|
package/lib/ably-service.js
CHANGED
|
@@ -170,7 +170,6 @@ class AblyService extends EventEmitter {
|
|
|
170
170
|
callback(null, this._ablyToken);
|
|
171
171
|
},
|
|
172
172
|
clientId: this._clientId,
|
|
173
|
-
echoMessages: false, // don't receive our own published messages
|
|
174
173
|
disconnectedRetryTimeout: 5000, // retry reconnect every 5s (default 15s)
|
|
175
174
|
suspendedRetryTimeout: 15000, // retry from suspended every 15s (default 30s)
|
|
176
175
|
logHandler: (msg) => {
|
|
@@ -231,7 +230,7 @@ class AblyService extends EventEmitter {
|
|
|
231
230
|
level: 'info',
|
|
232
231
|
message,
|
|
233
232
|
timestamp: Date.now(),
|
|
234
|
-
}).catch(() => {
|
|
233
|
+
}).catch(() => {}); // best-effort
|
|
235
234
|
});
|
|
236
235
|
this._automation.on('warn', (message) => {
|
|
237
236
|
if (!this._debugMode) return;
|
|
@@ -240,7 +239,7 @@ class AblyService extends EventEmitter {
|
|
|
240
239
|
level: 'warn',
|
|
241
240
|
message,
|
|
242
241
|
timestamp: Date.now(),
|
|
243
|
-
}).catch(() => {
|
|
242
|
+
}).catch(() => {}); // best-effort
|
|
244
243
|
});
|
|
245
244
|
this._automation.on('error', (message) => {
|
|
246
245
|
if (!this._debugMode) return;
|
|
@@ -249,24 +248,21 @@ class AblyService extends EventEmitter {
|
|
|
249
248
|
level: 'error',
|
|
250
249
|
message: typeof message === 'string' ? message : message.message || String(message),
|
|
251
250
|
timestamp: Date.now(),
|
|
252
|
-
}).catch(() => {
|
|
251
|
+
}).catch(() => {}); // best-effort
|
|
253
252
|
});
|
|
254
253
|
|
|
255
|
-
// Forward exec streaming chunks to SDK
|
|
256
|
-
// Exec output can produce many chunks rapidly (e.g. verbose commands);
|
|
257
|
-
// throttle to avoid hitting Ably's 50 msg/sec per-connection limit.
|
|
258
|
-
this._execOutputLastTime = 0;
|
|
259
|
-
this._execOutputMinIntervalMs = 50; // 20 msg/sec max for exec.output
|
|
260
|
-
this._execOutputQueue = []; // queued chunks waiting to send
|
|
261
|
-
this._execOutputDraining = false;
|
|
262
|
-
|
|
254
|
+
// Forward exec streaming chunks to SDK
|
|
263
255
|
this._automation.on('exec.output', ({ requestId, chunk }) => {
|
|
264
|
-
this.
|
|
265
|
-
|
|
256
|
+
this._sendResponse({
|
|
257
|
+
type: 'exec.output',
|
|
258
|
+
requestId,
|
|
259
|
+
chunk,
|
|
260
|
+
timestamp: Date.now(),
|
|
261
|
+
}).catch(() => {}); // best-effort, don't block exec
|
|
266
262
|
});
|
|
267
263
|
|
|
268
|
-
// Subscribe to commands
|
|
269
|
-
this.
|
|
264
|
+
// Subscribe to commands
|
|
265
|
+
this._sessionChannel.subscribe('command', async (msg) => {
|
|
270
266
|
const message = msg.data;
|
|
271
267
|
if (!message) return;
|
|
272
268
|
|
|
@@ -275,9 +271,6 @@ class AblyService extends EventEmitter {
|
|
|
275
271
|
|
|
276
272
|
this.emit('log', `Command received: ${type} (requestId=${requestId})`);
|
|
277
273
|
|
|
278
|
-
// Stop re-publishing runner.ready once we get the first command
|
|
279
|
-
this._stopReadySignal();
|
|
280
|
-
|
|
281
274
|
// Per-command timeout: use message.timeout if provided, else default 120s
|
|
282
275
|
// Prevents hanging forever if screenshot capture or S3 upload stalls
|
|
283
276
|
const commandTimeout = (message.timeout && message.timeout > 0)
|
|
@@ -300,7 +293,7 @@ class AblyService extends EventEmitter {
|
|
|
300
293
|
|
|
301
294
|
// Screenshots are now handled by automation.js (returns { s3Key })
|
|
302
295
|
// No need to check type here - just pass through the result
|
|
303
|
-
|
|
296
|
+
|
|
304
297
|
await this._sendResponse({
|
|
305
298
|
requestId,
|
|
306
299
|
type: `${type}.reply`,
|
|
@@ -331,8 +324,7 @@ class AblyService extends EventEmitter {
|
|
|
331
324
|
} else {
|
|
332
325
|
await executeCommand();
|
|
333
326
|
}
|
|
334
|
-
};
|
|
335
|
-
this._commandSubscription = await this._sessionChannel.subscribe('command', this._onCommandMsg);
|
|
327
|
+
});
|
|
336
328
|
|
|
337
329
|
// ─── Ably connection state monitoring → Sentry ─────────────────────────
|
|
338
330
|
this._ably.connection.on((stateChange) => {
|
|
@@ -415,27 +407,11 @@ class AblyService extends EventEmitter {
|
|
|
415
407
|
Sentry.captureException(err);
|
|
416
408
|
});
|
|
417
409
|
}
|
|
418
|
-
|
|
419
|
-
// Detect discontinuity: channel re-attached but message continuity was lost.
|
|
420
|
-
// Use historyBeforeSubscribe() on each subscription to recover missed messages.
|
|
421
|
-
if (current === 'attached' && stateChange.resumed === false && previous === 'attached') {
|
|
422
|
-
this.emit('log', `Ably channel [session]: DISCONTINUITY (resumed=false)${reasonMsg ? ' — ' + reasonMsg : ''}`);
|
|
423
|
-
|
|
424
|
-
Sentry.withScope((scope) => {
|
|
425
|
-
scope.setTag('ably.client', 'runner');
|
|
426
|
-
scope.setTag('ably.channel', sessionCh.name);
|
|
427
|
-
scope.setTag('ably.issue', 'discontinuity');
|
|
428
|
-
scope.setFingerprint(['ably-channel-discontinuity', 'runner']);
|
|
429
|
-
Sentry.captureMessage('Ably channel discontinuity (runner)', 'warning');
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
this._recoverFromDiscontinuity();
|
|
433
|
-
}
|
|
434
410
|
});
|
|
435
411
|
}
|
|
436
412
|
|
|
437
|
-
// Subscribe to control messages
|
|
438
|
-
this.
|
|
413
|
+
// Subscribe to control messages
|
|
414
|
+
this._sessionChannel.subscribe('control', async (msg) => {
|
|
439
415
|
const message = msg.data;
|
|
440
416
|
if (!message) return;
|
|
441
417
|
|
|
@@ -454,15 +430,14 @@ class AblyService extends EventEmitter {
|
|
|
454
430
|
this._debugMode = !!message.enabled;
|
|
455
431
|
this.emit('log', `Debug mode ${this._debugMode ? 'enabled' : 'disabled'}`);
|
|
456
432
|
}
|
|
457
|
-
};
|
|
458
|
-
this._controlSubscription = await this._sessionChannel.subscribe('control', this._onControlMsg);
|
|
433
|
+
});
|
|
459
434
|
|
|
460
435
|
this.emit('log', 'Listening for commands on Ably');
|
|
461
436
|
|
|
462
437
|
// Signal readiness to SDK — commands sent before this would be lost
|
|
463
438
|
const readyPayload = {
|
|
464
439
|
type: 'runner.ready',
|
|
465
|
-
os:
|
|
440
|
+
os: 'windows',
|
|
466
441
|
sandboxId: this._sandboxId,
|
|
467
442
|
runnerVersion: getLocalVersion() || 'unknown',
|
|
468
443
|
timestamp: Date.now(),
|
|
@@ -476,123 +451,6 @@ class AblyService extends EventEmitter {
|
|
|
476
451
|
}
|
|
477
452
|
await this._sessionChannel.publish('control', readyPayload);
|
|
478
453
|
this.emit('log', 'Published runner.ready signal');
|
|
479
|
-
|
|
480
|
-
// Re-publish runner.ready every 3s for up to 60s.
|
|
481
|
-
// The SDK may connect after the first publish (race condition),
|
|
482
|
-
// and Ably channel history may not be enabled. Repeating ensures
|
|
483
|
-
// the SDK catches at least one live runner.ready message.
|
|
484
|
-
this._readyInterval = setInterval(async () => {
|
|
485
|
-
try {
|
|
486
|
-
readyPayload.timestamp = Date.now();
|
|
487
|
-
await this._sessionChannel.publish('control', readyPayload);
|
|
488
|
-
this.emit('log', 'Re-published runner.ready signal');
|
|
489
|
-
} catch (err) {
|
|
490
|
-
this.emit('log', `Failed to re-publish runner.ready: ${err.message}`);
|
|
491
|
-
}
|
|
492
|
-
}, 3000);
|
|
493
|
-
|
|
494
|
-
// Stop after 60s regardless
|
|
495
|
-
this._readyTimeout = setTimeout(() => {
|
|
496
|
-
this._stopReadySignal();
|
|
497
|
-
}, 60000);
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* Stop the repeated runner.ready signal (called on first command or after timeout).
|
|
502
|
-
*/
|
|
503
|
-
_stopReadySignal() {
|
|
504
|
-
if (this._readyInterval) {
|
|
505
|
-
clearInterval(this._readyInterval);
|
|
506
|
-
this._readyInterval = null;
|
|
507
|
-
}
|
|
508
|
-
if (this._readyTimeout) {
|
|
509
|
-
clearTimeout(this._readyTimeout);
|
|
510
|
-
this._readyTimeout = null;
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* Drain the exec.output queue, respecting the rate limit interval.
|
|
516
|
-
* Coalesces queued chunks per-requestId into single messages to reduce
|
|
517
|
-
* message count when output arrives faster than we can send.
|
|
518
|
-
*/
|
|
519
|
-
async _drainExecOutputQueue() {
|
|
520
|
-
if (this._execOutputDraining) return; // already draining
|
|
521
|
-
this._execOutputDraining = true;
|
|
522
|
-
|
|
523
|
-
try {
|
|
524
|
-
while (this._execOutputQueue.length > 0) {
|
|
525
|
-
// Rate limit: wait if needed
|
|
526
|
-
const now = Date.now();
|
|
527
|
-
const elapsed = now - this._execOutputLastTime;
|
|
528
|
-
if (elapsed < this._execOutputMinIntervalMs) {
|
|
529
|
-
await new Promise((resolve) => setTimeout(resolve, this._execOutputMinIntervalMs - elapsed));
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
// Coalesce all queued chunks for the same requestId
|
|
533
|
-
const batch = {};
|
|
534
|
-
while (this._execOutputQueue.length > 0) {
|
|
535
|
-
const { requestId, chunk } = this._execOutputQueue.shift();
|
|
536
|
-
if (!batch[requestId]) batch[requestId] = '';
|
|
537
|
-
batch[requestId] += chunk;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
this._execOutputLastTime = Date.now();
|
|
541
|
-
|
|
542
|
-
// Send one message per requestId
|
|
543
|
-
for (const [requestId, chunk] of Object.entries(batch)) {
|
|
544
|
-
this._sendResponse({
|
|
545
|
-
type: 'exec.output',
|
|
546
|
-
requestId,
|
|
547
|
-
chunk,
|
|
548
|
-
timestamp: Date.now(),
|
|
549
|
-
}).catch(() => { }); // best-effort
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
} finally {
|
|
553
|
-
this._execOutputDraining = false;
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
/**
|
|
558
|
-
* Recover missed messages after a channel discontinuity.
|
|
559
|
-
* Uses historyBeforeSubscribe() on each subscription, which guarantees
|
|
560
|
-
* no gap between historical and live messages. Each recovered message
|
|
561
|
-
* is dispatched through the same handler that processes live messages
|
|
562
|
-
* so that missed commands are actually executed.
|
|
563
|
-
*/
|
|
564
|
-
async _recoverFromDiscontinuity() {
|
|
565
|
-
const subs = [
|
|
566
|
-
{ name: 'command', sub: this._commandSubscription, handler: this._onCommandMsg },
|
|
567
|
-
{ name: 'control', sub: this._controlSubscription, handler: this._onControlMsg },
|
|
568
|
-
];
|
|
569
|
-
for (const { name, sub, handler } of subs) {
|
|
570
|
-
if (!sub) continue;
|
|
571
|
-
try {
|
|
572
|
-
this.emit('log', `Discontinuity recovery: fetching historyBeforeSubscribe for ${name}...`);
|
|
573
|
-
let page = await sub.historyBeforeSubscribe({ limit: 100 });
|
|
574
|
-
let recovered = 0;
|
|
575
|
-
while (page) {
|
|
576
|
-
for (const item of page.items) {
|
|
577
|
-
recovered++;
|
|
578
|
-
try {
|
|
579
|
-
if (handler) {
|
|
580
|
-
this.emit('log', `Replaying recovered ${name} message (requestId=${item.data && item.data.requestId || 'none'})`);
|
|
581
|
-
await handler(item);
|
|
582
|
-
}
|
|
583
|
-
} catch (replayErr) {
|
|
584
|
-
this.emit('log', `Error replaying recovered ${name} message: ${replayErr.message}`);
|
|
585
|
-
Sentry.captureException(replayErr);
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
page = page.hasNext() ? await page.next() : null;
|
|
589
|
-
}
|
|
590
|
-
this.emit('log', `Discontinuity recovery: replayed ${recovered} ${name} message(s) from gap`);
|
|
591
|
-
} catch (err) {
|
|
592
|
-
this.emit('log', `Discontinuity recovery failed for ${name}: ${err.message}`);
|
|
593
|
-
Sentry.captureException(err);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
454
|
}
|
|
597
455
|
|
|
598
456
|
/**
|
|
@@ -653,8 +511,6 @@ class AblyService extends EventEmitter {
|
|
|
653
511
|
async close() {
|
|
654
512
|
this.emit('log', 'Closing Ably service...');
|
|
655
513
|
|
|
656
|
-
this._stopReadySignal();
|
|
657
|
-
|
|
658
514
|
if (this._statsInterval) {
|
|
659
515
|
clearInterval(this._statsInterval);
|
|
660
516
|
this._statsInterval = null;
|
|
@@ -662,12 +518,12 @@ class AblyService extends EventEmitter {
|
|
|
662
518
|
|
|
663
519
|
try {
|
|
664
520
|
if (this._sessionChannel) this._sessionChannel.detach();
|
|
665
|
-
} catch {
|
|
521
|
+
} catch {}
|
|
666
522
|
|
|
667
523
|
if (this._ably) {
|
|
668
524
|
try {
|
|
669
525
|
this._ably.close();
|
|
670
|
-
} catch {
|
|
526
|
+
} catch {}
|
|
671
527
|
this._ably = null;
|
|
672
528
|
}
|
|
673
529
|
|
package/lib/automation.js
CHANGED
|
@@ -45,10 +45,8 @@ const API_KEY = process.env.TD_API_KEY;
|
|
|
45
45
|
// shell injection and escaping issues.
|
|
46
46
|
|
|
47
47
|
const PYTHON = IS_WINDOWS ? 'python' : 'python3';
|
|
48
|
-
// On Linux, ensure DISPLAY is set (use env var or fallback to :0)
|
|
49
|
-
// The os.environ.get() preserves the parent's DISPLAY setting for E2B's :1 display
|
|
50
48
|
const PY_IMPORT = IS_LINUX
|
|
51
|
-
? "import os; os.environ
|
|
49
|
+
? "import os; os.environ['DISPLAY'] = ':0'; import pyautogui, sys; pyautogui.FAILSAFE = False; "
|
|
52
50
|
: 'import pyautogui, sys; pyautogui.FAILSAFE = False; ';
|
|
53
51
|
|
|
54
52
|
/**
|
|
@@ -527,14 +525,11 @@ class Automation extends EventEmitter {
|
|
|
527
525
|
const timeout = Math.ceil((data.timeout || 300000) / 1000); // ms to seconds
|
|
528
526
|
const requestId = data.requestId;
|
|
529
527
|
|
|
530
|
-
// Buffer stdout chunks to ~
|
|
528
|
+
// Buffer stdout chunks to ~16KB before emitting over Ably.
|
|
531
529
|
// This reduces message count while keeping each message well under
|
|
532
|
-
// Ably's 64KB limit.
|
|
533
|
-
//
|
|
534
|
-
|
|
535
|
-
// commands. The SDK accumulates these chunks and reconstructs the
|
|
536
|
-
// full stdout — the final response only carries returncode + stderr.
|
|
537
|
-
const CHUNK_FLUSH_SIZE = 32 * 1024; // 32KB
|
|
530
|
+
// Ably's 64KB limit. The SDK accumulates these chunks and reconstructs
|
|
531
|
+
// the full stdout — the final response only carries returncode + stderr.
|
|
532
|
+
const CHUNK_FLUSH_SIZE = 16 * 1024; // 16KB
|
|
538
533
|
let chunkBuffer = '';
|
|
539
534
|
const flushChunkBuffer = () => {
|
|
540
535
|
if (chunkBuffer.length > 0) {
|
package/package.json
CHANGED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
# ─── TestDriver Sandbox Agent Startup ────────────────────────────────────────
|
|
3
|
-
# Starts the sandbox-agent.js (Ably-based automation agent) inside the E2B
|
|
4
|
-
# sandbox. This script is called by the API after writing the config file
|
|
5
|
-
# to /tmp/testdriver-agent.json.
|
|
6
|
-
#
|
|
7
|
-
# This matches the Windows runner pattern: the agent runs locally on the
|
|
8
|
-
# sandbox and executes commands via pyautogui (instead of @e2b/desktop RPC).
|
|
9
|
-
#
|
|
10
|
-
# Usage: bash /opt/testdriver-runner/scripts-desktop/start-agent.sh [&]
|
|
11
|
-
#
|
|
12
|
-
# Prerequisites:
|
|
13
|
-
# - Desktop environment running (start-desktop.sh completed)
|
|
14
|
-
# - Config file at /tmp/testdriver-agent.json with Ably credentials
|
|
15
|
-
# - Node.js installed
|
|
16
|
-
# - Runner installed at /opt/testdriver-runner
|
|
17
|
-
|
|
18
|
-
set -e
|
|
19
|
-
|
|
20
|
-
export DISPLAY="${DISPLAY:-:0}"
|
|
21
|
-
export XAUTHORITY="${XAUTHORITY:-${HOME}/.Xauthority}"
|
|
22
|
-
|
|
23
|
-
RUNNER_DIR="/opt/testdriver-runner"
|
|
24
|
-
CONFIG_PATH="/tmp/testdriver-agent.json"
|
|
25
|
-
LOG_FILE="/tmp/sandbox-agent.log"
|
|
26
|
-
PID_FILE="/tmp/sandbox-agent.pid"
|
|
27
|
-
|
|
28
|
-
log() {
|
|
29
|
-
echo "[$(date -Iseconds)] [start-agent] $1" | tee -a "$LOG_FILE"
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
# ─── Check if already running ─────────────────────────────────────────────────
|
|
33
|
-
if [ -f "$PID_FILE" ]; then
|
|
34
|
-
existing_pid=$(cat "$PID_FILE")
|
|
35
|
-
if kill -0 "$existing_pid" 2>/dev/null; then
|
|
36
|
-
log "Agent already running (PID: $existing_pid), exiting"
|
|
37
|
-
exit 0
|
|
38
|
-
else
|
|
39
|
-
log "Stale PID file found, removing"
|
|
40
|
-
rm -f "$PID_FILE"
|
|
41
|
-
fi
|
|
42
|
-
fi
|
|
43
|
-
|
|
44
|
-
# ─── Verify prerequisites ─────────────────────────────────────────────────────
|
|
45
|
-
if [ ! -d "$RUNNER_DIR" ]; then
|
|
46
|
-
log "ERROR: Runner not found at $RUNNER_DIR"
|
|
47
|
-
exit 1
|
|
48
|
-
fi
|
|
49
|
-
|
|
50
|
-
if [ ! -f "$RUNNER_DIR/sandbox-agent.js" ]; then
|
|
51
|
-
log "ERROR: sandbox-agent.js not found in $RUNNER_DIR"
|
|
52
|
-
exit 1
|
|
53
|
-
fi
|
|
54
|
-
|
|
55
|
-
if ! command -v node &> /dev/null; then
|
|
56
|
-
log "ERROR: Node.js not installed"
|
|
57
|
-
exit 1
|
|
58
|
-
fi
|
|
59
|
-
|
|
60
|
-
# ─── Wait for config file (with timeout) ─────────────────────────────────────
|
|
61
|
-
# The API writes the config file before calling this script, but we add a
|
|
62
|
-
# brief wait just in case there's any race condition.
|
|
63
|
-
WAIT_TIMEOUT=30
|
|
64
|
-
WAIT_INTERVAL=1
|
|
65
|
-
elapsed=0
|
|
66
|
-
|
|
67
|
-
log "Waiting for config file: $CONFIG_PATH"
|
|
68
|
-
while [ ! -f "$CONFIG_PATH" ] && [ $elapsed -lt $WAIT_TIMEOUT ]; do
|
|
69
|
-
sleep $WAIT_INTERVAL
|
|
70
|
-
elapsed=$((elapsed + WAIT_INTERVAL))
|
|
71
|
-
done
|
|
72
|
-
|
|
73
|
-
if [ ! -f "$CONFIG_PATH" ]; then
|
|
74
|
-
log "ERROR: Config file not found after ${WAIT_TIMEOUT}s: $CONFIG_PATH"
|
|
75
|
-
exit 1
|
|
76
|
-
fi
|
|
77
|
-
|
|
78
|
-
log "Config file found"
|
|
79
|
-
|
|
80
|
-
# ─── Start the agent ──────────────────────────────────────────────────────────
|
|
81
|
-
log "Starting sandbox-agent.js..."
|
|
82
|
-
log "DISPLAY=$DISPLAY, RUNNER_DIR=$RUNNER_DIR"
|
|
83
|
-
|
|
84
|
-
# Run in background, redirect output to log file
|
|
85
|
-
cd "$RUNNER_DIR"
|
|
86
|
-
nohup node sandbox-agent.js >> "$LOG_FILE" 2>&1 &
|
|
87
|
-
AGENT_PID=$!
|
|
88
|
-
|
|
89
|
-
# Write PID file for process management
|
|
90
|
-
echo "$AGENT_PID" > "$PID_FILE"
|
|
91
|
-
|
|
92
|
-
log "Agent started (PID: $AGENT_PID)"
|
|
93
|
-
log "Log file: $LOG_FILE"
|
|
94
|
-
|
|
95
|
-
# Brief pause to catch any immediate startup errors
|
|
96
|
-
sleep 2
|
|
97
|
-
|
|
98
|
-
if kill -0 "$AGENT_PID" 2>/dev/null; then
|
|
99
|
-
log "Agent running successfully"
|
|
100
|
-
exit 0
|
|
101
|
-
else
|
|
102
|
-
log "ERROR: Agent exited unexpectedly. Check $LOG_FILE for details"
|
|
103
|
-
tail -20 "$LOG_FILE" | while read line; do log " $line"; done
|
|
104
|
-
exit 1
|
|
105
|
-
fi
|