mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 18:35:33 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Hosted stacks move from a volume to volume-free storage, offline in
maintenance or online with no outage
> - A stack moves only when no local native harness backup obligation
remains
> - A selected session can carry a backup stamp whose files are no
longer on the volume
> - Offline and online both refuse that stack forever, because nothing
can copy files that are gone
> - This pull request records "backup source was gone" for that session,
the same way offline and online, after a locked recheck
> - The benefit is that the stack moves, and the gone backup is recorded
instead of silently forgotten
## Linked Issues or Issue Description
**What happened?**
An online durable-data pass reports
`native_harness:selected_source_absent` on every pass for a stack. The
selected session has a v2 backup stamp, but `failover-backups/current`
for that scope is not on disk. The offline maintenance path also fails:
`native_harness_backups_require_migration` stays true because
`migrateNativeHarnessBackups` only visits sources on disk. The stack can
never move.
**Expected behavior**
When the backup files are already gone and the session is settled, both
paths record that the backup source was gone and stop counting it as an
obligation. A session that is still active, or a backup that the
expiration service is rotating, stays blocked with a name.
**Steps to reproduce**
1. Create a selected `paperclip_runner` session whose last run is
settled and whose environment lease has a v2 backup stamp.
2. Remove `<state base>/<scope digest>`.
3. Run `prestageNativeHarnessBackups` or `migrateNativeHarnessBackups`.
4. Before this change, `selected_source_absent` repeats and
`hasLocalNativeHarnessObligations` stays true.
**Paperclip version or commit**
master at b2a12fab49.
## What Changed
- Add `recordNativeHarnessBackupSourceAbsence`. In one transaction it
locks the selected session row, the run's finalization advisory lock,
the last run row and the finalization row. It requires a settled run
(terminal, no controller lease, no live execution control, no
finalization owner, no lease owner or scheduled retry on the
coordinator), no queued or running turn on the task or session, no
current durable backup for the scope, and stamped leases for the scope.
Then it rechecks the disk. If `current` is back, it records nothing. If
a `.retiring-*` backup exists, it returns `selected_source_retiring`. If
any other entry remains (for example `previous` from an interrupted
publish), it returns `selected_source_unsettled`. Only a scope with no
entries besides `.expired-*` trash counts as gone. Otherwise it writes
`nativeHarnessBackupSourceAbsent` on the last run (scope digest to
stamped manifest digests) and logs
`native_harness_backup_source_gone_recorded`.
- A lock timeout during the recheck returns `selected_source_busy` for
this pass instead of failing the harness phase.
- The obligation query excludes a scope only when the selected session's
last run records that exact scope and that exact stamped manifest
digest. A new turn changes the last run, and a new stamp changes the
manifest, so the obligation comes back.
- The online pass (`prestageNativeHarnessBackups`) and the offline pass
(`migrateNativeHarnessBackups`) both call the same recorder for the
obligations they could not copy. The online pass counts
`nativeHarnessBackupSourcesGone` and does not defer it.
- Previous-release story: the new key is additive run state. No resume
path reads it. A session behaves after the move exactly as it does on
the volume today without its backup. A previous release ignores the key
and still reports the obligation.
## Verification
- Red: the new tests fail on master. Online still reports
`selected_source_absent`, offline still has an obligation (`expected
true to be false`), and the retiring case is not named.
- Green: `npx vitest run
src/services/native-runtime/native-harness-online-preservation.integration.test.ts
src/services/native-runtime/native-harness-backup-obligations.integration.test.ts
src/services/online-durable-data-migration.test.ts` (62 passed,
including busy-row and previous-backup cases).
- Neighbors: `ephemeral-server-startup-run-state`,
`ephemeral-server-runtime-skill-startup`,
`native-harness-prestage(-runtime)`,
`native-harness-filesystem-independence`,
`native-harness-backup-storage` (218 passed).
- `pnpm typecheck` in `server` passes.
## Risks
- Medium. A gone backup stops blocking the move. The recorder writes
only after a locked recheck that the run is settled, no turn is active,
and the files are still absent. A backup that is rotating stays blocked.
- The record changes the last run's `runnerProfileJson`.
Filesystem-independence facts include that profile. The recorder writes
only when no valid independence receipt exists, so no existing receipt
changes.
- No schema migration.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Claude Code agent with tool use
and code execution.
## 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>
309 lines
19 KiB
TypeScript
309 lines
19 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { readdir } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { and, eq, inArray, or, sql } from "drizzle-orm";
|
|
import { environmentLeases, heartbeatRuns, issues, nativeHarnessBackups, nativeRunFinalizations, type Db } from "@paperclipai/db";
|
|
import type { StorageService } from "../../storage/types.js";
|
|
import { nativeHarnessStateBase, readNativeHarnessBackupStamp, verifyNativeHarnessBackupStampAsync } from "./native-harness-backup-stamp.js";
|
|
import { nativeHarnessBackupOwner, persistNativeHarnessBackup, verifyDurableNativeHarnessBackup } from "./native-harness-backup-storage.js";
|
|
import { MAX_NATIVE_HARNESS_BYTES, MAX_NATIVE_HARNESS_OBJECT_BYTES, nativeHarnessArchiveSizeLimit } from "./native-harness-backup-archive.js";
|
|
import { offlineMigrationExecutionWhere } from "../offline-migration-execution.js";
|
|
import { NativeHarnessBackupSourceError, readNativeHarnessBackupSource } from "./native-harness-backup-source.js";
|
|
import { settledNativeHarnessBackupOwner } from "./native-harness-preservation-source.js";
|
|
import { hasValidNativeHarnessFilesystemIndependence } from "./native-harness-filesystem-independence.js";
|
|
import { NATIVE_HARNESS_BACKUP_SOURCE_ABSENT_KEY, recordNativeHarnessBackupSourceAbsence } from "./native-harness-backup-source-absence.js";
|
|
|
|
const MAX_PARALLEL_INDEPENDENCE_CHECKS = 4;
|
|
|
|
export async function hasLocalNativeHarnessObligations(db: Db): Promise<boolean> {
|
|
for await (const _ of localNativeHarnessObligationScopes(db)) return true;
|
|
return false;
|
|
}
|
|
|
|
type ObligationScope = { companyId: string; selectedSessionId: string; scopeDigest: string };
|
|
|
|
async function* localNativeHarnessObligationScopes(db: Db, all = false, signal?: AbortSignal,
|
|
skip: ReadonlySet<string> = new Set()): AsyncGenerator<ObligationScope> {
|
|
let afterSession = "", afterScope = "";
|
|
for (;;) {
|
|
signal?.throwIfAborted();
|
|
const rows = await db.execute<{ company_id: string; selected_session_id: string; scope_digest: string; has_receipt: boolean }>(sql`with selected_sessions as materialized (
|
|
select s.company_id, s.id, s.last_run_id, r.native_issue_id, r.native_session_id
|
|
from agent_task_sessions s
|
|
join heartbeat_runs r on r.id = s.last_run_id and r.company_id = s.company_id and r.agent_id = s.agent_id
|
|
where s.adapter_type = 'paperclip_runner'
|
|
) select distinct
|
|
s.company_id, s.id::text as selected_session_id, coalesce(e.metadata->'nativeHarnessBackup'->>'sessionScopeSha256', '') as scope_digest,
|
|
r.runner_profile_json ? 'nativeHarnessFilesystemIndependence' as has_receipt from selected_sessions s
|
|
join heartbeat_runs r on r.id = s.last_run_id and r.company_id = s.company_id
|
|
join environment_leases e on e.company_id = s.company_id and e.issue_id = s.native_issue_id
|
|
and e.metadata->'nativeHarnessBackup'->>'normalizedSessionId' = s.native_session_id::text
|
|
where e.metadata->'nativeHarnessBackup'->>'schema' = 'paperclip.native-harness-backup-stamp.v2'
|
|
and not exists (select 1 from native_harness_backups b where b.company_id = s.company_id
|
|
and b.scope_digest = e.metadata->'nativeHarnessBackup'->>'sessionScopeSha256' and b.status = 'current')
|
|
and not coalesce(r.runner_profile_json #> array['${sql.raw(NATIVE_HARNESS_BACKUP_SOURCE_ABSENT_KEY)}', 'scopes',
|
|
e.metadata->'nativeHarnessBackup'->>'sessionScopeSha256', 'manifestSha256s']
|
|
? (e.metadata->'nativeHarnessBackup'->>'manifestSha256'), false)
|
|
and (s.id::text, coalesce(e.metadata->'nativeHarnessBackup'->>'sessionScopeSha256', '')) > (${afterSession}, ${afterScope})
|
|
order by selected_session_id, scope_digest limit 100`);
|
|
let next = 0, failed = false;
|
|
const invalid: ObligationScope[] = [];
|
|
const checked = await Promise.allSettled(Array.from({ length: Math.min(MAX_PARALLEL_INDEPENDENCE_CHECKS, rows.length) }, async () => {
|
|
while (!failed && (all || !invalid.length) && next < rows.length) {
|
|
signal?.throwIfAborted();
|
|
const row = rows[next++]!;
|
|
if (skip.has(row.scope_digest)) continue;
|
|
try {
|
|
if (!row.has_receipt || !await hasValidNativeHarnessFilesystemIndependence(db, {
|
|
companyId: row.company_id, selectedSessionId: row.selected_session_id, scopeDigest: row.scope_digest,
|
|
})) invalid.push({ companyId: row.company_id, selectedSessionId: row.selected_session_id, scopeDigest: row.scope_digest });
|
|
} catch (error) { failed = true; throw error; }
|
|
}
|
|
}));
|
|
for (const result of checked) if (result.status === "rejected") throw result.reason;
|
|
signal?.throwIfAborted();
|
|
yield* invalid;
|
|
if (rows.length < 100) return;
|
|
afterSession = rows.at(-1)!.selected_session_id; afterScope = rows.at(-1)!.scope_digest;
|
|
}
|
|
}
|
|
|
|
const MAX_PARALLEL_BACKUPS = 4;
|
|
// An archive and its encrypted copy coexist. Keep the same worst-case scratch
|
|
// bound as one maximum-size serial transfer, including tar header overhead.
|
|
const MAX_SCRATCH_BYTES = 2 * MAX_NATIVE_HARNESS_OBJECT_BYTES;
|
|
|
|
async function prepareSource(base: string, scopeDigest: string) {
|
|
const root = join(base, scopeDigest, "failover-backups", "current");
|
|
const files = await readNativeHarnessBackupSource(root);
|
|
if (!files) return null;
|
|
const { manifestBytes, manifest, state } = files;
|
|
if (state.lifecycle !== "suspended" || typeof state.runId !== "string"
|
|
|| manifest.schema !== "paperclip.native-harness-backup.v1") throw new Error("native_harness_migration_source_unsettled");
|
|
const names = new Set<string>();
|
|
let bytes = manifestBytes.length, closed = Array.isArray(manifest.directories) && manifest.directories.length > 0;
|
|
for (const value of Array.isArray(manifest.directories) ? manifest.directories : []) {
|
|
const entry = value as { name?: unknown; bytes?: unknown } | null;
|
|
if (!entry || typeof entry.name !== "string" || !/^[A-Za-z0-9._-]+$/.test(entry.name)
|
|
|| [".", ".."].includes(entry.name) || !Number.isSafeInteger(entry.bytes) || Number(entry.bytes) < 0) { closed = false; break; }
|
|
names.add(entry.name); bytes += Number(entry.bytes);
|
|
}
|
|
const entries = await readdir(root, { withFileTypes: true });
|
|
closed &&= entries.length === names.size + 1 && entries.every(entry => entry.name === "manifest.json"
|
|
? entry.isFile() : names.has(entry.name) && entry.isDirectory() && !entry.isSymbolicLink());
|
|
// Unknown extra files are preserved with an exclusive full-size reservation.
|
|
const archiveLimit = closed && Number.isSafeInteger(bytes) && bytes <= MAX_NATIVE_HARNESS_BYTES
|
|
? nativeHarnessArchiveSizeLimit(bytes)
|
|
: MAX_NATIVE_HARNESS_OBJECT_BYTES;
|
|
return { root, scopeDigest, runId: state.runId as string, archiveLimit,
|
|
sessionId: manifest.normalizedSessionId, runnerId: manifest.runnerInstanceId,
|
|
manifestSha256: `sha256:${createHash("sha256").update(manifestBytes).digest("hex")}` };
|
|
}
|
|
|
|
type Source = NonNullable<Awaited<ReturnType<typeof prepareSource>>>;
|
|
|
|
async function sourceRun(db: Db, source: Source) {
|
|
const [run] = await db.select({ companyId: heartbeatRuns.companyId, issueId: heartbeatRuns.nativeIssueId,
|
|
sessionId: heartbeatRuns.nativeSessionId, runnerId: heartbeatRuns.runnerInstanceId,
|
|
status: heartbeatRuns.status, finishedAt: heartbeatRuns.finishedAt }).from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, source.runId));
|
|
if (!run) return null;
|
|
if (!run.issueId || !run.sessionId || !run.runnerId || run.sessionId !== source.sessionId || run.runnerId !== source.runnerId) {
|
|
throw new Error("native_harness_migration_binding_mismatch");
|
|
}
|
|
return { ...run, issueId: run.issueId, sessionId: run.sessionId, runnerId: run.runnerId };
|
|
}
|
|
|
|
async function verifiedSourceLease(db: Db, source: Source, run: NonNullable<Awaited<ReturnType<typeof sourceRun>>>, signal: AbortSignal) {
|
|
const leases = await db.select({ stamp: sql<unknown>`${environmentLeases.metadata}->'nativeHarnessBackup'`,
|
|
providerLeaseId: environmentLeases.providerLeaseId }).from(environmentLeases).where(and(
|
|
eq(environmentLeases.companyId, run.companyId), eq(environmentLeases.issueId, run.issueId),
|
|
sql`${environmentLeases.metadata}->'nativeHarnessBackup'->>'manifestSha256' = ${source.manifestSha256}`));
|
|
for (const lease of leases) {
|
|
const stamp = readNativeHarnessBackupStamp(lease.stamp, lease.providerLeaseId ?? "");
|
|
if (stamp?.sessionScopeSha256 === source.scopeDigest && await verifyNativeHarnessBackupStampAsync(stamp, lease.providerLeaseId!, signal)) return lease.providerLeaseId!;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function migrateSource(db: Db, input: { storage: StorageService; signal: AbortSignal }, source: Source) {
|
|
input.signal.throwIfAborted();
|
|
const { root, scopeDigest, manifestSha256 } = source, run = await sourceRun(db, source);
|
|
if (!run) return 0;
|
|
const [current] = await db.select().from(nativeHarnessBackups).where(and(eq(nativeHarnessBackups.companyId, run.companyId),
|
|
eq(nativeHarnessBackups.scopeDigest, scopeDigest), eq(nativeHarnessBackups.status, "current")));
|
|
if (current) {
|
|
if (current.manifestSha256 !== manifestSha256) throw new Error("native_harness_migration_durable_source_conflict");
|
|
await verifyDurableNativeHarnessBackup({ db, storage: input.storage, companyId: run.companyId,
|
|
scopeDigest, manifestSha256: current.manifestSha256, signal: input.signal });
|
|
return 1;
|
|
}
|
|
if (!await verifiedSourceLease(db, source, run, input.signal)) throw new Error("native_harness_migration_backup_unverified");
|
|
await persistNativeHarnessBackup({ db, storage: input.storage, root, scopeDigest, signal: input.signal,
|
|
maxArchiveBytes: source.archiveLimit,
|
|
owner: { kind: "offline_migration", writersStopped: true, companyId: run.companyId, issueId: run.issueId, runId: source.runId } });
|
|
return 1;
|
|
}
|
|
|
|
async function mapSources<T>(input: { signal: AbortSignal; concurrency?: number; onResult?: (result: T) => void;
|
|
unavailableSource?: (error: unknown, scopeDigest: string) => T | undefined }, visit: (source: Source, signal: AbortSignal) => Promise<T>) {
|
|
const base = nativeHarnessStateBase();
|
|
const directories = await readdir(base).catch((error: NodeJS.ErrnoException) => {
|
|
if (error.code === "ENOENT") return [] as string[]; throw error;
|
|
});
|
|
let reserved = 0, wave: Source[] = [];
|
|
const results: T[] = [];
|
|
const flush = async () => {
|
|
const cancellation = new AbortController();
|
|
const signal = AbortSignal.any([input.signal, cancellation.signal]);
|
|
const settled = await Promise.allSettled(wave.map(async source => {
|
|
try { return await visit(source, signal); }
|
|
catch (error) { cancellation.abort(error); throw error; }
|
|
}));
|
|
// Never return failure while a peer can still upload or publish. Partial
|
|
// completed transfers remain valid for the next offline attempt.
|
|
if (cancellation.signal.aborted) throw cancellation.signal.reason;
|
|
for (const result of settled) {
|
|
if (result.status === "rejected") throw result.reason;
|
|
results.push(result.value);
|
|
input.onResult?.(result.value);
|
|
}
|
|
wave = []; reserved = 0;
|
|
};
|
|
for (const scopeDigest of directories.filter(name => /^[a-f0-9]{64}$/.test(name)).sort()) {
|
|
input.signal.throwIfAborted();
|
|
let source;
|
|
try { source = await prepareSource(base, scopeDigest); }
|
|
catch (error) {
|
|
input.signal.throwIfAborted();
|
|
const deferred = input.unavailableSource?.(error, scopeDigest);
|
|
if (deferred === undefined) throw error;
|
|
results.push(deferred); input.onResult?.(deferred); continue;
|
|
}
|
|
if (!source) continue;
|
|
const reservation = 2 * source.archiveLimit;
|
|
if (wave.length && (wave.length === (input.concurrency ?? MAX_PARALLEL_BACKUPS) || reserved + reservation > MAX_SCRATCH_BYTES)) await flush();
|
|
wave.push(source); reserved += reservation;
|
|
}
|
|
if (wave.length) await flush();
|
|
return results;
|
|
}
|
|
|
|
export async function migrateNativeHarnessBackups(db: Db, input: {
|
|
storage: StorageService; writersStopped: true; signal: AbortSignal;
|
|
}): Promise<number> {
|
|
if (!input.writersStopped) throw new Error("native_harness_migration_requires_offline_writers");
|
|
const [active] = await db.select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(offlineMigrationExecutionWhere()).limit(1);
|
|
if (active) throw new Error("native_harness_migration_requires_offline_writers");
|
|
const counts = await mapSources(input, (source, signal) => migrateSource(db, { storage: input.storage, signal }, source));
|
|
for await (const scope of localNativeHarnessObligationScopes(db, true, input.signal)) {
|
|
input.signal.throwIfAborted();
|
|
await recordNativeHarnessBackupSourceAbsence(db, { ...scope, signal: input.signal });
|
|
}
|
|
return counts.reduce<number>((total, count) => total + count, 0);
|
|
}
|
|
|
|
export async function prestageNativeHarnessBackups(db: Db, input: { storage: StorageService; signal: AbortSignal; onOutcome?: (outcome: string) => void }) {
|
|
const visited = new Set<string>();
|
|
const deferSource = (scopeDigest: string, reason: string, outcome: string) => {
|
|
console.warn(JSON.stringify({ schema: "paperclip.native-harness-prestage-source.v1", scopeDigest, reason }));
|
|
return outcome;
|
|
};
|
|
const outcomes = await mapSources({ ...input, concurrency: 1, onResult: (outcome: string) => input.onOutcome?.(outcome), unavailableSource: (error, scopeDigest) => {
|
|
visited.add(scopeDigest);
|
|
if (error instanceof NativeHarnessBackupSourceError || error instanceof Error && error.message === "native_harness_migration_source_unsettled") {
|
|
return deferSource(scopeDigest, error.message, "source_unavailable");
|
|
}
|
|
if (["ENOENT", "ENOTDIR", "EISDIR", "ELOOP", "EACCES", "EPERM"].includes((error as NodeJS.ErrnoException)?.code ?? "")) {
|
|
return deferSource(scopeDigest, "native_harness_migration_source_unavailable", "source_unavailable");
|
|
}
|
|
return undefined;
|
|
} }, async (source, signal) => {
|
|
signal.throwIfAborted();
|
|
visited.add(source.scopeDigest);
|
|
const run = await sourceRun(db, source).catch(error => {
|
|
if (error instanceof Error && error.message === "native_harness_migration_binding_mismatch") return null;
|
|
throw error;
|
|
});
|
|
if (!run) return "missing_run";
|
|
const [current] = await db.select().from(nativeHarnessBackups).where(and(eq(nativeHarnessBackups.companyId, run.companyId),
|
|
eq(nativeHarnessBackups.scopeDigest, source.scopeDigest), eq(nativeHarnessBackups.status, "current")));
|
|
if (current) {
|
|
if (current.manifestSha256 !== source.manifestSha256) return "durable_source_changed";
|
|
await verifyDurableNativeHarnessBackup({ db, storage: input.storage, companyId: run.companyId,
|
|
scopeDigest: source.scopeDigest, manifestSha256: source.manifestSha256, signal });
|
|
return "current";
|
|
}
|
|
if (run.status !== "succeeded" || !run.finishedAt) {
|
|
if (!run.finishedAt || !["failed", "cancelled", "interrupted", "timed_out"].includes(run.status)) {
|
|
return ["queued", "scheduled_retry", "running"].includes(run.status) ? "source_run_active" : "source_run_unfinished";
|
|
}
|
|
const providerLeaseId = await verifiedSourceLease(db, source, run, signal);
|
|
if (!providerLeaseId) return "source_unverified";
|
|
try {
|
|
const { owner, reason } = await settledNativeHarnessBackupOwner(db, { companyId: run.companyId, issueId: run.issueId,
|
|
runId: source.runId, sessionId: run.sessionId, runnerInstanceId: run.runnerId, scopeDigest: source.scopeDigest,
|
|
root: source.root, manifestSha256: source.manifestSha256, providerLeaseId, signal });
|
|
if (!owner) return `source_${reason}`;
|
|
await persistNativeHarnessBackup({ db, storage: input.storage, owner, root: source.root,
|
|
scopeDigest: source.scopeDigest, signal, maxArchiveBytes: source.archiveLimit });
|
|
return "published";
|
|
} catch (error) {
|
|
signal.throwIfAborted();
|
|
if (error instanceof NativeHarnessBackupSourceError || error instanceof Error && ["native_harness_preservation_source_unsettled",
|
|
"native_harness_archive_invalid"].includes(error.message)
|
|
|| ((error as { code?: string; cause?: { code?: string } })?.code
|
|
?? (error as { cause?: { code?: string } })?.cause?.code) === "55P03") {
|
|
return deferSource(source.scopeDigest, error instanceof Error ? error.message : "native_harness_source_busy", "source_changed");
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
const [coordinator] = await db.select().from(nativeRunFinalizations).where(and(
|
|
eq(nativeRunFinalizations.companyId, run.companyId), eq(nativeRunFinalizations.issueId, run.issueId),
|
|
eq(nativeRunFinalizations.runId, source.runId)));
|
|
if (coordinator?.phase !== "committed" || coordinator.leaseOwner || !coordinator.resultId || !coordinator.controllerBootId) return "owner_not_released";
|
|
const [task] = await db.select({ executionRunId: issues.executionRunId, checkoutRunId: issues.checkoutRunId }).from(issues)
|
|
.where(and(eq(issues.companyId, run.companyId), eq(issues.id, run.issueId)));
|
|
const [active] = await db.select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(and(
|
|
eq(heartbeatRuns.companyId, run.companyId), inArray(heartbeatRuns.status, ["queued", "running"]),
|
|
or(eq(heartbeatRuns.nativeIssueId, run.issueId), eq(heartbeatRuns.issueId, run.issueId), eq(heartbeatRuns.nativeSessionId, run.sessionId)))).limit(1);
|
|
if (!task || task.executionRunId || task.checkoutRunId || active) return "source_active";
|
|
const providerLeaseId = await verifiedSourceLease(db, source, run, signal);
|
|
if (!providerLeaseId) return "source_unverified";
|
|
try {
|
|
const owner = await nativeHarnessBackupOwner(db, { companyId: run.companyId, issueId: run.issueId,
|
|
runId: source.runId, bootId: coordinator.controllerBootId });
|
|
await persistNativeHarnessBackup({ db, storage: input.storage, root: source.root, scopeDigest: source.scopeDigest,
|
|
owner, signal, maxArchiveBytes: source.archiveLimit,
|
|
inactiveSource: { normalizedSessionId: run.sessionId, runnerInstanceId: run.runnerId, providerLeaseId } });
|
|
return "published";
|
|
} catch (error) {
|
|
signal.throwIfAborted();
|
|
if (error instanceof NativeHarnessBackupSourceError || error instanceof Error && ["native_runner_checkpoint_owner_lost", "native_harness_storage_owner_missing",
|
|
"native_harness_storage_prestage_source_active", "native_harness_storage_prestage_source_changed",
|
|
"native_harness_storage_owner_superseded", "native_harness_archive_invalid"].includes(error.message)) {
|
|
return deferSource(source.scopeDigest, error.message, "source_changed");
|
|
}
|
|
throw error;
|
|
}
|
|
});
|
|
const absent: Record<string, number> = {};
|
|
let gone = 0;
|
|
for await (const scope of localNativeHarnessObligationScopes(db, true, input.signal, visited)) {
|
|
input.signal.throwIfAborted();
|
|
if (visited.has(scope.scopeDigest)) continue;
|
|
visited.add(scope.scopeDigest);
|
|
const outcome = await recordNativeHarnessBackupSourceAbsence(db, { ...scope, signal: input.signal });
|
|
input.onOutcome?.(outcome);
|
|
if (outcome === "source_gone_recorded") gone++;
|
|
else absent[outcome] = (absent[outcome] ?? 0) + 1;
|
|
}
|
|
const counts = { inspected: outcomes.length, published: 0, current: 0, ...(gone ? { sourceGone: gone } : {}), deferred: absent };
|
|
for (const outcome of outcomes) {
|
|
if (outcome === "published" || outcome === "current") counts[outcome]++;
|
|
else counts.deferred[outcome] = (counts.deferred[outcome] ?? 0) + 1;
|
|
}
|
|
return counts;
|
|
}
|