Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/term-commands/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,11 @@ process.on('SIGINT', () => { server.close(); process.exit(0); });
// Helper: Generate Worker ID (teams)
// ============================================================================

async function generateWorkerId(team: string, role?: string): Promise<string> {
const base = role ? `${team}-${role}` : team;
const existing = await registry.list();
if (!existing.some((w) => w.id === base)) return base;

// Use crypto.randomUUID() for the suffix to avoid race conditions
const suffix = crypto.randomUUID().slice(0, 8);
return `${base}-${suffix}`;
async function generateWorkerId(_team: string, _role?: string): Promise<string> {
// Always return a UUID. agents.id constraint (migration 061) requires
// UUID shape or 'dir:%' prefix. Legacy team-role-suffix shape failed
// agents_id_shape_check on every new spawn after that migration.
return crypto.randomUUID();
}
Comment on lines +446 to 451

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Returning a fresh crypto.randomUUID() here introduces a regression in idempotency and a high risk of database errors due to unique constraint violations.

  1. Unique Constraint Violation: In handleWorkerSpawn, registry.findOrCreateAgent is called at line 2621. This function ensures an agent identity exists for the given (custom_name, team) and returns its UUID id. If generateWorkerId then returns a different random UUID, the subsequent registry.register call (line 665, via ctx.workerId) will attempt to insert a new row with the same custom_name and team but a different primary key id. This will trigger a unique constraint violation on the (custom_name, team) composite key.
  2. Loss of Stability: The previous implementation produced a stable ID based on team and role. Using a random UUID means every spawn attempt for the same role will try to create a new agent record instead of updating the existing one.
  3. Incomplete Fix: This change only addresses the legacy path. The identity.workerId path in handleWorkerSpawn (line 2641) still uses resolveSpawnIdentity, which returns non-UUID strings (e.g., engineer or engineer-abcd). Standard spawns will likely still fail the agents_id_shape_check constraint.

To fix this properly while staying within the current architecture, this function should look up the existing agent's UUID from the registry.

async function generateWorkerId(team: string, role?: string): Promise<string> {
  // Always return a UUID. agents.id constraint (migration 061) requires
  // UUID shape or 'dir:%' prefix. We look up the existing agent identity
  // to ensure we use its canonical UUID and avoid unique constraint violations
  // on (custom_name, team) during registration.
  const name = role || team;
  const existing = await registry.getAgentByName(name, team);
  if (existing) return existing.id;

  return crypto.randomUUID();
}


// ============================================================================
Expand Down
Loading