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): acquire spawningTasks guard before first await

[._.]/ Adam Eivy57419a64162b
diff --git a/server/services/agentLifecycle.js b/server/services/agentLifecycle.js
index 866208504b..8919e16d8a 100644
--- a/server/services/agentLifecycle.js
+++ b/server/services/agentLifecycle.js
@@ -119,6 +119,14 @@ export async function spawnAgentForTask(task) {
return null;
}
+ // Acquire the in-process dedup guard SYNCHRONOUSLY — before any `await` below.
+ // A `task:ready` re-emit for the same task id can land while this call is
+ // suspended at an await (e.g. `ensureInstanceId()` / `getTaskById()`), and
+ // EventEmitter does not serialize async listeners, so a guard taken after an
+ // await would let a second call slip past the `has()` check above and spawn a
+ // duplicate agent (codex review). Every early `return null` below releases it.
+ spawningTasks.add(task.id);
+
// Cross-instance claim guard (issue #1563, acceptance criterion 2). When this
// task list is shared with a federated peer (full-sync mode, #1561), the peer
// may already be working this task. Refuse to spawn while another instance
@@ -130,6 +138,7 @@ export async function spawnAgentForTask(task) {
// re-claiming our own task on retry/resume.
const instanceId = await ensureInstanceId();
if (!isClaimableBy(task.metadata, instanceId)) {
+ spawningTasks.delete(task.id);
console.log(`🔒 Task ${task.id} is claimed by instance ${getClaimOwner(task.metadata)} (live lease) — skipping spawn on ${instanceId}`);
return null;
}
@@ -137,6 +146,7 @@ export async function spawnAgentForTask(task) {
// Check total spawn count across all retry types to prevent runaway respawning
const totalSpawns = Number(task.metadata?.totalSpawnCount) || 0;
if (totalSpawns >= MAX_TOTAL_SPAWNS) {
+ spawningTasks.delete(task.id);
console.log(`🚫 Task ${task.id} hit max total spawns (${totalSpawns}/${MAX_TOTAL_SPAWNS}), blocking`);
await updateTask(task.id, {
status: 'blocked',
@@ -154,8 +164,6 @@ export async function spawnAgentForTask(task) {
return null;
}
- spawningTasks.add(task.id);
-
const agentId = `agent-${uuidv4().slice(0, 8)}`;
// Tag agent with execution lane (priority/observability only — concurrency
diff --git a/server/services/agentLifecycle.test.js b/server/services/agentLifecycle.test.js
index 50e2ce8db2..1b2bdcb7f8 100644
--- a/server/services/agentLifecycle.test.js
+++ b/server/services/agentLifecycle.test.js
@@ -712,6 +712,21 @@ describe('agentLifecycle — instance provenance stamping (#1563)', () => {
expect(recheckIdx, 'must re-check claimability against the fresh metadata').toBeGreaterThan(rereadIdx);
});
+ it('source: acquires the spawningTasks dedup guard before the first await', () => {
+ // The guard must be taken synchronously (before `await ensureInstanceId()`
+ // or `await getTaskById()`), or a concurrent task:ready re-emit could slip
+ // past the has() check while this call is suspended and spawn a duplicate.
+ const fnStart = AGENT_LIFECYCLE_SRC.indexOf('export async function spawnAgentForTask');
+ const fnBody = AGENT_LIFECYCLE_SRC.slice(fnStart, fnStart + 60_000);
+ const addIdx = fnBody.indexOf('spawningTasks.add(task.id)');
+ // The first awaited CALL in the function (anchor on the real statement, not
+ // the word "await" in a comment) is the identity resolution.
+ const firstAwaitIdx = fnBody.indexOf('await ensureInstanceId()');
+ expect(addIdx, 'spawningTasks.add must exist').toBeGreaterThan(-1);
+ expect(firstAwaitIdx, 'await ensureInstanceId() must exist').toBeGreaterThan(-1);
+ expect(addIdx, 'the dedup guard must be acquired BEFORE the first await').toBeLessThan(firstAwaitIdx);
+ });
+
it('source: releases the claim on a failed-setup early exit (cleanupOnError)', () => {
const fnStart = AGENT_LIFECYCLE_SRC.indexOf('const cleanupOnError = async');
const fnBody = AGENT_LIFECYCLE_SRC.slice(fnStart, fnStart + 1200);