diff --git a/server/scripts/migrate-skill-packages.ts b/server/scripts/migrate-skill-packages.ts index 7f761cf5afc..8d30219357b 100644 --- a/server/scripts/migrate-skill-packages.ts +++ b/server/scripts/migrate-skill-packages.ts @@ -38,4 +38,6 @@ try { const result = await action({ apply, writersStopped: process.argv.includes("--writers-stopped"), clearLegacy: process.argv.includes("--clear-legacy"), limit, after: value("--after"), companyId: value("--company") }); console.log(JSON.stringify({ phase, apply, ...result })); + if (phase === "runtime-assets" && (result as Awaited>).results + .some(row => row.status === "unsaved")) process.exitCode = 1; } finally { await db.$client.end({ timeout: 1 }); } diff --git a/server/src/__tests__/runtime-skill-preservation.test.ts b/server/src/__tests__/runtime-skill-preservation.test.ts index 2539879ce35..08d2eccb01b 100644 --- a/server/src/__tests__/runtime-skill-preservation.test.ts +++ b/server/src/__tests__/runtime-skill-preservation.test.ts @@ -51,13 +51,13 @@ describe("admitted unversioned runtime skill preservation", () => { await database?.cleanup(); if (home) await removeRuntimeAssetTree(home); }); - async function fixture() { + async function fixture(skill = "# original admitted skill\n") { const companyId = randomUUID(), agentId = randomUUID(), runId = randomUUID(), issueId = randomUUID(); await db.insert(companies).values({ id: companyId, name: "Skill preservation", issuePrefix: `R${companyId.slice(0, 6)}` }); await db.insert(agents).values({ id: agentId, companyId, name: "Recovery source" }); await db.insert(issues).values({ id: issueId, companyId, title: "Preserve admitted skill" }); const bundle = await materializeAsset([ - { path: "SKILL.md", content: Buffer.from("# original admitted skill\n"), mode: 0o400 }, + { path: "SKILL.md", content: Buffer.from(skill), mode: 0o400 }, { path: "scripts/run.sh", content: Buffer.from("#!/bin/sh\necho preserve\n"), mode: 0o500 }, { path: "binary", content: Buffer.from([0, 255, 7]), mode: 0o444 }, ]); @@ -187,6 +187,20 @@ describe("admitted unversioned runtime skill preservation", () => { await removeRuntimeAssetTree(f.bundle.rootPath); expect(await preserveRuntimeSkillAssets(db, f, storage)).toEqual({ preserved: 1 }); }); + it("names a retained run whose bundle left this disk and still saves the runs after it", async () => { + const lost = await fixture("# lost admitted skill\n"), kept = await fixture("# kept admitted skill\n"); + await removeRuntimeAssetTree(lost.bundle.rootPath); + const page = await migrateRuntimeSkillAssets(db, { apply: true }, storage); + expect([...page.results].sort((a, b) => a.runId.localeCompare(b.runId))).toEqual([ + { runId: lost.runId, status: "unsaved", code: "runtime_skill_source_absent:missing" }, + { runId: kept.runId, status: "preserved", preserved: 1 }, + ].sort((a, b) => a.runId.localeCompare(b.runId))); + expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, kept.runId)))[0]!.runnerProfileJson) + .toHaveProperty("nativeRuntimeSkillPreservation"); + expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, lost.runId)))[0]!.runnerProfileJson) + .not.toHaveProperty("nativeRuntimeSkillPreservation"); + expect(await hasLocalRuntimeSkillObligations(db)).toBe(true); + }); it("retains the original receipt through a contract revision but rejects a changed admitted asset or binding", async () => { const f = await fixture(); await preserveRuntimeSkillAssets(db, f, storage); diff --git a/server/src/services/native-runtime/runtime-skill-preservation.ts b/server/src/services/native-runtime/runtime-skill-preservation.ts index df8ad79216c..066f2ed14c5 100644 --- a/server/src/services/native-runtime/runtime-skill-preservation.ts +++ b/server/src/services/native-runtime/runtime-skill-preservation.ts @@ -268,13 +268,19 @@ export async function migrateRuntimeSkillAssets(db: Db, options: { companyId?: s .where(and(retainedRuntimeSkillsSql(), options.companyId ? eq(heartbeatRuns.companyId, options.companyId) : undefined, options.after ? gt(heartbeatRuns.id, options.after) : undefined)).orderBy(asc(heartbeatRuns.id)) .limit(Math.max(1, Math.min(50, options.limit ?? 10))); - const results: Array<{ runId: string; status: "candidate" | "preserved"; preserved?: number }> = []; + const results: Array<{ runId: string; status: "candidate" } | { runId: string; status: "preserved"; preserved: number } + | { runId: string; status: "unsaved"; code: `runtime_skill_source_absent:${RuntimeSkillSourceAbsent["reason"]}` }> = []; await withSkillPackageReadScope(db, async () => { for (const row of rows) { options.signal?.throwIfAborted(); if (!options.apply) { results.push({ runId: row.id, status: "candidate" }); continue; } - const result = await preserveRuntimeSkillAssets(db, { companyId: row.companyId, runId: row.id, signal: options.signal }, storage); - results.push({ runId: row.id, status: "preserved", preserved: result.preserved }); + try { + const result = await preserveRuntimeSkillAssets(db, { companyId: row.companyId, runId: row.id, signal: options.signal }, storage); + results.push({ runId: row.id, status: "preserved", preserved: result.preserved }); + } catch (error) { + if (!(error instanceof RuntimeSkillSourceAbsent)) throw error; + results.push({ runId: row.id, status: "unsaved", code: `runtime_skill_source_absent:${error.reason}` }); + } } }); return { inspected: rows.length, after: rows.at(-1)?.id ?? null, results }; diff --git a/server/src/services/online-durable-data-migration.test.ts b/server/src/services/online-durable-data-migration.test.ts index 83da5636465..0c5bff3f285 100644 --- a/server/src/services/online-durable-data-migration.test.ts +++ b/server/src/services/online-durable-data-migration.test.ts @@ -194,6 +194,34 @@ describe("online durable-data migration pass", () => { 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); diff --git a/server/src/services/online-durable-data-migration.ts b/server/src/services/online-durable-data-migration.ts index d8baec60846..f9aa3d5ead2 100644 --- a/server/src/services/online-durable-data-migration.ts +++ b/server/src/services/online-durable-data-migration.ts @@ -214,7 +214,10 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number }, } case "runtime_skill_assets": { const result = await owners.runtimeSkillAssets(db, { apply: true, after, limit, signal }, storage); - add(counts, "runtimeSkillRuns", result.results.filter(row => row.status === "preserved").length); + for (const row of result.results) { + if (row.status === "preserved") add(counts, "runtimeSkillRuns"); + else if (row.status === "unsaved") defer(`runtime_skill_assets:${row.code}`); + } return result.inspected < limit || !result.after ? null : result.after; } case "run_logs":