codex-supervisor-mcp 0.11.1 → 0.12.1

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/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;