Add rollback snapshot controls

This commit is contained in:
ZacharyZhang-NY committed 2026-05-23 18:57:18 -04:00
1 parent 6768d19239
commit 89763592de
19 files changed
+551 -7

No files matched your search

@@ -23,7 +23,7 @@ describe("domain services", () => {
const connection = createConnection();
const rows = connection.db.prepare("select version from schema_migrations order by version asc").all() as Array<{ version: number }>;
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]);
connection.close();
});
@@ -153,6 +153,7 @@ describe("migration service", () => {
create table retention_freezes(id text primary key, org_id text, created_by_employee_id text);
create table search_documents(entity_id text);
create table audit_replay_runs(id text primary key, org_id text, thread_id text, created_by_employee_id text);
create table rollback_snapshots(id text primary key, org_id text, migration_report_id text, created_by_employee_id text);
create table audit_events(id text primary key, org_id text);
insert into organizations(id) values ('org-1');
insert into employees(id, org_id) values ('emp-1', 'org-1');
@@ -216,6 +217,7 @@ describe("migration service", () => {
insert into retention_freezes(id, org_id, created_by_employee_id) values ('freeze-1', 'org-1', 'emp-1');
insert into search_documents(entity_id) values ('task-1');
insert into audit_replay_runs(id, org_id, thread_id, created_by_employee_id) values ('replay-1', 'org-1', 'thread-1', 'emp-1');
insert into rollback_snapshots(id, org_id, migration_report_id, created_by_employee_id) values ('rollback-1', 'org-1', 'migration-1', 'emp-1');
insert into audit_events(id, org_id) values ('audit-1', 'org-1');
`);
let orgId = "";
@@ -244,6 +246,7 @@ describe("migration service", () => {
expect(body.tableCounts.provider_usage_windows).toBe(1);
expect(body.tableCounts.compliance_exports).toBe(1);
expect(body.tableCounts.audit_replay_runs).toBe(1);
expect(body.tableCounts.rollback_snapshots).toBe(1);
expect(body.tableCounts.search_documents).toBe(1);
expect(body.tableChecksums.audit_events).toHaveLength(64);
expect(body.idMaps.skill_runtime_snapshots).toEqual(["snapshot-1"]);
@@ -257,6 +260,7 @@ describe("migration service", () => {
expect(body.idMaps.board_interventions).toEqual(["bdi-1"]);
expect(body.idMaps.retention_freezes).toEqual(["freeze-1"]);
expect(body.idMaps.audit_replay_runs).toEqual(["replay-1"]);
expect(body.idMaps.rollback_snapshots).toEqual(["rollback-1"]);
expect(body.idMaps.provider_usage_windows).toEqual(["usage-1"]);
connection.close();
});
@@ -0,0 +1,99 @@
import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { DatabaseSync } from "node:sqlite";
import { describe, expect, test } from "vitest";
import { DatabaseConnection } from "../../src/main/database/connection";
import { MigrationService } from "../../src/main/services/migration-service";
import { OrganizationService } from "../../src/main/services/organization-service";
import { RollbackService } from "../../src/main/services/rollback-service";
import { SnapshotService } from "../../src/main/services/snapshot-service";
function createConnection(): DatabaseConnection {
return new DatabaseConnection(join(tmpdir(), `agentcompany-new-rollback-${randomUUID()}.db`));
}
function createLegacyDatabase(): string {
const legacyPath = join(tmpdir(), `agentcompany-legacy-${randomUUID()}.db`);
const legacy = new DatabaseSync(legacyPath);
legacy.exec(`
create table organizations(id text primary key);
insert into organizations(id) values ('legacy-org');
`);
legacy.close();
return legacyPath;
}
describe("rollback service", () => {
test("creates immutable rollback snapshots from migration reports and workspace manifests", () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);
const migrations = new MigrationService(connection.db);
const rollback = new RollbackService(connection.db);
const snapshot = new SnapshotService(connection.db);
const legacyPath = createLegacyDatabase();
let orgId = "";
let employeeId = "";
let reportId = "";
let rollbackId = "";
connection.transaction(() => {
orgId = organization.createOrganization({
organizationName: "Acme Operations",
employeeName: "Dana Lee",
mission: "Coordinate agents with auditable approvals.",
goal: "Ship a verified control plane.",
});
employeeId = (connection.db.prepare("select id from employees where org_id = ?").get(orgId) as { id: string }).id;
seedWorkspace(connection.db, orgId);
reportId = migrations.createReport({ orgId, sourceDatabasePath: legacyPath });
rollbackId = rollback.createSnapshot({ orgId, migrationReportId: reportId, createdByEmployeeId: employeeId });
rollback.setActive({ orgId, snapshotId: rollbackId, active: true, employeeId });
});
const state = snapshot.load();
const record = state.rollbackSnapshots[0];
if (!record) throw new Error("Rollback snapshot was not created.");
const workspaceManifest = JSON.parse(record.workspaceManifestJson) as { workspaces: unknown[]; files: unknown[] };
const deltaJournal = JSON.parse(record.deltaJournalJson) as unknown[];
const restorePlan = JSON.parse(record.restorePlanJson) as { migrationReportId: string; restoreStrategy: string };
expect(record).toMatchObject({ orgId, migrationReportId: reportId, active: true, createdByEmployeeId: employeeId });
expect(record.sourceDatabaseSha256).toHaveLength(64);
expect(record.workspaceManifestSha256).toHaveLength(64);
expect(workspaceManifest.workspaces).toHaveLength(1);
expect(workspaceManifest.files).toHaveLength(1);
expect(deltaJournal.length).toBeGreaterThan(0);
expect(restorePlan).toMatchObject({ migrationReportId: reportId, restoreStrategy: "legacy_snapshot_restore" });
expect(state.auditEvents.some((event) => event.entityType === "rollback_snapshot" && event.action === "activated")).toBe(true);
connection.close();
});
});
function seedWorkspace(db: DatabaseSync, orgId: string): void {
const now = new Date().toISOString();
db.prepare(`
insert into workspaces(id, org_id, name, local_path, repo_url, repo_ref, project_id, is_primary, created_at, updated_at)
values (?, ?, ?, ?, ?, ?, null, 1, ?, ?)
`).run("wsp-rollback", orgId, "Primary", "C:\\repo\\workspace", "https://example.invalid/repo.git", "main", now, now);
db.prepare(`
insert into file_objects(
id, org_id, name, kind, size_bytes, sha256, storage_path, owner_type, owner_id,
created_at, pinned, space_id, updated_at, scan_state, scan_labels_json, scanned_at
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, null, ?, ?, ?, ?)
`).run(
"file-rollback",
orgId,
"release.txt",
"document",
12,
"0".repeat(64),
"artifacts/release.txt",
"system",
null,
now,
now,
"approved",
"[]",
now,
);
}