mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 15:52:36 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - A hosted server copies the runtime skill bundles that retained runs still need to shared storage before it moves to a volume-free shape > - The online pass copies them a page at a time with `migrateRuntimeSkillAssets` > - The page fails at the first run whose bundle is no longer on the disk, so the later runs on that page are never saved and the pass names only the first failure > - The offline migration continues past such a run, collects it as unsaved, and refuses at the end > - This pull request gives the online page the same behavior and names each unsaved run > - The benefit is that the pass saves every bundle it can and reports each one it cannot ## Linked Issues or Issue Description Refs #22190, #22294, #22641 **What happened?** On staging, one stack reports `runtime_skill_assets_require_migration`. Its passes showed `runtime_skill_assets:runtime_skill_source_absent:missing` as a whole-page failure: the runs after the first missing bundle were not visited. **Expected behavior** The page saves every run whose bundle is on the disk and names each run whose bundle is gone. **Steps to reproduce** 1. Keep two retained native runs with unversioned skill bundles. Remove the bundle of the first run from the disk. 2. Run `migrateRuntimeSkillAssets` with `apply`. 3. See that it throws `runtime_skill_source_absent:missing` and does not save the second run. **Additional context** #22641 already makes every blocker in the pass result carry a code. This pull request was reduced to the runtime skill page after #22641 merged. ## What Changed - `migrateRuntimeSkillAssets` returns `{ status: "unsaved", code: "runtime_skill_source_absent:<reason>" }` for a run whose bundle is not on this disk, and continues. Other errors still stop the page, as in the offline migration. - The result type is a discriminated union, so `unsaved` always carries a code. - The online pass reports each unsaved run as `runtime_skill_assets:<code>`. - `migrate-skill-packages --phase=runtime-assets` prints the unsaved rows and exits 1 when any run stays unsaved. ## Verification - Red: `cd server && npx vitest run src/services/online-durable-data-migration.test.ts src/__tests__/runtime-skill-preservation.test.ts` failed 2 new tests: the real page (embedded PostgreSQL, real bundle files) threw `runtime_skill_source_absent:missing` before it saved the next run, and the pass had no code for unsaved runs. - Green: the same files and `online-durable-data-migration-postgres.test.ts` pass (42 tests). - `pnpm --filter @paperclipai/server typecheck` passes. - Local `pnpm verify` selects most of the suite for this change (2,979 of 3,855 files). Required CI supplies that coverage. ## Risks - No migration rule changes. A run whose bundle is gone still blocks the move, online and offline. - The maintenance command now exits 1 when a run stays unsaved, as it did before when the page threw. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code execution in Claude Code. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
474 lines
31 KiB
TypeScript
474 lines
31 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import type { Db } from "@paperclipai/db";
|
|
import type { StorageService } from "../storage/index.js";
|
|
import { RUNTIME_SKILL_ASSETS_BLOCKER } from "./ephemeral-server-contract.js";
|
|
import { resetOnlineDurableDataMigrationProgressForTests, runOnlineDurableDataMigration } from "./online-durable-data-migration.js";
|
|
|
|
const db = {} as Db;
|
|
const s3 = { provider: "s3" } as StorageService;
|
|
const local = { provider: "local_disk" } as StorageService;
|
|
|
|
function owners(overrides: Record<string, unknown> = {}) {
|
|
const calls: string[] = [];
|
|
const page = <T>(name: string, value: T) => async () => { calls.push(name); return value; };
|
|
const base = {
|
|
storage: () => s3,
|
|
localSkillStorage: () => local,
|
|
durableAgentFiles: () => true,
|
|
agentFiles: page("agentFiles", { results: [{ agentId: "a1", state: "durable" }, { agentId: "a2", state: "active_execution" }], next: null }),
|
|
skillObjects: page("skillObjects", { inspected: 2, migrated: 1, busy: 1, after: null }),
|
|
skillPackages: () => ({
|
|
migrateSnapshots: page("skillSnapshots", { inspected: 1, migrated: 1, after: null }),
|
|
migrateVersions: page("skillVersions", { inspected: 0, migrated: 0, after: null }),
|
|
}),
|
|
skillInstallations: () => ({ installations: page("skillInstallations", { inspected: 3, after: null, results: [
|
|
{ skillId: "s1", companyId: "c", status: "converted" }, { skillId: "s2", companyId: "c", status: "unchanged" },
|
|
{ skillId: "s3", companyId: "c", status: "linked_local" }] }) }),
|
|
runtimeSkillAssets: page("runtimeSkillAssets", { inspected: 1, after: null, results: [{ runId: "r1", status: "preserved" }] }),
|
|
workspaceCompanies: page("workspaceCompanies", []),
|
|
workspaceOperations: page("workspaceOperations", { reconciled: 0, held: [] }),
|
|
localWorkspaces: page("localWorkspaces", []),
|
|
inventoryWorkspace: page("inventoryWorkspace", { reasons: [] }),
|
|
migrateWorkspace: page("migrateWorkspace", { alreadyMigrated: true, reasons: [] }),
|
|
nativeHarnessBackups: async (_db: Db, input: { onOutcome?: (outcome: string) => void }) => {
|
|
calls.push("nativeHarnessBackups");
|
|
for (const outcome of ["published", "current", "source_active", "source_active"]) input.onOutcome?.(outcome);
|
|
return { inspected: 4, published: 1, current: 1, deferred: { source_active: 2 } };
|
|
},
|
|
nativeControllers: async (_db: Db, input: { onOutcome?: (outcome: string) => void }) => {
|
|
calls.push("nativeControllers");
|
|
for (const outcome of ["imported", "unchanged"]) input.onOutcome?.(outcome);
|
|
return { inspected: 2, imported: 1, unchanged: 1, archivedFailed: 0, sourceAlreadyAbsent: 0, deferred: {} };
|
|
},
|
|
runEvidencePage: async (_db: Db, options: { kinds: string[] }) => { calls.push(options.kinds[0] === "log" ? "runLogs" : "providerTraces"); return { items: [] }; },
|
|
eligibility: vi.fn(async () => ({ eligible: false, blockers: ["skills_require_migration", "volume_free_storage_flags_required"] })),
|
|
record: vi.fn(async () => undefined),
|
|
...overrides,
|
|
};
|
|
return { owners: base as never, calls, eligibility: base.eligibility, record: base.record };
|
|
}
|
|
|
|
beforeEach(() => resetOnlineDurableDataMigrationProgressForTests());
|
|
|
|
describe("online durable-data migration pass", () => {
|
|
it("drives every online-safe owner while serving and reports what it moved and what it left", async () => {
|
|
const fixture = owners();
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(fixture.calls).toEqual(["agentFiles", "skillObjects", "skillSnapshots", "skillVersions", "skillInstallations",
|
|
"runtimeSkillAssets", "runLogs", "providerTraces", "workspaceCompanies", "localWorkspaces", "nativeHarnessBackups", "nativeControllers"]);
|
|
expect(result).toEqual({
|
|
state: "completed",
|
|
counts: { agentFiles: 1, skillObjects: 1, skillSnapshots: 1, skillInstallations: 1, runtimeSkillRuns: 1, nativeHarnessBackups: 1, nativeControllers: 1 },
|
|
deferred: { "agent_files:active_execution": 1, "skill_objects:owner_busy": 1, "skills:linked_local": 1, "native_harness:source_active": 2 },
|
|
eligible: false,
|
|
blockers: ["skills_require_migration", "volume_free_storage_flags_required"],
|
|
});
|
|
expect(fixture.eligibility).toHaveBeenCalledWith(db);
|
|
expect(fixture.record).toHaveBeenCalledWith(db, result);
|
|
});
|
|
|
|
it("names why skill installations and retained run logs stay behind", async () => {
|
|
const fixture = owners({
|
|
skillInstallations: () => ({ installations: async () => ({ inspected: 3, after: null, results: [
|
|
{ skillId: "s1", companyId: "c", status: "blocked", reason: "Saved version does not cover the installed inventory", code: "saved_version_incomplete" },
|
|
{ skillId: "s2", companyId: "c", status: "blocked", reason: "network down" },
|
|
{ skillId: "s3", companyId: "c", status: "linked_local" }] }) }),
|
|
runEvidencePage: async (_db: Db, input: { kinds: string[] }) => ({ items: input.kinds[0] === "log" ? [
|
|
{ kind: "log", id: "r1", state: "blocked", reason: "native_log_archive_source_unsettled" },
|
|
{ kind: "log", id: "r2", state: "blocked", reason: "native_log_archive_source_unsettled" },
|
|
] : [] }),
|
|
});
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result.deferred).toMatchObject({ "skills:saved_version_incomplete": 1, "skills:blocked": 1, "skills:linked_local": 1,
|
|
"run_logs:native_log_archive_source_unsettled": 2 });
|
|
});
|
|
|
|
it("migrates agent files with live writers, never the offline mode", async () => {
|
|
const agentFiles = vi.fn(async () => ({ results: [], next: null }));
|
|
await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, owners({ agentFiles }).owners);
|
|
expect(agentFiles).toHaveBeenCalledWith(db, expect.objectContaining({ apply: true, writersStopped: false, limit: 5, signal: expect.any(AbortSignal) }));
|
|
});
|
|
|
|
it.each([
|
|
[{ storage: () => local }, "shared_object_storage_required"],
|
|
[{ durableAgentFiles: () => false }, "durable_agent_files_disabled"],
|
|
])("does nothing on a server that would keep writing local data (%#)", async (override, reason) => {
|
|
const fixture = owners(override);
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result).toMatchObject({ state: "unsupported", reason, counts: {}, eligible: false });
|
|
expect(fixture.calls).toEqual([]);
|
|
expect(fixture.record).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("resumes where the last budget ended instead of rescanning finished rows", async () => {
|
|
let clock = 0;
|
|
const seen: Array<string | undefined> = [];
|
|
const runtimeSkillAssets = vi.fn(async (_db: Db, options: { after?: string }) => {
|
|
seen.push(options.after);
|
|
clock += 40_000;
|
|
const start = Number(options.after ?? 0);
|
|
return { inspected: 50, after: String(start + 50), results: [] };
|
|
});
|
|
const fixture = owners({ runtimeSkillAssets });
|
|
const run = () => runOnlineDurableDataMigration(db, { budgetMs: 60_000, now: () => clock }, fixture.owners);
|
|
expect(await run()).toMatchObject({ state: "budget_exhausted", resumePhase: "runtime_skill_assets" });
|
|
expect(await run()).toMatchObject({ state: "budget_exhausted", resumePhase: "runtime_skill_assets" });
|
|
expect(seen).toEqual([undefined, "50", "100", "150"]);
|
|
expect(fixture.calls.filter(call => call === "agentFiles")).toHaveLength(1);
|
|
});
|
|
|
|
it("reaches the later owners over repeated budget-limited calls", async () => {
|
|
let clock = 0;
|
|
const slow = (name: string, value: unknown) => vi.fn(async () => { clock += 70_000; return value; });
|
|
const fixture = owners({
|
|
agentFiles: slow("agentFiles", { results: [], next: null }),
|
|
runtimeSkillAssets: slow("runtimeSkillAssets", { inspected: 0, after: null, results: [] }),
|
|
});
|
|
const run = () => runOnlineDurableDataMigration(db, { budgetMs: 60_000, now: () => clock }, fixture.owners);
|
|
expect((await run()).state).toBe("budget_exhausted");
|
|
expect((await run()).state).toBe("budget_exhausted");
|
|
expect((await run()).state).toBe("completed");
|
|
expect(fixture.calls).toContain("nativeControllers");
|
|
});
|
|
|
|
it("stops an owner that overruns the budget and keeps the work it already finished", async () => {
|
|
const nativeHarnessBackups = vi.fn(async (_db: Db, input: { signal: AbortSignal; onOutcome?: (outcome: string) => void }) => {
|
|
input.onOutcome?.("published");
|
|
input.onOutcome?.("source_active");
|
|
return new Promise((_resolve, reject) => {
|
|
input.signal.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")), { once: true });
|
|
});
|
|
});
|
|
const fixture = owners({ nativeHarnessBackups });
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 20 }, fixture.owners);
|
|
expect(result).toMatchObject({ state: "budget_exhausted", resumePhase: "native_harness_backups",
|
|
counts: { agentFiles: 1, nativeHarnessBackups: 1 }, deferred: { "native_harness:source_active": 1 } });
|
|
expect(fixture.record).toHaveBeenCalledWith(db, result);
|
|
});
|
|
|
|
it("halves a page that overruns the budget, then skips an owner whose single item keeps overrunning", async () => {
|
|
const limits: number[] = [];
|
|
const runtimeSkillAssets = vi.fn(async (_db: Db, input: { limit: number; signal: AbortSignal }) => {
|
|
limits.push(input.limit);
|
|
return new Promise((_resolve, reject) => {
|
|
input.signal.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")), { once: true });
|
|
});
|
|
});
|
|
const fixture = owners({ runtimeSkillAssets });
|
|
let last;
|
|
for (let pass = 0; pass < 9 && !fixture.calls.includes("nativeControllers"); pass++) {
|
|
last = await runOnlineDurableDataMigration(db, { budgetMs: 10 }, fixture.owners);
|
|
}
|
|
expect(limits).toEqual([50, 25, 12, 6, 3, 1, 1]);
|
|
expect(fixture.calls).toContain("nativeControllers");
|
|
expect(last).toMatchObject({ state: "completed", deferred: expect.objectContaining({}) });
|
|
expect(fixture.record).toHaveBeenCalledWith(db, expect.objectContaining({ deferred: expect.objectContaining({ "runtime_skill_assets:stalled": 1 }) }));
|
|
});
|
|
|
|
it("defers an owner that fails and revisits earlier owners on the next cycle, so an abandoned claim cannot pin it", async () => {
|
|
let versionsFail = true;
|
|
const objectCalls: number[] = [];
|
|
const fixture = owners({
|
|
skillObjects: async () => { objectCalls.push(1); versionsFail = objectCalls.length < 2; return { inspected: 1, migrated: 1, busy: 0, after: null }; },
|
|
skillPackages: () => ({
|
|
migrateSnapshots: async () => ({ inspected: 0, migrated: 0, after: null }),
|
|
migrateVersions: async () => {
|
|
if (versionsFail) throw new Error("skill_package_not_durable");
|
|
return { inspected: 1, migrated: 1, after: null };
|
|
},
|
|
}),
|
|
});
|
|
const first = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(first).toMatchObject({ state: "completed", deferred: expect.objectContaining({ "skill_versions:skill_package_not_durable": 1 }) });
|
|
expect(fixture.calls).toContain("nativeControllers");
|
|
const second = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(objectCalls).toHaveLength(2);
|
|
expect(second).toMatchObject({ state: "completed", counts: expect.objectContaining({ skillVersions: 1 }) });
|
|
expect(second.deferred).not.toHaveProperty("skill_versions:skill_package_not_durable");
|
|
});
|
|
|
|
it("keeps a qualified owner code such as a missing runtime skill source", async () => {
|
|
const fixture = owners({ runtimeSkillAssets: async () => { throw new Error("runtime_skill_source_absent:missing"); } });
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result.deferred).toMatchObject({ "runtime_skill_assets:runtime_skill_source_absent:missing": 1 });
|
|
});
|
|
|
|
it("names each retained runtime skill run it could not save", async () => {
|
|
const fixture = owners({ runtimeSkillAssets: async () => ({ inspected: 3, after: null, results: [
|
|
{ runId: "r1", status: "preserved", preserved: 1 },
|
|
{ runId: "r2", status: "unsaved", code: "runtime_skill_source_absent:missing" },
|
|
{ runId: "r3", status: "unsaved", code: "runtime_skill_source_absent:missing" }] }) });
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result.counts.runtimeSkillRuns).toBe(1);
|
|
expect(result.deferred["runtime_skill_assets:runtime_skill_source_absent:missing"]).toBe(2);
|
|
});
|
|
|
|
it("keeps naming an unsaved runtime skill run after a later pass resumes past it", async () => {
|
|
let clock = 0;
|
|
const fixture = owners({
|
|
runtimeSkillAssets: async () => {
|
|
clock = 61_000;
|
|
return { inspected: 1, after: null, results: [{ runId: "r1", status: "unsaved", code: "runtime_skill_source_absent:missing" }] };
|
|
},
|
|
eligibility: vi.fn(async () => ({ eligible: false, blockers: ["runtime_skill_assets_require_migration"] })),
|
|
});
|
|
const first = await runOnlineDurableDataMigration(db, { budgetMs: 60_000, now: () => clock }, fixture.owners);
|
|
expect(first).toMatchObject({ state: "budget_exhausted", resumePhase: "run_logs" });
|
|
clock = 0;
|
|
const second = await runOnlineDurableDataMigration(db, { budgetMs: 60_000, now: () => clock }, fixture.owners);
|
|
expect(second.state).toBe("completed");
|
|
expect(second.deferred["runtime_skill_assets:runtime_skill_source_absent:missing"]).toBe(1);
|
|
expect(second.deferred["runtime_skill_assets:pending_inspection"]).toBeUndefined();
|
|
});
|
|
|
|
it("passes the page size to the skill object copy so a slow page can shrink", async () => {
|
|
const skillObjects = vi.fn(async () => ({ inspected: 0, migrated: 0, busy: 0, after: null }));
|
|
await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, owners({ skillObjects }).owners);
|
|
expect(skillObjects).toHaveBeenCalledWith(db, expect.objectContaining({ limit: 50, online: true }));
|
|
});
|
|
|
|
it("names an unexpected owner error without echoing its message", async () => {
|
|
const fixture = owners({ skillObjects: async () => { throw new Error("connect ECONNREFUSED 10.0.0.1:5432 secret"); } });
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result.deferred).toMatchObject({ "skill_objects:owner_failed": 1 });
|
|
expect(JSON.stringify(result)).not.toContain("secret");
|
|
});
|
|
|
|
it("runs one pass at a time and shares it with a concurrent caller", async () => {
|
|
let release!: () => void;
|
|
const gate = new Promise<void>(resolve => { release = resolve; });
|
|
const agentFiles = vi.fn(async () => { await gate; return { results: [], next: null }; });
|
|
const fixture = owners({ agentFiles });
|
|
const first = runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
const second = runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
release();
|
|
expect(await second).toBe(await first);
|
|
expect(agentFiles).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
describe("run evidence", () => {
|
|
it("moves log and trace pages before checking final eligibility and reports only current blockers", async () => {
|
|
let moved = false;
|
|
const runEvidencePage = vi.fn(async (_db: Db, input: { kinds: string[]; afterRunId?: string }) => {
|
|
if (input.kinds[0] === "trace") return { items: [{ kind: "trace", id: "t1", state: "migrated" }] };
|
|
if (input.afterRunId) { moved = true; return { items: [] }; }
|
|
return { items: [{ kind: "log", id: "r1", state: "migrated" }], nextRunId: "r1" };
|
|
});
|
|
const fixture = owners({ runEvidencePage, eligibility: async () => ({ eligible: moved, blockers: moved ? [] : ["run_logs_require_migration"] }) });
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result).toMatchObject({ eligible: true, blockers: [], counts: { runLogs: 1, providerTraces: 1 } });
|
|
expect(runEvidencePage).toHaveBeenNthCalledWith(2, db, expect.objectContaining({ afterRunId: "r1", kinds: ["log"], limit: 1 }));
|
|
expect(runEvidencePage).toHaveBeenNthCalledWith(3, db, expect.objectContaining({ kinds: ["trace"], signal: expect.any(AbortSignal) }));
|
|
});
|
|
|
|
it("retains active-source and owner-busy blockers without hiding them behind a completed pass", async () => {
|
|
const fixture = owners({ runEvidencePage: async (_db: Db, input: { kinds: string[] }) => input.kinds[0] === "trace"
|
|
? { items: [], busy: true }
|
|
: { items: [{ kind: "log", id: "active", state: "blocked", reason: "legacy_writer_not_settled" },
|
|
{ kind: "log", id: "unknown", state: "blocked", reason: "unsafe /private/source secret" }] } });
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result.eligible).toBe(false);
|
|
expect(result.deferred).toMatchObject({ "run_logs:legacy_writer_not_settled": 1, "run_logs:migration_failed": 1, "provider_traces:owner_busy": 1 });
|
|
expect(JSON.stringify(result)).not.toContain("secret");
|
|
});
|
|
|
|
it("resumes log paging from the saved cursor when the shared pass budget is spent", async () => {
|
|
let clock = 0;
|
|
const runEvidencePage = vi.fn(async (_db: Db, input: { kinds: string[]; afterRunId?: string }) => {
|
|
if (input.kinds[0] === "log" && !input.afterRunId) {
|
|
clock = 10;
|
|
return { items: [{ kind: "log", id: "r1", state: "migrated" }], nextRunId: "r1" };
|
|
}
|
|
return { items: [] };
|
|
});
|
|
const fixture = owners({ runEvidencePage });
|
|
expect(await runOnlineDurableDataMigration(db, { budgetMs: 10, now: () => clock }, fixture.owners))
|
|
.toMatchObject({ state: "budget_exhausted", resumePhase: "run_logs", counts: { runLogs: 1 } });
|
|
expect(await runOnlineDurableDataMigration(db, { budgetMs: 10, now: () => clock }, fixture.owners))
|
|
.toMatchObject({ state: "completed" });
|
|
expect(runEvidencePage).toHaveBeenNthCalledWith(2, db, expect.objectContaining({ kinds: ["log"], afterRunId: "r1" }));
|
|
});
|
|
});
|
|
|
|
describe("workspaces", () => {
|
|
const workspace = (n: number) => ({ companyId: "c1", workspaceId: `w${n}`, cursor: `20:w${n}` });
|
|
|
|
it("reconciles stale workspace operations per company before migrating workspaces, then runs the native owners", async () => {
|
|
const workspaceOperations = vi.fn(async (_db: Db, input: { companyId: string }) => {
|
|
fixture.calls.push(`reconcile:${input.companyId}`);
|
|
return { reconciled: input.companyId === "c1" ? 2 : 0, held: [{ reason: "finalization_owner_busy" }] };
|
|
});
|
|
const fixture = owners({
|
|
workspaceCompanies: vi.fn(async () => { fixture.calls.push("workspaceCompanies"); return ["c1", "c2"]; }),
|
|
workspaceOperations,
|
|
localWorkspaces: vi.fn(async () => { fixture.calls.push("localWorkspaces"); return [workspace(1)]; }),
|
|
migrateWorkspace: vi.fn(async () => { fixture.calls.push("migrateWorkspace"); return { alreadyMigrated: true, reasons: [] }; }),
|
|
});
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(fixture.calls.slice(fixture.calls.indexOf("runtimeSkillAssets"))).toEqual(["runtimeSkillAssets", "runLogs", "providerTraces", "workspaceCompanies",
|
|
"reconcile:c1", "reconcile:c2", "localWorkspaces", "inventoryWorkspace", "migrateWorkspace", "nativeHarnessBackups", "nativeControllers"]);
|
|
expect(workspaceOperations).toHaveBeenCalledWith(db, { companyId: "c1", signal: expect.any(AbortSignal) });
|
|
expect(result.counts).toMatchObject({ workspaceOperations: 2, workspaces: 1 });
|
|
expect(result.deferred).toMatchObject({ "workspace_operations:finalization_owner_busy": 2 });
|
|
});
|
|
|
|
it("names the workspace codes from an earlier pass when the pass that finishes the round does not reach workspaces", async () => {
|
|
let clock = 0;
|
|
const fixture = owners({
|
|
localWorkspaces: async () => { clock = 60_000; return [workspace(1), workspace(2)]; },
|
|
inventoryWorkspace: async (_db: Db, input: { workspaceId: string }) => ({ reasons: [input.workspaceId === "w1" ? "active_execution" : "task_binding_required"] }),
|
|
eligibility: async () => ({ eligible: false, blockers: ["workspaces_require_migration"] }),
|
|
});
|
|
const run = () => runOnlineDurableDataMigration(db, { budgetMs: 60_000, now: () => clock }, fixture.owners);
|
|
expect(await run()).toMatchObject({ state: "budget_exhausted", resumePhase: "native_harness_backups",
|
|
deferred: { "workspaces:active_execution": 1, "workspaces:task_binding_required": 1 } });
|
|
clock = 0;
|
|
const result = await run();
|
|
expect(result).toMatchObject({ state: "completed", blockers: ["workspaces_require_migration"] });
|
|
expect(result.deferred).toMatchObject({ "workspaces:active_execution": 1, "workspaces:task_binding_required": 1 });
|
|
});
|
|
|
|
it("names a workspace blocker that no inspected workspace explains", async () => {
|
|
const fixture = owners({ eligibility: async () => ({ eligible: false, blockers: ["workspaces_require_migration"] }) });
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result.deferred).toMatchObject({ "workspaces:pending_inspection": 1 });
|
|
});
|
|
|
|
it("names every durable-data blocker that readiness can report", async () => {
|
|
const source = await fs.readFile(new URL("./ephemeral-server-contract.ts", import.meta.url), "utf8");
|
|
const blockers = [...new Set([...source.matchAll(/blockers\.push\("([a-z_]+)"\)/g)].map(match => match[1]!)), RUNTIME_SKILL_ASSETS_BLOCKER];
|
|
expect(blockers).toEqual(expect.arrayContaining(["workspaces_require_migration", "local_instruction_copies_require_settlement", "external_agent_instructions"]));
|
|
for (const blocker of blockers) {
|
|
resetOnlineDurableDataMigrationProgressForTests();
|
|
const fixture = owners({ eligibility: async () => ({ eligible: false, blockers: [blocker] }),
|
|
agentFiles: async () => ({ results: [], next: null }),
|
|
skillObjects: async () => ({ inspected: 0, migrated: 0, busy: 0, after: null }),
|
|
skillInstallations: () => ({ installations: async () => ({ inspected: 0, after: null, results: [] }) }),
|
|
nativeHarnessBackups: async () => ({ inspected: 0, published: 0, current: 0, deferred: {} }) });
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(Object.keys(result.deferred), blocker).toHaveLength(1);
|
|
}
|
|
});
|
|
|
|
it("names a local instruction copy that still needs settlement after the agent files are durable", async () => {
|
|
const fixture = owners({ agentFiles: async () => ({ results: [{ agentId: "a1", state: "durable" }], next: null }),
|
|
eligibility: async () => ({ eligible: false, blockers: ["local_instruction_copies_require_settlement"] }) });
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result.deferred).toEqual({ "skill_objects:owner_busy": 1, "skills:linked_local": 1, "native_harness:source_active": 2,
|
|
"agent_files:instruction_copy_unsettled": 1 });
|
|
});
|
|
|
|
it("drops codes from an earlier round once the workspaces are inspected again", async () => {
|
|
let blocked = true;
|
|
const fixture = owners({
|
|
localWorkspaces: async () => [workspace(1)],
|
|
inventoryWorkspace: async () => ({ reasons: blocked ? ["active_execution"] : [] }),
|
|
eligibility: async () => ({ eligible: false, blockers: ["workspaces_require_migration"] }),
|
|
});
|
|
await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
blocked = false;
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result.deferred).not.toHaveProperty("workspaces:active_execution");
|
|
expect(result.deferred).toMatchObject({ "workspaces:pending_inspection": 1 });
|
|
});
|
|
|
|
it("migrates an idle workspace through the online owner with a bounded source-lock wait, never the offline mode", async () => {
|
|
const migrateWorkspace = vi.fn(async () => ({ alreadyMigrated: true, reasons: [] }));
|
|
const fixture = owners({ localWorkspaces: async () => [workspace(1)], migrateWorkspace });
|
|
await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(migrateWorkspace).toHaveBeenCalledWith({ db, storage: s3, companyId: "c1", workspaceId: "w1", apply: true,
|
|
signal: expect.any(AbortSignal), sourceLockWaitMs: 1_000 });
|
|
expect(migrateWorkspace.mock.calls[0]).not.toEqual([expect.objectContaining({ writersStopped: expect.anything() })]);
|
|
});
|
|
|
|
it("defers busy and unsupported workspaces with their owner reasons and does not force them", async () => {
|
|
const reasons: Record<string, string[]> = { w1: ["active_execution"], w2: ["task_binding_required", "workspace_provider_unsupported"], w3: [] };
|
|
const migrateWorkspace = vi.fn(async () => ({ alreadyMigrated: true, reasons: [] }));
|
|
const fixture = owners({
|
|
localWorkspaces: async () => [workspace(1), workspace(2), workspace(3)],
|
|
inventoryWorkspace: async (_db: Db, input: { workspaceId: string }) => ({ reasons: reasons[input.workspaceId] }),
|
|
migrateWorkspace,
|
|
});
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(migrateWorkspace).toHaveBeenCalledOnce();
|
|
expect(migrateWorkspace).toHaveBeenCalledWith(expect.objectContaining({ workspaceId: "w3" }));
|
|
expect(result.counts).toMatchObject({ workspaces: 1 });
|
|
expect(result.deferred).toMatchObject({ "workspaces:active_execution": 1, "workspaces:task_binding_required": 1,
|
|
"workspaces:workspace_provider_unsupported": 1 });
|
|
});
|
|
|
|
it("defers a workspace that the owner refuses under its locks and continues with the rest of the page", async () => {
|
|
const failures: Record<string, Error> = {
|
|
w1: new Error("remote_workspace_migration_blocked:active_execution,workspace_runtime_owned"),
|
|
w2: new Error("workspace_source_busy"),
|
|
w3: new Error("EACCES: permission denied, open '/private/path'"),
|
|
};
|
|
const fixture = owners({
|
|
localWorkspaces: async (_db: Db, input: { after?: string }) => input.after ? [] : [workspace(1), workspace(2), workspace(3), workspace(4)],
|
|
migrateWorkspace: async (input: { workspaceId: string }) => {
|
|
if (failures[input.workspaceId]) throw failures[input.workspaceId];
|
|
return { alreadyMigrated: true, reasons: [] };
|
|
},
|
|
});
|
|
const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(result.counts).toMatchObject({ workspaces: 1 });
|
|
expect(result.deferred).toMatchObject({ "workspaces:active_execution": 1, "workspaces:workspace_runtime_owned": 1,
|
|
"workspaces:workspace_source_busy": 1, "workspaces:owner_failed": 1 });
|
|
expect(JSON.stringify(result)).not.toContain("private");
|
|
});
|
|
|
|
it("pages workspaces with the owner cursor and resumes the same page after the budget ends", async () => {
|
|
let clock = 0;
|
|
const pages: Array<{ after?: string; limit: number }> = [];
|
|
const localWorkspaces = vi.fn(async (_db: Db, input: { after?: string; limit: number }) => {
|
|
pages.push(input);
|
|
const start = input.after ? Number(input.after.split(":w")[1]) : 0;
|
|
return start >= 8 ? [] : Array.from({ length: input.limit }, (_, index) => workspace(start + index + 1));
|
|
});
|
|
const migrateWorkspace = vi.fn(async () => { clock += 10_000; return { alreadyMigrated: true, reasons: [] }; });
|
|
const fixture = owners({ localWorkspaces, migrateWorkspace });
|
|
const run = () => runOnlineDurableDataMigration(db, { budgetMs: 60_000, now: () => clock }, fixture.owners);
|
|
expect(await run()).toMatchObject({ state: "budget_exhausted", resumePhase: "workspaces", counts: expect.objectContaining({ workspaces: 8 }) });
|
|
expect(await run()).toMatchObject({ state: "completed" });
|
|
expect(pages).toEqual([{ limit: 4, after: undefined }, { limit: 4, after: "20:w4" }, { limit: 4, after: "20:w8" }]);
|
|
expect(fixture.calls.filter(call => call === "agentFiles")).toHaveLength(1);
|
|
});
|
|
|
|
it("halves a workspace page that overruns the budget and keeps the workspaces it already moved", async () => {
|
|
const limits: number[] = [];
|
|
const localWorkspaces = vi.fn(async (_db: Db, input: { limit: number }) => {
|
|
limits.push(input.limit);
|
|
return Array.from({ length: input.limit }, (_, index) => workspace(index + 1));
|
|
});
|
|
const migrateWorkspace = vi.fn(async (input: { workspaceId: string; signal: AbortSignal }) => {
|
|
if (input.workspaceId === "w1") return { alreadyMigrated: true, reasons: [] };
|
|
return new Promise<never>((_resolve, reject) => {
|
|
input.signal.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")), { once: true });
|
|
});
|
|
});
|
|
const fixture = owners({ localWorkspaces, migrateWorkspace });
|
|
const first = await runOnlineDurableDataMigration(db, { budgetMs: 20 }, fixture.owners);
|
|
expect(first).toMatchObject({ state: "budget_exhausted", resumePhase: "workspaces", counts: expect.objectContaining({ workspaces: 1 }) });
|
|
expect(first.deferred).not.toHaveProperty("workspaces:owner_failed");
|
|
await runOnlineDurableDataMigration(db, { budgetMs: 20 }, fixture.owners);
|
|
expect(limits).toEqual([4, 2]);
|
|
});
|
|
|
|
it("returns at the budget while the owner waits on a lock, and skips workspaces until that owner settles", async () => {
|
|
let release!: () => void;
|
|
const held = new Promise<void>(resolve => { release = resolve; });
|
|
const migrateWorkspace = vi.fn(async () => { await held; return { alreadyMigrated: true, reasons: [] }; });
|
|
const fixture = owners({ localWorkspaces: async (_db: Db, input: { after?: string }) => input.after ? [] : [workspace(1)], migrateWorkspace });
|
|
expect(await runOnlineDurableDataMigration(db, { budgetMs: 20 }, fixture.owners)).toMatchObject({ state: "budget_exhausted", resumePhase: "workspaces" });
|
|
const busy = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners);
|
|
expect(busy).toMatchObject({ state: "completed", deferred: expect.objectContaining({ "workspaces:owner_busy": 1 }) });
|
|
expect(migrateWorkspace).toHaveBeenCalledOnce();
|
|
release();
|
|
await held;
|
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
expect(await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners)).toMatchObject({ counts: expect.objectContaining({ workspaces: 1 }) });
|
|
});
|
|
});
|
|
});
|