codex-supervisor-mcp 0.7.3 → 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/prompt.js CHANGED
@@ -1,12 +1,23 @@
1
- // Worker prompt framing.
1
+ // Worker prompt framing: what goes around the task text.
2
2
  //
3
- // Codex exposes goal tools (create_goal / get_goal / update_goal) to every
4
- // thread, but the model does not create a goal on its own. A worker therefore
5
- // starts with no native goal unless we ask for one, which is why the supervisor
6
- // prepends this block instead of only recording the goal on its own side.
3
+ // Goal preamble, in front. Codex exposes goal tools (create_goal / get_goal /
4
+ // update_goal) to every thread, but the model does not create a goal on its
5
+ // own. A worker therefore starts with no native goal unless we ask for one,
6
+ // which is why the supervisor prepends this block instead of only recording
7
+ // the goal on its own side. create_goal fails when an unfinished goal already
8
+ // exists, so the resume path must not ask for one: that thread already has
9
+ // its goal.
7
10
  //
8
- // create_goal fails when an unfinished goal already exists, so the resume path
9
- // must not ask for one: that thread already has its goal.
11
+ // Report format, at the end. A worker's last message is what the supervisor
12
+ // reads back through get_worker_result. Left to itself a worker narrates
13
+ // the whole run and lists every file it touched, and one real report ran to
14
+ // four thousand tokens with the conclusion buried in the middle. The format
15
+ // asks for the three things a reviewer checks and nothing else: the result,
16
+ // the commands that prove it, and what was left out. Files are not listed
17
+ // because the supervisor reads the diff. It goes after the task text because
18
+ // a format instruction at the end of a long prompt is the one that is
19
+ // followed, and because "the task's own format wins" has to come after the
20
+ // task.
10
21
 
11
22
  function oneLine(value) {
12
23
  return String(value ?? "").replace(/\s+/g, " ").trim();
@@ -33,7 +44,49 @@ export function buildGoalPreamble({ objective, tokenBudget = null, resume = fals
33
44
  ].join("\n");
34
45
  }
35
46
 
47
+ export const REPORT_SECTIONS = ["Result", "Verification", "Not done / risks"];
48
+
49
+ // The acceptance commands are shown to the worker. Hiding them would not
50
+ // stop a worker from gaming a check (it can see the tests in the tree
51
+ // anyway) and would stop it from running them before it reports, which is
52
+ // the cheapest fix loop there is. The supervisor runs them again after the
53
+ // worker exits regardless of what the worker says.
54
+ export function buildAcceptanceBlock(acceptance, { resume = false } = {}) {
55
+ const commands = Array.isArray(acceptance) ? acceptance.filter(Boolean) : [];
56
+ if (commands.length === 0) return "";
57
+ if (resume) {
58
+ return `Acceptance is unchanged; run it before you report: ${commands.join(" ; ")}`;
59
+ }
60
+ return [
61
+ "Acceptance: when you exit, the supervisor runs these commands in this directory and each must exit 0:",
62
+ ...commands.map((command) => `- ${command}`),
63
+ "Run them yourself before you report and put their exit codes under Verification. Do not edit what they test to make them pass; if a check is wrong, say why under Not done / risks."
64
+ ].join("\n");
65
+ }
66
+
67
+ export function buildReportPreamble({ resume = false, acceptance = null } = {}) {
68
+ const acceptanceBlock = buildAcceptanceBlock(acceptance, { resume });
69
+ const withAcceptance = (text) => (acceptanceBlock ? `${text}\n\n${acceptanceBlock}` : text);
70
+ if (resume) {
71
+ return withAcceptance("Report as before: Result / Verification (every command you ran to check, with exit codes) / Not done and risks. Under 2000 characters, no list of changed files, quote raw errors verbatim.");
72
+ }
73
+ return withAcceptance([
74
+ "Final report (your last message), under 2000 characters, in the language of the task. Quoted command output does not count toward the limit.",
75
+ "1. Result: what is done, in two or three sentences. Say plainly what is not done.",
76
+ "2. Verification: every command you ran to check the work, its exit code, and the output lines that prove the result. Write \"not verified\" if you ran none.",
77
+ "3. Not done / risks: what you skipped, what you are unsure of, what the reviewer should look at first.",
78
+ "Do not list the files you changed; the supervisor reads the diff.",
79
+ "Never call a failure an environment problem without quoting the raw error verbatim.",
80
+ "If the task above specifies its own report format, use that format and still cover the three points."
81
+ ].join("\n"));
82
+ }
83
+
36
84
  export function withGoalPreamble(prompt, goalOptions) {
37
85
  const preamble = buildGoalPreamble(goalOptions);
38
86
  return preamble ? `${preamble}\n\n${prompt}` : prompt;
39
87
  }
88
+
89
+ // The whole frame: goal in front, task text, report format at the end.
90
+ export function frameWorkerPrompt(prompt, goalOptions, { acceptance = null } = {}) {
91
+ return `${withGoalPreamble(prompt, goalOptions)}\n\n${buildReportPreamble({ resume: Boolean(goalOptions?.resume), acceptance })}`;
92
+ }
package/src/task-store.js CHANGED
@@ -24,7 +24,12 @@ const POST_V1_COLUMNS = [
24
24
  ["cancel_requested_at", "TEXT"],
25
25
  ["notices", "TEXT"],
26
26
  ["session_id", "TEXT"],
27
- ["base_commit", "TEXT"]
27
+ ["base_commit", "TEXT"],
28
+ ["acceptance", "TEXT"],
29
+ ["acceptance_timeout_ms", "INTEGER"],
30
+ ["acceptance_results", "TEXT"],
31
+ ["landed_at", "TEXT"],
32
+ ["landed_head", "TEXT"]
28
33
  ];
29
34
 
30
35
  const TASKS_DDL = `
@@ -74,6 +79,11 @@ const TASKS_DDL = `
74
79
  notices TEXT,
75
80
  worktree_path TEXT,
76
81
  base_commit TEXT,
82
+ acceptance TEXT,
83
+ acceptance_timeout_ms INTEGER,
84
+ acceptance_results TEXT,
85
+ landed_at TEXT,
86
+ landed_head TEXT,
77
87
  run_log TEXT NOT NULL
78
88
  );
79
89
  `;
@@ -86,7 +96,7 @@ const TASK_COLUMNS = [
86
96
  "current_command", "error", "followup_of", "resumed_from", "session_id", "thread_id", "owned_paths",
87
97
  "depends_on", "goal_objective", "goal_token_budget", "goal_status", "goal_tokens_used",
88
98
  "goal_time_used_seconds", "goal_updated_at", "run_count", "cancel_requested_at",
89
- "notices", "worktree_path", "base_commit", "run_log"
99
+ "notices", "worktree_path", "base_commit", "acceptance", "acceptance_timeout_ms", "acceptance_results", "landed_at", "landed_head", "run_log"
90
100
  ];
91
101
 
92
102
  function tableColumns(dbInstance, tableName) {
@@ -293,6 +303,8 @@ function rowToTask(row) {
293
303
  commands: decodeJson(row.commands, []),
294
304
  owned_paths: decodeJson(row.owned_paths, []),
295
305
  depends_on: decodeJson(row.depends_on, []),
306
+ acceptance: decodeJson(row.acceptance, null),
307
+ acceptance_results: decodeJson(row.acceptance_results, null),
296
308
  pid: row.pid ?? null,
297
309
  exit_code: row.exit_code ?? null,
298
310
  phase: row.phase ?? null,
@@ -341,7 +353,29 @@ function neverStarted(task, now = Date.now()) {
341
353
  return now - created > NEVER_STARTED_GRACE_MS;
342
354
  }
343
355
 
356
+ // A terminal row whose acceptance run was cut short: the MCP process that
357
+ // was running the checks is gone. Left alone it would read as "running"
358
+ // forever and land would treat it as never run.
359
+ async function reconcileInterruptedAcceptance() {
360
+ const tasks = await readTasks();
361
+ const interrupted = tasks.filter((task) =>
362
+ TERMINAL_STATUSES.has(task.status)
363
+ && task.acceptance_results?.status === "running"
364
+ && !isPidAlive(task.acceptance_results.supervisor_pid));
365
+ for (const task of interrupted) {
366
+ const now = new Date().toISOString();
367
+ await writeTask({
368
+ ...task,
369
+ acceptance_results: { ...task.acceptance_results, status: "interrupted", passed: null, finished_at: now },
370
+ current_action: "Acceptance interrupted: the supervisor process exited while running the checks",
371
+ updated_at: now
372
+ });
373
+ }
374
+ return interrupted;
375
+ }
376
+
344
377
  export async function reconcileDetachedActiveTasks() {
378
+ await reconcileInterruptedAcceptance();
345
379
  const tasks = await readActiveTasks();
346
380
  const staleTasks = tasks.filter((task) => (task.pid && !isPidAlive(task.pid)) || neverStarted(task));
347
381
  for (const task of staleTasks) {
@@ -386,6 +420,10 @@ export async function writeTask(task) {
386
420
  params.commands = encodeJson(task.commands);
387
421
  params.owned_paths = encodeJson(task.owned_paths);
388
422
  params.depends_on = encodeJson(task.depends_on);
423
+ // JSON columns: node:sqlite refuses to bind an array or object, and a
424
+ // refused bind drops the whole row update.
425
+ params.acceptance = task.acceptance ? JSON.stringify(task.acceptance) : null;
426
+ params.acceptance_results = task.acceptance_results ? JSON.stringify(task.acceptance_results) : null;
389
427
  params.run_count = task.run_count ?? 0;
390
428
  params.phase = task.phase ?? null;
391
429
 
package/src/truncate.js CHANGED
@@ -93,6 +93,19 @@ export function truncateMiddleChars(s, maxBytes) {
93
93
  return truncateWithByteEstimate(s, maxBytes, false);
94
94
  }
95
95
 
96
+ // A worker's report leads with its conclusion and ends with what it left
97
+ // out, so a clipped report keeps most of the head and some of the tail
98
+ // instead of an even split, which cut exactly the verification section out
99
+ // of a three-part report.
100
+ export function truncateReport(s, maxBytes, headShare = 0.7) {
101
+ if (s === "") return s;
102
+ const totalBytes = Buffer.byteLength(s, "utf8");
103
+ if (maxBytes <= 0 || totalBytes <= maxBytes) return s;
104
+ const head = Math.floor(maxBytes * headShare);
105
+ const { removedChars, before, after } = splitString(s, head, maxBytes - head);
106
+ return `${before}\n${formatTruncationMarker(false, removedChars)}\n${after}`;
107
+ }
108
+
96
109
  export function truncateMiddleWithTokenBudget(s, maxTokens) {
97
110
  if (s === "") return { text: "", originalTokenCount: null };
98
111
  if (maxTokens > 0 && Buffer.byteLength(s, "utf8") <= approxBytesForTokens(maxTokens)) {
package/src/web-app.js CHANGED
@@ -82,6 +82,8 @@ function workerRow(task) {
82
82
  current_action: clip(task.current_action, 300),
83
83
  last_message: clip(task.last_message, 400),
84
84
  changed_file_count: liveChangeCount(task),
85
+ acceptance: task.acceptance_results?.status ?? null,
86
+ landed_at: task.landed_at ?? null,
85
87
  command_count: (task.commands ?? []).length,
86
88
  error: task.error ?? null,
87
89
  exit_code: task.exit_code ?? null,
package/src/worktree.js CHANGED
@@ -211,3 +211,31 @@ export function worktreeRef(task) {
211
211
  export function readTaskChanges(task) {
212
212
  return mergeChangedFiles(task.changed_files, readWorktreeChanges(task.worktree_path, worktreeRef(task)));
213
213
  }
214
+
215
+ // How far the project has moved since the worktree was cut. Compared with
216
+ // the project directory's HEAD, which is the branch the main thread
217
+ // integrates on. contains_base false means the base was rewritten (an
218
+ // amend or a rebase on the main branch) and the count is of a history the
219
+ // base is not in; every git error is reported as null, never as 0.
220
+ export function baseBehind(projectRoot, baseCommit) {
221
+ if (!projectRoot || !baseCommit || !existsSync(projectRoot)) return { behind: null, contains_base: null, ref: null };
222
+ let ref = null;
223
+ try {
224
+ ref = git(projectRoot, ["symbolic-ref", "--short", "-q", "HEAD"]).trim() || "HEAD";
225
+ } catch {
226
+ ref = "HEAD";
227
+ }
228
+ try {
229
+ const behind = Number(git(projectRoot, ["rev-list", "--count", `${baseCommit}..HEAD`]).trim());
230
+ let containsBase = true;
231
+ try {
232
+ git(projectRoot, ["merge-base", "--is-ancestor", baseCommit, "HEAD"]);
233
+ } catch (error) {
234
+ if (error.status === 1) containsBase = false;
235
+ else return { behind: null, contains_base: null, ref };
236
+ }
237
+ return { behind: Number.isFinite(behind) ? behind : null, contains_base: containsBase, ref };
238
+ } catch {
239
+ return { behind: null, contains_base: null, ref };
240
+ }
241
+ }