turno-browser 0.3.0 → 0.4.0
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/CHANGELOG.md +98 -0
- package/README.md +32 -3
- package/dist/act-model.d.ts +85 -0
- package/dist/act-model.js +116 -0
- package/dist/act.d.ts +37 -0
- package/dist/act.js +599 -0
- package/dist/api.d.ts +29 -2
- package/dist/api.js +29 -2
- package/dist/argv.d.ts +16 -2
- package/dist/argv.js +100 -3
- package/dist/batch.d.ts +5 -0
- package/dist/batch.js +3 -3
- package/dist/bin/mcp.js +58 -1
- package/dist/captcha.d.ts +77 -0
- package/dist/captcha.js +333 -0
- package/dist/cdp-relay.d.ts +20 -0
- package/dist/cdp-relay.js +101 -0
- package/dist/chrome-features.d.ts +23 -0
- package/dist/chrome-features.js +44 -0
- package/dist/diagnose.d.ts +7 -0
- package/dist/diagnose.js +61 -6
- package/dist/doctor.d.ts +37 -3
- package/dist/doctor.js +230 -5
- package/dist/engine-attach.d.ts +27 -0
- package/dist/engine-attach.js +165 -0
- package/dist/engine-camoufox.d.ts +32 -0
- package/dist/engine-camoufox.js +270 -0
- package/dist/engine-lightpanda.d.ts +72 -0
- package/dist/engine-lightpanda.js +292 -0
- package/dist/engine.d.ts +63 -11
- package/dist/engine.js +104 -28
- package/dist/flows.d.ts +127 -0
- package/dist/flows.js +294 -0
- package/dist/governor.d.ts +42 -7
- package/dist/governor.js +82 -32
- package/dist/host.d.ts +17 -2
- package/dist/host.js +618 -41
- package/dist/human-input.d.ts +84 -0
- package/dist/human-input.js +443 -0
- package/dist/humanize.d.ts +123 -0
- package/dist/humanize.js +241 -0
- package/dist/identities.js +2 -0
- package/dist/idle-timer.d.ts +12 -0
- package/dist/idle-timer.js +25 -0
- package/dist/index.d.ts +8 -3
- package/dist/index.js +5 -1
- package/dist/lifecycle.d.ts +9 -1
- package/dist/lifecycle.js +9 -1
- package/dist/live-client.d.ts +22 -0
- package/dist/live-client.js +93 -0
- package/dist/live-page.d.ts +11 -0
- package/dist/live-page.js +143 -0
- package/dist/live-server.d.ts +173 -0
- package/dist/live-server.js +640 -0
- package/dist/live.d.ts +120 -0
- package/dist/live.js +203 -0
- package/dist/login-tool.d.ts +13 -0
- package/dist/login-tool.js +5 -1
- package/dist/login.d.ts +4 -1
- package/dist/login.js +11 -2
- package/dist/network-chrome.d.ts +74 -0
- package/dist/network-chrome.js +178 -0
- package/dist/network.d.ts +125 -0
- package/dist/network.js +263 -0
- package/dist/opt-in-settings.d.ts +69 -0
- package/dist/opt-in-settings.js +151 -0
- package/dist/recorder.d.ts +88 -0
- package/dist/recorder.js +538 -0
- package/dist/recordings.d.ts +116 -0
- package/dist/recordings.js +474 -0
- package/dist/redact.d.ts +7 -0
- package/dist/redact.js +18 -0
- package/dist/replay.d.ts +20 -0
- package/dist/replay.js +382 -0
- package/dist/reputation.d.ts +24 -2
- package/dist/reputation.js +63 -17
- package/dist/session-export.js +1 -1
- package/dist/session.d.ts +10 -2
- package/dist/session.js +10 -2
- package/dist/settings.d.ts +104 -2
- package/dist/settings.js +204 -6
- package/dist/sharing.d.ts +96 -0
- package/dist/sharing.js +269 -0
- package/dist/snippet.d.ts +8 -2
- package/dist/snippet.js +10 -4
- package/dist/tab-locks.d.ts +63 -0
- package/dist/tab-locks.js +179 -0
- package/dist/tools.d.ts +38 -0
- package/dist/tools.js +20 -1
- package/dist/tuning.d.ts +14 -0
- package/dist/tuning.js +9 -0
- package/dist/upstream.d.ts +11 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { inflateRawSync } from "node:zlib";
|
|
6
|
+
import { identityStateDir } from "./ids.js";
|
|
7
|
+
import { parseSize } from "./units.js";
|
|
8
|
+
/**
|
|
9
|
+
* Recordings of an identity's browser sessions, opt-in in identity.json (`record.traces`,
|
|
10
|
+
* `record.screenshots`): a Playwright trace (DOM snapshots, network, a screencast) and a
|
|
11
|
+
* screenshot after each action, in one folder per browser session under
|
|
12
|
+
* `<state>/browser/identities/<id>/recordings/`. The folders together stay under
|
|
13
|
+
* `record.maxSize` (200MB by default): the oldest go first.
|
|
14
|
+
*
|
|
15
|
+
* A trace keeps what the page held, so a vault sign-in is never in one:
|
|
16
|
+
*
|
|
17
|
+
* - Before `browser_login` fills a form (or a value goes into a password field, or a replay
|
|
18
|
+
* types a sensitive parameter), the trace is stopped and saved. Stopping, not a new chunk:
|
|
19
|
+
* Playwright keeps recording network entries between chunks and copies them into the next
|
|
20
|
+
* one, so a sign-in request would land in the next file.
|
|
21
|
+
* - It starts again at the first call after every page that holds those values has navigated
|
|
22
|
+
* away (the vault guard), so neither the filled form nor the request that sends it is traced.
|
|
23
|
+
* - Each saved trace is read back and searched for the passwords, one-time codes and sensitive
|
|
24
|
+
* values this server knows, in the encodings the redactor knows; a trace that holds one, or
|
|
25
|
+
* that cannot be read while such values exist, is deleted.
|
|
26
|
+
* - No screenshot is taken of a page while it holds those values. There is no video: a video
|
|
27
|
+
* cannot leave the sign-in out.
|
|
28
|
+
*
|
|
29
|
+
* Playwright writes a trace's raw files to the launch's `tracesDir` first; the host points it
|
|
30
|
+
* at a folder of this identity, emptied after every trace, instead of the workspace.
|
|
31
|
+
*/
|
|
32
|
+
const require = createRequire(import.meta.url);
|
|
33
|
+
/** Default size cap of an identity's recordings. */
|
|
34
|
+
const DEFAULT_RECORD_BYTES = 200 * 1024 ** 2;
|
|
35
|
+
/** Values a saved trace must not hold (labels the redactor gives them). */
|
|
36
|
+
const SECRET_LABEL = /password|one-time code|sensitive/;
|
|
37
|
+
const INDEX = "index.json";
|
|
38
|
+
const TRACES_TMP = ".traces-tmp";
|
|
39
|
+
export function recordingsDir(id, env = process.env) {
|
|
40
|
+
return join(identityStateDir(id, env), "recordings");
|
|
41
|
+
}
|
|
42
|
+
/** Where Playwright writes a trace's raw files while it records (the launch's `tracesDir`). */
|
|
43
|
+
export function tracesTmpDir(id, env = process.env) {
|
|
44
|
+
return join(recordingsDir(id, env), TRACES_TMP);
|
|
45
|
+
}
|
|
46
|
+
function dirSize(path) {
|
|
47
|
+
let total = 0;
|
|
48
|
+
let entries;
|
|
49
|
+
try {
|
|
50
|
+
entries = readdirSync(path, { withFileTypes: true });
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
for (const e of entries) {
|
|
56
|
+
const p = join(path, e.name);
|
|
57
|
+
if (e.isDirectory())
|
|
58
|
+
total += dirSize(p);
|
|
59
|
+
else
|
|
60
|
+
try {
|
|
61
|
+
total += statSync(p).size;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
/* gone meanwhile */
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return total;
|
|
68
|
+
}
|
|
69
|
+
function readIndex(dir) {
|
|
70
|
+
try {
|
|
71
|
+
const raw = JSON.parse(readFileSync(join(dir, INDEX), "utf8"));
|
|
72
|
+
return raw && raw.version === 1 && Array.isArray(raw.steps) && Array.isArray(raw.traces) ? raw : undefined;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** The recording folders of an identity, oldest first (their names sort by time). */
|
|
79
|
+
function recordingFolders(id, env) {
|
|
80
|
+
const root = recordingsDir(id, env);
|
|
81
|
+
if (!existsSync(root))
|
|
82
|
+
return [];
|
|
83
|
+
return readdirSync(root, { withFileTypes: true })
|
|
84
|
+
.filter((e) => e.isDirectory() && !e.name.startsWith("."))
|
|
85
|
+
.map((e) => e.name)
|
|
86
|
+
.sort();
|
|
87
|
+
}
|
|
88
|
+
/** An identity's recordings, newest first. */
|
|
89
|
+
export function listRecordings(id, env = process.env) {
|
|
90
|
+
const root = recordingsDir(id, env);
|
|
91
|
+
return recordingFolders(id, env)
|
|
92
|
+
.reverse()
|
|
93
|
+
.flatMap((name) => {
|
|
94
|
+
const dir = join(root, name);
|
|
95
|
+
const index = readIndex(dir);
|
|
96
|
+
if (!index)
|
|
97
|
+
return [];
|
|
98
|
+
const traces = index.traces
|
|
99
|
+
.filter((t) => !t.discarded && existsSync(join(dir, t.file)))
|
|
100
|
+
.map((t) => ({ file: join(dir, t.file), from: t.from, to: t.to, bytes: statSync(join(dir, t.file)).size }));
|
|
101
|
+
return [
|
|
102
|
+
{
|
|
103
|
+
name,
|
|
104
|
+
dir,
|
|
105
|
+
title: index.title,
|
|
106
|
+
started: index.started,
|
|
107
|
+
...(index.ended ? { ended: index.ended } : {}),
|
|
108
|
+
steps: index.steps.length,
|
|
109
|
+
screenshots: index.steps.filter((s) => s.screenshot).length,
|
|
110
|
+
traces,
|
|
111
|
+
discarded: index.traces.filter((t) => t.discarded).length,
|
|
112
|
+
bytes: dirSize(dir),
|
|
113
|
+
notes: index.notes,
|
|
114
|
+
},
|
|
115
|
+
];
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The command that opens a trace in the trace viewer patchright-core ships (its own
|
|
120
|
+
* `show-trace`, served on 127.0.0.1 and opened in the default browser). Never `npx`, which
|
|
121
|
+
* would download whatever package has that name.
|
|
122
|
+
*/
|
|
123
|
+
export function traceViewerCommand(file) {
|
|
124
|
+
const cli = join(dirname(require.resolve("patchright-core/package.json")), "cli.js");
|
|
125
|
+
return { command: process.execPath, args: [cli, "show-trace", "--host", "127.0.0.1", "--port", "0", file] };
|
|
126
|
+
}
|
|
127
|
+
/** Runs the trace viewer until the person stops it; resolves with its exit code. */
|
|
128
|
+
export function openTraceViewer(file, env = process.env) {
|
|
129
|
+
if (!existsSync(file))
|
|
130
|
+
return Promise.reject(new Error(`no trace at ${file}`));
|
|
131
|
+
const { command, args } = traceViewerCommand(file);
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
const child = spawn(command, args, { env, stdio: "inherit" });
|
|
134
|
+
child.once("error", reject);
|
|
135
|
+
child.once("exit", (code) => resolve(code ?? 0));
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The text of every entry of a zip, images left out. Handles stored and deflated entries (what
|
|
140
|
+
* Playwright writes); throws on anything else, so a caller can treat the file as unreadable.
|
|
141
|
+
*/
|
|
142
|
+
export function zipTexts(buf) {
|
|
143
|
+
let eocd = -1;
|
|
144
|
+
for (let i = buf.length - 22; i >= Math.max(0, buf.length - 22 - 65_535); i--) {
|
|
145
|
+
if (buf.readUInt32LE(i) === 0x06054b50) {
|
|
146
|
+
eocd = i;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (eocd < 0)
|
|
151
|
+
throw new Error("not a zip file");
|
|
152
|
+
const count = buf.readUInt16LE(eocd + 10);
|
|
153
|
+
let p = buf.readUInt32LE(eocd + 16);
|
|
154
|
+
const out = [];
|
|
155
|
+
for (let n = 0; n < count; n++) {
|
|
156
|
+
if (buf.readUInt32LE(p) !== 0x02014b50)
|
|
157
|
+
throw new Error("bad zip directory");
|
|
158
|
+
const method = buf.readUInt16LE(p + 10);
|
|
159
|
+
const size = buf.readUInt32LE(p + 20);
|
|
160
|
+
const nameLen = buf.readUInt16LE(p + 28);
|
|
161
|
+
const extraLen = buf.readUInt16LE(p + 30);
|
|
162
|
+
const commentLen = buf.readUInt16LE(p + 32);
|
|
163
|
+
const local = buf.readUInt32LE(p + 42);
|
|
164
|
+
const name = buf.toString("utf8", p + 46, p + 46 + nameLen);
|
|
165
|
+
p += 46 + nameLen + extraLen + commentLen;
|
|
166
|
+
if (size === 0xffffffff || local === 0xffffffff)
|
|
167
|
+
throw new Error("zip64 is not read");
|
|
168
|
+
if (/\.(png|jpe?g|webp)$/i.test(name))
|
|
169
|
+
continue;
|
|
170
|
+
if (buf.readUInt32LE(local) !== 0x04034b50)
|
|
171
|
+
throw new Error("bad zip entry");
|
|
172
|
+
const start = local + 30 + buf.readUInt16LE(local + 26) + buf.readUInt16LE(local + 28);
|
|
173
|
+
const data = buf.subarray(start, start + size);
|
|
174
|
+
if (method === 0)
|
|
175
|
+
out.push({ name, text: data.toString("utf8") });
|
|
176
|
+
else if (method === 8)
|
|
177
|
+
out.push({ name, text: inflateRawSync(data).toString("utf8") });
|
|
178
|
+
else
|
|
179
|
+
throw new Error(`zip method ${method} is not read`);
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
/** The secret labels a trace file holds; `["unreadable"]` when it cannot be searched. */
|
|
184
|
+
export function traceLeaks(file, redactor) {
|
|
185
|
+
let texts;
|
|
186
|
+
try {
|
|
187
|
+
texts = zipTexts(readFileSync(file));
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
return ["unreadable"];
|
|
191
|
+
}
|
|
192
|
+
const found = new Set();
|
|
193
|
+
for (const { text } of texts)
|
|
194
|
+
for (const l of redactor.labelsIn(text, (x) => SECRET_LABEL.test(x)))
|
|
195
|
+
found.add(l);
|
|
196
|
+
return [...found];
|
|
197
|
+
}
|
|
198
|
+
function clip(text, max) {
|
|
199
|
+
return text.length > max ? `${text.slice(0, max - 3)}...` : text;
|
|
200
|
+
}
|
|
201
|
+
function plainUrl(url) {
|
|
202
|
+
try {
|
|
203
|
+
const u = new URL(url);
|
|
204
|
+
return u.protocol === "about:" ? undefined : `${u.origin}${u.pathname}`;
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/** The recording of one browser session (one launch). */
|
|
211
|
+
export class Recording {
|
|
212
|
+
o;
|
|
213
|
+
dir;
|
|
214
|
+
index;
|
|
215
|
+
trace = "off";
|
|
216
|
+
segment = 0;
|
|
217
|
+
segmentFrom = 1;
|
|
218
|
+
flushing;
|
|
219
|
+
timer;
|
|
220
|
+
cap;
|
|
221
|
+
bytes = 0;
|
|
222
|
+
capNoted = false;
|
|
223
|
+
closed = false;
|
|
224
|
+
constructor(o) {
|
|
225
|
+
this.o = o;
|
|
226
|
+
const now = (o.now ?? (() => new Date()))();
|
|
227
|
+
const stamp = now.toISOString().replace(/[-:]/g, "").replace("T", "-").replace(".", "-").slice(0, 19);
|
|
228
|
+
let dir = join(recordingsDir(o.identity.id, o.identity.env), stamp);
|
|
229
|
+
for (let n = 2; existsSync(dir); n++)
|
|
230
|
+
dir = join(recordingsDir(o.identity.id, o.identity.env), `${stamp}-${n}`);
|
|
231
|
+
this.dir = dir;
|
|
232
|
+
this.cap = (o.settings.maxSize ? parseSize(o.settings.maxSize) : undefined) ?? DEFAULT_RECORD_BYTES;
|
|
233
|
+
this.index = {
|
|
234
|
+
version: 1,
|
|
235
|
+
identity: o.identity.id,
|
|
236
|
+
title: o.title,
|
|
237
|
+
started: now.toISOString(),
|
|
238
|
+
steps: [],
|
|
239
|
+
traces: [],
|
|
240
|
+
notes: o.noTraces && o.settings.traces ? [`no trace: ${o.noTraces}`] : [],
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/** Starts a recording for a launched browser; undefined when identity.json asks for none. */
|
|
244
|
+
static async open(o) {
|
|
245
|
+
if (!o.settings.traces && !o.settings.screenshots)
|
|
246
|
+
return undefined;
|
|
247
|
+
const r = new Recording(o);
|
|
248
|
+
mkdirSync(r.dir, { recursive: true, mode: 0o700 });
|
|
249
|
+
rmSync(tracesTmpDir(o.identity.id, o.identity.env), { recursive: true, force: true });
|
|
250
|
+
r.rotate(r.cap / 2);
|
|
251
|
+
r.write();
|
|
252
|
+
if (o.settings.traces && !o.noTraces)
|
|
253
|
+
await r.startTrace();
|
|
254
|
+
return r;
|
|
255
|
+
}
|
|
256
|
+
get others() {
|
|
257
|
+
const root = recordingsDir(this.o.identity.id, this.o.identity.env);
|
|
258
|
+
return recordingFolders(this.o.identity.id, this.o.identity.env)
|
|
259
|
+
.map((name) => join(root, name))
|
|
260
|
+
.filter((d) => d !== this.dir)
|
|
261
|
+
.reduce((sum, d) => sum + dirSize(d), 0);
|
|
262
|
+
}
|
|
263
|
+
/** Deletes the oldest other recordings until they take at most `limit` bytes. */
|
|
264
|
+
rotate(limit) {
|
|
265
|
+
const root = recordingsDir(this.o.identity.id, this.o.identity.env);
|
|
266
|
+
const folders = recordingFolders(this.o.identity.id, this.o.identity.env)
|
|
267
|
+
.map((name) => join(root, name))
|
|
268
|
+
.filter((d) => d !== this.dir);
|
|
269
|
+
let total = folders.reduce((sum, d) => sum + dirSize(d), 0);
|
|
270
|
+
for (const d of folders) {
|
|
271
|
+
if (total <= limit)
|
|
272
|
+
break;
|
|
273
|
+
total -= dirSize(d);
|
|
274
|
+
rmSync(d, { recursive: true, force: true });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/** Room for `size` more bytes, deleting older recordings first; false when the cap is reached. */
|
|
278
|
+
room(size) {
|
|
279
|
+
if (this.bytes + size + this.others <= this.cap)
|
|
280
|
+
return true;
|
|
281
|
+
this.rotate(Math.max(0, this.cap - this.bytes - size));
|
|
282
|
+
if (this.bytes + size + this.others <= this.cap)
|
|
283
|
+
return true;
|
|
284
|
+
if (!this.capNoted) {
|
|
285
|
+
this.capNoted = true;
|
|
286
|
+
this.index.notes.push(`stopped at the size cap (record.maxSize ${this.o.settings.maxSize ?? "200MB"})`);
|
|
287
|
+
}
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
write() {
|
|
291
|
+
try {
|
|
292
|
+
const file = join(this.dir, INDEX);
|
|
293
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
294
|
+
writeFileSync(tmp, `${JSON.stringify(this.index, null, 2)}\n`, { mode: 0o600 });
|
|
295
|
+
renameSync(tmp, file);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
/* best effort: a full disk must not break a tool call */
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
async startTrace() {
|
|
302
|
+
this.segment++;
|
|
303
|
+
try {
|
|
304
|
+
await this.o.context.tracing.start({
|
|
305
|
+
name: `segment-${this.segment}`,
|
|
306
|
+
title: this.o.title,
|
|
307
|
+
screenshots: true,
|
|
308
|
+
snapshots: true,
|
|
309
|
+
});
|
|
310
|
+
this.trace = "on";
|
|
311
|
+
this.segmentFrom = this.index.steps.length + 1;
|
|
312
|
+
}
|
|
313
|
+
catch (error) {
|
|
314
|
+
this.trace = "off";
|
|
315
|
+
this.index.notes.push(`no trace: ${String(error.message ?? error).split("\n")[0]}`);
|
|
316
|
+
this.write();
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/** Stops the trace and keeps the file, unless it holds a secret or passes the cap. */
|
|
320
|
+
async stopTrace(why) {
|
|
321
|
+
if (this.trace !== "on")
|
|
322
|
+
return;
|
|
323
|
+
this.trace = "paused";
|
|
324
|
+
const name = `trace-${this.segment}.zip`;
|
|
325
|
+
const file = join(this.dir, name);
|
|
326
|
+
const entry = { file: name, from: this.segmentFrom, to: this.index.steps.length };
|
|
327
|
+
if (entry.to < entry.from)
|
|
328
|
+
entry.from = entry.to = 0;
|
|
329
|
+
try {
|
|
330
|
+
await this.o.context.tracing.stop({ path: file });
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
entry.discarded = `not saved (${why}): ${String(error.message ?? error).split("\n")[0]}`;
|
|
334
|
+
}
|
|
335
|
+
finally {
|
|
336
|
+
rmSync(tracesTmpDir(this.o.identity.id, this.o.identity.env), { recursive: true, force: true });
|
|
337
|
+
}
|
|
338
|
+
if (!entry.discarded && existsSync(file)) {
|
|
339
|
+
const leaks = this.o.redactor.active ? traceLeaks(file, this.o.redactor) : [];
|
|
340
|
+
const size = statSync(file).size;
|
|
341
|
+
if (leaks.length) {
|
|
342
|
+
rmSync(file, { force: true });
|
|
343
|
+
entry.discarded =
|
|
344
|
+
leaks[0] === "unreadable"
|
|
345
|
+
? "deleted: it could not be searched for vault values"
|
|
346
|
+
: "deleted: it held a vault or sensitive value";
|
|
347
|
+
}
|
|
348
|
+
else if (!this.room(size)) {
|
|
349
|
+
rmSync(file, { force: true });
|
|
350
|
+
entry.discarded = "deleted: over the size cap";
|
|
351
|
+
this.trace = "off";
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
this.bytes += size;
|
|
355
|
+
entry.bytes = size;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
this.index.traces.push(entry);
|
|
359
|
+
this.write();
|
|
360
|
+
}
|
|
361
|
+
/** Before a tool call: waits for a save in progress and starts the trace again when it may. */
|
|
362
|
+
async beforeCall() {
|
|
363
|
+
if (this.timer)
|
|
364
|
+
clearTimeout(this.timer);
|
|
365
|
+
this.timer = undefined;
|
|
366
|
+
await this.flushing;
|
|
367
|
+
if (this.closed || this.trace !== "paused" || this.capNoted)
|
|
368
|
+
return;
|
|
369
|
+
const held = this.o.context.pages().some((p) => this.o.guard.holds(p));
|
|
370
|
+
if (!held)
|
|
371
|
+
await this.startTrace();
|
|
372
|
+
}
|
|
373
|
+
/** After a tool call: saves the trace shortly before the idle limit would close Chrome. */
|
|
374
|
+
callEnded() {
|
|
375
|
+
if (this.trace !== "on" || !this.o.idleMs)
|
|
376
|
+
return;
|
|
377
|
+
const ms = this.o.idleMs > 10_000 ? this.o.idleMs - 5_000 : Math.floor(this.o.idleMs / 2);
|
|
378
|
+
if (this.timer)
|
|
379
|
+
clearTimeout(this.timer);
|
|
380
|
+
this.timer = setTimeout(() => {
|
|
381
|
+
this.timer = undefined;
|
|
382
|
+
this.flushing = this.stopTrace("before the idle limit").finally(() => {
|
|
383
|
+
this.flushing = undefined;
|
|
384
|
+
});
|
|
385
|
+
}, ms);
|
|
386
|
+
this.timer.unref();
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Before a secret goes into `page` (a vault fill, a password typed, a sensitive parameter):
|
|
390
|
+
* saves the trace and holds `page` in the vault guard, so neither the trace nor a
|
|
391
|
+
* screenshot starts again until it navigates.
|
|
392
|
+
*/
|
|
393
|
+
async pauseForSecret(page) {
|
|
394
|
+
if (this.timer)
|
|
395
|
+
clearTimeout(this.timer);
|
|
396
|
+
this.timer = undefined;
|
|
397
|
+
await this.flushing;
|
|
398
|
+
if (page)
|
|
399
|
+
this.o.guard.mark(page);
|
|
400
|
+
await this.stopTrace("before a secret was filled");
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* The user takes over the browser (the live view's pause): the trace is saved, so what the
|
|
404
|
+
* user does is not in it, and starts again at the agent's next call.
|
|
405
|
+
*/
|
|
406
|
+
async pauseForUser() {
|
|
407
|
+
if (this.timer)
|
|
408
|
+
clearTimeout(this.timer);
|
|
409
|
+
this.timer = undefined;
|
|
410
|
+
await this.flushing;
|
|
411
|
+
await this.stopTrace("when the user took over");
|
|
412
|
+
}
|
|
413
|
+
/** After an action: one line in the index and, when asked, a screenshot. */
|
|
414
|
+
async afterStep(step) {
|
|
415
|
+
if (this.closed)
|
|
416
|
+
return;
|
|
417
|
+
const n = this.index.steps.length + 1;
|
|
418
|
+
const entry = { n, at: new Date().toISOString(), tool: step.tool, ok: step.ok };
|
|
419
|
+
if (typeof step.element === "string" && step.element)
|
|
420
|
+
entry.element = clip(this.o.redactor.text(step.element), 120);
|
|
421
|
+
const url = step.page ? plainUrl(step.page.url()) : undefined;
|
|
422
|
+
if (url)
|
|
423
|
+
entry.url = this.o.redactor.text(url);
|
|
424
|
+
if (this.o.settings.screenshots && step.screenshot && step.page) {
|
|
425
|
+
if (this.o.guard.holds(step.page))
|
|
426
|
+
entry.note = "no screenshot: the page holds vault or sensitive values";
|
|
427
|
+
else {
|
|
428
|
+
const shot = await step.page.screenshot({ type: "jpeg", quality: 60, timeout: 5_000 }).catch(() => undefined);
|
|
429
|
+
if (shot && this.room(shot.length)) {
|
|
430
|
+
const rel = join("steps", `${String(n).padStart(3, "0")}-${step.tool}.jpg`);
|
|
431
|
+
try {
|
|
432
|
+
mkdirSync(join(this.dir, "steps"), { recursive: true, mode: 0o700 });
|
|
433
|
+
writeFileSync(join(this.dir, rel), shot, { mode: 0o600 });
|
|
434
|
+
this.bytes += shot.length;
|
|
435
|
+
entry.screenshot = rel;
|
|
436
|
+
}
|
|
437
|
+
catch {
|
|
438
|
+
/* best effort */
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
this.index.steps.push(entry);
|
|
444
|
+
this.write();
|
|
445
|
+
}
|
|
446
|
+
/** Before turno closes the browser: saves the trace. */
|
|
447
|
+
async close() {
|
|
448
|
+
if (this.closed)
|
|
449
|
+
return;
|
|
450
|
+
if (this.timer)
|
|
451
|
+
clearTimeout(this.timer);
|
|
452
|
+
this.timer = undefined;
|
|
453
|
+
await this.flushing;
|
|
454
|
+
await this.stopTrace("when the browser closed");
|
|
455
|
+
this.closed = true;
|
|
456
|
+
this.index.ended = new Date().toISOString();
|
|
457
|
+
this.write();
|
|
458
|
+
}
|
|
459
|
+
/** The browser went away on its own (a crash, a kill): the trace in progress is lost. */
|
|
460
|
+
abandon() {
|
|
461
|
+
if (this.closed)
|
|
462
|
+
return;
|
|
463
|
+
this.closed = true;
|
|
464
|
+
if (this.timer)
|
|
465
|
+
clearTimeout(this.timer);
|
|
466
|
+
if (this.trace === "on")
|
|
467
|
+
this.index.notes.push("the browser closed before the last trace was saved");
|
|
468
|
+
this.trace = "off";
|
|
469
|
+
this.index.ended = new Date().toISOString();
|
|
470
|
+
this.write();
|
|
471
|
+
rmSync(tracesTmpDir(this.o.identity.id, this.o.identity.env), { recursive: true, force: true });
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
//# sourceMappingURL=recordings.js.map
|
package/dist/redact.d.ts
CHANGED
|
@@ -8,6 +8,13 @@ export declare class Redactor {
|
|
|
8
8
|
add(value: string, label: string, ttlMs?: number, now?: number): void;
|
|
9
9
|
/** True when there is something to redact. */
|
|
10
10
|
get active(): boolean;
|
|
11
|
+
/** True when `text` holds a value this redactor hides, in any of its forms. */
|
|
12
|
+
covers(text: string, now?: number): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* The labels of the values found in `text` whose label passes `which` (a recorder looks for
|
|
15
|
+
* passwords and one-time codes in a trace before it keeps it).
|
|
16
|
+
*/
|
|
17
|
+
labelsIn(text: string, which: (label: string) => boolean, now?: number): string[];
|
|
11
18
|
text(text: string, now?: number): string;
|
|
12
19
|
/** A tool result with every text part (and any string in structured content) redacted. */
|
|
13
20
|
result(result: ToolResult, now?: number): ToolResult;
|
package/dist/redact.js
CHANGED
|
@@ -48,6 +48,24 @@ export class Redactor {
|
|
|
48
48
|
get active() {
|
|
49
49
|
return this.entries.size > 0;
|
|
50
50
|
}
|
|
51
|
+
/** True when `text` holds a value this redactor hides, in any of its forms. */
|
|
52
|
+
covers(text, now = Date.now()) {
|
|
53
|
+
return this.labelsIn(text, () => true, now).length > 0;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The labels of the values found in `text` whose label passes `which` (a recorder looks for
|
|
57
|
+
* passwords and one-time codes in a trace before it keeps it).
|
|
58
|
+
*/
|
|
59
|
+
labelsIn(text, which, now = Date.now()) {
|
|
60
|
+
const found = new Set();
|
|
61
|
+
for (const e of this.entries.values()) {
|
|
62
|
+
if ((e.until !== undefined && now > e.until) || !which(e.label))
|
|
63
|
+
continue;
|
|
64
|
+
if (e.variants.some((v) => text.includes(v)))
|
|
65
|
+
found.add(e.label);
|
|
66
|
+
}
|
|
67
|
+
return [...found];
|
|
68
|
+
}
|
|
51
69
|
text(text, now = Date.now()) {
|
|
52
70
|
if (!this.entries.size || !text)
|
|
53
71
|
return text;
|
package/dist/replay.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { TurnoTool } from "./tools.js";
|
|
2
|
+
/** `browser_replay`. */
|
|
3
|
+
export declare function replayTool(): TurnoTool;
|
|
4
|
+
/** `browser_flows`. */
|
|
5
|
+
export declare function flowsTool(): TurnoTool;
|
|
6
|
+
export interface ReplayOptions {
|
|
7
|
+
params?: Record<string, string>;
|
|
8
|
+
from?: number;
|
|
9
|
+
headless?: boolean;
|
|
10
|
+
env?: NodeJS.ProcessEnv;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Replays a flow outside an agent (`turno browser replay`): starts this package's MCP server
|
|
14
|
+
* on the identity and calls `browser_replay`, so the run goes through exactly the path an agent's
|
|
15
|
+
* does (lock, vault, redaction, recordings). Parameter values travel over the server's stdin.
|
|
16
|
+
*/
|
|
17
|
+
export declare function replayFlow(id: string, flow: string, options?: ReplayOptions): Promise<{
|
|
18
|
+
ok: boolean;
|
|
19
|
+
text: string;
|
|
20
|
+
}>;
|