miadi-orchestration-kit

Pi session observability — T1 → T8

Local Pi package, version 0.1.1, tracked by #99. Canonical successor to /opt/binscripts/hooks/pi_hooks/event_capture.ts. Tested with Pi 1.4.0, Node 24 on gaia; Linux/Termux need bash, perl, jq (and tmux for terminal identity). No runtime npm dependencies, network calls, background processes, or timers inside the extension. No publishing or issue-creation hooks.

Install / cut over one account

KIT=/workspace/repos/jgwill/miadi-orchestration-kit
node "$KIT/pi/miadi-session-observability/scripts/migrate-settings.mjs"          # dry run
node "$KIT/pi/miadi-session-observability/scripts/migrate-settings.mjs" --apply  # account owner's consent
# Restart Pi if legacy capture came from CLI -e; otherwise /reload ONLY when idle.

The migration adds this local package to packages, removes legacy pi_hooks/event_capture.ts entries from extensions, and preserves unrelated settings/packages. It makes a private timestamped settings backup and refuses a concurrent edit. --agent-dir <dir> or PI_CODING_AGENT_DIR selects an alternate account configuration. No other accounts are changed.

Alternatively pi install "$KIT/pi/miadi-session-observability", then remove the old extension path in the same cutover. Both loaded means duplicate legacy capture. For an isolated one-invocation check: pi --no-extensions -e "$KIT/pi/miadi-session-observability"; this also disables other configured/built-in extensions for that invocation, not for ordinary operation.

The migration changes settings only. Check aliases/functions/wrappers for legacy -e /opt/binscripts/hooks/pi_hooks/event_capture.ts arguments too (for example, binscripts’ mpi). Remove that capture argument while preserving unrelated extensions; prefer an account-local launcher override during a one-account cutover. A CLI extension remains configured across /reload, so restart those Pi processes from the cleaned launcher instead. Eury cutover records a verified example.

Tide and miadi-chronicle annotate the same reply through /api/tide/annotate/*. With miadi-tide 0.1.11+, resolution uses this extension’s current PID/pane binding and native transcript_path. Multiple Pi sessions in one folder stay distinct. Missing, ended, or stale bindings are refused rather than replaced with another session. Capture and annotation must use the same session-data root.

A reload replaces the extension runtime, not the Pi process environment. New environment variables need a new process. The old binaries are not deleted; rollback restores the settings backup and starts/reloads Pi. Use the same session-data root.

Context capture diagnostic (0.1.1)

[miadi-pi-observability] context_with_system: capture failed means an observation was skipped; it does not stop Pi. A real conversation reproduced the failure at the sanitizer’s five-second deadline after earlier context snapshots had worked. Startup/shutdown smoke checks alone did not exercise this path.

The sanitizer now partitions JSON containers into 64 KiB groups and runs at most four sanitizing helpers concurrently per operation. Strings, including multiline private keys, stay whole; an indivisible large string can exceed the group size. Every helper keeps its original five-second deadline and output limit. Redaction still precedes storage bounds, numeric metadata is preserved, and no partial or raw fallback is persisted if any group fails. Diagnostics report a safe reason such as helper_timeout instead of guessing that permissions caused it.

For a local-path installation, an idle /reload or a fresh Pi process loads the fix. If the process was launched with the legacy capture -e, clean that launcher and restart when ready; /reload retains explicit CLI extensions. A running agent is not restarted automatically.

Files and contract

Root: MIADI_SESSION_DIR → MIADI_SESSIONDATA_ROOT → SESSION_DATA_ROOT → /src/_sessiondata when /src exists → ~/_sessiondata. Empty variables fall through. The session ID is Pi’s native session ID, never the tmux name.

<root>/<session-id>/
  _pi_hooks_all.jsonl               # legacy envelope: source,event,timestamp,session_id,cwd,payload
  _pi_<native-event>.jsonl          # per-event stream
  last_pi_<native-event>.json       # atomic last event
  _pi_observability.jsonl           # T8 ObsEvent-compatible journal (schema miadi/pi-observability@1)
  _terminal_binding.jsonl           # terminal, pid, argv, name, team, episode
  _transcript_final.jsonl           # sanitized incremental native Pi transcript, not Claude JSONL
  .pi-transcript-offset.json        # source offsets / rewrite fingerprint
  _pi_snapshots/<kind>-<sha>.json    # content-addressed prompt, context, direct-write snapshots
  _pi_files_written.jsonl           # direct write/edit outcomes, call IDs and snapshot refs
  .pi-forward-<endpoint-hash>.json   # dashboard acknowledgement cursor

Bindings also append to <root>/data/terminal_bindings.jsonl. Reload start/end do not write false terminal lifecycle lines. Pi session renames do; a resumed process writes a real start. agent_start observes a missing binding. Team/episode resolution stays with the shared writer, not another TS implementation. T8 can join the journal’s miadi.{team,episode,tmux,launch_alias} to those lines. Correlation is mirrored in payload.miadi so the reference dashboard’s opaque payload storage preserves it. miadi.run_id separates runs whose native turn indices restart; miadi.prompt_id references the most recently observed input event.

Canonical events: session_start/shutdown, agent_start/end, turn_start/end, user_message, assistant_message, tool_call/result, model_change, error, compaction, branch_nav, and custom (context_snapshot, session_name, agent_settled). Native lifecycle events remain separately available. before_agent_start supplies one canonical agent_start; the native agent_start is not counted a second time.

Bounds and privacy

Package-owned capture directories are 0700, files 0600; shared binding helpers create new files under a private umask but leave pre-existing shared binding-file permissions unchanged. The bundled shared sanitizer runs before persistence; failure skips capture and emits a generic stderr diagnostic, never falls back to raw content or interrupts tools. It redacts recognized credential shapes, not all possible confidential prose; prefixes remain. Observe only trusted local sessions and treat captures as private. No .env autoloading, provider-header collection, or remote forwarding.

Binary image/audio bodies are omitted. Legacy strings default to 64 KiB, normalized strings/snapshots to 128 KiB; containers to 256 entries and depth 16; records/snapshot documents to 1 MiB (oversize documents get an explicit omission receipt). Transcript lines over 1 MiB get an omission line. Agent-end raw capture stores a count rather than recopying the whole run; turn-end raw capture omits repeated boundary context/tool results. These are deliberate bounded differences from binscripts. Snapshot bounded:true always discloses that policy; this is not an unlimited forensic archive.

Successful native write/edit captures regular text files up to 256 KiB, with sanitized content-addressed snapshots and credential-name exclusions. Credential paths are indexed, not copied into snapshots. Symlink files and binary files are not copied. Bash/MCP writes, subagent transcripts, Claude publications/Artifact-comment/btw indices, clone guard and fork commands are not ported. Tool arguments/results and transcript content still receive pattern-based sanitization, including when a tool edits a credential file; the filename exclusion is not a guarantee that arbitrary passwords elsewhere cannot appear. The transcript is Pi-native and must not be fed to a Claude-only token parser.

There is no automatic deletion. Retention is an explicit remaining policy in #83; repeated bounded context snapshots can still accumulate. No upload occurs without a separately started forwarder.

Local dashboard / replay

T8 owns the dashboard; this package owns its durable input. Run the existing reference server only, not its demo stack or recipes which clear ports:

export OBS_HOST=127.0.0.1 OBS_PORT=43190
export OBS_AUTH_TOKEN="$(openssl rand -hex 24)"        # keep private; same token for forwarder
export OBS_DB_PATH="$HOME/.local/share/miadi/pi-observability/obs.db"
cd /workspace/repos/miadisabelle/pi-agent-observability
bun run apps/observability/server.ts

In another terminal (with the same token, or OBS_TOKEN_FILE pointing to a private file):

node "$KIT/pi/miadi-session-observability/scripts/forward.mjs" --session <pi-session-id> --watch

Select sessions explicitly; repeat --session for more. --root selects another capture root, --url another loopback server. Without --watch, drain pending events and exit. Durable cursor advances only after acknowledgement. Restart/network outage retries the same event IDs; rejected IDs are verified against the server so sequence conflicts are not silently dropped. Delete the endpoint cursor to replay into a rebuilt database. Agent capture is unaffected by forwarder failure.

Open http://127.0.0.1:43190/?token=<private-token>#view=single&sid=<pi-session-id>. Remote browser: ssh -L 43190:127.0.0.1:43190 mia@gaia. Never bind the reference server to a public interface; its static routes/CORS and development authentication are not a production security boundary. See HANDOFF-T8.md for this session’s service and transition.

Validation and ownership

npm test --prefix pi/miadi-session-observability
bash pi/miadi-session-observability/hooks/secret_capture_sanitizer.sh --self-test
python3 pi/miadi-session-observability/tests/rpc-smoke.py  # installed Pi, no model/API calls
python3 pi/miadi-session-observability/tests/context-smoke.py  # real context hook, local deterministic provider only
# Optional, against a running loopback reference dashboard; adds a labelled synthetic fixture row:
OBS_TOKEN_FILE=<private-token-file> node pi/miadi-session-observability/tests/dashboard-smoke.mjs

hooks/secret_capture_sanitizer.sh is byte-for-byte from claude/miadi-session-observability/hooks/; propagate fixes from that canonical sanitizer. hooks/terminal_binding.sh is the shared non-Claude writer, migrated byte-for-byte from /opt/binscripts/hooks/terminal_binding.sh; this is its kit home. Keep binscripts’ live copy and other capture packages aligned when changing shared rules. Do not replace it with Claude’s agent-specific writer.

Steering

extensions/steering-inbox.ts is the session’s steering inbox (jgwill/Miadi#942). A person’s correction reaches the running agent through Pi’s own steering queue, pi.sendUserMessage(text, { deliverAs: "steer" }): after the current assistant turn has run its tool calls, before the next model request. An idle session starts a run by itself. Nothing is typed, and no running tool is touched. lib/steering_inbox.mjs is the file every provider’s adapter carries, copied by scripts/sync-steering-inbox.sh. The contract is miadi.steering/1, in @miadi/connector’s STEERING.md. A session started before this package loaded answers unsupported.