Skip to content

Commit eb63cb1

Browse files
steipeteampagent
andauthored
fix: keep dirty changed checks scoped to local edits (#124685)
* fix(crabbox): preserve dirty changed-gate state Amp-Thread-ID: https://ampcode.com/threads/T-01a00a6a-b64e-74a5-8b15-2d3b966a468d * fix(ci): cover WSL2 dirty changed gates Amp-Thread-ID: https://ampcode.com/threads/T-01a00a6a-b64e-74a5-8b15-2d3b966a468d --------- Co-authored-by: Amp <[email protected]>
1 parent bdcc683 commit eb63cb1

2 files changed

Lines changed: 113 additions & 40 deletions

File tree

‎scripts/crabbox-wrapper.mts‎

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,9 @@ function satisfiesMinimumCrabboxVersion(version: string, minimum: number[]) {
541541
return !parsed.suffix || isPostReleaseDescribeSuffix(parsed.suffix);
542542
}
543543

544-
function gitOutput(commandArgs: string[]) {
544+
function gitOutput(commandArgs: string[], extraEnv: ProcessEnv = {}) {
545545
const gitBinary = resolvePathBinary("git", process.env, process.platform) ?? "git";
546-
const gitEnv = { ...process.env, GIT_CONFIG_GLOBAL: "/dev/null" };
546+
const gitEnv = { ...process.env, ...extraEnv, GIT_CONFIG_GLOBAL: "/dev/null" };
547547
const invocation = spawnInvocation(gitBinary, commandArgs, gitEnv, process.platform);
548548
const result = spawnSync(invocation.command, invocation.args, {
549549
cwd: repoRoot,
@@ -3200,7 +3200,13 @@ function analyzeRemoteCommand(invocation: RunInvocation) {
32003200
};
32013201
}
32023202

3203-
function prepareRemoteWsl2JsBootstrapScript(run: RunInvocation, facts: RunFacts, provider: string) {
3203+
function prepareRemoteWsl2JsBootstrapScript(
3204+
run: RunInvocation,
3205+
facts: RunFacts,
3206+
provider: string,
3207+
changedGateBase: string,
3208+
changedGateAlias: string,
3209+
) {
32043210
const runtimeEntrypoint = awsMacosBunEntrypoints.has(facts.runtimeEntrypoint)
32053211
? ""
32063212
: facts.runtimeEntrypoint;
@@ -3220,7 +3226,7 @@ function prepareRemoteWsl2JsBootstrapScript(run: RunInvocation, facts: RunFacts,
32203226
const originalShellCommand = facts.scopedEnvCommand?.shellCommand ?? renderRunShellCommand(run);
32213227
const script = `${remoteWsl2JsBootstrap({
32223228
packageManager: facts.packageManager,
3223-
})} || exit $?\n{ ${originalShellCommand}\n}\n`;
3229+
})} || exit $?\n${facts.changedGate && changedGateBase ? `${remoteGitBootstrapForChangedGate(changedGateBase, changedGateAlias)} || exit $?\n` : ""}{ ${originalShellCommand}\n}\n`;
32243230
writeFileSync(scriptPath, script, "utf8");
32253231
chmodSync(scriptPath, 0o700);
32263232

@@ -3410,21 +3416,19 @@ function isWorktreeClean() {
34103416
return status.status === 0 && status.stdout === "";
34113417
}
34123418

3413-
function shouldUseFullCheckoutForCleanRemoteSync(commandArgs: string[], _providerName: string) {
3419+
function shouldUseFullCheckoutForRemoteSync(commandArgs: string[], _providerName: string) {
34143420
if (commandArgs[0] !== "run") {
34153421
return false;
34163422
}
34173423
if (hasOption(commandArgs, "--no-sync")) {
34183424
return false;
34193425
}
3420-
if (!isWorktreeClean()) {
3421-
return false;
3422-
}
34233426

3424-
return (
3425-
isSparseCheckout() ||
3426-
isChangedGateCommand(parseRunInvocation(help.text, commandArgs).commandArgs)
3427-
);
3427+
const changedGate = isChangedGateCommand(parseRunInvocation(help.text, commandArgs).commandArgs);
3428+
if (changedGate && !isNativeWindowsRemoteTarget(commandArgs)) {
3429+
return true;
3430+
}
3431+
return isWorktreeClean() && (isSparseCheckout() || changedGate);
34283432
}
34293433

34303434
function defaultFullCheckoutSyncRoot() {
@@ -3498,9 +3502,27 @@ function assertFullCheckoutSyncDisk(root: string) {
34983502
);
34993503
}
35003504

3505+
function currentWorktreeTree(syncRoot: string) {
3506+
const indexDir = mkdtempSync(resolve(syncRoot, "openclaw-crabbox-index-"));
3507+
const indexPath = resolve(indexDir, "index");
3508+
const indexEnv = { GIT_INDEX_FILE: indexPath };
3509+
try {
3510+
const read = gitOutput(["read-tree", "HEAD"], indexEnv);
3511+
const add = gitOutput(["add", "-A", "--", "."], indexEnv);
3512+
const tree = gitOutput(["write-tree"], indexEnv);
3513+
if (read.status !== 0 || add.status !== 0 || tree.status !== 0 || !tree.stdout) {
3514+
throw new Error(read.text || add.text || tree.text || "git write-tree failed");
3515+
}
3516+
return tree.stdout;
3517+
} finally {
3518+
rmSync(indexDir, { recursive: true, force: true });
3519+
}
3520+
}
3521+
35013522
function prepareFullCheckoutForSync(options: { changedGateBase?: string } = {}) {
35023523
const syncRoot = fullCheckoutSyncRoot();
35033524
assertFullCheckoutSyncDisk(syncRoot);
3525+
const changedGateTree = options.changedGateBase ? currentWorktreeTree(syncRoot) : "";
35043526
const dir = mkdtempSync(resolve(syncRoot, "openclaw-crabbox-sync-"));
35053527
let active = false;
35063528
let resolvedChangedGateBase = options.changedGateBase ?? "";
@@ -3526,19 +3548,15 @@ function prepareFullCheckoutForSync(options: { changedGateBase?: string } = {})
35263548
try {
35273549
bundleTempDir = mkdtempSync(resolve(syncRoot, "openclaw-crabbox-bundle-"));
35283550
const bundleTempPath = resolve(bundleTempDir, "changed-gate.bundle");
3529-
const head = gitOutput(["-C", dir, "rev-parse", "HEAD"]);
35303551
const base = gitOutput(["-C", dir, "rev-parse", options.changedGateBase]);
3531-
if (head.status !== 0 || base.status !== 0 || !head.stdout || !base.stdout) {
3532-
throw new Error(`git rev-parse failed: ${head.text || base.text}`);
3552+
const baseTree = gitOutput(["-C", dir, "rev-parse", `${options.changedGateBase}^{tree}`]);
3553+
if (base.status !== 0 || baseTree.status !== 0 || !base.stdout || !baseTree.stdout) {
3554+
throw new Error(`git rev-parse failed: ${base.text || baseTree.text}`);
35333555
}
35343556
resolvedChangedGateBase = base.stdout;
3535-
if (head.stdout === base.stdout) {
3557+
if (changedGateTree === baseTree.stdout) {
35363558
writeFileSync(bundleTempPath, "", "utf8");
35373559
} else {
3538-
const headTree = gitOutput(["-C", dir, "rev-parse", "HEAD^{tree}"]);
3539-
if (headTree.status !== 0 || !headTree.stdout) {
3540-
throw new Error(headTree.text || "git rev-parse HEAD tree failed");
3541-
}
35423560
// A parentless carrier makes the bundle self-contained while sending
35433561
// only the final tree. The remote attaches the fetched base as parent.
35443562
const transportCommit = gitOutput([
@@ -3549,7 +3567,7 @@ function prepareFullCheckoutForSync(options: { changedGateBase?: string } = {})
35493567
"-c",
35503568
35513569
"commit-tree",
3552-
headTree.stdout,
3570+
changedGateTree,
35533571
"-m",
35543572
"remote-changed-gate-tree",
35553573
]);
@@ -3762,6 +3780,8 @@ function applyRunTransforms(
37623780
invocation,
37633781
facts,
37643782
options.provider,
3783+
options.changedGateBase,
3784+
options.changedGateAlias,
37653785
);
37663786
let transformedArgs = wsl2ScriptBootstrap.args;
37673787
invocation = parseRunInvocation(help.text, transformedArgs);
@@ -3938,7 +3958,7 @@ normalizedArgs = scriptBootstrap.args;
39383958
const scriptStdinPrepared = scriptBootstrap.prepared;
39393959
let wsl2ScriptBootstrap = { args: normalizedArgs, cleanup: () => {}, prepared: false };
39403960
try {
3941-
if (shouldUseFullCheckoutForCleanRemoteSync(normalizedArgs, provider)) {
3961+
if (shouldUseFullCheckoutForRemoteSync(normalizedArgs, provider)) {
39423962
const invocation = parseRunInvocation(help.text, normalizedArgs);
39433963
const facts = analyzeRemoteCommand(invocation);
39443964
const changedGate = facts.changedGate ? changedGateBaseForCommand(facts.commandArgs) : null;
@@ -3953,11 +3973,11 @@ try {
39533973
remoteChangedGateBase = checkout.changedGateBase;
39543974
remoteChangedGateAlias = changedGate?.remoteAlias ?? "";
39553975
console.error(
3956-
`[crabbox] sparse clean checkout detected; syncing from temporary full checkout ${checkout.dir}`,
3976+
`[crabbox] isolated checkout sync; syncing from temporary full checkout ${checkout.dir}`,
39573977
);
39583978
if (checkout.changedGateBase) {
39593979
console.error(
3960-
`[crabbox] remote changed gate detected; overlaying local HEAD as worktree changes from ${checkout.changedGateBase}`,
3980+
`[crabbox] remote changed gate detected; overlaying the local worktree as changes from ${checkout.changedGateBase}`,
39613981
);
39623982
}
39633983
}
@@ -4124,7 +4144,7 @@ if (childStderr) {
41244144
});
41254145
}
41264146
const childKillGraceMs = resolveChildKillGraceMs(process.env);
4127-
let childForceKillTimer: NodeJS.Timeout | undefined;
4147+
let childForceKillTimer: ReturnType<typeof setTimeout> | undefined;
41284148
let childTreeShutdownStarted = false;
41294149
if (fullCheckout) {
41304150
try {

‎test/scripts/crabbox-wrapper.test.ts‎

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,17 @@ if (args[0] === "worktree" && args[1] === "add") {
333333
fs.mkdirSync(args[3], { recursive: true });
334334
if (process.env.OPENCLAW_FAKE_GIT_CHANGED_GATE_BUNDLE_SYMLINK_TARGET) fs.symlinkSync(process.env.OPENCLAW_FAKE_GIT_CHANGED_GATE_BUNDLE_SYMLINK_TARGET, path.join(args[3], ".openclaw-crabbox-changed-gate.bundle")); process.exit(0);
335335
}
336+
if (args[0] === "read-tree") { fs.writeFileSync(process.env.GIT_INDEX_FILE, ""); process.exit(0); }
337+
if (args[0] === "add" && process.env.GIT_INDEX_FILE) process.exit(0);
338+
if (args[0] === "write-tree") { process.stdout.write((process.env.OPENCLAW_FAKE_GIT_WORKTREE_TREE_SHA || "tree456") + "\n"); process.exit(0); }
336339
if (args[0] === "-C" && args[2] === "sparse-checkout" && args[3] === "disable") process.exit(0);
337340
if (args[0] === "-C" && args[2] === "rev-parse") {
338-
const value = args[3] === "HEAD" ? process.env.OPENCLAW_FAKE_GIT_HEAD_SHA || "def456" : args[3] === "HEAD^{tree}" ? process.env.OPENCLAW_FAKE_GIT_HEAD_TREE_SHA || "tree456" : process.env.OPENCLAW_FAKE_GIT_BASE_SHA || "abc123";
341+
const value = args[3] === "HEAD" ? process.env.OPENCLAW_FAKE_GIT_HEAD_SHA || "def456" : args[3] === "HEAD^{tree}" ? process.env.OPENCLAW_FAKE_GIT_HEAD_TREE_SHA || "tree456" : args[3].endsWith("^{tree}") ? process.env.OPENCLAW_FAKE_GIT_BASE_TREE_SHA || "base-tree123" : process.env.OPENCLAW_FAKE_GIT_BASE_SHA || "abc123";
339342
process.stdout.write(value + "\n"); process.exit(0);
340343
}
341344
if (args[0] === "-C" && args[2] === "-c" && args[6] === "commit-tree") {
342345
if (process.env.OPENCLAW_FAKE_GIT_ROOT_COMMIT_MARKER && args.includes("-p")) process.exit(68);
346+
if (process.env.OPENCLAW_FAKE_GIT_EXPECT_COMMIT_TREE && args[7] !== process.env.OPENCLAW_FAKE_GIT_EXPECT_COMMIT_TREE) process.exit(69);
343347
touch("OPENCLAW_FAKE_GIT_ROOT_COMMIT_MARKER"); touch("OPENCLAW_FAKE_GIT_SYNTHETIC_COMMIT_MARKER");
344348
process.stdout.write((process.env.OPENCLAW_FAKE_GIT_SYNTHETIC_COMMIT_SHA || "synthetic789") + "\n"); process.exit(0);
345349
}
@@ -1948,17 +1952,31 @@ describe("scripts/crabbox-wrapper", () => {
19481952
});
19491953

19501954
it("prefers Azure for unqualified Windows runs", () => {
1951-
const { output, result } = runSuccessfulWrapper(azureProviderHelp, [
1952-
"run",
1953-
"--target",
1954-
"windows",
1955-
"--windows-mode",
1956-
"wsl2",
1957-
"--",
1958-
"corepack",
1959-
"pnpm",
1960-
"check:changed",
1961-
]);
1955+
const dirtyTree = "dirty-wsl2-tree-123";
1956+
const { output, result } = runSuccessfulWrapper(
1957+
azureProviderHelp,
1958+
[
1959+
"run",
1960+
"--target",
1961+
"windows",
1962+
"--windows-mode",
1963+
"wsl2",
1964+
"--",
1965+
"corepack",
1966+
"pnpm",
1967+
"check:changed",
1968+
],
1969+
{
1970+
env: {
1971+
OPENCLAW_FAKE_GIT_EXPECT_COMMIT_TREE: dirtyTree,
1972+
OPENCLAW_FAKE_GIT_WORKTREE_TREE_SHA: dirtyTree,
1973+
},
1974+
gitResponses: {
1975+
[GIT_STATUS_PORCELAIN_KEY]: { stdout: " M scripts/crabbox-wrapper.mts\n" },
1976+
[GIT_MERGE_BASE_MAIN_HEAD_KEY]: { stdout: "abc123\n" },
1977+
},
1978+
},
1979+
);
19621980

19631981
const remoteCommand = normalizeShellLineEndings(output.scriptContent!);
19641982
expect(output.args.slice(0, 7)).toEqual([
@@ -1980,9 +1998,12 @@ describe("scripts/crabbox-wrapper", () => {
19801998
expect(remoteCommand).toContain("corepack enable --install-directory");
19811999
expect(remoteCommand).toContain("pnpm install --frozen-lockfile");
19822000
expect(remoteCommand).toContain("openclaw_crabbox_bootstrap_wsl2_js || exit $?");
2001+
expectChangedGateGitBootstrap(remoteCommand);
19832002
expect(remoteCommand).toContain(
19842003
`{ openclaw_crabbox_env ${remoteChangedGateEnvPrefix} corepack pnpm check:changed\n}`,
19852004
);
2005+
expect(output.cwd).toContain("openclaw-crabbox-sync-");
2006+
expect(result.stderr).toContain("overlaying the local worktree as changes from abc123");
19862007
expect(result.stderr).toContain("provider=azure");
19872008
});
19882009

@@ -3303,7 +3324,7 @@ describe("scripts/crabbox-wrapper", () => {
33033324
cleanSparseSyncOptions,
33043325
);
33053326
expect(result.stderr).toContain("syncing from temporary full checkout");
3306-
expect(result.stderr).toContain("overlaying local HEAD as worktree changes from abc123");
3327+
expect(result.stderr).toContain("overlaying the local worktree as changes from abc123");
33073328
expect(output.args.join(" ")).toContain(
33083329
"openclaw_changed_gate_bundle=.openclaw-crabbox-changed-gate.bundle",
33093330
);
@@ -3370,7 +3391,7 @@ describe("scripts/crabbox-wrapper", () => {
33703391
},
33713392
},
33723393
);
3373-
expect(result.stderr).toContain("overlaying local HEAD as worktree changes from release123");
3394+
expect(result.stderr).toContain("overlaying the local worktree as changes from release123");
33743395
expect(remoteCommand).toContain("openclaw_changed_gate_base=release123");
33753396
expect(remoteCommand).toContain(
33763397
"openclaw_changed_gate_alias=refs/remotes/origin/release/2026.7.2",
@@ -3534,7 +3555,7 @@ describe("scripts/crabbox-wrapper", () => {
35343555
},
35353556
);
35363557
expect(result.stderr).toContain("syncing from temporary full checkout");
3537-
expect(result.stderr).toContain("overlaying local HEAD as worktree changes from abc123");
3558+
expect(result.stderr).toContain("overlaying the local worktree as changes from abc123");
35383559
expect(output.cwd).toContain("openclaw-crabbox-sync-");
35393560
expect(output.args).toContain("--shell");
35403561
expect(remoteCommand).toContain("git init -q");
@@ -3544,6 +3565,38 @@ describe("scripts/crabbox-wrapper", () => {
35443565
);
35453566
});
35463567

3568+
it("bootstraps the exact dirty worktree tree for remote changed gates", () => {
3569+
const dirtyTree = "dirty-tree-123";
3570+
const { output, remoteCommand, result } = runSuccessfulDefaultWrapper(
3571+
[
3572+
"run",
3573+
"--provider",
3574+
"blacksmith-testbox",
3575+
"--blacksmith-ref",
3576+
"main",
3577+
"--",
3578+
"corepack",
3579+
"pnpm",
3580+
"check:changed",
3581+
],
3582+
{
3583+
env: {
3584+
OPENCLAW_FAKE_GIT_EXPECT_COMMIT_TREE: dirtyTree,
3585+
OPENCLAW_FAKE_GIT_WORKTREE_TREE_SHA: dirtyTree,
3586+
},
3587+
gitResponses: {
3588+
[GIT_STATUS_PORCELAIN_KEY]: { stdout: " M scripts/crabbox-wrapper.mts\n" },
3589+
[GIT_MERGE_BASE_MAIN_HEAD_KEY]: { stdout: "abc123\n" },
3590+
},
3591+
},
3592+
);
3593+
3594+
expect(result.stderr).toContain("syncing from temporary full checkout");
3595+
expect(result.stderr).toContain("overlaying the local worktree as changes from abc123");
3596+
expect(output.cwd).toContain("openclaw-crabbox-sync-");
3597+
expectChangedGateGitBootstrap(remoteCommand);
3598+
});
3599+
35473600
it("bootstraps Git metadata for env-prefixed sparse changed gates", () => {
35483601
const { output, remoteCommand } = runSuccessfulDefaultWrapper(
35493602
[

0 commit comments

Comments
 (0)