A Pi extension for telling agents about other agents running in the same repo
Files
README.md

pi-neighbors #

A Pi extension that tells a pi session which other pi sessions are active in the same project, so the agent can work in a working copy of its own (a jj workspace or Git worktree), notice when tasks overlap, and leave other sessions notes.

When you send a prompt while the agent is waiting for you, it gets a notice listing the other sessions in the project, if that list has changed since it last saw one:

Other pi sessions are active in this project.

In your working copy (/work/app):
- "Fix the parser" (ID 3f9a1c2e): working for 4m, here before you

These sessions share your working copy, so your edits and theirs land in the same files and the same
jj change. Because one was working here before you, if your task changes files, move to a jj workspace
of your own with the `jj_workspace` tool before editing.

In other working copies of this repository:
- "Add chores to todo.txt" (ID b71d04aa): waiting for its user for 12m, in /home/me/.local/share/pi-jj/workspaces/app-1a2b3c4d/chores

Edits in separate working copies stay apart until the work is combined, when any conflicts can be resolved,
so don't narrow or put off your task to avoid them. If a task listed here overlaps yours, tell the user;
you can also tell the session itself with `note_neighbors`.

Agents told about other sessions tend to shrink or postpone their own tasks to keep clear of possible merge conflicts. Separate working copies make that unnecessary, so the notice says so; outside a repository, where there is no separate working copy, it leaves that out.

What counts #

  • Project: sessions share a project when they are in the same repository; any jj workspace or Git worktree of it counts, including Git worktrees of a Git-backed jj repo. Outside a repository, sessions share a project only if they run in the same directory.
  • Working copy: a jj workspace or Git worktree; outside a repository, the directory. Sessions in the same working copy edit the same files.
  • Session: one line of conversation in one pi process. /new, or resuming an unrelated session, starts a new one, listed as a newcomer once it gets a prompt; until then (say, while you only read it) it isn't listed. Reloading, forking, or moving with pi-jj continues the same one.
  • Title: the session's name, or the start of its first prompt if it has none. pi-neighbors doesn't name sessions itself; with an extension that does, the names show up here.
  • Status: working, or waiting for its user (which may mean it has finished, or is waiting for an answer), and for how long. Reloading or forking doesn't reset the time.
  • Who was here first: whichever session first started working in the working copy. A session that hasn't yet counts as arriving when it does. Only sessions that arrived after another are told to get a working copy of their own; the first can carry on where it is. When two start at about the same time, the one that turns out to be later is told after its first turn.

When notices come #

At a prompt, the notice comes again only when a session joins or leaves the project, another session moves to another working copy, a session listed without a title gets one, or the agent finds sessions in its working copy it hasn't been told about. Its own move to a working copy no one else uses brings no notice. When the others have all gone, one last notice says so. A change in status alone brings no notice. After compaction drops the last notice from the agent's context, the next prompt brings a fresh one if other sessions are still active. When a notice reverses who was working here first (two sessions started at about the same time, or this one was resumed in a new pi process), it says so.

While the agent is working, sessions in its working copy that it hasn't been told about are reported after the current turn (one model response and its tool calls), or before the first one in a run that started without a prompt, such as pi-jj continuing the task after a move. That covers prompts queued while it works, and a move into a working copy already in use. Anything else that changes mid-run waits for the next prompt.

The agent can check at any time with the neighbors tool, and you can with /neighbors. If the list of sessions can't be read (see How sessions find each other), these say so; notices stay silent rather than claim no one else is there.

Notes #

With the note_neighbors tool, an agent can leave a note for one or more sessions in the project, such as a heads-up that a change of its own affects their work, or a question about theirs. Sessions are named by the IDs listed in notices and by the neighbors tool, shown only while note_neighbors is active.

A working recipient's agent gets a note after its current turn. A recipient waiting for its user adds the note to its conversation within a second or so, where you see it and its agent reads it with your next prompt: notes never wake a session, so they never start a run without a user. The agent sees the note quoted, marked as coming from another agent rather than its user, with how long ago it was sent if that was a minute or more, and whether its sender is still active; it is asked to answer only if the note asks something. Notes are one-way and short (up to 2,000 characters); a conversation between sessions, which would need to wake them, is out of scope.

Other extensions #

pi-neighbors needs no other extension, nor jj or git on PATH: it reads repository metadata from disk. The integrations below are optional, and without them the notice falls back to generic advice.

pi-jj #

In a jj repo, the notice points the agent at the jj_workspace tool from pi-jj when that tool is active, and at jj workspace add otherwise (in a Git repo, git worktree add). A session pi-jj moves keeps its place in the list, now in its new working copy, where it counts as arriving once it works there.

Subagents #

Sessions started by pi-simple-subagents (PI_SUBAGENT=1) are left out, both from the list and from notices: they work inside their caller's task. Subagents from extensions that don't set PI_SUBAGENT=1 are treated as ordinary sessions.

How sessions find each other #

Each pi process keeps a small file describing its session in $XDG_RUNTIME_DIR/pi-neighbors/, or pi-neighbors-<uid>/ under the temporary directory when XDG_RUNTIME_DIR isn't set; a directory that isn't private to the user is not used. The process removes its file when it ends, and a file left by a process that has exited is deleted by the next session that reads it. Notes wait in notes/<ID>/ there until their recipient reads them; notes for a session that has ended are deleted an hour later, the next time a note is sent. Sessions only see each other when they share that directory, so the same user on the same machine (or container).

Install #

pi install https://tangled.org/did:plc:hfiwdzwmkequan34inl7ecmf/

Or try it for one session with pi -e <path-to-checkout>.

Development #

npm install
npm run check   # type-check
npm test