sumo-agents 0.7.2 → 0.8.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/src/tools.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { spawn, spawnSync } from 'node:child_process';
3
- import { accessSync, closeSync, constants, existsSync, fstatSync, ftruncateSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readlinkSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
3
+ import { accessSync, closeSync, constants, existsSync, fstatSync, ftruncateSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readlinkSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from 'node:fs';
4
4
  import { homedir, tmpdir } from 'node:os';
5
5
  import { delimiter, dirname, extname, join, relative, resolve, sep } from 'node:path';
6
6
  import { CREDENTIAL_NAMES } from './auth.mjs';
@@ -8,7 +8,7 @@ import { guardCommand, guardPath } from './guard.mjs';
8
8
  import { SELF } from './paths.mjs';
9
9
  import { readPlainFile } from './scan.mjs';
10
10
  import { redact } from './redact.mjs';
11
- import { blockedWrite, guardedPlaces, guardedWhy, readOnlyPlaces, sandboxWrap, unsandboxable, writablePlaces } from './sandbox.mjs';
11
+ import { blockedWrite, guardedPlaces, guardedWhy, ownGit, readOnlyPatterns, readOnlyPlaces, sandboxWrap, unsandboxable, writablePlaces } from './sandbox.mjs';
12
12
 
13
13
  /**
14
14
  * The two tools a job gets, and the policy every call passes through before it
@@ -166,7 +166,7 @@ const NO_PROMPTS = { GIT_TERMINAL_PROMPT: '0', GIT_EDITOR: 'true', GIT_PAGER: 'c
166
166
  const sshCommands = new Map();
167
167
  function hasSshCommand(cwd, env) {
168
168
  if (!sshCommands.has(cwd)) {
169
- const asked = spawnSync('git', ['config', '--get', 'core.sshCommand'], { cwd, env, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: PARSE_TIMEOUT_MS });
169
+ const asked = spawnSync('git', ownGit(['config', '--get', 'core.sshCommand']), { cwd, env, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: PARSE_TIMEOUT_MS });
170
170
  sshCommands.set(cwd, asked.status === 0 && asked.stdout.trim() !== '');
171
171
  }
172
172
  return sshCommands.get(cwd);
@@ -437,10 +437,11 @@ function jailOf(ctx) {
437
437
  toldUnsandboxed = true;
438
438
  return { wrap: null, writable: [], readOnly: [], note };
439
439
  }
440
- const writable = writablePlaces(ctx.roots, { opened: ctx.opened ?? [] });
440
+ // Every root can be read; only the project (the first) is written, as the editor writes it — a job's own folder holds what judges it.
441
+ const writable = writablePlaces(ctx.roots.slice(0, 1), { opened: ctx.opened ?? [] });
441
442
  const readOnly = [...new Set([...readOnlyPlaces(ctx.roots), ...guardedPlaces(ctx.roots)])];
442
443
  const offline = ctx.network === false;
443
- return { wrap: sandboxWrap(writable, { readOnly, network: !offline }), writable, readOnly, offline, note: '' };
444
+ return { wrap: sandboxWrap(writable, { readOnly, patterns: readOnlyPatterns(ctx.roots), network: !offline }), writable, readOnly, offline, note: '' };
444
445
  }
445
446
 
446
447
  /** What a job's own tools run the project's checks under: the sandbox its shell is in, or nothing where the shell has none. */
@@ -516,7 +517,9 @@ async function runInBackground(command, ctx, { env, jail }) {
516
517
  try {
517
518
  mkdirSync(dir, { recursive: true, mode: 0o700 });
518
519
  log = join(dir, `${Date.now()}-${process.pid}-${spilled++}.log`);
519
- fd = openSync(log, 'a', 0o600);
520
+ // Made new, never through a link: the folder may be the temporary one, which the sandboxed shell writes too, and a
521
+ // link it planted at this name would have sumo — outside the sandbox — append the command's output wherever it leads.
522
+ fd = openSync(log, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_APPEND | constants.O_NOFOLLOW, 0o600);
520
523
  } catch (cause) {
521
524
  return result(`the command could not be started in the background: there is nowhere to write its log (${cause.message})`, true);
522
525
  }
@@ -710,18 +713,20 @@ async function blocked(command, shown, ctx, jail, options) {
710
713
 
711
714
  /** How much of a file is looked at to tell a binary from text: a NUL byte in its first pages is one. */
712
715
  const BINARY_PROBE_BYTES = 8192;
716
+ /** The most a view reads: it shows a few hundred lines, and a file past this is looked at in parts, in the shell. */
717
+ const VIEW_MAX_BYTES = 64 * 1024 * 1024;
713
718
 
714
719
  function view(path, range) {
715
- let bytes;
716
- try {
717
- bytes = readFileSync(path);
718
- } catch (cause) {
719
- if (cause.code === 'EISDIR') return result(cap(readdirSync(path).join('\n')));
720
- throw cause;
720
+ // Judged and read through one descriptor that never waits: a FIFO or a device is said to be one, never read.
721
+ const text = readPlainFile(path, VIEW_MAX_BYTES);
722
+ if (text === null) {
723
+ const stat = statSync(path);
724
+ if (stat.isDirectory()) return result(cap(readdirSync(path).join('\n')));
725
+ return result(stat.isFile() ? `${path} is ${stat.size} bytes, too big to view whole — look at a part of it in the shell: sed -n '1,200p', tail or grep` : notPlain(path, stat), true);
721
726
  }
722
727
  // A binary shown as text is pages of nonsense, every page billed: say what it is instead.
723
- if (bytes.subarray(0, BINARY_PROBE_BYTES).includes(0)) return result(`${path} is a binary file (${bytes.length} bytes) — not shown; use a tool that reads its kind, or \`file\` and \`xxd | head\` for a look`, true);
724
- const lines = bytes.toString('utf8').split('\n');
728
+ if (text.slice(0, BINARY_PROBE_BYTES).includes('\0')) return result(`${path} is a binary file (${statSync(path).size} bytes) — not shown; use a tool that reads its kind, or \`file\` and \`xxd | head\` for a look`, true);
729
+ const lines = text.split('\n');
725
730
  let [from, to] = Array.isArray(range) && range.length === 2 ? range : [1, lines.length];
726
731
  from = Math.max(1, from);
727
732
  to = to === -1 ? lines.length : Math.min(lines.length, to);
@@ -828,13 +833,14 @@ const PY_PARSE = 'import ast,sys,traceback\ntry: ast.parse(open(sys.argv[1], "rb
828
833
  * or the parser is missing or too slow to say. `.js .mjs .cjs` by `node --check`, `.json` here, `.py` and `.go` by
829
834
  * their own tools when they are on the PATH.
830
835
  */
831
- function parseError(path, ctx) {
836
+ function parseError(path, ctx, text) {
832
837
  const env = ctx.env ?? childEnv();
833
838
  const ext = extname(path);
834
839
  let run;
840
+ // JSON is parsed here, from the text the editor read or wrote: a name looked up again could be anything by now.
835
841
  if (ext === '.json') {
836
842
  try {
837
- JSON.parse(readFileSync(path, 'utf8'));
843
+ JSON.parse(text);
838
844
  return null;
839
845
  } catch (cause) {
840
846
  return cause.message;
@@ -869,10 +875,14 @@ function undoTurns(sessionDir) {
869
875
  /** The folder the coming turn's undo goes in — made only when the editor first changes a file in it. */
870
876
  export const nextUndoTurn = (sessionDir) => join(sessionDir, String((undoTurns(sessionDir).at(-1) ?? 0) + 1));
871
877
 
872
- const digest = (data) => createHash('sha256').update(data).digest('hex');
878
+ export const digest = (data) => createHash('sha256').update(data).digest('hex');
873
879
 
874
- /** Keep the original bytes once, and the expected file identity and content after the last editor write. */
875
- function keepForUndo(turnDir, path, before, written) {
880
+ /**
881
+ * Keep the original bytes once, and what the file holds after the editor's last write: /undo judges it by that, not by its
882
+ * inode. `landed` is where the write went — the real path the checks settled on, and the descriptor's own stat after it —
883
+ * never the name looked up again: a link put there since would be taken for the file, and /undo would write through it.
884
+ */
885
+ function keepForUndo(turnDir, path, before, written, { target, stat }) {
876
886
  const file = join(turnDir, MANIFEST);
877
887
  const manifest = existsSync(turnDir) ? JSON.parse(readFileSync(file, 'utf8')) : [];
878
888
  if (manifest.length === 0) {
@@ -885,15 +895,13 @@ function keepForUndo(turnDir, path, before, written) {
885
895
  if (other !== sessionDir && Date.now() - statSync(other).mtimeMs > UNDO_KEEP_MS) rmSync(other, { recursive: true, force: true });
886
896
  }
887
897
  }
888
- const target = realpathSync.native(path);
889
- const stat = statSync(target);
890
898
  let entry = manifest.find((item) => item.path === path || item.target === target);
891
899
  if (!entry) {
892
900
  const kept = before === null ? null : String(manifest.length);
893
901
  if (kept !== null) writeFileSync(join(turnDir, kept), before, { mode: 0o600, flag: 'wx' });
894
- entry = { path, target, kept, original: before === null ? null : digest(before), dev: stat.dev, ino: stat.ino, nlink: stat.nlink };
902
+ entry = { path, target, kept, original: before === null ? null : digest(before), nlink: stat.nlink };
895
903
  manifest.push(entry);
896
- } else if (before === null || entry.after !== digest(before) || entry.target !== target || entry.dev !== stat.dev || entry.ino !== stat.ino || entry.nlink !== stat.nlink) {
904
+ } else if (before === null || entry.after !== digest(before) || entry.target !== target || entry.nlink !== stat.nlink) {
897
905
  entry.conflict = true; // A later editor write cannot claim an intervening change as its own.
898
906
  }
899
907
  entry.after = digest(written);
@@ -901,12 +909,20 @@ function keepForUndo(turnDir, path, before, written) {
901
909
  renameSync(file + '.tmp', file);
902
910
  }
903
911
 
904
- /** Check every destination and backup before touching the first file. A conflict keeps the whole turn for inspection or retry. */
912
+ /** A file that no longer holds what the editor left in it: someone's work since, kept as it is. */
913
+ const notAsLeft = (message) => Object.assign(new Error(message), { changed: true });
914
+
915
+ /**
916
+ * Check every destination and backup before touching the first file. A file changed since the editor wrote it keeps the
917
+ * whole turn for the user to put back and retry; asked again over the same file, the turn is let go, the file kept as it
918
+ * is, so the turns before it can still be undone.
919
+ */
905
920
  export function undoLast(sessionDir) {
906
921
  const n = undoTurns(sessionDir).at(-1);
907
922
  if (n === undefined) return null;
908
923
  const turnDir = join(sessionDir, String(n));
909
924
  const cursor = join(turnDir, 'restoring');
925
+ const refused = join(turnDir, 'refused');
910
926
  let manifest, start, plan;
911
927
  try {
912
928
  manifest = JSON.parse(readFileSync(join(turnDir, MANIFEST), 'utf8'));
@@ -916,16 +932,17 @@ export function undoLast(sessionDir) {
916
932
  const resuming = existsSync(cursor);
917
933
  start = resuming ? JSON.parse(readFileSync(cursor, 'utf8')) : 0;
918
934
  if (!Number.isInteger(start) || start < 0 || start >= manifest.length) throw new Error('invalid undo progress');
919
- plan = manifest.slice(start).map(({ path, target, kept, original, after, dev, ino, nlink, conflict }, offset) => {
935
+ plan = manifest.slice(start).map(({ path, target, kept, original, after, nlink, conflict }, offset) => {
920
936
  if (!after || !target) throw new Error(`the saved record for ${path} has no verifiable after-state`);
921
- if (conflict) throw new Error(`${path} was changed outside the editor during this turn`);
937
+ if (conflict) throw notAsLeft(`${path} was changed outside the editor during this turn`);
922
938
  const atCursor = resuming && offset === 0;
923
939
  const stat = statSync(path, { throwIfNoEntry: false });
924
- if (atCursor && kept === null && original === null && !stat && !lstatSync(target, { throwIfNoEntry: false })) return null;
925
- if (!stat?.isFile() || realpathSync.native(path) !== target || stat.dev !== dev || stat.ino !== ino || stat.nlink !== nlink) throw new Error(`${path} was removed or replaced`);
940
+ // A file the turn made that is gone already is as it was before the turn.
941
+ if (kept === null && original === null && !stat && !lstatSync(target, { throwIfNoEntry: false })) return null;
942
+ if (!stat?.isFile() || realpathSync.native(path) !== target || stat.nlink !== nlink) throw notAsLeft(`${path} was removed or replaced`);
926
943
  const content = digest(readFileSync(target));
927
944
  if (atCursor && kept !== null && content === original) return null;
928
- if (content !== after) throw new Error(`${path} changed after the editor wrote it`);
945
+ if (content !== after) throw notAsLeft(`${path} changed after the editor wrote it`);
929
946
  const remove = kept === null && original === null;
930
947
  if (remove) accessSync(dirname(target), constants.W_OK | constants.X_OK);
931
948
  else if (kept !== String(start + offset)) throw new Error(`invalid backup for ${path}`);
@@ -935,7 +952,19 @@ export function undoLast(sessionDir) {
935
952
  return { path, target, stat, after, bytes };
936
953
  });
937
954
  } catch (cause) {
938
- return { blocked: `undo stopped before changing any files: ${cause.message}. The saved turn is at ${turnDir}.` };
955
+ if (!cause.changed) return { blocked: `undo stopped before changing any files: ${cause.message}. The saved turn is at ${turnDir}.` };
956
+ let refusedBefore = null;
957
+ try {
958
+ refusedBefore = readFileSync(refused, 'utf8');
959
+ } catch {
960
+ // Not refused before: this is the first time.
961
+ }
962
+ if (refusedBefore === cause.message) {
963
+ rmSync(turnDir, { recursive: true, force: true });
964
+ return { blocked: `undo let go of the last turn without changing any more files: ${cause.message}, and it is kept as it is. /undo again reaches the turn before.` };
965
+ }
966
+ writeFileSync(refused, cause.message, { mode: 0o600 });
967
+ return { blocked: `undo stopped before changing any files: ${cause.message}. Put it back as the editor left it and /undo again — or /undo again to let that turn go and keep it as it is. The saved turn is at ${turnDir}.` };
939
968
  }
940
969
  try {
941
970
  for (const [offset, entry] of plan.entries()) {
@@ -948,7 +977,7 @@ export function undoLast(sessionDir) {
948
977
  throw new Error(`${path} changed while undo was running`);
949
978
  }
950
979
  if (bytes === null) rmSync(target);
951
- else writeTo(target, bytes);
980
+ else writeTo(target, bytes, current);
952
981
  }
953
982
  rmSync(turnDir, { recursive: true, force: true });
954
983
  } catch (cause) {
@@ -962,23 +991,26 @@ export function undoLast(sessionDir) {
962
991
  /**
963
992
  * One change to a file by the editor, and what is owed around it: what the file held is kept for /undo (`ctx.undo`, the
964
993
  * chat's turn folder), a change on disk since the model saw it is said first, and a parse the change broke — one that held
965
- * before it — is said after. `created`: the file was not there, so it parsed before as far as anyone is concerned.
994
+ * before it — is said after. `file`: the descriptor `openChecked` holds, read and written through; with none the file is
995
+ * made at `target`, where the checks found it lands — it was not there, so it parsed before as far as anyone is concerned.
966
996
  */
967
- function change(path, ctx, written, { created = false } = {}) {
997
+ function change(path, ctx, written, { file = null, target = null } = {}) {
998
+ const created = file === null;
968
999
  const stale = created ? null : changedSince(path, ctx);
969
- const before = ctx.undo && !created ? readFileSync(path) : null;
970
- const broken = created ? null : parseError(path, ctx);
971
- writeTo(path, written, { create: created });
1000
+ const before = ctx.undo && !created ? file.bytes : null;
1001
+ const broken = created ? null : parseError(path, ctx, file.bytes.toString('utf8'));
1002
+ const where = created ? (target ?? path) : file.target;
1003
+ const stat = created ? writeTo(where, written) : writeThrough(path, file, written);
972
1004
  // Kept once the write has gone through: a change refused — a create of a file already there — has nothing to undo. A keep
973
1005
  // that fails is said, not thrown: the change is made, and a model told it failed would make it twice.
974
1006
  let unkept = null;
975
1007
  try {
976
- if (ctx.undo) keepForUndo(ctx.undo, path, before, written);
1008
+ if (ctx.undo) keepForUndo(ctx.undo, path, before, written, { target: where, stat });
977
1009
  } catch (cause) {
978
1010
  unkept = `(this change could not be kept for /undo: ${cause.message})`;
979
1011
  }
980
1012
  if (created || ctx.seen?.has(path)) seenIn(ctx).set(path, stampOf(path));
981
- const now = broken === null ? parseError(path, ctx) : null;
1013
+ const now = broken === null ? parseError(path, ctx, written) : null;
982
1014
  return (said) => [stale, said, unkept, now && `The file no longer parses:\n${now}`].filter(Boolean).join('\n');
983
1015
  }
984
1016
 
@@ -1066,25 +1098,63 @@ function linesOf(text, old) {
1066
1098
  /** Why a file with another name is asked about each time. */
1067
1099
  const HARD_LINK = 'it has another name elsewhere (a hard link), and a change here changes that file too';
1068
1100
 
1101
+ /** What the editor says when the file it checked is not the one at that name any more. */
1102
+ const replaced = (path) => `${path} was replaced while it was being changed; nothing was written — view it again`;
1103
+ /** Why a path that is not a plain file is neither read nor written: a FIFO would wait on a writer for good, a device never ends. */
1104
+ const notPlain = (path, stat) => `${path} is ${stat.isDirectory() ? 'a directory' : 'not a plain file (a FIFO, a device or a socket)'} — not opened; look at it in the shell if it is safe to`;
1105
+
1106
+ /** Bytes put in place of a file's, from its start, through a descriptor already open on it. */
1107
+ function overwrite(fd, data) {
1108
+ const bytes = Buffer.from(data);
1109
+ ftruncateSync(fd, 0);
1110
+ for (let at = 0; at < bytes.length; ) at += writeSync(fd, bytes, at, bytes.length - at, at);
1111
+ }
1112
+
1069
1113
  /**
1070
- * A file the editor writes, written where its checks found it lands: through no link swapped in since — the last name
1071
- * is opened without following one — and only when the file opened is the one that was checked. Nothing is truncated
1072
- * before that. (A folder above it swapped for a link between the check and the open is not caught: Node has no openat.)
1114
+ * A file written at its real path, never looked up again: made new (`expected` null, and never through a link), or
1115
+ * written over only when the file opened there — the last name not followed — is the one `expected` names. Nothing is
1116
+ * truncated before that. (A folder above it swapped for a link between the check and the open is not caught: Node has no openat.)
1117
+ * Returns the stat of what was written, from its descriptor.
1073
1118
  */
1074
- function writeTo(path, data, { create = false } = {}) {
1075
- const target = landsAt(path) ?? path;
1076
- const before = create ? null : statSync(target);
1077
- const fd = openSync(target, constants.O_WRONLY | constants.O_NOFOLLOW | (create ? constants.O_CREAT | constants.O_EXCL : 0), 0o666);
1119
+ function writeTo(target, data, expected = null) {
1120
+ const fd = openSync(target, constants.O_WRONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK | (expected ? 0 : constants.O_CREAT | constants.O_EXCL), 0o666);
1078
1121
  try {
1079
1122
  const opened = fstatSync(fd);
1080
- if (before && (opened.ino !== before.ino || opened.dev !== before.dev)) throw new Error(`${path} was replaced while it was being changed; nothing was written — view it again`);
1081
- ftruncateSync(fd, 0);
1082
- writeFileSync(fd, data);
1123
+ if (expected && (!opened.isFile() || opened.ino !== expected.ino || opened.dev !== expected.dev)) throw new Error(replaced(target));
1124
+ overwrite(fd, data);
1125
+ return fstatSync(fd);
1083
1126
  } finally {
1084
1127
  closeSync(fd);
1085
1128
  }
1086
1129
  }
1087
1130
 
1131
+ /**
1132
+ * A file the editor changes, opened once where its checks found it lands (`target`, a real path; `checked`, its lstat
1133
+ * then): read and later written through this one descriptor, so what is written is what was read and judged. The last
1134
+ * name is not followed, nothing but a plain file is opened, and nothing that waits on a writer.
1135
+ */
1136
+ function openChecked(path, target, checked) {
1137
+ if (!checked) throw new Error(`${path} does not exist`);
1138
+ if (!checked.isFile()) throw new Error(notPlain(path, checked));
1139
+ const fd = openSync(target, constants.O_RDWR | constants.O_NOFOLLOW | constants.O_NONBLOCK);
1140
+ try {
1141
+ const stat = fstatSync(fd);
1142
+ if (!stat.isFile() || stat.dev !== checked.dev || stat.ino !== checked.ino) throw new Error(replaced(path));
1143
+ return { fd, target, stat, bytes: readFileSync(fd) };
1144
+ } catch (cause) {
1145
+ closeSync(fd);
1146
+ throw cause;
1147
+ }
1148
+ }
1149
+
1150
+ /** The change written through the descriptor `openChecked` holds — and only while the name still leads to that file: a link or another file put there since is neither written through nor said to be changed. Returns the descriptor's stat after it. */
1151
+ function writeThrough(path, file, data) {
1152
+ const now = lstatSync(file.target, { throwIfNoEntry: false });
1153
+ if (!now?.isFile() || now.dev !== file.stat.dev || now.ino !== file.stat.ino) throw new Error(replaced(path));
1154
+ overwrite(file.fd, data);
1155
+ return fstatSync(file.fd);
1156
+ }
1157
+
1088
1158
  /** The text editor's four commands, each inside the jail; secret files are neither shown nor written. */
1089
1159
  /** Whether the user opened a place for the session that holds this path (`ctx.opened`, kept by real path: a file, or a directory with everything under it). */
1090
1160
  function openedIn(ctx, path) {
@@ -1154,16 +1224,28 @@ export function runEditor(input, ctx) {
1154
1224
  return result(`${path} can be read, not changed — only files in the project (${ctx.roots[0]}) are edited`, true);
1155
1225
  }
1156
1226
  }
1227
+ // Where a write lands, decided here once — its real path, and the file there as it is now — and carried to the write,
1228
+ // which opens that and nothing else: a name swapped for a link while the work goes on cannot move it.
1229
+ const target = input.command === 'view' ? null : landsAt(path);
1230
+ const checked = target === null ? null : (lstatSync(target, { throwIfNoEntry: false }) ?? null);
1157
1231
  // What git runs by itself — a hook on the next commit, a command its config names — runs outside the sandbox: the shell cannot write it, nor can this.
1232
+ // Each check is made of the name given, the deepest folder there, and where the write lands: a link in the project to the
1233
+ // project (`ln -s . sub`) makes `sub/.npmrc` a new `.npmrc` at its top, which neither of the first two names.
1158
1234
  if (input.command !== 'view') {
1235
+ if (target === null) return result(`${path} goes through a link to something not there yet — not written: say in your report what it needs`, true);
1159
1236
  const at = realAncestor(path);
1160
- const kept = readOnlyPlaces(ctx.roots).find((place) => path === place || path.startsWith(`${place}${sep}`) || at === place || at.startsWith(`${place}${sep}`));
1237
+ const kept = readOnlyPlaces(ctx.roots).find((place) => [path, at, target].some((p) => p === place || p.startsWith(`${place}${sep}`)));
1161
1238
  if (kept) return result(`${path} is git's own hooks or configuration: not changed here — what git runs by itself would run outside the sandbox. Say in your report what it needs.`, true);
1239
+ const hidden = secret ?? guardPath(target);
1240
+ if (hidden) return result(hidden, true);
1162
1241
  // What something else reads later — the shell at its start, a tool before it runs, an editor — is changed only when the user says so, this once.
1163
1242
  const guarded = guardedWhy(path, { roots: ctx.roots }) ?? guardedWhy(at, { roots: ctx.roots });
1164
1243
  if (guarded && ctx.granted !== path) return askFirst(`${path} is changed only when the user says so: ${guarded}`, path, { outside: false, guarded });
1244
+ const lands = guarded ? null : guardedWhy(target, { roots: ctx.roots });
1245
+ if (lands && ctx.granted !== target) return askFirst(`${path} (a link to ${target}) is changed only when the user says so: ${lands}`, target, { outside: false, guarded: lands });
1165
1246
  // A hard link is the same file under another name, perhaps one outside the project or guarded: no path check sees it.
1166
- if (existsSync(path) && lstatSync(path).isFile() && lstatSync(path).nlink > 1 && ctx.granted !== path) return askFirst(`${path} is changed only when the user says so: ${HARD_LINK}`, path, { outside: false, guarded: HARD_LINK });
1247
+ // Judged on the file the write lands on, so a link to it is no way round.
1248
+ if (checked?.isFile() && checked.nlink > 1 && ctx.granted !== path) return askFirst(`${path} is changed only when the user says so: ${HARD_LINK}`, path, { outside: false, guarded: HARD_LINK });
1167
1249
  }
1168
1250
  switch (input.command) {
1169
1251
  case 'view': {
@@ -1189,45 +1271,56 @@ export function runEditor(input, ctx) {
1189
1271
  return result(`${path} already exists (${lines} line${lines === 1 ? '' : 's'}): change the lines that change with str_replace or insert — a file is never rewritten whole`, true);
1190
1272
  }
1191
1273
  mkdirSync(dirname(path), { recursive: true });
1192
- const said = change(path, ctx, String(input.file_text ?? ''), { created: true });
1274
+ const said = change(path, ctx, String(input.file_text ?? ''), { target });
1193
1275
  return result(said(`created ${path}`));
1194
1276
  }
1195
1277
  case 'str_replace': {
1196
1278
  if (secret) return result(secret, true);
1197
1279
  if (!input.old_str) return result('str_replace needs old_str', true);
1198
1280
  if (input.new_str !== undefined && input.old_str === input.new_str) return result('old_str and new_str are the same — nothing would change', true);
1199
- const text = readFileSync(path, 'utf8');
1200
- const gap = placeholderIn(path, 'new_str', input.new_str, text);
1201
- if (gap) return result(gap, true);
1202
- const where = locate(text, input.old_str);
1203
- if (!where) return result(`old_str was not found in ${path}`, true);
1204
- if (where.hits !== 1) return result(`old_str appears ${where.hits} times in ${path}${linesOf(text, input.old_str)} — include more context so it is unique`, true);
1205
- let replacement = String(input.new_str ?? '');
1206
- if (where.how) {
1207
- if (text.includes('\r\n')) replacement = replacement.replace(/\r?\n/g, '\r\n');
1208
- if (where.reindent) replacement = where.reindent(replacement);
1281
+ const file = openChecked(path, target, checked);
1282
+ try {
1283
+ const text = file.bytes.toString('utf8');
1284
+ const gap = placeholderIn(path, 'new_str', input.new_str, text);
1285
+ if (gap) return result(gap, true);
1286
+ const where = locate(text, input.old_str);
1287
+ if (!where) return result(`old_str was not found in ${path}`, true);
1288
+ if (where.hits !== 1) return result(`old_str appears ${where.hits} times in ${path}${linesOf(text, input.old_str)} — include more context so it is unique`, true);
1289
+ let replacement = String(input.new_str ?? '');
1290
+ if (where.how) {
1291
+ if (text.includes('\r\n')) replacement = replacement.replace(/\r?\n/g, '\r\n');
1292
+ if (where.reindent) replacement = where.reindent(replacement);
1293
+ }
1294
+ const said = change(path, ctx, `${text.slice(0, where.start)}${replacement}${text.slice(where.end)}`, { file });
1295
+ return { ...result(said(`edited ${path}${where.how ? ` (old_str matched ${where.how})` : ''}`)), edit: changeOf(text, where, replacement) };
1296
+ } finally {
1297
+ closeSync(file.fd);
1209
1298
  }
1210
- const said = change(path, ctx, `${text.slice(0, where.start)}${replacement}${text.slice(where.end)}`);
1211
- return { ...result(said(`edited ${path}${where.how ? ` (old_str matched ${where.how})` : ''}`)), edit: changeOf(text, where, replacement) };
1212
1299
  }
1213
1300
  case 'insert': {
1214
1301
  if (secret) return result(secret, true);
1215
- const lines = readFileSync(path, 'utf8').split('\n');
1216
- const line = Number(input.insert_line ?? 0);
1217
- if (!Number.isInteger(line)) return result('insert needs insert_line: the number of the line to insert after, 0 for the top of the file', true);
1218
- // The tool sends the text as insert_text, a line with its newline; new_str is what an earlier version of the tool sent.
1219
- const text = input.insert_text ?? input.new_str;
1220
- if (typeof text !== 'string') return result('insert needs insert_text: the text to insert', true);
1221
- const gap = placeholderIn(path, input.insert_text === undefined ? 'new_str' : 'insert_text', text, lines.join('\n'));
1222
- if (gap) return result(gap, true);
1223
- // A file ending in a newline splits to a last empty piece, which is not a line to insert after.
1224
- const at = Math.max(0, Math.min(lines.at(-1) === '' ? lines.length - 1 : lines.length, line));
1225
- lines.splice(at, 0, text.replace(/\n$/, ''));
1226
- const said = change(path, ctx, lines.join('\n'));
1227
- return { ...result(said(`inserted into ${path} after line ${at}`)), edit: { line: at + 1, old: '', new: redact(text.replace(/\n$/, '')).text } };
1302
+ const file = openChecked(path, target, checked);
1303
+ try {
1304
+ const lines = file.bytes.toString('utf8').split('\n');
1305
+ const line = Number(input.insert_line ?? 0);
1306
+ if (!Number.isInteger(line)) return result('insert needs insert_line: the number of the line to insert after, 0 for the top of the file', true);
1307
+ // The tool sends the text as insert_text, a line with its newline; new_str is what an earlier version of the tool sent.
1308
+ const text = input.insert_text ?? input.new_str;
1309
+ if (typeof text !== 'string') return result('insert needs insert_text: the text to insert', true);
1310
+ const gap = placeholderIn(path, input.insert_text === undefined ? 'new_str' : 'insert_text', text, lines.join('\n'));
1311
+ if (gap) return result(gap, true);
1312
+ // A file ending in a newline splits to a last empty piece, which is not a line to insert after.
1313
+ const at = Math.max(0, Math.min(lines.at(-1) === '' ? lines.length - 1 : lines.length, line));
1314
+ lines.splice(at, 0, text.replace(/\n$/, ''));
1315
+ const said = change(path, ctx, lines.join('\n'), { file });
1316
+ return { ...result(said(`inserted into ${path} after line ${at}`)), edit: { line: at + 1, old: '', new: redact(text.replace(/\n$/, '')).text } };
1317
+ } finally {
1318
+ closeSync(file.fd);
1319
+ }
1228
1320
  }
1229
1321
  default:
1230
- return result(`unknown command ${input.command}`, true);
1322
+ // Said with the way out: a model that never saw this tool's own schema (Cursor's) guesses its shape at first.
1323
+ return result(`${input.command === undefined ? 'no command given' : `unknown command ${input.command}`} — command is one of view, create, str_replace, insert`, true);
1231
1324
  }
1232
1325
  } catch (cause) {
1233
1326
  return result(cause.message, true);
package/src/ui.mjs CHANGED
@@ -12,6 +12,7 @@ import { INPUT_SHOWN } from './mcp.mjs';
12
12
  import { paths } from './paths.mjs';
13
13
  import { scratchDir } from './projects.mjs';
14
14
  import { redact } from './redact.mjs';
15
+ import { ownGit } from './sandbox.mjs';
15
16
  import { keyName, settingsPage, SettingsView } from './settings.mjs';
16
17
  import { elapsed } from './text.mjs';
17
18
  import { cleanPaste, editor, keysOf, matchFiles, menuOf, paste, press, under } from './editor.mjs';
@@ -143,7 +144,7 @@ const inTerminal = (program, file) =>
143
144
  /** The files of the project at `cwd` as git knows them, the untracked ones it does not ignore included; none where there is no repository. */
144
145
  const gitFiles = (cwd) =>
145
146
  new Promise((resolve) => {
146
- execFile('git', ['ls-files', '-co', '--exclude-standard'], { cwd, maxBuffer: 32 * 1024 * 1024 }, (error, out) => resolve(error ? [] : out.split('\n').filter(Boolean)));
147
+ execFile('git', ownGit(['ls-files', '-co', '--exclude-standard']), { cwd, maxBuffer: 32 * 1024 * 1024 }, (error, out) => resolve(error ? [] : out.split('\n').filter(Boolean)));
147
148
  });
148
149
 
149
150
  /**
@@ -1461,9 +1462,7 @@ function App({ session, events, messages, cwd, block, opening, logo, clipboard,
1461
1462
  }
1462
1463
  if (control === 'context') {
1463
1464
  record({ kind: 'user', text: line });
1464
- const turns = state.log.filter((entry) => entry.kind === 'user' && !/^[/!]/.test(entry.text)).length;
1465
- const calls = state.log.filter((entry) => entry.kind === 'tool').length;
1466
- return record({ kind: 'reply', raw: contextReport({ contextTokens: session.contextTokens, rows: session.usage(), turns, calls }), own: true });
1465
+ return record({ kind: 'reply', raw: contextReport({ contextTokens: session.contextTokens, rows: session.usage(), ...session.counts }), own: true });
1467
1466
  }
1468
1467
  if (control === 'thinking') {
1469
1468
  record({ kind: 'user', text: line });
@@ -1494,9 +1493,19 @@ function App({ session, events, messages, cwd, block, opening, logo, clipboard,
1494
1493
  if (control === 'config') {
1495
1494
  record({ kind: 'user', text: line });
1496
1495
  const left = () => `${routeLine(session)} ${state.where.name}${state.where.branch ? ` (${state.where.branch})` : ''}`;
1497
- state.settings = settingsPage({ live: { route: (to) => session.route(to), servers: () => session.mcpServers(), left }, cwd: state.where.cwd, onChange: redraw });
1496
+ state.settings = settingsPage({ live: { route: (to) => session.route(to), current: () => ({ model: session.model, effort: session.effort }), servers: () => session.mcpServers(), left }, cwd: state.where.cwd, onChange: redraw });
1498
1497
  return;
1499
1498
  }
1499
+ // Each source as a step of its own, said as it is read; then what was saved, as a table.
1500
+ if (control === 'scorecard') {
1501
+ record({ kind: 'user', text: line });
1502
+ state.working = pickMessage(messages);
1503
+ redraw();
1504
+ const report = await session.scorecard((source) =>
1505
+ record({ kind: 'tool', call: { id: `scorecard-${source.id}`, name: 'Fetch', input: { text: source.name } }, result: { content: source.steps.join('\n'), isError: !source.ok }, took: source.ms }),
1506
+ );
1507
+ return record({ kind: 'reply', raw: report, own: true });
1508
+ }
1500
1509
  if (control === 'memory' || control === 'mcp' || control === 'undo') {
1501
1510
  record({ kind: 'user', text: line });
1502
1511
  const text = control === 'undo' ? session.undo() : control === 'mcp' ? (await session.mcp()).join('\n') : await session.memoryPage();
package/src/verify.mjs CHANGED
@@ -5,8 +5,9 @@ import { join } from 'node:path';
5
5
  import { isSecretPath } from './guard.mjs';
6
6
  import { UsageError } from './memory.mjs';
7
7
  import { secretShape } from './redact.mjs';
8
+ import { ownGit } from './sandbox.mjs';
8
9
  import { readPlainFile, verifyCommands } from './scan.mjs';
9
- import { killGroup } from './tools.mjs';
10
+ import { digest, killGroup } from './tools.mjs';
10
11
 
11
12
  /**
12
13
  * A job's work, judged by running things rather than by reading the report.
@@ -44,7 +45,7 @@ const LOOKS_AWAY =
44
45
 
45
46
  function git(root, args) {
46
47
  try {
47
- return execFileSync('git', ['-C', root, '-c', 'core.quotePath=false', ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], maxBuffer: 64 * 1024 * 1024 });
48
+ return execFileSync('git', ['-C', root, '-c', 'core.quotePath=false', ...ownGit(args)], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], maxBuffer: 64 * 1024 * 1024 });
48
49
  } catch {
49
50
  return null;
50
51
  }
@@ -69,7 +70,7 @@ function hashesOf(root, files) {
69
70
  const there = files.filter((f) => !f.endsWith('/') && existsSync(join(root, f)));
70
71
  if (there.length === 0) return new Map();
71
72
  try {
72
- const out = execFileSync('git', ['-C', root, 'hash-object', '--stdin-paths'], { input: `${there.join('\n')}\n`, encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'], maxBuffer: 64 * 1024 * 1024 });
73
+ const out = execFileSync('git', ['-C', root, ...ownGit(['hash-object', '--stdin-paths'])], { input: `${there.join('\n')}\n`, encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'], maxBuffer: 64 * 1024 * 1024 });
73
74
  return new Map(out.split('\n').slice(0, there.length).map((hash, i) => [there[i], hash]));
74
75
  } catch {
75
76
  // One path git will not hash (a directory, an odd name) must not hide the others: they are asked one at a time.
@@ -136,7 +137,7 @@ function checkOutput(fd, failed) {
136
137
  const lines = bytes.subarray(0, readSync(fd, bytes, 0, bytes.length, start)).toString('utf8').trimEnd().split('\n');
137
138
  // Read from inside a line, the first piece is half of one.
138
139
  if (start > 0 && lines.length > 1) lines.shift();
139
- const failures = failed && start === 0 ? namedFailures(lines) : [];
140
+ const failures = (failed && start === 0 && namedFailures(lines)) || [];
140
141
  return { output: lines.slice(-OUTPUT_TAIL_LINES).join('\n'), failures };
141
142
  }
142
143
 
@@ -191,19 +192,74 @@ const FAILURE_LINE = /\b(?:fail(?:s|ed|ing|ures?)?|errors?|not ok)\b|[✗✕✖]
191
192
  /** A time a runner prints, which differs from one run to the next when nothing else does. */
192
193
  const TIMING = /\b\d+(?:\.\d+)?\s?m?s\b/g;
193
194
 
194
- /** Named cases printed by common test reporters. A wrapper's exit message or a failure count alone identifies no test. */
195
+ /** How long a named case took, after its name — `(0.84ms)`, `(5.1 s)`, vitest's bare `50ms`: it differs from one run to the next. */
196
+ const CASE_TIME = /\s*(?:\(\d+(?:\.\d+)?\s?m?s\)|\s\d+(?:\.\d+)?\s?m?s)$/;
197
+ const countOf = (line, pattern) => Number(pattern.exec(line)?.[1] ?? 0);
198
+ /** A runner's own count of what failed — a line that is never a name — and how many it says. */
199
+ const COUNTS = [
200
+ // pytest: `==== 1 failed, 2 passed, 1 error in 0.02s ====`, bare under -q.
201
+ [/^=*\s*\d+ (?:failed|passed|errors?|skipped|deselected|xfailed|xpassed|warnings?|rerun)\b.* in \d+(?:\.\d+)?s\b/, (l) => countOf(l, /(\d+) failed/) + countOf(l, /(\d+) errors?\b/)],
202
+ // unittest: `FAILED (failures=1, errors=1)`.
203
+ [/^FAILED \(\w+=\d+(?:, \w+=\d+)*\)$/, (l) => countOf(l, /\bfailures=(\d+)/) + countOf(l, /\berrors=(\d+)/)],
204
+ // jest `Tests: 1 failed, 1 passed`, vitest `Tests 1 failed | 1 passed`; node:test's spec and TAP; cargo, a line a target; mocha.
205
+ [/^Tests:?\s+\d+ failed\b/, (l) => countOf(l, /(\d+) failed/)],
206
+ [/^(?:ℹ|#) fail \d+$/, (l) => countOf(l, /(\d+)/)],
207
+ [/^test result: FAILED\./, (l) => countOf(l, /(\d+) failed/)],
208
+ [/^\d+ failing$/, (l) => countOf(l, /(\d+)/)],
209
+ ];
210
+
211
+ /**
212
+ * The cases a check's runner says failed, each under the file, target or package it printed it under — two tests called
213
+ * `works` in two files are two cases — or null when they cannot be compared: fewer named than the runner's own count, or
214
+ * a run it stopped before the rest of the suite ran, which hides whatever it never reached. A count is never a name.
215
+ */
195
216
  function namedFailures(lines) {
196
217
  const names = [];
218
+ let total = null;
219
+ // Where the cases that follow were printed: a cargo target, node's `test at <file>`. Go's are placed by the `FAIL<tab><package>`
220
+ // line that ends their package; node's spec reporter lists its failures again under their files, and that list names them.
221
+ let where = '';
222
+ let unplaced = [];
223
+ let listed = [];
224
+ let summary = false;
225
+ let tap = null;
226
+ let rerun = false;
227
+ let everyTarget = false;
197
228
  for (const raw of lines) {
198
229
  const line = raw.replace(/\x1b\[[0-9;]*m/g, '').trim();
199
- const match = /^(?:not ok(?: \d+)? - |--- FAIL: |(?:FAIL|FAILED) +|[✗✕✖×●] +)(.+)$/.exec(line)
200
- ?? /^test (.+?) \.\.\. FAILED$/.exec(line);
201
- if (match) {
202
- const name = match[1].replace(/\s*\(\d+(?:\.\d+)?\s?m?s\)$/, '').trim();
203
- if (name) names.push(createHash('sha256').update(name).digest('hex'));
230
+ if (/^!+ stopping after \d+ failures? !+$|^Interrupted: /.test(line)) return null;
231
+ if (/^error: test failed, to rerun pass\b/.test(line)) rerun = true;
232
+ if (/^error: \d+ targets? failed:$/.test(line)) everyTarget = true;
233
+ const count = COUNTS.find(([pattern]) => pattern.test(line));
234
+ if (count) {
235
+ total = (total ?? 0) + count[1](line);
236
+ continue;
237
+ }
238
+ let m;
239
+ if ((m = /^Running (?:unittests )?(\S+)/.exec(line) ?? /^(Doc-tests \S+)/.exec(line) ?? /^test at (.+?)(?::\d+)*$/.exec(line))) where = m[1];
240
+ else if ((m = /^location: '(.+?)(?::\d+)*'$/.exec(line))) {
241
+ if (tap !== null) names[tap] = `${m[1]} ${names[tap]}`;
242
+ tap = null;
243
+ } else if (/^✖ failing tests:$/.test(line)) [summary, listed] = [true, []];
244
+ else if ((m = /^--- FAIL: (.+)$/.exec(line))) unplaced.push(m[1].replace(CASE_TIME, ''));
245
+ else if ((m = /^FAIL\t(\S+)(?:\t\S+)?(?: (\[[^\]]+\]))?$/.exec(line))) {
246
+ names.push(...unplaced.map((name) => `${m[1]} ${name}`), [m[1], m[2]].filter(Boolean).join(' '));
247
+ unplaced = [];
248
+ } else if ((m = /^(FAILED|ERROR) (\S+\.py\b.*?)(?: - .*)?$/.exec(line) ?? /^(FAIL|ERROR): (.+)$/.exec(line))) names.push(`${m[1]} ${m[2]}`);
249
+ else if ((m = /^test (.+?) \.\.\. FAILED$/.exec(line))) names.push(`${where} ${m[1]}`.trim());
250
+ else if ((m = /^not ok(?: \d+)? - (.+)$/.exec(line))) tap = names.push(m[1].replace(CASE_TIME, '')) - 1;
251
+ else if ((m = /^(✖|[✗✕×●]|FAIL|FAILED) +(.+)$/.exec(line))) {
252
+ const name = m[2].replace(CASE_TIME, '').trim();
253
+ if (!name || /^\(?\w+=\d+(?:,\s*\w+=\d+)*\)?$|^\d+ (?:failed|failing|errors?|tests?)\b/.test(name)) continue;
254
+ if (m[1] !== '✖') names.push(name);
255
+ else if (summary) names.push(`${where} ${name}`.trim());
256
+ else listed.push(name);
204
257
  }
205
258
  }
206
- return names;
259
+ if (rerun && !everyTarget) return null;
260
+ names.push(...unplaced, ...listed);
261
+ if (total !== null && names.length < total) return null;
262
+ return names.map(digest);
207
263
  }
208
264
 
209
265
  /** A new failure must not be forgiven just because the same command was red before. Count duplicate case names as well. */
@@ -406,7 +462,7 @@ export function writeChanges(root, snap, file) {
406
462
  // What git never tracked has no earlier side to show: a new file, and one changed outside git, are given whole.
407
463
  for (const created of [...changes.created, ...outside.filter((c) => c.status === 'M').map((c) => c.path)]) {
408
464
  // `--no-index` exits 1 when the files differ, which is every time; the diff is on stdout either way.
409
- const run = spawnSync('git', ['-C', root, '-c', 'core.quotePath=false', ...DIFF, '--no-index', '--', '/dev/null', created], { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 });
465
+ const run = spawnSync('git', ['-C', root, '-c', 'core.quotePath=false', ...ownGit([...DIFF, '--no-index', '--', '/dev/null', created])], { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 });
410
466
  parts.push(run.stdout?.trimEnd() ?? '');
411
467
  }
412
468
  writeFileSync(file, `${parts.filter(Boolean).join('\n')}\n`, { mode: 0o600 });