Plan: one agent per directory, however its path is spelled
Closes: #839. Relates to: #833 (the missed messages this is the cause of), #837 (waits on this), ADR-008 (agent messaging), ADR-006 (remote workspaces). Branch: fix/agent-name-canonical-path. Status: Done (2026-11-17) — both spellings of a luchs project name one agent, over SSH from panther in 13-27 ms; not released; the start-time comparison was not run
Context
The operator’s finding (2026-11-17): on luchs /data/code and ~/code are the same directory, and it breaks subscriptions. Confirmed:
| Fact | Evidence |
|---|---|
One directory, two paths |
|
Nothing resolves one to the other |
A bind mount is not a link: |
The mount table does |
|
An agent is named by its path |
|
That is what #833 saw |
The three config agents were subscribed as |
#833’s fix (0.163.0) guesses by directory name and offers to copy; three of five offers on the box where it was measured were wrong. This plan removes the cause; that offer stays for a project that really moved.
Design
D1. A directory’s canonical path. New packages/pi-workspace/canonical-path.ts, one pure function canonicalPath(real, mountinfo, identityOf) returning { path, aliases }:
-
realis the link-resolved path. Find its mount inmountinfo(the longest mount point that is a prefix; the last listed where several share one). Its place inside the filesystem is that mount’s root plus the rest of the path. -
Every mount of the same device (
major:minor) whose root is a prefix of that place gives a candidate spelling: its mount point plus the remainder. -
A candidate counts only if
identityOf(candidate)equalsidentityOf(real)(device and inode of the directory): a mount hidden under another, or a table that misleads, is dropped here. -
The canonical path is the candidate whose mount has the shortest root (nearest the filesystem’s own root: the original, not the bind), then the shortest mount point, then the smallest by string comparison. The others are the aliases. On luchs:
/data/code/pi, alias/home/bitskrieg/code/pi. -
With no mount table (Windows, macOS, a read that fails), or no candidate but itself, the answer is
realand no alias: today’s behaviour.
D2. Asked on the host the directory is on. Workspace (types.ts) gains two primitives: mounts(): Promise<string | undefined> (the text of /proc/self/mountinfo; undefined where there is none) and identity(path): Promise<string | undefined> ("<dev>:<ino>" from a bigint stat; undefined when absent). local.ts implements them with node:fs; packages/pi-ssh/helper.mjs gains the two ops and ssh-workspace.ts, protocol.ts (the Op union; PROTOCOL_VERSION 2 to 3) and connection.ts (both in IDEMPOTENT) carry them; helper.d.mts is unchanged unless it lists ops. An identity call that throws for a candidate (EACCES, ENOTDIR) reads as undefined: that candidate is dropped. canonicalOf(ws, path) in canonical-path.ts composes ws.realpath, ws.mounts, ws.identity and the pure function, so the rule exists once, for local and remote.
D3. The agent’s name. agentName(cwd, deps): Promise<{ name: string; aliases: string[] }>. The configured name as today, with no aliases. Else the directory is ws.resolve(cwd) for a remote workspace and cwd for a local one, as today, and the name is canonicalOf(ws, directory).path, made to fit as today; case-folded only for a local workspace on win32, as today (a remote host’s path is never folded). For a remote workspace this also resolves links, which it did not. aliases are D1’s, each made to fit the same way, plus the name the old rule gave when it differs (the same directory by D1’s identity check).
The wait is bounded here, since Connection.request in pi-ssh has no per-call timeout: canonicalOf is raced against AGENT_NAME_WAIT_MS (2 s, in agent-name.ts). On a timeout or a throw the old rule’s name is used for this session, with no aliases, and pi_remote.agent_name_fallback is logged with which.
D4. The session start waits for the name. sessionHooks in index.ts awaits the name, for at most D3’s bound, before restoreLink (still not awaited itself) and startMessaging, so a member tells its hub the canonical name.
D5. What the aliases held moves to the canonical name, at session start. mergeAliases(agentDir, name, aliases) in messaging.ts: each alias’s subscriptions are merged into the name’s (a channel both have keeps one entry with both tag lists, as mergedTags does); the name takes an alias’s handle if it has none; the alias entries are removed from subscriptions.json and handles.json, and the pair is recorded in a new <state dir>/msg/aliases.json, canonical name to its aliases, added to and never removed from. This is a move, unlike #833’s adopt, because D1 proved the directory is the same. It also folds the duplicates adopt made on such a box. Logged as pi_remote.aliases_merged with the names.
D6. An agent still answers to its aliases. Messages in the store carry the old name. aliases.json is read wherever the box asks who an agent is, by the hub for every agent and by a session for itself, so nothing depends on one process’s memory: aliasesOf(agentDir, agent) in messaging.ts. With it: a to:<alias> tag wakes the agent as to:<name> does (receivedEvent for the session’s own wake; answeringTo for a sender’s notes and receiptBody for a receipt, both through one helper that passes the aliases to wakes); a message read by an alias is read and a post made under an alias from this box is the agent’s own (isUnreadBy takes the agent’s names; its callers in messaging.ts and msg-tools.ts pass them), so nothing already read is handed over again. recipients needs no change: it matches sessions by their own name, which is the canonical one.
D7. #833’s offer. formerSelves leaves out a name that is an alias (it was just merged). Nothing else in it changes.
Scope
In: packages/pi-workspace/canonical-path.ts, types.ts, local.ts, index.ts (export), contract.ts; packages/pi-ssh/helper.mjs, helper.d.mts, protocol.ts, connection.ts, ssh-workspace.ts; packages/pi-remote/agent-name.ts, index.ts, messaging.ts, msg-store.ts, msg-tools.ts, lifecycle.ts (the receipt’s caller); their tests; docs/modules/ROOT/pages/remote.adoc, ssh.adoc, public-surface.adoc; CHANGELOG.adoc.
Out, each an issue:
-
pi’s own session directory is named from the working directory as typed:
pi -cfrom the other spelling finds no session. Upstream’s; to be reported (#844). -
pi-ssh’s anchor (
anchor.ts,anchorFor) hashes the remote path as typed, so the two spellings of one remote directory are two anchors and two session histories. Needs a move of existing anchors (#842). -
The memory pool of a repository with no
originis keyed by its git directory’s real path (pi-modes/memory.ts), the plan draft by the working directory (plan-mode.ts,cwdSlug), a job’s project byws.root(job-tools.ts). Each should use D1 (#843). -
One filesystem reached under two device numbers (two btrfs subvolume mounts): D1 does not unify them; it also never confuses them (noted on #844).
Risks
| Risk | Mode | Mitigation |
|---|---|---|
Two different directories get one name, and one agent reads another’s messages |
fail-closed |
A candidate counts only when its device and inode equal the directory’s (D1.3). Property test: for any mount table and any identity function, every alias has the identity of the path; a path whose candidates all differ in identity keeps its own name. |
An agent’s name changes at the update (on luchs |
silent |
The session answers to its aliases (D6); peers here address handles, which move with the merge (D5). Contract test: a message tagged |
Everything read under the old name shows unread, and the last day’s messages wake the agent again at its next start |
fail-open |
|
The mount table changes (a bind added or removed), so the canonical name changes |
silent |
While the old spelling is still a mount it is an alias and its state is merged at the next start. Once it is gone the entry is orphaned and #833’s offer covers it. Stated. |
The merge writes |
silent |
The store’s accepted race, as its own comments say for subscribing and for handles; the window is one synchronous read-and-write. A lost merge is redone at the next start (the alias entry is still there); a lost subscription of the other project is that existing race, not made likelier per write. Test: |
A remote helper from before this change has no |
fail-open |
The helper is uploaded by content hash with each release, so a session always talks to its own. A failed call falls back to the old name (D3) and logs it; test with a workspace whose |
The session start waits on a remote round trip for the name, and a hung link holds it |
fail-open |
Bounded at |
|
fail-open |
The parser unescapes; a line it cannot read is skipped, so the result is at worst the old name. Unit tests with such lines. |
A second checkout of one repository is not unified (different inodes) |
fail-open |
Correct: it is another directory. #833’s offer still names it; unchanged. |
Tests
Unit (packages/pi-workspace/test/canonical-path.test.ts, on mount tables as text):
-
luchs’s two lines verbatim: both spellings of a project give
/data/code/<p>with the other as alias; a path outside the bind has no alias. -
A bind of a subdirectory, a filesystem mounted twice at its root (tie broken by mount point), nested binds (three spellings), a mount hidden under a later one (dropped by identity), an escaped mount point, no table.
Property (fast-check): canonicalPath applied to its own result gives the same path ("idempotent"); every alias of a path canonicalises to the same path ("all spellings agree"); no alias has another identity ("never two directories under one name").
Contract (packages/pi-workspace/contract.ts, run for the local and the SSH workspace): identity of a directory equals that of a link to it and differs from a sibling’s; undefined for a missing path; mounts is text with the workspace root’s mount on Linux, undefined elsewhere. packages/pi-ssh/test: the two ops over the wire.
Regression (packages/pi-remote/test, the cases that would have caught #833):
-
The stored shape of luchs: subscriptions and a handle under
/data/code/x, a session started in/home/bitskrieg/code/xwith that mount table: its name is/data/code/x, it is subscribed, a post to its handle wakes it, andformerSelvesoffers nothing. -
The reverse history: state under the
/home/…spelling, started from either: merged under/data/code/x, the alias entries gone, tags and handle kept. -
The panther shape after
adopt: both names hold the channel: one entry after the start, both tag lists. -
Reads under the alias: unread 0 and no hand-over at a fresh start. A message tagged
to:<alias>wakes. -
A remote workspace whose
mountsthrows: the old name, the fallback logged, the session otherwise as today. -
aliases.jsonafter a merge names the alias; a second box process (the hub, for a member’s agent) answersto:<alias>from the file alone. -
A workspace whose
mountsnever settles: the bound of D3, as in the Risks table.
A real bind mount: one test makes one in a user namespace (unshare --user --map-root-user --mount), and is skipped by the runner where that is refused; it asserts both spellings give one name through the local workspace.
Existing tests changed, not deleted: agent-name.test.ts (async; the remote case now link-resolved), the start-order cases of extension.test.ts and hub-e2e.test.ts (the member’s name is known before it attaches).
Measured before merge, prediction on file first:
-
luchs, read-only:
canonicalOffor/data/code/piand~/code/pigives/data/code/piboth times. -
The Windows seat, on a copy of panther’s stored state, through its real SSH workspace to luchs: the name each of the three config sessions would get, and the stored state after the merge (one entry each under
/data/code/…, handle kept, nothing of another agent touched). Its own local name unchanged. -
Session start time over SSH with and without the change, five interleaved rounds.
-
Over an SSH workspace, whether a fresh session start reaches
startMessagingwith the remote workspace published (on 0.163.0 the remote sessions were not told what the local ones were, #833, cause not found). Recorded on #841; its fix is not part of this plan.
Not tested: macOS; btrfs subvolumes.
Delivery
-
One branch,
fix/agent-name-canonical-path: the pure function and the two workspace primitives with their tests; the helper ops; the agent name, the merge and the alias handling; the docs. -
Canonical doc:
remote.adoc, where it says how an agent is named: the canonical path, what a bind mount did before, the merge at start, that a session answers to its aliases.ssh.adoc: the two helper ops.public-surface.adoc: theWorkspaceadditions. -
CHANGELOG, under Unreleased: a Changed entry saying a project reached through a bind mount is one agent, what its name becomes, and that subscriptions under the other spelling are merged at the next start.
-
node tools/task.mjs validate, fullconsumer-sim, the measurements above; MR closing the issue, merged with squash off, the merge SHA on the issue; the out-of-scope issues filed and linked there. -
Released with the next release;
@gadhs/pi-workspace,@gadhs/pi-ssh,@gadhs/pi-remoteand@gadhs/pimove.
Errata
Erratum (2026-11-17): a risk the table lacks, shown by the Windows seat on a copy of the affected box’s state: a session of the same project still running under the old spelling after a merge holds nothing under its name and is not woken until restarted (silent). Stated in remote.adoc and the CHANGELOG; an earlier release’s session cannot be taught the alias.
Erratum (2026-11-17): "Session start time over SSH with and without the change, five interleaved rounds" was not run. The naming itself was timed over the real link instead: 13-27 ms in fifteen runs, against the 2 s bound.