codex-supervisor-mcp 0.13.4 → 0.13.6
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/README.en.md +101 -297
- package/README.md +99 -310
- package/package.json +24 -63
- package/src/{acceptance.js → app/acceptance.js} +54 -68
- package/src/app/cancel.js +60 -0
- package/src/{cleanup-plan.js → app/cleanup-plan.js} +13 -14
- package/src/app/compose.js +7 -0
- package/src/{context-compiler.js → app/context-compiler.js} +7 -13
- package/src/app/dispatch.js +396 -0
- package/src/app/followup.js +98 -0
- package/src/app/land-chain.js +124 -0
- package/src/app/landing.js +357 -0
- package/src/app/ownership.js +57 -0
- package/src/app/rebase.js +216 -0
- package/src/app/resume.js +164 -0
- package/src/app/run-state.js +26 -0
- package/src/app/settled.js +28 -0
- package/src/{side-chat.js → app/side-chat.js} +79 -266
- package/src/app/sse.js +20 -0
- package/src/app/start-run.js +77 -0
- package/src/app/verify-push.js +119 -0
- package/src/app/worktree-setup.js +176 -0
- package/src/{acceptance-baseline.js → domain/acceptance-baseline.js} +2 -1
- package/src/domain/acceptance-spec.js +30 -0
- package/src/domain/command-outcome.js +6 -0
- package/src/{overview-delta.js → domain/overview-delta.js} +5 -1
- package/src/domain/side-turn-view.js +73 -0
- package/src/{worker-preflight.js → domain/worker-preflight.js} +1 -1
- package/src/infra/git.js +30 -0
- package/src/infra/process.js +59 -0
- package/src/{sandbox-roots.js → infra/sandbox-roots.js} +2 -1
- package/src/{skill-sync.js → infra/skill-sync.js} +1 -1
- package/src/infra/store/db.js +265 -0
- package/src/infra/store/events.js +146 -0
- package/src/infra/store/facts.js +338 -0
- package/src/infra/store/reconcile.js +172 -0
- package/src/infra/store/records.js +160 -0
- package/src/infra/store/runner-readers.js +16 -0
- package/src/infra/store/schema.js +306 -0
- package/src/infra/store/sessions.js +138 -0
- package/src/infra/store/tasks.js +307 -0
- package/src/{auto-update-worker.js → infra/update/auto-update-worker.js} +4 -4
- package/src/{auto-update.js → infra/update/auto-update.js} +3 -4
- package/src/{update-check.js → infra/update/update-check.js} +4 -4
- package/src/{workspace-kind.js → infra/workspace-kind.js} +5 -7
- package/src/{worktree.js → infra/worktree.js} +57 -42
- package/src/{acceptance-cli.js → interface/bin/acceptance-cli.js} +7 -3
- package/src/{cleanup.js → interface/bin/cleanup.js} +3 -2
- package/src/{monitor.jsx → interface/bin/monitor.jsx} +4 -1
- package/src/{setup.js → interface/bin/setup.js} +9 -8
- package/src/{stage.js → interface/bin/stage.js} +1 -1
- package/src/{web-server.js → interface/bin/web-server.js} +2 -2
- package/src/interface/mcp/meta.js +7 -0
- package/src/interface/mcp/result.js +34 -0
- package/src/interface/mcp/schemas.js +24 -0
- package/src/interface/mcp/server.js +94 -0
- package/src/interface/mcp/tools/delivery.js +197 -0
- package/src/interface/mcp/tools/dispatch.js +267 -0
- package/src/interface/mcp/tools/facts.js +239 -0
- package/src/interface/mcp/tools/overview.js +202 -0
- package/src/interface/mcp/tools/poll.js +62 -0
- package/src/interface/mcp/tools/results.js +270 -0
- package/src/interface/mcp/tools/review.js +135 -0
- package/src/interface/mcp/tools/wait.js +145 -0
- package/src/interface/mcp/update-state.js +34 -0
- package/src/interface/mcp/views/overview.js +183 -0
- package/src/interface/mcp/views/task.js +223 -0
- package/src/interface/web/app.js +226 -0
- package/src/interface/web/views.js +224 -0
- package/src/mcp-server.js +3 -2223
- package/src/run-host/entry.js +23 -0
- package/src/run-host/finalize.js +236 -0
- package/src/run-host/launch.js +63 -0
- package/src/run-host/live-run.js +73 -0
- package/src/run-host/outcome-facts.js +61 -0
- package/src/run-host/streams.js +76 -0
- package/src/run-host/track.js +128 -0
- package/src/{runner-adapter.js → runners/codex/adapter.js} +6 -6
- package/src/runners/codex/chat-chunks.js +97 -0
- package/src/{event-parser.js → runners/codex/event-parser.js} +57 -55
- package/src/{goal-store.js → runners/codex/goal-store.js} +2 -2
- package/src/{mcp-config.js → runners/codex/mcp-config.js} +1 -1
- package/src/codex-runner.js +0 -1586
- package/src/landing.js +0 -507
- package/src/task-store.js +0 -1800
- package/src/web-app.js +0 -425
- /package/src/{failure-class.js → domain/failure-class.js} +0 -0
- /package/src/{owned-changes.js → domain/owned-changes.js} +0 -0
- /package/src/{prompt.js → domain/prompt.js} +0 -0
- /package/src/{status.js → domain/status.js} +0 -0
- /package/src/{thread-health.js → domain/thread-health.js} +0 -0
- /package/src/{truncate.js → domain/truncate.js} +0 -0
- /package/src/{worker-changes.js → domain/worker-changes.js} +0 -0
- /package/src/{worker-env.js → domain/worker-env.js} +0 -0
- /package/src/{worker-report.js → domain/worker-report.js} +0 -0
- /package/src/{worker-report.schema.json → domain/worker-report.schema.json} +0 -0
- /package/src/{bin-resolver.js → infra/bin-resolver.js} +0 -0
- /package/src/{cleanup-hint.js → infra/cleanup-hint.js} +0 -0
- /package/src/{external-output.js → infra/external-output.js} +0 -0
- /package/src/{handoff.js → infra/handoff.js} +0 -0
- /package/src/{paths.js → infra/paths.js} +0 -0
- /package/src/{stage-store.js → infra/stage-store.js} +0 -0
- /package/src/{tool-guard.js → runners/codex/tool-guard.js} +0 -0
package/src/landing.js
DELETED
|
@@ -1,507 +0,0 @@
|
|
|
1
|
-
// Landing a worker's commits where they were dispatched from.
|
|
2
|
-
//
|
|
3
|
-
// A worker commits on codex/<taskId> inside its own worktree. Getting that
|
|
4
|
-
// work onto the branch the main thread integrates on used to be the main
|
|
5
|
-
// thread's job, one cherry-pick per step, dozens of times per batch. This
|
|
6
|
-
// does the same thing with the same guard rails a careful person would use:
|
|
7
|
-
// the target must be clean, it is never switched to another branch, and a
|
|
8
|
-
// conflict leaves nothing half-applied.
|
|
9
|
-
import { execFileSync } from "node:child_process";
|
|
10
|
-
import { existsSync, rmSync } from "node:fs";
|
|
11
|
-
import { join, relative } from "node:path";
|
|
12
|
-
import { filterWorkspaceSetupChanges, parsePorcelainLine, resolveWorktreeBase, worktreeRef, readWorktreeChanges, readWorktreeDiff, workspaceSetupIgnorePaths } from "./worktree.js";
|
|
13
|
-
import { findOutsideOwnedChanges } from "./owned-changes.js";
|
|
14
|
-
|
|
15
|
-
const defaultGitBin = process.env.GIT_BIN?.trim() || "git";
|
|
16
|
-
const PATCH_ID_MAX_BUFFER = 256 * 1024 * 1024;
|
|
17
|
-
|
|
18
|
-
// A commit needs a committer. On a machine where git has no user.name or
|
|
19
|
-
// user.email (CI runners, a fresh box, a GUI-launched MCP with no HOME
|
|
20
|
-
// config), cherry-pick and commit both die with "empty ident name", so a
|
|
21
|
-
// stand-in identity is supplied through the environment for that one
|
|
22
|
-
// directory. An identity git already has is used untouched.
|
|
23
|
-
const FALLBACK_IDENTITY = { name: "codex-supervisor", email: "codex-supervisor@localhost" };
|
|
24
|
-
const identityCache = new Map();
|
|
25
|
-
export function identityEnv(cwd) {
|
|
26
|
-
if (identityCache.has(cwd)) return identityCache.get(cwd);
|
|
27
|
-
let name = "";
|
|
28
|
-
let email = "";
|
|
29
|
-
try {
|
|
30
|
-
name = execFileSync(defaultGitBin, ["-C", cwd, "config", "user.name"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 10000 }).trim();
|
|
31
|
-
} catch {
|
|
32
|
-
// unset
|
|
33
|
-
}
|
|
34
|
-
try {
|
|
35
|
-
email = execFileSync(defaultGitBin, ["-C", cwd, "config", "user.email"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 10000 }).trim();
|
|
36
|
-
} catch {
|
|
37
|
-
// unset
|
|
38
|
-
}
|
|
39
|
-
const env = {};
|
|
40
|
-
if (!name || !email) {
|
|
41
|
-
env.GIT_AUTHOR_NAME = name || FALLBACK_IDENTITY.name;
|
|
42
|
-
env.GIT_AUTHOR_EMAIL = email || FALLBACK_IDENTITY.email;
|
|
43
|
-
env.GIT_COMMITTER_NAME = name || FALLBACK_IDENTITY.name;
|
|
44
|
-
env.GIT_COMMITTER_EMAIL = email || FALLBACK_IDENTITY.email;
|
|
45
|
-
}
|
|
46
|
-
identityCache.set(cwd, env);
|
|
47
|
-
return env;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function git(cwd, args, { timeout = 60000, input = null, maxBuffer = 1024 * 1024, encoding = "utf8" } = {}) {
|
|
51
|
-
return execFileSync(defaultGitBin, ["-C", cwd, "-c", "core.quotePath=false", ...args], {
|
|
52
|
-
encoding,
|
|
53
|
-
input,
|
|
54
|
-
timeout,
|
|
55
|
-
maxBuffer,
|
|
56
|
-
stdio: [input == null ? "ignore" : "pipe", "pipe", "pipe"],
|
|
57
|
-
env: { ...process.env, GIT_TERMINAL_PROMPT: "0", GIT_EDITOR: "true", ...identityEnv(cwd) }
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function lines(output) {
|
|
62
|
-
return output.split("\n").map((line) => line.trim()).filter(Boolean);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Porcelain lines carry their status in the first two columns, so they must
|
|
66
|
-
// not be trimmed before parsing.
|
|
67
|
-
function porcelainPaths(output) {
|
|
68
|
-
return output.split("\n").filter((line) => line.length > 3).map(parsePorcelainLine).filter(Boolean);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function patchIdsInRange(cwd, range) {
|
|
72
|
-
const patches = git(cwd, ["log", "--no-merges", "--format=commit %H", "--patch", "--binary", range], {
|
|
73
|
-
timeout: 2 * 60 * 1000,
|
|
74
|
-
encoding: "buffer",
|
|
75
|
-
maxBuffer: PATCH_ID_MAX_BUFFER
|
|
76
|
-
});
|
|
77
|
-
const output = git(cwd, ["patch-id", "--stable"], {
|
|
78
|
-
input: patches,
|
|
79
|
-
timeout: 2 * 60 * 1000,
|
|
80
|
-
maxBuffer: PATCH_ID_MAX_BUFFER
|
|
81
|
-
});
|
|
82
|
-
const ids = new Map();
|
|
83
|
-
for (const line of lines(output)) {
|
|
84
|
-
const [id, sha] = line.split(/\s+/);
|
|
85
|
-
if (id && sha && !/^0+$/.test(id)) ids.set(sha, id);
|
|
86
|
-
}
|
|
87
|
-
return ids;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// `git rev-list --cherry-pick` proves an original worker commit is not
|
|
91
|
-
// pending, but it does not name the target-side commit that carried the same
|
|
92
|
-
// patch. Lifecycle fields need that target-side SHA: using the target's
|
|
93
|
-
// current HEAD would point at unrelated later work and make cleanup/push
|
|
94
|
-
// verification lie.
|
|
95
|
-
function matchAlreadyLanded(cwd, target, targetHead, base, originals, workerHead) {
|
|
96
|
-
if (originals.length === 0) return { matches: [], complete: true, head: null };
|
|
97
|
-
const targetOrder = lines(git(target, ["rev-list", `${base}..${targetHead}`]));
|
|
98
|
-
const targetCommits = new Set(targetOrder);
|
|
99
|
-
const needsPatchIds = originals.some((original) => !targetCommits.has(original));
|
|
100
|
-
const targetByPatch = new Map([...(needsPatchIds ? patchIdsInRange(target, `${base}..${targetHead}`) : [])]
|
|
101
|
-
.map(([sha, id]) => [id, sha]));
|
|
102
|
-
const originalPatches = needsPatchIds ? patchIdsInRange(cwd, `${base}..${workerHead}`) : new Map();
|
|
103
|
-
const matches = originals.flatMap((original) => {
|
|
104
|
-
if (targetCommits.has(original)) return [{ original, target: original }];
|
|
105
|
-
const id = originalPatches.get(original);
|
|
106
|
-
const matched = id ? targetByPatch.get(id) : null;
|
|
107
|
-
return matched ? [{ original, target: matched }] : [];
|
|
108
|
-
});
|
|
109
|
-
const matchedTargets = new Set(matches.map((match) => match.target));
|
|
110
|
-
const head = targetOrder.find((sha) => matchedTargets.has(sha)) ?? null;
|
|
111
|
-
const complete = matches.length === originals.length && matches.every((match) => {
|
|
112
|
-
if (match.target === head) return true;
|
|
113
|
-
try { git(target, ["merge-base", "--is-ancestor", match.target, head]); return true; }
|
|
114
|
-
catch { return false; }
|
|
115
|
-
});
|
|
116
|
-
return { matches, complete, head: complete ? head : null };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function currentBranch(cwd) {
|
|
120
|
-
const name = git(cwd, ["symbolic-ref", "--short", "-q", "HEAD"]).trim();
|
|
121
|
-
return name || null;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function hasStagedChanges(cwd) {
|
|
125
|
-
try {
|
|
126
|
-
git(cwd, ["diff", "--cached", "--quiet"]);
|
|
127
|
-
return false;
|
|
128
|
-
} catch (error) {
|
|
129
|
-
if (error.status === 1) return true;
|
|
130
|
-
throw error;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Lands task's commits onto the current branch of its dispatch directory
|
|
135
|
-
// (task.project_root: the row's cwd is the worktree the worker ran in).
|
|
136
|
-
// `onto`, when given, must name that branch: it is a guard, not a switch.
|
|
137
|
-
//
|
|
138
|
-
// `commitMessage` commits what the worker left uncommitted in its worktree
|
|
139
|
-
// first, as one commit on codex/<id>. A worker in Codex's workspace-write
|
|
140
|
-
// sandbox cannot commit at all (the sandbox keeps .git read-only), so this
|
|
141
|
-
// is how "one step, one commit" is kept without giving workers full access.
|
|
142
|
-
//
|
|
143
|
-
// Returns { landed: [{ sha, original, subject }], onto, head } on success;
|
|
144
|
-
// otherwise { error, reason, ... } with nothing changed in the target.
|
|
145
|
-
export function landWorker(task, { onto = null, commitMessage = null, ignoreAcceptance = false, allowOutsideOwned = false } = {}) {
|
|
146
|
-
const target = task.project_root;
|
|
147
|
-
// The dispatcher's own checks are the gate. A failed run is refused until
|
|
148
|
-
// it passes; a run that was cut short is refused until it has run.
|
|
149
|
-
const acceptance = task.acceptance_results ?? null;
|
|
150
|
-
if (!ignoreAcceptance && acceptance) {
|
|
151
|
-
const failing = (acceptance.checks ?? []).find((check) => check.passed === false || check.exit_code !== 0 || check.timed_out) ?? null;
|
|
152
|
-
if (acceptance.passed === false) {
|
|
153
|
-
return {
|
|
154
|
-
error: "acceptance_failed",
|
|
155
|
-
reason: `the worker's acceptance run failed${failing ? ` on "${failing.command.slice(0, 120)}" (exit ${failing.error ?? (failing.baseline?.passed === false ? "baseline/evidence mismatch" : failing.timed_out ? "timeout" : failing.exit_code)})` : acceptance.error ? ` (${acceptance.error})` : ""}; resume the worker with the failing output and land once it passes. ignoreAcceptance: true lands anyway, only when the user said to.`,
|
|
156
|
-
acceptance: { status: acceptance.status, passed: false, checks: acceptance.checks ?? [], error: acceptance.error ?? null }
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
if (acceptance.status === "running" || acceptance.status === "interrupted" || acceptance.status === "cancelled") {
|
|
160
|
-
return {
|
|
161
|
-
error: "acceptance_not_run",
|
|
162
|
-
reason: `the worker's acceptance is ${acceptance.status}; wait for it (or resume the worker to run it again) before landing. ignoreAcceptance: true lands anyway, only when the user said to.`,
|
|
163
|
-
acceptance: { status: acceptance.status, passed: null }
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (!task.worktree_path || !existsSync(task.worktree_path)) {
|
|
168
|
-
return { error: "no_worktree", reason: "this worker has no worktree (it ran in place), so there is nothing on a codex/ branch to land" };
|
|
169
|
-
}
|
|
170
|
-
if (!target || !existsSync(target)) {
|
|
171
|
-
return { error: "no_target", reason: `the directory the worker was dispatched from (${target}) no longer exists` };
|
|
172
|
-
}
|
|
173
|
-
let branch;
|
|
174
|
-
try {
|
|
175
|
-
branch = currentBranch(target);
|
|
176
|
-
} catch (error) {
|
|
177
|
-
return { error: "no_target", reason: `${target} is not a git checkout: ${error.message.split("\n")[0]}` };
|
|
178
|
-
}
|
|
179
|
-
if (!branch) return { error: "detached_target", reason: `${target} has a detached HEAD; check out the branch you integrate on first` };
|
|
180
|
-
if (onto && onto !== branch) {
|
|
181
|
-
return { error: "wrong_branch", reason: `${target} is on ${branch}, not ${onto}; this tool never switches branches for you`, current_branch: branch };
|
|
182
|
-
}
|
|
183
|
-
const dirty = porcelainPaths(git(target, ["status", "--porcelain", "--untracked-files=no"]));
|
|
184
|
-
if (dirty.length > 0) {
|
|
185
|
-
return { error: "dirty_target", reason: `${target} has uncommitted changes; commit or stash them before landing`, dirty_files: dirty };
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const base = resolveWorktreeBase(task.worktree_path, worktreeRef(task));
|
|
189
|
-
if (!base) return { error: "no_base_commit", reason: "the commit this worker started from is unknown, so its commits cannot be told apart" };
|
|
190
|
-
if (!allowOutsideOwned) {
|
|
191
|
-
const diff = readWorktreeDiff(task.worktree_path, worktreeRef(task), { maxChars: 0, ignorePaths: workspaceSetupIgnorePaths(task) });
|
|
192
|
-
if (diff.error) return { error: "ownership_unverified", reason: "cannot prove the complete diff before landing", diff_error: diff.error };
|
|
193
|
-
const paths = [...filterWorkspaceSetupChanges(task, readWorktreeChanges(task.worktree_path, worktreeRef(task))),
|
|
194
|
-
...diff.files.flatMap((file) => [file.path, file.from].filter(Boolean).map((path) => join(task.worktree_path, path)))];
|
|
195
|
-
const { outside } = findOutsideOwnedChanges(task, [...new Set(paths)]);
|
|
196
|
-
if (outside.length) return { error: "outside_owned_paths", reason: "worker changes exceed declared ownedPaths; review them and obtain explicit authorization before allowing an exception", outside_paths: outside.map((entry) => relative(task.worktree_path, entry)) };
|
|
197
|
-
}
|
|
198
|
-
let commits;
|
|
199
|
-
try {
|
|
200
|
-
commits = lines(git(task.worktree_path, ["rev-list", "--reverse", `${base}..HEAD`]));
|
|
201
|
-
} catch (error) {
|
|
202
|
-
return { error: "no_base_commit", reason: `the base ${base} no longer resolves in the worktree: ${error.message.split("\n")[0]}` };
|
|
203
|
-
}
|
|
204
|
-
let uncommitted = porcelainPaths(git(task.worktree_path, ["--no-optional-locks", "status", "--porcelain"]));
|
|
205
|
-
let committedForWorker = null;
|
|
206
|
-
if (uncommitted.length > 0 && commitMessage) {
|
|
207
|
-
try {
|
|
208
|
-
if (allowOutsideOwned) git(task.worktree_path, ["add", "-A"]);
|
|
209
|
-
else {
|
|
210
|
-
const owned = findOutsideOwnedChanges(task, []).mapping.map((entry) => relative(task.worktree_path, entry.worker));
|
|
211
|
-
if (owned.length === 0) return { error: "outside_owned_paths", reason: "the worker left uncommitted changes but owns no landable path" };
|
|
212
|
-
git(task.worktree_path, ["add", "-A", "--", ...owned]);
|
|
213
|
-
}
|
|
214
|
-
if (hasStagedChanges(task.worktree_path)) {
|
|
215
|
-
git(task.worktree_path, ["commit", "-q", "-m", commitMessage]);
|
|
216
|
-
committedForWorker = git(task.worktree_path, ["rev-parse", "HEAD"]).trim();
|
|
217
|
-
commits.push(committedForWorker);
|
|
218
|
-
}
|
|
219
|
-
uncommitted = porcelainPaths(git(task.worktree_path, ["--no-optional-locks", "status", "--porcelain"]));
|
|
220
|
-
} catch (error) {
|
|
221
|
-
const tail = `${error.stdout ?? ""}${error.stderr ?? ""}`.trim().split("\n").slice(-3).join("\n");
|
|
222
|
-
return { error: "commit_failed", reason: `could not commit the worker's uncommitted edits in ${task.worktree_path}: ${tail || error.message.split("\n")[0]}`, uncommitted };
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
// Commits whose patch is already on the target (a previous land, or the
|
|
226
|
-
// main thread cherry-picked by hand) are skipped by patch id; picking
|
|
227
|
-
// them again produces an empty commit and git stops with an error.
|
|
228
|
-
const targetHead = git(target, ["rev-parse", "HEAD"]).trim();
|
|
229
|
-
let alreadyLanded = [];
|
|
230
|
-
let alreadyLandedMatches = [];
|
|
231
|
-
let matchedHead = null;
|
|
232
|
-
let alreadyLandedMatchComplete = true;
|
|
233
|
-
let alreadyLandedMatchError = null;
|
|
234
|
-
if (commits.length > 0) {
|
|
235
|
-
try {
|
|
236
|
-
const workerHead = git(task.worktree_path, ["rev-parse", "HEAD"]).trim();
|
|
237
|
-
const pending = new Set(lines(git(target, ["rev-list", "--cherry-pick", "--right-only", `${targetHead}...${workerHead}`])));
|
|
238
|
-
alreadyLanded = commits.filter((sha) => !pending.has(sha));
|
|
239
|
-
commits = commits.filter((sha) => pending.has(sha));
|
|
240
|
-
try {
|
|
241
|
-
({ matches: alreadyLandedMatches, complete: alreadyLandedMatchComplete, head: matchedHead } = matchAlreadyLanded(
|
|
242
|
-
task.worktree_path, target, targetHead, base, alreadyLanded, workerHead
|
|
243
|
-
));
|
|
244
|
-
} catch (error) {
|
|
245
|
-
alreadyLandedMatchComplete = false;
|
|
246
|
-
alreadyLandedMatchError = gitTail(error);
|
|
247
|
-
}
|
|
248
|
-
} catch (error) {
|
|
249
|
-
return { error: "landing_proof_failed", reason: "could not prove which worker commits remain unlanded", match_error: gitTail(error) };
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
if (commits.length === 0 && alreadyLanded.length > 0) {
|
|
253
|
-
return {
|
|
254
|
-
error: "nothing_to_land",
|
|
255
|
-
reason: `every commit on this worker's branch is already on ${branch} (landed before, by patch); nothing new to land`,
|
|
256
|
-
already_landed: alreadyLanded,
|
|
257
|
-
already_landed_matches: alreadyLandedMatches,
|
|
258
|
-
matched_head: matchedHead,
|
|
259
|
-
match_error: alreadyLandedMatchError,
|
|
260
|
-
match_complete: alreadyLandedMatchComplete,
|
|
261
|
-
onto: branch,
|
|
262
|
-
target,
|
|
263
|
-
uncommitted,
|
|
264
|
-
base_commit: base
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
if (commits.length === 0) {
|
|
268
|
-
return {
|
|
269
|
-
error: "nothing_to_land",
|
|
270
|
-
reason: uncommitted.length > 0
|
|
271
|
-
? "the worker made no commits on its branch (a workspace-write worker cannot: the sandbox keeps .git read-only); pass commitMessage to commit its edits as one commit and land that"
|
|
272
|
-
: "the worker made no commits and left no edits on its branch",
|
|
273
|
-
uncommitted,
|
|
274
|
-
base_commit: base
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const before = targetHead;
|
|
279
|
-
try {
|
|
280
|
-
git(target, ["cherry-pick", "--allow-empty-message", ...commits], { timeout: 10 * 60 * 1000 });
|
|
281
|
-
} catch (error) {
|
|
282
|
-
let conflicts = [];
|
|
283
|
-
try {
|
|
284
|
-
conflicts = lines(git(target, ["diff", "--name-only", "--diff-filter=U"]));
|
|
285
|
-
} catch {
|
|
286
|
-
// no conflict list, still abort
|
|
287
|
-
}
|
|
288
|
-
try {
|
|
289
|
-
git(target, ["cherry-pick", "--abort"]);
|
|
290
|
-
} catch {
|
|
291
|
-
// nothing in progress to abort
|
|
292
|
-
}
|
|
293
|
-
const tail = `${error.stdout ?? ""}${error.stderr ?? ""}`.trim().split("\n").slice(-4).join("\n");
|
|
294
|
-
return {
|
|
295
|
-
error: "conflict",
|
|
296
|
-
reason: conflicts.length > 0 ? `cherry-pick stopped on a conflict; ${target} was reset to where it was` : `cherry-pick failed; ${target} was reset to where it was`,
|
|
297
|
-
conflicts,
|
|
298
|
-
git: tail,
|
|
299
|
-
base_commit: base,
|
|
300
|
-
commits,
|
|
301
|
-
uncommitted
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
const landed = lines(git(target, ["rev-list", "--reverse", `${before}..HEAD`]));
|
|
305
|
-
const subjects = lines(git(target, ["log", "--format=%H%x09%s", `${before}..HEAD`]));
|
|
306
|
-
const subjectOf = new Map(subjects.map((line) => line.split("\t")).map(([sha, subject]) => [sha, subject]));
|
|
307
|
-
return {
|
|
308
|
-
onto: branch,
|
|
309
|
-
target,
|
|
310
|
-
base_commit: base,
|
|
311
|
-
head: landed.at(-1) ?? before,
|
|
312
|
-
landed: landed.map((sha, index) => ({ sha, original: commits[index] ?? null, subject: subjectOf.get(sha) ?? null })),
|
|
313
|
-
already_landed: alreadyLanded,
|
|
314
|
-
already_landed_matches: alreadyLandedMatches,
|
|
315
|
-
match_error: alreadyLandedMatchError,
|
|
316
|
-
match_complete: alreadyLandedMatchComplete,
|
|
317
|
-
committed_for_worker: committedForWorker,
|
|
318
|
-
uncommitted
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
function gitTail(error) {
|
|
323
|
-
return `${error.stdout ?? ""}${error.stderr ?? ""}`.trim().split("\n").slice(-4).join("\n") || error.message.split("\n")[0];
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
function conflictFiles(cwd) {
|
|
327
|
-
try {
|
|
328
|
-
return lines(git(cwd, ["diff", "--name-only", "--diff-filter=U"]));
|
|
329
|
-
} catch {
|
|
330
|
-
return [];
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
function stashRef(cwd) {
|
|
335
|
-
try {
|
|
336
|
-
return git(cwd, ["rev-parse", "-q", "--verify", "refs/stash"]).trim() || null;
|
|
337
|
-
} catch {
|
|
338
|
-
return null;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
function operationInProgress(cwd) {
|
|
343
|
-
for (const marker of ["rebase-merge", "rebase-apply", "CHERRY_PICK_HEAD", "MERGE_HEAD"]) {
|
|
344
|
-
try {
|
|
345
|
-
const path = git(cwd, ["rev-parse", "--git-path", marker]).trim();
|
|
346
|
-
if (path && existsSync(path.startsWith("/") || /^[A-Za-z]:/.test(path) ? path : join(cwd, path))) return marker;
|
|
347
|
-
} catch {
|
|
348
|
-
// not a repo; the caller reports that
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
return null;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// Moves a worker's worktree onto a newer commit of the project before the
|
|
355
|
-
// worker is resumed. The worktree was cut from base_commit when the worker
|
|
356
|
-
// was dispatched; by the time it is sent back for a fix the project's branch
|
|
357
|
-
// has often moved on, and a worker that keeps editing the old base does
|
|
358
|
-
// work that cannot land.
|
|
359
|
-
//
|
|
360
|
-
// Exactly the worker's own commits are replayed: `git rebase --onto <onto>
|
|
361
|
-
// <base>`, never a plain rebase, whose merge-base start would also replay
|
|
362
|
-
// the project's own commits when the base was rewritten. Edits the worker
|
|
363
|
-
// left uncommitted (every workspace-write worker: the sandbox keeps .git
|
|
364
|
-
// read-only) are stashed around the rebase, untracked files included.
|
|
365
|
-
//
|
|
366
|
-
// Nothing is left half done. A conflict in the replay aborts the rebase; a
|
|
367
|
-
// conflict when the stash comes back resets the branch to where it was and
|
|
368
|
-
// pops the stash there, which cannot conflict. Either way the worktree is as
|
|
369
|
-
// it was and the caller gets the file list.
|
|
370
|
-
//
|
|
371
|
-
// Returns { ok: true, noop: true, base_commit, head } when onto is already in
|
|
372
|
-
// the branch's history, { ok: true, base_commit: onto, head, replayed,
|
|
373
|
-
// stashed } after a real rebase, or { error, reason, files?, git?,
|
|
374
|
-
// base_commit, head } with nothing changed.
|
|
375
|
-
export function rebaseWorktree(task, ontoCommit) {
|
|
376
|
-
const cwd = task.worktree_path;
|
|
377
|
-
if (!cwd || !existsSync(cwd)) {
|
|
378
|
-
return { error: "no_worktree", reason: "this worker has no worktree (it ran in place), so there is nothing to rebase" };
|
|
379
|
-
}
|
|
380
|
-
const base = resolveWorktreeBase(cwd, worktreeRef(task));
|
|
381
|
-
if (!base) return { error: "no_base_commit", reason: "the commit this worker started from is unknown, so its own commits cannot be told apart for the replay" };
|
|
382
|
-
const inProgress = operationInProgress(cwd);
|
|
383
|
-
if (inProgress) {
|
|
384
|
-
return { error: "rebase_in_progress", reason: `${cwd} has a ${inProgress} in progress; finish or abort it first`, base_commit: base };
|
|
385
|
-
}
|
|
386
|
-
let head;
|
|
387
|
-
try {
|
|
388
|
-
head = git(cwd, ["rev-parse", "HEAD"]).trim();
|
|
389
|
-
} catch (error) {
|
|
390
|
-
return { error: "no_worktree", reason: `${cwd} has no resolvable HEAD: ${gitTail(error)}`, base_commit: base };
|
|
391
|
-
}
|
|
392
|
-
try {
|
|
393
|
-
git(cwd, ["merge-base", "--is-ancestor", ontoCommit, "HEAD"]);
|
|
394
|
-
// onto is already behind or at this branch: a rebase would move the
|
|
395
|
-
// worker's commits backwards. Nothing to do and base stays.
|
|
396
|
-
return { ok: true, noop: true, base_commit: base, head };
|
|
397
|
-
} catch (error) {
|
|
398
|
-
if (error.status !== 1) {
|
|
399
|
-
return { error: "rebase_failed", reason: `could not compare ${ontoCommit} with HEAD: ${gitTail(error)}`, git: gitTail(error), base_commit: base, head };
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
let pending;
|
|
404
|
-
let alreadyLanded;
|
|
405
|
-
try {
|
|
406
|
-
const commits = lines(git(cwd, ["rev-list", "--reverse", `${base}..HEAD`]));
|
|
407
|
-
const newPatches = new Set(lines(git(cwd, ["cherry", ontoCommit, "HEAD", base]))
|
|
408
|
-
.filter((line) => line.startsWith("+ ")).map((line) => line.slice(2)));
|
|
409
|
-
pending = commits.filter((sha) => newPatches.has(sha));
|
|
410
|
-
alreadyLanded = commits.filter((sha) => !newPatches.has(sha));
|
|
411
|
-
} catch (error) {
|
|
412
|
-
return { error: "rebase_failed", reason: `could not prove which worker patches are already integrated: ${gitTail(error)}`, base_commit: base, head };
|
|
413
|
-
}
|
|
414
|
-
const stashBefore = stashRef(cwd);
|
|
415
|
-
let stashed = false;
|
|
416
|
-
try {
|
|
417
|
-
git(cwd, ["stash", "push", "--include-untracked", "-q", "-m", "codex-supervisor: rebase"]);
|
|
418
|
-
} catch (error) {
|
|
419
|
-
return { error: "rebase_failed", reason: `could not stash the worker's uncommitted edits: ${gitTail(error)}`, git: gitTail(error), base_commit: base, head };
|
|
420
|
-
}
|
|
421
|
-
// "No local changes to save" also exits 0, so the stash ref decides.
|
|
422
|
-
stashed = stashRef(cwd) !== stashBefore;
|
|
423
|
-
|
|
424
|
-
// A pop that fails has already written the stash's untracked files back
|
|
425
|
-
// into the worktree, and reset --hard does not touch untracked files, so
|
|
426
|
-
// the next pop would stop on "already exists". Those files are the
|
|
427
|
-
// stash's third parent; they are removed before the pop that restores
|
|
428
|
-
// them for good.
|
|
429
|
-
const removeStashedUntracked = () => {
|
|
430
|
-
let paths = [];
|
|
431
|
-
try {
|
|
432
|
-
paths = lines(git(cwd, ["ls-tree", "-r", "--name-only", "refs/stash^3"]));
|
|
433
|
-
} catch {
|
|
434
|
-
return; // the stash carries no untracked files
|
|
435
|
-
}
|
|
436
|
-
for (const path of paths) rmSync(join(cwd, path), { force: true });
|
|
437
|
-
};
|
|
438
|
-
const restoreStash = () => {
|
|
439
|
-
if (!stashed) return null;
|
|
440
|
-
try {
|
|
441
|
-
removeStashedUntracked();
|
|
442
|
-
git(cwd, ["stash", "pop", "-q"]);
|
|
443
|
-
return null;
|
|
444
|
-
} catch (error) {
|
|
445
|
-
return gitTail(error);
|
|
446
|
-
}
|
|
447
|
-
};
|
|
448
|
-
|
|
449
|
-
try {
|
|
450
|
-
// Rebase --onto can replay a patch previously cherry-picked to main and
|
|
451
|
-
// conflict with main's later edits. Rebuild only the proven new patches.
|
|
452
|
-
git(cwd, ["reset", "-q", "--hard", ontoCommit]);
|
|
453
|
-
if (pending.length) git(cwd, ["cherry-pick", ...pending], { timeout: 10 * 60 * 1000 });
|
|
454
|
-
} catch (error) {
|
|
455
|
-
const files = conflictFiles(cwd);
|
|
456
|
-
try {
|
|
457
|
-
git(cwd, ["cherry-pick", "--abort"]);
|
|
458
|
-
} catch {
|
|
459
|
-
// nothing in progress to abort
|
|
460
|
-
}
|
|
461
|
-
git(cwd, ["reset", "-q", "--hard", head]);
|
|
462
|
-
const popError = restoreStash();
|
|
463
|
-
return {
|
|
464
|
-
error: files.length > 0 ? "rebase_conflict" : "rebase_failed",
|
|
465
|
-
reason: files.length > 0
|
|
466
|
-
? `replaying the worker's commits onto ${ontoCommit.slice(0, 12)} hit a conflict; the worktree is back as it was${popError ? " (but its stash could not be restored: run git stash pop there)" : ""}`
|
|
467
|
-
: `the rebase failed; the worktree is back as it was${popError ? " (but its stash could not be restored: run git stash pop there)" : ""}`,
|
|
468
|
-
files,
|
|
469
|
-
git: gitTail(error),
|
|
470
|
-
base_commit: base,
|
|
471
|
-
head
|
|
472
|
-
};
|
|
473
|
-
}
|
|
474
|
-
const newHead = git(cwd, ["rev-parse", "HEAD"]).trim();
|
|
475
|
-
let replayed = 0;
|
|
476
|
-
try {
|
|
477
|
-
replayed = lines(git(cwd, ["rev-list", `${ontoCommit}..HEAD`])).length;
|
|
478
|
-
} catch {
|
|
479
|
-
// reported as 0
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
if (stashed) {
|
|
483
|
-
try {
|
|
484
|
-
git(cwd, ["stash", "pop", "-q"]);
|
|
485
|
-
} catch (error) {
|
|
486
|
-
// The replay succeeded but the worker's uncommitted edits do not fit
|
|
487
|
-
// the new base. Back to the old base, where the same stash pops clean.
|
|
488
|
-
const files = conflictFiles(cwd);
|
|
489
|
-
const tail = gitTail(error);
|
|
490
|
-
try {
|
|
491
|
-
git(cwd, ["reset", "-q", "--hard", head]);
|
|
492
|
-
} catch {
|
|
493
|
-
// fall through; the stash is still there
|
|
494
|
-
}
|
|
495
|
-
const popError = restoreStash();
|
|
496
|
-
return {
|
|
497
|
-
error: "stash_conflict",
|
|
498
|
-
reason: `the worker's uncommitted edits conflict with ${ontoCommit.slice(0, 12)}; the worktree is back as it was${popError ? " (but its stash could not be restored: run git stash pop there)" : ""}. Commit them first (land_codex_worker with commitMessage) so the conflict is in a commit that can be resolved, or redispatch from the new commit with baseRef`,
|
|
499
|
-
files,
|
|
500
|
-
git: tail,
|
|
501
|
-
base_commit: base,
|
|
502
|
-
head
|
|
503
|
-
};
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
return { ok: true, base_commit: ontoCommit, head: newHead, previous_head: head, replayed, already_landed: alreadyLanded, stashed };
|
|
507
|
-
}
|