codex-supervisor-mcp 0.11.0 → 0.12.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.
@@ -22,8 +22,33 @@ function uniqueSorted(values) {
22
22
 
23
23
  export function normalizeObservedCommand(command) {
24
24
  const text = String(command ?? "").trim();
25
- const wrapped = text.match(/^(?:[^\s]+\/)?(?:zsh|bash|sh)\s+-l?c\s+(['"])([\s\S]*)\1$/);
26
- return wrapped ? wrapped[2].trim() : text;
25
+ const wrapped = text.match(/^(?:[^\s]+\/)?(?:zsh|bash|sh)\s+-l?c\s+([\s\S]+)$/);
26
+ if (!wrapped) return text;
27
+ let body = wrapped[1].trim();
28
+ const quote = body[0];
29
+ if ((quote === "'" || quote === '"') && body.at(-1) === quote) body = body.slice(1, -1);
30
+ // Codex may display the wrapped command with JSON-style escaped quotes.
31
+ // Decode those before reversing zsh's display-only quote insertions.
32
+ body = body.replace(/\\"/g, '"');
33
+ // An interactive zsh protects history expansion and quoted literals by
34
+ // inserting "' around them, for example if("'!ready) and ('"'setup').
35
+ // Both are display artifacts, not part of the command that ran.
36
+ body = body.replace(/"'/g, "");
37
+ // Codex displays a shell command that already contains single quotes by
38
+ // closing and reopening the outer quote around them: -run '"^Test$'"'.
39
+ // Decode that display-only wrapping before comparing it with the contract.
40
+ body = body.replace(/'"'/g, "'");
41
+ // Codex may quote path arguments while displaying a command even when the
42
+ // dispatcher wrote the path bare. Absolute, home-relative and Windows paths
43
+ // are quote-safe here because a path containing whitespace remains quoted in
44
+ // both forms and is not touched by this rule.
45
+ body = body.replace(/(^|\s)['"]((?:[A-Za-z]:[\\/]|\/|\.\/|\.\.\/|~\/)[^'"\s]*)['"](?=\s|$)/g, "$1$2");
46
+ return body.trim();
47
+ }
48
+
49
+ export function commandsEquivalent(observed, expected) {
50
+ const normalizedExpected = normalizeObservedCommand(expected);
51
+ return normalizeObservedCommand(observed) === normalizedExpected;
27
52
  }
28
53
 
29
54
  export function buildRuntimeContract({
@@ -36,6 +61,7 @@ export function buildRuntimeContract({
36
61
  writableRoots,
37
62
  context,
38
63
  contracts,
64
+ workspaceSetup = null,
39
65
  workspaceKind = "git_worktree",
40
66
  workspaceManifestSha256 = null
41
67
  }) {
@@ -53,7 +79,8 @@ export function buildRuntimeContract({
53
79
  context: (context ?? [])
54
80
  .map((entry) => ({ path: resolve(entry.path), sha256: entry.sha256 ?? null }))
55
81
  .sort((left, right) => left.path.localeCompare(right.path)),
56
- contracts: uniqueSorted(contracts)
82
+ contracts: uniqueSorted(contracts),
83
+ workspace_setup: stableValue(workspaceSetup)
57
84
  });
58
85
  }
59
86
 
@@ -80,7 +107,7 @@ export function readPreflightResult(task) {
80
107
 
81
108
  const commands = Array.isArray(task.commands) ? task.commands : [];
82
109
  const probes = metadata.probe_commands.map((command) => {
83
- const matches = commands.filter((entry) => normalizeObservedCommand(entry.command) === command);
110
+ const matches = commands.filter((entry) => commandsEquivalent(entry.command, command));
84
111
  const result = matches.at(-1) ?? null;
85
112
  return {
86
113
  command,
@@ -92,9 +119,22 @@ export function readPreflightResult(task) {
92
119
  });
93
120
  const terminal = ["completed", "failed", "cancelled", "lost"].includes(task.status);
94
121
  const failedCommands = commands.filter(commandFailed);
122
+ const probesPassed = probes.every((probe) => probe.passed);
123
+ const exitConfirmed = task.status === "completed" && task.exit_code === 0;
124
+ const reportWasInvalid = task.attention_reason === "invalid_report";
125
+ const exitWasLost = task.status === "lost" && task.exit_code === 0;
126
+ // A preflight proves the worker environment through its command evidence.
127
+ // Its own final report and close-handler completion are useful diagnostics,
128
+ // but neither changes what those commands already proved. A completed turn
129
+ // without a recorded exit is accepted for this probe-only task; real failed,
130
+ // cancelled or unexplained-lost commands still fail below.
131
+ const workerOutcomeAcceptable = exitConfirmed
132
+ || (task.status === "completed" && task.exit_code == null)
133
+ || reportWasInvalid
134
+ || exitWasLost;
95
135
  let status = "running";
96
136
  if (terminal) {
97
- status = task.status === "completed" && task.exit_code === 0 && probes.every((probe) => probe.passed) && failedCommands.length === 0
137
+ status = workerOutcomeAcceptable && probesPassed && failedCommands.length === 0
98
138
  ? "passed"
99
139
  : "failed";
100
140
  }
@@ -104,6 +144,13 @@ export function readPreflightResult(task) {
104
144
  terminal,
105
145
  contract_hash: metadata.contract_hash,
106
146
  contract: metadata.contract,
147
+ worker_outcome: {
148
+ status: task.status,
149
+ exit_code: task.exit_code ?? null,
150
+ exit_confirmed: exitConfirmed,
151
+ report_ignored: reportWasInvalid,
152
+ missing_exit_tolerated: task.status === "completed" && task.exit_code == null || exitWasLost
153
+ },
107
154
  probes,
108
155
  failed_commands: failedCommands.map((entry) => ({
109
156
  command: entry.command,
package/src/worktree.js CHANGED
@@ -12,7 +12,7 @@
12
12
  // base is the commit the worktree was created from.
13
13
  import { execFileSync } from "node:child_process";
14
14
  import { existsSync, readFileSync, lstatSync } from "node:fs";
15
- import { join } from "node:path";
15
+ import { join, relative, resolve } from "node:path";
16
16
  import { truncateMiddleChars } from "./truncate.js";
17
17
  import { createHash } from "node:crypto";
18
18
 
@@ -71,7 +71,7 @@ export function readWorktreeChanges(worktreePath, { baseCommit = null, branch =
71
71
  if (!worktreePath || !existsSync(worktreePath)) return [];
72
72
  const found = new Set();
73
73
  try {
74
- const output = git(worktreePath, ["status", "--porcelain"]);
74
+ const output = git(worktreePath, ["status", "--porcelain", "--untracked-files=all"]);
75
75
  for (const entry of output.split("\n").map(parsePorcelainLine).filter(Boolean)) found.add(entry);
76
76
  } catch {
77
77
  return [];
@@ -97,7 +97,30 @@ export function readWorktreeChanges(worktreePath, { baseCommit = null, branch =
97
97
  // file that does not fit is cut in the middle (truncated: true), and files
98
98
  // after the budget is spent come back without a patch so the list of what
99
99
  // changed is still complete.
100
- export function readWorktreeDiff(worktreePath, { baseCommit = null, branch = null } = {}, { maxChars = 60000, paths = [] } = {}) {
100
+ function normalizedPath(path) {
101
+ return String(path ?? "").replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, "");
102
+ }
103
+
104
+ function underPath(path, prefix) {
105
+ const file = normalizedPath(path);
106
+ const base = normalizedPath(prefix);
107
+ return base !== "" && (file === base || file.startsWith(`${base}/`));
108
+ }
109
+
110
+ export function workspaceSetupIgnorePaths(task) {
111
+ return (task?.workspace_metadata?.setup?.links ?? []).map((link) => normalizedPath(link.path));
112
+ }
113
+
114
+ export function filterWorkspaceSetupChanges(task, changes) {
115
+ const worktree = resolve(task?.worktree_path ?? task?.cwd ?? task?.project_root ?? ".");
116
+ const ignored = workspaceSetupIgnorePaths(task);
117
+ return (changes ?? []).filter((change) => {
118
+ const item = normalizedPath(relative(worktree, resolve(worktree, change)));
119
+ return !ignored.some((path) => underPath(item, path));
120
+ });
121
+ }
122
+
123
+ export function readWorktreeDiff(worktreePath, { baseCommit = null, branch = null } = {}, { maxChars = 60000, paths = [], ignorePaths = [] } = {}) {
101
124
  if (!worktreePath || !existsSync(worktreePath)) return { error: "no_worktree", files: [] };
102
125
  const base = resolveWorktreeBase(worktreePath, { baseCommit, branch });
103
126
  if (!base) return { error: "no_base_commit", files: [] };
@@ -108,6 +131,8 @@ export function readWorktreeDiff(worktreePath, { baseCommit = null, branch = nul
108
131
  // a worktree without a resolvable HEAD still has a working tree to diff
109
132
  }
110
133
  const scope = Array.isArray(paths) && paths.length > 0 ? ["--", ...paths] : [];
134
+ const ignored = ignorePaths.map(normalizedPath);
135
+ const isIgnored = (path) => ignored.some((entry) => underPath(path, entry));
111
136
 
112
137
  const files = [];
113
138
  const known = new Set();
@@ -115,7 +140,7 @@ export function readWorktreeDiff(worktreePath, { baseCommit = null, branch = nul
115
140
  for (const line of nonEmptyLines(git(worktreePath, ["diff", "--name-status", "-M", base, ...scope]))) {
116
141
  const [code, ...rest] = line.split("\t");
117
142
  const path = rest.at(-1);
118
- if (!path) continue;
143
+ if (!path || isIgnored(path)) continue;
119
144
  known.add(path);
120
145
  files.push({ path, status: statusWord(code), from: code.startsWith("R") && rest.length > 1 ? rest[0] : null });
121
146
  }
@@ -126,7 +151,8 @@ export function readWorktreeDiff(worktreePath, { baseCommit = null, branch = nul
126
151
  try {
127
152
  for (const line of nonEmptyLines(git(worktreePath, ["diff", "--numstat", "-M", base, ...scope]))) {
128
153
  const [added, deleted, ...rest] = line.split("\t");
129
- counts.set(rest.at(-1), { additions: added === "-" ? null : Number(added), deletions: deleted === "-" ? null : Number(deleted) });
154
+ const path = rest.at(-1);
155
+ if (!isIgnored(path)) counts.set(path, { additions: added === "-" ? null : Number(added), deletions: deleted === "-" ? null : Number(deleted) });
130
156
  }
131
157
  } catch {
132
158
  // counts are decoration
@@ -137,7 +163,7 @@ export function readWorktreeDiff(worktreePath, { baseCommit = null, branch = nul
137
163
  for (const line of git(worktreePath, ["status", "--porcelain", "--untracked-files=all", ...scope]).split("\n")) {
138
164
  if (!line.startsWith("??")) continue;
139
165
  const path = parsePorcelainLine(line);
140
- if (!path || known.has(path)) continue;
166
+ if (!path || isIgnored(path) || known.has(path)) continue;
141
167
  known.add(path);
142
168
  files.push({ path, status: "untracked", from: null });
143
169
  }
@@ -183,6 +209,32 @@ export function readWorktreeDiff(worktreePath, { baseCommit = null, branch = nul
183
209
  return { base_commit: base, head, files, total_files: files.length, truncated_files: truncatedFiles };
184
210
  }
185
211
 
212
+ export function splitPatchHunks(file) {
213
+ let patch = String(file.patch ?? "");
214
+ if (file.status === "untracked") {
215
+ const count = patch === "" ? 0 : patch.split("\n").length;
216
+ patch = [`--- /dev/null`, `+++ b/${file.path}`, `@@ -0,0 +1,${count} @@`, patch].join("\n");
217
+ }
218
+ if (!patch) return { headers: "", hunks: [] };
219
+ const lines = patch.split("\n");
220
+ const starts = [];
221
+ for (let index = 0; index < lines.length; index += 1) if (lines[index].startsWith("@@")) starts.push(index);
222
+ if (starts.length === 0) lines.unshift("@@ -0,0 +0,0 @@"), starts.push(0);
223
+ const headers = lines.slice(0, starts[0]).join("\n");
224
+ const hunks = starts.map((start, index) => {
225
+ const body = lines.slice(start, starts[index + 1] ?? lines.length).join("\n").replace(/\n+$/, "");
226
+ const counts = body.split("\n").slice(1).filter((line) => !line.startsWith("\\"));
227
+ return {
228
+ index,
229
+ header: lines[start],
230
+ additions: counts.filter((line) => line.startsWith("+") && !line.startsWith("+++")).length,
231
+ deletions: counts.filter((line) => line.startsWith("-") && !line.startsWith("---")).length,
232
+ patch: headers ? `${headers}\n${body}` : body
233
+ };
234
+ });
235
+ return { headers, hunks };
236
+ }
237
+
186
238
  function statusWord(code) {
187
239
  switch (code[0]) {
188
240
  case "A": return "added";
@@ -210,14 +262,14 @@ export function worktreeRef(task) {
210
262
  }
211
263
 
212
264
  export function readTaskChanges(task) {
213
- return mergeChangedFiles(task.changed_files, readWorktreeChanges(task.worktree_path, worktreeRef(task)));
265
+ return filterWorkspaceSetupChanges(task, mergeChangedFiles(task.changed_files, readWorktreeChanges(task.worktree_path, worktreeRef(task))));
214
266
  }
215
267
 
216
268
  // Compare output at this run's start/end, rather than treating the previous
217
269
  // run's committed or uncommitted work as fresh evidence. If the bounded read
218
270
  // cannot prove the full state, leave the fingerprint unknown.
219
271
  export function snapshotTaskOutput(task) {
220
- const diff = readWorktreeDiff(task.worktree_path, worktreeRef(task), { maxChars: 2_000_000 });
272
+ const diff = readWorktreeDiff(task.worktree_path, worktreeRef(task), { maxChars: 2_000_000, ignorePaths: workspaceSetupIgnorePaths(task) });
221
273
  if (diff.error || diff.truncated_files > 0 || diff.files.some((file) => file.patch == null)) return null;
222
274
  try {
223
275
  let remaining = 2_000_000;