Skip to content

Commit 6c76f18

Browse files
ruel225claude
andcommitted
fix(backup): exclude configured workspaces from the state asset when inclusion is disabled
Backup plan params carry workspaceExclusions so the inventory prunes configured workspaces (and their nested entries) from the state asset when workspace inclusion is disabled, instead of archiving them and leaking workspace files into backups. Co-Authored-By: Claude <[email protected]>
1 parent 5708157 commit 6c76f18

4 files changed

Lines changed: 87 additions & 6 deletions

File tree

‎src/commands/backup-resource-inventory.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ export async function createBackupResourceInventory(params: {
101101
configPath: string;
102102
oauthDir: string;
103103
workspaceDirs: readonly string[];
104+
/** Workspace roots requested out of scope; they prune from broader assets instead of becoming assets. */
105+
excludedWorkspaceDirs?: readonly string[];
104106
agentRoots: readonly BackupAgentRoot[];
105107
pluginResources: readonly ResolvedPluginBackupResource[];
106108
pluginRoots: readonly string[];
@@ -180,10 +182,15 @@ export async function createBackupResourceInventory(params: {
180182
}),
181183
);
182184
const protectedPaths = Object.freeze([...protectedPathSet].toSorted());
185+
// Out-of-scope workspace roots prune with the same subtree semantics as
186+
// regenerable roots so a broad state asset cannot swallow a nested workspace,
187+
// but they are not regenerable: keep them out of regenerableRoots so the
188+
// manifest never records a workspace as a disposable resource.
183189
const excludedPaths = Object.freeze(
184-
uniqueRegenerableRoots
185-
.map((resource) => resource.sourcePath)
186-
.toSorted((left, right) => right.length - left.length || left.localeCompare(right)),
190+
[
191+
...uniqueRegenerableRoots.map((resource) => resource.sourcePath),
192+
...(params.excludedWorkspaceDirs ?? []).map((workspaceDir) => path.resolve(workspaceDir)),
193+
].toSorted((left, right) => right.length - left.length || left.localeCompare(right)),
187194
);
188195

189196
const isIncluded = (sourcePath: string): boolean => {

‎src/commands/backup-shared.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ async function resolveBackupPlanFromPaths(params: {
173173
configPath: string;
174174
oauthDir: string;
175175
workspaceDirs?: string[];
176+
workspaceExclusions?: readonly string[];
176177
agentRoots?: readonly BackupAgentRoot[];
177178
pluginInventory?: ActivatedPluginBackupInventory;
178179
unresolvedOwnership?: boolean;
@@ -194,13 +195,24 @@ async function resolveBackupPlanFromPaths(params: {
194195
const configInsideState = params.configInsideState ?? false;
195196
const oauthInsideState = params.oauthInsideState ?? false;
196197
const canonicalStateDir = await canonicalizePathForContainment(stateDir);
198+
// A workspace root that contains the state directory would prune the whole
199+
// state asset; that layout leaves includeWorkspace=false without a meaningful
200+
// scope, so keep state backup intact and drop such an exclusion instead.
201+
const workspaceExclusions = (
202+
await Promise.all(
203+
(params.workspaceExclusions ?? []).map((workspaceDir) =>
204+
canonicalizePathForContainment(workspaceDir),
205+
),
206+
)
207+
).filter((workspaceDir) => !isPathWithin(canonicalStateDir, workspaceDir));
197208
const inventory = await createBackupResourceInventory({
198209
stateDir: canonicalStateDir,
199210
configPath: await canonicalizePathForContainment(configPath),
200211
oauthDir: await canonicalizePathForContainment(oauthDir),
201212
workspaceDirs: await Promise.all(
202213
workspaceDirs.map((workspaceDir) => canonicalizePathForContainment(workspaceDir)),
203214
),
215+
excludedWorkspaceDirs: workspaceExclusions,
204216
agentRoots,
205217
pluginResources: params.pluginInventory?.resources ?? [],
206218
pluginRoots: params.pluginInventory?.pluginRoots ?? [],
@@ -555,20 +567,24 @@ export async function resolveBackupPlanFromDisk(
555567
const agentRoots = unresolvedOwnership
556568
? []
557569
: await resolveBackupAgentRoots(discoverySnapshot.config);
558-
const workspaceDirs = includeWorkspace ? cleanupPlan.workspaceDirs : [];
570+
const workspaceDirs = cleanupPlan.workspaceDirs;
571+
// Plugin resources follow the requested scope: an excluded workspace is not
572+
// an include anchor for workspace-scoped plugin resources.
573+
const workspaceIncludes = includeWorkspace ? workspaceDirs : [];
559574
const pluginInventory = unresolvedOwnership
560575
? undefined
561576
: resolveActivatedPluginBackupInventory({
562577
config: discoverySnapshot.config,
563578
env: process.env,
564579
stateDir,
565-
workspaceDirs,
580+
workspaceDirs: workspaceIncludes,
566581
});
567582
return await resolveBackupPlanFromPaths({
568583
stateDir,
569584
configPath,
570585
oauthDir,
571-
workspaceDirs,
586+
workspaceDirs: workspaceIncludes,
587+
workspaceExclusions: includeWorkspace ? [] : workspaceDirs,
572588
agentRoots,
573589
pluginInventory,
574590
unresolvedOwnership,

‎src/commands/backup.test-support.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type ResolveBackupPlanFromPathsParams = {
1414
configPath: string;
1515
oauthDir: string;
1616
workspaceDirs?: string[];
17+
workspaceExclusions?: readonly string[];
1718
includeWorkspace?: boolean;
1819
onlyConfig?: boolean;
1920
configInsideState?: boolean;

‎src/commands/backup.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,63 @@ describe("backup commands", () => {
202202
expectWorkspaceCoveredByState(plan);
203203
});
204204

205+
it("prunes a workspace nested inside the state asset when workspace inclusion is disabled", async () => {
206+
const stateDir = path.join(tempHome.home, ".openclaw");
207+
const configPath = path.join(stateDir, "openclaw.json");
208+
const oauthDir = path.join(stateDir, "credentials");
209+
const workspaceDir = path.join(stateDir, "workspace");
210+
await fs.writeFile(configPath, JSON.stringify({}), "utf8");
211+
await fs.mkdir(oauthDir, { recursive: true });
212+
await fs.writeFile(path.join(oauthDir, "oauth.json"), "{}", "utf8");
213+
await fs.mkdir(workspaceDir, { recursive: true });
214+
await fs.writeFile(path.join(workspaceDir, "SOUL.md"), "# soul\n", "utf8");
215+
216+
const plan = await resolveBackupPlanFromPaths({
217+
stateDir,
218+
configPath,
219+
oauthDir,
220+
workspaceDirs: [workspaceDir],
221+
workspaceExclusions: [workspaceDir],
222+
includeWorkspace: false,
223+
configInsideState: true,
224+
oauthInsideState: true,
225+
nowMs: 123,
226+
});
227+
expect(plan.workspaceDirs).toStrictEqual([]);
228+
expect(plan.included.map((asset) => asset.kind)).toStrictEqual(["state"]);
229+
// The archive filter asks the inventory for every entry under the state
230+
// asset, so the exclusion must hold at inventory level, not just in assets.
231+
expect(plan.inventory.isIncluded(path.join(workspaceDir, "SOUL.md"))).toBe(false);
232+
expect(plan.inventory.isIncluded(workspaceDir)).toBe(false);
233+
expect(plan.inventory.isIncluded(configPath)).toBe(true);
234+
expect(plan.inventory.isTraversable(path.join(stateDir, "agents"))).toBe(true);
235+
});
236+
237+
it("keeps the state asset intact when a configured workspace contains the state directory", async () => {
238+
const stateDir = path.join(tempHome.home, ".openclaw");
239+
const configPath = path.join(stateDir, "openclaw.json");
240+
const oauthDir = path.join(stateDir, "credentials");
241+
await fs.writeFile(configPath, JSON.stringify({}), "utf8");
242+
await fs.mkdir(oauthDir, { recursive: true });
243+
await fs.writeFile(path.join(oauthDir, "oauth.json"), "{}", "utf8");
244+
245+
const plan = await resolveBackupPlanFromPaths({
246+
stateDir,
247+
configPath,
248+
oauthDir,
249+
// A workspace that contains the state directory leaves
250+
// includeWorkspace=false without a meaningful scope; excluding it would
251+
// prune the entire state asset.
252+
workspaceExclusions: [tempHome.home],
253+
includeWorkspace: false,
254+
configInsideState: true,
255+
oauthInsideState: true,
256+
nowMs: 123,
257+
});
258+
expect(plan.included.map((asset) => asset.kind)).toStrictEqual(["state"]);
259+
expect(plan.inventory.isIncluded(path.join(stateDir, "agents", "state.txt"))).toBe(true);
260+
});
261+
205262
it("orders coverage checks by canonical path so symlinked workspaces do not duplicate state", async () => {
206263
if (process.platform === "win32") {
207264
return;

0 commit comments

Comments
 (0)