Skip to content

Commit ce2d18a

Browse files
committed
fix(backup): block pushes of adopted history
1 parent 2ef3b69 commit ce2d18a

2 files changed

Lines changed: 82 additions & 10 deletions

File tree

‎src/snapshot/git-backup.test.ts‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import path from "node:path";
44
import { DatabaseSync } from "node:sqlite";
55
import { afterEach, describe, expect, it, vi } from "vitest";
66
import { loadSqliteVecExtension } from "../../packages/memory-host-sdk/src/engine-storage.js";
7-
import { requireGitCommand as requireGit } from "../infra/git-exec.js";
7+
import { backupGitCreateCommand } from "../commands/backup-git.js";
8+
import { readBackupFreshness } from "../commands/backup-health.js";
9+
import { createTestRuntime } from "../commands/test-runtime-config-helpers.js";
10+
import { executeGitCommand, requireGitCommand as requireGit } from "../infra/git-exec.js";
811
import { OPENCLAW_AGENT_SCHEMA_VERSION } from "../state/openclaw-agent-db-contract.js";
912
import { OPENCLAW_STATE_SCHEMA_VERSION } from "../state/openclaw-state-db-contract.js";
1013
import {
1114
closeOpenClawStateDatabaseForTest,
1215
openOpenClawStateDatabase,
1316
} from "../state/openclaw-state-db.js";
1417
import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
15-
import { createPathResolutionEnv } from "../test-utils/env.js";
18+
import { createPathResolutionEnv, withEnvAsync } from "../test-utils/env.js";
1619
import { dumpGitBackupDatabase, restoreGitBackupDirectory } from "./git-backup-codec.js";
1720
import { createGitBackup, initializeGitBackupRepository } from "./git-backup.js";
1821

@@ -412,6 +415,62 @@ describe("Git-backed SQLite snapshots", () => {
412415
expect(result.pushWarning?.length).toBeLessThanOrEqual(500);
413416
});
414417

418+
it("refuses adopted non-backup ancestry and records local push degradation", async () => {
419+
const root = await tempRoot();
420+
const { stateDir } = createStateDatabaseFixture(root);
421+
const repositoryPath = path.join(root, "adopted-repository");
422+
const remotePath = path.join(root, "remote.git");
423+
await requireGit(root, ["init", "--bare", remotePath]);
424+
await initializeGitBackupRepository({ repositoryPath, stateDir, remote: remotePath });
425+
await requireGit(repositoryPath, ["config", "user.name", "OpenClaw Backup Test"]);
426+
await requireGit(repositoryPath, ["config", "user.email", "[email protected]"]);
427+
await fs.writeFile(path.join(repositoryPath, "unrelated.txt"), "operator-owned\n");
428+
await requireGit(repositoryPath, ["add", "unrelated.txt"]);
429+
await requireGit(repositoryPath, ["commit", "-m", "operator history"]);
430+
431+
const warning =
432+
"repository history contains non-backup commits; use a dedicated backup repository";
433+
await withEnvAsync({ OPENCLAW_STATE_DIR: stateDir }, async () => {
434+
const result = await backupGitCreateCommand(createTestRuntime(), {
435+
repository: repositoryPath,
436+
global: true,
437+
push: true,
438+
excludeSecrets: true,
439+
});
440+
441+
expect(result).toMatchObject({ noChanges: false, pushed: false, pushWarning: warning });
442+
expect(result.commit).toMatch(/^[a-f0-9]{40}$/u);
443+
expect(readBackupFreshness(process.env)).toMatchObject({
444+
latest: { status: "ok", kind: "git", pushFailed: true, error: warning },
445+
latestOk: { status: "ok", kind: "git", pushFailed: true, error: warning },
446+
});
447+
});
448+
expect((await executeGitCommand(remotePath, ["show-ref"])).code).not.toBe(0);
449+
});
450+
451+
it("pushes backup-only ancestry to a new remote", async () => {
452+
const root = await tempRoot();
453+
const { stateDir, database } = createStateDatabaseFixture(root);
454+
const repositoryPath = path.join(root, "backup-repository");
455+
const remotePath = path.join(root, "remote.git");
456+
await requireGit(root, ["init", "--bare", remotePath]);
457+
await initializeGitBackupRepository({ repositoryPath, stateDir, remote: remotePath });
458+
await requireGit(repositoryPath, ["config", "user.name", "OpenClaw Backup Test"]);
459+
await requireGit(repositoryPath, ["config", "user.email", "[email protected]"]);
460+
461+
const result = await createGitBackup({
462+
repositoryPath,
463+
stateDir,
464+
databases: [database],
465+
push: true,
466+
});
467+
468+
const branch = await requireGit(repositoryPath, ["branch", "--show-current"]);
469+
expect(result).toMatchObject({ noChanges: false, pushed: true });
470+
expect(result).not.toHaveProperty("pushWarning");
471+
expect(await requireGit(remotePath, ["rev-parse", `refs/heads/${branch}`])).toBe(result.commit);
472+
});
473+
415474
it("redacts credential-bearing origins in conflict errors", async () => {
416475
const root = await tempRoot();
417476
const stateDir = path.join(root, "state");

‎src/snapshot/git-backup.ts‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import type { SnapshotDatabaseRef } from "./snapshot-provider.js";
2525

2626
const GIT_BACKUP_MATERIALIZE_MAX_BYTES = 1024 * 1024 * 1024;
2727
const GIT_BACKUP_DIAGNOSTIC_MAX_LENGTH = 500;
28+
const GIT_BACKUP_NON_BACKUP_HISTORY_WARNING =
29+
"repository history contains non-backup commits; use a dedicated backup repository";
2830

2931
type GitBackupCreateResult = {
3032
repositoryPath: string;
@@ -268,15 +270,26 @@ export async function createGitBackup(params: {
268270
let pushed = false;
269271
let pushWarning: string | undefined;
270272
if (params.push) {
271-
const pushedResult = await runGit(repositoryPath, ["push", "-u", "origin", "HEAD"], {
272-
env: params.gitEnv,
273-
});
274-
if (pushedResult.code === 0) {
275-
pushed = true;
273+
// Staging is path-scoped, but push ships HEAD's full ancestry. A dedicated
274+
// repository is the supported remote shape.
275+
const nonBackupCommitCount = await requireGit(
276+
repositoryPath,
277+
["rev-list", "HEAD", "--invert-grep", "--grep=^openclaw backup ", "--count"],
278+
{ env: params.gitEnv },
279+
);
280+
if (nonBackupCommitCount !== "0") {
281+
pushWarning = GIT_BACKUP_NON_BACKUP_HISTORY_WARNING;
276282
} else {
277-
pushWarning = sanitizeGitBackupDiagnostic(
278-
(pushedResult.stderr || pushedResult.stdout).trim() || "git push failed",
279-
);
283+
const pushedResult = await runGit(repositoryPath, ["push", "-u", "origin", "HEAD"], {
284+
env: params.gitEnv,
285+
});
286+
if (pushedResult.code === 0) {
287+
pushed = true;
288+
} else {
289+
pushWarning = sanitizeGitBackupDiagnostic(
290+
(pushedResult.stderr || pushedResult.stdout).trim() || "git push failed",
291+
);
292+
}
280293
}
281294
}
282295
return {

0 commit comments

Comments
 (0)