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

write the re-planned repair, and report a missing record without a spawn event (#4540)

[._.]/ Adam Eivy70cce237f818
diff --git a/server/lib/agentRunReconcile.js b/server/lib/agentRunReconcile.js
index a1756d1f57..238c1ed60f 100644
--- a/server/lib/agentRunReconcile.js
+++ b/server/lib/agentRunReconcile.js
@@ -83,10 +83,14 @@ export function diffRunRecord(projection, record) {
if (!projection || isAgentFallbackKey(projection.id)) return null;
if (!record) {
- // Only worth reporting once the ledger believes the run actually started.
- // A projection built solely from an annotation event (a PR verdict that
- // outlived its run) has no missing record to speak of.
- if (!projection.startedAt) return null;
+ // Reported for any projection that has an opinion about the run's state —
+ // NOT only one that saw the spawn. Retention can age out a `run.spawned`
+ // while the `run.finalized` that followed it is still in the ledger, and a
+ // finalized run with no record on disk is exactly the drift worth naming. A
+ // projection built solely from an annotation event (a PR verdict that
+ // outlived its run) folds to `unknown` and is skipped — it makes no claim
+ // that a record should exist.
+ if (projection.status === 'unknown') return null;
return finding('record-missing', false, { ledgerStatus: projection.status });
}
diff --git a/server/lib/agentRunReconcile.test.js b/server/lib/agentRunReconcile.test.js
index 9b71fc7ed5..52b13b6dad 100644
--- a/server/lib/agentRunReconcile.test.js
+++ b/server/lib/agentRunReconcile.test.js
@@ -56,8 +56,19 @@ describe('diffRunRecord', () => {
expect(diff).toMatchObject({ finding: 'record-missing', repairable: false });
});
- it('does not report a missing record for a projection that never saw a spawn', () => {
+ it('reports a missing record even when the spawn event has aged out', () => {
+ // Retention drops the oldest events first, so a run whose `run.spawned` is
+ // gone but whose `run.finalized` is still in the ledger is the NORMAL shape
+ // of an old run — and a finalized run with no record on disk is exactly the
+ // drift worth naming.
+ const finalizeOnly = one([finalized(true)]);
+ expect(finalizeOnly.startedAt).toBeNull();
+ expect(diffRunRecord(finalizeOnly, null)).toMatchObject({ finding: 'record-missing' });
+ });
+
+ it('does not report a missing record for a projection with no status opinion', () => {
const annotationOnly = one([{ kind: 'run.pr-verified', runId: 'r1', at: AT, data: { verified: true } }]);
+ expect(annotationOnly.status).toBe('unknown');
expect(diffRunRecord(annotationOnly, null)).toBeNull();
});
diff --git a/server/services/agentRunReconciler.js b/server/services/agentRunReconciler.js
index 6b1e6304b5..2f4dc7c5c6 100644
--- a/server/services/agentRunReconciler.js
+++ b/server/services/agentRunReconciler.js
@@ -192,15 +192,21 @@ async function repairNow({ runId, limit } = {}) {
// appended event is harmless (a real `run.finalized` outranks it in the
// fold, and its key dedupes a later retry).
const fresh = await readRecord(item.runId);
- if (!planRunRecordRepair(projection, fresh, patch.reconciledAt)) {
+ // Re-planned against the fresh record and the SAME instant, so the plan's
+ // `endTime` — and therefore the event key already in the ledger — is
+ // unchanged, while any field the plan reads off the record (a partial error
+ // the run managed to write) comes from the current copy rather than the
+ // stale one.
+ const finalPatch = planRunRecordRepair(projection, fresh, patch.reconciledAt);
+ if (!finalPatch) {
skipped += 1;
continue;
}
- await atomicWrite(path, { ...fresh, ...patch });
- console.log(`🩹 Closed run ${item.runId} from the event ledger: ledger read ${item.detail.ledgerStatus}, record now ${patch.success ? 'success' : 'failure'}`);
+ await atomicWrite(path, { ...fresh, ...finalPatch });
+ console.log(`🩹 Closed run ${item.runId} from the event ledger: ledger read ${item.detail.ledgerStatus}, record now ${finalPatch.success ? 'success' : 'failure'}`);
- repaired.push({ runId: item.runId, from: item.detail.ledgerStatus, success: patch.success, endTime: patch.endTime });
+ repaired.push({ runId: item.runId, from: item.detail.ledgerStatus, success: finalPatch.success, endTime: finalPatch.endTime });
}
return { checkedAt: new Date().toISOString(), repaired, skipped, ...report };