Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: keep checkout preview hashing outside the ownership queue (#13424)

[._.]/ Adam Eivyb5bcd9198731
diff --git a/server/services/heldCheckoutRecovery.js b/server/services/heldCheckoutRecovery.js
index ea344e1559..cdc5c6cecb 100644
--- a/server/services/heldCheckoutRecovery.js
+++ b/server/services/heldCheckoutRecovery.js
@@ -638,7 +638,7 @@ const reviewForgeOptions = (release, kind, opts) => ({ forgeAccount: opts.forgeA
...(!release.branch && { exactAliasHead: release.head }),
...(kind === 'source-supersession' && { sourceSupersessionHead: release.head }) });
-async function reviewCurrentCheckout({ repoPath, release, prUrl, kind, unlocked = false }, opts = {}) {
+async function matchReviewTarget({ repoPath, release, kind, unlocked = false }, opts) {
if (kind && kind !== 'source-supersession') return { ok: false, reason: 'retirement-review-kind-unavailable' };
if (kind && !release.branch) return { ok: false, reason: 'retirement-source-branch-required' };
if (PROTECTED_BRANCHES.includes(release.branch)) return { ok: false, reason: 'protected-branch' };
@@ -647,7 +647,14 @@ async function reviewCurrentCheckout({ repoPath, release, prUrl, kind, unlocked
const owner = await ownerReason({ repoPath, tree: matched.tree, branch: release.branch,
readAgents: opts.readAgents || readClaimOwnershipAgents, reviewed: true });
if (owner) return { ok: false, reason: owner };
- const { buildCheckoutRetirementReview, readRetirementForgeEvidence } = await import('./heldCheckoutReview.js');
+ return { ok: true, tree: matched.tree };
+}
+
+async function readCheckoutReviewPrerequisites(input, opts) {
+ const { repoPath, release, prUrl, kind } = input;
+ const matched = await matchReviewTarget(input, opts);
+ if (!matched.ok) return matched;
+ const { readRetirementForgeEvidence } = await import('./heldCheckoutReview.js');
const forge = await (opts.readForge || readRetirementForgeEvidence)(repoPath, release.branch, prUrl, reviewForgeOptions(release, kind, opts))
.catch(error => ({ error }));
if (forge?.error) return { ok: false, reason: /^retirement-[a-z-]+$/.test(forge.error.message) ? forge.error.message : 'retirement-forge-unavailable' };
@@ -665,13 +672,67 @@ async function reviewCurrentCheckout({ repoPath, release, prUrl, kind, unlocked
}
const policy = await retirementPolicy(repoPath, policyBranch, opts, identity);
if (policy.reason) return { ok: false, reason: policy.reason };
- return archiveSnapshots(matched.tree, repoPath, null, archiveWork(opts.signal))
+ return { ok: true, tree: matched.tree, forge, policy };
+}
+
+async function buildCurrentCheckoutReview({ repoPath, release, prUrl, kind }, { tree, forge, policy }, opts) {
+ const { buildCheckoutRetirementReview } = await import('./heldCheckoutReview.js');
+ return archiveSnapshots(tree, repoPath, null, archiveWork(opts.signal))
.then(([content, administration]) => buildCheckoutRetirementReview({ repoPath, release, prUrl, kind,
contentFingerprint: content.fingerprint, administrationFingerprint: administration.fingerprint, policy }, { ...opts, readForge: async () => forge }))
.then(review => ({ ok: true, review }), error => archiveLimitRefusal(error) || ({ ok: false,
reason: /^retirement-[a-z-]+$/.test(error.message) ? error.message : 'retirement-local-evidence-unavailable' }));
}
+async function reviewCurrentCheckout(input, opts = {}) {
+ const prerequisites = await readCheckoutReviewPrerequisites(input, opts);
+ return prerequisites.ok ? buildCurrentCheckoutReview(input, prerequisites, opts) : prerequisites;
+}
+
+async function previewCheckoutIdentity(path) {
+ const pointer = await readRetirementCheckoutIdentity(path);
+ if (!pointer) return null;
+ const gitdir = await realpath((await execGit(['rev-parse', '--absolute-git-dir'], path)).stdout.trim());
+ const info = await lstat(gitdir, { bigint: true });
+ if (!info.isDirectory() || await readRetirementCheckoutIdentity(path) !== pointer) return null;
+ return JSON.stringify([pointer, gitdir, String(info.dev), String(info.ino), String(info.birthtimeNs)]);
+}
+
+/** Preview is evidence, never a retirement reservation or mutation grant. Keep
+ * forge calls, hashing and diff construction outside the process-wide queue. */
+async function previewCurrentCheckout(input, opts) {
+ const matched = await withClaimOwnershipLock(() => matchReviewTarget(input, opts));
+ if (!matched.ok) return matched;
+ const { repoPath, release, prUrl, kind } = input;
+ const { readRetirementForgeEvidence } = await import('./heldCheckoutReview.js');
+ const forge = await (opts.readForge || readRetirementForgeEvidence)(repoPath, release.branch, prUrl,
+ reviewForgeOptions(release, kind, opts)).catch(error => ({ error }));
+ const localOptions = { ...opts, readForge: async () => forge };
+ const captured = await withClaimOwnershipLock(async () => {
+ const prerequisites = await readCheckoutReviewPrerequisites(input, localOptions);
+ if (!prerequisites.ok) return prerequisites;
+ const identity = await previewCheckoutIdentity(release.path).catch(() => null);
+ return identity && (!release.checkoutIdentity || await readRetirementCheckoutIdentity(release.path) === release.checkoutIdentity)
+ ? { ...prerequisites, identity }
+ : { ok: false, reason: 'checkout-identity-changed' };
+ });
+ if (!captured.ok) return captured;
+ const prepared = await buildCurrentCheckoutReview(input, captured, localOptions);
+ if (!prepared.ok) return prepared;
+ return withClaimOwnershipLock(async () => {
+ // Bind even legacy inventory rows to the generation captured before hashing.
+ const current = await readCheckoutReviewPrerequisites(input, localOptions);
+ if (!current.ok) return current;
+ if (await previewCheckoutIdentity(release.path).catch(() => null) !== captured.identity) {
+ return { ok: false, reason: 'checkout-identity-changed' };
+ }
+ if (JSON.stringify(current.policy) !== JSON.stringify(captured.policy)) {
+ return { ok: false, reason: 'retirement-review-changed' };
+ }
+ return prepared;
+ });
+}
+
/** Host-control-gated preview of one inventory row; may fetch missing proof objects. */
export async function prepareInventoriedCheckoutRetirement({ appId, release, prUrl, kind }, opts = {}) {
const [{ getAppById }, { getBranchReconcileOutcome }] = await Promise.all([import('./apps.js'), import('./taskSchedule.js')]);
@@ -681,7 +742,7 @@ export async function prepareInventoriedCheckoutRetirement({ appId, release, prU
release = bindInventoriedRelease(outcome, release);
if (!release) return { ok: false, reason: 'release-not-in-inventory' };
const reviewOptions = { ...opts, forgeAccount: app.forgeAccount ?? null };
- const review = () => withClaimOwnershipLock(() => reviewCurrentCheckout({ repoPath: app.repoPath, release, prUrl, kind }, reviewOptions));
+ const review = () => previewCurrentCheckout({ repoPath: app.repoPath, release, prUrl, kind }, reviewOptions);
let prepared = await review();
if (prepared.reason === 'retirement-local-objects-missing') {
// A remote default may advance while local refs stay put. Acquire only its
diff --git a/server/services/heldCheckoutRecovery.test.js b/server/services/heldCheckoutRecovery.test.js
index fc9529a950..67b8a4f5e8 100644
--- a/server/services/heldCheckoutRecovery.test.js
+++ b/server/services/heldCheckoutRecovery.test.js
@@ -30,6 +30,7 @@ const injected = vi.hoisted(() => ({ policies: [], schedule: null, app: null, fe
vi.mock('../lib/execGit.js', async importOriginal => {
const actual = await importOriginal();
return { ...actual, execGit: async (args, cwd, options) => {
+ if (injected.onSnapshot && args[0] === 'ls-files' && args[1] === '-u') await injected.onSnapshot();
// Pauses an archive mid-write, after its first snapshot and before its re-read.
if (injected.onBundle && args.includes('bundle') && args.includes('create')) await injected.onBundle();
if (injected.onUnlockedStatus && args[0] === 'status' && args[1] === '--porcelain') {
@@ -45,6 +46,7 @@ vi.mock('../lib/execGit.js', async importOriginal => {
}
try {
const result = await actual.execGit(args, cwd, options);
+ if (injected.onPreviewEvidence && args[0] === '--literal-pathspecs' && args[1] === 'diff') await injected.onPreviewEvidence();
if (injected.onArchiveRef && args[0] === 'update-ref' && args[1]?.startsWith('refs/portos/archive/')) await injected.onArchiveRef();
if (injected.onProof && args.includes('--bare') && options?.env?.GIT_NO_REPLACE_OBJECTS === '1') await injected.onProof();
return result;
@@ -95,7 +97,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()
beforeEach(async () => {
Object.assign(retirementFaults, { onRemove: null, onBranchDelete: null, scheduleWrites: 0, failScheduleWrite: null });
- Object.assign(injected, { policies: [], schedule: { tasks: {}, executions: {} }, app: { id: 'app-1', repoPath: null }, fetchRemote: null, fetches: [], onBundle: null, onArchiveRef: null, onEvidence: null, onProof: null, proofFailures: [], onUnlockedStatus: null, statusReads: 0 });
+ Object.assign(injected, { policies: [], schedule: { tasks: {}, executions: {} }, app: { id: 'app-1', repoPath: null }, fetchRemote: null, fetches: [], onBundle: null, onArchiveRef: null, onEvidence: null, onProof: null, proofFailures: [], onUnlockedStatus: null, statusReads: 0, onSnapshot: null, onPreviewEvidence: null });
scratch = realpathSync(await mkdtemp(join(tmpdir(), 'portos-held-checkout-')));
const safeRepo = join(scratch, 'repo');
await mkdir(safeRepo);
@@ -1159,6 +1161,84 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()
return { path, release, admin, opts, forge, prepare, confirm };
}
+ it.each(['snapshot', 'forge'])('keeps unrelated ownership admission available while preview %s work is pending', async phase => {
+ const c = await reviewedCheckout(`preview-queue-${phase}`);
+ let paused;
+ let resume;
+ const entered = new Promise(resolve => { paused = resolve; });
+ const pending = new Promise(resolve => { resume = resolve; });
+ const pause = async () => { paused(); await pending; };
+ if (phase === 'snapshot') injected.onSnapshot = pause;
+ else c.opts.readForge = async () => { await pause(); return c.forge; };
+ const preparing = c.prepare();
+ try {
+ await entered;
+ expect(await withClaimOwnershipLock(() => 'settled')).toBe('settled');
+ expect(await checkReconcileOwnership({ appId: 'app-1', branch: 'feature/unrelated' }, {
+ getAgents: async () => [], getActiveAgentIds: () => [], readCompletionHold: async () => null,
+ })).toEqual({ admitted: true });
+ } finally {
+ resume();
+ }
+ expect(await preparing).toMatchObject({ ok: true });
+ expect(await registered(c.path)).toBe(true);
+ expect(await lockText(c.path)).toBe(c.release.lockReason);
+ expect(await archives()).toEqual([]);
+ });
+
+ it.each([
+ ['owner', 'claim-owner-active'],
+ ['unreadable-owner', 'claim-ownership-unreadable'],
+ ['handoff', 'pr-completion-human-handoff'],
+ ['policy', 'retirement-review-changed'],
+ ['head', 'checkout-head-changed'],
+ ['lock', 'checkout-lock-changed'],
+ ['pointer', 'checkout-identity-changed'],
+ ['administration', 'checkout-identity-changed'],
+ ])('refuses preview when %s changes during unlocked evidence construction', async (change, reason) => {
+ const c = await reviewedCheckout(`preview-race-${change}`);
+ let owners = [];
+ c.opts.readAgents = async () => owners;
+ injected.onPreviewEvidence = async () => {
+ injected.onPreviewEvidence = null;
+ if (change === 'owner') owners = [{ id: 'agent-live', status: 'running', workspacePath: c.path }];
+ if (change === 'unreadable-owner') owners = null;
+ if (change === 'handoff') injected.policies[0].metadata.prCompletionPolicy = 'leave-open';
+ if (change === 'policy') injected.policies[0].id = 'different-completed-agent';
+ if (change === 'head') await execGit(['commit', '--allow-empty', '-m', 'new work during preview'], c.path);
+ if (change === 'lock') await writeFile(join(c.admin, 'locked'), 'new owner lock');
+ if (change === 'pointer') {
+ const pointer = join(c.path, '.git');
+ const bytes = await readFile(pointer);
+ await rename(pointer, join(scratch, 'original-pointer'));
+ await writeFile(pointer, bytes);
+ }
+ if (change === 'administration') {
+ const original = join(scratch, 'original-administration');
+ await rename(c.admin, original);
+ await cp(original, c.admin, { recursive: true });
+ }
+ };
+ expect(await c.prepare()).toMatchObject({ ok: false, reason });
+ expect(await registered(c.path)).toBe(true);
+ expect(await readFile(join(c.path, 'ignored.txt'), 'utf8')).toBe('ignored work\n');
+ expect(await archives()).toEqual([]);
+ });
+
+ it('rebuilds content evidence at confirmation after an unlocked preview observes older bytes', async () => {
+ const c = await reviewedCheckout('preview-content-change');
+ injected.onPreviewEvidence = async () => {
+ injected.onPreviewEvidence = null;
+ await writeFile(join(c.path, 'ignored.txt'), 'changed during preview\n');
+ };
+ const prepared = await c.prepare();
+ expect(prepared.ok).toBe(true);
+ expect(await c.confirm(prepared.review)).toMatchObject({ ok: false, reason: 'retirement-review-changed' });
+ expect(await readFile(join(c.path, 'ignored.txt'), 'utf8')).toBe('changed during preview\n');
+ expect(await lockText(c.path)).toBe(c.release.lockReason);
+ expect(await archives()).toEqual([]);
+ });
+
it.each(['unrelated-upstream', 'changed-delivery'])('uses exact merged PR file scope after inherited upstream history: %s', async scenario => {
const c = await reviewedCheckout(`scope-${scenario}`, { normalMerge: true });
await writeFile(join(repo, 'upstream.txt'), 'inherited upstream\n');