|
| 1 | +import { createHash } from "node:crypto"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import fsp from "node:fs/promises"; |
| 4 | +import path from "node:path"; |
| 5 | +import { describe, expect, it } from "vitest"; |
| 6 | +import { resolveWorkspaceStateIdentity } from "../agents/workspace-state-store.js"; |
| 7 | +import { openOpenClawStateDatabase } from "../state/openclaw-state-db.js"; |
| 8 | +import { useWorkspaceMigrationTestFixture } from "./state-migrations.workspace-setup.test-support.js"; |
| 9 | + |
| 10 | +describe("recreated legacy workspace state migration", () => { |
| 11 | + const { migrate, setup } = useWorkspaceMigrationTestFixture(); |
| 12 | + |
| 13 | + it("cleans a covered setup marker recreated after completed migration", async () => { |
| 14 | + const context = setup(); |
| 15 | + const identity = resolveWorkspaceStateIdentity(context.workspaceDir); |
| 16 | + const setupPath = path.join(context.workspaceDir, "openclaw-workspace-state.json"); |
| 17 | + const seededAt = "2026-07-15T10:00:00.000Z"; |
| 18 | + const completedAt = "2026-07-15T10:01:00.000Z"; |
| 19 | + await fsp.writeFile( |
| 20 | + setupPath, |
| 21 | + JSON.stringify({ version: 1, bootstrapSeededAt: seededAt, setupCompletedAt: completedAt }), |
| 22 | + "utf8", |
| 23 | + ); |
| 24 | + expect((await migrate(context)).warnings).toEqual([]); |
| 25 | + |
| 26 | + const recreated = JSON.stringify({ version: 1, setupCompletedAt: completedAt }); |
| 27 | + await fsp.writeFile(setupPath, recreated, "utf8"); |
| 28 | + |
| 29 | + const result = await migrate(context); |
| 30 | + |
| 31 | + expect(result.warnings).toEqual([]); |
| 32 | + expect(fs.existsSync(setupPath)).toBe(false); |
| 33 | + const db = openOpenClawStateDatabase({ env: context.env }).db; |
| 34 | + expect( |
| 35 | + db |
| 36 | + .prepare( |
| 37 | + "SELECT bootstrap_seeded_at, setup_completed_at FROM workspace_setup_state WHERE workspace_key = ?", |
| 38 | + ) |
| 39 | + .get(identity.workspaceKey), |
| 40 | + ).toEqual({ bootstrap_seeded_at: seededAt, setup_completed_at: completedAt }); |
| 41 | + const receipt = db |
| 42 | + .prepare( |
| 43 | + "SELECT source_sha256, removed_source, report_json FROM migration_sources WHERE source_path = ?", |
| 44 | + ) |
| 45 | + .get(setupPath) as { |
| 46 | + source_sha256: string; |
| 47 | + removed_source: number; |
| 48 | + report_json: string; |
| 49 | + }; |
| 50 | + expect(receipt).toMatchObject({ |
| 51 | + source_sha256: createHash("sha256").update(recreated).digest("hex"), |
| 52 | + removed_source: 1, |
| 53 | + }); |
| 54 | + expect(JSON.parse(receipt.report_json)).toMatchObject({ |
| 55 | + authoritative: true, |
| 56 | + resolution: "verified", |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it.each([ |
| 61 | + { claimed: false, state: "source" }, |
| 62 | + { claimed: true, state: "interrupted claim" }, |
| 63 | + ])("imports an attestation recreated as a sole $state", async ({ claimed }) => { |
| 64 | + const context = setup(); |
| 65 | + const identity = resolveWorkspaceStateIdentity(context.workspaceDir); |
| 66 | + const attestationPath = path.join( |
| 67 | + context.stateDir, |
| 68 | + "workspace-attestations", |
| 69 | + `${identity.workspaceKey}.attested`, |
| 70 | + ); |
| 71 | + await fsp.mkdir(path.dirname(attestationPath), { recursive: true }); |
| 72 | + await fsp.writeFile( |
| 73 | + attestationPath, |
| 74 | + "openclaw-workspace-attestation:v1\n2026-07-15T11:00:00.000Z\n", |
| 75 | + "utf8", |
| 76 | + ); |
| 77 | + const originalMtime = new Date("2026-07-15T11:01:00.000Z"); |
| 78 | + await fsp.utimes(attestationPath, originalMtime, originalMtime); |
| 79 | + expect((await migrate(context)).warnings).toEqual([]); |
| 80 | + |
| 81 | + const recreated = "openclaw-workspace-attestation:v1\n2026-07-16T11:00:00.000Z\n"; |
| 82 | + await fsp.mkdir(path.dirname(attestationPath), { recursive: true }); |
| 83 | + await fsp.writeFile(attestationPath, recreated, "utf8"); |
| 84 | + const recreatedMtime = new Date("2026-07-16T11:01:00.000Z"); |
| 85 | + await fsp.utimes(attestationPath, recreatedMtime, recreatedMtime); |
| 86 | + const claimPath = `${attestationPath}.doctor-importing`; |
| 87 | + if (claimed) { |
| 88 | + await fsp.rename(attestationPath, claimPath); |
| 89 | + } |
| 90 | + |
| 91 | + const result = await migrate(context); |
| 92 | + |
| 93 | + expect(result.warnings).toEqual([]); |
| 94 | + expect(fs.existsSync(attestationPath)).toBe(false); |
| 95 | + expect(fs.existsSync(claimPath)).toBe(false); |
| 96 | + const db = openOpenClawStateDatabase({ env: context.env }).db; |
| 97 | + expect( |
| 98 | + db |
| 99 | + .prepare("SELECT attested_at_ms FROM workspace_attestations WHERE workspace_key = ?") |
| 100 | + .get(identity.workspaceKey), |
| 101 | + ).toEqual({ attested_at_ms: recreatedMtime.getTime() }); |
| 102 | + expect( |
| 103 | + db |
| 104 | + .prepare( |
| 105 | + "SELECT source_sha256, removed_source FROM migration_sources WHERE source_path = ?", |
| 106 | + ) |
| 107 | + .get(attestationPath), |
| 108 | + ).toEqual({ |
| 109 | + source_sha256: createHash("sha256").update(recreated).digest("hex"), |
| 110 | + removed_source: 1, |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("retains colliding source and claim from a recreated generation", async () => { |
| 115 | + const context = setup(); |
| 116 | + const identity = resolveWorkspaceStateIdentity(context.workspaceDir); |
| 117 | + const attestationPath = path.join( |
| 118 | + context.stateDir, |
| 119 | + "workspace-attestations", |
| 120 | + `${identity.workspaceKey}.attested`, |
| 121 | + ); |
| 122 | + const original = "openclaw-workspace-attestation:v1\n2026-07-15T11:00:00.000Z\n"; |
| 123 | + await fsp.mkdir(path.dirname(attestationPath), { recursive: true }); |
| 124 | + await fsp.writeFile(attestationPath, original, "utf8"); |
| 125 | + const originalMtime = new Date("2026-07-15T11:01:00.000Z"); |
| 126 | + await fsp.utimes(attestationPath, originalMtime, originalMtime); |
| 127 | + expect((await migrate(context)).warnings).toEqual([]); |
| 128 | + |
| 129 | + const recreated = "openclaw-workspace-attestation:v1\n2026-07-16T11:00:00.000Z\n"; |
| 130 | + const claimPath = `${attestationPath}.doctor-importing`; |
| 131 | + await fsp.mkdir(path.dirname(attestationPath), { recursive: true }); |
| 132 | + await Promise.all([ |
| 133 | + fsp.writeFile(attestationPath, recreated, "utf8"), |
| 134 | + fsp.writeFile(claimPath, recreated, "utf8"), |
| 135 | + ]); |
| 136 | + |
| 137 | + const result = await migrate(context); |
| 138 | + |
| 139 | + expect(result.warnings).toEqual([ |
| 140 | + "Workspace state is in SQLite, but source and interrupted claim both exist.", |
| 141 | + ]); |
| 142 | + expect(fs.existsSync(attestationPath)).toBe(true); |
| 143 | + expect(fs.existsSync(claimPath)).toBe(true); |
| 144 | + const db = openOpenClawStateDatabase({ env: context.env }).db; |
| 145 | + expect( |
| 146 | + db |
| 147 | + .prepare("SELECT attested_at_ms FROM workspace_attestations WHERE workspace_key = ?") |
| 148 | + .get(identity.workspaceKey), |
| 149 | + ).toEqual({ attested_at_ms: originalMtime.getTime() }); |
| 150 | + expect( |
| 151 | + db |
| 152 | + .prepare( |
| 153 | + "SELECT source_sha256, removed_source FROM migration_sources WHERE source_path = ?", |
| 154 | + ) |
| 155 | + .get(attestationPath), |
| 156 | + ).toEqual({ |
| 157 | + source_sha256: createHash("sha256").update(original).digest("hex"), |
| 158 | + removed_source: 1, |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments