Skip to content

Fix #1978: a workspace runs while it has an open terminal, not while it has a map slot - #1980

Merged
waleedkadous merged 10 commits into
mainfrom
builder/bugfix-1978
Nov 25, 2026
Merged

waleedkadous merged 10 commits into
mainfrom
builder/bugfix-1978

Conversation

@waleedkadous

@waleedkadous waleedkadous commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1978

Problem

afx workspace list called stopped workspaces running. On the installed Tower (3.3.8) it listed 53 workspaces: 46 marked running, only 12 with a live agent. The other 34 stopped workspaces were printed with their registered architects and builders as addresses marked no live terminal.

The same wrong "running" caused three more problems:

  • A send to one of these workspaces was held no-live-pty, while a stopped workspace with no map slot was refused.
  • A stopped workspace could make a shared directory name ambiguous.
  • afx whoami could disagree with Tower about addresses.

Root cause

Everything that asked "is this workspace running?" asked "does it have a key in Tower's terminal map?". That covered the address book, the naming and ambiguity rules, the resolvers' workspace lookup, the reply footer and the name route. But that key is get-or-create, and many routes create it.

This is proved, not inferred. getInstances() sits behind /health, /api/workspaces, /api/status and /api/workspaces/:path/status. It calls getTerminalsForWorkspace() for every known workspace that exists on disk and is not a temp dir or a worktree. That function then stores its rebuilt entry whether or not the entry holds a terminal. The 46 "running" were exactly the 53 known minus the 7 that getInstances skips (6 missing directories and one temp dir). Per-workspace reads (GET /workspace/<p>/api/state, /api/overview) leave empty slots the same way.

Fix

One definition of running: runningWorkspaces() in utils/workspace-address.ts, the naming module Tower and the CLI already share. A workspace is running while it has at least one terminal.

  • Tower feeds it its open terminals through runningWorkspacePaths(): ids on the map whose session exists and has not exited. The address book, the naming and ambiguity rules, the resolvers, the reply footer and the name route all use it.
  • afx whoami feeds it the terminal_sessions rows, so whoami still does not need Tower.

Decisions (agreed with the architect before building):

  1. An activated workspace with no terminal yet is not running. There is nothing to list or deliver to, and the map cannot tell "launching" from "was merely asked about". A session is counted while it is open, even if not writable: one whose shellper connection is down still counts while it reconnects. Its mail stays held no-live-pty as before, and the naming rules do not flip during a reconnect.
  2. Behaviour change: a send to a registered agent in a stopped workspace, by an address that names the workspace, is refused with Workspace '<name>' (<path>) is not running. This applies to ws:architect, ws:architect:<name> and ws:<builder>. A stopped workspace with no slot already got this answer; now it is the answer whether or not a slot exists. It matches the listing, which shows a stopped workspace with no agents.
    • Mail already held is untouched. Mail held for an agent of a stopped workspace, including any this bug let through, stays held, is listed by afx inbox, and is offered to the agent's terminal when the workspace starts. The refusal applies to new sends only; nothing already accepted is dropped or refused after the fact.
    • Unchanged: an address with no workspace (the sender's own workspace) still holds for a registered agent with no terminal. That path never depended on map slots.
  3. The window after a Tower restart. Tower starts listening before the startup reconcile has re-adopted the shellpers, and the map fills as it goes. On today's restart that took 0.6 s for 64 sessions. /api/send and /api/address-book now wait for the reconcile before resolving, bounded at 10 s (CODEV_STARTUP_READY_TIMEOUT_MS) as the dashboard's reads already are. So a workspace that is coming back is neither refused nor listed as not running. The refusal text tells nobody to start anything. Only if the reconcile runs past the cap does a send go ahead against the partial map. A named-workspace send to a workspace not yet adopted then gets "is not running", which is what it gets today.

Known remainder, not changed here: the ~15 routes that get-or-create a map slot still do. Making them use a non-creating lookup is a separate change: they mix reads with writes, and a stopped workspace's file tabs live in that slot. "Running" no longer depends on it.

Known gap between whoami and Tower: whoami reads Tower's terminal_sessions rows, while Tower counts its open in-memory sessions. The two agree except when Tower keeps a row for a shellper it cannot reconnect to yet: its process is alive, but the reconnect failed, so the row is kept and retried on the next reconcile pass (tower-terminals.ts, Bugfix #1686). In that transient state whoami counts the workspace as running and Tower does not. Closing it would mean whoami asking Tower, or recording adoption in the database; both are larger than this fix. The architect accepted this gap before I built.

Small adjacent changes:

  • whoami no longer adds the caller's own workspace to the running set. Whether its own workspace is running never changes its own name.
  • Docs: the afx skill (.claude, .codex and both skeleton copies) and resources/commands/agent-farm.md (ours and the skeleton's) now say that an address naming a stopped workspace is refused. Shipped-skill hashes regenerated.

Testing

Regression tests, checked against the pre-fix source and build: 9 unit tests and 4 e2e cases fail without the fix; all pass with it.

  • spec-1940-address-book.test.ts, a stopped workspace with an empty slot:
    • is listed not running, with no agents;
    • a send to its registered architect is refused as not running, by both the live and the registry resolver, the same answer as with no slot;
    • it takes no part in ambiguity.
    • Also: a workspace whose every terminal has exited is not running; one whose only terminal is reconnecting is still running, and its mail is held.
  • spec-1313-registry-resolve.test.ts: an empty slot is refused, not held.
  • tower-routes.test.ts: a send that arrives before the startup reconcile settles waits for it and is delivered.
  • workspace-address.test.ts: runningWorkspaces().
  • cross-workspace-addressing.e2e.test.ts, on a test Tower with its own port and database: a stopped, registered workspace with an empty slot (left by a dashboard-style GET …/api/state) that shares the running codev-cloud's directory name.
    • afx workspace list shows it not running, with no agents.
    • The shared name still reaches the running workspace.
    • afx whoami gives the same address the listing prints.
    • Mail held for its architect while it is stopped stays held and is listed by afx inbox.
    • Once named, a send to it is refused as not running and stores nothing.
    • When it starts, the held mail is offered to the new terminal, and the address reaches it.
    • Without the fix this e2e reproduces every consequence in the issue: the stopped workspace is listed running with an agent, it takes codev-cloud's name, the name is refused as ambiguous, whoami and the listing disagree, and the send is held.

Fixtures that encoded the bug, now corrected:

  • spec-1313-registry-resolve treated an empty slot as a running workspace ("the resolver only cares that the key exists").
  • One route test removed every session on the Tower to simulate one dead recipient; it now removes only that one.
  • Mocks gain getTerminalManager and a session status.

Full suite: passed under porch's tests check (202 s), and the build check passed too.

Live verification (issue step 5): not done. It needs this build on a Tower with many workspaces. The only one is the live Tower on port 4100, and that means an install plus a Tower restart, which is the owner's call. A test Tower on a copy of global.db would reconcile the live shellpers and take over their connections, so I did not do that. After an install, check with afx workspace list --json: the count of running workspaces should equal the count of workspaces with a live agent. That was 46 vs 12 before.

Windows: on-demand workflow dispatched on this branch: https://github.com/cluesmith/codev/actions/runs/45502107634

…ot while it has a map slot

Every "running" (the address book, the naming and ambiguity rules, the resolvers'
workspace lookup, the reply footer, the name route) was "has a key in Tower's terminal
map". Many routes get-or-create that key: getInstances(), behind /health, /api/workspaces
and /api/status, stores an entry for every known workspace on disk, empty or not. So
stopped workspaces were listed as running, made shared directory names ambiguous, and
held mail that a stopped workspace with no key refuses.

- runningWorkspaces() in the shared naming module is the one definition: a workspace runs
  while it has at least one terminal. Tower feeds it its open terminals
  (runningWorkspacePaths(): on the map, session exists and has not exited); afx whoami
  feeds it the terminal_sessions rows.
- A send to a registered agent in a stopped workspace, by an address that names the
  workspace, is refused as not running, whatever slot the map holds. Own-workspace sends
  still hold, and mail already held is untouched.
- /api/send and /api/address-book wait for the startup reconcile (bounded), so a workspace
  whose terminals are still being adopted after a restart is not refused or listed as not
  running.
…space

- Address book: a stopped workspace with an empty slot is listed not running with no
  agents, takes no part in ambiguity, and a send to its registered architect is refused
  as not running, the same answer as with no slot. A workspace whose terminals have all
  exited is not running; one whose terminal is reconnecting still is.
- Registry resolver: an empty slot is refused, not held. Its fixture treated an empty slot
  as a running workspace, which is this bug; it now gives each workspace a terminal.
- Route: a send that arrives before the startup reconcile settles waits for it.
- e2e on a test Tower: the empty-slot case end to end, including afx whoami agreeing with
  the listing, held mail staying held and listed by afx inbox, and being offered to the
  terminal when the workspace starts.
- Mocks gain getTerminalManager / a session status; one route test removed every session
  on the Tower to simulate one dead recipient and now removes only that one.
… route waits, docs

- The address book carries `running` on each listed workspace instead of inferring it
  from a slot lookup, and fails loudly if a running workspace has no slot.
- runningWorkspacePaths() stops at the first open terminal of each workspace.
- PUT /api/workspaces/:path/address-name waits for the startup reconcile, as /api/send and
  /api/address-book do, since its answer reads which workspaces are running.
- The afx skill (all four copies) and resources/commands/agent-farm.md (ours and the
  skeleton's) say that an address naming a stopped workspace is refused. Shipped-skill
  hashes regenerated.
@waleedkadous
waleedkadous merged commit 5d5f88f into main Nov 25, 2026
8 checks passed
waleedkadous added a commit that referenced this pull request Nov 25, 2026
waleedkadous added a commit that referenced this pull request Nov 25, 2026
waleedkadous added a commit that referenced this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

afx workspace list shows stopped workspaces as running: an empty terminal-map entry counts as running

1 participant