Skip to content

Commit 819f7ec

Browse files
committed
1 parent 01a23bc commit 819f7ec

2 files changed

Lines changed: 75 additions & 27 deletions

File tree

‎scripts/crabbox-wrapper.mts‎

Lines changed: 35 additions & 23 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,
@@ -3410,21 +3410,19 @@ function isWorktreeClean() {
34103410
return status.status === 0 && status.stdout === "";
34113411
}
34123412

3413-
function shouldUseFullCheckoutForCleanRemoteSync(commandArgs: string[], _providerName: string) {
3413+
function shouldUseFullCheckoutForRemoteSync(commandArgs: string[], _providerName: string) {
34143414
if (commandArgs[0] !== "run") {
34153415
return false;
34163416
}
34173417
if (hasOption(commandArgs, "--no-sync")) {
34183418
return false;
34193419
}
3420-
if (!isWorktreeClean()) {
3421-
return false;
3422-
}
34233420

3424-
return (
3425-
isSparseCheckout() ||
3426-
isChangedGateCommand(parseRunInvocation(help.text, commandArgs).commandArgs)
3427-
);
3421+
const changedGate = isChangedGateCommand(parseRunInvocation(help.text, commandArgs).commandArgs);
3422+
if (changedGate && !isWindowsRemoteTarget(commandArgs)) {
3423+
return true;
3424+
}
3425+
return isWorktreeClean() && (isSparseCheckout() || changedGate);
34283426
}
34293427

34303428
function defaultFullCheckoutSyncRoot() {
@@ -3498,9 +3496,27 @@ function assertFullCheckoutSyncDisk(root: string) {
34983496
);
34993497
}
35003498

3499+
function currentWorktreeTree(syncRoot: string) {
3500+
const indexDir = mkdtempSync(resolve(syncRoot, "openclaw-crabbox-index-"));
3501+
const indexPath = resolve(indexDir, "index");
3502+
const indexEnv = { GIT_INDEX_FILE: indexPath };
3503+
try {
3504+
const read = gitOutput(["read-tree", "HEAD"], indexEnv);
3505+
const add = gitOutput(["add", "-A", "--", "."], indexEnv);
3506+
const tree = gitOutput(["write-tree"], indexEnv);
3507+
if (read.status !== 0 || add.status !== 0 || tree.status !== 0 || !tree.stdout) {
3508+
throw new Error(read.text || add.text || tree.text || "git write-tree failed");
3509+
}
3510+
return tree.stdout;
3511+
} finally {
3512+
rmSync(indexDir, { recursive: true, force: true });
3513+
}
3514+
}
3515+
35013516
function prepareFullCheckoutForSync(options: { changedGateBase?: string } = {}) {
35023517
const syncRoot = fullCheckoutSyncRoot();
35033518
assertFullCheckoutSyncDisk(syncRoot);
3519+
const changedGateTree = options.changedGateBase ? currentWorktreeTree(syncRoot) : "";
35043520
const dir = mkdtempSync(resolve(syncRoot, "openclaw-crabbox-sync-"));
35053521
let active = false;
35063522
let resolvedChangedGateBase = options.changedGateBase ?? "";
@@ -3526,19 +3542,15 @@ function prepareFullCheckoutForSync(options: { changedGateBase?: string } = {})
35263542
try {
35273543
bundleTempDir = mkdtempSync(resolve(syncRoot, "openclaw-crabbox-bundle-"));
35283544
const bundleTempPath = resolve(bundleTempDir, "changed-gate.bundle");
3529-
const head = gitOutput(["-C", dir, "rev-parse", "HEAD"]);
35303545
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}`);
3546+
const baseTree = gitOutput(["-C", dir, "rev-parse", `${options.changedGateBase}^{tree}`]);
3547+
if (base.status !== 0 || baseTree.status !== 0 || !base.stdout || !baseTree.stdout) {
3548+
throw new Error(`git rev-parse failed: ${base.text || baseTree.text}`);
35333549
}
35343550
resolvedChangedGateBase = base.stdout;
3535-
if (head.stdout === base.stdout) {
3551+
if (changedGateTree === baseTree.stdout) {
35363552
writeFileSync(bundleTempPath, "", "utf8");
35373553
} 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-
}
35423554
// A parentless carrier makes the bundle self-contained while sending
35433555
// only the final tree. The remote attaches the fetched base as parent.
35443556
const transportCommit = gitOutput([
@@ -3549,7 +3561,7 @@ function prepareFullCheckoutForSync(options: { changedGateBase?: string } = {})
35493561
"-c",
35503562
35513563
"commit-tree",
3552-
headTree.stdout,
3564+
changedGateTree,
35533565
"-m",
35543566
"remote-changed-gate-tree",
35553567
]);
@@ -3938,7 +3950,7 @@ normalizedArgs = scriptBootstrap.args;
39383950
const scriptStdinPrepared = scriptBootstrap.prepared;
39393951
let wsl2ScriptBootstrap = { args: normalizedArgs, cleanup: () => {}, prepared: false };
39403952
try {
3941-
if (shouldUseFullCheckoutForCleanRemoteSync(normalizedArgs, provider)) {
3953+
if (shouldUseFullCheckoutForRemoteSync(normalizedArgs, provider)) {
39423954
const invocation = parseRunInvocation(help.text, normalizedArgs);
39433955
const facts = analyzeRemoteCommand(invocation);
39443956
const changedGate = facts.changedGate ? changedGateBaseForCommand(facts.commandArgs) : null;
@@ -3953,11 +3965,11 @@ try {
39533965
remoteChangedGateBase = checkout.changedGateBase;
39543966
remoteChangedGateAlias = changedGate?.remoteAlias ?? "";
39553967
console.error(
3956-
`[crabbox] sparse clean checkout detected; syncing from temporary full checkout ${checkout.dir}`,
3968+
`[crabbox] isolated checkout sync; syncing from temporary full checkout ${checkout.dir}`,
39573969
);
39583970
if (checkout.changedGateBase) {
39593971
console.error(
3960-
`[crabbox] remote changed gate detected; overlaying local HEAD as worktree changes from ${checkout.changedGateBase}`,
3972+
`[crabbox] remote changed gate detected; overlaying the local worktree as changes from ${checkout.changedGateBase}`,
39613973
);
39623974
}
39633975
}
@@ -4124,7 +4136,7 @@ if (childStderr) {
41244136
});
41254137
}
41264138
const childKillGraceMs = resolveChildKillGraceMs(process.env);
4127-
let childForceKillTimer: NodeJS.Timeout | undefined;
4139+
let childForceKillTimer: ReturnType<typeof setTimeout> | undefined;
41284140
let childTreeShutdownStarted = false;
41294141
if (fullCheckout) {
41304142
try {

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

Lines changed: 40 additions & 4 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
}
@@ -3303,7 +3307,7 @@ describe("scripts/crabbox-wrapper", () => {
33033307
cleanSparseSyncOptions,
33043308
);
33053309
expect(result.stderr).toContain("syncing from temporary full checkout");
3306-
expect(result.stderr).toContain("overlaying local HEAD as worktree changes from abc123");
3310+
expect(result.stderr).toContain("overlaying the local worktree as changes from abc123");
33073311
expect(output.args.join(" ")).toContain(
33083312
"openclaw_changed_gate_bundle=.openclaw-crabbox-changed-gate.bundle",
33093313
);
@@ -3370,7 +3374,7 @@ describe("scripts/crabbox-wrapper", () => {
33703374
},
33713375
},
33723376
);
3373-
expect(result.stderr).toContain("overlaying local HEAD as worktree changes from release123");
3377+
expect(result.stderr).toContain("overlaying the local worktree as changes from release123");
33743378
expect(remoteCommand).toContain("openclaw_changed_gate_base=release123");
33753379
expect(remoteCommand).toContain(
33763380
"openclaw_changed_gate_alias=refs/remotes/origin/release/2026.7.2",
@@ -3534,7 +3538,7 @@ describe("scripts/crabbox-wrapper", () => {
35343538
},
35353539
);
35363540
expect(result.stderr).toContain("syncing from temporary full checkout");
3537-
expect(result.stderr).toContain("overlaying local HEAD as worktree changes from abc123");
3541+
expect(result.stderr).toContain("overlaying the local worktree as changes from abc123");
35383542
expect(output.cwd).toContain("openclaw-crabbox-sync-");
35393543
expect(output.args).toContain("--shell");
35403544
expect(remoteCommand).toContain("git init -q");
@@ -3544,6 +3548,38 @@ describe("scripts/crabbox-wrapper", () => {
35443548
);
35453549
});
35463550

3551+
it("bootstraps the exact dirty worktree tree for remote changed gates", () => {
3552+
const dirtyTree = "dirty-tree-123";
3553+
const { output, remoteCommand, result } = runSuccessfulDefaultWrapper(
3554+
[
3555+
"run",
3556+
"--provider",
3557+
"blacksmith-testbox",
3558+
"--blacksmith-ref",
3559+
"main",
3560+
"--",
3561+
"corepack",
3562+
"pnpm",
3563+
"check:changed",
3564+
],
3565+
{
3566+
env: {
3567+
OPENCLAW_FAKE_GIT_EXPECT_COMMIT_TREE: dirtyTree,
3568+
OPENCLAW_FAKE_GIT_WORKTREE_TREE_SHA: dirtyTree,
3569+
},
3570+
gitResponses: {
3571+
[GIT_STATUS_PORCELAIN_KEY]: { stdout: " M scripts/crabbox-wrapper.mts\n" },
3572+
[GIT_MERGE_BASE_MAIN_HEAD_KEY]: { stdout: "abc123\n" },
3573+
},
3574+
},
3575+
);
3576+
3577+
expect(result.stderr).toContain("syncing from temporary full checkout");
3578+
expect(result.stderr).toContain("overlaying the local worktree as changes from abc123");
3579+
expect(output.cwd).toContain("openclaw-crabbox-sync-");
3580+
expectChangedGateGitBootstrap(remoteCommand);
3581+
});
3582+
35473583
it("bootstraps Git metadata for env-prefixed sparse changed gates", () => {
35483584
const { output, remoteCommand } = runSuccessfulDefaultWrapper(
35493585
[

0 commit comments

Comments
 (0)