wonder 922e1c3be2 Initial commit: pi-lens session observability for pi
- collector, classify, prompt-parser, server (TypeScript)
- web panel (app.js, index.html, style.css)
- smoke and load-test scripts
- DESIGN.md and README.md
2026-10-04 19:54:34 +08:00

pi-lens

Session observability for pi: prompt assembly, skill invocation traces, tool chains, and session state — served as a local web panel.

What it does

On every session start, pi-lens begins recording and serves a read-only web panel at http://127.0.0.1:7777/ (port auto-increments). Four tabs:

Tab Shows
Overview session id/file/cwd, model, thinking level, context window usage, tool table, MCP servers, skill ledger, totals
Prompt per-request prompt assembly waterfall — every context source as a colored segment (byte-accurate), with per-request diff
Skills skill ledger + invocation trace chains: trigger → SKILL.md load → tool calls → output, for both /skill:name and model-initiated loads
Timeline all events, virtualized, colored by context kind, filterable

Click anything → a detail card on the right with metadata and full content (fetched on demand — lists never carry big text).

Context color legend

Color Context type
🔵 blue base instructions (preamble)
🟣 purple tool rules (<tools>, <rules>)
⚪ slate pi docs (<docs>)
🟢 green context files (<project_context>, AGENTS.md …)
🟡 yellow skill list (advertisements, name+description only)
🟠 orange skill loaded (full instructions injected)
🩷 pink user append (APPEND_SYSTEM.md, <addendum>)
🩵 cyan custom/extension sections
🔴 red forced / replaced prompts (forceSystemPrompt)

Install

Already installed at ~/.pi/agent/extensions/pi-lens/. Otherwise copy the directory there (or pi --extension ./index.ts during development), then start pi and open the URL shown in the status bar or run /lens.

Session focus

The timeline (and every tab) shows only the current pi session. pi-lens keeps one trace store per session id: switching sessions (new / resume / fork) starts a clean view, reloading the same session keeps its history. Events are stamped with their session id and the panel resets itself the moment pi switches.

Design principles

  • No decoration. Color is data: one hue per context type, nothing else is colored.
  • No constant refreshing. Append-only rendering, virtualized timeline (constant DOM), on-demand content fetch, 250ms batched SSE, Live/Pause toggle; opening a detail card suspends list updates and shows "N new · click to merge".
  • Tabs, not pages. One screen, detail cards for drill-down, scroll position preserved.

Layout

index.ts          pi extension entry: lifecycle, event wiring, /lens command
collector.ts      TraceStore: prompt builds, skill spans, tool calls, events
prompt-parser.ts  byte-accurate segmentation of pi's rendered system prompt
classify.ts       context kinds → colors (single source of truth)
server.ts         node:http REST + SSE on 127.0.0.1:7777
web/              vanilla JS panel (index.html, style.css, app.js)

API

GET /api/state · /api/prompt-builds · /api/skills · /api/timeline?before=&limit= · /api/content/:id (on-demand full text) · /api/events (SSE) · /api/export (JSON dump)

Tests

node --experimental-strip-types smoke.ts       # parser + collector + server
LENS_INDEX=~/.pi/agent/extensions/pi-lens/index.ts node --experimental-strip-types load-test.ts
                                               # full wiring through jiti (pi's loader)
S
Description
Session observability for pi: prompt assembly, skill traces, session state — local web panel
Readme
71 KiB
Languages
TypeScript 59.1%
JavaScript 28.3%
CSS 10.2%
HTML 2.4%