@peakflo/agent-harness 0.2.19 → 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-proxy.d.ts +1 -1
- 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 +27 -1
- 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,460 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Files in and out of a chat browser, WITHOUT the broker socket (protocol v2).
|
|
3
|
+
* Bytes move through the Daytona toolbox (the API reads and writes sandbox
|
|
4
|
+
* files as root); the broker only prepares and checks the paths.
|
|
5
|
+
*
|
|
6
|
+
* UPLOADS (a person picks a file for the page's file chooser)
|
|
7
|
+
* upload_begin controller + epoch checked by the caller; free disk checked
|
|
8
|
+
* here against the shared reserve (disk-guard.mjs); creates the root-only staging dir
|
|
9
|
+
* /var/pf/browser-transfer/<uploadId>/ (0700 root).
|
|
10
|
+
* (API) writes part-000001 … part-NNNNNN there as root.
|
|
11
|
+
* upload_commit every part must be a regular root-owned file (lstat, opened
|
|
12
|
+
* O_NOFOLLOW); root streams them, in order, into a child that
|
|
13
|
+
* runs AS THE CHAT USER and writes
|
|
14
|
+
* /var/pf/browsers/<user>/uploads/<uploadId>/<name>; staging is
|
|
15
|
+
* deleted. Root never writes inside the chat user's tree.
|
|
16
|
+
* A FAILED commit (missing/extra/duplicate part, wrong total
|
|
17
|
+
* size, symlink, hard link) deletes staging at once too.
|
|
18
|
+
*
|
|
19
|
+
* DOWNLOADS (the page downloads a file)
|
|
20
|
+
* Kept in the chat's own downloads dir: downloads/<downloadId>/<name>,
|
|
21
|
+
* written AS the chat user. No size cap; the disk guard (disk-guard.mjs)
|
|
22
|
+
* cancels a download that would eat into the free-disk reserve.
|
|
23
|
+
* download_path the chat user's tree is writable by a possibly compromised
|
|
24
|
+
* browser, so its path is NEVER handed to a root reader: the
|
|
25
|
+
* file is read AS the chat user and copied into a fresh
|
|
26
|
+
* root-only staging dir /var/pf/browser-transfer/dl-<uuid>/,
|
|
27
|
+
* and THAT path is returned. A planted symlink can only
|
|
28
|
+
* expose what the chat user could read anyway.
|
|
29
|
+
*
|
|
30
|
+
* Staging dirs older than an hour are removed (sweep).
|
|
31
|
+
*/
|
|
32
|
+
import { randomUUID } from 'node:crypto';
|
|
33
|
+
import fs from 'node:fs';
|
|
34
|
+
import path from 'node:path';
|
|
35
|
+
import { spawn } from 'node:child_process';
|
|
36
|
+
import { BrokerError } from './protocol.mjs';
|
|
37
|
+
import { runAsUser } from './os-users.mjs';
|
|
38
|
+
import { DiskGuard } from './disk-guard.mjs';
|
|
39
|
+
|
|
40
|
+
/** Wall-clock limit for writing one upload into the chat's uploads dir. */
|
|
41
|
+
const UPLOAD_WRITE_TIMEOUT_MS = 10 * 60_000;
|
|
42
|
+
|
|
43
|
+
export const TRANSFER_ROOT = '/var/pf/browser-transfer';
|
|
44
|
+
export const UPLOAD_ID = /^[a-zA-Z0-9_-]{8,64}$/;
|
|
45
|
+
export const DOWNLOAD_ID = /^[a-zA-Z0-9_-]{1,100}$/;
|
|
46
|
+
export const STAGING_MAX_AGE_MS = 60 * 60 * 1000;
|
|
47
|
+
export const MAX_UPLOAD_PARTS = 100_000;
|
|
48
|
+
|
|
49
|
+
export function partName(n) {
|
|
50
|
+
return `part-${String(n).padStart(6, '0')}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A file name that is safe as one path segment. */
|
|
54
|
+
export function safeFileName(name, fallback = 'download') {
|
|
55
|
+
const base = path
|
|
56
|
+
.basename(String(name || fallback))
|
|
57
|
+
.replace(/[\u0000-\u001f\u007f/\\]/g, '_');
|
|
58
|
+
const trimmed = base.replace(/^\.+/, '_').slice(0, 180);
|
|
59
|
+
return trimmed || fallback;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Spawn `setpriv … cat -- file` as the user; returns the child (stdout = bytes). */
|
|
63
|
+
function catAsUser(user, file) {
|
|
64
|
+
return spawn(
|
|
65
|
+
'/usr/bin/setpriv',
|
|
66
|
+
[
|
|
67
|
+
`--reuid=${user.uid}`,
|
|
68
|
+
`--regid=${user.gid}`,
|
|
69
|
+
'--clear-groups',
|
|
70
|
+
'--no-new-privs',
|
|
71
|
+
'--inh-caps=-all',
|
|
72
|
+
'--',
|
|
73
|
+
'/bin/cat',
|
|
74
|
+
'--',
|
|
75
|
+
file
|
|
76
|
+
],
|
|
77
|
+
{ stdio: ['ignore', 'pipe', 'ignore'], env: { PATH: '/usr/bin:/bin' } }
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class Transfers {
|
|
82
|
+
#uploads = new Map();
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {{ root?: string, statfs?: (p: string) => { bavail: number, bsize: number },
|
|
86
|
+
* asUser?: typeof runAsUser, now?: () => number }} options
|
|
87
|
+
* Free disk: the shared reserve, max(1 GiB, 10% of the filesystem) (disk-guard.mjs).
|
|
88
|
+
*/
|
|
89
|
+
constructor({
|
|
90
|
+
root = TRANSFER_ROOT,
|
|
91
|
+
statfs = fs.statfsSync,
|
|
92
|
+
asUser = runAsUser,
|
|
93
|
+
now = Date.now
|
|
94
|
+
} = {}) {
|
|
95
|
+
this.root = root;
|
|
96
|
+
this.disk = new DiskGuard({ path: root, statfs });
|
|
97
|
+
this.asUser = asUser;
|
|
98
|
+
this.now = now;
|
|
99
|
+
fs.mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
100
|
+
fs.chmodSync(root, 0o700);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
freeBytes() {
|
|
104
|
+
return this.disk.freeBytes();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
#requireSpace(bytes) {
|
|
108
|
+
this.disk.requireSpace(bytes);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A root-owned real directory directly under the transfer root, or throws. */
|
|
112
|
+
#ownDir(dir) {
|
|
113
|
+
const stat = fs.lstatSync(dir);
|
|
114
|
+
if (!stat.isDirectory() || stat.uid !== process.getuid()) {
|
|
115
|
+
throw new BrokerError(
|
|
116
|
+
'invalid_args',
|
|
117
|
+
"The staging folder is not the broker's."
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return stat;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/* ------------------------------------------------------------------------ */
|
|
124
|
+
/* Uploads */
|
|
125
|
+
/* ------------------------------------------------------------------------ */
|
|
126
|
+
|
|
127
|
+
beginUpload({ sessionId, uploadId, fileName, size }) {
|
|
128
|
+
if (typeof uploadId !== 'string' || !UPLOAD_ID.test(uploadId)) {
|
|
129
|
+
throw new BrokerError(
|
|
130
|
+
'invalid_args',
|
|
131
|
+
'uploadId must match ^[a-zA-Z0-9_-]{8,64}$'
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
if (!Number.isSafeInteger(size) || size < 0)
|
|
135
|
+
throw new BrokerError(
|
|
136
|
+
'invalid_args',
|
|
137
|
+
'size must be a non-negative integer'
|
|
138
|
+
);
|
|
139
|
+
if (typeof fileName !== 'string' || !fileName || fileName.length > 1024) {
|
|
140
|
+
throw new BrokerError('invalid_args', 'fileName is required');
|
|
141
|
+
}
|
|
142
|
+
const existing = this.#uploads.get(uploadId);
|
|
143
|
+
if (existing && existing.sessionId !== sessionId)
|
|
144
|
+
throw new BrokerError('invalid_args', 'uploadId is in use');
|
|
145
|
+
this.#requireSpace(size);
|
|
146
|
+
const stagingDir = path.join(this.root, uploadId);
|
|
147
|
+
try {
|
|
148
|
+
fs.mkdirSync(stagingDir, { mode: 0o700 });
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (error.code !== 'EEXIST' || !existing)
|
|
151
|
+
throw new BrokerError('invalid_args', 'uploadId is in use');
|
|
152
|
+
this.#ownDir(stagingDir);
|
|
153
|
+
}
|
|
154
|
+
fs.chmodSync(stagingDir, 0o700);
|
|
155
|
+
this.#uploads.set(uploadId, {
|
|
156
|
+
sessionId,
|
|
157
|
+
fileName: safeFileName(fileName, 'upload'),
|
|
158
|
+
size,
|
|
159
|
+
at: this.now()
|
|
160
|
+
});
|
|
161
|
+
return { stagingDir };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Open every part (regular, root-owned, no symlink). Returns fds + total bytes. */
|
|
165
|
+
#openParts(stagingDir, parts) {
|
|
166
|
+
const fds = [];
|
|
167
|
+
let total = 0;
|
|
168
|
+
try {
|
|
169
|
+
for (let n = 1; n <= parts; n++) {
|
|
170
|
+
const file = path.join(stagingDir, partName(n));
|
|
171
|
+
let fd;
|
|
172
|
+
try {
|
|
173
|
+
fd = fs.openSync(
|
|
174
|
+
file,
|
|
175
|
+
fs.constants.O_RDONLY |
|
|
176
|
+
fs.constants.O_NOFOLLOW |
|
|
177
|
+
fs.constants.O_NONBLOCK
|
|
178
|
+
);
|
|
179
|
+
} catch (error) {
|
|
180
|
+
if (error.code === 'ELOOP') {
|
|
181
|
+
throw new BrokerError(
|
|
182
|
+
'invalid_args',
|
|
183
|
+
`Upload part ${n} is a symlink.`
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
throw new BrokerError('invalid_args', `Upload part ${n} is missing.`);
|
|
187
|
+
}
|
|
188
|
+
fds.push(fd);
|
|
189
|
+
const stat = fs.fstatSync(fd);
|
|
190
|
+
if (
|
|
191
|
+
!stat.isFile() ||
|
|
192
|
+
stat.uid !== process.getuid() ||
|
|
193
|
+
stat.nlink !== 1
|
|
194
|
+
) {
|
|
195
|
+
throw new BrokerError(
|
|
196
|
+
'invalid_args',
|
|
197
|
+
`Upload part ${n} is not a plain file written for this upload.`
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
total += stat.size;
|
|
201
|
+
}
|
|
202
|
+
} catch (error) {
|
|
203
|
+
for (const fd of fds) fs.closeSync(fd);
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
return { fds, total };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The staging dir holds exactly part-000001 … part-<parts> and nothing
|
|
211
|
+
* else: a stray, extra or duplicate entry means the parts are not what the
|
|
212
|
+
* API wrote for this upload.
|
|
213
|
+
*/
|
|
214
|
+
#checkEntries(stagingDir, parts) {
|
|
215
|
+
const names = fs.readdirSync(stagingDir);
|
|
216
|
+
const expected = new Set(
|
|
217
|
+
Array.from({ length: parts }, (_, i) => partName(i + 1))
|
|
218
|
+
);
|
|
219
|
+
for (const name of names) {
|
|
220
|
+
if (!expected.has(name)) {
|
|
221
|
+
throw new BrokerError(
|
|
222
|
+
'invalid_args',
|
|
223
|
+
`The upload has an unexpected or duplicate part (${safeFileName(name, 'part')}).`
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Delete an upload's staging dir and forget the upload. Runs as the broker
|
|
231
|
+
* (root) and never follows a symlink: the top entry is lstat'ed, a
|
|
232
|
+
* non-directory (a planted link) is unlinked, not followed, and fs.rmSync
|
|
233
|
+
* removes symlinks inside the dir as links.
|
|
234
|
+
*/
|
|
235
|
+
#discardUpload(uploadId) {
|
|
236
|
+
this.#uploads.delete(uploadId);
|
|
237
|
+
const stagingDir = path.join(this.root, uploadId);
|
|
238
|
+
try {
|
|
239
|
+
const stat = fs.lstatSync(stagingDir);
|
|
240
|
+
if (stat.isDirectory())
|
|
241
|
+
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
242
|
+
else fs.unlinkSync(stagingDir);
|
|
243
|
+
} catch {
|
|
244
|
+
/* already gone */
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Assemble the parts AS the chat user into uploads/<uploadId>/<name>.
|
|
250
|
+
* Returns the destination path (inside the chat user's tree) for setFiles.
|
|
251
|
+
*
|
|
252
|
+
* ANY failure once the upload is identified (missing, extra or duplicate
|
|
253
|
+
* part, wrong total size, symlink, hard link, the as-user write) deletes the
|
|
254
|
+
* staging dir at once and forgets the upload: a failed upload is begun
|
|
255
|
+
* again, it never lingers until the hourly sweep.
|
|
256
|
+
*/
|
|
257
|
+
async commitUpload({ sessionId, uploadId, parts, user }) {
|
|
258
|
+
if (typeof uploadId !== 'string' || !UPLOAD_ID.test(uploadId))
|
|
259
|
+
throw new BrokerError('invalid_args', 'uploadId is invalid');
|
|
260
|
+
const upload = this.#uploads.get(uploadId);
|
|
261
|
+
if (!upload || upload.sessionId !== sessionId)
|
|
262
|
+
throw new BrokerError('not_found', 'No such upload was begun.');
|
|
263
|
+
let result;
|
|
264
|
+
try {
|
|
265
|
+
result = await this.#assemble(upload, uploadId, parts, user);
|
|
266
|
+
} finally {
|
|
267
|
+
this.#discardUpload(uploadId);
|
|
268
|
+
}
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async #assemble(upload, uploadId, parts, user) {
|
|
273
|
+
if (!Number.isSafeInteger(parts) || parts < 1 || parts > MAX_UPLOAD_PARTS) {
|
|
274
|
+
throw new BrokerError('invalid_args', 'parts must be a positive integer');
|
|
275
|
+
}
|
|
276
|
+
const stagingDir = path.join(this.root, uploadId);
|
|
277
|
+
this.#ownDir(stagingDir);
|
|
278
|
+
this.#checkEntries(stagingDir, parts);
|
|
279
|
+
const { fds, total } = this.#openParts(stagingDir, parts);
|
|
280
|
+
let dest;
|
|
281
|
+
try {
|
|
282
|
+
// The assembled file must be exactly what upload_begin declared: the API
|
|
283
|
+
// bounds each part by the declared size, and this closes the sum.
|
|
284
|
+
if (total !== upload.size) {
|
|
285
|
+
throw new BrokerError(
|
|
286
|
+
'invalid_args',
|
|
287
|
+
`The upload has ${total} bytes but ${upload.size} were declared.`
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
this.#requireSpace(total);
|
|
291
|
+
const dir = path.join(user.uploads, uploadId);
|
|
292
|
+
dest = path.join(dir, upload.fileName);
|
|
293
|
+
const stream = (async function* () {
|
|
294
|
+
for (const fd of fds) {
|
|
295
|
+
const input = fs.createReadStream(null, { fd, autoClose: false });
|
|
296
|
+
for await (const chunk of input) yield chunk;
|
|
297
|
+
}
|
|
298
|
+
})();
|
|
299
|
+
const { Readable } = await import('node:stream');
|
|
300
|
+
await this.asUser(
|
|
301
|
+
user,
|
|
302
|
+
[
|
|
303
|
+
'/bin/sh',
|
|
304
|
+
'-c',
|
|
305
|
+
'umask 077 && mkdir -p -- "$1" && cat > "$1/$2"',
|
|
306
|
+
'sh',
|
|
307
|
+
dir,
|
|
308
|
+
upload.fileName
|
|
309
|
+
],
|
|
310
|
+
// Bounded like every as-user child, with room for a large file.
|
|
311
|
+
{ stdin: Readable.from(stream), timeoutMs: UPLOAD_WRITE_TIMEOUT_MS }
|
|
312
|
+
);
|
|
313
|
+
} finally {
|
|
314
|
+
for (const fd of fds) {
|
|
315
|
+
try {
|
|
316
|
+
fs.closeSync(fd);
|
|
317
|
+
} catch {
|
|
318
|
+
/* closed */
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return { path: dest, name: upload.fileName, size: total };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/* ------------------------------------------------------------------------ */
|
|
326
|
+
/* Downloads */
|
|
327
|
+
/* ------------------------------------------------------------------------ */
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Keep a finished Playwright download in downloads/<id>/<name>, as the user.
|
|
331
|
+
* MOVED, not copied: the source is Chromium's file in the same downloads
|
|
332
|
+
* dir, and a copy would need the download's size free a second time.
|
|
333
|
+
*/
|
|
334
|
+
async saveDownload(user, sourcePath, suggestedName) {
|
|
335
|
+
const downloadId = `d-${randomUUID()}`;
|
|
336
|
+
const name = safeFileName(suggestedName);
|
|
337
|
+
const out = await this.asUser(user, [
|
|
338
|
+
'/bin/sh',
|
|
339
|
+
'-c',
|
|
340
|
+
'umask 077 && mkdir -p -- "$1/$2" && mv -- "$3" "$1/$2/$4" && chmod 0600 -- "$1/$2/$4" && stat -c %s -- "$1/$2/$4"',
|
|
341
|
+
'sh',
|
|
342
|
+
user.downloads,
|
|
343
|
+
downloadId,
|
|
344
|
+
sourcePath,
|
|
345
|
+
name
|
|
346
|
+
]);
|
|
347
|
+
return { downloadId, name, size: Number(String(out).trim()) || 0 };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** `[{ downloadId, name, size, createdAt }]`, newest first, read as the user. */
|
|
351
|
+
async listDownloads(user) {
|
|
352
|
+
let out;
|
|
353
|
+
try {
|
|
354
|
+
out = await this.asUser(user, [
|
|
355
|
+
'/usr/bin/find',
|
|
356
|
+
user.downloads,
|
|
357
|
+
'-mindepth',
|
|
358
|
+
'2',
|
|
359
|
+
'-maxdepth',
|
|
360
|
+
'2',
|
|
361
|
+
'-type',
|
|
362
|
+
'f',
|
|
363
|
+
'-printf',
|
|
364
|
+
'%h\\t%f\\t%s\\t%T@\\n'
|
|
365
|
+
]);
|
|
366
|
+
} catch {
|
|
367
|
+
return [];
|
|
368
|
+
}
|
|
369
|
+
const rows = [];
|
|
370
|
+
for (const line of String(out).split('\n')) {
|
|
371
|
+
const [dir, name, size, mtime] = line.split('\t');
|
|
372
|
+
if (!dir || !name) continue;
|
|
373
|
+
const downloadId = path.basename(dir);
|
|
374
|
+
if (path.dirname(dir) !== user.downloads || !DOWNLOAD_ID.test(downloadId))
|
|
375
|
+
continue;
|
|
376
|
+
rows.push({
|
|
377
|
+
downloadId,
|
|
378
|
+
name,
|
|
379
|
+
size: Number(size) || 0,
|
|
380
|
+
createdAt: new Date(Math.round(Number(mtime) * 1000) || 0).toISOString()
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
return rows.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Copy one download (read AS the chat user) into a fresh root-only staging
|
|
388
|
+
* dir and return that `BrowserSandboxFile`.
|
|
389
|
+
*/
|
|
390
|
+
async stageDownload(user, downloadId) {
|
|
391
|
+
if (typeof downloadId !== 'string' || !DOWNLOAD_ID.test(downloadId)) {
|
|
392
|
+
throw new BrokerError('invalid_args', 'downloadId is invalid');
|
|
393
|
+
}
|
|
394
|
+
const found = (await this.listDownloads(user)).find(
|
|
395
|
+
(d) => d.downloadId === downloadId
|
|
396
|
+
);
|
|
397
|
+
if (!found) throw new BrokerError('not_found', 'Download not found');
|
|
398
|
+
this.#requireSpace(found.size);
|
|
399
|
+
const stagingDir = path.join(this.root, `dl-${randomUUID()}`);
|
|
400
|
+
fs.mkdirSync(stagingDir, { mode: 0o700 });
|
|
401
|
+
const name = safeFileName(found.name);
|
|
402
|
+
const dest = path.join(stagingDir, name);
|
|
403
|
+
const fd = fs.openSync(
|
|
404
|
+
dest,
|
|
405
|
+
fs.constants.O_WRONLY |
|
|
406
|
+
fs.constants.O_CREAT |
|
|
407
|
+
fs.constants.O_EXCL |
|
|
408
|
+
fs.constants.O_NOFOLLOW,
|
|
409
|
+
0o600
|
|
410
|
+
);
|
|
411
|
+
const output = fs.createWriteStream(null, { fd });
|
|
412
|
+
const child = catAsUser(
|
|
413
|
+
user,
|
|
414
|
+
path.join(user.downloads, downloadId, found.name)
|
|
415
|
+
);
|
|
416
|
+
const exited = new Promise((resolve) => child.on('close', resolve));
|
|
417
|
+
await new Promise((resolve, reject) => {
|
|
418
|
+
child.stdout.pipe(output);
|
|
419
|
+
output.on('finish', resolve);
|
|
420
|
+
output.on('error', reject);
|
|
421
|
+
child.on('error', reject);
|
|
422
|
+
});
|
|
423
|
+
const code = await exited;
|
|
424
|
+
if (code !== 0) {
|
|
425
|
+
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
426
|
+
throw new BrokerError('not_found', 'The download could not be read.');
|
|
427
|
+
}
|
|
428
|
+
return { name, path: dest, size: fs.statSync(dest).size };
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/* ------------------------------------------------------------------------ */
|
|
432
|
+
|
|
433
|
+
/** Remove staging dirs older than STAGING_MAX_AGE_MS. */
|
|
434
|
+
sweep() {
|
|
435
|
+
let names = [];
|
|
436
|
+
try {
|
|
437
|
+
names = fs.readdirSync(this.root);
|
|
438
|
+
} catch {
|
|
439
|
+
return 0;
|
|
440
|
+
}
|
|
441
|
+
let removed = 0;
|
|
442
|
+
const cutoff = this.now() - STAGING_MAX_AGE_MS;
|
|
443
|
+
for (const name of names) {
|
|
444
|
+
const full = path.join(this.root, name);
|
|
445
|
+
try {
|
|
446
|
+
const stat = fs.lstatSync(full);
|
|
447
|
+
if (stat.mtimeMs < cutoff) {
|
|
448
|
+
fs.rmSync(full, { recursive: true, force: true });
|
|
449
|
+
this.#uploads.delete(name);
|
|
450
|
+
removed++;
|
|
451
|
+
}
|
|
452
|
+
} catch {
|
|
453
|
+
/* gone */
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
for (const [id, upload] of this.#uploads)
|
|
457
|
+
if (upload.at < cutoff) this.#uploads.delete(id);
|
|
458
|
+
return removed;
|
|
459
|
+
}
|
|
460
|
+
}
|
package/dist/control-plane.d.ts
CHANGED
|
@@ -184,6 +184,12 @@ export declare class ControlPlaneClient {
|
|
|
184
184
|
*/
|
|
185
185
|
deregister(reason?: string, hadActiveSessions?: boolean): Promise<DeregisterResponse>;
|
|
186
186
|
private requireCredential;
|
|
187
|
+
browserGrant(sessionId: string): Promise<{
|
|
188
|
+
token: string;
|
|
189
|
+
routeId: string;
|
|
190
|
+
machine: string;
|
|
191
|
+
controlPlaneUrl: string;
|
|
192
|
+
}>;
|
|
187
193
|
private requireRunnerId;
|
|
188
194
|
/**
|
|
189
195
|
* Exponential back-off, capped by attempt count rather than by wall clock so
|
package/dist/control-plane.js
CHANGED
|
@@ -416,6 +416,16 @@ class ControlPlaneClient {
|
|
|
416
416
|
}
|
|
417
417
|
return this.runnerCredential;
|
|
418
418
|
}
|
|
419
|
+
async browserGrant(sessionId) {
|
|
420
|
+
const result = await this.send({
|
|
421
|
+
method: 'POST',
|
|
422
|
+
path: `/api/runners/${this.requireRunnerId()}/browser/${sessionId}/grant`,
|
|
423
|
+
token: this.requireCredential(),
|
|
424
|
+
body: {},
|
|
425
|
+
timeoutMs: 10_000
|
|
426
|
+
});
|
|
427
|
+
return { ...result, controlPlaneUrl: this.baseUrl };
|
|
428
|
+
}
|
|
419
429
|
requireRunnerId() {
|
|
420
430
|
if (!this.runnerId) {
|
|
421
431
|
throw new ControlPlaneError('Not registered', 401, {
|
package/dist/github-proxy.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface GitHubProxyOptions {
|
|
|
13
13
|
certificates: GitHubProxyCertificates;
|
|
14
14
|
findSession(sentinel: string): GitHubProxySession | undefined;
|
|
15
15
|
isDirectHostAllowed?(hostname: string): boolean;
|
|
16
|
-
/**
|
|
16
|
+
/** Tunnel non-GitHub HTTPS; the host network still enforces egress rules. */
|
|
17
17
|
allowDirectHttps?: boolean;
|
|
18
18
|
logger?: Pick<Console, 'warn'>;
|
|
19
19
|
connectDirect?: (hostname: string, port: number) => Socket;
|
|
@@ -35,6 +35,8 @@ export declare class GitHubWorkspaceProvider implements WorkspaceProvider {
|
|
|
35
35
|
private readonly preparationNotes;
|
|
36
36
|
private readonly preparedWorktrees;
|
|
37
37
|
private readonly directHosts;
|
|
38
|
+
/** The live loopback service port, including an ephemeral fallback. */
|
|
39
|
+
getLoopbackPort(): number | null;
|
|
38
40
|
constructor(options: GitHubWorkspaceOptions);
|
|
39
41
|
private rememberDirectUrls;
|
|
40
42
|
private runGit;
|
package/dist/github-workspace.js
CHANGED
|
@@ -74,6 +74,10 @@ class GitHubWorkspaceProvider {
|
|
|
74
74
|
preparationNotes = new Map();
|
|
75
75
|
preparedWorktrees = new Map();
|
|
76
76
|
directHosts = new Set();
|
|
77
|
+
/** The live loopback service port, including an ephemeral fallback. */
|
|
78
|
+
getLoopbackPort() {
|
|
79
|
+
return this.port;
|
|
80
|
+
}
|
|
77
81
|
constructor(options) {
|
|
78
82
|
this.options = options;
|
|
79
83
|
// Capture PATH before harness.start prepends the agent-facing guard bin.
|
package/dist/harness.d.ts
CHANGED
package/dist/harness.js
CHANGED
|
@@ -21,6 +21,7 @@ const browser_takeover_1 = require("./runtime/browser-takeover");
|
|
|
21
21
|
const cold_start_batch_1 = require("./runtime/cold-start-batch");
|
|
22
22
|
const knowledge_tools_1 = require("./runtime/knowledge-tools");
|
|
23
23
|
const lease_renewal_1 = require("./runtime/lease-renewal");
|
|
24
|
+
const local_browser_1 = require("./runtime/local-browser");
|
|
24
25
|
const mcp_auth_proxy_1 = require("./runtime/mcp-auth-proxy");
|
|
25
26
|
const session_runner_1 = require("./runtime/session-runner");
|
|
26
27
|
const conversation_store_1 = require("./store/conversation-store");
|
|
@@ -210,6 +211,7 @@ class Harness {
|
|
|
210
211
|
tenantId = null;
|
|
211
212
|
runnerId = null;
|
|
212
213
|
capabilities = null;
|
|
214
|
+
localBrowsers = null;
|
|
213
215
|
/**
|
|
214
216
|
* The cached model catalogue, probed at start-up and on a LONG interval.
|
|
215
217
|
*
|
|
@@ -329,7 +331,9 @@ class Harness {
|
|
|
329
331
|
root: options.workspaceRoot,
|
|
330
332
|
port: options.githubProxyPort,
|
|
331
333
|
directUrls: [options.controlPlaneUrl],
|
|
332
|
-
|
|
334
|
+
// Package managers inherit HTTPS_PROXY too. Tunnel non-GitHub HTTPS
|
|
335
|
+
// in every runtime; Daytona owns any sandbox egress restrictions.
|
|
336
|
+
allowDirectHttps: true
|
|
333
337
|
});
|
|
334
338
|
this.secrets = deps.secrets ?? new ports_default_1.NoSecretProvider();
|
|
335
339
|
this.events = deps.events ?? new ports_default_1.NullEventSink();
|
|
@@ -483,6 +487,22 @@ class Harness {
|
|
|
483
487
|
await this.stop();
|
|
484
488
|
throw error;
|
|
485
489
|
}
|
|
490
|
+
if (this.options.runtime !== 'sandbox') {
|
|
491
|
+
const executablePath = (0, local_browser_1.localChromium)();
|
|
492
|
+
if (executablePath)
|
|
493
|
+
this.localBrowsers = new local_browser_1.LocalBrowsers({
|
|
494
|
+
executablePath,
|
|
495
|
+
root: this.options.workspaceRoot,
|
|
496
|
+
deniedPorts: () => [
|
|
497
|
+
this.health.boundPort,
|
|
498
|
+
this.proxy.getPort(),
|
|
499
|
+
this.knowledgeHost.getLoopbackPort(),
|
|
500
|
+
this.workspace.getLoopbackPort?.() ?? null
|
|
501
|
+
],
|
|
502
|
+
grant: (sessionId) => this.client.browserGrant(sessionId),
|
|
503
|
+
log
|
|
504
|
+
});
|
|
505
|
+
}
|
|
486
506
|
this.status = 'ready';
|
|
487
507
|
log('Harness ready', { tenantId: this.tenantId, runnerId: this.runnerId });
|
|
488
508
|
this.scheduleHeartbeat();
|
|
@@ -518,6 +538,7 @@ class Harness {
|
|
|
518
538
|
}
|
|
519
539
|
// Abort in-flight turns BEFORE deregistering, so the control plane's
|
|
520
540
|
// requeue count is accurate rather than racing the turns it is counting.
|
|
541
|
+
await this.localBrowsers?.close();
|
|
521
542
|
const hadActive = this.active.size > 0;
|
|
522
543
|
for (const controller of this.active.values()) {
|
|
523
544
|
controller.abort();
|
|
@@ -1004,6 +1025,9 @@ class Harness {
|
|
|
1004
1025
|
* the same reason — one source, so the two lists cannot disagree.
|
|
1005
1026
|
*/
|
|
1006
1027
|
applyHarnessReport(capabilities) {
|
|
1028
|
+
capabilities.features = (capabilities.features ?? []).filter((f) => f !== local_browser_1.LOCAL_BROWSER_FEATURE);
|
|
1029
|
+
if (this.options.runtime !== 'sandbox' && (0, local_browser_1.localChromium)())
|
|
1030
|
+
capabilities.features.push(local_browser_1.LOCAL_BROWSER_FEATURE);
|
|
1007
1031
|
if (!this.harnessReport) {
|
|
1008
1032
|
return;
|
|
1009
1033
|
}
|
|
@@ -1636,6 +1660,8 @@ class Harness {
|
|
|
1636
1660
|
await sleep(LEASE_ERROR_BACKOFF_MS);
|
|
1637
1661
|
continue;
|
|
1638
1662
|
}
|
|
1663
|
+
// Preview setup must not delay an agent turn or its lease renewal.
|
|
1664
|
+
void this.localBrowsers?.ensure(lease.session.sessionId);
|
|
1639
1665
|
void this.runLeasedTurn(lease, coldStart);
|
|
1640
1666
|
}
|
|
1641
1667
|
await this.stop();
|
|
@@ -38,6 +38,7 @@ export declare class KnowledgeToolHost {
|
|
|
38
38
|
private readonly turns;
|
|
39
39
|
/** Chat browsers run as their own uids; they get nothing from this host. */
|
|
40
40
|
private readonly peerGuard;
|
|
41
|
+
getLoopbackPort(): number | null;
|
|
41
42
|
constructor(options: KnowledgeToolHostOptions);
|
|
42
43
|
/** The endpoint a session's agent is configured with. Starts the listener on first use. */
|
|
43
44
|
endpointFor(sessionId: string): Promise<string>;
|
|
@@ -170,6 +170,9 @@ class KnowledgeToolHost {
|
|
|
170
170
|
turns = new Map();
|
|
171
171
|
/** Chat browsers run as their own uids; they get nothing from this host. */
|
|
172
172
|
peerGuard;
|
|
173
|
+
getLoopbackPort() {
|
|
174
|
+
return this.port || null;
|
|
175
|
+
}
|
|
173
176
|
constructor(options) {
|
|
174
177
|
this.options = options;
|
|
175
178
|
this.peerGuard = new loopback_peer_1.BrowserPeerGuard(options.peerUid);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const LOCAL_BROWSER_FEATURE = "localBrowserPreview";
|
|
2
|
+
export declare function localChromium(): string | null;
|
|
3
|
+
type Broker = {
|
|
4
|
+
updateToken(token: string): void;
|
|
5
|
+
close(): Promise<void>;
|
|
6
|
+
};
|
|
7
|
+
interface Grant {
|
|
8
|
+
token: string;
|
|
9
|
+
routeId: string;
|
|
10
|
+
machine: string;
|
|
11
|
+
controlPlaneUrl: string;
|
|
12
|
+
}
|
|
13
|
+
declare const nativeImport: (url: string) => Promise<{
|
|
14
|
+
startLocalBrowser(options: object): Promise<Broker>;
|
|
15
|
+
}>;
|
|
16
|
+
export declare class LocalBrowsers {
|
|
17
|
+
private readonly options;
|
|
18
|
+
private readonly sessions;
|
|
19
|
+
private readonly opening;
|
|
20
|
+
private stopped;
|
|
21
|
+
constructor(options: {
|
|
22
|
+
root: string;
|
|
23
|
+
grant: (sessionId: string) => Promise<Grant>;
|
|
24
|
+
executablePath: string;
|
|
25
|
+
deniedPorts?: () => Array<number | null>;
|
|
26
|
+
log: (message: string) => void;
|
|
27
|
+
load?: () => ReturnType<typeof nativeImport>;
|
|
28
|
+
});
|
|
29
|
+
ensure(sessionId: string): Promise<void>;
|
|
30
|
+
private refresh;
|
|
31
|
+
close(): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export {};
|