Skip to content

Commit af05f64

Browse files
yetvalsteipete
andauthored
fix(agents): preserve worktree-local ignored state during lossless cleanup (#108401)
* fix(worktrees): preserve provisioned cleanup state Co-authored-by: yetval <[email protected]> * refactor(worktrees): break provisioned registry cycle --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent a1ed5e7 commit af05f64

13 files changed

Lines changed: 1448 additions & 104 deletions

‎docs/concepts/managed-worktrees.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Add `.worktreeinclude` at the source repository root to copy selected ignored, u
3030
fixtures/generated/**
3131
```
3232

33-
Only files reported by git as both ignored and untracked are eligible. Tracked files are already present through git and are never copied by this step. OpenClaw does not overwrite destination files or follow symlinked directories, and it preserves copied file modes.
33+
Only files reported by git as both ignored and untracked are eligible. Tracked files are already present through git and are never copied by this step. OpenClaw does not overwrite or change destination files that already exist, does not follow symlinked directories, and preserves copied file modes. It records only paths it actually creates, so later manifest edits cannot make those files disappear from cleanup protection.
3434

3535
## Run repository setup
3636

@@ -59,7 +59,7 @@ The resulting managed worktree is owned by the session, and every agent run in t
5959

6060
## Snapshots, cleanup, and restore
6161

62-
Removal first creates a synthetic commit containing tracked and non-ignored untracked files, and pins it at `refs/openclaw/snapshots/<id>`. Gitignored files are excluded from the repository object database; files selected by `.worktreeinclude` are copied again during restore. If snapshot creation fails, removal stops. An explicit force delete can continue without a snapshot.
62+
Removal first creates a synthetic commit containing tracked and non-ignored untracked files, then pins it at `refs/openclaw/snapshots/<id>`. Ignored files never enter the repository object database. OpenClaw stores only the ignored files it actually provisioned in chunked shared-state database rows; the recorded path set remains authoritative even if `.worktreeinclude` later changes or disappears. Restore reads those bytes from the immutable snapshot and reapplies their complete modes. Automatic cleanup preserves a live worktree when a recorded path can no longer be snapshotted safely. If snapshot creation fails, removal stops. An explicit force delete can continue without a snapshot.
6363

6464
OpenClaw applies these cleanup rules:
6565

‎src/agents/worktrees/git.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { existsSync } from "node:fs";
22
import fs from "node:fs/promises";
33
import path from "node:path";
4-
import { runCommandWithTimeout } from "../../process/exec.js";
4+
import { runCommandBuffered, runCommandWithTimeout } from "../../process/exec.js";
55

66
const GIT_TIMEOUT_MS = 120_000;
77

@@ -19,7 +19,7 @@ type WorktreeListEntry = {
1919
export async function runGit(
2020
cwd: string,
2121
args: string[],
22-
options: { env?: NodeJS.ProcessEnv; input?: string } = {},
22+
options: { env?: NodeJS.ProcessEnv; input?: string | Uint8Array } = {},
2323
): Promise<GitResult> {
2424
return await runCommandWithTimeout(["git", "-C", cwd, ...args], {
2525
timeoutMs: GIT_TIMEOUT_MS,
@@ -36,7 +36,7 @@ export function commandError(command: string, result: GitResult): Error {
3636
export async function requireGit(
3737
cwd: string,
3838
args: string[],
39-
options: { env?: NodeJS.ProcessEnv; input?: string } = {},
39+
options: { env?: NodeJS.ProcessEnv; input?: string | Uint8Array } = {},
4040
): Promise<string> {
4141
const result = await runGit(cwd, args, options);
4242
if (result.code !== 0) {
@@ -53,6 +53,28 @@ export async function requireGitRaw(cwd: string, args: string[]): Promise<string
5353
return result.stdout;
5454
}
5555

56+
export async function requireGitBuffer(
57+
cwd: string,
58+
args: string[],
59+
options: { env?: NodeJS.ProcessEnv; input?: Uint8Array } = {},
60+
): Promise<Buffer> {
61+
const result = await runCommandBuffered(["git", "-C", cwd, ...args], {
62+
timeoutMs: GIT_TIMEOUT_MS,
63+
env: options.env,
64+
input: options.input,
65+
});
66+
if (result.code !== 0) {
67+
const detail = (result.stderr.length > 0 ? result.stderr : result.stdout)
68+
.toString("utf8")
69+
.trim()
70+
.split("\n")
71+
.slice(-12)
72+
.join("\n");
73+
throw new Error(`git ${args.join(" ")} failed${detail ? `:\n${detail}` : ""}`);
74+
}
75+
return result.stdout;
76+
}
77+
5678
function parseWorktreeList(output: string): WorktreeListEntry[] {
5779
const entries: WorktreeListEntry[] = [];
5880
let current: WorktreeListEntry | undefined;

0 commit comments

Comments
 (0)