turno-browser 0.3.0 → 0.5.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 +122 -0
- package/README.md +95 -123
- package/dist/act-model.d.ts +85 -0
- package/dist/act-model.js +117 -0
- package/dist/act.d.ts +37 -0
- package/dist/act.js +600 -0
- package/dist/api.d.ts +90 -2
- package/dist/api.js +41 -4
- package/dist/argv.d.ts +16 -2
- package/dist/argv.js +100 -3
- package/dist/background-state.d.ts +54 -0
- package/dist/background-state.js +97 -0
- package/dist/background.d.ts +69 -0
- package/dist/background.js +458 -0
- package/dist/batch.d.ts +5 -0
- package/dist/batch.js +3 -3
- package/dist/bin/mcp.js +103 -6
- package/dist/captcha.d.ts +77 -0
- package/dist/captcha.js +334 -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/cli/background.d.ts +11 -0
- package/dist/cli/background.js +132 -0
- package/dist/cli/commands.d.ts +16 -0
- package/dist/cli/commands.js +515 -0
- package/dist/cli/context.d.ts +75 -0
- package/dist/cli/context.js +44 -0
- package/dist/cli/do-background.d.ts +27 -0
- package/dist/cli/do-background.js +212 -0
- package/dist/cli/do.d.ts +85 -0
- package/dist/cli/do.js +558 -0
- package/dist/cli/flows.d.ts +28 -0
- package/dist/cli/flows.js +303 -0
- package/dist/cli/identity.d.ts +37 -0
- package/dist/cli/identity.js +906 -0
- package/dist/cli/live.d.ts +7 -0
- package/dist/cli/live.js +99 -0
- package/dist/cli/main.d.ts +16 -0
- package/dist/cli/main.js +171 -0
- package/dist/cli/secret.d.ts +4 -0
- package/dist/cli/secret.js +151 -0
- package/dist/cli/session.d.ts +6 -0
- package/dist/cli/session.js +106 -0
- package/dist/cli/setup.d.ts +7 -0
- package/dist/cli/setup.js +180 -0
- package/dist/cli/vault.d.ts +5 -0
- package/dist/cli/vault.js +192 -0
- package/dist/client.d.ts +82 -0
- package/dist/client.js +303 -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 +264 -6
- 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 +271 -0
- package/dist/engine-lightpanda.d.ts +72 -0
- package/dist/engine-lightpanda.js +293 -0
- package/dist/engine.d.ts +63 -11
- package/dist/engine.js +106 -29
- 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/health.js +2 -1
- package/dist/host.d.ts +38 -2
- package/dist/host.js +689 -52
- package/dist/human-input.d.ts +84 -0
- package/dist/human-input.js +448 -0
- package/dist/humanize.d.ts +123 -0
- package/dist/humanize.js +241 -0
- package/dist/identities.js +4 -1
- package/dist/idle-timer.d.ts +12 -0
- package/dist/idle-timer.js +25 -0
- package/dist/index.d.ts +13 -4
- package/dist/index.js +7 -1
- package/dist/lifecycle.d.ts +11 -1
- package/dist/lifecycle.js +11 -2
- package/dist/live-client.d.ts +22 -0
- package/dist/live-client.js +95 -0
- package/dist/live-page.d.ts +4 -0
- package/dist/live-page.js +144 -0
- package/dist/live-server.d.ts +173 -0
- package/dist/live-server.js +643 -0
- package/dist/live.d.ts +120 -0
- package/dist/live.js +204 -0
- package/dist/login-tool.d.ts +13 -0
- package/dist/login-tool.js +8 -3
- package/dist/login.d.ts +4 -1
- package/dist/login.js +12 -2
- package/dist/names.d.ts +28 -0
- package/dist/names.js +25 -0
- 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 +264 -0
- package/dist/opt-in-settings.d.ts +69 -0
- package/dist/opt-in-settings.js +152 -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 +383 -0
- package/dist/reputation.d.ts +24 -2
- package/dist/reputation.js +63 -17
- package/dist/runtime-lock.js +3 -2
- 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/dist/vault.js +5 -4
- package/npm-shrinkwrap.json +6 -5
- package/package.json +13 -7
package/dist/doctor.js
CHANGED
|
@@ -3,12 +3,19 @@ import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync } from "node
|
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { dirname, join } from "node:path";
|
|
5
5
|
import { browserPaths as paths } from "turno-core/paths";
|
|
6
|
+
import { readProcTable, signalProcess } from "turno-core/process";
|
|
7
|
+
import { secretStore } from "turno-core/secrets";
|
|
8
|
+
import { backgroundAlive, readBackgroundState } from "./background-state.js";
|
|
6
9
|
import { readHealth } from "./health.js";
|
|
7
10
|
import { identityDir, isValidIdentityId } from "./ids.js";
|
|
11
|
+
import { defaultChromeUserDataDir, parseAttachTarget, resolveAttachEndpoint } from "./engine-attach.js";
|
|
12
|
+
import { findLightpanda, findOrphanLightpandas, lightpandaCookieJar, lightpandaInstallHint, } from "./engine-lightpanda.js";
|
|
13
|
+
import { camoufoxInstallHint, camoufoxProfileDir, findCamoufox, findPlaywrightCore, playwrightCoreHint, } from "./engine-camoufox.js";
|
|
8
14
|
import { findProfileChrome, sweepOrphan } from "./lifecycle.js";
|
|
9
15
|
import { readChallenges, summarizeChallenges } from "./reputation.js";
|
|
10
16
|
import { inUseMessage, readRuntimeLock, runtimeLockAlive, runtimeLockPath } from "./runtime-lock.js";
|
|
11
17
|
import { acceptLanguages, checkIdentitySettings, systemTimezone } from "./settings.js";
|
|
18
|
+
import { names } from "./names.js";
|
|
12
19
|
const FIX = "turno browser doctor --fix";
|
|
13
20
|
const CHROME_PATHS = (env, platform) => {
|
|
14
21
|
const home = paths(env).home;
|
|
@@ -127,7 +134,27 @@ function settingsFix(id, problem) {
|
|
|
127
134
|
].find(([key]) => problem.startsWith(key))?.[1];
|
|
128
135
|
return flag ? `turno identity set browser:${id} ${flag}` : undefined;
|
|
129
136
|
}
|
|
130
|
-
|
|
137
|
+
/**
|
|
138
|
+
* A server that lets several client sessions in (`exclusive = false`, one server for all of
|
|
139
|
+
* them) on an identity that serves one session at a time: those sessions silently share one
|
|
140
|
+
* current tab, and one agent's navigation replaces the page another is reading.
|
|
141
|
+
*/
|
|
142
|
+
function servingRows(id, settings, servers) {
|
|
143
|
+
const out = [];
|
|
144
|
+
for (const server of servers) {
|
|
145
|
+
if (server.identity !== id || server.exclusive || server.scope === "session")
|
|
146
|
+
continue;
|
|
147
|
+
if ((server.sharing ?? settings?.sharing ?? "session") !== "session")
|
|
148
|
+
continue;
|
|
149
|
+
out.push({
|
|
150
|
+
level: "warn",
|
|
151
|
+
text: `[server.${server.name}] in mcp.toml lets several sessions in (exclusive = false), but this identity serves one at a time ("sharing": "session"), so those sessions share one current tab`,
|
|
152
|
+
fix: `turno browser set ${id} --sharing tabs`,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
function identityChecks(id, env, now, servers = []) {
|
|
131
158
|
const area = `identity ${id}`;
|
|
132
159
|
const out = [];
|
|
133
160
|
const add = (c) => out.push({ area, ...c });
|
|
@@ -153,6 +180,12 @@ function identityChecks(id, env, now) {
|
|
|
153
180
|
else {
|
|
154
181
|
add({ level: "ok", text: `identity.json: ${describeSettings(settings)}` });
|
|
155
182
|
}
|
|
183
|
+
if (settings)
|
|
184
|
+
for (const c of engineRows(id, settings, env))
|
|
185
|
+
add(c);
|
|
186
|
+
if (!problems.length)
|
|
187
|
+
for (const c of servingRows(id, settings, servers))
|
|
188
|
+
add(c);
|
|
156
189
|
const lock = readRuntimeLock(id, env);
|
|
157
190
|
const chrome = findProfileChrome(identityDir(id, env));
|
|
158
191
|
if (lock && runtimeLockAlive(lock)) {
|
|
@@ -189,6 +222,17 @@ function identityChecks(id, env, now) {
|
|
|
189
222
|
else {
|
|
190
223
|
add({ level: "ok", text: "free: no process holds it" });
|
|
191
224
|
}
|
|
225
|
+
// The background server `turno-browser do` starts: while it runs, an agent's server of this
|
|
226
|
+
// identity refuses to launch the browser, so say it runs and how to stop it.
|
|
227
|
+
const background = readBackgroundState(id, env);
|
|
228
|
+
if (background?.transport && backgroundAlive(background)) {
|
|
229
|
+
const idle = background.idleMs > 0 ? `; it exits after ${Math.round(background.idleMs / 60_000)}m without a request` : "";
|
|
230
|
+
add({
|
|
231
|
+
level: "info",
|
|
232
|
+
text: `background server (pid ${background.pid}) since ${background.startedAt} serves it to turno-browser do; an agent's server of this identity cannot open the browser until it stops${idle}`,
|
|
233
|
+
fix: `turno-browser background stop ${id}`,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
192
236
|
const check = settings?.health;
|
|
193
237
|
const health = readHealth(id, env);
|
|
194
238
|
if (!check) {
|
|
@@ -233,6 +277,195 @@ function identityChecks(id, env, now) {
|
|
|
233
277
|
}
|
|
234
278
|
return out;
|
|
235
279
|
}
|
|
280
|
+
const ENABLE_DEBUGGING = "start Chrome, open chrome://inspect/#remote-debugging and turn remote debugging on";
|
|
281
|
+
/** What an identity's engine needs (identity.json `engine`, `attach`); none for Chrome. */
|
|
282
|
+
function engineRows(id, settings, env) {
|
|
283
|
+
if (settings.engine === "camoufox")
|
|
284
|
+
return [
|
|
285
|
+
{
|
|
286
|
+
level: "info",
|
|
287
|
+
text: `Camoufox: its own Firefox profile in ${camoufoxProfileDir(id, env)}, not the Chrome profile (sign-ins do not carry over)`,
|
|
288
|
+
fix: `turno browser set ${id} --engine chrome`,
|
|
289
|
+
},
|
|
290
|
+
];
|
|
291
|
+
if (settings.engine === "lightpanda")
|
|
292
|
+
return [
|
|
293
|
+
{
|
|
294
|
+
level: "info",
|
|
295
|
+
text: `read mode on Lightpanda: its own cookies in ${lightpandaCookieJar(id, env)}, not the Chrome profile`,
|
|
296
|
+
fix: `turno browser set ${id} --engine chrome`,
|
|
297
|
+
},
|
|
298
|
+
];
|
|
299
|
+
if (settings.attach === undefined)
|
|
300
|
+
return [];
|
|
301
|
+
const target = parseAttachTarget(settings.attach);
|
|
302
|
+
if ("error" in target)
|
|
303
|
+
return [];
|
|
304
|
+
try {
|
|
305
|
+
const endpoint = resolveAttachEndpoint(target, env);
|
|
306
|
+
return [
|
|
307
|
+
{
|
|
308
|
+
level: "ok",
|
|
309
|
+
text: `attach: a Chrome listens at ${endpoint.replace(/\/devtools\/.*$/, "")}; it asks you to allow each new connection, and the agent then sees every window of that profile`,
|
|
310
|
+
},
|
|
311
|
+
];
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
const dir = target.kind === "dir" ? target.dir : target.kind === "auto" ? defaultChromeUserDataDir(env) : undefined;
|
|
315
|
+
return [
|
|
316
|
+
{
|
|
317
|
+
level: "warn",
|
|
318
|
+
text: `attach: no Chrome with remote debugging on${dir ? ` for ${dir}` : ""}, so the next tool call fails`,
|
|
319
|
+
manual: ENABLE_DEBUGGING,
|
|
320
|
+
},
|
|
321
|
+
];
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/** A `lightpanda serve` that a turno-browser-mcp started and that outlived it. */
|
|
325
|
+
function strayLightpandas(table) {
|
|
326
|
+
return table ? findOrphanLightpandas("any", table).map((o) => o.pid) : [];
|
|
327
|
+
}
|
|
328
|
+
function engineChecks(opts, env, ids) {
|
|
329
|
+
const out = [];
|
|
330
|
+
const area = "engines";
|
|
331
|
+
const lightpanda = (opts.lightpanda ?? findLightpanda)(env);
|
|
332
|
+
const users = ids.filter((id) => checkIdentitySettings(id, env).settings?.engine === "lightpanda");
|
|
333
|
+
const platform = opts.platform ?? process.platform;
|
|
334
|
+
const hint = lightpandaInstallHint(platform);
|
|
335
|
+
const install = platform === "darwin" || platform === "linux" ? { fix: hint } : { manual: hint };
|
|
336
|
+
if (lightpanda)
|
|
337
|
+
out.push({
|
|
338
|
+
area,
|
|
339
|
+
level: "ok",
|
|
340
|
+
text: `Lightpanda${lightpanda.version ? ` ${lightpanda.version}` : ""} at ${lightpanda.path} (read mode)`,
|
|
341
|
+
});
|
|
342
|
+
else if (users.length)
|
|
343
|
+
out.push({
|
|
344
|
+
area,
|
|
345
|
+
level: "fail",
|
|
346
|
+
text: `Lightpanda not found, and read mode needs it for ${users.map((u) => `browser:${u}`).join(", ")}`,
|
|
347
|
+
...install,
|
|
348
|
+
});
|
|
349
|
+
else
|
|
350
|
+
out.push({
|
|
351
|
+
area,
|
|
352
|
+
level: "info",
|
|
353
|
+
text: "Lightpanda not found: only read mode (--engine lightpanda) needs it, and turno never downloads it",
|
|
354
|
+
...install,
|
|
355
|
+
});
|
|
356
|
+
out.push(...camoufoxChecks(opts, env, ids, platform));
|
|
357
|
+
const stray = strayLightpandas((opts.procTable ?? readProcTable)());
|
|
358
|
+
if (stray.length)
|
|
359
|
+
out.push({
|
|
360
|
+
area,
|
|
361
|
+
level: "warn",
|
|
362
|
+
text: `Lightpanda pid ${stray.join(", ")} was left running by a server that is gone`,
|
|
363
|
+
fix: FIX,
|
|
364
|
+
auto: true,
|
|
365
|
+
});
|
|
366
|
+
return out;
|
|
367
|
+
}
|
|
368
|
+
/** Camoufox and the stock Playwright it runs on: a failure only when an identity uses the engine. */
|
|
369
|
+
function camoufoxChecks(opts, env, ids, platform) {
|
|
370
|
+
const out = [];
|
|
371
|
+
const area = "engines";
|
|
372
|
+
const users = ids.filter((id) => checkIdentitySettings(id, env).settings?.engine === "camoufox");
|
|
373
|
+
const needed = users.length > 0;
|
|
374
|
+
const who = users.map((u) => `browser:${u}`).join(", ");
|
|
375
|
+
const camoufox = (opts.camoufox ?? findCamoufox)(env);
|
|
376
|
+
const fix = camoufoxInstallHint(platform);
|
|
377
|
+
if (camoufox)
|
|
378
|
+
out.push({
|
|
379
|
+
area,
|
|
380
|
+
level: "ok",
|
|
381
|
+
text: `Camoufox${camoufox.version ? ` (${camoufox.version})` : ""} at ${camoufox.path}`,
|
|
382
|
+
});
|
|
383
|
+
else if (needed)
|
|
384
|
+
out.push({ area, level: "fail", text: `Camoufox not found, and the camoufox engine needs it for ${who}`, fix });
|
|
385
|
+
else
|
|
386
|
+
out.push({
|
|
387
|
+
area,
|
|
388
|
+
level: "info",
|
|
389
|
+
text: "Camoufox not found: only the camoufox engine (--engine camoufox) needs it, and turno never downloads it",
|
|
390
|
+
fix,
|
|
391
|
+
});
|
|
392
|
+
const pw = (opts.playwrightCore ?? findPlaywrightCore)(env);
|
|
393
|
+
const pwFix = playwrightCoreHint();
|
|
394
|
+
if (pw?.ok)
|
|
395
|
+
out.push({ area, level: "ok", text: `playwright-core ${pw.version} at ${pw.dir} (for Camoufox)` });
|
|
396
|
+
else if (pw)
|
|
397
|
+
out.push({
|
|
398
|
+
area,
|
|
399
|
+
level: needed ? "fail" : "warn",
|
|
400
|
+
text: `playwright-core ${pw.version} at ${pw.dir} is not the version patchright-core is built on, so Camoufox cannot run on it`,
|
|
401
|
+
fix: pwFix,
|
|
402
|
+
});
|
|
403
|
+
else if (needed)
|
|
404
|
+
out.push({
|
|
405
|
+
area,
|
|
406
|
+
level: "fail",
|
|
407
|
+
text: `playwright-core not found, and Camoufox runs on stock Playwright (patchright-core drives Chromium only): needed for ${who}`,
|
|
408
|
+
fix: pwFix,
|
|
409
|
+
});
|
|
410
|
+
else if (camoufox)
|
|
411
|
+
out.push({
|
|
412
|
+
area,
|
|
413
|
+
level: "info",
|
|
414
|
+
text: "playwright-core not found: the camoufox engine runs on stock Playwright, since patchright-core drives Chromium only",
|
|
415
|
+
fix: pwFix,
|
|
416
|
+
});
|
|
417
|
+
return out;
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* The opt-in network settings of an identity: where its traffic goes, and whether the secret
|
|
421
|
+
* store holds the proxy credentials (it asks whether the item exists, never reads its value).
|
|
422
|
+
*/
|
|
423
|
+
async function networkRows(id, env, opts) {
|
|
424
|
+
const settings = checkIdentitySettings(id, env).settings;
|
|
425
|
+
const out = [];
|
|
426
|
+
const area = `identity ${id}`;
|
|
427
|
+
if (settings?.userAgent === "coherent")
|
|
428
|
+
out.push({
|
|
429
|
+
area,
|
|
430
|
+
level: "info",
|
|
431
|
+
text: "userAgent coherent: the installed Chrome's own user agent and client hints, with the identity's languages and time zone, checked at every launch",
|
|
432
|
+
});
|
|
433
|
+
const proxy = settings?.proxy;
|
|
434
|
+
if (!proxy)
|
|
435
|
+
return out;
|
|
436
|
+
out.push({
|
|
437
|
+
area,
|
|
438
|
+
level: "info",
|
|
439
|
+
text: `proxy ${proxy.server}: the proxy's operator sees where this identity goes (and the content of plain http:// pages), and sites see the proxy's address`,
|
|
440
|
+
});
|
|
441
|
+
const refs = [proxy.username, proxy.password].filter((r) => r?.startsWith("keychain:") === true);
|
|
442
|
+
if (!refs.length)
|
|
443
|
+
return out;
|
|
444
|
+
const store = opts.secrets ?? secretStore(env);
|
|
445
|
+
if (!store) {
|
|
446
|
+
out.push({
|
|
447
|
+
area,
|
|
448
|
+
level: "fail",
|
|
449
|
+
text: "the proxy credentials are in the secret store, and this machine has none",
|
|
450
|
+
manual: "install libsecret (secret-tool), or use a proxy without a password",
|
|
451
|
+
});
|
|
452
|
+
return out;
|
|
453
|
+
}
|
|
454
|
+
for (const ref of refs) {
|
|
455
|
+
const item = ref.slice("keychain:".length);
|
|
456
|
+
const has = await store.has(item).catch(() => false);
|
|
457
|
+
if (has)
|
|
458
|
+
out.push({ area, level: "ok", text: `proxy credentials: ${store.label} item "${item}" is there` });
|
|
459
|
+
else
|
|
460
|
+
out.push({
|
|
461
|
+
area,
|
|
462
|
+
level: "fail",
|
|
463
|
+
text: `proxy credentials: ${store.label} has no item "${item}", so the browser does not start`,
|
|
464
|
+
fix: `turno secret set ${item}`,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
return out;
|
|
468
|
+
}
|
|
236
469
|
/** Every identity id, including profile dirs with an identity.json only. */
|
|
237
470
|
function identityIds(env) {
|
|
238
471
|
try {
|
|
@@ -284,6 +517,7 @@ async function collect(opts, env, platform, now) {
|
|
|
284
517
|
}
|
|
285
518
|
checks.push(...systemChecks(env, platform));
|
|
286
519
|
const ids = identityIds(env);
|
|
520
|
+
checks.push(...engineChecks(opts, env, ids));
|
|
287
521
|
if (!ids.length)
|
|
288
522
|
checks.push({
|
|
289
523
|
area: "identities",
|
|
@@ -292,12 +526,16 @@ async function collect(opts, env, platform, now) {
|
|
|
292
526
|
fix: "turno identity add browser:<id>",
|
|
293
527
|
});
|
|
294
528
|
for (const id of ids)
|
|
295
|
-
checks.push(...identityChecks(id, env, now));
|
|
529
|
+
checks.push(...identityChecks(id, env, now, opts.servers), ...(await networkRows(id, env, opts)));
|
|
296
530
|
return checks;
|
|
297
531
|
}
|
|
298
|
-
/** Repairs what is safe: stale runtime locks
|
|
299
|
-
async function repair(env) {
|
|
532
|
+
/** Repairs what is safe: stale runtime locks, a Chrome left running with no server, a stray Lightpanda. */
|
|
533
|
+
async function repair(env, opts) {
|
|
300
534
|
const done = [];
|
|
535
|
+
for (const pid of strayLightpandas((opts.procTable ?? readProcTable)())) {
|
|
536
|
+
signalProcess(pid, "SIGTERM");
|
|
537
|
+
done.push(`stopped Lightpanda pid ${pid}, left running by a server that is gone`);
|
|
538
|
+
}
|
|
301
539
|
for (const id of identityIds(env)) {
|
|
302
540
|
const lock = readRuntimeLock(id, env);
|
|
303
541
|
if (lock && !runtimeLockAlive(lock)) {
|
|
@@ -319,13 +557,33 @@ export async function runDoctor(opts = {}) {
|
|
|
319
557
|
let checks = await collect(opts, env, platform, now);
|
|
320
558
|
const fixed = [];
|
|
321
559
|
if (opts.fix && checks.some((c) => c.auto && (c.level === "warn" || c.level === "fail"))) {
|
|
322
|
-
fixed.push(...(await repair(env)));
|
|
560
|
+
fixed.push(...(await repair(env, opts)));
|
|
323
561
|
checks = await collect({ ...opts, launch: false }, env, platform, now).then((again) => {
|
|
324
562
|
// Keep the smoke launch result of the first pass: it does not change with a repair.
|
|
325
563
|
const smoke = checks.find((c) => c.area === "chrome" && /^smoke launch/.test(c.text));
|
|
326
564
|
return smoke ? again.map((c) => (c.area === "chrome" && /^smoke launch/.test(c.text) ? smoke : c)) : again;
|
|
327
565
|
});
|
|
328
566
|
}
|
|
329
|
-
return { checks, fixed };
|
|
567
|
+
return { checks: checks.map(named), fixed };
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* A check's fix and text with the commands named the way this process's host types them: the
|
|
571
|
+
* checks are written with turno's names (the troubleshooting docs are generated from them), and
|
|
572
|
+
* turno-browser on its own prints `turno-browser set <id> ...` instead.
|
|
573
|
+
*/
|
|
574
|
+
function named(check) {
|
|
575
|
+
const n = names();
|
|
576
|
+
if (n.browser === "turno browser")
|
|
577
|
+
return check;
|
|
578
|
+
const swap = (s) => s
|
|
579
|
+
.replace(/turno identity (\w+) browser:(\S+)/g, (_m, sub, id) => n.identity(sub, id))
|
|
580
|
+
.replace(/turno secret set (\S+)/g, (_m, item) => n.secretSet(item))
|
|
581
|
+
.replace(/turno browser /g, `${n.browser} `);
|
|
582
|
+
return {
|
|
583
|
+
...check,
|
|
584
|
+
text: swap(check.text),
|
|
585
|
+
...(check.fix ? { fix: swap(check.fix) } : {}),
|
|
586
|
+
...(check.manual ? { manual: swap(check.manual) } : {}),
|
|
587
|
+
};
|
|
330
588
|
}
|
|
331
589
|
//# sourceMappingURL=doctor.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Engine } from "./engine.js";
|
|
2
|
+
export type AttachTarget = {
|
|
3
|
+
kind: "auto";
|
|
4
|
+
} | {
|
|
5
|
+
kind: "dir";
|
|
6
|
+
dir: string;
|
|
7
|
+
} | {
|
|
8
|
+
kind: "endpoint";
|
|
9
|
+
url: string;
|
|
10
|
+
};
|
|
11
|
+
/** The target an `attach` value names, or a message saying what is wrong with it. */
|
|
12
|
+
export declare function parseAttachTarget(value: string): AttachTarget | {
|
|
13
|
+
error: string;
|
|
14
|
+
};
|
|
15
|
+
/** The user data dir of the installed Chrome's default profile, where Chrome writes `DevToolsActivePort`. */
|
|
16
|
+
export declare function defaultChromeUserDataDir(env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform): string;
|
|
17
|
+
/** `DevToolsActivePort`: the port on the first line, the browser's WebSocket path on the second. */
|
|
18
|
+
export declare function parseDevToolsActivePort(text: string): {
|
|
19
|
+
port: number;
|
|
20
|
+
path: string;
|
|
21
|
+
} | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Where to connect for a target, without connecting: for `auto` and a dir, the endpoint in its
|
|
24
|
+
* `DevToolsActivePort`. Throws with what the user should do when Chrome does not listen.
|
|
25
|
+
*/
|
|
26
|
+
export declare function resolveAttachEndpoint(target: AttachTarget, env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform): string;
|
|
27
|
+
export declare function createAttachEngine(target: AttachTarget): Engine;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { isAbsolute, join } from "node:path";
|
|
3
|
+
import { browserPaths as paths } from "turno-core/paths";
|
|
4
|
+
import { IdleTimer, idleMs } from "./idle-timer.js";
|
|
5
|
+
import { playwright, upstream } from "./upstream.js";
|
|
6
|
+
/**
|
|
7
|
+
* Attach mode: the `attach` engine. Instead of launching a Chrome, turno connects over CDP to
|
|
8
|
+
* the Chrome the user already has open, so the agent works in the user's real, signed-in
|
|
9
|
+
* browser. Opt-in only, per identity (identity.json `attach`) or per server (`--attach`).
|
|
10
|
+
*
|
|
11
|
+
* How Chrome allows it today: since Chrome 136, `--remote-debugging-port` is ignored on the
|
|
12
|
+
* default user data dir. Since Chrome 144 the user can turn on remote debugging for the running
|
|
13
|
+
* browser at chrome://inspect/#remote-debugging; Chrome then listens on 127.0.0.1, writes the
|
|
14
|
+
* port and path to `DevToolsActivePort` in its user data dir, and asks the user to allow each new
|
|
15
|
+
* connection. That dialog is the consent step: turno waits for it (`CONNECT_TIMEOUT_MS`) and
|
|
16
|
+
* cannot answer it.
|
|
17
|
+
*
|
|
18
|
+
* The target (`parseAttachTarget`) is `auto` (the default Chrome profile of this computer),
|
|
19
|
+
* the absolute path of a Chrome user data dir (Beta, Canary, a profile started with
|
|
20
|
+
* `--remote-debugging-port` and its own `--user-data-dir`), or a loopback `http://` or `ws://`
|
|
21
|
+
* endpoint.
|
|
22
|
+
*
|
|
23
|
+
* turno never closes the user's browser or a tab it did not open:
|
|
24
|
+
*
|
|
25
|
+
* - at connect it opens one tab of its own and makes it the agent's current tab, so a first
|
|
26
|
+
* `browser_navigate` never replaces a page of the user's;
|
|
27
|
+
* - tabs that open during a tool call count as the agent's (`own`); tabs that were open before,
|
|
28
|
+
* or that the user opens between calls, are the user's;
|
|
29
|
+
* - closing (`browser_close`, the session ending, the idle limit) closes the agent's tabs that
|
|
30
|
+
* are still open and disconnects: Playwright's `Browser.close` on a CDP connection only drops
|
|
31
|
+
* the connection, it does not close Chrome (checked with Chrome 157);
|
|
32
|
+
* - the host's tab governor touches only the agent's tabs, and never closes a popup;
|
|
33
|
+
* - no runtime lock, orphan sweep or shutdown signal reaches this Chrome: those act on the
|
|
34
|
+
* identity's own profile and the Chrome turno started on it, which attach mode never starts.
|
|
35
|
+
*/
|
|
36
|
+
/** How long to wait for the connection, which includes the user answering Chrome's dialog. */
|
|
37
|
+
const CONNECT_TIMEOUT_MS = 60_000;
|
|
38
|
+
const LOOPBACK = new Set(["127.0.0.1", "localhost", "[::1]"]);
|
|
39
|
+
/** The target an `attach` value names, or a message saying what is wrong with it. */
|
|
40
|
+
export function parseAttachTarget(value) {
|
|
41
|
+
const v = value.trim();
|
|
42
|
+
if (v === "auto")
|
|
43
|
+
return { kind: "auto" };
|
|
44
|
+
if (isAbsolute(v))
|
|
45
|
+
return { kind: "dir", dir: v };
|
|
46
|
+
if (/^(https?|wss?):\/\//.test(v)) {
|
|
47
|
+
let url;
|
|
48
|
+
try {
|
|
49
|
+
url = new URL(v);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return { error: `attach "${v}" is not a URL` };
|
|
53
|
+
}
|
|
54
|
+
if (!LOOPBACK.has(url.hostname))
|
|
55
|
+
return { error: `attach "${v}" must be on this computer (127.0.0.1, localhost or [::1])` };
|
|
56
|
+
return { kind: "endpoint", url: v };
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
error: `attach must be "auto", the absolute path of a Chrome user data dir, or a loopback http:// or ws:// endpoint, not "${v}"`,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** The user data dir of the installed Chrome's default profile, where Chrome writes `DevToolsActivePort`. */
|
|
63
|
+
export function defaultChromeUserDataDir(env = process.env, platform = process.platform) {
|
|
64
|
+
const home = paths(env).home;
|
|
65
|
+
if (platform === "darwin")
|
|
66
|
+
return join(home, "Library", "Application Support", "Google", "Chrome");
|
|
67
|
+
if (platform === "win32")
|
|
68
|
+
return join(env.LOCALAPPDATA ?? join(home, "AppData", "Local"), "Google", "Chrome", "User Data");
|
|
69
|
+
return join(env.XDG_CONFIG_HOME || join(home, ".config"), "google-chrome");
|
|
70
|
+
}
|
|
71
|
+
/** `DevToolsActivePort`: the port on the first line, the browser's WebSocket path on the second. */
|
|
72
|
+
export function parseDevToolsActivePort(text) {
|
|
73
|
+
const [rawPort, rawPath] = text
|
|
74
|
+
.split("\n")
|
|
75
|
+
.map((l) => l.trim())
|
|
76
|
+
.filter(Boolean);
|
|
77
|
+
const port = Number(rawPort);
|
|
78
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65535 || !rawPath?.startsWith("/"))
|
|
79
|
+
return undefined;
|
|
80
|
+
return { port, path: rawPath };
|
|
81
|
+
}
|
|
82
|
+
const ENABLE = "open chrome://inspect/#remote-debugging in that Chrome and turn remote debugging on";
|
|
83
|
+
/**
|
|
84
|
+
* Where to connect for a target, without connecting: for `auto` and a dir, the endpoint in its
|
|
85
|
+
* `DevToolsActivePort`. Throws with what the user should do when Chrome does not listen.
|
|
86
|
+
*/
|
|
87
|
+
export function resolveAttachEndpoint(target, env = process.env, platform = process.platform) {
|
|
88
|
+
if (target.kind === "endpoint")
|
|
89
|
+
return target.url;
|
|
90
|
+
const dir = target.kind === "dir" ? target.dir : defaultChromeUserDataDir(env, platform);
|
|
91
|
+
let text;
|
|
92
|
+
try {
|
|
93
|
+
text = readFileSync(join(dir, "DevToolsActivePort"), "utf8");
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
throw new Error(`attach: no Chrome with remote debugging on for ${dir} (no DevToolsActivePort). Start Chrome, then ${ENABLE}.`);
|
|
97
|
+
}
|
|
98
|
+
const parsed = parseDevToolsActivePort(text);
|
|
99
|
+
if (!parsed)
|
|
100
|
+
throw new Error(`attach: ${join(dir, "DevToolsActivePort")} is not a port and a path`);
|
|
101
|
+
return `ws://127.0.0.1:${parsed.port}${parsed.path}`;
|
|
102
|
+
}
|
|
103
|
+
export function createAttachEngine(target) {
|
|
104
|
+
return {
|
|
105
|
+
id: "attach",
|
|
106
|
+
refuse(tool) {
|
|
107
|
+
// Arbitrary Playwright code in the user's own browser reaches every site it is signed in to.
|
|
108
|
+
if (tool === "browser_run_code_unsafe")
|
|
109
|
+
return "browser_run_code_unsafe is refused in attach mode: it would run arbitrary code against your own Chrome and every site it is signed in to. Use the other browser tools.";
|
|
110
|
+
return undefined;
|
|
111
|
+
},
|
|
112
|
+
async launch({ config, identity }) {
|
|
113
|
+
const env = identity?.env ?? process.env;
|
|
114
|
+
const endpoint = resolveAttachEndpoint(target, env);
|
|
115
|
+
let browser;
|
|
116
|
+
try {
|
|
117
|
+
browser = await playwright().chromium.connectOverCDP(endpoint, { timeout: CONNECT_TIMEOUT_MS });
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
const first = error.message.split("\n")[0];
|
|
121
|
+
throw new Error(`attach: could not connect to Chrome at ${endpoint.replace(/\/devtools\/browser\/.*/, "")} (${first}). ` +
|
|
122
|
+
`Chrome asks you to allow each new connection: click Allow in its dialog. If it did not ask, ${ENABLE}.`);
|
|
123
|
+
}
|
|
124
|
+
const context = browser.contexts()[0];
|
|
125
|
+
if (!context) {
|
|
126
|
+
await browser.close().catch(() => { });
|
|
127
|
+
throw new Error("attach: that Chrome has no default browser context to work in");
|
|
128
|
+
}
|
|
129
|
+
const owned = new Set();
|
|
130
|
+
let closing = false;
|
|
131
|
+
const close = async () => {
|
|
132
|
+
closing = true;
|
|
133
|
+
upstream().core.utils.gracefullyCloseSet.delete(close);
|
|
134
|
+
idleTimer?.dispose();
|
|
135
|
+
for (const page of owned)
|
|
136
|
+
if (!page.isClosed())
|
|
137
|
+
await page.close().catch(() => { });
|
|
138
|
+
owned.clear();
|
|
139
|
+
// On a CDP connection this disconnects; Chrome and the user's tabs stay.
|
|
140
|
+
await browser.close().catch(() => { });
|
|
141
|
+
};
|
|
142
|
+
// The exit watchdog awaits this before the process exits, so the agent's tabs close then too.
|
|
143
|
+
upstream().core.utils.gracefullyCloseSet.add(close);
|
|
144
|
+
const ms = idleMs(config);
|
|
145
|
+
let idleTimer;
|
|
146
|
+
if (ms) {
|
|
147
|
+
idleTimer = new IdleTimer(ms, () => void close());
|
|
148
|
+
browser.once("disconnected", () => idleTimer?.dispose());
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
engine: "attach",
|
|
152
|
+
context,
|
|
153
|
+
userBrowser: true,
|
|
154
|
+
own(page) {
|
|
155
|
+
owned.add(page);
|
|
156
|
+
page.once("close", () => owned.delete(page));
|
|
157
|
+
},
|
|
158
|
+
...(idleTimer ? { idleTimer } : {}),
|
|
159
|
+
isOpen: () => !closing && browser.isConnected(),
|
|
160
|
+
close,
|
|
161
|
+
};
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=engine-attach.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Engine } from "./engine.js";
|
|
2
|
+
/** Why the Camoufox engine refuses this tool, or undefined when it runs it. */
|
|
3
|
+
export declare function camoufoxRefusal(tool: string): string | undefined;
|
|
4
|
+
/** How to install Camoufox, for errors and `turno browser doctor`. */
|
|
5
|
+
export declare function camoufoxInstallHint(platform?: NodeJS.Platform): string;
|
|
6
|
+
/** Where Camoufox's own installer keeps its builds (platformdirs' user cache folder). */
|
|
7
|
+
export declare function camoufoxCacheDir(env: NodeJS.ProcessEnv, platform?: NodeJS.Platform): string;
|
|
8
|
+
export interface CamoufoxInfo {
|
|
9
|
+
path: string;
|
|
10
|
+
version?: string;
|
|
11
|
+
}
|
|
12
|
+
/** The Camoufox turno would run: `TURNO_CAMOUFOX_PATH`, else the newest build in Camoufox's cache folder. */
|
|
13
|
+
export declare function findCamoufox(env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform): CamoufoxInfo | undefined;
|
|
14
|
+
/** The minor version of Playwright that patchright-core is built on, like "1.64". */
|
|
15
|
+
export declare function playwrightMinor(): string;
|
|
16
|
+
export interface PlaywrightCoreInfo {
|
|
17
|
+
dir: string;
|
|
18
|
+
version: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The stock `playwright-core` this engine would load: `TURNO_PLAYWRIGHT_CORE` (its package
|
|
22
|
+
* folder), else the one Node finds from turno-browser. Undefined when there is none; `ok` is
|
|
23
|
+
* false when its minor version is not patchright-core's.
|
|
24
|
+
*/
|
|
25
|
+
export declare function findPlaywrightCore(env?: NodeJS.ProcessEnv): (PlaywrightCoreInfo & {
|
|
26
|
+
ok: boolean;
|
|
27
|
+
}) | undefined;
|
|
28
|
+
/** How to install the stock Playwright this engine needs. */
|
|
29
|
+
export declare function playwrightCoreHint(): string;
|
|
30
|
+
/** Where an identity's Camoufox keeps its Firefox profile. */
|
|
31
|
+
export declare function camoufoxProfileDir(id: string, env: NodeJS.ProcessEnv): string;
|
|
32
|
+
export declare function createCamoufoxEngine(): Engine;
|