pi-session-anchor

extension

Remember your conversation tree position across pi restarts. Drop an anchor with /tree or /rewind, and pi will return to that spot next time you start (unless you chatted past it).

by · v0.3.0 · published 4w ago

$ pi install npm:pi-session-anchor
downloads/mo
801
stars
last push
open issues

Signals

license: MITtestspi manifest: missinginstall size: —deps: 0peer deps: 0

Download trend

No downloads in the last 12 weeks.

README

pi-session-anchor

Drop an anchor in your conversation tree — pi will return to that spot after a restart.

What it does

When you use /tree or /rewind to navigate to an earlier point in your session, pi-session-anchor saves that position (leafId) to a small sidecar file.

The next time you start pi (or resume the session), it restores the tree position so you pick up exactly where you left off — not at the latest message.

This fills the gap left by pi-rewind (which saves file state but not conversation tree position) and pi-undo-redo (which only incidentally saves position as a side-effect of its redo stack).

Resume switching (fixed!)

When you use /resume to switch between sessions and switch back, the anchor is now correctly restored — including the visual display. This was a two-part fix:

  1. Never overwrite the anchor with the current position.
    Only session_tree (explicit navigation) saves the anchor. session_shutdown does NOT overwrite it.

  2. Apply the anchor at session-file load, before any render.
    SessionManager.open() is patched to read the sidecar and call branch() immediately after _buildIndex() runs. This means renderInitialMessages() always sees the correct leafId — no timing gap.

How it works

/tree → navigate to older message
  │
  ├─► pi-session-anchor saves newLeafId → sidecar
  │
  ▼ restart or /resume
  │
  ├─► SessionManager.open() loads the session file
  ├─► patched open() reads sidecar, calls branch(anchor)
  ├─► renderInitialMessages() → correct tree displayed
  └─► ✅ conversation tree shows the anchored position

No session entries are modified. The anchor is kept in ~/.pi/agent/state/session-anchor/<sessionId>.json — clean, separable, inspectable.

Commands

CommandDescription
/pinManually save the current tree position as an anchor
/anchorShow the saved anchor for the current session

The anchor is also auto-saved on every /tree or /rewind navigation.

Edge cases handled

SituationBehaviour
Target entry compacted awaySilent fallback to latest position
Navigation to root (before first message)Saved as leafId: null, restored via resetLeaf()
Sidecar file deleted / corruptedTreated as "no anchor", normal startup
Multiple sessionsSeparate sidecar per session ID
Anchor then continue chattingNew messages are children of the anchored position; anchor stays until next /tree overwrites it
/resume switch away and backAnchor applied at SessionManager.open() — correct from first render
Extension reloadSessionManager.open patch is re-applied; no side effects

Installation

# If published on npm:
pi install pi-session-anchor

# Or copy to extensions directory:
cp -r pi-session-anchor ~/.pi/agent/extensions/

Then reload pi or restart.

Configuration (optional)

No configuration is required. If you want to disable auto-restore, remove the sidecar file for that session from ~/.pi/agent/state/session-anchor/.