Skip to content

feat: add list-cwd action to scope peer discovery by working directory#58

Open
iRonin wants to merge 1 commit into
nicobailon:mainfrom
iRonin:feat/list-cwd
Open

feat: add list-cwd action to scope peer discovery by working directory#58
iRonin wants to merge 1 commit into
nicobailon:mainfrom
iRonin:feat/list-cwd

Conversation

@iRonin

@iRonin iRonin commented Jul 2, 2026

Copy link
Copy Markdown

What

Adds an intercom({ action: "list-cwd" }) action that lists only the sessions sharing the caller's working directory — the scoped-discovery default an orchestrator usually wants — while list stays all-directories. An optional cwd narrows to a specific directory (absolute, or relative to the caller's cwd; "." means the current cwd).

Why

When several pi sessions run on one machine across different projects, list returns every peer regardless of directory. A coordinating agent almost always wants just the peers in the current working tree.

Doing that with a raw s.cwd === currentCwd string match is subtly wrong — it hides genuine same-directory peers whose cwd string differs only by:

  • a trailing slash (/work vs /work/),
  • a . / .. segment, or
  • a symlink (on macOS /tmp/private/tmp, or any symlinked project root).

So the new cwd.ts normalizes both sides before comparing — resolve() for the lexical variants and realpathSync() for symlinks (best-effort, with a graceful fallback if the directory no longer exists; memoized since the set of distinct cwds is small and stable).

Small robustness touch: if you filter by a directory that has no peers while your own cwd does (easy to do by passing a guessed parent path), the empty result points back to your real cwd and its peer count instead of a silently misleading "no sessions".

Scope

Purely additive — a new action mirroring list, reusing the existing formatSessionListRow and result conventions. Zero behavior change to list / send / ask / reply / pending / status.

Design note

I chose a separate list-cwd action over adding a cwd filter to list, for explicitness and back-compat (existing list callers and output are untouched, and the intent reads clearly at the call site). Happy to refactor into a list filter instead if you'd prefer a single action.

Tests

New cwd.test.ts covers normalizeCwd / sameCwd: trailing slash, lexical ./.. collapse, symlink resolution (real fixture), and the missing-dir fallback. Full suite green (96/96, including the 5 new).

intercom({ action: "list-cwd" }) lists only sessions that share the caller's
working directory — the default scope an orchestrator usually wants — while
list stays all-directories. An optional cwd narrows to a specific directory
(absolute, or relative to the caller's cwd; "." means the current cwd).

Directory matching goes through a normalized comparison (new cwd.ts:
normalizeCwd/sameCwd) so a peer whose cwd string differs only by a trailing
slash, a "."/".." segment, or a symlink (e.g. macOS /tmp <-> /private/tmp)
still matches. When the filtered directory has no peers but the caller's own
cwd does, the empty result points back to it so a mistargeted filter is
obvious instead of silently empty.

Adds cwd.test.ts covering normalize/sameCwd: trailing slash, lexical
"."/".." collapse, symlink resolution, and missing-dir fallback.
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.

1 participant