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

reject stale-filing verdicts + stamp post-restart terminal failures (#2779)

[._.]/ Adam Eivy0fa8df1fd6b6
diff --git a/server/services/agentLifecycle.js b/server/services/agentLifecycle.js
index 971cd2ce6b..13a1aba9a0 100644
--- a/server/services/agentLifecycle.js
+++ b/server/services/agentLifecycle.js
@@ -1331,6 +1331,18 @@ export async function handleAgentCompletion(agentId, exitCode, success, duration
// Import handleOrphanedTask dynamically to avoid circular dep with agentManagement
const { handleOrphanedTask } = await import('./agentManagement.js');
await handleOrphanedTask(cosAgent.taskId, agentId, getTaskById);
+ // If orphan recovery settled the task into a terminal `blocked` state (retry budget
+ // exhausted), the local completion already recorded the proposal failure — so stamp
+ // the LI failure verdict here too (#2779, codex P2) or the originating peer would
+ // receive a terminal task with no verdict. A revived (pending) task carries no
+ // settled outcome yet, so it is intentionally left unstamped until it re-completes.
+ const settled = await getTaskById(cosAgent.taskId).catch(() => null);
+ if (settled && settled.status === 'blocked' && settled.metadata?.liProposal) {
+ const stamp = await stampLiExecutionVerdict({}, settled, { success: false });
+ if (stamp.metadata) {
+ await updateTask(cosAgent.taskId, stamp, settled.taskType || 'user').catch(() => {});
+ }
+ }
}
}
}
diff --git a/server/services/agentLifecycle.test.js b/server/services/agentLifecycle.test.js
index 255480c24e..7a0b4ea592 100644
--- a/server/services/agentLifecycle.test.js
+++ b/server/services/agentLifecycle.test.js
@@ -270,8 +270,13 @@ describe('agentLifecycle — guard wiring', () => {
// finalizeAgent — it must still stamp so the outcome federates to the originating peer.
const idx = AGENT_LIFECYCLE_SRC.indexOf('Completing untracked agent');
expect(idx, 'post-restart recovery branch must exist').toBeGreaterThan(-1);
- const body = AGENT_LIFECYCLE_SRC.slice(idx, idx + 1200);
+ const body = AGENT_LIFECYCLE_SRC.slice(idx, idx + 2000);
+ // Success path stamps a clean completion…
expect(body).toMatch(/await stampLiExecutionVerdict\(\{ status: 'completed' \}, task, \{ success \}\)/);
+ // …and the FAILURE path re-reads the task after orphan recovery and stamps the failure
+ // verdict when recovery settled it into terminal `blocked` (codex P2 round 2).
+ expect(body).toMatch(/settled\.status === 'blocked' && settled\.metadata\?\.liProposal/);
+ expect(body).toMatch(/await stampLiExecutionVerdict\(\{\}, settled, \{ success: false \}\)/);
});
});
diff --git a/server/services/layeredIntelligenceHandoffFederation.test.js b/server/services/layeredIntelligenceHandoffFederation.test.js
index d36a6b7716..5c3ca970ad 100644
--- a/server/services/layeredIntelligenceHandoffFederation.test.js
+++ b/server/services/layeredIntelligenceHandoffFederation.test.js
@@ -204,7 +204,7 @@ describe('recordProposalExecutionFromVerdict (#2779) — the originating peer de
});
it('is a durable no-op when the same verdict re-syncs (idempotency, codex P2)', async () => {
- await recordFiledProposal({ appId: 'app-x', slug: 'add-metrics', scope: 'refactor' }, store);
+ await recordFiledProposal({ appId: 'app-x', slug: 'add-metrics', scope: 'refactor', now: Date.parse('2026-07-20T08:00:00.000Z') }, store);
const verdict = { appId: 'app-x', slug: 'add-metrics', scope: 'refactor', success: true, executedAt: '2026-07-20T10:00:00.000Z' };
expect(await recordProposalExecutionFromVerdict(verdict, store)).toBe(true);
const [first] = await listOutcomes({ appId: 'app-x' }, store);
@@ -217,7 +217,7 @@ describe('recordProposalExecutionFromVerdict (#2779) — the originating peer de
});
it('overwrites with a genuinely NEWER (re-executed) verdict — latest wins (parity)', async () => {
- await recordFiledProposal({ appId: 'app-x', slug: 'add-metrics', scope: 'refactor' }, store);
+ await recordFiledProposal({ appId: 'app-x', slug: 'add-metrics', scope: 'refactor', now: Date.parse('2026-07-20T08:00:00.000Z') }, store);
// First hand-off failed on a peer.
expect(await recordProposalExecutionFromVerdict(
{ appId: 'app-x', slug: 'add-metrics', scope: 'refactor', success: false, errorCategory: 'test-failure', executedAt: '2026-07-20T10:00:00.000Z' },
@@ -232,6 +232,25 @@ describe('recordProposalExecutionFromVerdict (#2779) — the originating peer de
expect((await listOutcomes({ appId: 'app-x' }, store))[0].executionOutcome).toBe('success');
});
+ it('rejects a stale verdict from an EARLIER filing after a re-file (codex P2)', async () => {
+ // Proposal re-filed after the dedup window: recordFiledProposal stamps a fresh filedAt and
+ // clears execution state, but the old completed hand-off still rides the federated task list
+ // and is re-offered. A verdict whose execution predates the CURRENT filing must not be
+ // inherited by the new proposal (it belongs to the prior filing generation).
+ await recordFiledProposal(
+ { appId: 'app-x', slug: 'add-metrics', scope: 'refactor', now: Date.parse('2026-07-20T11:00:00.000Z') },
+ store
+ );
+ const staleVerdict = { appId: 'app-x', slug: 'add-metrics', scope: 'refactor', success: true, executedAt: '2026-07-20T09:00:00.000Z' };
+ expect(await recordProposalExecutionFromVerdict(staleVerdict, store)).toBe(false);
+ const [rec] = await listOutcomes({ appId: 'app-x' }, store);
+ expect(rec.executionOutcome).toBeNull(); // new proposal keeps a clean execution slate
+ // A verdict from the CURRENT filing generation (executed after the re-file) is still accepted.
+ const freshVerdict = { appId: 'app-x', slug: 'add-metrics', scope: 'refactor', success: true, executedAt: '2026-07-20T12:00:00.000Z' };
+ expect(await recordProposalExecutionFromVerdict(freshVerdict, store)).toBe(true);
+ expect((await listOutcomes({ appId: 'app-x' }, store))[0].executionOutcome).toBe('success');
+ });
+
it('records a cross-peer FAILURE so a failing domain surfaces to A', async () => {
await recordFiledProposal({ appId: 'app-x', slug: 'flaky-thing', scope: 'testing' }, store);
const verdict = buildLiExecutionVerdict({
diff --git a/server/services/layeredIntelligenceOutcomes.js b/server/services/layeredIntelligenceOutcomes.js
index 3c52fb49ae..89487751cc 100644
--- a/server/services/layeredIntelligenceOutcomes.js
+++ b/server/services/layeredIntelligenceOutcomes.js
@@ -256,6 +256,17 @@ export async function recordProposalExecution({ appId, slug, scope = null, succe
// (never filed it) has no record, so `requireExisting` makes it a no-op there
// instead of minting a stray minimal record on every full-sync peer.
if (!hasExisting && requireExisting) return false;
+ // Stale-filing-generation gate (#2779, codex P2): when a proposal is re-filed after the
+ // dedup window, recordFiledProposal stamps a fresh `filedAt` and clears the execution
+ // fields — but the OLD completed hand-off task still rides the federated task list and is
+ // re-offered on every merge. An execution can only legitimately happen AFTER its filing,
+ // so a verdict whose `executedAt` predates the current `filedAt` belongs to a prior filing
+ // generation and must NOT be inherited by the new proposal. The re-file gap is the 30-day
+ // dedup window, which dwarfs any cross-peer clock skew, so a strict `<` is safe here.
+ if (requireExisting && hasExisting && existing.filedAt) {
+ const filedMs = Date.parse(existing.filedAt);
+ if (Number.isFinite(filedMs) && Number.isFinite(executionAtMs) && executionAtMs < filedMs) return false;
+ }
// Durable idempotency for the federated consume (#2779, codex P2): the record's own
// executionOutcome/executionAt IS the consumed-marker, so a re-synced terminal task is
// a no-op WITHOUT relying on the in-memory status transition (which a crash between the