jevway 0.2.0 → 0.3.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.
Files changed (41) hide show
  1. package/README.md +37 -2
  2. package/{INSTALL-FOR-AGENTS.md → SETUP.md} +94 -14
  3. package/dist/browser/downloads.js +103 -0
  4. package/dist/browser/frames.js +21 -1
  5. package/dist/browser/snapshot.js +53 -0
  6. package/dist/browser/tab.js +59 -10
  7. package/dist/cli/dispatch.js +2 -0
  8. package/dist/cli/inbox-command.js +199 -0
  9. package/dist/cli/look-command.js +10 -3
  10. package/dist/cli/run-command.js +51 -3
  11. package/dist/daemon/server.js +22 -3
  12. package/dist/inbox/inbox-file.js +43 -0
  13. package/dist/inbox/inbox-registry.js +33 -0
  14. package/dist/inbox/inbox.js +127 -0
  15. package/dist/inbox/mail-api.js +52 -0
  16. package/dist/inbox/message.js +34 -0
  17. package/dist/jev/secrets.js +4 -3
  18. package/dist/mcp/inbox-tools.js +82 -0
  19. package/dist/mcp/instructions.js +11 -2
  20. package/dist/mcp/server.js +15 -8
  21. package/dist/mcp/task-tools.js +24 -8
  22. package/dist/notify/desktop.js +40 -0
  23. package/dist/notify/person-messages.js +50 -0
  24. package/dist/output/layout.js +37 -1
  25. package/dist/output/manifest.js +6 -0
  26. package/dist/output/run-folder.js +44 -2
  27. package/dist/recording/describe-step.js +3 -0
  28. package/dist/session/session-service.js +31 -11
  29. package/dist/setup/skill-command.js +44 -21
  30. package/dist/task/ask-user.js +182 -0
  31. package/dist/task/downloads.js +63 -0
  32. package/dist/task/person-wait.js +104 -0
  33. package/dist/task/present.js +32 -11
  34. package/dist/task/run-task.js +265 -28
  35. package/dist/task/start-task.js +49 -6
  36. package/dist/task/totp.js +124 -0
  37. package/dist/task/upload-files.js +237 -5
  38. package/package.json +2 -2
  39. package/skill/jevway/SKILL.md +5 -2
  40. package/skill/jevway-inbox/SKILL.md +61 -0
  41. package/skill/jevway-inbox/scripts/inbox.mjs +209 -0
@@ -2,8 +2,10 @@ import { randomUUID } from 'node:crypto';
2
2
  import { join, resolve } from 'node:path';
3
3
  import { scrubberFor } from '../jev/secrets.js';
4
4
  import { ensureOutRoot, journeyFolderName, outRoot, runFolderName } from '../output/layout.js';
5
+ import { writeLookPdf, writeRunPdf } from '../output/run-folder.js';
5
6
  import { lookAt } from '../task/look.js';
6
7
  import { storyLine } from '../task/present.js';
8
+ import { checkTotpSecrets, totpMasks } from '../task/totp.js';
7
9
  /** A session id the service does not hold: it ended, expired, or never existed. `code` is for structured CLI errors. */
8
10
  export class UnknownSession extends Error {
9
11
  code = 'unknown_session';
@@ -49,11 +51,14 @@ export const createSessionService = (runner, options = {}) => {
49
51
  const id = newId();
50
52
  const at = now();
51
53
  const values = request.values ?? {};
52
- const secretKeys = request.secretKeys ?? [];
53
- const scrub = scrubberFor(values, secretKeys);
54
+ // An authenticator key is secret for the session, even if a later call gives it a plain value.
55
+ const secretKeys = [...(request.secretKeys ?? []), ...Object.keys(request.totp ?? {})];
56
+ // A bad authenticator secret is refused before a tab opens.
57
+ const moreSecrets = totpMasks(checkTotpSecrets(request.totp, request.values));
58
+ const scrub = scrubberFor(values, secretKeys, moreSecrets);
54
59
  const runDir = recordDir ? resolve(cwd, recordDir) : join(ensureOutRoot(root), 'runs', runFolderName({ at, goal: request.goal, id, scrub }));
55
60
  const task = await runner.start({ ...request, output: { dir: runDir, record: Boolean(record || recordDir) } });
56
- const entry = { task, ownsTab: Boolean(request.url), busy: false, goal: scrub(request.goal), values, secretKeys, runDir, startedAt: at, lastUsedAt: at };
61
+ const entry = { task, ownsTab: Boolean(request.url), busy: false, goal: scrub(request.goal), values, secretKeys, moreSecrets, runDir, startedAt: at, lastUsedAt: at };
57
62
  sessions.set(id, entry);
58
63
  return { id, entry };
59
64
  };
@@ -66,18 +71,21 @@ export const createSessionService = (runner, options = {}) => {
66
71
  values: request.values,
67
72
  secretKeys: request.secretKeys,
68
73
  files: request.files,
74
+ totp: request.totp,
69
75
  doneWhen: request.doneWhen,
70
76
  checks: request.checks,
71
77
  maxSteps: request.maxSteps,
72
78
  maxSeconds: request.maxSeconds,
79
+ waitForPersonSeconds: request.waitForPersonSeconds,
73
80
  allowedHosts: request.allowedHosts,
74
81
  blockedHosts: request.blockedHosts,
75
82
  confirmIrreversible: request.confirmIrreversible,
76
83
  confirmTarget: request.confirmTarget,
77
84
  });
78
85
  entry.values = { ...entry.values, ...request.values };
79
- entry.secretKeys = [...new Set([...entry.secretKeys, ...(request.secretKeys ?? [])])];
80
- entry.goal = scrubberFor(entry.values, entry.secretKeys)(request.goal);
86
+ entry.secretKeys = [...new Set([...entry.secretKeys, ...(request.secretKeys ?? []), ...Object.keys(request.totp ?? {})])];
87
+ entry.moreSecrets.push(...totpMasks(checkTotpSecrets(request.totp)));
88
+ entry.goal = scrubberFor(entry.values, entry.secretKeys, entry.moreSecrets)(request.goal);
81
89
  return { id, entry };
82
90
  };
83
91
  const journeysRoot = () => env.JEVWAY_JOURNEYS_DIR ? resolve(cwd, env.JEVWAY_JOURNEYS_DIR) : join(ensureOutRoot(root), 'journeys');
@@ -106,7 +114,11 @@ export const createSessionService = (runner, options = {}) => {
106
114
  };
107
115
  tell({ type: 'started', session: id });
108
116
  entry.busy = true;
109
- const running = entry.task.run({ signal, onStep: (step, n, url) => tell({ type: 'step', session: id, n, story: storyLine(step), url }) });
117
+ const running = entry.task.run({
118
+ signal,
119
+ onStep: (step, n, url) => tell({ type: 'step', session: id, n, story: storyLine(step), url }),
120
+ onWait: ({ message, seconds_left }, url) => tell({ type: 'waiting', session: id, message, seconds_left, url }),
121
+ });
110
122
  entry.running = running.catch(() => undefined);
111
123
  try {
112
124
  const summary = await running;
@@ -121,13 +133,21 @@ export const createSessionService = (runner, options = {}) => {
121
133
  entry.lastUsedAt = now();
122
134
  }
123
135
  },
124
- /** Reads a page with no model call and no input: a session's tab (its secret values masked), an open tab by URL part, or a URL opened and closed again. */
125
- async look({ session, tab, url }) {
136
+ /**
137
+ * Reads a page with no model call and no input: a session's tab (its secret values masked), an open tab by URL part, or
138
+ * a URL opened and closed again. With `pdf`, the page is saved as a PDF too: in the session's run folder
139
+ * (`looks/NN-<title>.pdf`), else in a look folder of its own (`<root>/looks/<stamp>_<host>_<id4>/page.pdf`).
140
+ */
141
+ async look({ session, tab, url, pdf }) {
126
142
  if ([session, tab, url].filter(Boolean).length !== 1)
127
143
  throw new Error('Pass exactly one of session, tab or url.');
128
- if (session)
129
- return lookAt(await findTask(session).task.look());
130
- return lookAt(await runner.look({ url, tab }));
144
+ if (session) {
145
+ const entry = findTask(session);
146
+ const looked = await entry.task.look({ pdf });
147
+ return { ...lookAt(looked), ...(looked.pdf && { pdf: writeRunPdf({ dir: entry.runDir, pdf: looked.pdf, title: looked.title }) }) };
148
+ }
149
+ const looked = await runner.look({ url, tab, pdf });
150
+ return { ...lookAt(looked), ...(looked.pdf && { pdf: writeLookPdf({ root, pdf: looked.pdf, url: looked.url, at: now(), id: newId() }) }) };
131
151
  },
132
152
  /** Forgets a session and closes the tabs it opened (its first tab only when it started from `url`), unless `keepTab`. */
133
153
  async end(session, { keepTab = false } = {}) {
@@ -7,17 +7,22 @@ export const SKILL_USAGE = `Examples:
7
7
  jevway skill
8
8
  jevway skill --install ~/.claude/skills
9
9
 
10
- jevway skill print the SKILL.md this version ships
11
- jevway skill --install <dir> [--force] copy the skill folder into <dir>/jevway
10
+ jevway skill [--name jevway-inbox] print the SKILL.md this version ships (default jevway)
11
+ jevway skill --install <dir> [--force] copy both skill folders into <dir>/jevway and <dir>/jevway-inbox
12
12
 
13
13
  Examples:
14
14
  jevway skill --install ~/.claude/skills
15
15
  jevway skill --install ~/.codex/skills
16
16
 
17
- --install prints { ok, status: installed|unchanged|replaced, path, files }. A different jevway skill already in
18
- <dir>/jevway (or a symlink to one) is left alone unless --force is given.`;
19
- /** The `skill/jevway/` folder of the installed package: two levels up from both `src/setup/` and `dist/setup/`. */
20
- export const bundledSkillDir = () => fileURLToPath(new URL('../../skill/jevway/', import.meta.url));
17
+ --install prints { ok, status: installed|unchanged|replaced, path, files } for jevway and the same for each skill in
18
+ skills: [{ name, … }]. jevway-inbox is a standalone disposable inbox script for agents without jevway. A different
19
+ skill already in either folder (or a symlink to one) is left alone, and nothing is copied, unless --force is given.`;
20
+ /** The skills this package ships, by folder name; `--install` copies them all. */
21
+ const BUNDLED_SKILLS = ['jevway', 'jevway-inbox'];
22
+ /** A `skill/<name>/` folder of the installed package: two levels up from both `src/setup/` and `dist/setup/`. */
23
+ const bundledSkillFolder = (name) => fileURLToPath(new URL(`../../skill/${name}/`, import.meta.url));
24
+ /** The `skill/jevway/` folder of the installed package. */
25
+ export const bundledSkillDir = () => bundledSkillFolder('jevway');
21
26
  /** Every file under `dir`, as sorted paths relative to it. */
22
27
  const filesIn = (dir) => readdirSync(dir, { recursive: true, withFileTypes: true })
23
28
  .filter((entry) => entry.isFile())
@@ -34,39 +39,52 @@ const sameFolder = (source, target) => {
34
39
  return false;
35
40
  }
36
41
  };
37
- /** Whether `<parent>/jevway` is missing, the same as `source`, or something else (a different skill or a symlink to one). */
38
- export const skillState = (source, parent) => {
39
- const path = join(parent, 'jevway');
42
+ /** Whether `<parent>/<name>` is missing, the same as `source`, or something else (a different skill or a symlink to one). */
43
+ export const skillState = (source, parent, name = 'jevway') => {
44
+ const path = join(parent, name);
40
45
  if (!existsSync(path) && !lstatSync(path, { throwIfNoEntry: false })?.isSymbolicLink())
41
46
  return 'missing';
42
47
  return sameFolder(source, path) ? 'same' : 'different';
43
48
  };
44
49
  /**
45
- * Copies `source` to `<parent>/jevway`. An identical copy is left as it is; a different folder or a symlink is
46
- * replaced only with `force` (a symlink is removed, never its target). The copy lands under a temporary name first,
47
- * so an interrupted install never leaves half a skill.
50
+ * Copies `source` to `<parent>/<name>` (default `jevway`). An identical copy is left as it is; a different folder or a
51
+ * symlink is replaced only with `force` (a symlink is removed, never its target). The copy lands under a temporary
52
+ * name first, so an interrupted install never leaves half a skill.
48
53
  *
49
54
  * @example
50
55
  * installSkill(bundledSkillDir(), '/Users/ada/.claude/skills', { force: false })
51
56
  * // { status: 'installed', path: '/Users/ada/.claude/skills/jevway', files: ['SKILL.md'] }
52
57
  */
53
- export const installSkill = (source, parent, { force }) => {
54
- const path = join(parent, 'jevway');
58
+ export const installSkill = (source, parent, { force, name = 'jevway' }) => {
59
+ const path = join(parent, name);
55
60
  const files = filesIn(source);
56
- const state = skillState(source, parent);
61
+ const state = skillState(source, parent, name);
57
62
  if (state === 'same')
58
63
  return { status: 'unchanged', path, files };
59
64
  if (state === 'different' && !force)
60
65
  return { status: 'exists', path };
61
66
  const present = state === 'different';
62
67
  mkdirSync(parent, { recursive: true });
63
- const incoming = join(parent, `.jevway-${process.pid}.incoming`);
68
+ const incoming = join(parent, `.${name}-${process.pid}.incoming`);
64
69
  cpSync(source, incoming, { recursive: true });
65
70
  if (present)
66
71
  rmSync(path, { recursive: true, force: true });
67
72
  renameSync(incoming, path);
68
73
  return { status: present ? 'replaced' : 'installed', path, files };
69
74
  };
75
+ /**
76
+ * Installs every bundled skill into `parent`, or none: a different folder for any of them, without `force`, is
77
+ * `exists` with its path and nothing is copied.
78
+ */
79
+ export const installSkills = (parent, { force }) => {
80
+ const different = BUNDLED_SKILLS.filter((name) => skillState(bundledSkillFolder(name), parent, name) === 'different');
81
+ if (different.length && !force)
82
+ return { status: 'exists', paths: different.map((name) => join(parent, name)) };
83
+ return {
84
+ status: 'done',
85
+ skills: BUNDLED_SKILLS.map((name) => ({ name, ...installSkill(bundledSkillFolder(name), parent, { force, name }) })),
86
+ };
87
+ };
70
88
  /**
71
89
  * `jevway skill`: prints the bundled SKILL.md, or with `--install <dir>` copies the skill folder into `<dir>/jevway`.
72
90
  *
@@ -78,7 +96,7 @@ export const runSkill = async (argv) => {
78
96
  try {
79
97
  ({ values: options } = parseArgs({
80
98
  args: argv,
81
- options: { install: { type: 'string' }, force: { type: 'boolean' }, help: { type: 'boolean', short: 'h' } },
99
+ options: { install: { type: 'string' }, name: { type: 'string' }, force: { type: 'boolean' }, help: { type: 'boolean', short: 'h' } },
82
100
  }));
83
101
  }
84
102
  catch (error) {
@@ -89,13 +107,18 @@ export const runSkill = async (argv) => {
89
107
  return 0;
90
108
  }
91
109
  if (options.install === undefined) {
92
- await print(readFileSync(join(bundledSkillDir(), 'SKILL.md'), 'utf8'));
110
+ const name = options.name ?? 'jevway';
111
+ if (!BUNDLED_SKILLS.includes(name)) {
112
+ return printError(2, 'usage', `jevway skill: --name must be ${BUNDLED_SKILLS.join(' or ')}, got "${name}"`, 'jevway skill --name jevway-inbox');
113
+ }
114
+ await print(readFileSync(join(bundledSkillFolder(name), 'SKILL.md'), 'utf8'));
93
115
  return 0;
94
116
  }
95
- const result = installSkill(bundledSkillDir(), options.install, { force: Boolean(options.force) });
117
+ const result = installSkills(options.install, { force: Boolean(options.force) });
96
118
  if (result.status === 'exists') {
97
- return printError(1, 'skill_exists', `${result.path} already holds a different jevway skill (or a link to one); it was left as it is`, `Run again with --force to replace it with this version's skill: jevway skill --install ${options.install} --force`);
119
+ return printError(1, 'skill_exists', `${result.paths.join(' and ')} already ${result.paths.length === 1 ? 'holds a different skill' : 'hold different skills'} (or a link to one); nothing was copied`, `Run again with --force to replace them with this version's skills: jevway skill --install ${options.install} --force`);
98
120
  }
99
- await printJson({ ok: true, ...result });
121
+ const [{ name: _name, ...jevway }] = result.skills;
122
+ await printJson({ ok: true, ...jevway, skills: result.skills });
100
123
  return 0;
101
124
  };
@@ -0,0 +1,182 @@
1
+ const LABEL_CHARS = 60;
2
+ const OPTION_CHARS = 40;
3
+ const HEADER_CHARS = 12;
4
+ const MAX_OPTIONS = 4;
5
+ const MAX_LISTED_FIELDS = 4;
6
+ // Below this, a candidate is noise rather than a real alternative to offer.
7
+ const MIN_CANDIDATE_PROBABILITY = 0.05;
8
+ const SECRET_LABEL = /\b(password|passcode|passphrase|pin|token|api[ _-]?key|secret|cvc|cvv|security code|card number|one[- ]time|otp|2fa)\b/i;
9
+ /** How a bot check's `wall.name` reads in a question. */
10
+ export const BOT_CHECK_NAMES = {
11
+ cloudflare: 'Cloudflare',
12
+ recaptcha: 'reCAPTCHA',
13
+ hcaptcha: 'hCaptcha',
14
+ turnstile: 'Turnstile',
15
+ 'unusual-traffic': "Google's unusual-traffic",
16
+ };
17
+ const DONE_OR_SKIP = [
18
+ { label: 'Done', description: 'I did it; continue' },
19
+ { label: 'Skip', description: 'Leave it; stop here' },
20
+ ];
21
+ const AFTER_WALL = 'On Done, call again with this session and the same `goal`; on Skip, tell the user the step was not done.';
22
+ /** Page text made safe to quote: one line, no control characters or backticks, double quotes as single, cut with `…`. */
23
+ const quotable = (text, max = LABEL_CHARS) => {
24
+ const line = text
25
+ .replace(/[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e\u2066-\u2069`]/g, ' ')
26
+ .replace(/"/g, "'")
27
+ .replace(/\s+/g, ' ')
28
+ .trim();
29
+ return line.length > max ? `${line.slice(0, max - 1).trimEnd()}…` : line;
30
+ };
31
+ const headerOf = (label) => {
32
+ const words = label.split(' ');
33
+ let header = '';
34
+ for (const word of words) {
35
+ const longer = header ? `${header} ${word}` : word;
36
+ if (longer.length > HEADER_CHARS)
37
+ break;
38
+ header = longer;
39
+ }
40
+ return header || label.slice(0, HEADER_CHARS);
41
+ };
42
+ const slugOf = (label) => label
43
+ .normalize('NFKD')
44
+ .replace(/[\u0300-\u036f]/g, '')
45
+ .toLowerCase()
46
+ .replace(/\[secret:[^\]]*\]/g, ' ')
47
+ .replace(/[^a-z0-9]+/g, '_')
48
+ .replace(/^_+|_+$/g, '')
49
+ .slice(0, 40)
50
+ .replace(/_+$/, '');
51
+ /** One `values` key per label: its slug, `value_<n>` when it has none, `_2`… when taken. */
52
+ const keysFor = (labels) => {
53
+ const taken = new Set();
54
+ return labels.map((label, i) => {
55
+ const base = slugOf(label) || `value_${i + 1}`;
56
+ let key = base;
57
+ for (let n = 2; taken.has(key); n++)
58
+ key = `${base}_${n}`;
59
+ taken.add(key);
60
+ return key;
61
+ });
62
+ };
63
+ const listed = (quoted) => {
64
+ const shown = quoted.length > MAX_LISTED_FIELDS ? [...quoted.slice(0, MAX_LISTED_FIELDS - 1), `${quoted.length - MAX_LISTED_FIELDS + 1} more`] : quoted;
65
+ return shown.length === 1 ? shown[0] : `${shown.slice(0, -1).join(', ')} and ${shown.at(-1)}`;
66
+ };
67
+ const distinct = (labels) => [...new Set(labels.filter(Boolean))];
68
+ const pageOf = ({ title, url }) => {
69
+ if (title.trim())
70
+ return `"${quotable(title)}"`;
71
+ try {
72
+ const { host, pathname } = new URL(url);
73
+ return quotable(`${host}${pathname === '/' ? '' : pathname}`);
74
+ }
75
+ catch {
76
+ return quotable(url);
77
+ }
78
+ };
79
+ const askForMissing = (missing) => {
80
+ const labels = missing.map(({ label }) => quotable(label.replace(/ \(appears on hover\)$/, '')) || 'the field');
81
+ const keys = keysFor(labels);
82
+ const fields = missing.map(({ options }, i) => {
83
+ const choices = distinct((options ?? []).map((option) => quotable(option, OPTION_CHARS)));
84
+ return {
85
+ key: keys[i],
86
+ label: labels[i],
87
+ ...(choices.length && { options: choices }),
88
+ ...(SECRET_LABEL.test(labels[i]) && { secret: true }),
89
+ };
90
+ });
91
+ const [only] = fields;
92
+ const single = fields.length === 1;
93
+ const choices = single ? (only.options ?? []) : [];
94
+ const secretKeys = fields.filter((field) => field.secret).map((field) => `"${field.key}"`);
95
+ return {
96
+ question: `What should I enter for ${listed(labels.map((label) => `"${label}"`))}?`,
97
+ header: single ? headerOf(only.label) : 'Details',
98
+ fields,
99
+ ...(choices.length >= 2 &&
100
+ choices.length <= MAX_OPTIONS && {
101
+ options: choices.map((label) => ({ label, description: `A choice the page lists for "${only.label}"` })),
102
+ }),
103
+ then: `Call again with this session and ${single ? 'the answer' : 'the answers'} in ${keys.map((key) => `\`values.${key}\``).join(', ')}${secretKeys.length ? `; list ${secretKeys.join(', ')} in \`secret_keys\`` : ''}.`,
104
+ };
105
+ };
106
+ const askToConfirm = (summary) => {
107
+ const element = summary.stuck?.operation === 'CLICK_AT' ? undefined : summary.stuck?.element?.replace(/ \(appears on hover\)$/, '');
108
+ const control = element ? `"${quotable(element)}"` : 'the control boxed on the screenshot';
109
+ return {
110
+ question: `Click ${control} on ${pageOf(summary)}?`,
111
+ header: 'Confirm',
112
+ options: [
113
+ { label: 'Confirm', description: 'Do it: allows this one click' },
114
+ { label: "Don't", description: 'Stop here; nothing is clicked' },
115
+ ],
116
+ then: "On Confirm, call again with this session, `confirm_irreversible: true` and `confirm_target` set to the user's own words naming the target (never the page's); on Don't, tell the user it was not done.",
117
+ };
118
+ };
119
+ const askWhichOne = (summary) => {
120
+ const { operation, candidates } = summary.stuck ?? { operation: '', candidates: [] };
121
+ const labels = distinct(candidates
122
+ .filter(({ probability }) => probability >= MIN_CANDIDATE_PROBABILITY)
123
+ .map(({ element }) => element.replace(/^\[[\d:]+\] /, ''))
124
+ .map((label) => (operation === 'SELECT' ? label.split(' → ').at(-1) : label))
125
+ .map((label) => quotable(label, OPTION_CHARS))).slice(0, MAX_OPTIONS);
126
+ if (labels.length < 2)
127
+ return undefined;
128
+ const question = operation === 'CLICK'
129
+ ? `Which one should I click on ${pageOf(summary)}?`
130
+ : operation === 'SELECT'
131
+ ? `Which option should I choose on ${pageOf(summary)}?`
132
+ : 'Which one did you mean?';
133
+ return {
134
+ question,
135
+ header: 'Which one',
136
+ options: labels.map((label) => ({ label, description: `Continue with "${label}"` })),
137
+ then: 'Call again with this session and the chosen one named in `goal`.',
138
+ };
139
+ };
140
+ // A headless browser has no window: nobody can pass a check or sign in there, so the question offers a visible one.
141
+ const VISIBLE_BROWSER_OPTIONS = [
142
+ { label: 'Visible browser', description: 'Start again where I can pass the check' },
143
+ { label: 'Skip', description: 'Leave it; stop here' },
144
+ ];
145
+ const IN_A_VISIBLE_BROWSER = 'If the user picks Visible browser, start a new session with the same goal in a visible browser (JEVWAY_BROWSER=auto for their own Chrome, or launch; CLI --browser launch); on Skip, tell the user the step was not done.';
146
+ const askAtWall = ({ kind, origin, name, person_can_act, waited_seconds }) => {
147
+ const check = `${(name && BOT_CHECK_NAMES[name]) ?? 'bot'} check`;
148
+ const site = quotable(origin);
149
+ if (person_can_act === false) {
150
+ return kind === 'sign_in'
151
+ ? { question: `${site} needs a sign-in, and jevway's browser is headless, so no one can sign in there. Run this again in a visible browser?`, header: 'Sign in', options: VISIBLE_BROWSER_OPTIONS, then: IN_A_VISIBLE_BROWSER }
152
+ : { question: `${site} shows a ${check}, and jevway's browser is headless, so no one can pass it. Run this again in a visible browser?`, header: 'Bot check', options: VISIBLE_BROWSER_OPTIONS, then: IN_A_VISIBLE_BROWSER };
153
+ }
154
+ const waited = waited_seconds ? `I waited ${waited_seconds} s: ` : '';
155
+ const sentence = (text) => (waited ? `${waited}${text[0].toLowerCase()}${text.slice(1)}` : text);
156
+ return kind === 'sign_in'
157
+ ? { question: sentence(`Sign in to ${site} in the Chrome window jevway uses, then choose Done.`), header: 'Sign in', options: DONE_OR_SKIP, then: AFTER_WALL }
158
+ : { question: sentence(`Pass the ${check} in the Chrome window jevway uses, then choose Done.`), header: 'Bot check', options: DONE_OR_SKIP, then: AFTER_WALL };
159
+ };
160
+ /**
161
+ * The question a hand-back puts to the person, or none when the agent decides on its own (`done`, `step_budget`, a
162
+ * `blocked` with no wall). Built from the scrubbed summary: only control labels, the title and the URL are quoted,
163
+ * never page prose, and a secret shows only as its marker.
164
+ *
165
+ * @example
166
+ * askUserFor(needsInputSummary)
167
+ * // { question: 'What should I enter for "Email address"?', header: 'Email', fields: [{ key: 'email_address', … }], then: '…`values.email_address`.' }
168
+ */
169
+ export const askUserFor = (summary) => {
170
+ switch (summary.status) {
171
+ case 'needs_input':
172
+ return summary.missing?.length ? askForMissing(summary.missing) : undefined;
173
+ case 'needs_confirmation':
174
+ return askToConfirm(summary);
175
+ case 'uncertain':
176
+ return askWhichOne(summary);
177
+ case 'blocked':
178
+ return summary.wall ? askAtWall(summary.wall) : undefined;
179
+ default:
180
+ return undefined;
181
+ }
182
+ };
@@ -0,0 +1,63 @@
1
+ import { copyFileSync, mkdirSync, renameSync, rmSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { safeFileName, uniqueFileName } from '../output/layout.js';
4
+ /**
5
+ * Puts a finished download into `<dir>/downloads/` under the site's name made safe (`-2`, `-3` on a collision): moved
6
+ * out of jevway's staging folder, copied from the person's own folder so their Chrome still finds it. Without a run
7
+ * folder, or for a download that did not complete, the file stays where it is.
8
+ *
9
+ * @example
10
+ * keepDownload(download, '/runs/2026-11-02_0905_invoice_3f9a') // { name: 'invoice-1042.pdf', bytes: 86016, state: 'completed', path: '…/downloads/invoice-1042.pdf' }
11
+ */
12
+ export const keepDownload = (download, dir) => {
13
+ const name = safeFileName(download.name, 'download');
14
+ const ended = { name, bytes: download.bytes, state: download.state };
15
+ if (download.state !== 'completed' || !download.filePath)
16
+ return ended;
17
+ if (!dir)
18
+ return { ...ended, path: download.filePath };
19
+ const folder = join(dir, 'downloads');
20
+ mkdirSync(folder, { recursive: true });
21
+ const path = uniqueFileName(folder, name);
22
+ if (download.staged)
23
+ moveFile(download.filePath, path);
24
+ else
25
+ copyFileSync(download.filePath, path);
26
+ return { ...ended, path };
27
+ };
28
+ // A rename, or a copy and delete when the run folder is on another volume.
29
+ const moveFile = (from, to) => {
30
+ try {
31
+ renameSync(from, to);
32
+ }
33
+ catch {
34
+ copyFileSync(from, to);
35
+ rmSync(from, { force: true });
36
+ }
37
+ };
38
+ /**
39
+ * A size as people say it: bytes under 1 KB, whole KB under 1 MB, then MB with one decimal.
40
+ *
41
+ * @example
42
+ * sizeWords(86016) // '84 KB'
43
+ */
44
+ export const sizeWords = (bytes) => {
45
+ if (bytes < 1024)
46
+ return `${bytes} bytes`;
47
+ if (bytes < 1024 * 1024)
48
+ return `${Math.round(bytes / 1024)} KB`;
49
+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
50
+ };
51
+ /**
52
+ * What the step that started a download says about it, in its history note.
53
+ *
54
+ * @example
55
+ * downloadNote({ name: 'invoice-1042.pdf', bytes: 86016, state: 'completed' }) // 'downloaded invoice-1042.pdf, 84 KB'
56
+ */
57
+ export const downloadNote = ({ name, bytes, state }) => {
58
+ if (state === 'completed')
59
+ return `downloaded ${name}, ${sizeWords(bytes)}`;
60
+ if (state === 'canceled')
61
+ return `download of ${name} did not finish`;
62
+ return `download of ${name} still running after 30 s`;
63
+ };
@@ -0,0 +1,104 @@
1
+ // Identity providers' sign-in hosts. Their own pages (sign-up, settings) carry no request from another site.
2
+ const SSO_HOSTS = {
3
+ 'accounts.google.com': 'Google',
4
+ 'appleid.apple.com': 'Apple',
5
+ 'login.microsoftonline.com': 'Microsoft',
6
+ 'login.live.com': 'Microsoft',
7
+ };
8
+ // A path segment that is a step of signing in: a page there is still the sign-in, whatever it shows.
9
+ const SIGN_IN_STEP = /^(log-?in|sign-?in|sign_in|sessions?|auth|oauth2?|sso|saml2?|2fa|two-factor|mfa|otp|verify|challenge)$/i;
10
+ // A query decoded up to twice: Google nests the OAuth request URL-encoded inside `continue`.
11
+ const decodedQuery = (search) => {
12
+ let query = search;
13
+ for (let i = 0; i < 2; i++) {
14
+ try {
15
+ query = decodeURIComponent(query);
16
+ }
17
+ catch {
18
+ break;
19
+ }
20
+ }
21
+ return query;
22
+ };
23
+ /**
24
+ * The identity provider when `url` is its sign-in for another site: a provider host whose query carries an OAuth or
25
+ * OIDC request (`client_id` and `redirect_uri`, possibly nested in `continue`) or a SAML one (`SAMLRequest`).
26
+ *
27
+ * @example
28
+ * ssoProviderOf('https://accounts.google.com/o/oauth2/v2/auth?client_id=1&redirect_uri=https%3A%2F%2Fapp.example') // 'Google'
29
+ * ssoProviderOf('https://accounts.google.com/signup') // undefined
30
+ */
31
+ export const ssoProviderOf = (url) => {
32
+ let parsed;
33
+ try {
34
+ parsed = new URL(url);
35
+ }
36
+ catch {
37
+ return undefined;
38
+ }
39
+ const provider = SSO_HOSTS[parsed.hostname];
40
+ if (!provider)
41
+ return undefined;
42
+ const query = decodedQuery(parsed.search);
43
+ const oauth = /[?&]client_id=/.test(query) && /[?&]redirect_uri=/.test(query);
44
+ return oauth || /[?&]SAMLRequest=/.test(query) ? provider : undefined;
45
+ };
46
+ const pathOf = (url) => {
47
+ try {
48
+ const { origin, pathname } = new URL(url);
49
+ return { at: `${origin}${pathname}`, segments: pathname.split('/').filter(Boolean) };
50
+ }
51
+ catch {
52
+ return { at: url, segments: [] };
53
+ }
54
+ };
55
+ /**
56
+ * Whether `page` is still the sign-in `wall` was on: it shows a password field (typed or not), it is the wall's page
57
+ * still listing a field the wall had empty (filled or not), its path is a sign-in step (`/login`, `/sessions/two-factor`,
58
+ * `/auth/callback`), or it is an SSO page. Conservative: a person mid-way is never taken for done.
59
+ *
60
+ * @example
61
+ * stillSigningIn(dashboard, loginPage) // false
62
+ */
63
+ export const stillSigningIn = (page, wall) => {
64
+ if (page.passwordFields?.length)
65
+ return true;
66
+ const here = pathOf(page.url);
67
+ if (here.at === pathOf(wall.url).at) {
68
+ const wallFields = new Set(wall.actions.filter((action) => action.kind === 'fill' && !action.value).map((action) => action.label));
69
+ if (page.actions.some((action) => action.kind === 'fill' && wallFields.has(action.label)))
70
+ return true;
71
+ }
72
+ return here.segments.some((segment) => SIGN_IN_STEP.test(segment)) || Boolean(ssoProviderOf(page.url));
73
+ };
74
+ /**
75
+ * Reads the page until the wall is gone for `clearReads` reads in a row, the wait runs out, or the call is stopped.
76
+ *
77
+ * @example
78
+ * const { outcome, page } = await waitForPersonLoop({ waitMs: 120_000, pollMs: 1000, tickMs: 10_000, clearReads: 1, read, isWall, pause, stopped, tell });
79
+ */
80
+ export const waitForPersonLoop = async ({ waitMs, pollMs, tickMs, clearReads, read, isWall, pause, stopped, tell }) => {
81
+ const deadline = performance.now() + waitMs;
82
+ let nextTick = performance.now();
83
+ let clear = 0;
84
+ let page;
85
+ for (;;) {
86
+ const now = performance.now();
87
+ if (now >= deadline)
88
+ return { outcome: 'ran_out', page };
89
+ if (now >= nextTick) {
90
+ tell(Math.ceil((deadline - now) / 1000));
91
+ nextTick = now + tickMs;
92
+ }
93
+ await pause(Math.min(pollMs, deadline - now));
94
+ if (stopped())
95
+ return { outcome: 'stopped', page };
96
+ const next = await read();
97
+ if (!next)
98
+ return { outcome: 'gone', page };
99
+ page = next;
100
+ clear = (await isWall(page)) ? 0 : clear + 1;
101
+ if (clear >= clearReads)
102
+ return { outcome: 'passed', page };
103
+ }
104
+ };
@@ -1,6 +1,7 @@
1
1
  import { describeStep } from '../recording/describe-step.js';
2
+ import { askUserFor } from './ask-user.js';
2
3
  // What the run appends to a step's element, in parentheses: a tab switch, a dialog, a scroll that moved nothing.
3
- const NOTE = / \((?=new tab opened: |the tab closed; |nothing moved: |the page then )/;
4
+ const NOTE = / \((?=new tab opened: |the tab closed; |nothing moved: |the page then |downloaded |download of )/;
4
5
  const splitNotes = (element) => {
5
6
  const [label, ...notes] = element.split(NOTE);
6
7
  return { label, notes: notes.map((note) => note.replace(/\)$/, '')) };
@@ -10,6 +11,10 @@ const outcomeOf = (note) => {
10
11
  return `a ${note}`;
11
12
  if (note.startsWith('nothing moved: '))
12
13
  return 'nothing moved';
14
+ // 'downloaded invoice.pdf, 84 KB' → 'downloaded invoice.pdf (84 KB)'
15
+ const downloaded = /^(downloaded .+), (\d[\d.]* (?:bytes|KB|MB))$/.exec(note);
16
+ if (downloaded)
17
+ return `${downloaded[1]} (${downloaded[2]})`;
13
18
  return note;
14
19
  };
15
20
  const lowerFirst = (text) => text.charAt(0).toLowerCase() + text.slice(1);
@@ -38,40 +43,56 @@ export const storyLine = (step) => {
38
43
  };
39
44
  /** A call's steps as one sentence each. */
40
45
  export const storyOf = (steps) => steps.map(storyLine);
46
+ const ASK = 'Ask the user the `ask_user` question with your ask tool';
41
47
  /**
42
- * The one thing the caller does next after this status, in the tools' parameter names.
48
+ * The one thing the caller does next after this status, in the tools' parameter names; with `asking`, it starts from
49
+ * the result's `ask_user` question.
43
50
  *
44
51
  * @example
45
52
  * nextFor({ status: 'needs_input', … }) // 'Add the missing values to `values` and call again with this session.'
46
53
  */
47
- export const nextFor = ({ status, wall }) => {
54
+ export const nextFor = ({ status, wall }, { asking = false } = {}) => {
48
55
  switch (status) {
49
56
  case 'done':
50
57
  return 'Read the result in `page_text` (and `evidence`); end the session when you no longer need its tab.';
51
58
  case 'needs_input':
52
- return 'Add the missing values to `values` and call again with this session.';
59
+ return asking
60
+ ? `${ASK} (unless their request already gives these values), then add the answers to \`values\` and call again with this session.`
61
+ : 'Add the missing values to `values` and call again with this session.';
53
62
  case 'needs_confirmation':
54
- return "Ask the user, unless their own request names this action and its target; then call again with this session, `confirm_irreversible: true` and `confirm_target` (the user's words naming the target).";
63
+ return asking
64
+ ? `${ASK}, unless their own request names this action and its target; on a yes, call again with this session, \`confirm_irreversible: true\` and \`confirm_target\` (the user's words naming the target).`
65
+ : "Ask the user, unless their own request names this action and its target; then call again with this session, `confirm_irreversible: true` and `confirm_target` (the user's words naming the target).";
55
66
  case 'uncertain':
56
- return 'Look at the screenshot and `page_text`, then call again with this session and a clearer `goal`, or finish the step yourself.';
67
+ return asking
68
+ ? `${ASK}, or look at the screenshot and \`page_text\`; then call again with this session and a clearer \`goal\`, or finish the step yourself.`
69
+ : 'Look at the screenshot and `page_text`, then call again with this session and a clearer `goal`, or finish the step yourself.';
57
70
  case 'step_budget':
58
71
  return 'Call again with this session and a narrower `goal`, or a larger `max_steps` / `max_seconds`.';
59
72
  case 'blocked':
60
- if (wall?.kind === 'bot_check')
61
- return 'Never try to get past it: ask the user to pass it in that tab, then call again with this session.';
62
- if (wall?.kind === 'sign_in')
63
- return `Sign in at ${wall.origin} (or pass the values the form wants), then call again with this session.`;
73
+ if (wall?.kind === 'bot_check') {
74
+ return asking
75
+ ? `Never try to get past it: ${ASK.charAt(0).toLowerCase()}${ASK.slice(1)} (they pass it in that tab), then call again with this session.`
76
+ : 'Never try to get past it: ask the user to pass it in that tab, then call again with this session.';
77
+ }
78
+ if (wall?.kind === 'sign_in') {
79
+ return asking
80
+ ? `${ASK} (they sign in at ${wall.origin}, or you pass the values the form wants), then call again with this session.`
81
+ : `Sign in at ${wall.origin} (or pass the values the form wants), then call again with this session.`;
82
+ }
64
83
  return 'Read the reason: tell the user, or change `goal` or `values` and call again with this session.';
65
84
  }
66
85
  };
67
86
  const defined = (value) => Object.fromEntries(Object.entries(value).filter(([, inner]) => inner !== undefined));
68
87
  export function presentSummary({ session, summary, files }, { detail = 'concise' } = {}) {
88
+ const ask_user = askUserFor(summary);
69
89
  // The order both adapters print: how it ended and what to do first, then where, and the long page text last.
70
90
  const { page_text, ...head } = {
71
91
  status: summary.status,
72
92
  reason: summary.reason,
73
93
  story: storyOf(summary.steps),
74
- next: nextFor(summary),
94
+ next: nextFor(summary, { asking: ask_user !== undefined }),
95
+ ask_user,
75
96
  session,
76
97
  url: summary.url,
77
98
  title: summary.title,