mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-25 16:14:26 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Hosted stacks move from a disk volume to volume-free storage, and
the durable-data migration must move every skill installation first
> - A remote skill (GitHub, skills.sh or URL) with no local files, no
complete saved version and an unreachable source blocks the move with
`skills:remote_source_unavailable`
> - Both the online and the offline migration refuse it today, so real
stacks stay on volumes forever
> - This pull request settles that case the same way in both paths:
rebuild the skill from its saved instructions, or keep it present but
unavailable until it is reinstalled
> - The benefit is that the move can finish without deleting any bytes
that still exist, and each outcome is logged
## Linked Issues or Issue Description
**What happened?**
A hosted stack could not move to volume-free storage. Two skill
installations had no local files, no complete saved version, and a
remote source that did not answer. The online and offline durable-data
migrations both reported `skills:remote_source_unavailable` and kept the
stack on its volume.
**Expected behavior**
Recovery works without manual repair. If the stored skill instructions
exist, the migration saves them as a new version and marks that version
as rebuilt. If no instructions are stored, the skill stays in the
library, is marked unavailable, is not offered to agents until it is
reinstalled, and does not block the move. The activity log records both
outcomes.
**Steps to reproduce**
1. Create a `github` skill with a pinned commit, an installed inventory
of `SKILL.md` and `reference.md`, and no saved version.
2. Make the remote source return 404.
3. Run the skill installation conversion. It reports `blocked` with code
`remote_source_unavailable`.
**Paperclip version or commit**
`master` at 8ce422e45d.
## What Changed
- `skillInstallationMaintenance().installations` takes a new
`settleUnavailableSources` option. The online and offline migrations
pass it. Direct calls keep the old blocking behavior, so a transient
remote failure outside a storage move never rebuilds a skill.
- Only a source that is gone counts as unavailable: a 404 or 410
response for an installed file. The capture path throws a typed
`RemoteSkillSourceUnavailableError` for these. Other failures (network
errors, 5xx, 429, unpinned revision, blocked scripts, package limits)
keep the `remote_source_unavailable` block.
- When the source is gone and the option is set, the migration rebuilds
the files from the saved incomplete version (if any) plus the stored
`SKILL.md` instructions. It publishes them as a version labelled
"Rebuilt from saved instructions", records
`reconstructedFromStoredContent` (time and missing files) in the skill
metadata, and writes a `company.skill_reconstructed_from_stored_content`
activity entry in the same transaction as the new version. The rebuilt
version is always a new version, never a reuse of the old one.
- When there are no stored instructions either, the migration sets the
existing `missingSource` marker with reason `remote_source_unavailable`
and writes a `company.skill_source_unavailable` activity entry. The
update is conditional on the row being unchanged. Later passes report
`source_unavailable` without calling the remote again.
- Runtime resolution returns `missing` for a marked skill unless a run
explicitly pinned a saved version, which stays usable. Otherwise it
shows with a plain message: the files could not be recovered when
Paperclip moved the company to new storage, and the user can reinstall
the skill. A reinstall replaces the metadata, which clears the marker.
- Eligibility reports `source_unavailable` for a marked skill. The data
contract and the offline skill phases accept it. The version check does
not fail the marked skill's incomplete current version, and it reports
`source_unavailable` for that version when it is an empty or incomplete
database-only snapshot (its bytes are in the database, so nothing stays
on the volume). Package-backed versions keep every check.
- The online migration counts `skillInstallationsReconstructed` and
`skillInstallationsSourceUnavailable` instead of deferring them.
- Other skill blockers (unsafe local source, catalog origin missing,
saved version incomplete without a remote failure, and so on) keep their
existing codes.
## Verification
- Red/green: with the service files from `master`, the three first tests
fail. The three review follow-up tests (503 stays blocked, empty saved
version verifies, pinned version stays available) fail on the first
commit of this PR and pass now. With this change they pass.
- `cd server && npx vitest run
src/__tests__/managed-skill-content.test.ts
src/services/online-durable-data-migration.test.ts` passes (embedded
PostgreSQL, real files, fetch mocked to 404).
- `npx vitest run src/__tests__/company-skills*.test.ts
src/__tests__/agent-runtime-skills.test.ts
src/__tests__/heartbeat-runtime-skills.test.ts`: all pass except two
`project workspace skill discovery` cases in `company-skills.test.ts`.
These fail identically on `master` in this macOS temp-dir checkout, so
they are not related to this change.
- `npx vitest run src/__tests__/ephemeral-server-data.test.ts` passes.
Its new test fails against the `master` data contract.
- `pnpm --filter @paperclipai/server typecheck` passes.
- `src/__tests__/ephemeral-migration.test.ts` needs an S3 endpoint and
was skipped locally. CI covers it.
## Risks
- A rebuilt skill keeps only the files that could be recovered. The
missing file names go to the metadata and the activity log. The old
version rows and the original remote source fields are kept, so a later
update or reinstall can fetch the exact files again.
- A 404 or 410 during a storage move is treated as final. The skill
stays recoverable through a reinstall if the source comes back. Network
errors and 5xx keep the block, so a stack with an outage at move time
waits for the next pass.
- No schema change.
- The marker guard and type now live in `company-skills.ts`, and the
maintenance module imports them.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used in Claude Code
with tool use (shell, file edits, test runs).
## 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>
190 lines
12 KiB
TypeScript
190 lines
12 KiB
TypeScript
import { and, eq, isNotNull, isNull, sql } from "drizzle-orm";
|
|
import { agentFileRoots, agents, executionWorkspaces, heartbeatRuns, providerTraceRecords, type Db } from "@paperclipai/db";
|
|
import type { Config } from "../config.js";
|
|
import { isValidLocalEncryptedMasterKey } from "../secrets/local-encrypted-provider.js";
|
|
import { agentInstructionsBundleMode } from "./agent-instructions.js";
|
|
import { skillInstallationMaintenance } from "./skill-installation-maintenance.js";
|
|
import { hasLocalNativeHarnessObligations } from "./native-runtime/native-harness-backup-migration.js";
|
|
import { findLocalNativeControllerObligation } from "./native-runtime/native-controller-migration.js";
|
|
import { hasUnsettledLocalInstructionCopies } from "./agent-instruction-copy-preservation.js";
|
|
import { hasLocalRuntimeSkillObligations, preserveLocalRuntimeSkillObligations } from "./native-runtime/runtime-skill-preservation.js";
|
|
import { localWorkspaceDependencyWhere } from "./local-workspace-dependencies.js";
|
|
|
|
import { ephemeralServerEnabled } from "./ephemeral-server-mode.js";
|
|
import { volumeFreeControllerStateOwned } from "./native-runtime/volume-free-controller-state.js";
|
|
import { logger } from "../middleware/logger.js";
|
|
|
|
export { ephemeralServerEnabled };
|
|
|
|
/** Validate before consumers can generate a replacement local identity. Error
|
|
* messages name settings only, never credential values or connection URLs. */
|
|
export function assertEphemeralServerConfig(config: Pick<Config, "databaseUrl" | "storageProvider">, env = process.env,
|
|
options: { allowMissingListener?: boolean } = {}): string | null {
|
|
if (env.PAPERCLIP_EPHEMERAL_SERVER !== "true") return null;
|
|
const refuse = (reason: string): never => { throw new Error(`Volume-free server configuration: ${reason}`); };
|
|
if (config.storageProvider !== "s3" || env.PAPERCLIP_DURABLE_AGENT_FILES !== "true" || env.PAPERCLIP_SHARED_UI_ASSETS !== "true") {
|
|
refuse("shared S3 storage, durable agent files and shared UI assets are required");
|
|
}
|
|
if (!env.PAPERCLIP_SECRETS_MASTER_KEY || !isValidLocalEncryptedMasterKey(env.PAPERCLIP_SECRETS_MASTER_KEY)) {
|
|
refuse("a stable PAPERCLIP_SECRETS_MASTER_KEY is required");
|
|
}
|
|
if ((env.PAPERCLIP_DECISION_SIGNING_SECRET?.trim().length ?? 0) < 32) {
|
|
refuse("a stable PAPERCLIP_DECISION_SIGNING_SECRET of at least 32 characters is required; preserve the existing decision key during migration");
|
|
}
|
|
let runtime: URL, listener: URL;
|
|
const waiveListener = options.allowMissingListener === true && !env.PAPERCLIP_DATABASE_LISTEN_URL?.trim();
|
|
const runtimeRequired = waiveListener ? "an external postgres DATABASE_URL is required"
|
|
: "external DATABASE_URL and PAPERCLIP_DATABASE_LISTEN_URL are required";
|
|
try { runtime = new URL(config.databaseUrl ?? ""); }
|
|
catch { return refuse(runtimeRequired); }
|
|
if (waiveListener) {
|
|
if (!["postgres:", "postgresql:"].includes(runtime.protocol)) refuse(runtimeRequired);
|
|
return null;
|
|
}
|
|
try { listener = new URL(env.PAPERCLIP_DATABASE_LISTEN_URL ?? ""); }
|
|
catch { return refuse("external DATABASE_URL and PAPERCLIP_DATABASE_LISTEN_URL are required"); }
|
|
if (![runtime, listener].every(url => ["postgres:", "postgresql:"].includes(url.protocol))
|
|
|| runtime.username !== listener.username || runtime.password !== listener.password || runtime.pathname !== listener.pathname
|
|
|| runtime.hostname.replace(/-pooler(?=\.)/, "") !== listener.hostname
|
|
|| /pooler|pgbouncer/i.test(listener.hostname)) {
|
|
refuse("PAPERCLIP_DATABASE_LISTEN_URL must be a direct connection to the same database using the runtime role");
|
|
}
|
|
return listener.href;
|
|
}
|
|
|
|
/** Findings that describe work in progress when a server stopped, not data
|
|
* waiting on a volume to be migrated. Every volume-free server that stops
|
|
* during a run leaves some of them behind. Runtime skill assets are not in
|
|
* this set: the server first copies the bundles still on this filesystem
|
|
* (settleRuntimeSkillObligations), and waives only the ones not here. */
|
|
const interruptedRunBlockers: ReadonlySet<string> = new Set([
|
|
"native_harness_backups_require_migration",
|
|
"local_instruction_copies_require_settlement",
|
|
]);
|
|
|
|
export const RUNTIME_SKILL_ASSETS_BLOCKER = "runtime_skill_assets_require_migration";
|
|
|
|
/** Runtime skill bundles admitted before Core saved them at admission
|
|
* (Nov 2026) can still be retained by old runs. Copy the ones this filesystem
|
|
* holds into durable storage now. A bundle whose files are not here cannot be
|
|
* saved by refusing to start: on a filesystem with the volume-free owner
|
|
* marker there is no volume to read it from, and a volume-free stack never
|
|
* gets its volume back. Refusing would keep the stack down on every restart
|
|
* (staging, 2026-11-18: twelve days of failed deploys, then an outage). */
|
|
async function settleRuntimeSkillObligations(db: Db, owned: () => Promise<boolean>) {
|
|
let outcome: Awaited<ReturnType<typeof preserveLocalRuntimeSkillObligations>>;
|
|
try { outcome = await preserveLocalRuntimeSkillObligations(db); }
|
|
catch (error) {
|
|
logger.warn({ err: error }, "Volume-free server could not save retained runtime skill bundles to durable storage");
|
|
return false;
|
|
}
|
|
if (outcome.bundles) logger.info({ bundles: outcome.bundles }, "Volume-free server saved retained runtime skill bundles to durable storage");
|
|
// The copy pass visited every obligation, so its result is the answer.
|
|
if (!outcome.unsaved.length) return true;
|
|
if (!await owned()) return false;
|
|
logger.warn({ blockers: [RUNTIME_SKILL_ASSETS_BLOCKER], runs: outcome.unsaved.length },
|
|
"Volume-free server is starting over runtime skill bundles that never reached durable storage; restart recovery owns those runs");
|
|
return true;
|
|
}
|
|
|
|
/** A readiness gate, not a periodic scan. Existing volume data is adopted by
|
|
* the owning maintenance commands before removal; the one copy it makes
|
|
* itself is of retained runtime skill bundles still on this filesystem. */
|
|
export async function assertEphemeralServerData(db: Db) {
|
|
if (!ephemeralServerEnabled()) return;
|
|
const eligibility = await readEphemeralServerDataEligibility(db, { purpose: "startup" });
|
|
const { controller } = eligibility;
|
|
let blockers = eligibility.blockers;
|
|
let ownedMarker: Promise<boolean> | undefined;
|
|
const owned = () => ownedMarker ??= volumeFreeControllerStateOwned();
|
|
if (blockers.includes(RUNTIME_SKILL_ASSETS_BLOCKER) && await settleRuntimeSkillObligations(db, owned)) {
|
|
blockers = blockers.filter(blocker => blocker !== RUNTIME_SKILL_ASSETS_BLOCKER);
|
|
}
|
|
// Run state that never reached durable storage needs migration only while
|
|
// it can still be on a volume. Once this filesystem carries the volume-free
|
|
// owner marker there is no volume: the state was written by a volume-free
|
|
// server, this one or a predecessor that stopped mid-run, and refusing to
|
|
// start cannot bring it back. It would only keep every restart from
|
|
// starting again. Restart recovery owns those runs; start and say so.
|
|
// Configuration that would write new data to the local disk stays refused.
|
|
const interrupted = blockers.filter(blocker => interruptedRunBlockers.has(blocker));
|
|
if (interrupted.length && await owned()) {
|
|
blockers = blockers.filter(blocker => !interruptedRunBlockers.has(blocker));
|
|
logger.warn({ blockers: interrupted },
|
|
"Volume-free server is starting over run state that never reached durable storage; restart recovery owns those runs");
|
|
}
|
|
if (!blockers.length) return;
|
|
const detail = (blocker: string) => blocker === "native_controllers_require_migration" && controller
|
|
? `${blocker} (run ${controller.runId} on task ${controller.issueId}: controller state at ${controller.source} is not proven by the database, and this filesystem has no volume-free owner marker, so it may be the only copy of that run's progress; adopt it with the native controller migration before starting a volume-free server)`
|
|
: blocker;
|
|
throw new Error(`Volume-free server data is not eligible: ${blockers.map(detail).join(", ")}`);
|
|
}
|
|
|
|
const failedCheckCode = (error: unknown) => {
|
|
const message = error instanceof Error ? error.message : "";
|
|
if (/^[a-z_]{1,80}$/.test(message)) return `:${message}`;
|
|
if (error instanceof SyntaxError) return ":invalid_source_json";
|
|
const errno = (error as { code?: unknown } | null)?.code;
|
|
return typeof errno === "string" && /^E[A-Z]{2,20}$/.test(errno) ? `:source_${errno.toLowerCase()}` : "";
|
|
};
|
|
|
|
export async function readEphemeralServerDataEligibility(db: Db, options: { purpose?: "migration" | "startup" } = {}) {
|
|
const blockers: string[] = [];
|
|
const check = async (area: string, blocker: string, inspect: () => Promise<boolean>) => {
|
|
try {
|
|
if (await inspect()) blockers.push(blocker);
|
|
} catch (error) {
|
|
logger.warn({ err: error, blocker }, "Volume-free data eligibility check failed");
|
|
blockers.push(`${area}_check_failed${failedCheckCode(error)}`);
|
|
}
|
|
};
|
|
await check("agent_files", "agent_files_require_migration", async () => {
|
|
const [missingFiles] = await db.select({ id: agents.id }).from(agents).leftJoin(agentFileRoots,
|
|
and(eq(agentFileRoots.companyId, agents.companyId), eq(agentFileRoots.agentId, agents.id)))
|
|
.where(isNull(agentFileRoots.agentId)).limit(1);
|
|
return Boolean(missingFiles);
|
|
});
|
|
await check("local_instruction_copies", "local_instruction_copies_require_settlement", () => hasUnsettledLocalInstructionCopies(db));
|
|
// Only inspect configuration here; filesystem reads must never adopt an
|
|
// empty directory on the replacement machine as the old server's content.
|
|
await check("agent_instructions", "external_agent_instructions", async () => {
|
|
const configured = await db.select({ id: agents.id, companyId: agents.companyId, name: agents.name, adapterConfig: agents.adapterConfig }).from(agents);
|
|
return configured.some(agent => agentInstructionsBundleMode(agent) === "external");
|
|
});
|
|
await check("workspaces", "workspaces_require_migration", async () => {
|
|
const [localWorkspace] = await db.select({ id: executionWorkspaces.id }).from(executionWorkspaces)
|
|
.where(localWorkspaceDependencyWhere(db)).limit(1);
|
|
return Boolean(localWorkspace);
|
|
});
|
|
await check("run_logs", "run_logs_require_migration", async () => {
|
|
const [localLog] = await db.select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(and(
|
|
eq(heartbeatRuns.logStore, "local_file"), isNotNull(heartbeatRuns.logRef),
|
|
)).limit(1);
|
|
return Boolean(localLog);
|
|
});
|
|
await check("provider_traces", "provider_traces_require_migration", async () => {
|
|
const [localTrace] = await db.select({ id: providerTraceRecords.id }).from(providerTraceRecords).where(and(
|
|
isNull(providerTraceRecords.deletedAt), sql`${providerTraceRecords.traceRef} not like 's3-segments-v1:%'`,
|
|
)).limit(1);
|
|
return Boolean(localTrace);
|
|
});
|
|
await check("native_harness_backups", "native_harness_backups_require_migration", () => hasLocalNativeHarnessObligations(db));
|
|
const found: { controller: Awaited<ReturnType<typeof findLocalNativeControllerObligation>> } = { controller: null };
|
|
await check("native_controllers", "native_controllers_require_migration", async () => {
|
|
found.controller = await findLocalNativeControllerObligation(db);
|
|
return Boolean(found.controller);
|
|
});
|
|
await check("runtime_skill_assets", RUNTIME_SKILL_ASSETS_BLOCKER, () => hasLocalRuntimeSkillObligations(db));
|
|
await check("skills", "skills_require_migration", async () => {
|
|
const skills = skillInstallationMaintenance(db);
|
|
let after: string | undefined;
|
|
for (;;) {
|
|
const page = await skills.eligibility({ after, limit: 50, purpose: options.purpose });
|
|
if (page.results.some(skill => skill.status !== "version_backed" && skill.status !== "source_unavailable")) return true;
|
|
if (page.inspected < 50 || !page.after) return false;
|
|
after = page.after;
|
|
}
|
|
});
|
|
return { eligible: blockers.length === 0, blockers, controller: found.controller };
|
|
}
|