Skip to content

afx: reconcileTerminalSessions doesn't self-repair orphaned architect registry rows (inverse of #1150) #1176

Description

@amrmelsayed

Problem

Inverse of #1150. When Tower boot reconciliation finds a live type='architect' terminal_sessions row whose matching architects registry row is missing, it reconnects the terminal (correctly — the process is alive) but does NOT re-create the registry row. The architect appears to work for the current Tower lifetime, then silently fails in two ways:

  1. Doesn't survive afx workspace stop/start: launchInstance's reconciliation loop iterates getArchitects(workspacePath) to respawn siblings. No row → no respawn → architect disappears on next start with no warning.
  2. Loses conversation on in-process crash: session-manager.ts's auto-restart bakes --resume <session_id> from the architect row. No row → no stored session id → fresh spawn without resume → new conversation, prior context lost.

Both failures are silent to the user — the architect worked, then didn't, with no diagnostic pointing at the missing registry row.

How the state gets orphaned:

Origin

Follow-up filed by the pir-1150 builder during its dev-approval discussion. #1150 fixes the "row survives, terminal doesn't → zombie respawn from stale row" direction. This issue fixes the mirror direction: "terminal survives, row doesn't → silent loss on next lifecycle event."

Root cause hypothesis

reconcileTerminalSessions in packages/codev/src/agent-farm/servers/tower-terminals.ts reconnects each live type='architect' terminal but assumes the accompanying architect registry row exists. There is no cross-check that verifies the row is present or a self-repair path that re-creates it.

Fix sketch

In reconcileTerminalSessions Phase 1 (tower-terminals.ts), when reconnecting a type='architect' terminal_sessions row:

  1. Check getArchitectByName(workspacePath, session.role_id) — if a row exists, no action needed
  2. If missing, re-create it via setArchitectByName(workspacePath, session.role_id, null) (or the appropriate helper)
    • workspace_path: from the terminal_sessions row
    • name: from role_id (which is how architects register their name)
    • session_id: null (backfills at next restart-bake once the process crashes-then-recovers and a fresh session id is minted)
  3. Log INFO: "self-repaired architect registry row for <workspace>/<name> (was orphaned from terminal_sessions)"

This is the inverse operation of #1150's fix: #1150 prunes registry rows without live terminals; this issue re-creates registry rows for terminals that survived.

Scope

  • packages/codev/src/agent-farm/servers/tower-terminals.tsreconcileTerminalSessions Phase 1 self-repair branch
  • Tests: simulate orphaned architect terminal → verify registry row created on reconcile
  • No CLI changes, no HTTP endpoint changes, no schema changes

Related

Suggested label: area/tower.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/towerArea: Tower server / agent farm CLI

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions