mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-24 05:47:10 +01:00
fix(cloud): name every durable-data blocker in the online pass result (#22641)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - A hosted server moves its durable data to shared storage in bounded online passes while it serves work > - Each pass reports the remaining eligibility blockers and a code for each item it left behind > - The pass keeps its place between calls, but each result only lists the codes of the phases that this call visited > - A pass that finishes a round after an earlier call ran the workspace phase reports `workspaces_require_migration` with no workspace code > - This pull request carries each phase's codes from its latest visit, and names a blocker that no visit explains > - The benefit is that every data blocker in a pass result names a cause ## Linked Issues or Issue Description Refs #22270, #22294 **What happened?** A staging stack ran repeated online durable-data passes. The pass result reported `workspaces_require_migration`, but the `deferred` map had no `workspaces:` code. **Expected behavior** Every data blocker in the pass result names at least one code. **Steps to reproduce** 1. Run a pass whose budget ends right after the workspace phase. The result shows the workspace codes. 2. Run the next pass. It resumes after the workspace phase and completes the round. 3. See that the completed result reports `workspaces_require_migration` with no workspace code. ## What Changed - The pass keeps the codes of the latest visit of each phase. A new visit of a phase replaces its earlier codes. - When a data blocker has no code from this call, the result includes the codes from the latest visit of the matching phases. - When no visit explains a data blocker, the result names `<phase>:pending_inspection`. For example, a workspace created after the workspace phase ran shows `workspaces:pending_inspection`. - Instruction blockers also get a code: `local_instruction_copies_require_settlement` reports `agent_files:instruction_copy_unsettled` and `external_agent_instructions` reports `agent_instructions:external_instructions_configured` when no agent-file code explains them. - One table maps each phase to its code prefixes and each data blocker to its phases and fallback code. A test reads every blocker that readiness can push and checks that each one gets a code. ## Verification - `cd server && npx vitest run src/services/online-durable-data-migration.test.ts src/services/online-durable-data-migration-postgres.test.ts`: 31 passed. - Red/green: the three new tests fail on `origin/master` (3 failed, 24 passed) and pass with this change. - `pnpm verify` (see the PR comments for the result). ## Risks - Low risk. The change only adds keys to the `deferred` map of the pass result. It does not change what the pass moves or when a stack is eligible. - A carried code can describe a state from an earlier call in the same round. The next visit of that phase replaces it. > 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>
This commit is contained in:
1 parent
571b4bcae9
commit
5f5ac15436
2 files changed
+108
-12
No files matched your search
@@ -1,6 +1,8 @@
|
||||
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;
|
||||
@@ -283,6 +285,66 @@ describe("online durable-data migration pass", () => {
|
||||
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 });
|
||||
|
||||
@@ -90,6 +90,25 @@ let running: Promise<OnlineDurableDataMigrationResult> | null = null;
|
||||
let workspaceOwner: Promise<void> | null = null;
|
||||
type Progress = { phase: number; after?: string; limit?: number; timeouts?: number };
|
||||
let progress: Progress = { phase: 0 };
|
||||
let carried: Record<string, number> = {};
|
||||
const PHASE_CODES: Record<Phase, string[]> = { agent_files: ["agent_files"], skill_objects: ["skill_objects"],
|
||||
skill_snapshots: ["skill_snapshots"], skill_versions: ["skill_versions"], skill_installations: ["skill_installations", "skills"],
|
||||
runtime_skill_assets: ["runtime_skill_assets"], run_logs: ["run_logs"], provider_traces: ["provider_traces"],
|
||||
workspace_operations: ["workspace_operations"], workspaces: ["workspaces"],
|
||||
native_harness_backups: ["native_harness_backups", "native_harness"], native_controllers: ["native_controllers"] };
|
||||
const DATA_BLOCKER_CODES: Record<string, { phases: Phase[]; unexplained: string }> = {
|
||||
agent_files_require_migration: { phases: ["agent_files"], unexplained: "agent_files:pending_inspection" },
|
||||
local_instruction_copies_require_settlement: { phases: ["agent_files"], unexplained: "agent_files:instruction_copy_unsettled" },
|
||||
external_agent_instructions: { phases: [], unexplained: "agent_instructions:external_instructions_configured" },
|
||||
skills_require_migration: { phases: ["skill_objects", "skill_snapshots", "skill_versions", "skill_installations"], unexplained: "skills:pending_inspection" },
|
||||
runtime_skill_assets_require_migration: { phases: ["runtime_skill_assets"], unexplained: "runtime_skill_assets:pending_inspection" },
|
||||
run_logs_require_migration: { phases: ["run_logs"], unexplained: "run_logs:pending_inspection" },
|
||||
provider_traces_require_migration: { phases: ["provider_traces"], unexplained: "provider_traces:pending_inspection" },
|
||||
workspaces_require_migration: { phases: ["workspace_operations", "workspaces"], unexplained: "workspaces:pending_inspection" },
|
||||
native_harness_backups_require_migration: { phases: ["native_harness_backups"], unexplained: "native_harness:pending_inspection" },
|
||||
native_controllers_require_migration: { phases: ["native_controllers"], unexplained: "native_controllers:pending_inspection" },
|
||||
};
|
||||
const prefixOf = (key: string) => key.slice(0, key.indexOf(":"));
|
||||
const DEFAULT_LIMITS: Partial<Record<Phase, number>> = { agent_files: 5, skill_objects: 50, skill_snapshots: 50, skill_versions: 50,
|
||||
skill_installations: 50, runtime_skill_assets: 50, workspace_operations: 10, workspaces: 4 };
|
||||
const WORKSPACE_SOURCE_LOCK_WAIT_MS = 1_000;
|
||||
@@ -106,6 +125,7 @@ function workspaceDeferrals(error: unknown): string[] {
|
||||
|
||||
export function resetOnlineDurableDataMigrationProgressForTests() {
|
||||
progress = { phase: 0 };
|
||||
carried = {};
|
||||
workspaceOwner = null;
|
||||
}
|
||||
|
||||
@@ -134,8 +154,18 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number },
|
||||
const counts: Record<string, number> = {};
|
||||
const deferred: Record<string, number> = {};
|
||||
const add = (bucket: Record<string, number>, key: string, by = 1) => { if (by) bucket[key] = (bucket[key] ?? 0) + by; };
|
||||
const defer = (key: string, by = 1) => { add(deferred, key, by); add(carried, key, by); };
|
||||
const finish = async (state: OnlineDurableDataMigrationResult["state"], reason?: string): Promise<OnlineDurableDataMigrationResult> => {
|
||||
const { eligible, blockers } = await owners.eligibility(db);
|
||||
for (const blocker of blockers) {
|
||||
const codes = DATA_BLOCKER_CODES[blocker];
|
||||
if (!codes) continue;
|
||||
const prefixes = codes.phases.flatMap(phase => PHASE_CODES[phase]);
|
||||
if (Object.keys(deferred).some(key => prefixes.includes(prefixOf(key)))) continue;
|
||||
const earlier = Object.entries(carried).filter(([key]) => prefixes.includes(prefixOf(key)));
|
||||
for (const [key, count] of earlier) deferred[key] = count;
|
||||
if (!earlier.length) deferred[codes.unexplained] = 1;
|
||||
}
|
||||
const result: OnlineDurableDataMigrationResult = { state, ...(reason ? { reason } : {}),
|
||||
...(state === "budget_exhausted" ? { resumePhase: PHASES[progress.phase] } : {}), counts, deferred, eligible, blockers };
|
||||
await owners.record(db, result);
|
||||
@@ -157,14 +187,14 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number },
|
||||
const result = await owners.agentFiles(db, { apply: true, writersStopped: false, after, limit, signal });
|
||||
for (const row of result.results) {
|
||||
if (row.state === "durable") add(counts, "agentFiles");
|
||||
else add(deferred, `agent_files:${row.state}`);
|
||||
else defer(`agent_files:${row.state}`);
|
||||
}
|
||||
return result.next ?? null;
|
||||
}
|
||||
case "skill_objects": {
|
||||
const result = await owners.skillObjects(db, { source: localSkills, destination: storage, writersStopped: false, online: true, signal, after, limit });
|
||||
add(counts, "skillObjects", result.migrated);
|
||||
add(deferred, "skill_objects:owner_busy", result.busy);
|
||||
defer("skill_objects:owner_busy", result.busy);
|
||||
return result.inspected < limit || !result.after ? null : result.after;
|
||||
}
|
||||
case "skill_snapshots":
|
||||
@@ -178,7 +208,7 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number },
|
||||
const result = await skills.installations({ apply: true, writersStopped: false, after, limit, signal });
|
||||
for (const row of result.results) {
|
||||
if (row.status === "converted") add(counts, "skillInstallations");
|
||||
else if (row.status !== "unchanged") add(deferred, `skills:${row.status === "blocked" ? row.code ?? "blocked" : row.status}`);
|
||||
else if (row.status !== "unchanged") defer(`skills:${row.status === "blocked" ? row.code ?? "blocked" : row.status}`);
|
||||
}
|
||||
return result.inspected < limit || !result.after ? null : result.after;
|
||||
}
|
||||
@@ -194,12 +224,12 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number },
|
||||
kinds: [logs ? "log" : "trace"], limit, signal,
|
||||
...(logs ? { afterRunId: after } : { afterTraceId: after }),
|
||||
}), signal);
|
||||
if (result.busy) { add(deferred, `${phase}:owner_busy`); return null; }
|
||||
if (result.busy) { defer(`${phase}:owner_busy`); return null; }
|
||||
for (const item of result.items) {
|
||||
if (item.state === "migrated") add(counts, logs ? "runLogs" : "providerTraces");
|
||||
else if (item.state === "blocked") {
|
||||
const reason = item.reason && /^[a-z_]{1,60}$/.test(item.reason) ? item.reason : "migration_failed";
|
||||
add(deferred, `${phase}:${reason}`);
|
||||
defer(`${phase}:${reason}`);
|
||||
}
|
||||
}
|
||||
return (logs ? result.nextRunId : result.nextTraceId) ?? null;
|
||||
@@ -209,13 +239,13 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number },
|
||||
for (const companyId of companies) {
|
||||
const result = await owners.workspaceOperations(db, { companyId, signal });
|
||||
add(counts, "workspaceOperations", result.reconciled);
|
||||
for (const held of result.held) add(deferred, `workspace_operations:${held.reason}`);
|
||||
for (const held of result.held) defer(`workspace_operations:${held.reason}`);
|
||||
}
|
||||
return companies.length < limit ? null : companies.at(-1)!;
|
||||
}
|
||||
case "workspaces": {
|
||||
if (workspaceOwner) {
|
||||
add(deferred, "workspaces:owner_busy");
|
||||
defer("workspaces:owner_busy");
|
||||
return null;
|
||||
}
|
||||
const rows = await owners.localWorkspaces(db, { after, limit });
|
||||
@@ -237,21 +267,21 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number },
|
||||
reasons = workspaceDeferrals(error);
|
||||
}
|
||||
if (!reasons.length) add(counts, "workspaces");
|
||||
for (const reason of reasons) add(deferred, `workspaces:${reason}`);
|
||||
for (const reason of reasons) defer(`workspaces:${reason}`);
|
||||
}
|
||||
return rows.length < limit ? null : rows.at(-1)!.cursor;
|
||||
}
|
||||
case "native_harness_backups": {
|
||||
await owners.nativeHarnessBackups(db, { storage, signal, onOutcome: outcome => {
|
||||
if (outcome === "published") add(counts, "nativeHarnessBackups");
|
||||
else if (outcome !== "current") add(deferred, `native_harness:${outcome}`);
|
||||
else if (outcome !== "current") defer(`native_harness:${outcome}`);
|
||||
} });
|
||||
return null;
|
||||
}
|
||||
case "native_controllers": {
|
||||
await owners.nativeControllers(db, { storage, signal, onOutcome: outcome => {
|
||||
if (outcome === "imported") add(counts, "nativeControllers");
|
||||
else if (!["unchanged", "archived_failed", "source_already_absent"].includes(outcome)) add(deferred, `native_controllers:${outcome}`);
|
||||
else if (!["unchanged", "archived_failed", "source_already_absent"].includes(outcome)) defer(`native_controllers:${outcome}`);
|
||||
} });
|
||||
return null;
|
||||
}
|
||||
@@ -261,13 +291,17 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number },
|
||||
while (progress.phase < PHASES.length) {
|
||||
if (spent()) return await finish("budget_exhausted");
|
||||
const phase = PHASES[progress.phase]!;
|
||||
if (progress.after === undefined) {
|
||||
const prefixes = PHASE_CODES[phase];
|
||||
for (const key of Object.keys(carried)) if (prefixes.includes(prefixOf(key))) delete carried[key];
|
||||
}
|
||||
let next: string | null;
|
||||
try {
|
||||
next = await page(phase, progress.after, progress.limit ?? DEFAULT_LIMITS[phase] ?? 1);
|
||||
} catch (error) {
|
||||
if (signal.reason instanceof BudgetSpent) throw error;
|
||||
const code = error instanceof Error && /^[a-z_]{1,60}(?::[a-z_]{1,40})?$/.test(error.message) ? error.message : "owner_failed";
|
||||
add(deferred, `${phase}:${code}`);
|
||||
defer(`${phase}:${code}`);
|
||||
next = null;
|
||||
}
|
||||
progress = next ? { phase: progress.phase, after: next, limit: progress.limit } : { phase: progress.phase + 1 };
|
||||
@@ -281,7 +315,7 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number },
|
||||
const limit = progress.limit ?? DEFAULT_LIMITS[phase] ?? 1;
|
||||
if (limit > 1) progress = { ...progress, limit: Math.max(1, Math.floor(limit / 2)), timeouts: 0 };
|
||||
else if ((progress.timeouts ?? 0) + 1 >= 2) {
|
||||
add(deferred, `${phase}:stalled`);
|
||||
defer(`${phase}:stalled`);
|
||||
progress = { phase: progress.phase + 1 };
|
||||
} else progress = { ...progress, timeouts: (progress.timeouts ?? 0) + 1 };
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user