mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-12 14:19:00 +01:00
## Thinking Path > - Paperclip preserves the work produced by its agents. > - Remote workspace migration must preserve legacy Git history and local edits. > - Linked checkouts can share administration and objects with other tasks. > - A task lock alone cannot exclude writers of a shared or borrowed store. > - The online source owner must derive and retain the full physical ownership closure. > - This change reuses task, project, lifecycle and finalizer locks for that closure. > - Idle legacy Git work can then migrate while unrelated remote turns continue. ## Linked Issues or Issue Description Refs: #19642. **What happened?** Online migration keeps legacy Git sources blocked because several task checkouts can share a project base or a private Git cache. A read-only source observation found 160 pending Git records, 120 distinct paths and nine common stores. Its largest project cohort contained 126 records and 110 task owners. An ordinary-directory fence cannot preserve these sources by itself. **Expected behavior** Derive the physical source owners from stable Git metadata. Hold their existing source and lifecycle keys through publication. Retain all history and recovery obligations. Reject foreign aliases, unknown stores and changed sources. Let unrelated object-backed work continue. **Steps to reproduce** 1. Create a managed project checkout and linked task worktrees. 2. Keep a settled prepared recovery source on a sibling task. 3. Keep unrelated remote work running. 4. Request an online import of an idle leaf checkout. 5. The existing owner keeps the shared source blocked. **Paperclip version or commit** This branch builds on merged #19642 (`7ac99718a4`) and fresh master `fb3cf1a072`. It adds the Git closure. It does not add a writers-stopped shortcut. **Deployment mode** Hosted native Runner with remote workspace storage. Local qualification uses owned synthetic Git repositories, disposable PostgreSQL databases and the actual archive and storage owners. ## What Changed - Read bounded stable Git pointers, common-directory metadata and local alternates. Derive the complete physical source ownership cohort. Preserve all object stores. - Retain common and borrowed store owners in preparation, lifecycle, runtime, expiration and actual reopen paths. Keep company boundaries and the existing 128-key bound. - Hold exact sibling native recovery proofs under their existing physical finalizer keys. Recheck descriptors, allocations, recovery actions and source identities before capture and publication. Preserve original states and warnings. - Require leaf-first nested imports. Reuse the existing immutable child download, restore and source comparison before parent exclusion. - Bind the self-contained archive owner to the held Git namespaces. Keep its three walks, history normalization, fsck, bytes and digest format. Reject bare, detached, network, unknown or changed Git storage. - Check both foreign path fields. Keep foreign metadata private. Document the proof, bounds, source observation and remaining eligibility limits. ## Verification - Final revision `2934e41c923b93e16508b978c849b747c86be645`: all checks are terminal, with 56 successful and four skipped, zero failures and zero pending. The [required CI aggregate](https://github.com/paperclipai/paperclip/actions/runs/42326464408/job/128266279210) passed. Build, types, all server/Runner/serialized suites, end-to-end aggregate and Snyk passed. [Fresh exact-head Greptile review](https://github.com/paperclipai/paperclip/pull/19688#issuecomment-6699760104) is 5/5 with no open threads. GitHub reports MERGEABLE and CLEAN. - Review then found two correctness gaps: an ordinary managed directory inside an unrelated host repository was blocked, and a Git parent could miss a completed nested operation after its child snapshot became missing or deleted. Three actual-owner regressions reproduced both gaps in 9.70 seconds. The corrected owner passes 12 focused cases in 17.08 seconds. It imports and restores only the ordinary root bytes, preserves the outer repository, rejects both missing/deleted child receipts before a parent journal, and retains valid/changed child coverage and private-cache contention. Its wrapper exited 0 and cleanup completed. The first bootstrap attempt failed before tests ran because the host exhausted its 32 shared-memory segment limit; no shared services were stopped and no pass is claimed for that attempt. The final qualified source/test type graph, module boundaries and diff checks pass. - Receipt scans keep canonical path resolution. A lexical SQL prefix can omit children reached through symlinked ancestors. The observed full metadata closure completed in 1.17 seconds; no measured bottleneck justifies weakening that alias proof. Writer admission still rejects uncertain Git ownership before mutation. Expiration returns its typed skip, and reopen returns its structured failure. Runtime and lifecycle do not bypass an unknown source. No broad error-contract change was added. - Full CI at `720104ea975e` exposed five existing boundary cases across three server shards. Four exact cases reproduced locally as failures in 25.98 seconds. Reopen now preserves its structured failure when source inspection rejects the path, without reconstructing it. Runtime recognizes only the exact regular ignore-only `.git/info/exclude` fragment; migration still rejects that fragment. The original arbitrary-path and file-URL import/delete-host/resume fixtures keep both supported input forms and use the genuine offline setup path after their sole writer finishes. The production mode gate is unchanged. A new real-owner control proves the offline flag still rejects a running legacy database writer. - The final focused run passed all ten cases across four suites in 22.75 seconds, including all five CI failures, invalid-pointer admission, fragment rejection, offline writer exclusion and actual nested reopen lock ordering. Its wrapper exited 0 and cleanup completed. The known invalid-pointer case remains fail-closed; the separate unexpected-I/O diagnostic retains safe error metadata without command text or credentials. Existing Git stderr redaction coverage remains intact. The final metadata reader suite passed 20 cases in 2.68 seconds. The qualified server/test source type graph, module boundaries and diff checks pass. No production assertions or timeout defaults were weakened. Fresh CI and review are required for the repair revision. - A later review found that the ordinary early return still used only one foreign path field. The actual ordinary providerRef-only regression failed as incorrectly eligible. One source-path owner now enumerates both local locators for company/foreign ordinary and Git scans, layout checks, early admission, lifecycle/reopen/runtime and expiration/move/preparation retention. Capture and display keep their original preferred path. The actual owner canary passed 49 cases across two suites in 109.15 seconds. It covers cwd-only/ref-only/different/symlink locators, same-company mismatches, foreign owners, lifecycle ordering, Git/recovery imports and expiration/reopen. Three selected-locator fallback cases also failed before their guard; the final eight-case control passed in 31.05 seconds, including the configured alias and valid import/private-child controls. All wrappers exited 0 and completed cleanup. The metadata/locator suite passed 19 cases. The final source/test type graph, module boundaries and diff checks pass. - Review found two P1 gaps. Both configured-directory symlink cases failed before canonicalization. The actual parent import lost an already-imported child's unique private cache and failed its archive fence instead of retaining that owner. The corrected real-owner canary passed four cases in 20.78 seconds: configured symlink runtime provisioning, private-cache contention before the parent journal, successful parent publication after release, and unchanged/changed child coverage. The metadata suite passed 15 cases in 4.14 seconds. Both wrappers exited 0 and cleanup completed. - A fresh source read-only observation found 160 pending Git records and nine common stores. An additional exact receipt query found 1,243 completed migration receipts and 557 nested source paths under pending Git roots. All 126 records in the largest project were present in the captured Git metadata set. For all 110 present candidate workspace records, the derived closure fits the unchanged 128-key cap. The worst case needs 110 issue keys and one project key, with 17 keys of headroom. Its 173 imported children introduce no physical issue owner outside those 110 logical task owners. This checks the observed key bound only. It does not claim migration eligibility or a coherent snapshot. - The initial stacked CI did not run four Runner test lanes: trusted master passed new --package-lane/--native-server options to the old base lane script. The branch is now rebased onto actual merged master and retargeted before synchronization. It includes the canonical current lane parser without an unrelated Runner patch. Fresh new-head CI is required. - The actual importer/source owner run passed 36 cases across three suites in 187.75 seconds. It covers sibling prepared recovery, unrelated running remote work, common-cache contention, changed coordinator/allocation/path state during upload, exact nested coverage, dirty unpushed work, and existing expiration/reopen owners. Wrapper exit 0 and cleanup completed. - A separate previously hanging lifecycle-worker restore passed in 17.33 seconds with the unchanged 15-second test-body limit. An earlier run timed out in three expiration/reopen cases and blocked cleanup; its wrapper later exited 1. These failures remain recorded. They are not counted as passes. - Two boundary controls first failed: a foreign providerRef-only alias was incorrectly eligible, and actual reopen did not admit its distinct cache owner at the initial source boundary. The narrow guards now inspect both foreign locators and derive physical Git keys before the raw lifecycle transaction. The foreign alias and existing real nested reopen contention passed. The private-cache control first retained contradictory recorded source provenance and stayed fail-closed after release. The corrected present legacy-worktree fixture passed in 12.61 seconds. These failed attempts remain recorded; they are not passes. - The metadata reader and serialized/local archive controls use actual Git repositories. They compare archive bytes and digests, remove every original source, restore staged and unstaged work, and run fsck. The metadata reader passed 13 cases in 2.85 seconds. The local/serialized archive controls passed nine cases in 9.27 seconds. All processes exited 0. An additional actual importer control changed a relative commondir pointer to the absolute name of the same store during upload. Publication was rejected, the uploaded journal remained available, and original files stayed intact (one case, 5.86 seconds; wrapper exit 0 and cleanup complete). - The final rebased full local server/test type graph passes with db/shared/Runner source aliases because reused dependency declarations predate new master exports. The first rebased attempt reported those stale declaration fields/tables, and is not counted as a pass. Module boundaries and diff checks pass. Standard package builds, types and broad regression checks remain required in CI. - No hosted import, admission hold, cancellation, provider mutation, source filesystem write or shared-cache Git command was performed for this change. The source observation is read-only and is not a locked eligibility proof. ## Risks - A missing writer owner could make physical source exclusion incomplete. Existing mutation owners retain the same company/task/project keys, and uncertain metadata fails closed. - A project can contain a large cohort. Limits remain finite. A bound failure keeps the source blocked. - Bare, detached, unowned or network Git stores remain blocked online. Missing archived sources retain their existing authenticated reconstruction path. - Exact recovery proofs preserve authority and obligations. They do not infer provider termination or mark a prepared descriptor finalized. - This change does not claim all observed Git records are immediately eligible. Existing preparation, provider, recovery, runtime and source-change blockers remain. ## Model Used OpenAI GPT-6 through Codex, with reasoning, tool use and code execution. The host does not expose a more specific backend model ID or context-window size. ## 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 - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
504 lines
36 KiB
TypeScript
504 lines
36 KiB
TypeScript
import { captureGitWorkspaceContents, assertGitWorkspaceContentsUnchanged, readManagedCheckoutContents, selectRestoredProjectRepositories, type ManagedCheckoutContents } from "./git-workspace-contents.js";
|
|
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { randomBytes } from "node:crypto";
|
|
import { acquireGitExpirationLocks } from "./git-workspace-expiration-locks.js";
|
|
import { and, eq, inArray, ne, sql } from "drizzle-orm";
|
|
import { executionWorkspaces, issues, heartbeatRuns, nativeRunFinalizations, environmentLeases,
|
|
workspaceRuntimeServices, issueRecoveryActions, projectWorkspaces, withDedicatedDbConnection, type Db } from "@paperclipai/db";
|
|
import { createIssueGitRemoteAuthProvider } from "./git-credentials.js";
|
|
import type { StorageService } from "../storage/types.js";
|
|
import { TASK_DIRECTORY_RETENTION_KEY } from "./isolated-task-directory-snapshot.js";
|
|
import { currentRestorePointSnapshot, RestorePointSkip, type RestorePointLimits } from "./git-restore-point.js";
|
|
import { abandonGitRestorePoint, abandonGitRestorePointsByKey, activateGitRestorePoints, restorePointHistoryUnchanged, restorePointSnapshotMatches, storeGitRestorePoint } from "./git-restore-point-storage.js";
|
|
import { withIsolatedRepositorySourceLock, verifiedRecordedIsolatedRemoteSource } from "./isolated-remote-workspace-source.js";
|
|
import { retainCurrentWorkspaceSource } from "./workspace-source-lock.js";
|
|
import { localWorkspaceSourcePaths } from "./execution-workspace-source-roots.js";
|
|
import { managedProjectCheckoutName } from "./managed-project-checkout-name.js";
|
|
import { nestedRepositoryBaseCandidates } from "./nested-repository-worktrees.js";
|
|
import type { GitRemoteAuthProvider } from "./git-credentials.js";
|
|
import { assertReproducibleGitFiles, assertReproducibleBaseGitStorage, assertReproducibleManagedCheckout, provenManagedCheckoutHistory, inspectGitCacheRecipe, inspectGitCacheRecipeForRestorePoint, readGitCacheRestoration, GIT_CACHE_RESTORATION_SCHEMA_V2,
|
|
verifyRemoteGitCommit, gitCacheExpirationLockLocations, recoverGitCacheExpirationLocks, GitCacheRestorationFailure, type ManagedCheckoutProver, gitCacheCommand, readGitCacheExpiration, finishGitCacheTrash, type GitCacheExpiration, type GitCacheRestoration, GIT_CACHE_RESTORATION_KEY, GIT_CACHE_EXPIRATION_REASON } from "./git-workspace-restoration.js";
|
|
|
|
export async function allocatedBytes(target: string, shouldContinue: () => boolean): Promise<number> {
|
|
if (!shouldContinue()) throw new Error("scan_stopped");
|
|
const entry = await fs.lstat(target);
|
|
if (!entry.isDirectory() && !entry.isFile() && !entry.isSymbolicLink()) throw new Error("unsupported_filesystem_entry");
|
|
let bytes = entry.blocks * 512;
|
|
if (entry.isDirectory()) for (const name of await fs.readdir(target)) bytes += await allocatedBytes(path.join(target, name), shouldContinue);
|
|
return bytes;
|
|
}
|
|
export function gitExpirationSkipReason(error: unknown) {
|
|
if (error instanceof GitCacheRestorationFailure) return "upstream_commit_unverified";
|
|
return error instanceof Error && /^[a-z][a-z_]+$/.test(error.message) ? error.message : "inspection_failed";
|
|
}
|
|
const skipReason = gitExpirationSkipReason;
|
|
|
|
export function withAnonymousGitFallback(provider: GitRemoteAuthProvider): GitRemoteAuthProvider {
|
|
return async (remoteUrl) => provider(remoteUrl).catch(() => null);
|
|
}
|
|
|
|
function expirationGitAuth(db: Db, workspace: Workspace) {
|
|
return withAnonymousGitFallback(createIssueGitRemoteAuthProvider(db, workspace.companyId, workspace.sourceIssueId!));
|
|
}
|
|
|
|
const EXPIRATION_KEY = "gitCacheExpiration";
|
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
type Workspace = typeof executionWorkspaces.$inferSelect;
|
|
type Tx = Parameters<Parameters<Db["transaction"]>[0]>[0];
|
|
type Expiration = GitCacheExpiration;
|
|
export type GitExpirationFailure = { name: string; code: string | null; at: string | null; stderr: string | null };
|
|
|
|
export type GitExpirationResult = { candidate: boolean; expired: boolean; reclaimedBytes: number;
|
|
candidateBytes: number; baseExpired: boolean; skipReason: string | null; restorePoint: boolean; failure?: GitExpirationFailure };
|
|
|
|
function redactGitOutput(text: string) {
|
|
return text.replace(/:\/\/[^@\s/]+@/g, "://<redacted>@")
|
|
.replace(/(authorization|token|password|secret)(\s*[:=]\s*)[^\n]*/gi, "$1$2<redacted>");
|
|
}
|
|
|
|
function recordSkip(result: GitExpirationResult, error: unknown) {
|
|
result.skipReason = error instanceof RestorePointSkip ? error.reason : skipReason(error);
|
|
if (result.skipReason === "inspection_failed") result.failure = describeGitExpirationFailure(error);
|
|
}
|
|
|
|
export function describeGitExpirationFailure(error: unknown): GitExpirationFailure {
|
|
if (!(error instanceof Error)) return { name: typeof error, code: null, at: null, stderr: null };
|
|
const details = error as Error & { code?: unknown; stderr?: unknown };
|
|
const frame = error.stack?.split("\n").slice(1).map(line => line.trim()).find(line => line.startsWith("at ")) ?? null;
|
|
const stderr = typeof details.stderr === "string" ? details.stderr : Buffer.isBuffer(details.stderr) ? details.stderr.toString("utf8") : "";
|
|
return {
|
|
name: error.name,
|
|
code: details.code === undefined || details.code === null ? null : String(details.code),
|
|
at: frame ? frame.slice(0, 300) : null,
|
|
stderr: stderr.trim() ? redactGitOutput(stderr.trim()).slice(0, 500) : null,
|
|
};
|
|
}
|
|
|
|
export type GitRestorePointExpirationOptions = {
|
|
storage: StorageService;
|
|
notBefore: Date;
|
|
keyDurable: boolean;
|
|
limits?: Partial<RestorePointLimits>;
|
|
};
|
|
|
|
const RESTORE_POINT_RELAXABLE = new Set(["dirty_or_untracked_files", "unique_ignored_files", "unique_original_head",
|
|
"unique_reflog_commits", "upstream_commit_unverified", "unpushed_managed_checkout"]);
|
|
const CHECKOUT_RESTORE_POINT_RELAXABLE = new Set(["dirty_or_untracked_files", "unpushed_managed_checkout", "unique_ignored_files",
|
|
"unique_original_head", "unique_reflog_commits"]);
|
|
|
|
type CheckoutRestorePointNeed = { headOnRemote: boolean; path: string; repoUrl: string };
|
|
|
|
async function restorePointBoundary(cwd: string, repoUrl: string, headOnRemote: boolean, auth: GitRemoteAuthProvider, signal?: AbortSignal) {
|
|
const head = (await gitCacheCommand(cwd, ["rev-parse", "--verify", "HEAD^{commit}"], signal)).trim();
|
|
if (headOnRemote) return [head];
|
|
const upstream = await gitCacheCommand(cwd, ["rev-parse", "--verify", "-q", "refs/remotes/origin/HEAD"], signal)
|
|
.catch(() => "");
|
|
const tip = upstream.trim();
|
|
if (!tip) return [];
|
|
const base = (await gitCacheCommand(cwd, ["merge-base", "HEAD", tip], signal).catch(() => "")).trim();
|
|
if (!/^[a-f0-9]{40}$/.test(base)) return [];
|
|
try { await verifyRemoteGitCommit(repoUrl, base, auth, signal); return [base]; }
|
|
catch (error) { if (signal?.aborted) throw error; return []; }
|
|
}
|
|
|
|
/** Re-read the whole tree and every runtime/recovery consumer immediately before deletion. */
|
|
export function expirationScope(workspace: Workspace) {
|
|
// Decode the three legacy keys together only after cheap native/pointer
|
|
// matches fail. Strict JSONPath leaves non-object inputs unmatched, as ->>
|
|
// did, while those inputs can still match an independent native binding.
|
|
return sql`
|
|
WITH RECURSIVE tree AS (
|
|
SELECT id, status, completed_at, cancelled_at, updated_at FROM ${issues}
|
|
WHERE company_id = ${workspace.companyId} AND id = ${workspace.sourceIssueId}
|
|
UNION
|
|
SELECT c.id, c.status, c.completed_at, c.cancelled_at, c.updated_at FROM ${issues} c
|
|
JOIN tree p ON c.parent_id = p.id WHERE c.company_id = ${workspace.companyId}
|
|
), bound AS (
|
|
SELECT id, updated_at, checkout_run_id, execution_run_id FROM ${issues}
|
|
WHERE company_id = ${workspace.companyId} AND execution_workspace_id = ${workspace.id}
|
|
), legacy_issue_ids AS MATERIALIZED (
|
|
SELECT unnest(array_agg(id::text)) AS id FROM tree
|
|
), relevant_runs AS (
|
|
SELECT r.id, r.status, r.created_at, r.started_at, r.finished_at, r.controller_lease_expires_at
|
|
FROM ${heartbeatRuns} r WHERE r.company_id = ${workspace.companyId} AND (
|
|
r.issue_id IN (SELECT id FROM tree) OR r.native_issue_id IN (SELECT id FROM tree)
|
|
OR r.issue_id IN (SELECT id FROM bound) OR r.native_issue_id IN (SELECT id FROM bound)
|
|
OR r.id IN (SELECT unnest(ARRAY[checkout_run_id, execution_run_id]) FROM bound)
|
|
OR EXISTS (
|
|
SELECT 1 FROM jsonb_to_record(
|
|
jsonb_path_query_first(r.context_snapshot, 'strict $ ? (@.type() == "object")')
|
|
) context("issueId" text, "taskId" text, "executionWorkspaceId" text)
|
|
WHERE context."issueId" IN (SELECT id FROM legacy_issue_ids)
|
|
OR context."taskId" IN (SELECT id FROM legacy_issue_ids)
|
|
OR context."executionWorkspaceId" = ${workspace.id}
|
|
)
|
|
)
|
|
)`;
|
|
}
|
|
|
|
function issueUsesWorkspace(workspace: Pick<Workspace, "id" | "sourceIssueId">) {
|
|
return sql`(${issues.id} = ${workspace.sourceIssueId} OR ${issues.executionWorkspaceId} = ${workspace.id}
|
|
OR (${issues.workspaceSelection}->'selection'->>'kind' = 'existing'
|
|
AND ${issues.workspaceSelection}->'selection'->>'workspaceId' = ${workspace.id})
|
|
OR (${issues.workspacePendingSelection}->'intent'->'selection'->>'kind' = 'existing'
|
|
AND ${issues.workspacePendingSelection}->'intent'->'selection'->>'workspaceId' = ${workspace.id}))`;
|
|
}
|
|
|
|
export async function listIssuesUsingGitWorkspace(db: Db | Tx, workspace: Pick<Workspace, "id" | "companyId" | "sourceIssueId">, issueIds: string[]) {
|
|
if (issueIds.length === 0) return new Set<string>();
|
|
const rows = await db.select({ id: issues.id }).from(issues)
|
|
.where(and(eq(issues.companyId, workspace.companyId), inArray(issues.id, issueIds), issueUsesWorkspace(workspace)));
|
|
return new Set(rows.map(row => row.id));
|
|
}
|
|
|
|
export async function gitWorkspaceLastActivityAt(db: Db | Tx, workspace: Workspace): Promise<Date | null> {
|
|
if (!workspace.sourceIssueId) return null;
|
|
const rows = await db.execute<{ at: string | Date | null }>(sql`${expirationScope(workspace)}
|
|
SELECT greatest(
|
|
(SELECT max(updated_at) FROM tree),
|
|
(SELECT max(updated_at) FROM bound),
|
|
(SELECT max(greatest(created_at, started_at, finished_at)) FROM relevant_runs)
|
|
) AS at`);
|
|
const at = rows[0]?.at;
|
|
return at ? new Date(at) : null;
|
|
}
|
|
|
|
export async function isGitWorkspaceExpirationEligible(tx: Tx, workspace: Workspace, now: Date, idleCutoff?: Date,
|
|
options: { closedSiblingsAllowed?: boolean } = {}) {
|
|
if (workspace.metadata?.reopenPendingConsumption === true) return false;
|
|
const siblingBlocks = options.closedSiblingsAllowed
|
|
? sql`AND (other.status NOT IN ('archived', 'cleanup_failed') OR other.closed_at IS NULL
|
|
OR other.metadata->${TASK_DIRECTORY_RETENTION_KEY}->>'removed' = 'false')`
|
|
: sql``;
|
|
const closedTree = idleCutoff ? sql`
|
|
NOT EXISTS (SELECT 1 FROM tree WHERE updated_at > ${idleCutoff.toISOString()}::timestamptz)
|
|
AND NOT EXISTS (SELECT 1 FROM bound WHERE updated_at > ${idleCutoff.toISOString()}::timestamptz)
|
|
AND NOT EXISTS (SELECT 1 FROM relevant_runs WHERE greatest(created_at, started_at, finished_at) > ${idleCutoff.toISOString()}::timestamptz)` : sql`
|
|
NOT EXISTS (SELECT 1 FROM ${issues} WHERE ${issues.companyId} = ${workspace.companyId}
|
|
AND ${issues.id} IN (SELECT id FROM tree) AND ${issueUsesWorkspace(workspace)}
|
|
AND (${issues.status} NOT IN ('done', 'cancelled')
|
|
OR coalesce(CASE WHEN ${issues.status} = 'done' THEN ${issues.completedAt} ELSE ${issues.cancelledAt} END, ${issues.updatedAt}) > ${new Date(now.getTime() - DAY_MS).toISOString()}::timestamptz))
|
|
AND NOT EXISTS (SELECT 1 FROM ${issues} WHERE company_id = ${workspace.companyId}
|
|
AND execution_workspace_id = ${workspace.id} AND status NOT IN ('done', 'cancelled'))`;
|
|
const recoveryActionBlocks = idleCutoff ? sql`true` : sql`(cause = 'native_workspace_finalization_owner_unverified'
|
|
OR evidence->'systemRecovery'->>'state' IN ('scheduled', 'running', 'awaiting_evidence'))`;
|
|
const finalizationBlocks = idleCutoff
|
|
? sql`(f.phase <> 'committed' OR f.lease_owner IS NOT NULL OR f.recovery_state IS NOT NULL)`
|
|
: sql`(f.lease_owner IS NOT NULL OR f.phase NOT IN ('committed', 'terminal_failure'))`;
|
|
const rows = await tx.execute<{ allowed: boolean }>(sql`${expirationScope(workspace)}
|
|
SELECT CASE WHEN
|
|
EXISTS (SELECT 1 FROM ${executionWorkspaces} other WHERE other.id <> ${workspace.id}
|
|
AND (other.cwd = ${workspace.cwd} OR other.provider_ref = ${workspace.cwd}
|
|
OR left(other.cwd, length(${workspace.cwd}) + 1) = ${workspace.cwd} || '/') ${siblingBlocks})
|
|
OR EXISTS (SELECT 1 FROM ${projectWorkspaces} WHERE cwd = ${workspace.cwd}
|
|
OR left(cwd, length(${workspace.cwd}) + 1) = ${workspace.cwd} || '/')
|
|
THEN false ELSE (
|
|
EXISTS (SELECT 1 FROM tree)
|
|
AND ${closedTree}
|
|
AND NOT EXISTS (SELECT 1 FROM relevant_runs WHERE status NOT IN ('succeeded', 'failed', 'timed_out', 'cancelled', 'interrupted')
|
|
OR finished_at IS NULL OR controller_lease_expires_at > now())
|
|
AND NOT EXISTS (SELECT 1 FROM ${issueRecoveryActions} WHERE company_id = ${workspace.companyId}
|
|
AND source_issue_id IN (SELECT id FROM tree) AND status IN ('active', 'escalated')
|
|
AND ${recoveryActionBlocks})
|
|
AND NOT EXISTS (SELECT 1 FROM ${nativeRunFinalizations} f WHERE f.company_id = ${workspace.companyId}
|
|
AND (f.issue_id IN (SELECT id FROM tree) OR f.run_id IN (SELECT id FROM relevant_runs))
|
|
AND ${finalizationBlocks})
|
|
AND NOT EXISTS (SELECT 1 FROM ${environmentLeases} l WHERE l.company_id = ${workspace.companyId}
|
|
AND (l.execution_workspace_id = ${workspace.id} OR l.heartbeat_run_id IN (SELECT id FROM relevant_runs))
|
|
AND (l.status = 'pending_cleanup' OR l.cleanup_status = 'failed' OR l.metadata ? 'nativeWorkspaceExportResume'
|
|
OR (l.released_at IS NULL AND NOT coalesce(l.status = 'retained' AND l.lease_policy = 'reuse_by_environment' AND l.cleanup_status = 'success', false))))
|
|
AND NOT EXISTS (SELECT 1 FROM ${workspaceRuntimeServices} WHERE company_id = ${workspace.companyId}
|
|
AND (execution_workspace_id = ${workspace.id} OR cwd = ${workspace.cwd}
|
|
OR left(cwd, length(${workspace.cwd}) + 1) = ${workspace.cwd} || '/') AND status <> 'stopped')
|
|
|
|
) END AS allowed`);
|
|
return rows[0]?.allowed === true;
|
|
}
|
|
|
|
export function hasPendingGitExpiration(workspace: Workspace) {
|
|
const state = workspace.metadata?.[EXPIRATION_KEY] as Expiration | undefined;
|
|
return state?.schema === "paperclip.git-cache-expiration.v1" && !state.completed;
|
|
}
|
|
|
|
async function exists(target: string) {
|
|
return fs.lstat(target).then(() => true, (error: NodeJS.ErrnoException) => { if (error.code === "ENOENT") return false; throw error; });
|
|
}
|
|
|
|
/** Each ref/reflog object must be recoverable, and no other checkout may consume the base. */
|
|
async function inspectBase(db: Db, workspace: Workspace, signal?: AbortSignal, allowCurrentWorktree = false) {
|
|
const recipe = readGitCacheRestoration(workspace)!;
|
|
if (recipe.sourceKind !== "isolated_remote") throw new Error("shared_base_preserved");
|
|
const other = await db.select().from(executionWorkspaces).where(and(ne(executionWorkspaces.id, workspace.id),
|
|
sql`(${executionWorkspaces.metadata}->'isolatedRemoteSource'->>'companyId' = ${workspace.companyId}
|
|
AND ${executionWorkspaces.metadata}->'isolatedRemoteSource'->>'issueId' = ${workspace.sourceIssueId})
|
|
OR ${executionWorkspaces.cwd} = ${recipe.sourcePath} OR ${executionWorkspaces.providerRef} = ${recipe.sourcePath}`));
|
|
if (other.some(row => !["archived", "cleanup_failed"].includes(row.status)
|
|
|| !readGitCacheRestoration(row) || !readGitCacheExpiration(row)?.workspaceRemoved)) throw new Error("base_has_consumers");
|
|
const [projectConsumer] = await db.select({ id: projectWorkspaces.id }).from(projectWorkspaces).where(eq(projectWorkspaces.cwd, recipe.sourcePath)).limit(1);
|
|
if (projectConsumer) throw new Error("base_has_consumers");
|
|
const [service] = await db.select({ id: workspaceRuntimeServices.id }).from(workspaceRuntimeServices).where(sql`
|
|
${workspaceRuntimeServices.status} <> 'stopped' AND (${workspaceRuntimeServices.cwd} = ${recipe.sourcePath}
|
|
OR left(${workspaceRuntimeServices.cwd}, length(${recipe.sourcePath}) + 1) = ${recipe.sourcePath} || '/')`).limit(1);
|
|
if (service) throw new Error("base_has_live_services");
|
|
await verifiedRecordedIsolatedRemoteSource(workspace);
|
|
const worktrees = (await gitCacheCommand(recipe.sourcePath, ["worktree", "list", "--porcelain"], signal))
|
|
.split("\n").filter(line => line.startsWith("worktree ")).map(line => line.slice(9));
|
|
const allowed = [await fs.realpath(recipe.sourcePath), ...(allowCurrentWorktree ? [await fs.realpath(recipe.worktreePath)] : [])];
|
|
if (worktrees.some(cwd => !allowed.includes(cwd))) throw new Error("base_has_worktrees");
|
|
await assertReproducibleGitFiles(recipe.sourcePath, signal);
|
|
await assertReproducibleBaseGitStorage(recipe.sourcePath, signal);
|
|
const refs = (await gitCacheCommand(recipe.sourcePath, ["for-each-ref", "--format=%(objectname) %(refname)"], signal)).trim().split("\n").filter(Boolean);
|
|
if (refs.some(ref => !/^[a-f0-9]{40} refs\/(heads|remotes\/origin)\//.test(ref))) throw new Error("unsupported_base_refs");
|
|
const commits = new Set([...refs.map(ref => ref.split(" ")[0]!),
|
|
...(await gitCacheCommand(recipe.sourcePath, ["reflog", "show", "--all", "--format=%H"], signal)).trim().split("\n").filter(Boolean)]);
|
|
if (commits.size > 50) throw new Error("base_ref_verification_budget");
|
|
const auth = expirationGitAuth(db, workspace);
|
|
for (const commit of commits) await verifyRemoteGitCommit(recipe.repoUrl, commit, auth, signal);
|
|
// Unreachable objects may contain unique rewritten work not present in a reflog.
|
|
const unreachable = await gitCacheCommand(recipe.sourcePath, ["fsck", "--no-reflogs", "--unreachable", "--no-progress"], signal);
|
|
if (unreachable.trim()) throw new Error("unreachable_base_objects");
|
|
return allocatedBytes(recipe.sourcePath, () => { signal?.throwIfAborted(); return true; });
|
|
}
|
|
|
|
function managedCheckoutProver(db: Db, workspace: Workspace, auth: GitRemoteAuthProvider, checkouts: Map<string, ManagedCheckoutContents>,
|
|
restorePoints?: Map<string, CheckoutRestorePointNeed>): ManagedCheckoutProver {
|
|
let rows: Promise<Array<{ id: string; repoUrl: string | null; repoRef: string | null; cwd: string | null }>> | undefined;
|
|
return async (checkoutPath, name, signal) => {
|
|
if (!workspace.projectId) throw new Error("unknown_managed_checkout");
|
|
rows ??= db.select({ id: projectWorkspaces.id, repoUrl: projectWorkspaces.repoUrl, repoRef: projectWorkspaces.repoRef,
|
|
cwd: projectWorkspaces.cwd }).from(projectWorkspaces)
|
|
.where(and(eq(projectWorkspaces.companyId, workspace.companyId), eq(projectWorkspaces.projectId, workspace.projectId)));
|
|
const source = selectRestoredProjectRepositories(await rows, readGitCacheRestoration(workspace)?.contents).find(row => row.repoUrl
|
|
&& managedProjectCheckoutName({ id: row.id, repoUrl: row.repoUrl, repoRef: row.repoRef, cwd: row.cwd }) === name);
|
|
if (!source?.repoUrl) throw new Error("unknown_managed_checkout");
|
|
const baseCandidates = nestedRepositoryBaseCandidates({ companyId: workspace.companyId, projectId: workspace.projectId, repoUrl: source.repoUrl });
|
|
try { await assertReproducibleManagedCheckout(checkoutPath, { repoUrl: source.repoUrl, resolveGitAuth: auth, signal, baseCandidates }); }
|
|
catch (error) {
|
|
if (!restorePoints || !CHECKOUT_RESTORE_POINT_RELAXABLE.has(skipReason(error))) throw error;
|
|
const { headOnRemote } = await assertReproducibleManagedCheckout(checkoutPath, { repoUrl: source.repoUrl, resolveGitAuth: auth, signal, restorePoint: true, baseCandidates });
|
|
restorePoints.set(name, { headOnRemote, path: checkoutPath, repoUrl: source.repoUrl });
|
|
}
|
|
checkouts.set(name, await readManagedCheckoutContents(checkoutPath, name, { ...source, repoUrl: source.repoUrl }, signal));
|
|
};
|
|
}
|
|
|
|
export async function expireReconstructableGitWorkspace(db: Db, snapshot: Workspace, options: {
|
|
dryRun: boolean; expireBase: boolean; now: Date; signal?: AbortSignal;
|
|
beforeDelete?: (workspace: Workspace) => Promise<void>;
|
|
restorePoint?: GitRestorePointExpirationOptions;
|
|
idleCutoff?: Date;
|
|
}): Promise<GitExpirationResult> {
|
|
const result: GitExpirationResult = { candidate: false, expired: false, reclaimedBytes: 0,
|
|
candidateBytes: 0, baseExpired: false, skipReason: null, restorePoint: false };
|
|
let pendingRestorePoints: string[] = [];
|
|
if (!snapshot.sourceIssueId) return { ...result, skipReason: "missing_source_issue" };
|
|
try {
|
|
return await withIsolatedRepositorySourceLock(db, snapshot.companyId, snapshot.sourceIssueId, async () => {
|
|
for (const cwd of localWorkspaceSourcePaths(snapshot)) await retainCurrentWorkspaceSource(cwd);
|
|
const retainedSource = readGitCacheRestoration(snapshot)?.sourcePath;
|
|
if (retainedSource) await retainCurrentWorkspaceSource(retainedSource);
|
|
return withDedicatedDbConnection(db, connection => connection.transaction(async tx => {
|
|
const acquired = await tx.execute<{ locked: boolean }>(sql`select pg_try_advisory_xact_lock(hashtextextended(${`execution_workspace_lifecycle:${snapshot.id}`}, 0)) as locked`);
|
|
if (!acquired[0]?.locked) throw new Error("lifecycle_busy");
|
|
let workspace = (await db.select().from(executionWorkspaces).where(and(eq(executionWorkspaces.id, snapshot.id), eq(executionWorkspaces.companyId, snapshot.companyId))))[0];
|
|
if (!workspace || workspace.updatedAt.getTime() !== snapshot.updatedAt.getTime()) throw new Error("workspace_changed");
|
|
if (!await isGitWorkspaceExpirationEligible(tx, workspace, options.now, options.idleCutoff)) throw new Error("live_or_unresolved_consumer");
|
|
let state = ["active", "idle", "in_review"].includes(workspace.status) ? null : readGitCacheExpiration(workspace);
|
|
// A restored active workspace starts a new expiration cycle and records its new HEAD.
|
|
if (!hasPendingGitExpiration(workspace) && !["active", "idle", "in_review"].includes(workspace.status)) throw new Error("not_expirable");
|
|
if (["active", "idle", "in_review"].includes(workspace.status)) state = null;
|
|
const auth = expirationGitAuth(db, workspace);
|
|
const checkouts = new Map<string, ManagedCheckoutContents>();
|
|
const identity = { companyId: workspace.companyId, workspaceId: workspace.id };
|
|
const proveManagedCheckout = managedCheckoutProver(db, workspace, auth, checkouts);
|
|
const checkoutRestorePoints = new Map<string, CheckoutRestorePointNeed>();
|
|
let headOnRemote = true;
|
|
let needsRestorePoint = false;
|
|
let recipe: GitCacheRestoration;
|
|
if (state) recipe = readGitCacheRestoration(workspace)!;
|
|
else {
|
|
try { recipe = await inspectGitCacheRecipe(workspace, auth, options.signal, { proveManagedCheckout }); }
|
|
catch (error) {
|
|
if (!options.restorePoint || !RESTORE_POINT_RELAXABLE.has(skipReason(error))) throw error;
|
|
if (options.now.getTime() < options.restorePoint.notBefore.getTime()) throw new Error("restore_point_not_due");
|
|
if (!options.restorePoint.keyDurable) throw new Error("restore_point_key_not_durable");
|
|
checkouts.clear();
|
|
checkoutRestorePoints.clear();
|
|
const inspected = await inspectGitCacheRecipeForRestorePoint(workspace, auth, options.signal, {
|
|
proveManagedCheckout: managedCheckoutProver(db, workspace, auth, checkouts, checkoutRestorePoints) });
|
|
recipe = inspected.recipe;
|
|
headOnRemote = inspected.headOnRemote;
|
|
needsRestorePoint = true;
|
|
result.restorePoint = true;
|
|
}
|
|
}
|
|
if (!state) recipe.contents = await captureGitWorkspaceContents(recipe.worktreePath, identity, [...checkouts.values()]);
|
|
const present = await exists(recipe.worktreePath);
|
|
if (!state && !present) throw new Error("missing_unverified_worktree");
|
|
result.candidate = true;
|
|
if (present) result.candidateBytes = await allocatedBytes(recipe.worktreePath, () => { options.signal?.throwIfAborted(); return true; });
|
|
if (options.dryRun) {
|
|
if (options.expireBase) {
|
|
try { result.candidateBytes += await inspectBase(db, { ...workspace,
|
|
metadata: { ...workspace.metadata, [GIT_CACHE_RESTORATION_KEY]: recipe } }, options.signal, present); }
|
|
catch (error) { recordSkip(result, error); }
|
|
}
|
|
return result;
|
|
}
|
|
if (!state) {
|
|
const generation = Number(workspace.metadata?.lifecycleGeneration ?? 0) + 1;
|
|
const rawAdmin = (await gitCacheCommand(recipe.worktreePath, ["rev-parse", "--absolute-git-dir"], options.signal)).trim();
|
|
const worktreeAdmin = path.join(recipe.sourcePath, ".git", "worktrees", path.basename(rawAdmin));
|
|
const canonicalAdmin = path.join(await fs.realpath(recipe.sourcePath), ".git", "worktrees", path.basename(rawAdmin));
|
|
if (await fs.realpath(worktreeAdmin) !== canonicalAdmin || await fs.realpath(rawAdmin) !== canonicalAdmin) {
|
|
throw new Error("unproven_worktree_admin");
|
|
}
|
|
if (needsRestorePoint) {
|
|
const owner = { companyId: workspace.companyId, id: workspace.id, sourceIssueId: workspace.sourceIssueId };
|
|
const stored = await storeGitRestorePoint({ db, storage: options.restorePoint!.storage, workspace: owner,
|
|
cwd: recipe.worktreePath, generation, headOnRemote, limits: options.restorePoint!.limits, signal: options.signal,
|
|
upstreamCommits: await restorePointBoundary(recipe.worktreePath, recipe.repoUrl, headOnRemote, auth, options.signal) });
|
|
if (stored) {
|
|
pendingRestorePoints.push(stored.ledgerId);
|
|
recipe = { ...recipe, restorePoint: stored.ref };
|
|
} else if (!headOnRemote) throw new Error("upstream_commit_unverified");
|
|
for (const [name, need] of [...checkoutRestorePoints.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
const storedCheckout = await storeGitRestorePoint({ db, storage: options.restorePoint!.storage, workspace: owner,
|
|
cwd: need.path, checkout: name, generation, headOnRemote: need.headOnRemote,
|
|
resolvePreserveCommits: async () => (await provenManagedCheckoutHistory(need.path, need.repoUrl, auth, options.signal)).preserve,
|
|
limits: options.restorePoint!.limits,
|
|
signal: options.signal, upstreamCommits: await restorePointBoundary(need.path, need.repoUrl, need.headOnRemote, auth, options.signal) });
|
|
if (!storedCheckout) {
|
|
if (!need.headOnRemote) throw new Error("unpushed_managed_checkout");
|
|
continue;
|
|
}
|
|
pendingRestorePoints.push(storedCheckout.ledgerId);
|
|
recipe = { ...recipe, contents: { ...recipe.contents!, checkouts: recipe.contents!.checkouts.map(checkout =>
|
|
checkout.name === name ? { ...checkout, restorePoint: storedCheckout.ref } : checkout) } };
|
|
}
|
|
if (pendingRestorePoints.length > 0) recipe = { ...recipe, schema: GIT_CACHE_RESTORATION_SCHEMA_V2 };
|
|
}
|
|
state = { schema: "paperclip.git-cache-expiration.v1", generation, workspaceRemoved: false,
|
|
worktreeTrash: path.join(path.dirname(recipe.worktreePath), `.expired-git-worktree-${workspace.id}-${generation}`),
|
|
worktreeAdmin, baseTrash: null, completed: false, lockToken: randomBytes(32).toString("hex") };
|
|
if (await exists(state.worktreeTrash)) throw new Error("unknown_worktree_trash");
|
|
// Commit the immutable restoration recipe BEFORE touching disk. The separate
|
|
// lifecycle-lock connection remains held through deletion and final state.
|
|
const archive = { status: "archived", closedAt: options.now,
|
|
cleanupEligibleAt: options.now, cleanupReason: GIT_CACHE_EXPIRATION_REASON, updatedAt: options.now,
|
|
metadata: { ...workspace.metadata, lifecycleGeneration: generation, [GIT_CACHE_RESTORATION_KEY]: recipe, [EXPIRATION_KEY]: state },
|
|
};
|
|
[workspace] = await db.transaction(async (archiveTx) => {
|
|
const rows = await archiveTx.update(executionWorkspaces).set(archive).where(eq(executionWorkspaces.id, workspace!.id)).returning();
|
|
await activateGitRestorePoints(archiveTx, { ledgerIds: pendingRestorePoints, workspaceId: workspace!.id, at: options.now });
|
|
return rows;
|
|
});
|
|
pendingRestorePoints = [];
|
|
}
|
|
if (!workspace) throw new Error("workspace_changed");
|
|
const persist = async () => {
|
|
[workspace] = await db.update(executionWorkspaces).set({ metadata: { ...workspace!.metadata, [EXPIRATION_KEY]: state }, updatedAt: new Date() })
|
|
.where(eq(executionWorkspaces.id, snapshot.id)).returning();
|
|
};
|
|
if (!state.lockToken) {
|
|
state.lockToken = randomBytes(32).toString("hex");
|
|
await persist();
|
|
}
|
|
await recoverGitCacheExpirationLocks(workspace!);
|
|
const lockLocations = await gitCacheExpirationLockLocations(workspace!);
|
|
await options.beforeDelete?.(workspace!);
|
|
if (!await isGitWorkspaceExpirationEligible(tx, workspace, options.now, options.idleCutoff)) throw new Error("live_or_unresolved_consumer");
|
|
if (!state.workspaceRemoved && present) {
|
|
const locks = [await acquireGitExpirationLocks(lockLocations.filter(location => location.checkout === null), state.lockToken)];
|
|
try {
|
|
try {
|
|
checkouts.clear();
|
|
const savedCheckoutRestorePoints = new Map((recipe.contents?.checkouts ?? []).filter(checkout => checkout.restorePoint)
|
|
.map(checkout => [checkout.name, checkout.restorePoint!]));
|
|
const relaxedChecks = new Map<string, CheckoutRestorePointNeed>();
|
|
const relaxedProver = managedCheckoutProver(db, workspace, auth, checkouts, relaxedChecks);
|
|
await assertReproducibleGitFiles(recipe.worktreePath, options.signal, { preserveSettings: true,
|
|
proveManagedCheckout: async (checkoutPath, name, signal) => {
|
|
if (!savedCheckoutRestorePoints.has(name)) return proveManagedCheckout(checkoutPath, name, signal);
|
|
await relaxedProver(checkoutPath, name, signal);
|
|
if (!await restorePointHistoryUnchanged(savedCheckoutRestorePoints.get(name)!, checkoutPath, signal)) {
|
|
throw new Error("restore_point_contents_changed");
|
|
}
|
|
},
|
|
restorePoint: Boolean(recipe.restorePoint),
|
|
verifyFetchedCommit: (commit, signal) => verifyRemoteGitCommit(recipe.repoUrl, commit, auth, signal) });
|
|
if (recipe.restorePoint && (!restorePointSnapshotMatches(recipe.restorePoint,
|
|
await currentRestorePointSnapshot(recipe.worktreePath, options.signal))
|
|
|| !await restorePointHistoryUnchanged(recipe.restorePoint, recipe.worktreePath, options.signal))) throw new Error("restore_point_contents_changed");
|
|
for (const checkout of checkouts.values()) {
|
|
const cwd = path.join(recipe.worktreePath, ".paperclip-repositories", checkout.name);
|
|
locks.push(await acquireGitExpirationLocks(lockLocations.filter(location => location.checkout === checkout.name), state.lockToken));
|
|
const savedCheckout = savedCheckoutRestorePoints.get(checkout.name);
|
|
if (savedCheckout) {
|
|
if (!restorePointSnapshotMatches(savedCheckout, await currentRestorePointSnapshot(cwd, options.signal))) throw new Error("restore_point_contents_changed");
|
|
} else if ((await gitCacheCommand(cwd, ["status", "--porcelain=v1", "--untracked-files=all", "-z"], options.signal)).length) throw new Error("managed_checkouts_changed");
|
|
checkouts.set(checkout.name, await readManagedCheckoutContents(cwd, checkout.name, checkout.source, options.signal));
|
|
}
|
|
await assertGitWorkspaceContentsUnchanged(recipe.worktreePath, identity, recipe.contents, [...checkouts.values()]);
|
|
if ((await gitCacheCommand(recipe.worktreePath, ["symbolic-ref", "--short", "HEAD"], options.signal)).trim() !== recipe.branchName) throw new Error("branch_identity_changed");
|
|
if ((await gitCacheCommand(recipe.worktreePath, ["rev-parse", "HEAD"], options.signal)).trim() !== recipe.commit) throw new Error("head_changed");
|
|
} catch (error) {
|
|
for (const lock of [...locks].reverse()) await lock.release();
|
|
const { restorePoint: _abandoned, ...reproducible } = recipe;
|
|
const strippedContents = reproducible.contents && { ...reproducible.contents,
|
|
checkouts: reproducible.contents.checkouts.map(({ restorePoint: _checkoutRestorePoint, ...checkout }) => checkout) };
|
|
const metadata: Record<string, unknown> = { ...workspace.metadata, lifecycleGeneration: state.generation + 1,
|
|
[GIT_CACHE_RESTORATION_KEY]: { ...reproducible, ...(strippedContents ? { contents: strippedContents } : {}),
|
|
schema: "paperclip.git-cache-restoration.v1", restoredAt: new Date().toISOString() } };
|
|
delete metadata[EXPIRATION_KEY];
|
|
await db.update(executionWorkspaces).set({ status: "active", closedAt: null, cleanupEligibleAt: null,
|
|
cleanupReason: null, metadata, updatedAt: new Date() }).where(eq(executionWorkspaces.id, workspace.id));
|
|
await abandonGitRestorePointsByKey(db, workspace.id, [recipe.restorePoint?.objectKey,
|
|
...(recipe.contents?.checkouts ?? []).map(checkout => checkout.restorePoint?.objectKey)].filter((key): key is string => Boolean(key)));
|
|
throw error;
|
|
}
|
|
await fs.rename(recipe.worktreePath, state.worktreeTrash);
|
|
await finishGitCacheTrash(workspace);
|
|
result.reclaimedBytes += result.candidateBytes;
|
|
} finally { for (const lock of locks.reverse()) await lock.release(); }
|
|
}
|
|
await finishGitCacheTrash(workspace);
|
|
state.workspaceRemoved = true;
|
|
await persist();
|
|
result.expired = true;
|
|
if (options.expireBase) {
|
|
try {
|
|
if ((!state.baseTrash || !await exists(state.baseTrash)) && await exists(recipe.sourcePath)) {
|
|
const bytes = await inspectBase(db, workspace!, options.signal);
|
|
if (!await isGitWorkspaceExpirationEligible(tx, workspace!, options.now, options.idleCutoff)) throw new Error("live_or_unresolved_consumer");
|
|
const trash = path.join(path.dirname(recipe.sourcePath), `.expired-git-base-${snapshot.id}-${state.generation}`);
|
|
if (await exists(trash)) throw new Error("unknown_base_trash");
|
|
state.baseTrash = trash;
|
|
await persist();
|
|
await fs.rename(recipe.sourcePath, trash);
|
|
result.candidateBytes += bytes;
|
|
}
|
|
if (state.baseTrash && await exists(state.baseTrash)) {
|
|
const stat = await fs.lstat(state.baseTrash);
|
|
if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("unsafe_base_trash");
|
|
const bytes = await allocatedBytes(state.baseTrash, () => { options.signal?.throwIfAborted(); return true; });
|
|
await fs.rm(state.baseTrash, { recursive: true, force: true });
|
|
result.reclaimedBytes += bytes;
|
|
result.baseExpired = true;
|
|
}
|
|
} catch (error) { recordSkip(result, error); }
|
|
}
|
|
state.completed = recipe.sourceKind !== "isolated_remote" || !options.expireBase || (result.baseExpired || !await exists(recipe.sourcePath)) && (!state.baseTrash || !await exists(state.baseTrash));
|
|
await persist();
|
|
return result;
|
|
}));
|
|
}, true);
|
|
} catch (error) {
|
|
for (const ledgerId of pendingRestorePoints) await abandonGitRestorePoint(db, ledgerId).catch(() => undefined);
|
|
options.signal?.throwIfAborted();
|
|
recordSkip(result, error);
|
|
return result;
|
|
}
|
|
}
|