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

address review (codex): block permanent provider failure before releasing the lease

[._.]/ Adam Eivy64cc0bebeb11
diff --git a/server/services/agentLifecycle.js b/server/services/agentLifecycle.js
index e313a5f088..c39a96ccfb 100644
--- a/server/services/agentLifecycle.js
+++ b/server/services/agentLifecycle.js
@@ -293,15 +293,21 @@ export async function spawnAgentForTask(task) {
// spawn-local guard/lane/execution state lives.
const resolution = await resolveAgentProviderAndModel(task);
if (!resolution.ok) {
- await cleanupOnError(resolution.error);
// A PERMANENT provider-config failure (e.g. an `api`-only provider pinned
// to an agent task, which has no file-writing harness) fails identically on
// every re-dispatch. Leaving the task pending makes it silently re-fail
// forever AND wedge its app's single improvement slot. Block it with an
// actionable reason so it stops re-dispatching and surfaces in the UI.
- // updateTask strips the federation claim on any non-`in_progress` status
- // change, so the block also frees the lease. Transient (non-permanent)
- // failures fall through and stay pending to retry.
+ //
+ // Do this BEFORE cleanupOnError releases the federation lease, and while we
+ // still hold it: the block write itself frees the lease (updateTask strips
+ // the claim on any non-`in_progress` status change), so the terminal
+ // transition + lease release are one atomic write. Blocking AFTER the
+ // release would open a window where a federated peer could claim + start the
+ // task (e.g. with a working CLI provider) and then have its live
+ // `in_progress` record clobbered to `blocked` — which outranks `in_progress`
+ // in the claim-aware merge. Transient failures fall through, skip the block,
+ // and stay pending to retry.
if (resolution.permanent) {
await updateTask(task.id, {
status: 'blocked',
@@ -313,6 +319,7 @@ export async function spawnAgentForTask(task) {
}
}, task.taskType || 'user').catch(() => {});
}
+ await cleanupOnError(resolution.error);
cosEvents.emit('agent:error', {
taskId: task.id,
error: resolution.error,
diff --git a/server/services/agentLifecycle.test.js b/server/services/agentLifecycle.test.js
index a0557aed7b..07446a98b0 100644
--- a/server/services/agentLifecycle.test.js
+++ b/server/services/agentLifecycle.test.js
@@ -391,11 +391,26 @@ describe('agentLifecycle source — permanent provider-config failure blocks the
it('the resolution-failure path blocks a permanent failure with a provider-config reason', () => {
const idx = AGENT_LIFECYCLE_SRC.indexOf('const resolution = await resolveAgentProviderAndModel(task)');
expect(idx, 'resolution call must exist').toBeGreaterThan(-1);
- const body = AGENT_LIFECYCLE_SRC.slice(idx, idx + 1200);
+ const body = AGENT_LIFECYCLE_SRC.slice(idx, idx + 2000);
expect(body, 'gates the block on resolution.permanent').toMatch(/if\s*\(resolution\.permanent\)/);
expect(body, 'flips the task to blocked').toMatch(/status:\s*'blocked'/);
expect(body, 'tags the block category').toMatch(/blockedCategory:\s*'provider-config'/);
});
+
+ it('blocks BEFORE releasing the lease so a federated peer cannot be clobbered', () => {
+ // The block write must run while the instance still holds the federation
+ // lease and land BEFORE cleanupOnError releases it — otherwise a peer could
+ // claim + start the task in the gap and have its live in_progress clobbered
+ // to blocked. Pin the ordering: `if (resolution.permanent)` precedes the
+ // `await cleanupOnError` inside the resolution-failure branch.
+ const idx = AGENT_LIFECYCLE_SRC.indexOf('const resolution = await resolveAgentProviderAndModel(task)');
+ const body = AGENT_LIFECYCLE_SRC.slice(idx, idx + 2000);
+ const permanentIdx = body.indexOf('if (resolution.permanent)');
+ const cleanupIdx = body.indexOf('await cleanupOnError(resolution.error)');
+ expect(permanentIdx, 'permanent block must exist').toBeGreaterThan(-1);
+ expect(cleanupIdx, 'cleanupOnError must exist').toBeGreaterThan(-1);
+ expect(permanentIdx, 'block must precede the lease release').toBeLessThan(cleanupIdx);
+ });
});
// ─── spawnAgentForTask — cleanupOnError ────────────────────────────────────