Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .claude/skills/afx/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ workspace on this Tower with its agents, each as an address ready to paste (see
below). `<workspace>` is the workspace's directory name, or the name it was given with
`afx workspace name`. A named architect that is not registered fails, listing the workspace's
architects and its front door; nothing is rerouted. Mail to a registered architect with no live
terminal is held (`no-live-pty`), like any other.
terminal is held (`no-live-pty`), like any other, while its workspace is running; an address that
names a stopped workspace (one with no open terminal) is refused as not running.

**Replying to another workspace.** A message from another workspace names its sender by full
address, and its last line is the command that answers it. Run it as printed:
Expand Down
3 changes: 2 additions & 1 deletion .codex/skills/afx/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ workspace on this Tower with its agents, each as an address ready to paste (see
below). `<workspace>` is the workspace's directory name, or the name it was given with
`afx workspace name`. A named architect that is not registered fails, listing the workspace's
architects and its front door; nothing is rerouted. Mail to a registered architect with no live
terminal is held (`no-live-pty`), like any other.
terminal is held (`no-live-pty`), like any other, while its workspace is running; an address that
names a stopped workspace (one with no open terminal) is refused as not running.

**Replying to another workspace.** A message from another workspace names its sender by full
address, and its last line is the command that answers it. Run it as printed:
Expand Down
3 changes: 2 additions & 1 deletion codev-skeleton/.claude/skills/afx/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ workspace on this Tower with its agents, each as an address ready to paste (see
below). `<workspace>` is the workspace's directory name, or the name it was given with
`afx workspace name`. A named architect that is not registered fails, listing the workspace's
architects and its front door; nothing is rerouted. Mail to a registered architect with no live
terminal is held (`no-live-pty`), like any other.
terminal is held (`no-live-pty`), like any other, while its workspace is running; an address that
names a stopped workspace (one with no open terminal) is refused as not running.

**Replying to another workspace.** A message from another workspace names its sender by full
address, and its last line is the command that answers it. Run it as printed:
Expand Down
3 changes: 2 additions & 1 deletion codev-skeleton/.codex/skills/afx/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ workspace on this Tower with its agents, each as an address ready to paste (see
below). `<workspace>` is the workspace's directory name, or the name it was given with
`afx workspace name`. A named architect that is not registered fails, listing the workspace's
architects and its front door; nothing is rerouted. Mail to a registered architect with no live
terminal is held (`no-live-pty`), like any other.
terminal is held (`no-live-pty`), like any other, while its workspace is running; an address that
names a stopped workspace (one with no open terminal) is refused as not running.

**Replying to another workspace.** A message from another workspace names its sender by full
address, and its last line is the command that answers it. Run it as printed:
Expand Down
3 changes: 2 additions & 1 deletion codev-skeleton/resources/commands/agent-farm.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ codev-cloud /Users/ana/dev/codev-cloud
```

- One block per workspace: its address name and path, then its architects, builders and shells.
Workspaces that are not running come last, marked `not running`, with no agents listed.
Workspaces that are not running (no open terminal) come last, marked `not running`, with no
agents listed; an address that names one is refused as not running.
- A line that is only an address and `live` or `no live terminal` is ready to paste into
`afx send`. Mail to an agent with no live terminal is held (`no-live-pty`) until it has one.
- An agent the caller may not address is still listed, with `reach through <address>` beside
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
id: bugfix-1978
title: afx-workspace-list-shows-stopp
protocol: bugfix
phase: verified
plan_phases: []
current_plan_phase: null
gates:
pr:
status: approved
requested_at: '2026-11-24T22:31:55.063Z'
approved_at: '2026-11-25T00:39:59.209Z'
iteration: 1
build_complete: false
history: []
started_at: '2026-11-24T21:54:58.375Z'
updated_at: '2026-11-25T00:40:04.043Z'
pr_ready_for_human: false
3 changes: 2 additions & 1 deletion codev/resources/commands/agent-farm.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ codev-cloud /Users/ana/dev/codev-cloud
```

- One block per workspace: its address name and path, then its architects, builders and shells.
Workspaces that are not running come last, marked `not running`, with no agents listed.
Workspaces that are not running (no open terminal) come last, marked `not running`, with no
agents listed; an address that names one is refused as not running.
- A line that is only an address and `live` or `no live terminal` is ready to paste into
`afx send`. Mail to an agent with no live terminal is held (`no-live-pty`) until it has one.
- An agent the caller may not address is still listed, with `reach through <address>` beside
Expand Down
82 changes: 82 additions & 0 deletions codev/state/bugfix-1978_thread.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# bugfix-1978 thread

Issue #1978: `afx workspace list` shows stopped workspaces as running, because an empty
terminal-map entry counts as running.

## Investigate (2026-11-24)

Reproduced read-only on the installed Tower (3.3.8): `afx workspace list --json` gives 53
workspaces, 46 marked running, 7 not running, and only 12 with a live agent.

The root cause is proved, not inferred. `getInstances()` (`tower-instances.ts`) 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 builder worktree. That function ends with `workspaceTerminals.set(path, freshEntry)` whether
or not the entry holds a terminal. The 7 marked not running are exactly the ones `getInstances`
skips: 6 missing directories and `/private/tmp/todo`. The per-workspace reads (`GET
/workspace/<p>/api/state`, `/api/overview`) leave empty entries the same way, which is how
`shannon/.builders/research-8593`, a worktree, got one.

Every "running" in the address book, the naming rules (`findWorkspaceByAddressName`,
`workspaceAddressName`), the resolvers (`locateWorkspace`), the reply footer
(`crossWorkspaceOrigin`) and the name route is "has a key in the terminal map". `afx whoami` uses
"has a `terminal_sessions` row" instead.

Decisions sent to the architect:
1. A workspace is running when the map holds at least one terminal for it whose session is open
(it exists in the terminal manager and has not exited). A workspace that is activated but has
no terminal yet is not running. "Open" rather than "writable", so the naming and ambiguity
rules do not flip while a shellper reconnects. The rule is one function,
`runningWorkspaces()`, in the shared naming module: Tower feeds it its open terminals and
whoami feeds it the `terminal_sessions` rows.
2. A send to a registered agent in a stopped workspace, by an address that names the workspace,
is refused as not running, whether or not an entry exists. Own-workspace sends, with no
workspace written, still hold as before.
3. Read routes that create entries are left alone: that is a separate change of about 15 call
sites, and (1) does not depend on it.

The architect agreed to all three (2026-11-24 22:00Z) and added two things to pin down:
(a) mail already held for an agent of a stopped workspace stays held, shows in `afx inbox`, and
delivers when the workspace starts; (b) what a send gets in the window after a Tower restart,
before the shellpers are re-adopted.

## Fix (2026-11-24)

- `runningWorkspaces()` (`utils/workspace-address.ts`) is the one definition. Tower feeds it
through `runningWorkspacePaths()` (`tower-messages.ts`): a terminal counts when its id is on the
map and its session exists and has not exited. That function is used by the address book,
`locateWorkspace`, `crossWorkspaceOrigin` and the name route. `afx whoami` feeds
`runningWorkspaces()` from the `terminal_sessions` rows and no longer adds its own workspace
(that never changed its own name).
- (b) The window is real: Tower listens before the startup reconcile finishes. On today's
restart that took 0.6 s for 64 sessions (21:28:47.463 listen, 21:28:48.081 reconcile complete).
`/api/send` and `/api/address-book` now await `whenStartupReconcileSettled()` (bounded at 10 s),
as the dashboard's reads already did.
- (a) The fix doesn't touch the mailbox: delivery keys on the agent's live session, not on
"running". The e2e pins it: mail held while the workspace is stopped stays held, is listed by
`afx inbox`, is untouched by the refusal, and is offered to the new terminal when the workspace
starts (its reason goes from `no-live-pty` to `no-profile` under the inert terminal).
- Fixtures that encoded the bug: `spec-1313-registry-resolve` treated an empty slot as a live
workspace, and one route test removed every session on the Tower to simulate one dead
recipient.
- Checked the tests against the pre-fix source and build: 9 unit tests and 4 e2e cases fail, and
the e2e reproduces every consequence in the issue. With the fix, all pass.
- Step 5 (verify on a real Tower with many workspaces) needs the fix on the live Tower, which
means an install plus a Tower restart: 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 didn't do that.

## PR #1980 and CMAP (2026-11-24)

- codex: REQUEST_CHANGES.
- (1) whoami can still disagree with Tower when Tower keeps a `terminal_sessions` row for a
shellper it has not re-adopted. This is the residual gap the architect accepted; it is now
stated in the PR body.
- (2) Commit prefixes: the three it named are porch's own `chore(porch)` commits.
- (3) PR headings: kept in PRFT form, as the architect asked.
- claude: APPROVE, with nits. Fixed in place:
- the address book carries `running` structurally, and throws if a running workspace has no
slot;
- `runningWorkspacePaths()` stops at the first open terminal per workspace;
- the name route waits for the startup reconcile;
- the afx skill (4 copies) and `agent-farm.md` (2 copies) say that naming a stopped workspace is
refused, and the shipped-skill hashes were regenerated.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const { mockGetWorkspaceTerminals, mockLookupBuilderSpawningArchitect } = vi.hoi

vi.mock('../servers/tower-terminals.js', () => ({
getWorkspaceTerminals: () => mockGetWorkspaceTerminals(),
// Every terminal on the fake map is open, so a workspace with one is running (Issue #1978).
getTerminalManager: () => ({ getSession: () => ({ status: 'running' }) }),
}));

vi.mock('../state.js', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ vi.mock('../db/index.js', async (importOriginal) => {
const { send } = await import('../commands/send.js');
const { workspaceName } = await import('../commands/workspace-name.js');
const { workspaceList } = await import('../commands/workspace-list.js');
const { resolveIdentity } = await import('../commands/whoami.js');
const { inboxList } = await import('../commands/inbox.js');

/** An inert terminal: it stays alive, reads nothing, and shows no agent composer. */
const INERT_SCRIPT = 'trap "" INT; while true; do sleep 3600; done';
Expand Down Expand Up @@ -222,6 +224,11 @@ async function activate(workspacePath: string): Promise<void> {
throw new Error(`workspace ${workspacePath} did not activate: ${res?.status} ${await res?.text()}`);
}

async function deactivate(workspacePath: string): Promise<void> {
const res = await tower('POST', `/api/workspaces/${encodeWorkspacePath(workspacePath)}/deactivate`);
if (!res.ok) throw new Error(`workspace ${workspacePath} did not deactivate: ${res.status} ${await res.text()}`);
}

async function addArchitect(workspacePath: string, name: string): Promise<void> {
const res = await tower('POST', `/api/workspaces/${encodeWorkspacePath(workspacePath)}/architects`, { name });
if (!res.ok) throw new Error(`add-architect ${name} failed: ${res.status} ${await res.text()}`);
Expand Down Expand Up @@ -283,6 +290,8 @@ describe('Spec 1940: cross-workspace addressing on a real Tower that has never c
/** `<root>/b/codev`: a second directory called `codev`, started only for the last cases. */
let otherCodev: string;
let builderWorktree: string;
/** The inert program every workspace here runs as its architect. */
let inertAgent: string;
const active: string[] = [];

const review = (): Agent => ({ cwd: codev, architect: 'review' });
Expand All @@ -302,7 +311,7 @@ describe('Spec 1940: cross-workspace addressing on a real Tower that has never c
// The temp dir is behind a symlink on macOS (/var → /private/var); Tower keys a
// workspace by its real path, and the CLI must name the same one.
root = realpathSync(mkdtempSync(join(tmpdir(), 'codev-1940-')));
const inertAgent = writeInertAgent(root);
inertAgent = writeInertAgent(root);
codev = makeWorkspace(join(root, 'a', 'codev'), inertAgent);
cloud = makeWorkspace(join(root, 'codev-cloud'), inertAgent);
otherCodev = makeWorkspace(join(root, 'b', 'codev'), inertAgent);
Expand Down Expand Up @@ -528,6 +537,114 @@ describe('Spec 1940: cross-workspace addressing on a real Tower that has never c
]);
});

describe('a stopped workspace that Tower holds an empty slot for (Issue #1978)', () => {
/**
* `<root>/parked/codev-cloud`: started once, so Tower registered it and its architect
* `main`, then stopped, which keeps that registration. It shares its directory name with
* the running codev-cloud.
*/
let parked: string;
const HELD_WHILE_STOPPED = 'For main in the parked workspace, when it is back';

/** A person at a plain shell in the parked workspace: no terminal of Tower's. */
const atParked = (): Agent => ({ cwd: parked });

beforeAll(async () => {
parked = makeWorkspace(join(root, 'parked', 'codev-cloud'), inertAgent);
await activate(parked);
await deactivate(parked);
// What a dashboard or extension poll of a stopped workspace does: it asks for the
// workspace's state, and the route leaves an empty slot on the terminal map.
const res = await tower('GET', `/workspace/${encodeWorkspacePath(parked)}/api/state`);
expect(res.ok).toBe(true);
}, 60_000);

it('afx workspace list lists it as not running, with no agents, and the running codev-cloud keeps the name', async () => {
const listed = await runAs(builder(), () => workspaceList());

expect(listed.exitCode).toBeNull();
const lines = listed.stdout.map((line) => line.replace(/ +/g, ' ').trim());
// The running workspaces, exactly as before, then the stopped one, with no agents.
expect(lines).toEqual([
`codev ${codev}`,
'codev:architect:main live reach through codev:architect',
'codev:architect:review live reach through codev:architect',
`codev-cloud ${cloud}`,
'codev-cloud:architect:main live',
'codev-cloud:architect:data no live terminal reach through architect',
'codev-cloud:architect:tunnel live reach through architect',
`codev-cloud:${BUILDER} live`,
// The running codev-cloud keeps the directory name; the stopped one has none of its own.
`(no address name) ${parked} not running`,
`no address name: it shares the directory name 'codev-cloud' with ${cloud}; give it a name of its own: afx workspace name <name>`,
]);
});

it('the shared directory name still reaches the running workspace: the stopped one is not a second match', async () => {
const sent = await afxSend(review(), 'codev-cloud:architect:tunnel', 'Tunnel, past the parked namesake');

expect(sent.exitCode).toBeNull();
expect(mailWithBody('Tunnel, past the parked namesake')).toMatchObject({ workspace_path: cloud, to_agent: 'tunnel' });
});

it('afx whoami gives the address the listing prints for the same agent', async () => {
const who = await runAs(builder(), async () => resolveIdentity());
const book = await tower('GET', `/api/address-book?workspace=${encodeURIComponent(cloud)}&from=${BUILDER}`);
const listed = ((await book.json()) as { workspaces: Array<{ path: string; agents: Array<{ name: string; address: string | null }> }> })
.workspaces.find((w) => w.path === cloud)!
.agents.find((a) => a.name === BUILDER)!;

expect(who.result).toMatchObject({ type: 'builder', name: BUILDER, address: `codev-cloud:${BUILDER}` });
expect(listed.address).toBe(`codev-cloud:${BUILDER}`);
});

it('mail held for its architect while it is stopped stays held, and is listed by afx inbox', async () => {
// A sender in the workspace itself addresses no workspace, and that path holds mail
// for a registered agent with no terminal, as it always has.
const sent = await afxSend(atParked(), 'architect', HELD_WHILE_STOPPED);
expect(sent.exitCode).toBeNull();
expect(sent.result).toBe('held');
const held = mailWithBody(HELD_WHILE_STOPPED);
expect(held).toMatchObject({ workspace_path: parked, to_agent: 'main', status: 'held', reason: 'no-live-pty' });

const inbox = await runAs(atParked(), () => inboxList());
expect(inbox.exitCode).toBeNull();
expect(inbox.stdout.some((line) => line.includes(held.id) && line.includes('no-live-pty'))).toBe(true);
});

it('once it has a name, a send to its registered architect is refused as not running, and nothing is stored', async () => {
const named = await runAs(atParked(), () => workspaceName({ name: 'parked' }));
expect(named.exitCode).toBeNull();
const before = mailCount();

const sent = await afxSend(review(), 'parked:architect', 'For the parked workspace, by its name');

expect(sent.exitCode).toBe(1);
expect(outcomeLines(sent)).toEqual([]);
expect(sent.stderr).toEqual([`[error] Workspace 'parked' (${parked}) is not running. (NOT_FOUND)`]);
expect(mailCount()).toBe(before);
// The mail it already held is untouched by the refusal.
expect(mailWithBody(HELD_WHILE_STOPPED)).toMatchObject({ status: 'held', reason: 'no-live-pty' });
});

it('when the workspace starts, the mail held for its architect is offered to the new terminal', async () => {
await start(parked);

// The inert terminal shows no agent composer, so the gate holds the message for that
// reason now: it was offered to a live terminal, and it is still on record, not dropped.
let mail = mailWithBody(HELD_WHILE_STOPPED);
for (let attempt = 0; attempt < 60 && mail.reason === 'no-live-pty'; attempt++) {
await new Promise((r) => setTimeout(r, 500));
mail = mailWithBody(HELD_WHILE_STOPPED);
}
expect(mail).toMatchObject({ workspace_path: parked, to_agent: 'main', status: 'held', reason: 'no-profile' });
// And the address that was refused while it was stopped now reaches it.
const sent = await afxSend(review(), 'parked:architect', 'For the parked workspace, now running');
expect(sent.exitCode).toBeNull();
expect(mailWithBody('For the parked workspace, now running')).toMatchObject({ workspace_path: parked, to_agent: 'main' });
}, 60_000);
});

describe('two running workspaces whose directories are both called codev', () => {
beforeAll(async () => {
await start(otherCodev);
Expand Down
Loading
Loading