feat: package Pi extensions as Bun workspaces

This commit is contained in:
jb
2026-09-01 17:24:46 -04:00
parent 90f8090f61
commit 118b5bee06
214 changed files with 19566 additions and 5876 deletions
+3
View File
@@ -1,6 +1,9 @@
node_modules/
coverage/
dist/
__pycache__/
*.py[cod]
*.tgz
*.log
.DS_Store
.mesh
+1
View File
@@ -0,0 +1 @@
@_jb:registry=https://git.jb.codes/api/packages/jb/npm/
+52 -62
View File
@@ -9,14 +9,12 @@ Personal [Pi](https://github.com/earendil-works/pi-mono) extensions, skills, glo
- global agent instructions for Pi, Codex, and Claude;
- 34 shared skills under `~/.agents/skills/`;
- the `manage-pi-links` and `nono-profiles` project-only skills under `.pi/skills/`;
- five global Pi extensions under `~/.pi/agent/extensions/`:
- three global Pi extensions under `~/.pi/agent/extensions/`:
- **dictate**: local Linux voice dictation;
- **effort-controls**: `/effort` reasoning selection and `/fast` priority mode;
- **pi-workflow**: persisted, gated feature workflows;
- **prompt-snippets**: per-message prompt rule toggles;
- **update**: `/update` and `--update` support.
- **prompt-snippets**: per-message prompt rule toggles.
The repository also contains `copy-all`, `file-search`, and `subagents` extensions, but the normal manifest does not install them globally. The `pptx`, `stop-slop`, `subagents`, and `xlsx` skills are also intentionally excluded. Archived resources under `__archived/` are never installed. Update `config/pi-links.json` deliberately if that policy changes.
The repository also contains `copy-all`, `file-search`, `interactive-subagents`, `lm-studio`, `native-hooks`, `subagents`, `todo-herdr`, `update`, and the private `workflow` extension, but the normal manifest does not install them globally. The `pptx`, `stop-slop`, `subagents`, and `xlsx` skills are also intentionally excluded. Archived resources under `__archived/` are never installed. Update `config/pi-links.json` deliberately if that policy changes.
## Fresh-machine setup
@@ -27,7 +25,8 @@ The commands below reproduce the normal setup. They assume Linux, a systemd user
Required:
- Git with access to `git.jb.codes`;
- Node.js 22.19 or newer with npm;
- Node.js 22.19 or newer;
- Bun 1.3.14 or newer;
- Pi Coding Agent;
- a configured model provider.
@@ -84,11 +83,11 @@ The linked TypeScript extensions resolve runtime dependencies from this checkout
```bash
cd /srv/data/code/harness-tools
npm ci
npm run typecheck
bun install --frozen-lockfile
bun run typecheck
```
Use `npm ci` after pulling a lockfile change. Node.js must satisfy the version in [`package.json`](package.json).
Use `bun install --frozen-lockfile` after pulling a lockfile change and plain `bun install` only when intentionally updating [`bun.lock`](bun.lock). Node.js and Bun must satisfy the versions in [`package.json`](package.json).
### 4. Install instructions, skills, and extensions
@@ -109,7 +108,7 @@ A successful check currently reports 44 links and zero local package entries. Th
Resolve any reported collision manually instead of deleting it blindly. Run `/reload` in an active Pi session, or restart Pi, after the sync.
Do not also run `pi install /path/to/harness-tools` for the normal setup. That installs the root as a Pi package and applies the broader `package.json` resource globs, which is different from the curated `config/pi-links.json` policy and can duplicate resources. It remains useful for a temporary package-development test in an isolated Pi agent directory.
The repository root is a private Bun workspace and is not installable as a Pi package. Use the curated `config/pi-links.json` policy for live development, or install one independently published `@_jb/pi-*` package. See [Bun workspace packages and Gitea npm publishing](docs/npm-packages.md).
### 5. Verify Pi
@@ -120,44 +119,39 @@ pi
Inside Pi:
1. Run `/reload`.
2. Confirm `/effort`, `/snippets`, `/update`, and `/workflow` are available.
2. Confirm `/effort` and `/snippets` are available.
3. Ask Pi to list its available skills and confirm a shared skill such as `tdd` appears.
4. From this repository, confirm the project-only `manage-pi-links` and `nono-profiles` skills appear.
## Additional Pi packages from Gitea
## Optional Pi packages from Gitea
Two extensions are maintained in separate Gitea repositories rather than `harness-tools`. Install them as Pi-managed Git packages after completing the canonical link sync above. Pi clones each package into its agent directory and installs its package dependencies.
The monorepo contains three optional packages that are not enabled by the curated link policy:
Run these commands from a normal host terminal. A restrictive nono session may intentionally lack the SSH key or agent socket needed to clone private repositories.
- `@_jb/pi-interactive-subagents`;
- `@_jb/pi-native-hooks`;
- `@_jb/pi-todo-herdr`.
### Verify Gitea SSH access
After publishing them, configure the scoped Gitea npm registry and install only the packages you want. Keep the token in the user environment or user-level npm configuration; never commit it:
Both repositories use SSH on port 7722. Confirm access without changing either checkout:
```bash
GIT_TERMINAL_PROMPT=0 git ls-remote \
ssh://git@git.jb.codes:7722/jb/pi-interactive-subagents.git HEAD
GIT_TERMINAL_PROMPT=0 git ls-remote \
ssh://git@git.jb.codes:7722/jb/pi-todo-herdr.git HEAD
```ini
@_jb:registry=https://git.jb.codes/api/packages/jb/npm/
//git.jb.codes/api/packages/jb/npm/:_authToken=${GITEA_NPM_TOKEN}
```
Each command should print a commit hash and `HEAD`. Configure the machine's SSH key and `git.jb.codes` host key before continuing if either command fails.
### Install both packages
```bash
pi install ssh://git@git.jb.codes:7722/jb/pi-interactive-subagents.git
pi install ssh://git@git.jb.codes:7722/jb/pi-todo-herdr.git
pi install npm:@_jb/pi-interactive-subagents
pi install npm:@_jb/pi-native-hooks
pi install npm:@_jb/pi-todo-herdr
pi list
```
Confirm both Gitea URLs appear under `User packages`, then restart Pi or run `/reload`. These package entries are independent of `config/pi-links.json`, so `./scripts/pi-links.mjs check` still reports zero local package entries.
Restart Pi or run `/reload` afterward. These package entries remain independent of `config/pi-links.json`, so `./scripts/pi-links.mjs check` still reports zero local package entries.
Do not globally enable `harness-tools/extensions/subagents` at the same time as `pi-interactive-subagents`. They provide overlapping subagent tools. The curated harness-tools manifest leaves its in-repository implementation disabled for this reason.
### Configure interactive subagents
[`pi-interactive-subagents`](https://git.jb.codes/jb/pi-interactive-subagents) creates asynchronous child Pi sessions in Herdr tabs or tmux panes. It requires one terminal backend:
[`@_jb/pi-interactive-subagents`](extensions/interactive-subagents) creates asynchronous child Pi sessions in Herdr tabs or tmux panes. It requires one terminal backend:
- **Herdr:** install its Pi integration, then launch Pi inside a Herdr-managed pane;
- **tmux:** launch Pi inside tmux when Herdr is unavailable.
@@ -188,11 +182,15 @@ Inside Pi, verify and try the extension:
4. Run `/subagent scout Summarize this repository` from a disposable test checkout.
5. Confirm a Herdr tab or tmux pane opens and the completed result returns to the parent session.
The status widget is enabled by the package's example configuration when no local `config.json` exists. Copy and edit `config.json.example` beside the installed package only when a status override is needed. Treat Pi's managed Git checkout as disposable because package updates may reset it; prefer global or project agent overrides for durable customization.
The status widget is enabled by the package's example configuration when no local `config.json` exists. Copy and edit `config.json.example` beside the installed package only when a status override is needed. Treat Pi's managed package installation as disposable because package updates may reset it; prefer global or project agent overrides for durable customization.
### Configure Native Hooks
[`@_jb/pi-native-hooks`](extensions/native-hooks) coordinates trusted personal diagnostic and verification policies after workspace mutations. Review its [trust model and initialization workflow](extensions/native-hooks/README.md) before use. Preview with `/hooks-init --dry-run`; do not run `/hooks-init --apply` until every destination and executable policy has been reviewed.
### Configure Pi Todo Herdr
[`pi-todo-herdr`](https://git.jb.codes/jb/pi-todo-herdr) provides the `set_tasks`, `update_task`, `rm_task`, `list_task`, and `get_task` tools plus a session-local Pi task widget. Herdr is optional for the tools and widget.
[`@_jb/pi-todo-herdr`](extensions/todo-herdr) provides the `set_tasks`, `patch_tasks`, `rm_task`, `list_task`, and `get_task` tools plus a session-local Pi task widget. Herdr is optional for the tools and widget.
Inside Pi after `/reload`:
@@ -224,41 +222,33 @@ herdr status client
### Update or remove the Gitea packages
Use the same source strings stored by Pi:
```bash
pi update ssh://git@git.jb.codes:7722/jb/pi-interactive-subagents.git
pi update ssh://git@git.jb.codes:7722/jb/pi-todo-herdr.git
```
pi update npm:@_jb/pi-interactive-subagents
pi update npm:@_jb/pi-native-hooks
pi update npm:@_jb/pi-todo-herdr
Remove them with:
```bash
pi remove ssh://git@git.jb.codes:7722/jb/pi-interactive-subagents.git
pi remove ssh://git@git.jb.codes:7722/jb/pi-todo-herdr.git
pi remove npm:@_jb/pi-interactive-subagents
pi remove npm:@_jb/pi-native-hooks
pi remove npm:@_jb/pi-todo-herdr
```
Run `/reload` after an update or removal.
### Optional development checkouts
### Imported source provenance
A Pi-managed package clone is enough for normal use. For source development, use separate durable checkouts so Pi package updates cannot discard work:
The three packages were imported as snapshots of their source repositories' `main` branches. [`config/imported-packages.json`](config/imported-packages.json) records each source and exact commit. Check for upstream movement without changing this checkout:
```bash
mkdir -p /srv/data/repo-store
git clone ssh://git@git.jb.codes:7722/jb/pi-interactive-subagents.git \
/srv/data/repo-store/pi-interactive-subagents
git clone ssh://git@git.jb.codes:7722/jb/pi-todo-herdr.git \
/srv/data/repo-store/pi-todo-herdr
npm ci --prefix /srv/data/repo-store/pi-interactive-subagents
npm test --prefix /srv/data/repo-store/pi-interactive-subagents
npm ci --prefix /srv/data/repo-store/pi-todo-herdr
npm run typecheck --prefix /srv/data/repo-store/pi-todo-herdr
npm test --prefix /srv/data/repo-store/pi-todo-herdr
bun run imports:check
```
Both repositories are separate projects. Keep their changes and releases out of the `harness-tools` checkout.
Stage fresh snapshots outside the repository for review and reconciliation:
```bash
bun scripts/import-package-snapshots.mjs stage all --output /tmp/harness-tools-imports
```
The stage command deliberately does not overwrite monorepo files. Review upstream changes, preserve the scoped package manifests and catalog dependencies, run the full validation suite, and then update the recorded commit.
## Companion tools used by installed skills
@@ -475,11 +465,11 @@ After pulling repository changes:
```bash
cd /srv/data/code/harness-tools
git pull --rebase
npm ci
bun install --frozen-lockfile
./scripts/pi-links.mjs sync --dry-run
./scripts/pi-links.mjs sync
./scripts/pi-links.mjs check
npm run typecheck
bun run typecheck
```
Run `/reload` after skill, extension, instruction, or separately installed Gitea package changes. Re-run `./services/dictate/install.sh` when Dictate's host service or protocol changes.
@@ -497,9 +487,9 @@ Only run `./scripts/pi-links.mjs prune` after reviewing every removal. The manag
Extensions are TypeScript modules loaded directly by Pi; no build step is required.
```bash
npm ci
npm run typecheck
npm test
bun install --frozen-lockfile
bun run typecheck
bun run test
```
For a one-off extension development run:
@@ -514,7 +504,7 @@ pi -e ./extensions/update
```text
config/ Installation and launcher manifests
docs/ Usage guides, plans, and ADRs
extensions/ Pi extensions
extensions/ Bun workspace packages for Pi extensions and shared runtime code
profiles/nono/ Repository-managed nono profiles
scripts/ Link manager and sandbox launch helpers
services/ Host-side services used by extensions
+1007
View File
File diff suppressed because it is too large Load Diff
+26
View File
@@ -0,0 +1,26 @@
{
"version": 1,
"packages": [
{
"name": "pi-interactive-subagents",
"source": "ssh://git@git.jb.codes:7722/jb/pi-interactive-subagents.git",
"branch": "main",
"commit": "a3cd328edef59ad491d4f35f9fbadf8ec80cadc4",
"target": "extensions/interactive-subagents"
},
{
"name": "pi-native-hooks",
"source": "ssh://git@git.jb.codes:7722/jb/pi-native-hooks.git",
"branch": "main",
"commit": "73fa3299bf30fd6da5abfc74a0ee7a36e0a07f0a",
"target": "extensions/native-hooks"
},
{
"name": "pi-todo-herdr",
"source": "ssh://git@git.jb.codes:7722/jb/pi-todo-herdr.git",
"branch": "main",
"commit": "d9e1ffec51e25238e0baec48ce1303893f13849f",
"target": "extensions/todo-herdr"
}
]
}
@@ -2,9 +2,9 @@
status: accepted
---
# Build pi-workflow around persisted policy gates
# Build workflow around persisted policy gates
`pi-workflow` will be a new extension rather than an activation or rename of `_workflows`. It will expose one command-style tool backed by atomic revisioned snapshots, Markdown Agent Profiles, deterministic eligibility followed by planner selection, a mandatory feature pipeline, and restricted feature or read-only analysis scripts. Every feature run receives a distinct exclusive worktree. Concurrency defaults to 8 under an immutable process-wide maximum of 32. Non-trivial UI work requires at least three structurally distinct prototypes and explicit user acceptance. Before implementation, the user chooses a fresh plan-seeded session, compaction of the current session that prefers `smart_compact` and falls back to normal Pi compaction when unavailable, or unchanged current context. Review uses a fresh session and excludes every Agent Profile that mutated the reviewed diff.
`workflow` will be a new extension rather than an activation or rename of `_workflows`. It will expose one command-style tool backed by atomic revisioned snapshots, Markdown Agent Profiles, deterministic eligibility followed by planner selection, a mandatory feature pipeline, and restricted feature or read-only analysis scripts. Every feature run receives a distinct exclusive worktree. Concurrency defaults to 8 under an immutable process-wide maximum of 32. Non-trivial UI work requires at least three structurally distinct prototypes and explicit user acceptance. Before implementation, the user chooses a fresh plan-seeded session, compaction of the current session that prefers `smart_compact` and falls back to normal Pi compaction when unavailable, or unchanged current context. Review uses a fresh session and excludes every Agent Profile that mutated the reviewed diff.
This keeps the caller interface small while making approval, prototype, recovery, and reviewer-independence rules enforceable in one workflow engine. We rejected unrestricted custom graphs because they could bypass policy, a general registry architecture because it adds speculative extension points, and event-sourced persistence because revisioned snapshots already provide the required recovery and audit data with less machinery.
@@ -1,3 +0,0 @@
# Reuse standard agents through workflow role overlays
`pi-workflow` will reuse standard `pi-interactive-subagents` definitions instead of duplicating their prompts and model settings in workflow-only agents. Explicit configuration maps standard agent names to workflow roles, while `pi-workflow` retains ownership, tool-intersection, Gate, verification, and reviewer-independence policy. The default maps `scout` to Discovery and `worker` to Implementation; bundled profiles remain fallbacks for roles without a standard mapping. This keeps one source of truth for agent identity without allowing arbitrary prompt prose or agent names to imply mutability or workflow authority.
@@ -1,11 +1,11 @@
---
status: accepted
supersedes: 0001-pi-workflow-architecture-24AUG2026.md
supersedes: 0001-workflow-architecture-24AUG2026.md
---
# Use drill-down overlays and bounded recovery
This decision supersedes the dashboard, dirty-source, execution-timeout, and startup-ownership assumptions in [ADR 0001](./0001-pi-workflow-architecture-24AUG2026.md). The persisted gated pipeline, worktree isolation, and independent-review decisions from ADR 0001 remain accepted.
This decision supersedes the dashboard, dirty-source, execution-timeout, and startup-ownership assumptions in [ADR 0001](./0001-workflow-architecture-24AUG2026.md). The persisted gated pipeline, worktree isolation, and independent-review decisions from ADR 0001 remain accepted.
## Context
@@ -39,7 +39,7 @@ Static prototypes compared a centered compact modal, a near-full-screen workspac
- Dead ownership can recover without manual state surgery. A running process or Assignment cannot be displaced based only on an old heartbeat.
- Users may proceed from a dirty source checkout without copying local changes into the worktree. Persisted evidence records what was knowingly omitted.
- Git, host-command cleanup, and child execution now have bounded failure paths rather than indefinite waits.
- Direction C has automated renderer and orchestration coverage. Its completed manual application validation is recorded below. The static frames in `docs/pi-workflow/dashboard-mocks.md` remain labeled prototype evidence, not production screenshots.
- Direction C has automated renderer and orchestration coverage. Its completed manual application validation is recorded below. The static frames in `docs/workflow/dashboard-mocks.md` remain labeled prototype evidence, not production screenshots.
## Manual application validation
@@ -0,0 +1,3 @@
# Reuse standard agents through workflow role overlays
`workflow` will reuse standard `pi-interactive-subagents` definitions instead of duplicating their prompts and model settings in workflow-only agents. Explicit configuration maps standard agent names to workflow roles, while `workflow` retains ownership, tool-intersection, Gate, verification, and reviewer-independence policy. The default maps `scout` to Discovery and `worker` to Implementation; bundled profiles remain fallbacks for roles without a standard mapping. This keeps one source of truth for agent identity without allowing arbitrary prompt prose or agent names to imply mutability or workflow authority.
@@ -0,0 +1,21 @@
# Package Pi extensions as a Bun workspace catalog
## Decision
Keep extension sources under `extensions/*` and make each maintained extension directory a Bun workspace with its own package manifest. Use the `@_jb/pi-*` namespace, a root Bun catalog for external dependency versions, and `workspace:^` for repository-owned libraries. The repository root is private and is no longer itself a Pi package.
Publish packages to the `jb` owner endpoint of the Gitea npm registry. Repository configuration maps `@_jb` to `https://git.jb.codes/api/packages/jb/npm/`; credentials remain in the user environment or user-level npm configuration and are never committed.
`extensions/shared` is the publishable `@_jb/pi-shared` library. Extensions that import it declare a normal runtime dependency so Pi's production package installation receives the required code.
`extensions/_workflows` is reference code, not a package or publishable extension. `@_jb/pi-workflow` remains a private workspace until the required reference runtime is merged under `extensions/workflow`; this prevents publishing two workflow packages or publishing an incomplete package.
The existing `config/pi-links.json` policy remains the authority for live-development exposure. Package boundaries and publication do not automatically enable extensions globally.
## Consequences
- Bun owns dependency installation and `bun.lock`; `package-lock.json` is removed.
- Catalog and workspace protocols require `bun publish` or `bun pm pack` before npm publication so concrete versions are written into package metadata.
- Shared libraries must be published before extensions that depend on the same new version.
- Pi packages continue shipping TypeScript source because Pi loads extension entrypoints directly.
- The `workflow` merge is an explicit blocker for its first release.
@@ -0,0 +1,25 @@
# Import external Pi packages as reviewed main-branch snapshots
## Decision
Import the tracked files from the `main` branch of these repositories into Bun workspaces:
| Source | Imported commit | Workspace | Package |
| --- | --- | --- | --- |
| `jb/pi-interactive-subagents` | `a3cd328edef59ad491d4f35f9fbadf8ec80cadc4` | `extensions/interactive-subagents` | `@_jb/pi-interactive-subagents` |
| `jb/pi-native-hooks` | `73fa3299bf30fd6da5abfc74a0ee7a36e0a07f0a` | `extensions/native-hooks` | `@_jb/pi-native-hooks` |
| `jb/pi-todo-herdr` | `d9e1ffec51e25238e0baec48ce1303893f13849f` | `extensions/todo-herdr` | `@_jb/pi-todo-herdr` |
Carry snapshots rather than submodules, subtrees, or unrelated Git histories. The monorepo owns package manifests, dependency normalization, compatibility changes, tests, and releases after import. Preserve source attribution in each package.
Record source URLs, branches, and commits in `config/imported-packages.json`. `bun run imports:check` compares those commits with current remote `main` heads. The staging command in `scripts/import-package-snapshots.mjs` clones fresh snapshots outside the repository for explicit review; it never overwrites the maintained workspaces.
Rename the private `pi-workflow` extension identity to `workflow`. Its workspace is `extensions/workflow`, its tool and user-facing runtime identifiers use `workflow`, and its configuration/state paths use the same name. The npm package remains `@_jb/pi-workflow` under the repository's `@_jb/pi-<name>` convention.
## Consequences
- The three imported extensions share the Bun lockfile and catalog with the rest of the monorepo.
- Published packages use the scoped Gitea registry instead of Pi-managed Git sources.
- Upstream movement is detected but never merged automatically; imported changes require reconciliation with monorepo-specific manifests and APIs.
- Only `main` is tracked. Source tags, branches, and historical commits are not copied.
- The renamed `workflow` extension remains private until its `_workflows` dependencies are merged and isolated packaging succeeds.
+13 -13
View File
@@ -94,7 +94,7 @@ Node/Bun Python/Rust/Go/Java local integrations child isol
| | | |
+--------------------+--------------------------+---------------------+
v
Slice 6B2: pi-workflow/toolchain broker
Slice 6B2: workflow/toolchain broker
|
v
Slice 7A: Docker/browser Slice 7B: release/production Slice 7C: maintenance
@@ -155,7 +155,7 @@ Slices in the same row may proceed in parallel after their listed dependencies a
- one selected provider route;
- no package registries, SSH, Docker, browser, microphone, deployment credentials, or broad localhost access.
4. Separate extension source from extension state. Confirm that normal sessions cannot modify global instructions, installed extension code, authentication state, other Pi profiles, or sibling repositories.
5. Materialize the normal in-process extension allowlist as a generated, isolated Pi `settings.json` under the selected personal/work state root. Its package/extension entries come only from `config/pi-package-lock.json` and the trusted repository map. Disable `pi-ask` arbitrary notification commands, `/update`, `pi-workflow`, background shell jobs, and any privileged direct spawn path until a controlled subprocess policy exists.
5. Materialize the normal in-process extension allowlist as a generated, isolated Pi `settings.json` under the selected personal/work state root. Its package/extension entries come only from `config/pi-package-lock.json` and the trusted repository map. Disable `pi-ask` arbitrary notification commands, `/update`, `workflow`, background shell jobs, and any privileged direct spawn path until a controlled subprocess policy exists.
6. Import the unmanaged `~/.pi/agent/extensions/lm-studio.ts` implementation into a repository-managed extension at `extensions/lm-studio/index.ts`, lock its source revision, and change its fixed base URL contract to `http://127.0.0.1:1234/v1`. Maintenance moves the old extension to the quarantined state store and removes its active settings entry. A duplicate-provider test fails if the old source, another `lm-studio` registration, or `localhost` base URL remains loadable.
7. Define and own stable profile fragments for `provider-lm-studio`, `provider-openai`, `provider-anthropic`, and `provider-morph`, granting only one route at launch. Prove nono 0.74's `tcp_connect_ports` behavior with real loopback fixtures listening on ports 1234 and 1235: the selected provider must connect to 1234 while 1235 and every other loopback port fail. Do not use hostname-only `allow_domain` as evidence of port restriction. If the fixture cannot prove exact enforcement, `provider-lm-studio` remains unavailable until a separately reviewed mediated relay is designed.
@@ -165,7 +165,7 @@ Slices in the same row may proceed in parallel after their listed dependencies a
- The selected provider works while non-selected provider hosts, raw key environment names, and credentials are absent from the environment and process table.
- Reads outside the exact Pi state, declared active-worktree access, and per-launch temporary paths fail; writes to a read-only active worktree fail; another session's temporary directory is inaccessible.
- LM Studio, when selected, reaches only `127.0.0.1:1234`.
- `/update`, `pi-workflow`, arbitrary notification commands, and unbrokered background shell jobs are unavailable.
- `/update`, `workflow`, arbitrary notification commands, and unbrokered background shell jobs are unavailable.
- The repository-managed LM Studio provider uses only the exact `127.0.0.1:1234` base URL, the old unmanaged source is quarantined, duplicate registration fails, and a real two-port fixture proves all other loopback ports are denied.
- All base and provider profiles pass strict validation and resolved-policy review.
@@ -342,7 +342,7 @@ A failed pilot returns to the owning slice. Do not compensate by broadening the
**Owner surfaces:** `profiles/nono/{node-npm,pnpm,bun}.json` and disjoint tests under `tests/nono/capabilities/{node-npm,pnpm,bun}/`.
**Work:** grant each command the active repository, exact cache/store paths, required registry and artifact hosts, and a token-free normal configuration. Separate install/build/test from publish. Cover lockfile-respecting commands used by the mapped repositories. Treat npm/pnpm/Bun lifecycle scripts and `npm test`, `npm run`, `pnpm run`, and equivalents as shell-spawning paths: prove their children inherit the package-manager command sandbox and cannot invoke SSH, Docker, browsers, deployment tools, sibling repositories, raw credentials, or an alternate package binary. Keep `pi-workflow` disabled until Slice 6B2 integrates its executor with these completed policies.
**Work:** grant each command the active repository, exact cache/store paths, required registry and artifact hosts, and a token-free normal configuration. Separate install/build/test from publish. Cover lockfile-respecting commands used by the mapped repositories. Treat npm/pnpm/Bun lifecycle scripts and `npm test`, `npm run`, `pnpm run`, and equivalents as shell-spawning paths: prove their children inherit the package-manager command sandbox and cannot invoke SSH, Docker, browsers, deployment tools, sibling repositories, raw credentials, or an alternate package binary. Keep `workflow` disabled until Slice 6B2 integrates its executor with these completed policies.
**Acceptance criteria:** installs, builds, and tests work in representative npm, pnpm, and Bun fixtures; adversarial lifecycle/run scripts remain inside the command policy; sibling repositories, unrelated caches, privileged commands, and raw credentials remain unavailable; publish credentials and commands are absent outside release mode.
@@ -381,32 +381,32 @@ A failed pilot returns to the owning slice. Do not compensate by broadening the
## Slice 6B1: Child-agent and background-task isolation
**Goal:** isolate spawned Pi, Claude, Codex, bounded background jobs, Herdr panes, and tmux panes without enabling repository toolchain execution or `pi-workflow`.
**Goal:** isolate spawned Pi, Claude, Codex, bounded background jobs, Herdr panes, and tmux panes without enabling repository toolchain execution or `workflow`.
**Owner surfaces:** `profiles/nono/{child-pi,child-claude,child-codex,background-jobs}.json`, new spawn-wrapper sources under `scripts/nono/child-agents/`, isolated child-state configuration, and `tests/nono/capabilities/child-agents/`. Coordinate with extension owners but avoid editing Slice 6A's socket fragments or shared extension files concurrently.
**Work:**
1. Inventory every spawn path in active subagent, `pi-ask`, update, and background-task extensions. Classify each in-process operation as safe UI/state access, disabled behavior, or a brokered subprocess. Keep `pi-workflow` absent from generated settings.
1. Inventory every spawn path in active subagent, `pi-ask`, update, and background-task extensions. Classify each in-process operation as safe UI/state access, disabled behavior, or a brokered subprocess. Keep `workflow` absent from generated settings.
2. Route child Pi, Claude, and Codex through command policies with isolated child state and the immutable provider selected by Slice 3.
3. Broker only named background jobs and notification commands that do not invoke repository toolchains; keep arbitrary commands, package updates, and direct privileged spawn paths disabled.
4. Define caller relationships and prohibit privileged-command escalation through child commands.
5. Verify Herdr and tmux targets launch under equivalent or narrower generated policies before enabling command delivery.
6. Add containment tests for nested children, background completion, cancellation, inherited environment, provider override denial, notification commands, package-update denial, and artifact paths.
**Acceptance criteria:** child commands receive no broader filesystem, network, credential, socket, environment, or provider access than declared; nested child tests prove containment; unsandboxed Herdr/tmux targets are rejected; bounded extension behavior remains functional without `pi-workflow` or toolchain execution.
**Acceptance criteria:** child commands receive no broader filesystem, network, credential, socket, environment, or provider access than declared; nested child tests prove containment; unsandboxed Herdr/tmux targets are rejected; bounded extension behavior remains functional without `workflow` or toolchain execution.
**Dependencies:** pilot acceptance. May run in parallel with Slices 5A, 5B, and 6A.
## Slice 6B2: `pi-workflow` and toolchain integration
## Slice 6B2: `workflow` and toolchain integration
**Goal:** enable `pi-workflow` only after every toolchain command it can invoke has a completed command policy.
**Goal:** enable `workflow` only after every toolchain command it can invoke has a completed command policy.
**Owner surfaces:** `profiles/nono/pi-workflow.json`, workflow spawn-wrapper sources under `scripts/nono/child-agents/pi-workflow/`, the generated extension allowlist entry, and `tests/nono/capabilities/pi-workflow/`.
**Owner surfaces:** `profiles/nono/workflow.json`, workflow spawn-wrapper sources under `scripts/nono/child-agents/workflow/`, the generated extension allowlist entry, and `tests/nono/capabilities/workflow/`.
**Work:** route `pi-workflow` executable resolution and every npm/pnpm/Bun/Python/Rust/Go/Maven/Gradle task through the completed Slice 5 policies. Include lifecycle/run scripts, nested package executables, verification commands, and cancellation. Any unmodeled executable or repository without the required toolchain fragment fails before workflow execution.
**Work:** route `workflow` executable resolution and every npm/pnpm/Bun/Python/Rust/Go/Maven/Gradle task through the completed Slice 5 policies. Include lifecycle/run scripts, nested package executables, verification commands, and cancellation. Any unmodeled executable or repository without the required toolchain fragment fails before workflow execution.
**Acceptance criteria:** adversarial workflow tasks cannot bypass toolchain policies, select another provider, invoke high-risk commands, read sibling repositories, or access raw credentials; repositories lacking a required toolchain capability do not load `pi-workflow`.
**Acceptance criteria:** adversarial workflow tasks cannot bypass toolchain policies, select another provider, invoke high-risk commands, read sibling repositories, or access raw credentials; repositories lacking a required toolchain capability do not load `workflow`.
**Dependencies:** Slices 5A, all applicable 5B language fragments, and Slice 6B1. It does not run in parallel with the toolchain policy it consumes.
@@ -479,7 +479,7 @@ To keep parallel work mergeable:
| Base filesystem | Active registered worktree has exactly its mapped read-only or read/write access; exact mutable Pi state and the per-launch temporary directory are writable | A read-only worktree cannot be written; broad `/tmp`, runtime parents, other sessions, sibling or unregistered worktrees, global instructions, extension source, other Pi state, `~/.ssh`, and `~/.config/gh` are inaccessible or read-only as designed |
| Temporary lifecycle | Normal exit and cancellation remove the private runtime directory; approved active use remains within its aggregate bound | Cleanup preserves a held lease, matching PID/start time, populated cgroup, live unit, or inconclusive identity; proven crash leftovers expire, unsafe paths survive for review, and an active session cannot exhaust `/run` |
| Worktree lifecycle | Broker creates, registers, resolves, and safely removes an external clean worktree | Broad worktree-root access, age-only deletion, dirty/untracked/unpushed removal, active-worktree removal, and linked-worktree confidentiality claims fail |
| Environment/extensions | Reviewed extensions and selected mediated provider credential work | Raw credential variables, old LM Studio source, duplicate providers, `/update`, `pi-workflow` before 6B2, arbitrary notification commands, and unbrokered shell jobs are absent |
| Environment/extensions | Reviewed extensions and selected mediated provider credential work | Raw credential variables, old LM Studio source, duplicate providers, `/update`, `workflow` before 6B2, arbitrary notification commands, and unbrokered shell jobs are absent |
| Provider | Trusted default or one pre-boundary approved flag selects the route; LM Studio reaches port 1234 | Ambiguous/post-boundary/child overrides, non-selected providers, LM Studio port 1235, and broad localhost fail |
| Git/SSH | Fetch/push uses the correct provisioned agent and immutable wrapper | Direct SSH, stale/substituted agent, bypass config, alternate binary, same-port host, wrong agent, wrong host, and wrong port fail |
| GitHub CLI | Active-repository operation succeeds through proxy | Real token exposure, cross-repository API, and unapproved endpoints fail |
+3 -3
View File
@@ -20,9 +20,9 @@ Captured for Slice 0 on 27AUG2026 with nono 0.74.0. This record contains no cred
| `pi-orca-local` | Symlink to `profiles/nono/pi-orca-local.json` | Approved drift imported, narrowed to read-only trusted-file access, and relinked on 27AUG2026. |
| `pi-harness-tools` | Symlink to `profiles/nono/pi-harness-tools.json` | Approved drift imported, narrowed to read-only trusted-file access, and relinked on 27AUG2026. |
The imported `pi-orca-local` drift is limited to exact read-only access for `$HOME/.pi/agent/AGENTS.md`. Its saved grant for `$HOME/.pi/agent/extensions/pi-workflow/index.ts` was not imported because the user removed that extension and deferred its replacement to a future skill. The imported `pi-harness-tools` drift is limited to exact read-only access for `$HOME/.pi/agent/extensions/pi-todo-herdr/index.ts` and `$HOME/.config/herdr/herdr.sock`. Its inherited `pi-local` policy already contains the Herdr Unix-socket grant. The active files used read/write `allow_file` saved grants; the repository deliberately narrows them to `read_file` because normal sessions do not need to modify trusted instructions or extension code. No other active saved grant was imported.
The imported `pi-orca-local` drift is limited to exact read-only access for `$HOME/.pi/agent/AGENTS.md`. Its saved grant for `$HOME/.pi/agent/extensions/workflow/index.ts` was not imported because the user removed that extension and deferred its replacement to a future skill. The imported `pi-harness-tools` drift is limited to exact read-only access for `$HOME/.pi/agent/extensions/pi-todo-herdr/index.ts` and `$HOME/.config/herdr/herdr.sock`. Its inherited `pi-local` policy already contains the Herdr Unix-socket grant. The active files used read/write `allow_file` saved grants; the repository deliberately narrows them to `read_file` because normal sessions do not need to modify trusted instructions or extension code. No other active saved grant was imported.
The `pi-workflow` extension was unlinked from active Pi state by the user during this slice. It is also absent from normal `config/pi-links.json` exposure so reconciliation will not recreate it. The future replacement is tracked in `docs/todos.md`.
The `workflow` extension was unlinked from active Pi state by the user during this slice. It is also absent from normal `config/pi-links.json` exposure so reconciliation will not recreate it. The future replacement is tracked in `docs/todos.md`.
## Draft disposition
@@ -53,7 +53,7 @@ The `.nono-pi-pack-marker` is package wiring, not a profile draft, and remains i
Only `@pi-kaush/pi-welcome-screen@0.1.3` is currently locked end to end. Bare npm sources, `@latest`, and Git URLs without revisions remain blocked even though their installed contents are identified. `nolabs-ai/pi` remains blocked because the nono lockfile records `pinned: false`.
The extension inventory records the active repository-backed links and their observed commits. Mutable checkout targets and unmanaged links remain blocked until maintenance installs immutable snapshots. The unmanaged active `lm-studio.ts` file is recorded by SHA-256 and remains blocked pending the Slice 1 import and quarantine. `update` remains actively linked at capture time but has been removed from normal link exposure. The active `pi-workflow` link was removed by the user and is not part of the current active inventory.
The extension inventory records the active repository-backed links and their observed commits. Mutable checkout targets and unmanaged links remain blocked until maintenance installs immutable snapshots. The unmanaged active `lm-studio.ts` file is recorded by SHA-256 and remains blocked pending the Slice 1 import and quarantine. `update` remains actively linked at capture time but has been removed from normal link exposure. The active `workflow` link was removed by the user and is not part of the current active inventory.
## Maintenance-only follow-up
+59
View File
@@ -0,0 +1,59 @@
# Bun workspace packages and Gitea npm publishing
The repository uses Bun workspaces under `extensions/*`. External dependency versions live in the root `package.json` catalog; repository-owned dependencies use `workspace:^`.
Publishable packages include the repository-native extensions plus the imported `@_jb/pi-interactive-subagents`, `@_jb/pi-native-hooks`, and `@_jb/pi-todo-herdr` snapshots. `config/imported-packages.json` records their source `main` commits; use `bun run imports:check` to detect upstream movement.
## Install and verify
```bash
bun install --frozen-lockfile
bun run typecheck
bun run test
```
Use plain `bun install` when intentionally updating `bun.lock`.
## Registry configuration
The tracked `.npmrc` routes only the `@_jb` scope:
```ini
@_jb:registry=https://git.jb.codes/api/packages/jb/npm/
```
Configure a Gitea personal access token with package permissions outside the repository:
```ini
//git.jb.codes/api/packages/jb/npm/:_authToken=${GITEA_NPM_TOKEN}
```
The token line may live in `~/.npmrc`, or `GITEA_NPM_TOKEN` may be supplied by the publishing environment. Never commit a token.
## Pack and publish
Bun must resolve `catalog:` and `workspace:` references before publication. From a publishable workspace:
```bash
cd extensions/shared
bun pm pack --destination ../../dist
bun publish
```
Publish `@_jb/pi-shared` before a new version of any extension that depends on it. Gitea rejects republishing an existing name/version, so bump the changed package version first.
`extensions/workflow` is intentionally private until the `_workflows` reference dependencies are merged into that package. `extensions/_workflows` must not be published.
## Install with Pi
After configuring the same scope and token in the npm configuration visible to Pi:
```bash
pi install npm:@_jb/pi-dictate
pi install npm:@_jb/pi-file-search
pi install npm:@_jb/pi-interactive-subagents
pi install npm:@_jb/pi-native-hooks
pi install npm:@_jb/pi-todo-herdr
```
Pi installs runtime dependencies from the same scoped registry. The repository's symlink-based development exposure remains controlled separately by `config/pi-links.json`.
+1 -1
View File
@@ -1,3 +1,3 @@
# TODO
- [ ] Make pi-workflow verification tolerate inaccessible sibling Git worktrees. `GitStateProbe.captureRepository()` currently enumerates every path from `git worktree list --porcelain` and fails the verification command before execution when any unrelated worktree cannot be inspected. Preserve detection of cross-worktree mutations without making unrelated worktrees a hard verification dependency. Add focused coverage for an inaccessible registered sibling worktree.
- [ ] Make workflow verification tolerate inaccessible sibling Git worktrees. `GitStateProbe.captureRepository()` currently enumerates every path from `git worktree list --porcelain` and fails the verification command before execution when any unrelated worktree cannot be inspected. Preserve detection of cross-worktree mutations without making unrelated worktrees a hard verification dependency. Add focused coverage for an inaccessible registered sibling worktree.
+6 -2
View File
@@ -1,5 +1,9 @@
# Todos
## Merge `_workflows` reference runtime into `workflow`
`extensions/_workflows` was retained only as a reference while `extensions/workflow` was developed, but `workflow` still imports its runner, model, prompt, and serialization modules. Move the required code under `extensions/workflow`, remove obsolete model-authored workflow surfaces, update tests and documentation, and then delete the reference directory. Until that work is complete, `@_jb/pi-workflow` must remain a private Bun workspace and `extensions/_workflows` must remain excluded from workspaces and publication. Acceptance requires `bun pm pack` for `workflow` to contain every runtime import without reaching outside the package root, followed by an isolated Pi package smoke test.
## Make `pi-links` checks work from linked worktrees
A Slice 0 `node scripts/pi-links.mjs prune --dry-run` from `.worktrees/nono-dynamic-permissions-slice-zero-direct` exited 2 and classified every active link into the primary checkout as an outside-repository conflict. The tool anchors ownership to the current worktree root, while active links correctly target the primary repository checkout. Teach check and dry-run planning to recognize canonical checkouts sharing the same Git common directory without broadening stale-link deletion to unrelated repositories. Add linked-worktree fixtures before changing prune behavior. Until fixed, run host reconciliation only from the integrated primary checkout.
@@ -8,9 +12,9 @@ A Slice 0 `node scripts/pi-links.mjs prune --dry-run` from `.worktrees/nono-dyna
Slice 1 found two Nono 0.74 limitations while building behavioral policy proofs. First, command-policy startup fails with `path must be shorter than SUN_LEN` when Nono creates its supervisor socket beneath a moderately long isolated `TMPDIR` such as `/tmp/nono-slice-one-*/sessions/personal/tmp`. Before broker profiles activate command policies, reproduce the maximum safe path, then have the launcher select a private short socket root or confirm an upstream fix. Second, `nono profile show --json` omits resolved `environment`, `command_policies`, `network.connect_port`, and `network.custom_credentials` fields even though strict validation and live enforcement accept them. Do not treat resolved JSON as complete evidence for these capabilities; retain behavioral `nono run` and `nono why --command` proofs until the output is complete and regression-tested.
## Replace `pi-workflow` with a skill-based orchestrator
## Replace `workflow` with a skill-based orchestrator
The strict `pi-workflow` extension repeatedly failed before implementation because planner output and persisted state-machine constraints were too brittle for adaptive subagent orchestration. In a separate session, design a repository-managed skill where the main Pi agent owns the plan, task tree, isolated worktree, explicit user approval gates, verification, and independent review while launching standard `scout` and `worker` subagents directly. Preserve file-ownership boundaries, no inferred approvals, review independence, and resumable repository evidence, but prefer native Pi primitives over a custom orchestration engine. Evaluate a small committed state manifest for crash recovery before retiring or archiving the extension. Use the abandoned `.worktrees/pi-workflow-plan-seed` implementation only as reference; do not integrate it by default.
The strict `workflow` extension repeatedly failed before implementation because planner output and persisted state-machine constraints were too brittle for adaptive subagent orchestration. In a separate session, design a repository-managed skill where the main Pi agent owns the plan, task tree, isolated worktree, explicit user approval gates, verification, and independent review while launching standard `scout` and `worker` subagents directly. Preserve file-ownership boundaries, no inferred approvals, review independence, and resumable repository evidence, but prefer native Pi primitives over a custom orchestration engine. Evaluate a small committed state manifest for crash recovery before retiring or archiving the extension. Use the abandoned `.worktrees/workflow-plan-seed` implementation only as reference; do not integrate it by default.
## Support subagent launches through `multi_tool_use.parallel`
@@ -1,6 +1,6 @@
# pi-workflow
# workflow
`pi-workflow` plans and implements feature work through persisted human Gates, isolated Git worktrees, focused verification, and independent review.
`workflow` plans and implements feature work through persisted human Gates, isolated Git worktrees, focused verification, and independent review.
## Start a feature
@@ -10,9 +10,9 @@ Start with:
/workflow start <message>
```
The command sends the message through the assistant, which chooses the descriptive feature name and idempotency key before invoking `pi_workflow` through the normal tool path. In interactive TUI mode, `/workflow start` without a message asks what you want to start working on. Non-interactive modes require the message in the command.
The command sends the message through the assistant, which chooses the descriptive feature name and idempotency key before invoking `workflow` through the normal tool path. In interactive TUI mode, `/workflow start` without a message asks what you want to start working on. Non-interactive modes require the message in the command.
You can also ask the assistant to use `pi_workflow` with:
You can also ask the assistant to use `workflow` with:
- `action: "start"`
- `workflow: { mode: "feature" }` (or omit `workflow`)
@@ -37,9 +37,9 @@ The assistant must inspect the current run before each state-changing call and o
## Agent selection
`pi-workflow` reuses explicitly mapped standard definitions from `pi-interactive-subagents` before falling back to its bundled workflow profiles. By default, `scout` owns Discovery and `worker` owns Implementation; bundled profiles continue to supply Design, Prototype, Verify, and independent Review. Definitions resolve with package, global, then trusted-project precedence and are pinned into the run with their workflow role overlay.
`workflow` reuses explicitly mapped standard definitions from `pi-interactive-subagents` before falling back to its bundled workflow profiles. By default, `scout` owns Discovery and `worker` owns Implementation; bundled profiles continue to supply Design, Prototype, Verify, and independent Review. Definitions resolve with package, global, then trusted-project precedence and are pinned into the run with their workflow role overlay.
Configure or disable mappings in `~/.pi/agent/pi-workflow.json` or trusted `.pi/pi-workflow.json`:
Configure or disable mappings in `~/.pi/agent/workflow.json` or trusted `.pi/workflow.json`:
```json
{
@@ -99,11 +99,11 @@ Interrupted work opens a Recovery Gate with retry, inspect, and abort choices. C
Run state and bounded artifacts are stored under:
```text
~/.pi/agent/pi-workflows/runs/<run-id>/
~/.pi/agent/workflows/runs/<run-id>/
```
Important files include immutable plan revisions, Assignment transcripts, retained prototype reports and screenshots, `result.json`, and the terminal Markdown report. Report headings use the readable label and include the full canonical Run ID as explicit metadata. Completed worktrees remain available for manual inspection and integration.
## Reload after exposure
Repository-managed extension links are controlled through `config/pi-links.json`. Run `/reload` after the managed link is synchronized so Pi discovers `pi-workflow`.
Repository-managed extension links are controlled through `config/pi-links.json`. Run `/reload` after the managed link is synchronized so Pi discovers `workflow`.
@@ -1,10 +1,10 @@
# pi-workflow popup prototypes
# workflow popup prototypes
**Status:** Direction C accepted
These static terminal frames compare popup directions for `/workflow`. They are prototype evidence, not production screenshots or manual validation of the implemented extension. All variants use Pi theme tokens, keyboard-visible focus, one border at most, and no animation.
Representative run: `pi-workflow-usability-resume`, waiting at Plan Approval after Discover and Design.
Representative run: `workflow-usability-resume`, waiting at Plan Approval after Discover and Design.
## A. Centered compact modal
@@ -13,8 +13,8 @@ A small modal preserves the surrounding conversation and keeps one decision in f
```text
┌──────────────────────────────────────────────────────────────────────┐
│ │
│ ╭─ pi-workflow ──────────────────────────────╮ │
│ │ pi-workflow-usability-resume │ │
│ ╭─ workflow ──────────────────────────────╮ │
│ │ workflow-usability-resume │ │
│ │ WAITING · Plan Approval · revision 12 │ │
│ │ │ │
│ │ ✓ Discover ✓ Design · Prototype │ │
@@ -46,7 +46,7 @@ Weaknesses:
### A at 60 columns
```text
╭─ pi-workflow ───────────────────────╮
╭─ workflow ───────────────────────╮
│ usability-resume · WAITING │
│ Plan Approval · r12 │
│ │
@@ -64,9 +64,9 @@ Weaknesses:
A stable workspace keeps the run list, selected run, progress, and actions visible together.
```text
╭─ pi-workflow ────────────────────────────────────────────────────────────────╮
│ Runs │ pi-workflow-usability-resume WAITING │
│ │ feature/pi-workflow-usability-resume · r12 │
╭─ workflow ────────────────────────────────────────────────────────────────╮
│ Runs │ workflow-usability-resume WAITING │
│ │ feature/workflow-usability-resume · r12 │
│ > usability-resume │ │
│ auth-cleanup │ Progress │
│ cache-audit │ ✓ Discover ✓ Design · Prototype · Implement │
@@ -101,7 +101,7 @@ Weaknesses:
### B at 80x24
```text
╭─ pi-workflow ────────────────────────────────────────────────────────────────╮
╭─ workflow ────────────────────────────────────────────────────────────────╮
│ Runs │ usability-resume · WAITING · r12 │
│ > usability │ ✓ Discover ✓ Design · Prototype │
│ auth-cleanup │ · Implement · Verify · Review │
@@ -127,7 +127,7 @@ Direction C uses shallow browser-style navigation. Each overlay does one job. Es
### C1. Run list popup
```text
╭─ pi-workflow ─────────────────────────────╮
╭─ workflow ─────────────────────────────╮
│ > usability-resume WAITING Plan Approval│
│ auth-cleanup RUNNING Implement │
│ cache-audit DONE report ready │
@@ -160,11 +160,11 @@ Direction C uses shallow browser-style navigation. Each overlay does one job. Es
The reader uses the whole overlay. The terminal edge is the outer frame, so there is no decorative full-screen border. The left outline tracks the heading containing the top visible document line.
```text
pi-workflow plan · usability-resume line 42/216
workflow plan · usability-resume line 42/216
Outline Plan
───────────────────────────── ─────────────────────────────────────────────
pi-workflow usability update # pi-workflow usability update plan
workflow usability update # workflow usability update plan
Locked decisions
> Implementation phases ## Implementation phases
Prototype overlays
@@ -186,7 +186,7 @@ The reader uses the whole overlay. The terminal edge is the outer frame, so ther
### C3 at 80x24
```text
pi-workflow plan · usability-resume row 42/216
workflow plan · usability-resume row 42/216
Outline Plan
────────────────────── ─────────────────────────────────────────────
@@ -207,7 +207,7 @@ The reader uses the whole overlay. The terminal edge is the outer frame, so ther
The outline hides and the document becomes the only pane. The current heading remains visible in the header.
```text
pi-workflow plan · Implementation phases · row 42/216
workflow plan · Implementation phases · row 42/216
## Implementation phases
@@ -228,7 +228,7 @@ The outline hides and the document becomes the only pane. The current heading re
terminal too small
pi-workflow plan needs 48x12
workflow plan needs 48x12
current terminal is 47x11
resize the terminal or press esc
@@ -271,7 +271,7 @@ Use direction C. It has one border only on bounded popup cards, no redundant fra
## Production mapping
The accepted direction maps to `extensions/pi-workflow/tui.ts` and `dashboard.ts`. The implementation uses popup overlays for bounded selections and text entry, then switches to the full-screen reader for verified plans and retained evidence. `progress.ts` owns the separate one-line widget. Pure renderer tests cover the wide, 80-column, 60-column, and below-48x12 contracts; interactive Pi validation remains a separate manual step.
The accepted direction maps to `extensions/workflow/tui.ts` and `dashboard.ts`. The implementation uses popup overlays for bounded selections and text entry, then switches to the full-screen reader for verified plans and retained evidence. `progress.ts` owns the separate one-line widget. Pure renderer tests cover the wide, 80-column, 60-column, and below-48x12 contracts; interactive Pi validation remains a separate manual step.
## Interaction contract
@@ -1,13 +1,13 @@
# pi-workflow Design
# workflow Design
**Status:** Accepted
**Date:** 24AUG2026
## Purpose
`pi-workflow` coordinates feature planning and implementation through selected Markdown-defined agents, persisted human gates, isolated Pi child sessions, and mandatory independent review.
`workflow` coordinates feature planning and implementation through selected Markdown-defined agents, persisted human gates, isolated Pi child sessions, and mandatory independent review.
The extension is based on `extensions/_workflows`, but it is a new extension rather than a rename. The existing extension remains an inactive reference until `pi-workflow` is verified and explicitly exposed.
The extension is based on `extensions/_workflows`, but it is a new extension rather than a rename. The existing extension remains an inactive reference until `workflow` is verified and explicitly exposed.
## Goals
@@ -58,7 +58,7 @@ The extension is based on `extensions/_workflows`, but it is a new extension rat
The extension exposes one state-changing tool and one workflow command.
```ts
type PiWorkflowInput =
type WorkflowInput =
| {
action: "start";
request: string;
@@ -152,13 +152,13 @@ Feature and feature-script starts require `featureName` as a concise lowercase k
Human-facing dashboard, selector, notification, handoff, compaction, tool, and report prose uses a shared presentation label. It prefers `featureName`; otherwise it normalizes the request to one line and bounds it to 48 characters, preserving whole words where possible and adding `…` when truncated; only a missing request falls back to `runId`. When derived labels collide within an available run list, only those labels receive ` · <prefix>`, where the run-ID prefix is the shortest unique prefix with a minimum of eight characters. This label is never persisted as identity or accepted as a lookup alias. It requires no schema migration and does not change persisted paths. The complete `runId` remains canonical for snapshots, structured fields, command arguments, paths, receipts, leases, and idempotency. Copyable `/workflow continue <run-id>` and `/workflow stop <run-id>` commands retain it, and reports include it as `Run ID` metadata beneath the readable heading.
The `pi_workflow` tool calls a single internal `execute(command)` interface. `/workflow` opens a drill-down TUI dashboard and may accept a run ID for direct inspection. Run selection, actions, Gates, confirmations, questions, and feedback use bounded popup overlays. Verified plans, reports, prototype evidence, and Assignment transcripts use a sanitized full-screen reader with H1-H3 outline navigation at widths of 80 columns or more and a single document pane from 60 through 79 columns. Below 48x12 it renders only a truthful minimum-size state. RPC, print, and JSON paths remain linear and do not call `ctx.ui.custom()`.
The `workflow` tool calls a single internal `execute(command)` interface. `/workflow` opens a drill-down TUI dashboard and may accept a run ID for direct inspection. Run selection, actions, Gates, confirmations, questions, and feedback use bounded popup overlays. Verified plans, reports, prototype evidence, and Assignment transcripts use a sanitized full-screen reader with H1-H3 outline navigation at widths of 80 columns or more and a single document pane from 60 through 79 columns. Below 48x12 it renders only a truthful minimum-size state. RPC, print, and JSON paths remain linear and do not call `ctx.ui.custom()`.
`/workflow start <message>` sends the initial request through the assistant so the normal `pi_workflow` tool path owns feature naming and idempotency; in interactive TUI mode, omitting the message opens an input overlay. `/workflow continue` presents eligible pending context transitions for selection, while `/workflow continue <run-id>` continues one directly; `/workflow resume` is an alias for both forms. Selecting a fresh-session continuation from the dashboard routes directly through the same handler and calls `newSession()` without a second command. `/workflow stop [run-id]` aborts a non-terminal run owned by the active parent session and presents a selector when multiple owned runs are active. The misspelled `/worlflow` alias is intentionally unsupported. Continue and stop selectors require interactive TUI mode and provide direct-run-ID guidance otherwise.
`/workflow start <message>` sends the initial request through the assistant so the normal `workflow` tool path owns feature naming and idempotency; in interactive TUI mode, omitting the message opens an input overlay. `/workflow continue` presents eligible pending context transitions for selection, while `/workflow continue <run-id>` continues one directly; `/workflow resume` is an alias for both forms. Selecting a fresh-session continuation from the dashboard routes directly through the same handler and calls `newSession()` without a second command. `/workflow stop [run-id]` aborts a non-terminal run owned by the active parent session and presents a selector when multiple owned runs are active. The misspelled `/worlflow` alias is intentionally unsupported. Continue and stop selectors require interactive TUI mode and provide direct-run-ID guidance otherwise.
Continuation performs only a pending fresh-session handoff or normal-compaction fallback after the run is safely waiting; it cannot approve plans, select prototypes, or make other Decisions. Stop treats `running` and `waiting` runs as active and excludes terminal runs and runs owned only by historical parent sessions.
When a run reaches a Gate, the tool returns a structured waiting view. The parent agent asks the user through its normal interaction tools, then calls `pi_workflow` with `action: "decide"`. Child agents never receive `ask_user`.
When a run reaches a Gate, the tool returns a structured waiting view. The parent agent asks the user through its normal interaction tools, then calls `workflow` with `action: "decide"`. Child agents never receive `ask_user`.
Background runs return a structured run ID immediately. When they reach a Gate or settle, the extension sends one custom follow-up message with a stable delivery key to the active parent session and a readable run label in its prose.
@@ -170,7 +170,7 @@ After `start` creates the run lease, every subsequent state-changing parent oper
Profiles are loaded in this order:
1. Bundled workflow profiles from `extensions/pi-workflow/agents/*.md`.
1. Bundled workflow profiles from `extensions/workflow/agents/*.md`.
2. Configured standard definitions from `${PI_CODING_AGENT_DIR:-~/.pi/agent}/extensions/pi-interactive-subagents/agents/`.
3. Configured standard definitions from `${PI_CODING_AGENT_DIR:-~/.pi/agent}/agents/`.
4. Workflow profiles or configured standard definitions from the nearest `.pi/agents/*.md`, only when `ctx.isProjectTrusted()` is true.
@@ -252,8 +252,8 @@ The engine selects the initial discovery profile deterministically from eligible
Configuration sources are:
1. Bundled defaults.
2. `~/.pi/agent/pi-workflow.json`.
3. Trusted `.pi/pi-workflow.json`.
2. `~/.pi/agent/workflow.json`.
3. Trusted `.pi/workflow.json`.
4. Per-run invocation values.
Invocation values may change requested concurrency but never the safety ceiling. A project may lower the global ceiling but cannot raise it.
@@ -484,7 +484,7 @@ return results;
Every feature Workflow Run owns a distinct, exclusive worktree under:
```text
<repository>/.worktrees/pi-workflow-<feature-name>-<run-id>/
<repository>/.worktrees/workflow-<feature-name>-<run-id>/
```
The corresponding branch is:
@@ -506,7 +506,7 @@ Read-only analysis runs use the parent cwd and do not create worktrees. All chil
Artifacts live under:
```text
~/.pi/agent/pi-workflows/runs/<run-id>/
~/.pi/agent/workflows/runs/<run-id>/
```
Files include:
@@ -554,11 +554,11 @@ The effective model is resolved in this order:
Unknown or ambiguous models fail the Assignment before execution. The effective provider, model, effort, profile digest, child session ID, and context capacity are persisted.
Children cannot access recursive workflow or subagent tools or `ask_user`. Standard definitions are execution identities, not permission grants: `worker` may declare `bash` and nested subagents, but mutating workflow Assignments still receive only audited reads plus guarded `edit`/`write`, and read-only Assignments receive only audited reads. `pi_workflow` is added to both the shared child-tool exclusion policy and the separate Pi-subagent child exclusion list alongside the legacy `workflow` name; implementation should consolidate the duplicate list onto the shared policy when possible. Each child tool call retains the shared timeout guard, including tools registered after startup. The first-response watchdog and bounded shutdown behavior from `_workflows` are retained.
Children cannot access recursive workflow or subagent tools or `ask_user`. Standard definitions are execution identities, not permission grants: `worker` may declare `bash` and nested subagents, but mutating workflow Assignments still receive only audited reads plus guarded `edit`/`write`, and read-only Assignments receive only audited reads. `workflow` is added to the shared child-tool exclusion policy used by Pi-subagent children so recursive orchestration remains unavailable. Each child tool call retains the shared timeout guard, including tools registered after startup. The first-response watchdog and bounded shutdown behavior from `_workflows` are retained.
## Internal Modules
`pi-workflow` should remain a small set of deep modules:
`workflow` should remain a small set of deep modules:
- `index.ts`: Pi tool, command, lifecycle, and composition root.
- `engine.ts`: command handling, state transitions, gates, policies, and scheduling.
@@ -578,9 +578,9 @@ Pure transition and eligibility logic stay inside the engine and profile modules
## Exposure and Migration
- Do not rename or activate `_workflows` during initial development.
- Add `extensions/pi-workflow` tests to `package.json` explicitly.
- Add `extensions/workflow` tests to `package.json` explicitly.
- Typecheck through the existing `extensions/**/*.ts` configuration.
- Add `pi-workflow` to `config/pi-links.json` only after focused tests, full tests, independent review, and manual TUI validation pass.
- Add `workflow` to `config/pi-links.json` only after focused tests, full tests, independent review, and manual TUI validation pass.
- Run the link reconciler in check mode before sync.
- Preserve unrelated managed links and require `/reload` after exposure.
@@ -627,7 +627,7 @@ Implement the production Pi Assignment executor and eligible planner-selection p
- Planner choices are restricted to eligible profiles and persist selected, rejected, and rationale data.
- Aggregate running Assignments across all Workflow Runs never exceed 32 or a lower configured ceiling.
- Capacity is reserved synchronously; queued cancellation and bounded session shutdown release capacity exactly once.
- `pi_workflow`, legacy workflow/subagent tools, and user interaction remain unavailable through both shared workflow-child and Pi-subagent child policies.
- `workflow`, legacy workflow/subagent tools, and user interaction remain unavailable through both shared workflow-child and Pi-subagent child policies.
- A tool registered after child startup is still guarded by `extensions/shared/tool-call-timeout.ts`.
### Phase 4: Worktrees, implementation, verification, and review
@@ -640,7 +640,7 @@ Extend the Phase 2 worktree scaffold with the Implementation Context Gate, `/wor
- Workspace creation intent is persisted first; creation failure rolls back safely or remains reconcilable after restart.
- Every feature run receives a distinct leased worktree and worktrees are retained until explicitly removed.
- A versioned `plan.md` is persisted before the Implementation Context Gate and revised with an accepted prototype when applicable.
- Phase 4 registers `/workflow start`, `/workflow stop`, `/workflow continue`, and its `/workflow resume` alias. Start routes through the assistant's normal `pi_workflow` tool path, stop is scoped to active runs owned by the current parent session, and continuation without a run ID selects from valid candidates. Its `ExtensionCommandContext` performs both fresh-session replacement and approved normal-compaction fallback rather than deferring either path to dashboard integration.
- Phase 4 registers `/workflow start`, `/workflow stop`, `/workflow continue`, and its `/workflow resume` alias. Start routes through the assistant's normal `workflow` tool path, stop is scoped to active runs owned by the current parent session, and continuation without a run ID selects from valid candidates. Its `ExtensionCommandContext` performs both fresh-session replacement and approved normal-compaction fallback rather than deferring either path to dashboard integration.
- `fresh-session` persists a Gate-, revision-, plan-, preparation-, and source-session-bound handoff intent before `newSession`; only the replacement `withSession` callback consumes it, attaches the new active parent session, seeds the bounded plan handoff, and resumes.
- `smart-compact` uses a Gate-bound `report-context-preparation` transition with distinct `unavailable`, `succeeded`, `no-op`, and `failed` outcomes; `failed` requires a non-empty actionable error. The host validates `unavailable` against `pi.getActiveTools()`; only that outcome permits plan-focused `ctx.compact({ onComplete, onError })`, whose intent-bound callback persists success or failure.
- An available-but-failed `smart_compact` call does not silently fall back and keeps the Implementation Context Gate waiting with an actionable error; `current-session` preserves context unchanged.
@@ -1,4 +1,4 @@
# pi-workflow usability update plan
# workflow usability update plan
Status: manual recovery plan after managed Workflow Runs `512d6c31`, `51b0c1cb`, and `ecb11e1e` failed or were interrupted before implementation.
@@ -26,7 +26,7 @@ These failures are acceptance-test inputs for assignment timeout handling and de
### 1. Prototype overlays
Own only `docs/pi-workflow/dashboard-mocks.md` and isolated prototype artifacts. Compare:
Own only `docs/workflow/dashboard-mocks.md` and isolated prototype artifacts. Compare:
- A: centered compact modal
- B: near-full-screen workspace with list/detail panes
+1 -1
View File
@@ -58,7 +58,7 @@ test("artifact transcript keeps the initial prompt, marker, and newest entries",
});
test("live artifact persistence includes current agents and transcripts", () => {
const directory = mkdtempSync(join(tmpdir(), "pi-workflow-artifacts-"));
const directory = mkdtempSync(join(tmpdir(), "workflow-artifacts-"));
try {
const details = workflowDetails();
details.agents.push({
+1 -1
View File
@@ -226,7 +226,7 @@ function run(source, argsJson) {
};
const context = vm.createContext(sandbox, {
name: "pi-workflow",
name: "workflow",
codeGeneration: { strings: false, wasm: false },
});
new vm.Script(BOOTSTRAP, {
+1 -1
View File
@@ -27,7 +27,7 @@ test("safeStringify handles cycles, bigint, depth, and size", () => {
});
test("atomic writes leave complete readable content", () => {
const directory = mkdtempSync(join(tmpdir(), "pi-workflow-test-"));
const directory = mkdtempSync(join(tmpdir(), "workflow-test-"));
try {
const file = join(directory, "artifact.json");
writeFileAtomic(file, '{"value":1}');
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 JB
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+7
View File
@@ -0,0 +1,7 @@
# @_jb/pi-copy-all
Pi extension that adds `/copy-all` for copying the current conversation as Markdown.
```bash
pi install npm:@_jb/pi-copy-all
```
+22
View File
@@ -0,0 +1,22 @@
{
"name": "@_jb/pi-copy-all",
"version": "0.1.0",
"description": "Pi command for copying the current conversation as Markdown.",
"type": "module",
"license": "MIT",
"engines": { "node": ">=22.19.0" },
"keywords": ["pi-package", "pi-extension"],
"files": ["index.ts", "README.md", "LICENSE"],
"pi": { "extensions": ["./index.ts"] },
"peerDependencies": {
"@earendil-works/pi-coding-agent": "catalog:pi-peer"
},
"publishConfig": {
"registry": "https://git.jb.codes/api/packages/jb/npm/"
},
"repository": {
"type": "git",
"url": "git+ssh://git@git.jb.codes:7722/jb/harness-tools.git",
"directory": "extensions/copy-all"
}
}
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 JB
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+22
View File
@@ -0,0 +1,22 @@
{
"name": "@_jb/pi-dictate",
"version": "0.1.0",
"description": "Pi extension for local Linux voice dictation through a Unix socket service.",
"type": "module",
"license": "MIT",
"engines": { "node": ">=22.19.0" },
"keywords": ["pi-package", "pi-extension", "dictation"],
"files": ["index.ts", "README.md", "LICENSE"],
"pi": { "extensions": ["./index.ts"] },
"peerDependencies": {
"@earendil-works/pi-coding-agent": "catalog:pi-peer"
},
"publishConfig": {
"registry": "https://git.jb.codes/api/packages/jb/npm/"
},
"repository": {
"type": "git",
"url": "git+ssh://git@git.jb.codes:7722/jb/harness-tools.git",
"directory": "extensions/dictate"
}
}
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 JB
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+7
View File
@@ -0,0 +1,7 @@
# @_jb/pi-effort-controls
Pi extension that adds `/effort` reasoning selection and `/fast` priority-mode controls.
```bash
pi install npm:@_jb/pi-effort-controls
```
+22
View File
@@ -0,0 +1,22 @@
{
"name": "@_jb/pi-effort-controls",
"version": "0.1.0",
"description": "Pi commands for reasoning effort and priority-mode controls.",
"type": "module",
"license": "MIT",
"engines": { "node": ">=22.19.0" },
"keywords": ["pi-package", "pi-extension"],
"files": ["index.ts", "README.md", "LICENSE"],
"pi": { "extensions": ["./index.ts"] },
"peerDependencies": {
"@earendil-works/pi-coding-agent": "catalog:pi-peer"
},
"publishConfig": {
"registry": "https://git.jb.codes/api/packages/jb/npm/"
},
"repository": {
"type": "git",
"url": "git+ssh://git@git.jb.codes:7722/jb/harness-tools.git",
"directory": "extensions/effort-controls"
}
}
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 JB
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+7
View File
@@ -0,0 +1,7 @@
# @_jb/pi-file-search
Pi extension providing structured `fd` file search and `rg` text search tools.
```bash
pi install npm:@_jb/pi-file-search
```
+30
View File
@@ -0,0 +1,30 @@
{
"name": "@_jb/pi-file-search",
"version": "0.1.0",
"description": "Pi file and text search tools backed by fd and ripgrep.",
"type": "module",
"license": "MIT",
"engines": { "node": ">=22.19.0" },
"keywords": ["pi-package", "pi-extension", "search"],
"files": ["index.ts", "src", "README.md", "LICENSE"],
"pi": { "extensions": ["./index.ts"] },
"dependencies": {
"@effect/platform-node": "catalog:",
"@effect/platform-node-shared": "catalog:",
"effect": "catalog:"
},
"peerDependencies": {
"@earendil-works/pi-ai": "catalog:pi-peer",
"@earendil-works/pi-coding-agent": "catalog:pi-peer",
"@earendil-works/pi-tui": "catalog:pi-peer",
"typebox": "catalog:pi-peer"
},
"publishConfig": {
"registry": "https://git.jb.codes/api/packages/jb/npm/"
},
"repository": {
"type": "git",
"url": "git+ssh://git@git.jb.codes:7722/jb/harness-tools.git",
"directory": "extensions/file-search"
}
}
@@ -0,0 +1,6 @@
node_modules/
config.json
.DS_Store
.memory/
.pi/
.worktrees/
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 HazAT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+227
View File
@@ -0,0 +1,227 @@
## Forked from [amosblomqvist/pi-interactive-subagents](https://github.com/amosblomqvist/pi-interactive-subagents)
# pi-interactive-subagents
Async subagents for [pi](https://github.com/badlogic/pi-mono), running in Herdr tabs, Orca terminal tabs, or tmux panes. Spawn a sub-agent, keep working in the main session, and get the result steered back when it finishes. Fully non-blocking.
The backend is selected automatically: Herdr is used when Pi runs in a Herdr-managed pane (`HERDR_ENV=1`), then Orca when Pi runs in an Orca-managed terminal (`ORCA_TERMINAL_HANDLE`), otherwise tmux is used when available.
## How it works
`subagent()` returns immediately. In Herdr, the sub-agent runs in a new unfocused tab in the current workspace. In Orca, it runs in a new unfocused terminal tab in the current worktree, started in the parent Pi's working directory. In tmux, it runs in a right split off the parent Pi pane. A live widget above the input tracks every running sub-agent, and when one finishes, its result is steered into the main session as a notification that triggers a new turn.
```
Subagents 2 running
⟳ 00:23 scout active · bash 7m
○ 00:45 scout-2 waiting 2m
```
Spawn several in parallel — they run concurrently and steer results back independently as each finishes.
Running subagents survive a parent `/reload`. Their terminal sessions and completion watchers stay active, then rebind result delivery and the status widget to the fresh Pi extension runtime. Replacing or closing the parent session still cancels its running subagents. This lifecycle behavior requires Pi 0.68 or newer.
tmux panes are kept evenly sized: the extension re-applies an `even-horizontal` layout after every spawn and exit (debounced). The layout is a single constant, `SUBAGENT_TMUX_LAYOUT` in `pi-extension/subagents/tmux.ts` — change it to any named tmux layout (`main-vertical`, `tiled`, …). Herdr and Orca subagents use separate tabs, so they do not resize the parent tab.
If your shell startup is slow and launch commands get dropped before the prompt is ready, raise the delay:
```bash
export PI_SUBAGENT_SHELL_READY_DELAY_MS=2500 # defaults: Herdr 1000, Orca 500, tmux 500
```
## Tools
| Tool | Description |
| --- | --- |
| `subagent` | Spawn a sub-agent in a dedicated terminal surface (async) |
| `subagent_message` | Message a sub-agent by name — steers it if running, resumes its session if finished |
| `subagents_list` | List available agent definitions |
| `ask_question` | *(sub-agent sessions only)* Ask the orchestrator a question and wait for the reply |
There is also a `/subagent <agent> <task>` command for spawning directly.
### Spawning
```typescript
subagent({ agent: "scout", task: "Analyze the auth module" });
subagent({ agent: "worker", name: "dark-mode", task: "Implement the dark mode toggle" });
```
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| `agent` | string | required | Which agent to spawn (must be known and permitted) |
| `task` | string | required | Task prompt |
| `name` | string | agent name | Display name for the pane and widget. Must be unique — duplicates are auto-suffixed (`scout`, `scout-2`, …) |
| `model` | string | agent's model | Override the model for this spawn |
| `cwd` | string | agent's `cwd` | Working directory (see [Role folders](#role-folders)) |
### Messaging
`subagent_message` is addressed **by name only**. Names are unique per session and persist after a sub-agent finishes, so the same name works either way:
```typescript
subagent_message({ name: "scout", message: "Also check the auth middleware" });
```
- **Running** — the message is typed into the live pane (newlines flattened) and picked up at the next turn boundary. The call returns immediately; the eventual completion still arrives as a steer message.
- **Finished** — the session is resumed with the message as the follow-up task, like a fresh spawn: fire-and-forget, always autonomous, result steered back later. The resumed run reclaims its original name.
Every spawn records name → session file in `artifacts/<sessionId>/subagent-registry.json`, so names stay addressable across pi restarts. A nested sub-agent that spawns children gets its own registry keyed by its own session id. Resume is refused with a clear error (listing known names) if the name isn't registered, the session file is gone, or the session predates sandboxed resume.
**Resume replays the original sandbox.** At spawn time the fully-resolved loadout — tool allowlist, backing extensions, model, thinking level, system prompt, spawn whitelist, cwd — is snapshotted to `<session>.loadout.json`. Resume rebuilds the exact same restricted process from that snapshot rather than relaunching unrestricted.
### ask_question
A sub-agent can ask its orchestrator a single freeform question when requirements are ambiguous or a decision materially affects the work. The session **stays open** (parked as `waiting`) instead of exiting; the parent is notified with the sub-agent's name, replies via `subagent_message({ name, message })`, and the reply arrives as the sub-agent's next turn. Parallel questions are supported — each waiting sub-agent has its own name.
If the reply arrives while the sub-agent is still mid-turn, it is absorbed into the current turn — either way the question is marked answered and the session exits normally when the work is done. If the parent never replies, the pane stays open until a human closes it. Only available inside sub-agent sessions.
## Bundled agents
| Agent | Model | Tools | Role |
| ----- | ----- | ----- | ---- |
| **scout** | `openai-codex/gpt-5.6-luna` (`xhigh`) | `read`, `grep`, `find`, `ls` | Fast read-only codebase recon |
| **researcher** | `openai-codex/gpt-5.6-luna` (`xhigh`) | `web_search`, `web_fetch`, `safe_bash` | Web research, synthesized into a sourced brief |
| **worker** | `openai-codex/gpt-5.6-luna` (`xhigh`) | `read`, `write`, `edit`, `bash`, `web_search`, `web_fetch` + spawning | General implementer; may spawn `scout` and `researcher` |
All three are autonomous (`auto-exit: true`) and carry their identity in the system prompt (`system-prompt: append`).
## Custom agents
Place a `.md` file in `.pi/agents/` (project) or `~/.pi/agent/agents/` (global). Discovery priority: **project > global > package-bundled** — a project-local file overrides a bundled agent with the same name.
```markdown
---
name: my-agent
description: Does something specific
model: openai-codex/gpt-5.6-luna
thinking: xhigh
tools: read, edit, write, safe_bash, web_search
session-mode: lineage-only
auto-exit: true
---
You are a specialized agent that does X...
```
### Frontmatter reference
| Field | Type | Description |
| ----- | ---- | ----------- |
| `name` | string | Agent name (used in `agent: "my-agent"`) |
| `description` | string | Shown in `subagents_list` |
| `model` | string | Default model |
| `thinking` | string | `minimal`, `low`, `medium`, or `high` |
| `tools` | string | Strict tool allowlist. Built-ins: `read`, `write`, `edit`, `bash`, `grep`, `find`, `ls`. Extension-backed: `web_search`, `web_fetch`, `safe_bash`, `video_extract`, `youtube_search`, `google_image_search`. Only the extensions backing the listed tools are loaded into the child |
| `subagent_agents` | string | Comma-separated agent names this agent may spawn. **Presence of this field grants the spawning toolset** (`subagent`, `subagent_message`, `subagents_list`) and restricts spawn targets to the list. Omit it and the agent cannot spawn at all |
| `skills` | string | Comma-separated skill names to auto-load |
| `session-mode` | string | `standalone` (default), `lineage-only`, or `fork` — see below |
| `system-prompt` | string | `append` or `replace`: pass the body as the child's `--append-system-prompt` / `--system-prompt`. Omit and the body is prepended to the task prompt instead |
| `auto-exit` | boolean | Auto-shutdown when the agent finishes (see below) |
| `interactive` | boolean | Whether stall/recovery transitions wake the parent (see below) |
| `cwd` | string | Default working directory |
| `disable-model-invocation` | boolean | Hide from `subagents_list`; still spawnable by explicit name |
| `cli` | string | `claude` runs the agent via the Claude Code CLI instead of pi |
### session-mode
- `standalone` — fresh session, no lineage link to the caller (default)
- `lineage-only` — fresh session with `parentSession` linkage for discovery/fork UX, but no copied turns
- `fork` — child session seeded with the caller's conversation context
### auto-exit
With `auto-exit: true`, the session shuts down when the agent's turn ends — the agent just writes its final message and stops (there is no "done" tool). The last assistant message becomes the summary returned to the parent. Recommended for all autonomous agents.
Notes:
- **Manual input does not strand an auto-exit sub-agent.** If a human types into the pane, the session still closes once that turn completes normally — only an escape/abort leaves it open.
- **Auto-exit is suppressed while work is in flight:** the session parks as `waiting` instead of exiting when an `ask_question` is still unanswered, or when the agent's own child sub-agents are still running (a worker can stop after dispatching children and stays open until the last result returns).
### interactive
Controls whether `stalled`/`recovered` status transitions send a steer message to the parent session. Defaults to the inverse of `auto-exit`: autonomous agents get stall pings; user-driven agents stay quiet (the user is already working in that pane — the widget still updates). Set explicitly to override.
## Tool access control
Access is **whitelist-only**. Every sub-agent process is launched with `--no-extensions` (extension discovery disabled) and `--tools <allowlist>`; only the extensions backing the listed tools are loaded back in explicitly. There is no default toolset and no deny-list — an agent gets exactly what its frontmatter lists. The restriction survives resume via the loadout snapshot.
Spawns must name a known agent at **every** depth. A top-level session may spawn anything discoverable; a sub-agent may only spawn the agents in its `subagent_agents` list (enforced via `PI_SUBAGENT_ALLOWED`). There is no agentless spawn route, so a child can never escalate to a full-toolset profile by omitting its agent.
Extensions can register additional tools for sub-agents at runtime via `registerToolExtension(name, path)` on the `__pi_interactive_subagents` process global. Hosts that compose widgets can optionally use its reload-safe `statusWidget` surface:
```typescript
const { statusWidget } = globalThis.__pi_interactive_subagents;
const onChange = () => rerender();
const release = statusWidget.claim(onChange);
statusWidget.hasContent(); // whether any subagent rows exist
statusWidget.getBodyRowCount(); // uncapped number of running agents
statusWidget.render(width, maxBodyRows, ctx.ui.theme); // compact unboxed lines; cap includes an overflow row
statusWidget.isClaimed(onChange); // true only for the current provider claim
release(); // restore the standalone subagent-status widget
```
`render(width, maxBodyRows?, theme?)` accepts Pi's current theme as an optional structural value. The standalone widget receives and uses its own Pi theme automatically. When no theme is passed, a legacy ANSI fallback keeps status colors usable. The header is a compact unboxed `Subagents <count> running` row; body rows remain width-safe, and `maxBodyRows` counts body rows including the overflow summary, not the header. Semantic row colors match Pi's Todo conventions: accent header, muted count, warning active/running, error stalled, dim starting/provider tags/elapsed time, and accent waiting.
`claim()` replaces any prior claim, and an old release function cannot release a newer claim. Standalone-widget clearing/restoration is deferred to a microtask, so hosts may release and immediately re-claim without a transient fallback widget. `isClaimed(onChange)` checks both callback identity and the current runtime ownership, so hosts can detect invalidation after reload/session rebind. Claims are cleared during session shutdown, including `/reload`, so a fresh host runtime must claim again rather than retaining a stale callback. Calling `render()` directly does not claim or change the standalone widget.
## Role folders
`cwd` starts a sub-agent in a directory with its own config, so role-specific setups (CLAUDE.md, skills, extensions) apply:
```
project/
└── agents/
├── game-designer/ ← CLAUDE.md, .pi/…
└── sre/ ← CLAUDE.md, .pi/…
```
```typescript
subagent({ agent: "worker", cwd: "agents/sre", task: "Review the deployment pipeline" });
```
Set a per-agent default with `cwd:` in frontmatter.
## Status widget & configuration
The widget tracks each sub-agent from a runtime activity snapshot written by the child: `starting`, `active` (turn/provider/tool work), `waiting` (open for input or another stage), `stalled` (no valid snapshot for too long), or `running` (fallback). Sub-agent sessions also show their own tools widget — toggle it with `Ctrl+Alt+O`. Completion messages expand with `Ctrl+O`.
Status display is configured via `config.json` in the extension directory (copy `config.json.example`; it's gitignored):
```json
{
"status": { "enabled": true }
}
```
## Install
```bash
pi install npm:@_jb/pi-interactive-subagents
```
## Requirements
- [Pi](https://github.com/earendil-works/pi-mono)
- One supported terminal manager:
- [Herdr](https://herdr.dev), with Pi launched inside Herdr
- [Orca](https://orca.build), with Pi launched inside an Orca-managed terminal
- [tmux](https://github.com/tmux/tmux)
Herdr requires no extension configuration once its Pi integration is installed. The extension detects `HERDR_ENV=1` and creates subagents as unfocused tabs in the current workspace.
Orca requires no extension configuration either. The extension detects `ORCA_TERMINAL_HANDLE` and creates subagents as unfocused terminal tabs in the current worktree via the `orca` CLI (`ORCA_CLI_COMMAND` is honored when set). New tabs start in the parent Pi's working directory with a bash shell.
For tmux:
```bash
tmux new -A -s pi 'pi'
```
## Acknowledgements
Forked from [HazAT/pi-interactive-subagents](https://github.com/HazAT/pi-interactive-subagents), which originated the subagent architecture, the multi-multiplexer surface layer, and the status widget; its supervision features were inspired by [RepoPrompt](https://repoprompt.com/).
## License
MIT
@@ -0,0 +1,51 @@
---
name: researcher
description: Web researcher — searches the web and synthesizes findings
tools: safe_bash
model: openai-codex/gpt-5.6-luna
thinking: xhigh
system-prompt: append
auto-exit: true
---
You are a research specialist. Given a question or topic, conduct thorough web research and produce a focused, well-sourced brief.
You operate in an isolated context with no knowledge of any prior conversation. All necessary context is in the task description.
Process:
1. Break the question into 2-4 searchable facets
2. Search with the web using varied angles
3. Read the answers. Identify what's well-covered, what has gaps.
4. For the 2-3 most promising source URLs, use firecrawl to get full page content
5. Synthesize everything into a brief that directly answers the question
Search strategy — always vary your angles:
- Direct answer query (the obvious one)
- Authoritative source query (official docs, specs, primary sources)
- Practical experience query (case studies, benchmarks, real-world usage)
- Recent developments query (only if the topic is time-sensitive)
Evaluation — what to keep vs drop:
- Official docs and primary sources outweigh blog posts and forum threads
- Recent sources outweigh stale ones
- Sources that directly address the question outweigh tangentially related ones
- Drop: SEO filler, outdated info, beginner tutorials (unless that's the audience)
If the first round of searches doesn't fully answer the question, search again with refined queries targeting the gaps.
Your FINAL assistant message is your entire deliverable — it must stand alone, using this format:
## Summary
2-3 sentence direct answer.
## Findings
Numbered findings with inline source citations:
1. **Finding** — explanation. [Source](url)
2. **Finding** — explanation. [Source](url)
## Sources
- Kept: Source Title (url) — why relevant
- Dropped: Source Title — why excluded
## Gaps
What couldn't be answered. Suggested next steps.
@@ -0,0 +1,40 @@
---
name: scout
description: Fast codebase recon — explores files, finds patterns, maps architecture
tools: read, grep, find, ls
model: openai-codex/gpt-5.6-luna
thinking: xhigh
system-prompt: append
auto-exit: true
---
You are a scout agent. Quickly investigate a codebase and return structured findings.
You operate in an isolated context with no knowledge of any prior conversation. All necessary context is in the task description. You are read-only: never build, test, or modify anything.
Thoroughness (infer from task, default medium):
- Quick: Targeted lookups, key files only
- Medium: Follow imports, read critical sections
- Thorough: Trace all dependencies, check tests/types
Strategy:
1. grep/find to locate relevant code
2. Read key sections (not entire files)
3. Identify types, interfaces, key functions
4. Note dependencies between files
Your FINAL assistant message is your entire deliverable — it must stand alone, using this format:
## Files Found
List with exact line ranges:
1. `path/to/file.ts` (lines 10-50) — Description
2. `path/to/other.ts` (lines 100-150) — Description
## Key Code
Critical types, interfaces, or functions with actual code snippets.
## Architecture
Brief explanation of how the pieces connect.
## Start Here
Which file to look at first and why.
@@ -0,0 +1,79 @@
---
name: worker
description: General-purpose worker — reads, writes, and edits code
tools: read, write, edit, bash
subagent_agents: scout, researcher
model: openai-codex/gpt-5.6-luna
thinking: xhigh
system-prompt: append
auto-exit: true
---
You are a worker agent. You operate in an isolated context — you have no knowledge of any prior conversation. All necessary context will be provided in the task description.
You run in your own pane and work autonomously to complete the assigned task. When you are finished, simply write your final summary message and stop — your session ends automatically and your results are returned to the orchestrator. Do not announce that you are finishing; just produce the answer. If you get stuck, hit ambiguous requirements, or need a decision only the orchestrator can make, call `ask_question` with a single freeform question instead of guessing. Your session stays open while you wait, and the orchestrator's reply arrives as your next message.
Guidelines:
- Read files before editing to understand existing code
- Make targeted edits, not wholesale rewrites
- Use `bash` for running commands (tests, builds, installs, etc.)
- If something fails, diagnose and fix it
- Your FINAL assistant message should summarize what you did and what changed
## Delegation — protecting your context window
Your context is finite. Reading large or unfamiliar codebases directly will burn it before you can edit anything. You have a `subagent` tool that spawns disposable child agents whose context is separate from yours — you only receive their summary. Use it.
You can dispatch:
- **scout** — read-only recon (read, grep, find, ls). Returns a structured map of files, line ranges, and key snippets. Cheap (haiku). Use for *exploring unfamiliar territory*.
- **researcher** — web research. Returns a sourced brief. Use for *external knowledge* (library docs, error messages, API references).
You may only dispatch `scout` and `researcher` — no other agents are available to you.
**Always select the agent with the `agent` field**, e.g. `subagent({ agent: "scout", name: "recon", task: "…" })`. The `name` field is only a cosmetic pane label — it does NOT pick the agent. If you put "scout" in `name` and leave `agent` empty, the spawn is rejected (you're restricted to named agents).
### When to dispatch a scout vs. read directly
Dispatch a scout when:
- The task brief names a feature/area but not specific files ("fix the auth flow", "add a field to user settings")
- You'd need to grep + read 5+ files just to orient
- You only need to know *where* something lives or *what shape* it has, not its full source
Read directly when:
- The brief gives you explicit file paths
- You already know the file you need to edit
- You need the exact bytes for an `edit` call (scouts return summaries, not verbatim source — re-read the 1–3 files you actually edit)
A good rhythm: **scout to find, read to edit.** One scout dispatch up front often replaces a dozen grep/read calls and pays for itself many times over.
### When to dispatch a researcher vs. web_fetch directly
Dispatch a researcher when:
- The question is open-ended ("what's the idiomatic way to X in library Y")
- You'd need to search + read 3+ pages to triangulate
- You want sources synthesized, not raw HTML in your context
Fetch directly when:
- You already have the exact URL (a known docs page, a GitHub issue)
- You need a single specific piece of information from one page
### Parallelism
If you need two independent investigations (e.g. "map the auth code" AND "look up the library's session API"), emit multiple `subagent` tool calls in the same turn — they run in parallel automatically. Don't serialize independent work. After spawning, the results arrive as steer messages — don't poll or fabricate them.
After dispatching subagents you can just say what you're waiting for and stop the turn — your session will **not** close while children are still running. It stays open until every child has reported back, then wakes you with each result. Don't spin in a loop trying to "check" on them.
### What a subagent doesn't replace
Subagents can't edit files for you. You still do the `edit`/`write` calls yourself, with the focused context the scouts gave you. Treat them as a context-protecting prefetch, not a substitute for thinking.
## Output format when done
## Changes Made
- `path/to/file.ts` — what changed and why
## Verification
How you verified the changes work (tests run, build succeeded, etc.)
## Notes
Any caveats, follow-up items, or decisions made.
@@ -0,0 +1,5 @@
{
"status": {
"enabled": true
}
}
@@ -0,0 +1,5 @@
# Lease the status widget to optional compositors
Subagents exposes its running-status panel through a structural, process-local interface while retaining ownership of the standalone widget. A host claims a callback-identified lease and must verify it before rendering; runtime replacement invalidates the lease and stale releases cannot affect a newer owner. This avoids a package dependency or shared TUI class identity while preserving running subagents across reload and restoring the standalone panel whenever no valid host owns it.
The composable surface is `statusWidget.render(width, maxBodyRows?, theme?)`. The optional third argument accepts the small structural `{ fg(color, text) }` theme contract, allowing hosted output to use the current Pi theme without importing a shared Theme class. Standalone rendering passes its factory-provided Pi theme. Calls without a theme use a legacy ANSI fallback. Both hosted and standalone output use a compact unboxed `Subagents <count> running` header followed by capped, width-safe body rows; the cap includes an overflow row but not the header. Status colors remain semantic: active/running warning, stalled error, starting dim, waiting accent, with provider tags and elapsed times dimmed.
@@ -0,0 +1,61 @@
{
"name": "@_jb/pi-interactive-subagents",
"version": "3.7.2",
"description": "Interactive subagents for Pi — spawn, orchestrate, and manage sub-agent sessions in Orca, Herdr or tmux.",
"type": "module",
"license": "MIT",
"author": "JB",
"homepage": "https://git.jb.codes/jb/harness-tools/src/branch/main/extensions/interactive-subagents",
"repository": {
"type": "git",
"url": "git+ssh://git@git.jb.codes:7722/jb/harness-tools.git",
"directory": "extensions/interactive-subagents"
},
"bugs": {
"url": "https://git.jb.codes/jb/harness-tools/issues"
},
"engines": {
"node": ">=22.19.0"
},
"files": [
"pi-extension/**/*",
"agents/**/*",
"config.json.example",
"README.md",
"LICENSE"
],
"keywords": [
"pi-package",
"pi-extension",
"subagents",
"herdr",
"tmux"
],
"pi": {
"extensions": [
"./pi-extension/subagents/index.ts"
]
},
"scripts": {
"test": "node --test test/test.ts test/system-prompt-mode.test.ts",
"test:integration": "node --test --test-concurrency=1 test/integration/*.test.ts",
"typecheck": "tsc --noEmit",
"prepublishOnly": "bun run typecheck && bun run test"
},
"peerDependencies": {
"@earendil-works/pi-coding-agent": "catalog:pi-peer",
"@earendil-works/pi-tui": "catalog:pi-peer",
"typebox": "catalog:pi-peer"
},
"devDependencies": {
"@earendil-works/pi-coding-agent": "catalog:",
"@earendil-works/pi-tui": "catalog:",
"@types/node": "catalog:",
"typebox": "catalog:",
"typescript": "catalog:"
},
"publishConfig": {
"access": "public",
"registry": "https://git.jb.codes/api/packages/jb/npm/"
}
}
@@ -0,0 +1,513 @@
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
export type SubagentActivityPhase = "starting" | "active" | "waiting" | "done";
export type SubagentActivityScope = "agent" | "turn" | "provider" | "streaming" | "tool";
export type SubagentActivityEvent =
| "session_start"
| "input"
| "before_agent_start"
| "agent_start"
| "agent_end"
| "turn_start"
| "turn_end"
| "before_provider_request"
| "after_provider_response"
| "message_update"
| "tool_execution_start"
| "tool_call"
| "tool_execution_update"
| "tool_result"
| "tool_execution_end"
| "ask_question"
| "session_shutdown";
export interface SubagentActivityState {
version: 1;
runningChildId: string;
createdAt: number;
updatedAt: number;
sequence: number;
latestEvent: SubagentActivityEvent;
phase: SubagentActivityPhase;
agentActive: boolean;
turnActive: boolean;
providerActive: boolean;
toolActive: boolean;
activeScope?: SubagentActivityScope;
activeSince?: number;
waitingSince?: number;
turnIndex?: number;
messageEventType?: string;
toolCallId?: string;
toolName?: string;
toolStartedAt?: number;
toolEndedAt?: number;
}
export type ActivityReadResult =
| { ok: true; activity: SubagentActivityState }
| { ok: false; reason: "missing" | "invalid" | "wrong-id"; error?: string };
export type SubagentShutdownReason = "quit" | "reload" | "new" | "resume" | "fork";
export interface SubagentActivityRecorder {
sessionStart(): void;
input(): void;
beforeAgentStart(): void;
agentStart(): void;
agentEndWaiting(): void;
agentEndDone(): void;
turnStart(turnIndex?: number): void;
turnEnd(turnIndex?: number): void;
beforeProviderRequest(): void;
afterProviderResponse(): void;
messageUpdate(messageEventType?: string): void;
toolExecutionStart(toolCallId?: string, toolName?: string): void;
toolCall(toolCallId?: string, toolName?: string): void;
toolExecutionUpdate(toolCallId?: string, toolName?: string): void;
toolResult(toolCallId?: string, toolName?: string): void;
toolExecutionEnd(toolCallId?: string, toolName?: string): void;
askQuestion(): void;
sessionShutdown(reason: SubagentShutdownReason): void;
}
const ACTIVITY_UPDATE_THROTTLE_MS = 500;
const MAX_WRITE_FAILURES = 3;
const KNOWN_PHASES = new Set<SubagentActivityPhase>(["starting", "active", "waiting", "done"]);
const KNOWN_SCOPES = new Set<SubagentActivityScope>(["agent", "turn", "provider", "streaming", "tool"]);
const KNOWN_EVENTS = new Set<SubagentActivityEvent>([
"session_start",
"input",
"before_agent_start",
"agent_start",
"agent_end",
"turn_start",
"turn_end",
"before_provider_request",
"after_provider_response",
"message_update",
"tool_execution_start",
"tool_call",
"tool_execution_update",
"tool_result",
"tool_execution_end",
"ask_question",
"session_shutdown",
]);
const MAX_ACTIVITY_STRING_LENGTH = 200;
export function getSubagentActivityFile(artifactDir: string, runningChildId: string): string {
return join(artifactDir, "subagent-activity", `${runningChildId}.json`);
}
function requireObject(value: unknown): Record<string, unknown> | null {
if (value == null || typeof value !== "object" || Array.isArray(value)) return null;
return value as Record<string, unknown>;
}
function validateFiniteNumber(object: Record<string, unknown>, fieldName: string): string | null {
return Number.isFinite(object[fieldName]) ? null : `${fieldName} must be finite`;
}
function validateOptionalFiniteNumber(object: Record<string, unknown>, fieldName: string): string | null {
const value = object[fieldName];
return value == null || Number.isFinite(value) ? null : `${fieldName} must be finite when present`;
}
function validateInteger(object: Record<string, unknown>, fieldName: string): string | null {
return Number.isInteger(object[fieldName]) ? null : `${fieldName} must be an integer`;
}
function validateOptionalInteger(object: Record<string, unknown>, fieldName: string): string | null {
const value = object[fieldName];
return value == null || Number.isInteger(value) ? null : `${fieldName} must be an integer when present`;
}
function validateBoolean(object: Record<string, unknown>, fieldName: string): string | null {
return typeof object[fieldName] === "boolean" ? null : `${fieldName} must be a boolean`;
}
function validateOptionalActivityString(object: Record<string, unknown>, fieldName: string): string | null {
const value = object[fieldName];
if (value == null) return null;
if (typeof value !== "string") return `${fieldName} must be a string when present`;
if (/\r|\n/.test(value)) return `${fieldName} must not contain newlines`;
return value.length <= MAX_ACTIVITY_STRING_LENGTH ? null : `${fieldName} is too long`;
}
function invalidActivity(error: string): ActivityReadResult {
return { ok: false, reason: "invalid", error };
}
function validateActivity(value: unknown, expectedRunningChildId: string): ActivityReadResult {
const object = requireObject(value);
if (!object) return invalidActivity("activity must be an object");
if (object.version !== 1) return invalidActivity("unsupported activity version");
if (typeof object.runningChildId !== "string") return invalidActivity("runningChildId must be a string");
if (object.runningChildId !== expectedRunningChildId) return { ok: false, reason: "wrong-id" };
if (typeof object.latestEvent !== "string" || !KNOWN_EVENTS.has(object.latestEvent as SubagentActivityEvent)) {
return invalidActivity("unknown latestEvent");
}
if (typeof object.phase !== "string" || !KNOWN_PHASES.has(object.phase as SubagentActivityPhase)) {
return invalidActivity("unknown activity phase");
}
if (
object.activeScope != null &&
(typeof object.activeScope !== "string" || !KNOWN_SCOPES.has(object.activeScope as SubagentActivityScope))
) {
return invalidActivity("unknown activeScope");
}
const validationError = [
validateFiniteNumber(object, "createdAt"),
validateFiniteNumber(object, "updatedAt"),
validateInteger(object, "sequence"),
validateBoolean(object, "agentActive"),
validateBoolean(object, "turnActive"),
validateBoolean(object, "providerActive"),
validateBoolean(object, "toolActive"),
validateOptionalFiniteNumber(object, "activeSince"),
validateOptionalFiniteNumber(object, "waitingSince"),
validateOptionalInteger(object, "turnIndex"),
validateOptionalFiniteNumber(object, "toolStartedAt"),
validateOptionalFiniteNumber(object, "toolEndedAt"),
validateOptionalActivityString(object, "messageEventType"),
validateOptionalActivityString(object, "toolCallId"),
validateOptionalActivityString(object, "toolName"),
].find((error) => error != null);
if (validationError) return invalidActivity(validationError);
return { ok: true, activity: object as unknown as SubagentActivityState };
}
export function readSubagentActivityFile(
activityFile: string,
expectedRunningChildId: string,
): ActivityReadResult {
if (!existsSync(activityFile)) return { ok: false, reason: "missing" };
let parsed: unknown;
try {
parsed = JSON.parse(readFileSync(activityFile, "utf8"));
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return { ok: false, reason: "invalid", error: message };
}
return validateActivity(parsed, expectedRunningChildId);
}
export function writeSubagentActivityFile(activityFile: string, activity: SubagentActivityState): void {
const dir = dirname(activityFile);
mkdirSync(dir, { recursive: true });
const tempFile = join(dir, `${activity.runningChildId}.json.${process.pid}.${activity.sequence}.tmp`);
try {
writeFileSync(tempFile, `${JSON.stringify(activity)}\n`, "utf8");
renameSync(tempFile, activityFile);
} catch (error) {
try {
unlinkSync(tempFile);
} catch (cleanupError) {
// Temp cleanup is best effort; preserve the original write/rename failure
void cleanupError;
}
throw error;
}
}
function createNoopRecorder(): SubagentActivityRecorder {
return {
sessionStart() {},
input() {},
beforeAgentStart() {},
agentStart() {},
agentEndWaiting() {},
agentEndDone() {},
turnStart() {},
turnEnd() {},
beforeProviderRequest() {},
afterProviderResponse() {},
messageUpdate() {},
toolExecutionStart() {},
toolCall() {},
toolExecutionUpdate() {},
toolResult() {},
toolExecutionEnd() {},
askQuestion() {},
sessionShutdown() {},
};
}
function clearActiveState(activity: SubagentActivityState): void {
activity.agentActive = false;
activity.turnActive = false;
activity.providerActive = false;
activity.toolActive = false;
delete activity.activeScope;
delete activity.activeSince;
}
function refreshActiveScope(activity: SubagentActivityState): void {
if (activity.toolActive) {
activity.phase = "active";
activity.activeScope = "tool";
return;
}
if (activity.providerActive) {
activity.phase = "active";
activity.activeScope = "provider";
return;
}
if (activity.turnActive) {
activity.phase = "active";
activity.activeScope = "turn";
return;
}
if (activity.agentActive) {
activity.phase = "active";
activity.activeScope = "agent";
return;
}
delete activity.activeScope;
delete activity.activeSince;
}
function markActive(
activity: SubagentActivityState,
scope: SubagentActivityScope,
now: number,
resetActiveSince = false,
): void {
activity.phase = "active";
activity.activeScope = scope;
if (activity.activeSince == null || resetActiveSince) activity.activeSince = now;
delete activity.waitingSince;
}
export function createSubagentActivityRecorder(params: {
runningChildId?: string;
activityFile?: string;
now?: () => number;
}): SubagentActivityRecorder {
const candidateRunningChildId = params.runningChildId?.trim();
const candidateActivityFile = params.activityFile?.trim();
if (!candidateRunningChildId || !candidateActivityFile) return createNoopRecorder();
const runningChildId: string = candidateRunningChildId;
const activityFile: string = candidateActivityFile;
const now = params.now ?? (() => Date.now());
const createdAt = now();
const activity: SubagentActivityState = {
version: 1,
runningChildId,
createdAt,
updatedAt: createdAt,
sequence: 0,
latestEvent: "session_start",
phase: "starting",
agentActive: false,
turnActive: false,
providerActive: false,
toolActive: false,
};
let disabled = false;
let failureCount = 0;
let lastFlushAt = 0;
let pendingFlush: ReturnType<typeof setTimeout> | null = null;
function clearPendingFlush(): void {
if (!pendingFlush) return;
clearTimeout(pendingFlush);
pendingFlush = null;
}
function disable(): void {
disabled = true;
clearPendingFlush();
}
function flushNow(): void {
if (disabled) return;
try {
writeSubagentActivityFile(activityFile, activity);
lastFlushAt = now();
failureCount = 0;
} catch {
failureCount += 1;
if (failureCount >= MAX_WRITE_FAILURES) disable();
}
}
function scheduleFlush(): void {
if (disabled || pendingFlush) return;
const remainingMs = Math.max(0, ACTIVITY_UPDATE_THROTTLE_MS - (now() - lastFlushAt));
if (remainingMs === 0) {
flushNow();
return;
}
pendingFlush = setTimeout(() => {
pendingFlush = null;
flushNow();
}, remainingMs);
}
function record(
latestEvent: SubagentActivityEvent,
update: (current: SubagentActivityState, now: number) => void,
flush: "immediate" | "throttled",
): void {
if (disabled) return;
if (flush === "immediate") clearPendingFlush();
const observedAt = now();
activity.latestEvent = latestEvent;
activity.updatedAt = observedAt;
activity.sequence += 1;
update(activity, observedAt);
if (flush === "immediate") flushNow();
else scheduleFlush();
}
function markDone(latestEvent: SubagentActivityEvent): void {
record(latestEvent, (current) => {
current.phase = "done";
clearActiveState(current);
delete current.waitingSince;
}, "immediate");
disable();
}
return {
sessionStart() {
record("session_start", (current) => {
current.phase = "starting";
clearActiveState(current);
delete current.waitingSince;
}, "immediate");
},
input() {
record("input", () => {}, "immediate");
},
beforeAgentStart() {
record("before_agent_start", (current, observedAt) => {
current.agentActive = true;
markActive(current, "agent", observedAt);
}, "immediate");
},
agentStart() {
record("agent_start", (current, observedAt) => {
current.agentActive = true;
markActive(current, "agent", observedAt);
}, "immediate");
},
agentEndWaiting() {
record("agent_end", (current, observedAt) => {
clearActiveState(current);
current.phase = "waiting";
current.waitingSince = observedAt;
}, "immediate");
},
agentEndDone() {
markDone("agent_end");
},
turnStart(turnIndex) {
record("turn_start", (current, observedAt) => {
current.agentActive = true;
current.turnActive = true;
if (turnIndex != null) current.turnIndex = turnIndex;
markActive(current, current.toolActive || current.providerActive ? current.activeScope ?? "turn" : "turn", observedAt);
}, "immediate");
},
turnEnd(turnIndex) {
record("turn_end", (current) => {
current.turnActive = false;
current.providerActive = false;
current.toolActive = false;
if (turnIndex != null) current.turnIndex = turnIndex;
refreshActiveScope(current);
}, "immediate");
},
beforeProviderRequest() {
record("before_provider_request", (current, observedAt) => {
current.providerActive = true;
markActive(current, "provider", observedAt, true);
}, "immediate");
},
afterProviderResponse() {
record("after_provider_response", (current) => {
current.providerActive = false;
refreshActiveScope(current);
}, "immediate");
},
messageUpdate(messageEventType) {
record("message_update", (current, observedAt) => {
current.agentActive = true;
current.turnActive = true;
current.messageEventType = messageEventType;
if (!current.toolActive) markActive(current, "streaming", observedAt);
}, "throttled");
},
toolExecutionStart(toolCallId, toolName) {
record("tool_execution_start", (current, observedAt) => {
current.toolActive = true;
current.toolCallId = toolCallId;
current.toolName = toolName;
current.toolStartedAt = observedAt;
markActive(current, "tool", observedAt, true);
}, "immediate");
},
toolCall(toolCallId, toolName) {
record("tool_call", (current, observedAt) => {
current.toolActive = true;
current.toolCallId = toolCallId ?? current.toolCallId;
current.toolName = toolName ?? current.toolName;
markActive(current, "tool", observedAt);
}, "immediate");
},
toolExecutionUpdate(toolCallId, toolName) {
record("tool_execution_update", (current, observedAt) => {
current.toolActive = true;
current.toolCallId = toolCallId ?? current.toolCallId;
current.toolName = toolName ?? current.toolName;
markActive(current, "tool", observedAt);
}, "throttled");
},
toolResult(toolCallId, toolName) {
record("tool_result", (current) => {
current.toolCallId = toolCallId ?? current.toolCallId;
current.toolName = toolName ?? current.toolName;
refreshActiveScope(current);
}, "immediate");
},
toolExecutionEnd(toolCallId, toolName) {
record("tool_execution_end", (current, observedAt) => {
current.toolActive = false;
current.toolCallId = toolCallId ?? current.toolCallId;
current.toolName = toolName ?? current.toolName;
current.toolEndedAt = observedAt;
refreshActiveScope(current);
}, "immediate");
},
askQuestion() {
// The subagent paused to ask the orchestrator a question. Park it in the
// "waiting" phase (do NOT disable the recorder) so the status widget shows
// it as waiting and recording resumes when the answer arrives.
record("ask_question", (current, observedAt) => {
clearActiveState(current);
current.phase = "waiting";
current.waitingSince = observedAt;
}, "immediate");
},
sessionShutdown(reason) {
if (reason === "quit") markDone("session_shutdown");
else disable();
},
};
}
@@ -0,0 +1,178 @@
/** Herdr surface backend. Subagents run in unfocused tabs in the current workspace. */
import { execFile, execFileSync } from "node:child_process";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
const commandAvailability = new Map<string, boolean>();
const tabByPane = new Map<string, string>();
interface HerdrTabCreated {
result?: {
root_pane?: { pane_id?: unknown };
tab?: { tab_id?: unknown };
};
}
interface HerdrPaneCreated {
result?: {
pane?: { pane_id?: unknown };
};
}
function hasCommand(command: string): boolean {
const cached = commandAvailability.get(command);
if (cached !== undefined) return cached;
let available = false;
try {
execFileSync("sh", ["-c", `command -v ${command}`], { stdio: "ignore" });
available = true;
} catch {
available = false;
}
commandAvailability.set(command, available);
return available;
}
export function isAvailable(): boolean {
return (
process.env.HERDR_ENV === "1" &&
!!process.env.HERDR_WORKSPACE_ID &&
!!process.env.HERDR_PANE_ID &&
hasCommand("herdr")
);
}
export function setupHint(): string {
return "Start pi inside a Herdr-managed pane with the `herdr` CLI on PATH.";
}
function requireHerdr(): void {
if (!isAvailable()) {
throw new Error(`Herdr is required for this surface. ${setupHint()}`);
}
}
function parseJson<T>(command: string, output: string): T {
try {
return JSON.parse(output) as T;
} catch {
throw new Error(`Unexpected Herdr ${command} output: ${output.trim() || "(empty)"}`);
}
}
function requireId(value: unknown, command: string, field: string): string {
if (typeof value !== "string" || value.length === 0) {
throw new Error(`Herdr ${command} did not return ${field}`);
}
return value;
}
export function parseCreatedTab(output: string): { paneId: string; tabId: string } {
const response = parseJson<HerdrTabCreated>("tab create", output);
return {
paneId: requireId(response.result?.root_pane?.pane_id, "tab create", "result.root_pane.pane_id"),
tabId: requireId(response.result?.tab?.tab_id, "tab create", "result.tab.tab_id"),
};
}
export function parseCreatedPane(output: string): string {
const response = parseJson<HerdrPaneCreated>("pane split", output);
return requireId(response.result?.pane?.pane_id, "pane split", "result.pane.pane_id");
}
export function createSurface(name: string): string {
requireHerdr();
const workspace = process.env.HERDR_WORKSPACE_ID!;
const output = execFileSync(
"herdr",
[
"tab",
"create",
"--workspace",
workspace,
"--cwd",
process.cwd(),
"--label",
name,
"--no-focus",
],
{ encoding: "utf8" },
);
const { paneId, tabId } = parseCreatedTab(output);
tabByPane.set(paneId, tabId);
return paneId;
}
export function createSurfaceSplit(
name: string,
direction: "left" | "right" | "up" | "down",
fromSurface?: string,
): string {
requireHerdr();
if (direction === "left" || direction === "up") {
throw new Error(`Herdr pane splits only support right or down, not ${direction}`);
}
const source = fromSurface ?? process.env.HERDR_PANE_ID!;
const output = execFileSync(
"herdr",
[
"pane",
"split",
"--pane",
source,
"--direction",
direction,
"--cwd",
process.cwd(),
"--no-focus",
],
{ encoding: "utf8" },
);
const paneId = parseCreatedPane(output);
try {
execFileSync("herdr", ["pane", "rename", paneId, name], { encoding: "utf8" });
} catch {
// A cosmetic label must not break a successful split.
}
return paneId;
}
export function sendCommand(surface: string, command: string): void {
requireHerdr();
execFileSync("herdr", ["pane", "run", surface, command], { encoding: "utf8" });
}
export function readScreen(surface: string, lines = 50): string {
requireHerdr();
return execFileSync(
"herdr",
["pane", "read", surface, "--source", "recent-unwrapped", "--lines", String(Math.max(1, lines))],
{ encoding: "utf8" },
);
}
export async function readScreenAsync(surface: string, lines = 50): Promise<string> {
requireHerdr();
const { stdout } = await execFileAsync(
"herdr",
["pane", "read", surface, "--source", "recent-unwrapped", "--lines", String(Math.max(1, lines))],
{ encoding: "utf8" },
);
return stdout;
}
export function closeSurface(surface: string): void {
requireHerdr();
const tabId = tabByPane.get(surface);
if (tabId) {
execFileSync("herdr", ["tab", "close", tabId], { encoding: "utf8" });
tabByPane.delete(surface);
return;
}
execFileSync("herdr", ["pane", "close", surface], { encoding: "utf8" });
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,294 @@
/**
* Orca surface backend. Subagents run in unfocused terminal tabs in the
* current Orca worktree.
*
* Detection relies on ORCA_TERMINAL_HANDLE, which Orca exports inside its
* managed terminals. Inside those terminals `orca` resolves to the Orca CLI
* shim; outside them bare `orca` on Linux is usually the GNOME screen reader,
* so resolution explicitly rejects /usr/bin/orca.
*/
import { execFile, execFileSync } from "node:child_process";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
/** GNOME screen reader path — never a valid Orca CLI. */
const GNOME_SCREEN_READER = "/usr/bin/orca";
let cachedCli: string | null | undefined;
/** Terminal handles created as whole tabs (vs splits), mapped for tab close. */
const tabByHandle = new Map<string, string>();
interface OrcaResponse {
ok?: boolean;
error?: { message?: unknown };
}
interface OrcaTerminalCreated extends OrcaResponse {
result?: {
terminal?: { handle?: unknown; tabId?: unknown };
};
}
interface OrcaSplitCreated extends OrcaResponse {
result?: {
split?: { handle?: unknown };
};
}
interface OrcaTerminalRead extends OrcaResponse {
result?: {
terminal?: { tail?: unknown; status?: unknown };
};
}
/**
* Marker echoed by the startup command once the new terminal has cd'd into
* the target directory and is about to exec bash. Written with a quote split
* (`__PI_SURFACE_''READY__`) so the typed command line on screen never
* matches, only the echoed output does.
*/
const READY_MARKER = "__PI_SURFACE_READY__";
function resolveCli(): string | null {
if (cachedCli !== undefined) return cachedCli;
const override = process.env.ORCA_CLI_COMMAND?.trim();
if (override) {
cachedCli = override;
return cachedCli;
}
try {
const resolved = execFileSync("sh", ["-c", "command -v orca"], { encoding: "utf8" }).trim();
cachedCli = resolved && resolved !== GNOME_SCREEN_READER ? "orca" : null;
} catch {
cachedCli = null;
}
return cachedCli;
}
export function isAvailable(): boolean {
return !!process.env.ORCA_TERMINAL_HANDLE && resolveCli() !== null;
}
export function setupHint(): string {
return "Start pi inside an Orca-managed terminal with the `orca` CLI on PATH.";
}
function requireOrca(): string {
const cli = resolveCli();
if (!isAvailable() || !cli) {
throw new Error(`Orca is required for this surface. ${setupHint()}`);
}
return cli;
}
function shellEscape(value: string): string {
return "'" + value.replace(/'/g, "'\\''") + "'";
}
/** Startup command that lands the new terminal in pi's cwd with a bash shell. */
function startupCommand(): string {
return `cd ${shellEscape(process.cwd())} && echo __PI_SURFACE_''READY__ && exec bash`;
}
function sleepSync(seconds: number): void {
try {
execFileSync("sleep", [String(seconds)], { stdio: "ignore" });
} catch {}
}
/** Probe marker proving the shell reads and executes typed input. */
const SHELL_UP_MARKER = "__PI_SHELL_UP__";
/**
* Block until the new terminal's shell is genuinely interactive.
*
* Two races exist at startup: input typed before the initial shell execs bash
* is swallowed with it, and input typed while bash itself initializes can be
* flushed (observed as stray kitty-keyboard residue like `0u` at the prompt).
* So first wait for the startup command's ready marker, then repeatedly send a
* probe echo until its output appears — an executed probe also clears any
* residue from the readline buffer. Falls through after the deadline as
* best-effort.
*/
function waitForSurfaceReady(surface: string, timeoutMs = 15_000): void {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
try {
if (readScreen(surface, 40).includes(READY_MARKER)) break;
} catch {}
sleepSync(0.25);
}
while (Date.now() < deadline) {
try {
sendCommand(surface, "echo __PI_SHELL_''UP__");
} catch {
sleepSync(0.25);
continue;
}
const probeDeadline = Math.min(Date.now() + 2_000, deadline);
while (Date.now() < probeDeadline) {
sleepSync(0.25);
try {
if (readScreen(surface, 40).includes(SHELL_UP_MARKER)) return;
} catch {}
}
}
}
function parseResponse<T extends OrcaResponse>(command: string, output: string): T {
let response: T;
try {
response = JSON.parse(output) as T;
} catch {
throw new Error(`Unexpected Orca ${command} output: ${output.trim() || "(empty)"}`);
}
if (response.ok === false) {
const message =
typeof response.error?.message === "string" ? response.error.message : "unknown error";
throw new Error(`Orca ${command} failed: ${message}`);
}
return response;
}
function requireId(value: unknown, command: string, field: string): string {
if (typeof value !== "string" || value.length === 0) {
throw new Error(`Orca ${command} did not return ${field}`);
}
return value;
}
export function parseCreatedTerminal(output: string): { handle: string; tabId: string } {
const response = parseResponse<OrcaTerminalCreated>("terminal create", output);
return {
handle: requireId(response.result?.terminal?.handle, "terminal create", "result.terminal.handle"),
tabId: requireId(response.result?.terminal?.tabId, "terminal create", "result.terminal.tabId"),
};
}
export function parseCreatedSplit(output: string): string {
const response = parseResponse<OrcaSplitCreated>("terminal split", output);
return requireId(response.result?.split?.handle, "terminal split", "result.split.handle");
}
export function parseReadTail(output: string): string {
const response = parseResponse<OrcaTerminalRead>("terminal read", output);
const status = response.result?.terminal?.status;
if (typeof status === "string" && status !== "running") {
throw new Error(`Orca terminal read: terminal is not running (status: ${status})`);
}
const tail = response.result?.terminal?.tail;
if (!Array.isArray(tail)) {
throw new Error("Orca terminal read did not return result.terminal.tail");
}
return tail.filter((line): line is string => typeof line === "string").join("\n");
}
export function createSurface(name: string): string {
const cli = requireOrca();
// Target the enclosing terminal's worktree explicitly: pi's cwd (which the
// CLI would otherwise resolve against) may live outside any Orca worktree.
const worktreeId = process.env.ORCA_WORKTREE_ID;
const output = execFileSync(
cli,
[
"terminal",
"create",
...(worktreeId ? ["--worktree", `id:${worktreeId}`] : []),
"--title",
name,
"--command",
startupCommand(),
"--json",
],
{ encoding: "utf8" },
);
const { handle, tabId } = parseCreatedTerminal(output);
tabByHandle.set(handle, tabId);
waitForSurfaceReady(handle);
return handle;
}
export function createSurfaceSplit(
name: string,
direction: "left" | "right" | "up" | "down",
fromSurface?: string,
): string {
void name;
const cli = requireOrca();
if (direction === "left" || direction === "up") {
throw new Error(`Orca terminal splits only support right or down, not ${direction}`);
}
const source = fromSurface ?? process.env.ORCA_TERMINAL_HANDLE!;
const output = execFileSync(
cli,
[
"terminal",
"split",
"--terminal",
source,
"--direction",
direction === "right" ? "horizontal" : "vertical",
"--command",
startupCommand(),
"--json",
],
{ encoding: "utf8" },
);
const handle = parseCreatedSplit(output);
waitForSurfaceReady(handle);
return handle;
}
export function sendCommand(surface: string, command: string): void {
const cli = requireOrca();
const output = execFileSync(
cli,
["terminal", "send", "--terminal", surface, "--text", command, "--enter", "--json"],
{ encoding: "utf8" },
);
parseResponse("terminal send", output);
}
function readScreenArgs(surface: string, lines: number): string[] {
return [
"terminal",
"read",
"--terminal",
surface,
"--screen",
"--limit",
String(Math.max(1, lines)),
"--json",
];
}
export function readScreen(surface: string, lines = 50): string {
const cli = requireOrca();
const output = execFileSync(cli, readScreenArgs(surface, lines), { encoding: "utf8" });
return parseReadTail(output);
}
export async function readScreenAsync(surface: string, lines = 50): Promise<string> {
const cli = requireOrca();
const { stdout } = await execFileAsync(cli, readScreenArgs(surface, lines), {
encoding: "utf8",
});
return parseReadTail(stdout);
}
export function closeSurface(surface: string): void {
const cli = requireOrca();
const args = ["terminal", "close", "--terminal", surface];
if (tabByHandle.has(surface)) args.push("--tab");
args.push("--json");
const output = execFileSync(cli, args, { encoding: "utf8" });
parseResponse("terminal close", output);
tabByHandle.delete(surface);
}
@@ -0,0 +1,5 @@
{
"name": "pi-auto-exit",
"version": "1.0.0",
"description": "Auto-exit plugin for pi-spawned Claude sessions"
}
@@ -0,0 +1,15 @@
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks/on-stop.sh",
"timeout": 10
}
]
}
]
}
}
@@ -0,0 +1,68 @@
#!/usr/bin/env bash
# Stop hook for pi-spawned Claude sessions.
# Writes a sentinel file when Claude completes autonomously (no user interjection).
set -euo pipefail
# Read JSON input from stdin
input=$(cat)
# Guard: if stop_hook_active is true, we're in a loop — bail out
stop_hook_active=$(echo "$input" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('stop_hook_active', False))" 2>/dev/null || echo "False")
if [ "$stop_hook_active" = "True" ]; then
exit 0
fi
# Guard: only act for pi-spawned sessions
if [ -z "${PI_CLAUDE_SENTINEL:-}" ]; then
exit 0
fi
# Get transcript path
transcript_path=$(echo "$input" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('transcript_path', ''))" 2>/dev/null || echo "")
if [ -z "$transcript_path" ] || [ ! -f "$transcript_path" ]; then
exit 0
fi
# Count real human messages in transcript (not tool results)
# Claude's transcript format:
# Human message: {"type": "user", "message": {"role": "user", "content": "..."}}
# Tool result: {"type": "user", "message": {"role": "user", "content": [{"type": "tool_result", ...}]}}
# We only count entries where content is a string (real human input)
user_msg_count=$(python3 - "$transcript_path" <<'EOF'
import sys, json
transcript_path = sys.argv[1]
count = 0
with open(transcript_path, 'r') as f:
for line in f:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
if entry.get('type') != 'user':
continue
content = entry.get('message', {}).get('content', '')
# Real human messages have string content
# Tool results have array content with tool_result blocks
if isinstance(content, str):
count += 1
except (json.JSONDecodeError, AttributeError):
pass
print(count)
EOF
)
# Always write transcript path so the watcher can copy the session file
if [ -n "$transcript_path" ]; then
echo "$transcript_path" > "${PI_CLAUDE_SENTINEL}.transcript" 2>/dev/null || true
fi
# If exactly 1 user message (the initial prompt), this was autonomous — signal completion
if [ "$user_msg_count" -eq 1 ]; then
# Write last_assistant_message to sentinel so the watcher gets a clean result
echo "$input" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('last_assistant_message', ''))" > "$PI_CLAUDE_SENTINEL" 2>/dev/null || touch "$PI_CLAUDE_SENTINEL"
fi
exit 0
@@ -0,0 +1,625 @@
import {
appendFileSync,
closeSync,
copyFileSync,
existsSync,
mkdirSync,
openSync,
readFileSync,
readSync,
readdirSync,
renameSync,
statSync,
writeFileSync,
} from "node:fs";
import { randomBytes, randomUUID } from "node:crypto";
import { dirname, join } from "node:path";
export interface SessionEntry {
type: string;
id: string;
parentId?: string;
[key: string]: unknown;
}
export interface MessageEntry extends SessionEntry {
type: "message";
message: {
role: "user" | "assistant" | "toolResult";
content: Array<{ type: string; text?: string; [key: string]: unknown }>;
};
}
export type SeededSubagentSessionMode = "lineage-only" | "fork";
function getForkContentLines(parentSessionFile: string): string[] {
const raw = readFileSync(parentSessionFile, "utf8");
const lines = raw.split("\n").filter((line) => line.trim());
let truncateAt = lines.length;
for (let i = lines.length - 1; i >= 0; i--) {
try {
const entry = JSON.parse(lines[i]);
if (entry.type === "message" && entry.message?.role === "user") {
truncateAt = i;
break;
}
} catch {
// ignore malformed lines
}
}
return lines.slice(0, truncateAt).filter((line) => {
try {
return JSON.parse(line).type !== "session";
} catch {
return true;
}
});
}
export function seedSubagentSessionFile(params: {
mode: SeededSubagentSessionMode;
parentSessionFile: string;
childSessionFile: string;
childCwd: string;
}): void {
const header = {
type: "session",
version: 3,
id: randomUUID(),
timestamp: new Date().toISOString(),
cwd: params.childCwd,
parentSession: params.parentSessionFile,
};
const contentLines =
params.mode === "fork" ? getForkContentLines(params.parentSessionFile) : [];
const lines = [JSON.stringify(header), ...contentLines];
mkdirSync(dirname(params.childSessionFile), { recursive: true });
writeFileSync(params.childSessionFile, lines.join("\n") + "\n", "utf8");
}
/**
* A snapshot of everything needed to reconstruct a subagent's sandbox when its
* session is later resumed via `subagent_message({ sessionId })`.
*
* Written next to the session file as `<sessionFile>.loadout.json` at spawn
* time. Resume replays this exact snapshot so the reincarnated process gets the
* same `--no-extensions` + `--tools` restriction, model, identity, spawn
* whitelist, cwd, and config dir it originally ran with — instead of falling
* back to pi's default (all global extensions + full toolset). Storing the
* resolved loadout (rather than re-deriving from the agent `.md` by name) keeps
* resume faithful even if the agent definition is later edited, moved, or
* deleted.
*/
export interface SubagentLoadout {
/** Agent profile name (for PI_SUBAGENT_AGENT); null for agentless spawns. */
agent: string | null;
/** The `--tools` allowlist string, or null when the spawn was unrestricted. */
toolAllowlist: string | null;
/** Model id (without thinking suffix), or null to use the session default. */
model: string | null;
/** Thinking level appended to the model as `model:level`, or null. */
thinking: string | null;
/** How the identity text was applied: append/replace, or null. */
systemPromptMode: "append" | "replace" | null;
/** The system-prompt/identity text, only when it lived in the system prompt. */
identity: string | null;
/** Agents this subagent was allowed to spawn (for PI_SUBAGENT_ALLOWED). */
spawnable: string[] | null;
/** Whether the agent auto-exits (informational; resume forces autonomous). */
autoExit: boolean;
/** Working directory the subagent ran in, or null. */
cwd: string | null;
/** PI_CODING_AGENT_DIR the subagent resolved config/extensions from, or null. */
agentDir: string | null;
}
/** Path of the loadout sidecar written next to a subagent session file. */
export function loadoutSidecarPath(sessionFile: string): string {
return `${sessionFile}.loadout.json`;
}
/** Persist a subagent's resolved sandbox loadout beside its session file. */
export function writeSubagentLoadout(sessionFile: string, loadout: SubagentLoadout): void {
try {
writeFileSync(loadoutSidecarPath(sessionFile), JSON.stringify(loadout), "utf8");
} catch {
// Best-effort: a missing snapshot only means resume will refuse, never that
// it launches unrestricted.
}
}
/** Read a subagent's loadout snapshot, or null if absent/unparseable. */
export function readSubagentLoadout(sessionFile: string): SubagentLoadout | null {
try {
const p = loadoutSidecarPath(sessionFile);
if (!existsSync(p)) return null;
const parsed = JSON.parse(readFileSync(p, "utf8"));
if (!parsed || typeof parsed !== "object") return null;
return parsed as SubagentLoadout;
} catch {
return null;
}
}
// ── Name registry ────────────────────────────────────────────────────────────
// Each spawner session (the top-level pi session, or a worker that spawns its
// own children) gets a registry mapping a subagent's display name to the
// session file it ran in. Names are unique per spawner session and persist on
// disk, so `subagent_message({ name })` can steer a running subagent or resume
// a finished one by the same handle — even across a pi restart. The registry
// lives in the spawner's own artifact dir, which is directly addressable from
// the spawner's session id (no sessions-tree scan, so resume stays fast).
export interface NameRegistryEntry {
/** Absolute path to the subagent's session .jsonl file. */
sessionFile: string;
/** Canonical session header id (kept for display/lineage). */
sessionId: string | null;
}
export type NameRegistry = Record<string, NameRegistryEntry>;
/** Path of the name registry for a given spawner session's artifact dir. */
export function nameRegistryPath(artifactDir: string): string {
return join(artifactDir, "subagent-registry.json");
}
/** Read a spawner session's name registry, or {} if absent/corrupt. */
export function readNameRegistry(artifactDir: string): NameRegistry {
try {
const p = nameRegistryPath(artifactDir);
if (!existsSync(p)) return {};
const parsed = JSON.parse(readFileSync(p, "utf8"));
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
return parsed as NameRegistry;
} catch {
return {};
}
}
/**
* Register (or overwrite) a name → session mapping for a spawner session.
* Writes atomically (temp file + rename) so a concurrent reader never sees a
* partial registry.
*/
export function registerName(
artifactDir: string,
name: string,
entry: NameRegistryEntry,
): void {
try {
mkdirSync(artifactDir, { recursive: true });
const registry = readNameRegistry(artifactDir);
registry[name] = entry;
const p = nameRegistryPath(artifactDir);
const tmp = `${p}.tmp-${process.pid}-${Math.random().toString(16).slice(2, 8)}`;
writeFileSync(tmp, JSON.stringify(registry, null, 2), "utf8");
renameSync(tmp, p);
} catch {
// Best-effort: a failed registration only means resume-by-name won't find
// this subagent later; it never breaks the spawn itself.
}
}
/** Resolve a name to its registry entry within a spawner session, or null. */
export function resolveNameInRegistry(
artifactDir: string,
name: string,
): NameRegistryEntry | null {
const entry = readNameRegistry(artifactDir)[name];
return entry && typeof entry.sessionFile === "string" ? entry : null;
}
function readEntries(sessionFile: string): SessionEntry[] {
const raw = readFileSync(sessionFile, "utf8");
return raw
.split("\n")
.filter((line) => line.trim())
.map((line) => JSON.parse(line) as SessionEntry);
}
/**
* Return the id of the last entry in the session file (current branch point / leaf).
*/
export function getLeafId(sessionFile: string): string | null {
const entries = readEntries(sessionFile);
return entries.length > 0 ? entries[entries.length - 1].id : null;
}
/**
* Read the canonical session id from a session file's header.
*
* pi's `--session <id>` flag resolves against this header `id` (exact match,
* then prefix), NOT the filename — so this is the value to hand back to the
* orchestrator for follow-ups.
*/
/**
* Read only the first line of a file without loading the whole thing into
* memory. Session files grow to many MB, but the header we need is always the
* first JSON line, so reading a small prefix keeps header lookups cheap — this
* is what makes scanning a large session tree fast enough to avoid blocking the
* event loop. Returns the first line (sans trailing newline), or null.
*/
function readFirstLine(path: string, maxBytes = 65536): string | null {
let fd: number | undefined;
try {
fd = openSync(path, "r");
const buf = Buffer.allocUnsafe(maxBytes);
const bytes = readSync(fd, buf, 0, maxBytes, 0);
if (bytes <= 0) return null;
const nl = buf.indexOf(0x0a); // '\n'
const end = nl === -1 || nl >= bytes ? bytes : nl;
return buf.toString("utf8", 0, end);
} catch {
return null;
} finally {
if (fd !== undefined) {
try {
closeSync(fd);
} catch {
/* ignore */
}
}
}
}
export function getSessionId(sessionFile: string): string | null {
return readHeaderId(sessionFile);
}
function readHeaderId(sessionFile: string): string | null {
const firstLine = readFirstLine(sessionFile)?.trim();
if (!firstLine) return null;
try {
const entry = JSON.parse(firstLine) as { type?: string; id?: string };
return entry.type === "session" && typeof entry.id === "string" ? entry.id : null;
} catch {
return null;
}
}
/**
* Resolve a session id (or id prefix) to a session file path by scanning every
* `*.jsonl` under `sessionsRoot` and matching the header `id`. Mirrors pi's own
* resolution order: exact match first, then prefix match. Most recently
* modified file wins on ties. Returns null when nothing matches.
*/
/**
* In-process index of session id → session file, per sessions root.
*
* Resolving a session id naively walks every `.jsonl` under the sessions tree
* and reads each header. With a few thousand sessions that is thousands of
* synchronous open/read/stat syscalls — on the extension host's single thread
* that blocks the entire terminal UI for many seconds (measured ~67s on a
* 2010-file tree). To avoid that, we build the index once per root and cache
* it; subsequent lookups are O(1). The cache is validated cheaply (a directory
* listing plus statSync-only mtime checks) on every call, so new sessions are
* picked up without re-reading unchanged headers and without ever freezing the
* UI again.
*/
interface SessionIndex {
idToFile: Map<string, { path: string; mtime: number }>;
/** file path → mtime when indexed (staleness detection). */
files: Map<string, number>;
/** top-level dir signature used to detect newly added cwd dirs. */
topSig: string;
}
const sessionIndexCache = new Map<string, SessionIndex>();
function topLevelSignature(root: string): string {
const parts: string[] = [];
let entries: import("node:fs").Dirent[];
try {
entries = readdirSync(root, { withFileTypes: true });
} catch {
return "";
}
for (const e of entries) {
const full = join(root, e.name);
if (e.isDirectory()) {
let m = 0;
try {
m = statSync(full).mtimeMs;
} catch {
/* ignore */
}
parts.push(`d:${e.name}:${m}`);
} else if (e.isFile() && e.name.endsWith(".jsonl")) {
parts.push(`f:${e.name}`);
}
}
parts.sort();
return parts.join("|");
}
/** Recursively index new/changed .jsonl files under dir into idx. */
function indexDir(dir: string, idx: SessionIndex): void {
let entries: import("node:fs").Dirent[];
try {
entries = readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
const full = join(dir, entry.name);
if (entry.isDirectory()) {
indexDir(full, idx);
} else if (entry.isFile() && entry.name.endsWith(".jsonl")) {
let mtime = 0;
try {
mtime = statSync(full).mtimeMs;
} catch {
continue;
}
const known = idx.files.get(full);
if (known !== undefined && known === mtime) continue; // unchanged
const id = readHeaderId(full); // only read headers for new/changed files
idx.files.set(full, mtime);
if (!id) continue;
const prev = idx.idToFile.get(id);
if (!prev || mtime >= prev.mtime) {
idx.idToFile.set(id, { path: full, mtime });
}
}
}
}
function getSessionIndex(sessionsRoot: string): SessionIndex {
let idx = sessionIndexCache.get(sessionsRoot);
const sig = topLevelSignature(sessionsRoot);
if (!idx) {
idx = { idToFile: new Map(), files: new Map(), topSig: sig };
sessionIndexCache.set(sessionsRoot, idx);
indexDir(sessionsRoot, idx); // first build: full scan, once per process
} else if (idx.topSig !== sig) {
idx.topSig = sig;
indexDir(sessionsRoot, idx); // a cwd dir was added/changed: incremental rescan
} else {
indexDir(sessionsRoot, idx); // cheap: stats files, reads only new/changed headers
}
return idx;
}
export function resolveSessionFileById(sessionId: string, sessionsRoot: string): string | null {
if (!sessionId || !existsSync(sessionsRoot)) return null;
const idx = getSessionIndex(sessionsRoot);
return lookupSessionIndex(idx, sessionId);
}
function lookupSessionIndex(
idx: { idToFile: Map<string, { path: string; mtime: number }> },
sessionId: string,
): string | null {
// Exact match first.
const exact = idx.idToFile.get(sessionId);
if (exact && existsSync(exact.path)) return exact.path;
// Prefix match: most recently modified wins (ids are unique in practice, so
// this is only a convenience for hand-typed short prefixes).
let best: { path: string; mtime: number } | null = null;
for (const [id, rec] of idx.idToFile) {
if (!id.startsWith(sessionId)) continue;
if (!existsSync(rec.path)) continue;
if (!best || rec.mtime > best.mtime) best = rec;
}
return best ? best.path : null;
}
/**
* Async variant used by the interactive resume path. Index building/refresh is
* synchronous I/O, which can take many seconds on a cold OS page cache with a
* few thousand sessions; running it synchronously would block the extension
* host's single thread and freeze the terminal UI. Deferring to a macrotask
* keeps the event loop responsive. The heavy work only happens on the first
* resolution per process (and incrementally thereafter); warm lookups are ~50ms.
*/
export async function resolveSessionFileByIdAsync(
sessionId: string,
sessionsRoot: string,
): Promise<string | null> {
if (!sessionId || !existsSync(sessionsRoot)) return null;
// Let the event loop breathe (and the UI repaint) before the sync scan.
await new Promise<void>((r) => setImmediate(r));
const idx = getSessionIndex(sessionsRoot);
return lookupSessionIndex(idx, sessionId);
}
/** Test hook: drop the cached session index so tests start clean. */
export function resetSessionIndexCache(): void {
sessionIndexCache.clear();
}
/**
* Return entries added after `afterLine` (1-indexed count of existing entries).
*/
/**
* Count the number of entry lines in a session file without parsing each line
* into an object. Used by the resume path, which only needs the *count* of
* pre-existing entries (so it can later slice out the new ones). Parsing every
* line of a large resumed transcript synchronously at resume time would block
* the UI; counting newlines is dramatically cheaper.
*/
export function countSessionEntryLines(sessionFile: string): number {
try {
const raw = readFileSync(sessionFile, "utf8");
// Count non-blank lines, mirroring getNewEntries' `.filter(line => line.trim())`
// but skipping the per-line JSON.parse that makes resume slow on big files.
let count = 0;
for (const line of raw.split("\n")) {
if (line.trim()) count++;
}
return count;
} catch {
return 0;
}
}
export function getNewEntries(sessionFile: string, afterLine: number): SessionEntry[] {
const raw = readFileSync(sessionFile, "utf8");
const lines = raw.split("\n").filter((line) => line.trim());
return lines.slice(afterLine).map((line) => JSON.parse(line) as SessionEntry);
}
/**
* Find the last assistant message text in a list of entries.
*
* Falls back to the `errorMessage` field when the last assistant message has
* `stopReason: "error"` and no usable text content — this happens when
* auto-retry exhausts on a provider overload / rate limit / server error, and
* without this fallback the parent would silently see a stale earlier message.
*/
export function findLastAssistantMessage(entries: SessionEntry[]): string | null {
for (let i = entries.length - 1; i >= 0; i--) {
const entry = entries[i];
if (entry.type !== "message") continue;
const msg = entry as MessageEntry;
if (msg.message.role !== "assistant") continue;
const texts = msg.message.content
.filter(
(block) =>
block.type === "text" && typeof block.text === "string" && block.text.trim() !== "",
)
.map((block) => block.text as string);
if (texts.length > 0 && texts.join("").trim()) return texts.join("\n");
const stopReason = (msg.message as { stopReason?: unknown }).stopReason;
const errorMessage = (msg.message as { errorMessage?: unknown }).errorMessage;
if (
stopReason === "error" &&
typeof errorMessage === "string" &&
errorMessage.trim() !== ""
) {
return `Subagent error: ${errorMessage.trim()}`;
}
}
return null;
}
/**
* Append a branch_summary entry to the session file.
* Returns the new entry's id.
*/
export function appendBranchSummary(
sessionFile: string,
branchPointId: string,
fromId: string | null,
summary: string,
): string {
const id = randomBytes(4).toString("hex");
const entry = {
type: "branch_summary",
id,
parentId: branchPointId,
timestamp: new Date().toISOString(),
fromId: fromId ?? branchPointId,
summary,
};
appendFileSync(sessionFile, JSON.stringify(entry) + "\n", "utf8");
return id;
}
/**
* Copy the session file to destDir for parallel worker isolation.
* Returns the path of the copy.
*/
export function copySessionFile(sessionFile: string, destDir: string): string {
const id = randomBytes(4).toString("hex");
const dest = join(destDir, `subagent-${id}.jsonl`);
copyFileSync(sessionFile, dest);
return dest;
}
/**
* Read new entries from sourceFile (after afterLine), append them to targetFile.
* Returns the appended entries.
*/
export function mergeNewEntries(
sourceFile: string,
targetFile: string,
afterLine: number,
): SessionEntry[] {
const entries = getNewEntries(sourceFile, afterLine);
for (const entry of entries) {
appendFileSync(targetFile, JSON.stringify(entry) + "\n", "utf8");
}
return entries;
}
export interface SessionStats {
model: string | null;
toolCount: number;
/** Cumulative token usage across all assistant turns. */
inputTokens: number;
outputTokens: number;
cacheReadTokens: number;
cacheWriteTokens: number;
/** Current context size: the last assistant turn's totalTokens. */
contextTokens: number;
/** Cumulative cost in USD across all assistant turns. */
cost: number;
}
/**
* Parse a completed subagent session JSONL into aggregate stats for display:
* model, tool-call count, cumulative token usage + cost, and current context
* size. Cumulative usage fields are summed across every assistant turn; the
* context size is taken from the last assistant turn's `totalTokens` (the live
* context window occupancy). Returns null if the file can't be read.
*/
export function summarizeSessionStats(sessionFile: string): SessionStats | null {
let entries: SessionEntry[];
try {
entries = readEntries(sessionFile);
} catch {
return null;
}
const stats: SessionStats = {
model: null,
toolCount: 0,
inputTokens: 0,
outputTokens: 0,
cacheReadTokens: 0,
cacheWriteTokens: 0,
contextTokens: 0,
cost: 0,
};
for (const entry of entries) {
if (entry.type === "model_change") {
const modelId = (entry as { modelId?: unknown }).modelId;
if (typeof modelId === "string" && modelId) stats.model = modelId;
continue;
}
if (entry.type !== "message") continue;
const msg = (entry as MessageEntry).message;
if (msg.role !== "assistant") continue;
const model = (msg as { model?: unknown }).model;
if (typeof model === "string" && model) stats.model = model;
for (const block of msg.content) {
if (block.type === "toolCall") stats.toolCount++;
}
const usage = (msg as { usage?: Record<string, unknown> }).usage;
if (usage && typeof usage === "object") {
const num = (v: unknown): number => (typeof v === "number" && Number.isFinite(v) ? v : 0);
stats.inputTokens += num(usage.input);
stats.outputTokens += num(usage.output);
stats.cacheReadTokens += num(usage.cacheRead);
stats.cacheWriteTokens += num(usage.cacheWrite);
const total = num(usage.totalTokens);
if (total > 0) stats.contextTokens = total;
const cost = usage.cost;
if (cost && typeof cost === "object") stats.cost += num((cost as Record<string, unknown>).total);
}
}
return stats;
}
@@ -0,0 +1,513 @@
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
export const SNAPSHOT_STALLED_AFTER_MS = 60_000;
export const DEFAULT_STATUS_LINE_LIMIT = 4;
export const MAX_STATUS_NAME_LENGTH = 72;
export const MAX_STATUS_LINE_LENGTH = 120;
const PACKAGE_ROOT = join(dirname(fileURLToPath(import.meta.url)), "../..");
const DEFAULT_STATUS_CONFIG_PATH = join(PACKAGE_ROOT, "config.json");
const STATUS_CONFIG_EXAMPLE_PATH = join(PACKAGE_ROOT, "config.json.example");
export type SubagentStatusKind = "starting" | "active" | "waiting" | "stalled" | "running";
export type SubagentStatusSource = "pi" | "claude";
export type SubagentStatusTransition = "stalled" | "recovered" | null;
export type StatusSnapshotState = "unseen" | "present" | "missing" | "invalid" | "wrong-id";
export type StatusActivityPhase = "starting" | "active" | "waiting" | "done";
export interface StatusConfig {
enabled: boolean;
lineLimit: number;
}
export type StatusObservation =
| {
snapshot: "present";
updatedAt: number;
sequence: number;
phase: StatusActivityPhase;
active?: boolean;
activeScope?: string;
activeSince?: number;
waitingSince?: number;
latestEvent?: string;
activityLabel?: string;
}
| {
snapshot: "missing" | "invalid" | "wrong-id";
snapshotError?: string;
};
export interface SubagentStatusState {
source: SubagentStatusSource;
startTimeMs: number;
firstObservationAtMs: number | null;
lastActivityAtMs: number | null;
lastActivitySequence: number | null;
localOverrideAtMs: number | null;
localOverrideSequence: number | null;
activeNow: boolean;
activeSinceMs: number | null;
activeScope: string | null;
waitingSinceMs: number | null;
phase: StatusActivityPhase | null;
latestEvent: string | null;
activityLabel: string | null;
snapshotState: StatusSnapshotState;
snapshotProblemSinceMs: number | null;
snapshotError: string | null;
currentKind: SubagentStatusKind;
}
export interface StatusSnapshot {
kind: SubagentStatusKind;
elapsedMs: number;
elapsedText: string;
activeSinceMs: number | null;
activeDurationText: string | null;
activeScope: string | null;
waitingSinceMs: number | null;
waitingDurationText: string | null;
latestEvent: string | null;
activityLabel: string | null;
snapshotState: StatusSnapshotState;
snapshotError: string | null;
snapshotProblemText: string | null;
statusLabel: string | null;
}
export interface CappedStatusLines {
visibleLines: string[];
overflow: number;
}
function invalidStatusConfig(source: string, message: string): never {
throw new Error(`Invalid subagent status config in ${source}: ${message}`);
}
function requireObject(value: unknown, source: string, fieldName: string): Record<string, unknown> {
if (value == null || typeof value !== "object" || Array.isArray(value)) {
invalidStatusConfig(source, `${fieldName} must be an object`);
}
return value as Record<string, unknown>;
}
function requireBoolean(value: unknown, source: string, fieldName: string): boolean {
if (typeof value !== "boolean") {
invalidStatusConfig(source, `${fieldName} must be a boolean`);
}
return value;
}
function rejectUnsupportedKeys(
value: Record<string, unknown>,
allowedKeys: string[],
source: string,
fieldName: string,
): void {
const unsupportedKeys = Object.keys(value).filter((key) => !allowedKeys.includes(key));
if (unsupportedKeys.length > 0) {
invalidStatusConfig(source, `${fieldName} has unsupported key(s): ${unsupportedKeys.join(", ")}`);
}
}
function truncateText(text: string, maxLength: number): string {
if (text.length <= maxLength) return text;
if (maxLength <= 1) return text.slice(0, maxLength);
return `${text.slice(0, maxLength - 1)}…`;
}
export function normalizeStatusName(name: string): string {
const collapsed = name.replace(/\s+/g, " ").trim() || "subagent";
return truncateText(collapsed, MAX_STATUS_NAME_LENGTH);
}
function boundStatusLine(line: string): string {
return truncateText(line.replace(/\s+/g, " ").trim(), MAX_STATUS_LINE_LENGTH);
}
function snapshotProblemLabel(snapshotState: StatusSnapshotState): string | null {
if (snapshotState === "wrong-id") return "wrong activity id";
return null;
}
function activityLabel(snapshot: Pick<StatusSnapshot, "activityLabel" | "activeScope">): string | null {
return snapshot.activityLabel ?? snapshot.activeScope;
}
export function parseStatusConfig(rawConfig: unknown, source = "config.json"): StatusConfig {
const config = requireObject(rawConfig, source, "root");
const status = requireObject(config.status, source, "status");
rejectUnsupportedKeys(status, ["enabled"], source, "status");
const enabled = requireBoolean(status.enabled, source, "status.enabled");
return {
enabled,
lineLimit: DEFAULT_STATUS_LINE_LIMIT,
};
}
function readStatusConfigFile(configPath: string, examplePath: string): { sourcePath: string; rawConfig: string } {
try {
return { sourcePath: configPath, rawConfig: readFileSync(configPath, "utf8") };
} catch (error) {
const errno = error as NodeJS.ErrnoException;
if (errno.code !== "ENOENT") throw error;
}
try {
return { sourcePath: examplePath, rawConfig: readFileSync(examplePath, "utf8") };
} catch (error) {
const errno = error as NodeJS.ErrnoException;
if (errno.code === "ENOENT") {
throw new Error(
`Missing subagent status config. Expected ${configPath} or ${examplePath}.`,
);
}
throw error;
}
}
export function loadStatusConfig(
configPath = DEFAULT_STATUS_CONFIG_PATH,
examplePath = STATUS_CONFIG_EXAMPLE_PATH,
): StatusConfig {
const { sourcePath, rawConfig } = readStatusConfigFile(configPath, examplePath);
let parsed: unknown;
try {
parsed = JSON.parse(rawConfig) as unknown;
} catch (error) {
const detail = error instanceof Error ? error.message : String(error);
throw new Error(`Invalid JSON in subagent config ${sourcePath}: ${detail}`);
}
return parseStatusConfig(parsed, sourcePath);
}
export function formatElapsedDuration(ms: number): string {
const totalSeconds = Math.max(0, Math.floor(ms / 1000));
if (totalSeconds < 60) return `${totalSeconds}s`;
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
if (hours > 0) return `${hours}h ${minutes}m`;
return `${minutes}m`;
}
export function createStatusState(params: {
source: SubagentStatusSource;
startTimeMs: number;
}): SubagentStatusState {
const initialKind = params.source === "claude" ? "running" : "starting";
return {
source: params.source,
startTimeMs: params.startTimeMs,
firstObservationAtMs: null,
lastActivityAtMs: null,
lastActivitySequence: null,
localOverrideAtMs: null,
localOverrideSequence: null,
activeNow: false,
activeSinceMs: null,
activeScope: null,
waitingSinceMs: null,
phase: null,
latestEvent: null,
activityLabel: null,
snapshotState: params.source === "claude" ? "unseen" : "unseen",
snapshotProblemSinceMs: null,
snapshotError: null,
currentKind: initialKind,
};
}
export function observeStatus(
state: SubagentStatusState,
observation: StatusObservation,
now: number,
): SubagentStatusState {
if (state.source === "claude") return state;
if (observation.snapshot !== "present") {
return {
...state,
firstObservationAtMs: state.firstObservationAtMs ?? now,
snapshotState: observation.snapshot,
snapshotProblemSinceMs: state.snapshotProblemSinceMs ?? now,
snapshotError: observation.snapshotError ?? null,
};
}
const updatedAt = observation.updatedAt;
const sequence = observation.sequence;
const lastActivityAtMs = state.lastActivityAtMs;
const lastActivitySequence = state.lastActivitySequence;
const olderThanLastActivity = lastActivityAtMs != null && (
updatedAt < lastActivityAtMs ||
(updatedAt === lastActivityAtMs && lastActivitySequence != null && sequence < lastActivitySequence)
);
if (olderThanLastActivity) return state;
const blockedByLocalOverride = state.localOverrideAtMs != null && (
updatedAt < state.localOverrideAtMs ||
(updatedAt === state.localOverrideAtMs && state.localOverrideSequence != null && sequence <= state.localOverrideSequence)
);
if (blockedByLocalOverride) return state;
const phase = observation.phase;
const activeNow = phase === "active" || observation.active === true;
const activeSinceMs = activeNow
? observation.activeSince ?? state.activeSinceMs ?? updatedAt
: null;
const waitingSinceMs = phase === "waiting"
? observation.waitingSince ?? state.waitingSinceMs ?? updatedAt
: null;
return {
...state,
firstObservationAtMs: state.firstObservationAtMs ?? now,
lastActivityAtMs: updatedAt,
lastActivitySequence: sequence,
activeNow,
activeSinceMs,
activeScope: activeNow ? observation.activeScope ?? null : null,
waitingSinceMs,
phase,
latestEvent: observation.latestEvent ?? null,
activityLabel: observation.activityLabel ?? null,
snapshotState: "present",
snapshotProblemSinceMs: null,
snapshotError: null,
localOverrideAtMs: null,
localOverrideSequence: null,
};
}
export function forceStatusAfterInterrupt(state: SubagentStatusState, now: number): SubagentStatusState {
if (state.source === "claude") return state;
return {
...state,
firstObservationAtMs: state.firstObservationAtMs ?? now,
lastActivityAtMs: now,
localOverrideAtMs: now,
localOverrideSequence: state.lastActivitySequence,
activeNow: false,
activeSinceMs: null,
activeScope: null,
waitingSinceMs: now,
phase: "waiting",
latestEvent: "interrupt_requested",
activityLabel: "interrupted",
snapshotState: "present",
snapshotProblemSinceMs: null,
snapshotError: null,
currentKind: "waiting",
};
}
function classifyProblemState(state: SubagentStatusState, now: number): Pick<StatusSnapshot, "kind" | "statusLabel"> {
const problemLabel = snapshotProblemLabel(state.snapshotState);
const hasValidSnapshot = state.lastActivityAtMs != null;
if (!hasValidSnapshot) {
const referenceMs = state.firstObservationAtMs ?? state.startTimeMs;
const elapsedMs = Math.max(0, now - referenceMs);
return elapsedMs >= SNAPSHOT_STALLED_AFTER_MS
? { kind: "stalled", statusLabel: problemLabel }
: { kind: "starting", statusLabel: null };
}
const problemSinceMs = state.snapshotProblemSinceMs ?? now;
const problemMs = Math.max(0, now - problemSinceMs);
if (problemMs >= SNAPSHOT_STALLED_AFTER_MS) return { kind: "stalled", statusLabel: problemLabel };
const lastHealthyKind = state.activeNow
? "active"
: state.waitingSinceMs != null || state.phase === "done"
? "waiting"
: state.currentKind === "stalled"
? "starting"
: state.currentKind;
return { kind: lastHealthyKind, statusLabel: problemLabel };
}
export function classifyStatus(state: SubagentStatusState, now: number): StatusSnapshot {
const elapsedMs = Math.max(0, now - state.startTimeMs);
const elapsedText = formatElapsedDuration(elapsedMs);
if (state.source === "claude") {
return {
kind: "running",
elapsedMs,
elapsedText,
activeSinceMs: null,
activeDurationText: null,
activeScope: null,
waitingSinceMs: null,
waitingDurationText: null,
latestEvent: null,
activityLabel: null,
snapshotState: state.snapshotState,
snapshotError: null,
snapshotProblemText: null,
statusLabel: null,
};
}
let kind: SubagentStatusKind;
let statusLabel: string | null = null;
if (state.snapshotState === "present") {
if (state.phase === "active" || state.activeNow) {
kind = "active";
} else if (state.phase === "waiting") {
kind = "waiting";
} else if (state.phase === "done") {
kind = "waiting";
statusLabel = "done";
} else {
const referenceMs = state.firstObservationAtMs ?? state.startTimeMs;
const elapsedSinceObservationMs = Math.max(0, now - referenceMs);
kind = elapsedSinceObservationMs >= SNAPSHOT_STALLED_AFTER_MS ? "stalled" : "starting";
statusLabel = null;
}
} else {
const classified = classifyProblemState(state, now);
kind = classified.kind;
statusLabel = classified.statusLabel;
}
const activeDurationText = state.activeSinceMs == null
? null
: formatElapsedDuration(now - state.activeSinceMs);
const waitingDurationText = state.waitingSinceMs == null
? null
: formatElapsedDuration(now - state.waitingSinceMs);
const snapshotProblemText = state.snapshotProblemSinceMs == null
? null
: formatElapsedDuration(now - state.snapshotProblemSinceMs);
return {
kind,
elapsedMs,
elapsedText,
activeSinceMs: state.activeSinceMs,
activeDurationText,
activeScope: state.activeScope,
waitingSinceMs: state.waitingSinceMs,
waitingDurationText,
latestEvent: state.latestEvent,
activityLabel: state.activityLabel,
snapshotState: state.snapshotState,
snapshotError: state.snapshotError,
snapshotProblemText,
statusLabel,
};
}
export function advanceStatusState(
state: SubagentStatusState,
now: number,
): {
nextState: SubagentStatusState;
snapshot: StatusSnapshot;
transition: SubagentStatusTransition;
} {
const snapshot = classifyStatus(state, now);
const transition =
state.currentKind !== "stalled" && snapshot.kind === "stalled"
? "stalled"
: state.currentKind === "stalled" && (snapshot.kind === "active" || snapshot.kind === "waiting")
? "recovered"
: null;
return {
snapshot,
transition,
nextState: {
...state,
currentKind: snapshot.kind,
},
};
}
function formatActiveDetail(snapshot: StatusSnapshot): string {
const label = activityLabel(snapshot);
if (!label) return "active";
const duration = snapshot.activeDurationText ? ` ${snapshot.activeDurationText}` : "";
return `active (${label}${duration})`;
}
function formatWaitingDetail(snapshot: StatusSnapshot): string {
const duration = snapshot.waitingDurationText ? ` ${snapshot.waitingDurationText}` : "";
return `waiting${duration}`;
}
function formatStalledDetail(snapshot: StatusSnapshot): string {
const detail = snapshot.statusLabel ? ` (${snapshot.statusLabel})` : "";
const duration = snapshot.snapshotProblemText ? ` ${snapshot.snapshotProblemText}` : "";
return `stalled${duration}${detail}`;
}
export function formatStatusLine(name: string, snapshot: StatusSnapshot): string {
const boundedName = normalizeStatusName(name);
if (snapshot.kind === "starting") {
const label = snapshot.statusLabel ? ` (${snapshot.statusLabel})` : "";
return boundStatusLine(`${boundedName} running ${snapshot.elapsedText}, starting${label}.`);
}
if (snapshot.kind === "running") {
return boundStatusLine(`${boundedName} running ${snapshot.elapsedText}.`);
}
if (snapshot.kind === "active") {
return boundStatusLine(`${boundedName} running ${snapshot.elapsedText}, ${formatActiveDetail(snapshot)}.`);
}
if (snapshot.kind === "waiting") {
const problem = snapshot.statusLabel && snapshot.statusLabel !== "done"
? ` (${snapshot.statusLabel})`
: snapshot.statusLabel === "done"
? " (done)"
: "";
return boundStatusLine(`${boundedName} running ${snapshot.elapsedText}, ${formatWaitingDetail(snapshot)}${problem}.`);
}
return boundStatusLine(`${boundedName} running ${snapshot.elapsedText}, ${formatStalledDetail(snapshot)}.`);
}
export function formatTransitionLine(
name: string,
snapshot: StatusSnapshot,
transition: Exclude<SubagentStatusTransition, null>,
): string {
const boundedName = normalizeStatusName(name);
if (transition === "recovered") {
const detail = snapshot.kind === "waiting" ? formatWaitingDetail(snapshot) : formatActiveDetail(snapshot);
return boundStatusLine(`${boundedName} running ${snapshot.elapsedText}, recovered; ${detail}.`);
}
return formatStatusLine(boundedName, snapshot);
}
export function capStatusLines(lines: string[], lineLimit: number): CappedStatusLines {
const visibleLines = lines.slice(0, lineLimit);
return {
visibleLines,
overflow: Math.max(0, lines.length - visibleLines.length),
};
}
export function formatStatusAggregate(lines: string[], lineLimit: number): string {
const { visibleLines, overflow } = capStatusLines(lines, lineLimit);
const bulletLines = visibleLines.map((line) => `• ${line}`);
if (overflow > 0) bulletLines.push(`• +${overflow} more running.`);
return `Subagent status:\n${bulletLines.join("\n")}`;
}
@@ -0,0 +1,399 @@
/**
* Extension loaded into sub-agents.
* - Shows agent identity + available tools as a styled widget above the editor (toggle with Ctrl+Alt+O)
* - Provides an `ask_question` tool for asking the parent orchestrator a question
*
* Subagents do NOT self-terminate via a tool. Auto-exit agents shut down
* automatically when their agent loop ends (see the `agent_end` handler);
* interactive agents end when the human exits the pane.
*
* `ask_question` keeps the session OPEN: it writes a `${sessionFile}.ask`
* signal the parent's watcher picks up, parks the session in a "waiting" state
* (auto-exit is suppressed for that turn via `awaitingAnswer`), and the parent
* replies with subagent_message — which lands as the subagent's next turn.
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { Box, Text } from "@earendil-works/pi-tui";
import { Type } from "typebox";
import { writeFileSync } from "node:fs";
import { createSubagentActivityRecorder } from "./activity.ts";
export function shouldMarkUserTookOver(agentStarted: boolean): boolean {
return agentStarted;
}
/**
* Number of child subagents this session itself still has in flight.
*
* When this extension is loaded inside a subagent that can spawn its own
* children (e.g. a worker delegating to scout/researcher), `index.ts` runs in
* the same process and publishes a live count through a shared process-global
* symbol. A subagent that spawns children and then writes a "waiting for
* results" message would otherwise auto-exit the instant that turn ends —
* killing the session before its children report back. Reading this count lets
* `agent_end` keep the session open until every child has finished and its
* result has been delivered.
*
* Returns 0 when the spawning tools aren't loaded (scout/researcher, or a
* standalone session), so those agents auto-exit exactly as before.
*/
export function runningChildrenCount(): number {
const fn = (globalThis as any)[Symbol.for("pi-subagents/running-children-count")];
if (typeof fn !== "function") return 0;
try {
const n = fn();
return typeof n === "number" && n > 0 ? n : 0;
} catch {
return 0;
}
}
export function shouldAutoExitOnAgentEnd(
_userTookOver: boolean,
messages: any[] | undefined,
): boolean {
// Manual input should not strand an auto-exit subagent. If the latest agent
// turn completed normally, close the session. Escape/abort still leaves it
// open for inspection or another prompt.
//
// stopReason: "error" (e.g. exhausted retries on a provider overload) also
// returns true — we want to shut down so the parent is woken up — but we
// pair this with findLatestAssistantError() so the parent learns it was an
// error, not a clean completion.
if (messages) {
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i];
if (msg?.role === "assistant") {
return msg.stopReason !== "aborted";
}
}
}
return true;
}
export interface SubagentErrorInfo {
errorMessage: string;
stopReason: "error";
}
/**
* If the last assistant message in the turn ended with `stopReason: "error"`
* (typically auto-retry exhausted on an overload / rate limit / server error),
* return its error info so the parent orchestrator can surface a clear
* failure instead of silently treating the run as completed.
*
* Returns `null` when the latest assistant turn completed normally or was
* aborted by the user (handled separately by shouldAutoExitOnAgentEnd).
*/
export function findLatestAssistantError(
messages: any[] | undefined,
): SubagentErrorInfo | null {
if (!messages) return null;
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i];
if (msg?.role !== "assistant") continue;
if (msg.stopReason !== "error") return null;
const raw = typeof msg.errorMessage === "string" ? msg.errorMessage.trim() : "";
return {
errorMessage: raw || "Subagent agent loop ended with stopReason=error (no errorMessage field).",
stopReason: "error",
};
}
return null;
}
export function parseDeniedTools(rawValue: string | undefined): string[] {
return (rawValue ?? "")
.split(",")
.map((value) => value.trim())
.filter(Boolean);
}
export default function (pi: ExtensionAPI) {
let toolNames: string[] = [];
let denied: string[] = [];
let expanded = false;
// Read subagent identity from env vars (set by parent orchestrator)
const subagentName = process.env.PI_SUBAGENT_NAME ?? "";
const subagentAgent = process.env.PI_SUBAGENT_AGENT ?? "";
const deniedToolsValue = process.env.PI_DENY_TOOLS;
const autoExit = process.env.PI_SUBAGENT_AUTO_EXIT === "1";
const recorder = createSubagentActivityRecorder({
runningChildId: process.env.PI_SUBAGENT_ID,
activityFile: process.env.PI_SUBAGENT_ACTIVITY_FILE,
});
function renderWidget(ctx: { ui: { setWidget: Function } }, _theme: any) {
ctx.ui.setWidget(
"subagent-tools",
(_tui: any, theme: any) => {
const box = new Box(1, 0, (text: string) => theme.bg("toolSuccessBg", text));
const label = subagentAgent || subagentName;
const agentTag = label ? theme.bold(theme.fg("accent", `[${label}]`)) : "";
if (expanded) {
// Expanded: full tool list + denied
const countInfo = theme.fg("dim", ` — ${toolNames.length} available`);
const hint = theme.fg("muted", " (Ctrl+Alt+O to collapse)");
const toolList = toolNames
.map((name: string) => theme.fg("dim", name))
.join(theme.fg("muted", ", "));
let deniedLine = "";
if (denied.length > 0) {
const deniedList = denied
.map((name: string) => theme.fg("error", name))
.join(theme.fg("muted", ", "));
deniedLine = "\n" + theme.fg("muted", "denied: ") + deniedList;
}
const content = new Text(
`${agentTag}${countInfo}${hint}\n${toolList}${deniedLine}`,
0,
0,
);
box.addChild(content);
} else {
// Collapsed: one-line summary
const countInfo = theme.fg("dim", ` — ${toolNames.length} tools`);
const deniedInfo =
denied.length > 0
? theme.fg("dim", " · ") + theme.fg("error", `${denied.length} denied`)
: "";
const hint = theme.fg("muted", " (Ctrl+Alt+O to expand)");
const content = new Text(`${agentTag}${countInfo}${deniedInfo}${hint}`, 0, 0);
box.addChild(content);
}
return box;
},
{ placement: "aboveEditor" },
);
}
let userTookOver = false;
let agentStarted = false;
// Set when ask_question is called; suppresses auto-exit so the session stays
// open while it waits for the orchestrator's reply. Cleared when the reply
// lands — on `input` (covers a reply steered into the current run) and on
// `agent_start` (covers a reply that starts a fresh turn after parking).
let awaitingAnswer = false;
// Show widget + status bar on session start
pi.on("session_start", (_event, ctx) => {
recorder.sessionStart();
const tools = pi.getAllTools();
toolNames = tools.map((t) => t.name).sort();
denied = parseDeniedTools(deniedToolsValue);
renderWidget(ctx, null);
});
pi.on("input", () => {
recorder.input();
// A submitted message is the orchestrator's (or a human's) reply — the
// pending ask_question has been answered, however it was delivered. Clear
// here, not only on agent_start, because a reply steered in *mid-run* is
// absorbed into the current run (pi's `steer` behavior injects it before
// the next LLM call): no new agent_start fires, so without this the flag
// would stay set and agent_end would park the session as `waiting` even
// though the answer already arrived and was consumed. (The `input` event
// fires for mid-run steers because prompt() emits it before queueing.)
awaitingAnswer = false;
// Ignore the initial task message that starts an autonomous subagent.
// Only inputs after the first agent run has started count as user takeover.
if (!shouldMarkUserTookOver(agentStarted)) return;
userTookOver = true;
});
pi.on("before_agent_start", () => {
recorder.beforeAgentStart();
});
pi.on("agent_start", () => {
agentStarted = true;
// A new turn is starting — any pending ask_question has now been answered
// (or superseded), so let auto-exit resume normally when this turn ends.
awaitingAnswer = false;
recorder.agentStart();
});
pi.on("agent_end", (event, ctx) => {
const messages = (event as any).messages as any[] | undefined;
// Never shut down while this session still has work in flight:
// - awaitingAnswer: an ask_question is pending the orchestrator's reply.
// - runningChildrenCount(): this subagent spawned its own children and is
// waiting for their results (delivered as steered turns). Exiting now
// would strand those children and drop their results.
// In both cases the session parks as `waiting` and resumes when the next
// turn lands.
const hasPendingChildren = runningChildrenCount() > 0;
const shouldExit =
!awaitingAnswer &&
!hasPendingChildren &&
autoExit &&
shouldAutoExitOnAgentEnd(userTookOver, messages);
if (shouldExit) {
// Surface stopReason: "error" turns (auto-retry exhausted, provider
// overload, etc.) to the parent via the .exit sidecar so the watcher
// can report a clear failure with the underlying error message.
// Without this the parent would only see exit code 0 and a stale
// assistant message, mistaking the crash for a successful completion.
const errorInfo = findLatestAssistantError(messages);
const sessionFile = process.env.PI_SUBAGENT_SESSION;
if (errorInfo && sessionFile) {
try {
writeFileSync(
`${sessionFile}.exit`,
JSON.stringify({
type: "error",
errorMessage: errorInfo.errorMessage,
stopReason: errorInfo.stopReason,
}),
);
} catch {
// Best effort — even without the sidecar, watcher's session-file
// fallback can still recover the errorMessage.
}
}
recorder.agentEndDone();
ctx.shutdown();
return;
}
recorder.agentEndWaiting();
if (autoExit) {
// Reset any recorded manual input marker. Auto-exit is decided by whether
// the latest agent turn completed normally, not by who initiated it.
userTookOver = false;
}
});
pi.on("turn_start", (event) => {
recorder.turnStart((event as any).turnIndex);
});
pi.on("turn_end", (event) => {
recorder.turnEnd((event as any).turnIndex);
});
pi.on("before_provider_request", () => {
recorder.beforeProviderRequest();
});
pi.on("after_provider_response", () => {
recorder.afterProviderResponse();
});
pi.on("message_update", (event) => {
recorder.messageUpdate((event as any).assistantMessageEvent?.type);
});
pi.on("tool_execution_start", (event) => {
recorder.toolExecutionStart((event as any).toolCallId, (event as any).toolName);
});
pi.on("tool_call", (event) => {
recorder.toolCall((event as any).toolCallId, (event as any).toolName);
});
pi.on("tool_execution_update", (event) => {
recorder.toolExecutionUpdate((event as any).toolCallId, (event as any).toolName);
});
pi.on("tool_result", (event) => {
recorder.toolResult((event as any).toolCallId, (event as any).toolName);
});
pi.on("tool_execution_end", (event) => {
recorder.toolExecutionEnd((event as any).toolCallId, (event as any).toolName);
});
pi.on("session_shutdown", (event) => {
recorder.sessionShutdown((event as any).reason);
});
// Toggle expand/collapse with Ctrl+Alt+O
pi.registerShortcut("ctrl+alt+o", {
description: "Toggle subagent tools widget",
handler: (ctx) => {
expanded = !expanded;
renderWidget(ctx, null);
},
});
pi.registerTool({
name: "ask_question",
label: "ask_question",
description:
"Ask the orchestrator (the parent agent that spawned you) a single question and pause until they reply. " +
"Use this when requirements are ambiguous, a decision would materially affect your work, you're blocked, " +
"or you need information or confirmation only the orchestrator has. Prefer asking over guessing. " +
"Your session stays open while you wait — the answer arrives as your next message, then you continue. " +
"Ask exactly one question per call; make separate calls for unrelated questions.",
promptSnippet:
"Use this tool to ask the orchestrator one clarifying, missing-requirement, preference, or decision question before continuing — instead of guessing.",
promptGuidelines: [
"Ask exactly one question per tool call.",
"If you need answers to multiple things, make separate ask_question calls instead of bundling them.",
"Prefer this tool over guessing when requirements, preferences, or implementation choices are unclear.",
"Use it when multiple valid paths exist and the right one depends on the orchestrator's intent.",
"Give enough context in the question that the orchestrator can answer without re-reading your whole task.",
"After asking, stop and wait — the reply will arrive as your next message.",
],
parameters: Type.Object({
question: Type.String({
description:
"The single freeform question to ask the orchestrator. Include enough context to answer it directly.",
}),
}),
async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
const sessionFile = process.env.PI_SUBAGENT_SESSION;
if (!sessionFile) {
throw new Error(
"ask_question is only available in subagent contexts. " +
"PI_SUBAGENT_SESSION environment variable is not set.",
);
}
// Keep the session open: suppress auto-exit for this turn and park in the
// "waiting" phase. The parent's watcher picks up the `.ask` signal and
// notifies the orchestrator, who replies via subagent_message.
awaitingAnswer = true;
recorder.askQuestion();
const askData = {
name: process.env.PI_SUBAGENT_NAME ?? "subagent",
agent: process.env.PI_SUBAGENT_AGENT ?? "",
question: params.question,
};
writeFileSync(`${sessionFile}.ask`, JSON.stringify(askData));
return {
content: [
{
type: "text",
text:
"Question sent to the orchestrator. Stop here and wait — do not continue working or " +
"assume an answer. Their reply will arrive as your next message.",
},
],
details: { question: params.question },
};
},
renderCall(args, theme) {
const text =
theme.fg("toolTitle", theme.bold("ask_question ")) +
theme.fg("muted", String((args as any).question ?? ""));
return new Text(text, 0, 0);
},
});
}
@@ -0,0 +1,224 @@
/**
* Backend-neutral terminal surface facade.
*
* Herdr is selected when Pi is running in a Herdr-managed pane, Orca when Pi
* is running in an Orca-managed terminal. tmux remains the fallback when Pi is
* running inside tmux. Selection is automatic so a process uses one backend
* consistently for its whole lifetime.
*/
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import * as herdr from "./herdr.ts";
import * as orca from "./orca.ts";
import * as tmux from "./tmux.ts";
export type MuxBackend = "herdr" | "orca" | "tmux";
type Direction = "left" | "right" | "up" | "down";
type SurfaceBackend = {
createSurface(name: string): string;
createSurfaceSplit(name: string, direction: Direction, fromSurface?: string): string;
sendCommand(surface: string, command: string): void;
readScreen(surface: string, lines?: number): string;
readScreenAsync(surface: string, lines?: number): Promise<string>;
closeSurface(surface: string): void;
};
export function selectMuxBackend(availability: {
herdr: boolean;
orca: boolean;
tmux: boolean;
}): MuxBackend | null {
if (availability.herdr) return "herdr";
if (availability.orca) return "orca";
if (availability.tmux) return "tmux";
return null;
}
export function getMuxBackend(): MuxBackend | null {
return selectMuxBackend({
herdr: herdr.isAvailable(),
orca: orca.isAvailable(),
tmux: tmux.isAvailable(),
});
}
function getBackend(): SurfaceBackend {
const backend = getMuxBackend();
if (backend === "herdr") return herdr;
if (backend === "orca") return orca;
if (backend === "tmux") return tmux;
throw new Error(`No supported terminal multiplexer is available. ${muxSetupHint()}`);
}
export function isMuxAvailable(): boolean {
return getMuxBackend() !== null;
}
export function muxSetupHint(): string {
if (process.env.HERDR_ENV === "1") return herdr.setupHint();
if (process.env.ORCA_TERMINAL_HANDLE) return orca.setupHint();
if (process.env.TMUX) return tmux.setupHint();
return "Start pi inside Herdr, Orca, or tmux.";
}
export function createSurface(name: string): string {
return getBackend().createSurface(name);
}
export function createSurfaceSplit(
name: string,
direction: Direction,
fromSurface?: string,
): string {
return getBackend().createSurfaceSplit(name, direction, fromSurface);
}
export function sendCommand(surface: string, command: string): void {
getBackend().sendCommand(surface, command);
}
export function readScreen(surface: string, lines = 50): string {
return getBackend().readScreen(surface, lines);
}
export function readScreenAsync(surface: string, lines = 50): Promise<string> {
return getBackend().readScreenAsync(surface, lines);
}
export function closeSurface(surface: string): void {
getBackend().closeSurface(surface);
}
export function shellEscape(value: string): string {
return "'" + value.replace(/'/g, "'\\''") + "'";
}
export function wrapWithCompletionFile(command: string, completionFile: string): string {
return (
`${command}; exit_code=$?; ` +
`printf '%s\\n' "$exit_code" > ${shellEscape(completionFile)}; ` +
`echo "__SUBAGENT_DONE_\${exit_code}__"`
);
}
export function sendLongCommand(
surface: string,
command: string,
options?: { scriptPath?: string; scriptPreamble?: string },
): string {
const scriptPath =
options?.scriptPath ??
join(
tmpdir(),
"pi-subagent-scripts",
`cmd-${Date.now()}-${Math.random().toString(16).slice(2, 8)}.sh`,
);
mkdirSync(dirname(scriptPath), { recursive: true });
const scriptParts = ["#!/bin/bash"];
if (options?.scriptPreamble) scriptParts.push(options.scriptPreamble.trimEnd());
scriptParts.push(command);
writeFileSync(scriptPath, scriptParts.join("\n") + "\n", { mode: 0o755 });
sendCommand(surface, `bash ${shellEscape(scriptPath)}`);
return scriptPath;
}
export interface PollResult {
reason: "done" | "sentinel" | "error";
exitCode: number;
errorMessage?: string;
}
function interpretExitSidecar(data: unknown): PollResult {
const payload = data as { type?: unknown; errorMessage?: unknown } | null;
if (payload?.type === "error") {
const errorMessage =
typeof payload.errorMessage === "string" && payload.errorMessage.trim() !== ""
? payload.errorMessage
: "Subagent exited with stopReason=error (no errorMessage in sidecar).";
return { reason: "error", exitCode: 1, errorMessage };
}
return { reason: "done", exitCode: 0 };
}
function readCompletionFile(path: string): PollResult | null {
if (!existsSync(path)) return null;
const raw = readFileSync(path, "utf8").trim();
const exitCode = Number.parseInt(raw, 10);
if (!/^\d+$/.test(raw) || !Number.isSafeInteger(exitCode)) return null;
rmSync(path, { force: true });
return { reason: "sentinel", exitCode };
}
export const __pollForExitTest__ = { interpretExitSidecar, readCompletionFile };
export async function pollForExit(
surface: string,
signal: AbortSignal,
options: {
interval: number;
sessionFile?: string;
sentinelFile?: string;
completionFile?: string;
onTick?: (elapsed: number) => void;
},
): Promise<PollResult> {
const start = Date.now();
for (;;) {
if (signal.aborted) throw new Error("Aborted while waiting for subagent to finish");
if (options.sessionFile) {
try {
const exitFile = `${options.sessionFile}.exit`;
if (existsSync(exitFile)) {
const result = interpretExitSidecar(JSON.parse(readFileSync(exitFile, "utf8")));
rmSync(exitFile, { force: true });
return result;
}
} catch {}
}
if (options.sentinelFile) {
try {
if (existsSync(options.sentinelFile)) {
return { reason: "sentinel", exitCode: 0 };
}
} catch {}
}
if (options.completionFile) {
try {
const result = readCompletionFile(options.completionFile);
if (result) return result;
} catch {}
}
try {
const screen = await readScreenAsync(surface, 5);
const match = screen.match(/__SUBAGENT_DONE_(\d+)__/);
if (match) return { reason: "sentinel", exitCode: Number.parseInt(match[1], 10) };
} catch {
// Surface reads are best-effort. Filesystem completion remains authoritative.
}
const elapsed = Math.floor((Date.now() - start) / 1000);
options.onTick?.(elapsed);
await new Promise<void>((resolve, reject) => {
if (signal.aborted) return reject(new Error("Aborted"));
const timer = setTimeout(() => {
signal.removeEventListener("abort", onAbort);
resolve();
}, options.interval);
function onAbort() {
clearTimeout(timer);
reject(new Error("Aborted"));
}
signal.addEventListener("abort", onAbort, { once: true });
});
}
}
@@ -0,0 +1,119 @@
/**
* tmux surface backend.
*
* This module only implements tmux-specific terminal operations. Backend
* selection, command scripts, and exit polling live in surface.ts.
*/
import { execFile, execFileSync } from "node:child_process";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
const commandAvailability = new Map<string, boolean>();
function hasCommand(command: string): boolean {
const cached = commandAvailability.get(command);
if (cached !== undefined) return cached;
let available = false;
try {
execFileSync("sh", ["-c", `command -v ${command}`], { stdio: "ignore" });
available = true;
} catch {
available = false;
}
commandAvailability.set(command, available);
return available;
}
export function isAvailable(): boolean {
return !!process.env.TMUX && hasCommand("tmux");
}
export function setupHint(): string {
return "Start pi inside tmux (`tmux new -A -s pi 'pi'`).";
}
function requireTmux(): void {
if (!isAvailable()) {
throw new Error(`tmux is required for this surface. ${setupHint()}`);
}
}
/** tmux layout used to keep sibling panes evenly sized. */
const SUBAGENT_TMUX_LAYOUT = "even-horizontal";
let rebalanceTimer: ReturnType<typeof setTimeout> | null = null;
function rebalanceSurfaces(hintPane?: string): void {
const target = process.env.TMUX_PANE ?? hintPane;
if (!target) return;
if (rebalanceTimer) clearTimeout(rebalanceTimer);
rebalanceTimer = setTimeout(() => {
rebalanceTimer = null;
try {
execFileSync("tmux", ["select-layout", "-t", target, SUBAGENT_TMUX_LAYOUT], {
encoding: "utf8",
});
} catch {
// Pane/window may be gone; balancing is best-effort.
}
}, 120);
}
export function createSurface(name: string): string {
return createSurfaceSplit(name, "right", process.env.TMUX_PANE);
}
export function createSurfaceSplit(
name: string,
direction: "left" | "right" | "up" | "down",
fromSurface?: string,
): string {
void name;
requireTmux();
const args = ["split-window", "-d"];
args.push(direction === "left" || direction === "right" ? "-h" : "-v");
if (direction === "left" || direction === "up") args.push("-b");
if (fromSurface) args.push("-t", fromSurface);
args.push("-P", "-F", "#{pane_id}");
const pane = execFileSync("tmux", args, { encoding: "utf8" }).trim();
if (!pane.startsWith("%")) {
throw new Error(`Unexpected tmux split-window output: ${pane}`);
}
rebalanceSurfaces(pane);
return pane;
}
export function sendCommand(surface: string, command: string): void {
requireTmux();
execFileSync("tmux", ["send-keys", "-t", surface, "-l", command], { encoding: "utf8" });
execFileSync("tmux", ["send-keys", "-t", surface, "Enter"], { encoding: "utf8" });
}
export function readScreen(surface: string, lines = 50): string {
requireTmux();
return execFileSync(
"tmux",
["capture-pane", "-p", "-t", surface, "-S", `-${Math.max(1, lines)}`],
{ encoding: "utf8" },
);
}
export async function readScreenAsync(surface: string, lines = 50): Promise<string> {
requireTmux();
const { stdout } = await execFileAsync(
"tmux",
["capture-pane", "-p", "-t", surface, "-S", `-${Math.max(1, lines)}`],
{ encoding: "utf8" },
);
return stdout;
}
export function closeSurface(surface: string): void {
requireTmux();
execFileSync("tmux", ["kill-pane", "-t", surface], { encoding: "utf8" });
rebalanceSurfaces();
}
@@ -0,0 +1,63 @@
/**
* Safe bash extension for the worker subagent.
* Wraps the built-in bash tool with dangerous command blocking.
*
* Loaded into a child pi process via `--extension` when an agent's `tools`
* frontmatter lists `safe_bash`. See CUSTOM_TOOL_EXTENSIONS in ../index.ts.
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { createBashTool } from "@earendil-works/pi-coding-agent";
import { Type } from "typebox";
const DANGEROUS_PATTERNS = [
/\brm\s+(-[a-zA-Z]*f[a-zA-Z]*\s+)?(-[a-zA-Z]*r[a-zA-Z]*\s+)?(\/|~\/?\s|~\/?\b)/,
/\brm\s+(-[a-zA-Z]*r[a-zA-Z]*\s+)?(-[a-zA-Z]*f[a-zA-Z]*\s+)?(\/|~\/?\s|~\/?\b)/,
/\bsudo\b/,
/\bmkfs\b/,
/\bdd\s+if=/,
/:\(\)\s*\{\s*:\|:&\s*\}\s*;:/,
/>\s*\/dev\/[sh]d[a-z]/,
/\bchmod\s+(-[a-zA-Z]+\s+)?777\s+\//,
/\bchown\s+(-[a-zA-Z]+\s+)?root/,
/\bcurl\s.*\|\s*(ba)?sh/,
/\bwget\s.*\|\s*(ba)?sh/,
/\bshutdown\b/,
/\breboot\b/,
/\binit\s+0\b/,
/\bkill\s+-9\s+1\b/,
/\bkillall\b/,
];
function isDangerous(command: string): string | null {
const normalized = command.replace(/\\\n/g, " ");
for (const pattern of DANGEROUS_PATTERNS) {
if (pattern.test(normalized)) {
return `Command blocked by safe_bash: matches dangerous pattern ${pattern}`;
}
}
return null;
}
export default function (pi: ExtensionAPI) {
const bashTool = createBashTool(process.cwd());
pi.registerTool({
name: "safe_bash",
label: "Safe Bash",
description:
"Execute a bash command. Blocks dangerous commands (rm -rf /, sudo, mkfs, etc.).",
parameters: Type.Object({
command: Type.String({ description: "Bash command to execute" }),
timeout: Type.Optional(
Type.Number({ description: "Timeout in seconds (optional)" }),
),
}),
async execute(toolCallId, params, signal, onUpdate, ctx) {
const danger = isDangerous(params.command);
if (danger) {
throw new Error(danger);
}
return bashTool.execute(toolCallId, params, signal, onUpdate);
},
});
}
@@ -0,0 +1,13 @@
---
name: test-echo
description: Integration test agent — completes simple file-writing tasks
model: anthropic/claude-haiku-4-5
tools: read, bash, write, edit
spawning: false
auto-exit: true
disable-model-invocation: true
---
You are a test agent. Complete the task given to you immediately. Be direct and concise.
When asked to write content to a file, do it right away using the bash tool.
Do not ask questions. Do not explain. Just execute the task.
@@ -0,0 +1,13 @@
---
name: test-fork
description: Integration test agent — fork-mode child that inherits the parent session
model: anthropic/claude-haiku-4-5
tools: read, bash
session-mode: fork
auto-exit: true
disable-model-invocation: true
---
You are a test agent. Complete the task given to you immediately. Be direct and concise.
When asked to write content to a file, do it right away using the bash tool.
Do not ask questions. Do not explain. Just execute the task.
@@ -0,0 +1,11 @@
---
name: test-ping
description: Integration test agent — calls caller_ping instead of completing task
model: anthropic/claude-haiku-4-5
tools: read, bash
spawning: false
disable-model-invocation: true
---
You are a test agent. When given ANY task, you must call the caller_ping tool with the message set to "PING: " followed by the task text you received.
Do NOT complete the task yourself. Do NOT use any other tools. ONLY call caller_ping.
@@ -0,0 +1,402 @@
/**
* Integration test harness for pi-interactive-subagents.
*
* Provides utilities to:
* - Detect the active terminal-surface backend
* - Create isolated test environments with test agent definitions
* - Start real pi sessions in Herdr tabs or tmux panes
* - Poll for file creation and screen output
* - Clean up panes and temp files after tests
*/
import { execFileSync } from "node:child_process";
import {
mkdtempSync,
mkdirSync,
cpSync,
readdirSync,
rmSync,
existsSync,
readFileSync,
unlinkSync,
writeFileSync,
} from "node:fs";
import { join, resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { tmpdir } from "node:os";
import {
getMuxBackend,
createSurface,
createSurfaceSplit,
sendCommand,
sendLongCommand,
readScreen,
readScreenAsync,
closeSurface,
pollForExit,
shellEscape,
wrapWithCompletionFile,
type MuxBackend,
} from "../../pi-extension/subagents/surface.ts";
// Re-export backend-neutral surface primitives for tests
export {
createSurface,
createSurfaceSplit,
sendCommand,
sendLongCommand,
readScreen,
readScreenAsync,
closeSurface,
pollForExit,
shellEscape,
wrapWithCompletionFile,
};
// ── Paths ──
const HARNESS_DIR = dirname(fileURLToPath(import.meta.url));
const PROJECT_ROOT = resolve(HARNESS_DIR, "../..");
const TEST_AGENTS_SRC = join(HARNESS_DIR, "agents");
/**
* Absolute path to the extension source in the working tree.
*
* Integration tests must exercise the code on the current branch — NOT the
* version installed as a pi-package under `~/.pi/agent/git/...` or the project
* mirror under `.pi/git/...`, which stays pinned to the last released tag.
*
* We force-load this file via `pi -ne -e <path>` in startPi() below so local
* edits are always the code under test, regardless of what pi-packages are
* installed on the host.
*/
const EXTENSION_SOURCE = join(PROJECT_ROOT, "pi-extension", "subagents", "index.ts");
// ── Configuration ──
/**
* Model used for integration tests. Override with PI_TEST_MODEL env var.
*
* The default must be a provider that exists under `pi -ne`: startPi() launches
* with extensions disabled, so extension-registered providers (and therefore
* the parent session's active model) may not resolve in the child.
*/
export const TEST_MODEL = process.env.PI_TEST_MODEL ?? "openai-codex/gpt-5.6-luna";
/** Per-test timeout in ms. Override with PI_TEST_TIMEOUT env var. */
export const PI_TIMEOUT = Number(process.env.PI_TEST_TIMEOUT ?? "120000");
// ── Backend detection ──
/** Return the one backend selected automatically for this process. */
export function getAvailableBackends(): MuxBackend[] {
const backend = getMuxBackend();
return backend ? [backend] : [];
}
function parseHerdrJson(command: string, args: string[]): any {
const output = execFileSync(command, args, { encoding: "utf8" });
return JSON.parse(output);
}
function orcaCli(): string {
return process.env.ORCA_CLI_COMMAND?.trim() || "orca";
}
function parseOrcaJson(args: string[]): any {
const output = execFileSync(orcaCli(), args, { encoding: "utf8" });
const response = JSON.parse(output);
if (response.ok === false) {
throw new Error(`Orca ${args.slice(0, 2).join(" ")} failed: ${response.error?.message}`);
}
return response;
}
/** Depth-first search of an Orca visual-layout pane tree for the active terminal. */
function findActiveOrcaTerminal(node: any): string | null {
if (!node || typeof node !== "object") return null;
if (node.type === "terminal") return node.active ? node.handle ?? null : null;
for (const child of Object.values(node)) {
if (Array.isArray(child)) {
for (const item of child) {
const found = findActiveOrcaTerminal(item);
if (found) return found;
}
} else if (child && typeof child === "object") {
const found = findActiveOrcaTerminal(child);
if (found) return found;
}
}
return null;
}
export function focusSurface(surface: string): void {
if (getMuxBackend() === "orca") {
parseOrcaJson(["terminal", "switch", "--terminal", surface, "--json"]);
return;
}
if (getMuxBackend() === "herdr") {
const pane = parseHerdrJson("herdr", ["pane", "get", surface]);
const tabId = pane.result?.pane?.tab_id;
if (!tabId) throw new Error(`Herdr pane ${surface} has no tab id`);
execFileSync("herdr", ["tab", "focus", tabId], { encoding: "utf8" });
return;
}
execFileSync("tmux", ["select-pane", "-t", surface], { encoding: "utf8" });
}
export function getFocusedSurface(): string | null {
try {
if (getMuxBackend() === "orca") {
const response = parseOrcaJson(["terminal", "list", "--include-visual-layouts", "--json"]);
const layouts = response.result?.visualLayouts ?? [];
for (const layout of layouts) {
const root = layout.root;
if (!root) continue;
const activeTab = root.tabs?.find((tab: any) => tab.tabId === root.activeTabId);
const handle = findActiveOrcaTerminal(activeTab?.panes);
if (handle) return handle;
}
return null;
}
if (getMuxBackend() === "herdr") {
const workspace = process.env.HERDR_WORKSPACE_ID;
if (!workspace) return null;
const response = parseHerdrJson("herdr", ["pane", "list", "--workspace", workspace]);
return response.result?.panes?.find((pane: any) => pane.focused)?.pane_id ?? null;
}
const panes = execFileSync("tmux", ["list-panes", "-F", "#{pane_id} #{pane_active}"], {
encoding: "utf8",
});
const activeLine = panes.split("\n").find((line) => line.endsWith(" 1"));
return activeLine?.split(" ")[0] ?? null;
} catch {
return null;
}
}
export async function waitForFocusedSurface(
surface: string,
timeout: number = PI_TIMEOUT,
): Promise<void> {
const start = Date.now();
while (Date.now() - start < timeout) {
if (getFocusedSurface() === surface) return;
await sleep(200);
}
throw new Error(
`Timeout (${timeout}ms) waiting for focused terminal surface ${surface}; ` +
`current focus is ${getFocusedSurface() ?? "unknown"}`,
);
}
// ── Test environment ──
export interface TestEnv {
/** Temp directory serving as the test project root */
dir: string;
/** Panes created during the test (cleaned up automatically) */
surfaces: string[];
/** Temp files to clean up */
tempFiles: string[];
}
/**
* Create an isolated test environment with test agent definitions.
* The temp dir has `.pi/agents/` containing copies of all test agents.
*/
export function createTestEnv(): TestEnv {
const dir = mkdtempSync(join(tmpdir(), "pi-integ-"));
const agentsDir = join(dir, ".pi", "agents");
mkdirSync(agentsDir, { recursive: true });
// Copy test agent definitions into the project-local agents dir
if (existsSync(TEST_AGENTS_SRC)) {
for (const file of readdirSync(TEST_AGENTS_SRC)) {
if (file.endsWith(".md")) {
const destination = join(agentsDir, file);
cpSync(join(TEST_AGENTS_SRC, file), destination);
const definition = readFileSync(destination, "utf8").replace(
/^model:\s*.+$/m,
`model: ${TEST_MODEL}`,
);
writeFileSync(destination, definition);
}
}
}
return { dir, surfaces: [], tempFiles: [] };
}
/**
* Clean up all resources created during the test.
*/
export function cleanupTestEnv(env: TestEnv): void {
for (const surface of env.surfaces) {
try {
closeSurface(surface);
} catch {}
}
for (const file of env.tempFiles) {
try {
unlinkSync(file);
} catch {}
}
try {
rmSync(env.dir, { recursive: true, force: true });
} catch {}
}
/**
* Create a surface and register it for automatic cleanup.
*/
export function createTrackedSurface(env: TestEnv, name: string): string {
const surface = createSurface(name);
env.surfaces.push(surface);
return surface;
}
export function createTrackedSurfaceSplit(
env: TestEnv,
name: string,
direction: "left" | "right" | "up" | "down",
fromSurface?: string,
): string {
const surface = createSurfaceSplit(name, direction, fromSurface);
env.surfaces.push(surface);
return surface;
}
/**
* Remove a surface from tracking (after manual close).
*/
export function untrackSurface(env: TestEnv, surface: string): void {
env.surfaces = env.surfaces.filter((s) => s !== surface);
}
// ── Pi session management ──
/**
* Start a pi session in a mux surface with the subagents extension loaded.
* Returns immediately — the pi process runs asynchronously in the surface.
*
* The command ends with a sentinel so we can detect when pi exits:
* `pi ...; echo '__TEST_DONE_'$?'__'`
*/
export function startPi(
surface: string,
testDir: string,
task: string,
opts?: { model?: string; extraArgs?: string },
): void {
const model = opts?.model ?? TEST_MODEL;
const extra = opts?.extraArgs ?? "";
// Force pi to load the working-tree extension (not an installed pi-package
// snapshot). `-ne` disables extension auto-discovery, `-e <path>` loads the
// current branch's source directly. Without this, the tests silently run
// against whatever version is checked out under `~/.pi/agent/git/...`.
const cmd = [
`cd ${shellEscape(testDir)} &&`,
`pi`,
`-ne`,
`-e ${shellEscape(EXTENSION_SOURCE)}`,
`--model ${shellEscape(model)}`,
extra,
shellEscape(task),
]
.filter(Boolean)
.join(" ");
// Herdr does not retain terminal output for tabs that have never been shown.
// Focus the test runner surface so waitForScreen can observe its transcript.
focusSurface(surface);
sendLongCommand(surface, `${cmd}; echo '__TEST_DONE_'$?'__'`, {
scriptPath: join(testDir, `test-launch-${Date.now()}.sh`),
});
}
// ── Polling helpers ──
/**
* Poll until a regex pattern appears in the surface's screen output.
* Throws on timeout with the last screen contents for debugging.
*/
export async function waitForScreen(
surface: string,
pattern: RegExp,
timeout: number = PI_TIMEOUT,
lines: number = 200,
): Promise<string> {
const start = Date.now();
while (Date.now() - start < timeout) {
try {
const screen = await readScreenAsync(surface, lines);
if (pattern.test(screen)) return screen;
} catch {}
await sleep(2000);
}
let finalScreen = "";
try {
finalScreen = readScreen(surface, lines);
} catch {}
throw new Error(
`Timeout (${timeout}ms) waiting for pattern ${pattern}.\nLast screen:\n${finalScreen.slice(-1000)}`,
);
}
/**
* Poll until a file exists and optionally matches a content pattern.
* Returns the file content on success.
*/
export async function waitForFile(
path: string,
timeout: number = PI_TIMEOUT,
contentPattern?: RegExp,
): Promise<string> {
const start = Date.now();
while (Date.now() - start < timeout) {
if (existsSync(path)) {
const content = readFileSync(path, "utf8");
if (!contentPattern || contentPattern.test(content)) return content;
}
await sleep(2000);
}
throw new Error(
`Timeout (${timeout}ms) waiting for file: ${path}` +
(contentPattern ? ` matching ${contentPattern}` : ""),
);
}
/**
* Wait for the pi process in a surface to exit (sentinel detection).
* Returns the exit code.
*/
export async function waitForPiExit(
surface: string,
timeout: number = PI_TIMEOUT,
): Promise<number> {
const screen = await waitForScreen(surface, /__TEST_DONE_(\d+)__/, timeout);
const match = screen.match(/__TEST_DONE_(\d+)__/);
return match ? parseInt(match[1], 10) : -1;
}
// ── Utilities ──
export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function uniqueId(): string {
return Math.random().toString(36).slice(2, 10) + Date.now().toString(36);
}
/**
* Register a temp file for cleanup.
*/
export function trackTempFile(env: TestEnv, path: string): void {
env.tempFiles.push(path);
}
@@ -0,0 +1,378 @@
/**
* Integration tests for the full subagent lifecycle.
*
* These tests spawn REAL pi sessions with REAL LLM calls (gpt-5.6-luna by default).
* Each test creates a terminal surface, runs pi with a task that uses the subagent
* tool, and verifies the outcome via marker files and screen output.
*
* Costs: ~$0.01-0.05 per test run.
* Duration: ~30-90s per test.
*
* Run inside Herdr, inside an Orca terminal, or inside tmux:
* tmux new 'npm run test:integration'
*
* Configuration:
* PI_TEST_MODEL — model for all pi sessions (default: openai-codex/gpt-5.6-luna)
* PI_TEST_TIMEOUT — per-test timeout in ms (default: 120000)
*/
import { describe, it, before, after } from "node:test";
import assert from "node:assert/strict";
import { existsSync, readFileSync, unlinkSync } from "node:fs";
import {
getAvailableBackends,
createTestEnv,
cleanupTestEnv,
createTrackedSurface,
startPi,
waitForScreen,
waitForFile,
sleep,
uniqueId,
trackTempFile,
readScreen,
sendCommand,
PI_TIMEOUT,
type TestEnv,
} from "./harness.ts";
const backends = getAvailableBackends();
if (backends.length === 0) {
console.log("⚠️ Herdr/Orca/tmux is not available — skipping subagent lifecycle integration tests");
console.log(" Run inside Herdr, Orca, or tmux to enable these tests.");
}
for (const backend of backends) {
describe(`subagent-lifecycle [${backend}]`, { timeout: PI_TIMEOUT * 3 }, () => {
let env: TestEnv;
before(() => {
env = createTestEnv();
});
after(() => {
cleanupTestEnv(env);
});
// ── Basic spawn + completion ──
it("spawns a subagent that writes a file and verifies the session", async () => {
const id = uniqueId();
const markerFile = `/tmp/pi-integ-echo-${id}.txt`;
trackTempFile(env, markerFile);
const surface = createTrackedSurface(env, `echo-${id}`);
await sleep(1000);
const task = [
`Call the subagent tool with these EXACT parameters:`,
` name: "Echo-${id}"`,
` agent: "test-echo"`,
` task: "Run this bash command: echo 'PASS_${id}' > '${markerFile}'"`,
`Do not do anything else. Just call the subagent tool once.`,
`After you receive the subagent result, say INTEGRATION_COMPLETE.`,
].join("\n");
startPi(surface, env.dir, task);
// Verify: subagent created the marker file
const content = await waitForFile(markerFile, PI_TIMEOUT, /PASS/);
assert.ok(
content.includes(`PASS_${id}`),
`Marker file should contain PASS_${id}. Got: ${content.trim()}`,
);
// Verify: outer pi received the subagent result
const screen = await waitForScreen(
surface,
/INTEGRATION_COMPLETE|completed|Sub-agent.*"Echo/i,
PI_TIMEOUT,
);
// Verify: session file was created (shown in steer result)
const sessionMatch = screen.match(/Session:\s*(\S+\.jsonl)/);
if (sessionMatch) {
const sessionFile = sessionMatch[1];
assert.ok(existsSync(sessionFile), `Subagent session file should exist: ${sessionFile}`);
const lines = readFileSync(sessionFile, "utf8").trim().split("\n");
assert.ok(lines.length >= 2, `Session should have ≥2 entries, got ${lines.length}`);
const header = JSON.parse(lines[0]);
assert.equal(header.type, "session", "First entry should be session header");
assert.ok(header.id, "Session header should have an id");
}
});
// ── Reload preservation ──
it("keeps a running subagent and fresh tool wrappers across parent extension reload", async () => {
const id = uniqueId();
const startedFile = `/tmp/pi-integ-reload-start-${id}.txt`;
const completedFile = `/tmp/pi-integ-reload-done-${id}.txt`;
const followupFile = `/tmp/pi-integ-reload-followup-${id}.txt`;
trackTempFile(env, startedFile);
trackTempFile(env, completedFile);
trackTempFile(env, followupFile);
const surface = createTrackedSurface(env, `reload-${id}`);
await sleep(1000);
const task = [
`Call the subagent tool once with these EXACT parameters:`,
` name: "Reload-${id}"`,
` agent: "test-echo"`,
` task: "Run: echo 'START_${id}' > '${startedFile}'; sleep 10; echo 'DONE_${id}' > '${completedFile}'"`,
`Do not run the command yourself.`,
`After that subagent result arrives, call the subagent tool once more with these EXACT parameters:`,
` name: "Reload-followup-${id}"`,
` agent: "test-echo"`,
` task: "Write 'FOLLOWUP_${id}' to '${followupFile}'"`,
`After the second subagent result arrives, say RELOAD_SURVIVED_${id}.`,
].join("\n");
startPi(surface, env.dir, task);
await waitForFile(startedFile, PI_TIMEOUT, /START_/);
sendCommand(surface, "/reload");
const completed = await waitForFile(completedFile, PI_TIMEOUT, /DONE_/);
assert.ok(completed.includes(`DONE_${id}`), "child should finish after parent reload");
const followup = await waitForFile(followupFile, PI_TIMEOUT, /FOLLOWUP_/);
assert.ok(
followup.includes(`FOLLOWUP_${id}`),
"the result-triggered turn should spawn through the fresh tool wrapper",
);
const resultMarker = `RELOAD_SURVIVED_${id}`;
const responseLine = new RegExp(`^ ${resultMarker}\\s*$`, "m");
const screen = await waitForScreen(surface, responseLine, PI_TIMEOUT, 300);
assert.equal(
screen.match(new RegExp(`^ ${resultMarker}\\s*$`, "gm"))?.length,
1,
"the fresh runtime should receive the completion exactly once",
);
assert.doesNotMatch(screen, /Subagent cancelled|ctx is stale after session replacement or reload/i);
});
// ── In-progress activity snapshots ──
it("keeps a long active tool call from surfacing false stalled status", async () => {
const id = uniqueId();
const startFile = `/tmp/pi-integ-status-start-${id}.txt`;
const markerFile = `/tmp/pi-integ-status-${id}.txt`;
trackTempFile(env, startFile);
trackTempFile(env, markerFile);
const surface = createTrackedSurface(env, `status-${id}`);
await sleep(1000);
const task = [
`Call the subagent tool with these EXACT parameters:`,
` name: "Status-${id}"`,
` agent: "test-echo"`,
` task: "Run this bash command: echo 'START_${id}' > '${startFile}'; sleep 90; echo 'STATUS_${id}' > '${markerFile}'"`,
`Do not do anything else. Just call the subagent tool once.`,
`After you receive the subagent result, say STATUS_TEST_DONE.`,
].join("\n");
startPi(surface, env.dir, task);
const activeScreen = await waitForScreen(surface, /active[\s\S]*bash|bash[\s\S]*active/i, PI_TIMEOUT, 300);
assert.doesNotMatch(activeScreen, /Subagent status[\s\S]*stalled|stalled[\s\S]*Subagent status/i);
await waitForFile(startFile, PI_TIMEOUT, /START_/);
assert.equal(existsSync(markerFile), false, "Completion marker should not exist before the long sleep");
await sleep(65_000);
assert.equal(existsSync(markerFile), false, "Completion marker should not exist before the watchdog assertion");
const watchdogScreen = readScreen(surface, 300);
assert.doesNotMatch(watchdogScreen, /Subagent status[\s\S]*stalled|stalled[\s\S]*Subagent status/i);
const content = await waitForFile(markerFile, PI_TIMEOUT, /STATUS_/);
assert.ok(content.includes(`STATUS_${id}`), `Marker file should contain STATUS_${id}`);
const completionScreen = await waitForScreen(
surface,
/STATUS_TEST_DONE|completed|Sub-agent.*"Status-/i,
PI_TIMEOUT,
300,
);
assert.ok(/STATUS_TEST_DONE|completed/i.test(completionScreen));
});
// ── Parallel subagent spawn ──
it("spawns two subagents in parallel and both complete", async () => {
const id = uniqueId();
const fileA = `/tmp/pi-integ-para-${id}-a.txt`;
const fileB = `/tmp/pi-integ-para-${id}-b.txt`;
trackTempFile(env, fileA);
trackTempFile(env, fileB);
const surface = createTrackedSurface(env, `parallel-${id}`);
await sleep(1000);
const task = [
`You must call the subagent tool TWICE. Make both calls before waiting for results.`,
``,
`First call:`,
` name: "ParaA-${id}"`,
` agent: "test-echo"`,
` task: "Run: echo 'DONE_A_${id}' > '${fileA}'"`,
``,
`Second call:`,
` name: "ParaB-${id}"`,
` agent: "test-echo"`,
` task: "Run: echo 'DONE_B_${id}' > '${fileB}'"`,
``,
`Call both subagent tools NOW, do not wait between them.`,
].join("\n");
startPi(surface, env.dir, task);
// Both marker files should appear
const [contentA, contentB] = await Promise.all([
waitForFile(fileA, PI_TIMEOUT, /DONE_A/),
waitForFile(fileB, PI_TIMEOUT, /DONE_B/),
]);
assert.ok(contentA.includes(`DONE_A_${id}`), `File A should contain marker`);
assert.ok(contentB.includes(`DONE_B_${id}`), `File B should contain marker`);
});
// ── Fork mode ──
it("fork mode creates a child session linked to the parent", async () => {
const id = uniqueId();
const markerFile = `/tmp/pi-integ-fork-${id}.txt`;
trackTempFile(env, markerFile);
const surface = createTrackedSurface(env, `fork-${id}`);
await sleep(1000);
const task = [
`Call the subagent tool with these EXACT parameters:`,
` name: "Fork-${id}"`,
` agent: "test-fork"`,
` task: "Run this bash command: echo 'FORK_OK_${id}' > '${markerFile}'"`,
`Do not do anything else. Just call the subagent tool once.`,
`After you receive the result, say FORK_COMPLETE.`,
].join("\n");
startPi(surface, env.dir, task);
// Verify: forked subagent created the file
const content = await waitForFile(markerFile, PI_TIMEOUT, /FORK_OK/);
assert.ok(content.includes(`FORK_OK_${id}`), `Fork marker file should exist with content`);
// Wait for the outer pi to show the result
const screen = await waitForScreen(
surface,
/FORK_COMPLETE|completed|Sub-agent.*"Fork/i,
PI_TIMEOUT,
);
// Verify: the forked session has a parent link
const sessionMatch = screen.match(/Session:\s*(\S+\.jsonl)/);
if (sessionMatch) {
const sessionFile = sessionMatch[1];
assert.ok(existsSync(sessionFile), `Fork session file should exist: ${sessionFile}`);
const entries = readFileSync(sessionFile, "utf8")
.trim()
.split("\n")
.map((l) => JSON.parse(l));
const header = entries[0];
assert.equal(header.type, "session", "First entry should be session header");
assert.ok(header.parentSession, "Fork session should have parentSession field");
// Fork sessions include parent context (model_change entries etc.)
assert.ok(entries.length >= 2, "Fork session should have context entries beyond header");
}
});
// ── caller_ping ──
it("subagent caller_ping sends notification back to the parent", async () => {
const id = uniqueId();
const surface = createTrackedSurface(env, `ping-${id}`);
await sleep(1000);
const task = [
`Call the subagent tool with these EXACT parameters:`,
` name: "Ping-${id}"`,
` agent: "test-ping"`,
` task: "PING_TEST_${id}"`,
`Just call the subagent tool once. Do not do anything else before calling it.`,
].join("\n");
startPi(surface, env.dir, task);
// The test-ping agent calls caller_ping, which steers a "needs help" message
// back to the outer pi. Look for it on screen.
const screen = await waitForScreen(
surface,
/needs help|PING|caller_ping|ping/i,
PI_TIMEOUT,
);
assert.ok(
/needs help|PING/i.test(screen),
`Screen should show ping notification. Got:\n${screen.slice(-800)}`,
);
});
// ── Agent discovery ──
it("subagent discovers project-local test agents", async () => {
const id = uniqueId();
const markerFile = `/tmp/pi-integ-discovery-${id}.txt`;
trackTempFile(env, markerFile);
const surface = createTrackedSurface(env, `discovery-${id}`);
await sleep(1000);
// Use subagents_list to verify test agents are discoverable,
// then spawn one to prove it works end-to-end.
const task = [
`First, call the subagents_list tool to see available agents.`,
`Then call the subagent tool:`,
` name: "Disco-${id}"`,
` agent: "test-echo"`,
` task: "Run: echo 'DISCO_${id}' > '${markerFile}'"`,
`After you receive the subagent result, say DISCOVERY_DONE.`,
].join("\n");
startPi(surface, env.dir, task);
// The test-echo agent (discovered from project .pi/agents/) should work
const content = await waitForFile(markerFile, PI_TIMEOUT, /DISCO/);
assert.ok(content.includes(`DISCO_${id}`), `Discovery test marker should exist`);
});
// ── Subagent with custom system prompt ──
it("passes systemPrompt to subagent", async () => {
const id = uniqueId();
const markerFile = `/tmp/pi-integ-sysprompt-${id}.txt`;
trackTempFile(env, markerFile);
const surface = createTrackedSurface(env, `sysprompt-${id}`);
await sleep(1000);
const task = [
`Call the subagent tool with these parameters:`,
` name: "SysP-${id}"`,
` agent: "test-echo"`,
` systemPrompt: "Always start your response with CUSTOM_PROMPT_ACTIVE."`,
` task: "Write 'SYSPROMPT_${id}' to ${markerFile} using bash: echo 'SYSPROMPT_${id}' > '${markerFile}'"`,
`After the subagent completes, say SYSPROMPT_TEST_DONE.`,
].join("\n");
startPi(surface, env.dir, task);
const content = await waitForFile(markerFile, PI_TIMEOUT, /SYSPROMPT/);
assert.ok(content.includes(`SYSPROMPT_${id}`), `System prompt test marker should exist`);
});
});
}
@@ -0,0 +1,226 @@
/**
* Integration tests for the selected terminal-surface backend.
*
* These tests exercise real Herdr, Orca, or tmux operations: creating surfaces,
* sending commands, reading screen output, and closing panes.
* No LLM calls — fast and free.
*
* Run inside Herdr, inside an Orca terminal, or inside tmux:
* tmux new 'npm run test:integration'
*/
import { describe, it, before, after } from "node:test";
import assert from "node:assert/strict";
import { unlinkSync } from "node:fs";
import {
getAvailableBackends,
createTestEnv,
cleanupTestEnv,
createTrackedSurface,
focusSurface,
getFocusedSurface,
waitForFocusedSurface,
untrackSurface,
sendCommand,
sendLongCommand,
readScreen,
readScreenAsync,
closeSurface,
pollForExit,
wrapWithCompletionFile,
sleep,
uniqueId,
trackTempFile,
waitForFile,
waitForScreen,
type TestEnv,
} from "./harness.ts";
const backends = getAvailableBackends();
const FOCUS_TEST_SHELL_READY_DELAY_MS = Number(process.env.PI_SUBAGENT_SHELL_READY_DELAY_MS ?? "2500");
if (backends.length === 0) {
console.log("⚠️ Herdr/Orca/tmux is not available — skipping surface integration tests");
console.log(" Run inside Herdr, Orca, or tmux to enable these tests.");
}
for (const backend of backends) {
describe(`surface [${backend}]`, { timeout: 60_000 }, () => {
let env: TestEnv;
before(() => {
env = createTestEnv();
});
after(() => {
cleanupTestEnv(env);
});
it("keeps focus on the active surface while creating and targeting subagent surfaces", async () => {
const anchor = createTrackedSurface(env, "focus-anchor");
await sleep(1000);
focusSurface(anchor);
await waitForFocusedSurface(anchor, 10_000);
const childA = createTrackedSurface(env, "focus-child-a");
await sleep(FOCUS_TEST_SHELL_READY_DELAY_MS);
assert.equal(getFocusedSurface(), anchor);
const childB = createTrackedSurface(env, "focus-child-b");
await sleep(FOCUS_TEST_SHELL_READY_DELAY_MS);
assert.equal(getFocusedSurface(), anchor);
const markerA = uniqueId();
const markerB = uniqueId();
focusSurface(childA);
sendCommand(childA, `echo "FOCUS_A_${markerA}"`);
await waitForScreen(childA, new RegExp(`FOCUS_A_${markerA}`), 20_000, 50);
focusSurface(childB);
sendCommand(childB, `echo "FOCUS_B_${markerB}"`);
await waitForScreen(childB, new RegExp(`FOCUS_B_${markerB}`), 20_000, 50);
focusSurface(anchor);
await waitForFocusedSurface(anchor, 10_000);
});
it("polls file-based completion and closes an unfocused surface", async () => {
const focusedBefore = getFocusedSurface();
const surface = createTrackedSurface(env, "background-completion");
await sleep(1000);
const completionFile = `${env.dir}/background.exit-code`;
sendLongCommand(surface, wrapWithCompletionFile("false", completionFile));
const result = await pollForExit(surface, new AbortController().signal, {
interval: 100,
completionFile,
});
assert.deepEqual(result, { reason: "sentinel", exitCode: 1 });
assert.equal(getFocusedSurface(), focusedBefore);
closeSurface(surface);
untrackSurface(env, surface);
assert.throws(() => readScreen(surface, 5));
});
it("creates a surface, sends a command, reads output, and closes it", async () => {
const surface = createTrackedSurface(env, "echo-test");
await sleep(1000);
focusSurface(surface);
const marker = uniqueId();
sendCommand(surface, `echo "MARKER_${marker}"`);
await sleep(1500);
const screen = readScreen(surface, 50);
assert.ok(
screen.includes(`MARKER_${marker}`),
`Expected screen to contain MARKER_${marker}. Got:\n${screen}`,
);
closeSurface(surface);
untrackSurface(env, surface);
});
it("preserves shell special characters in echo output", async () => {
const surface = createTrackedSurface(env, "escape-test");
await sleep(1000);
focusSurface(surface);
const marker = uniqueId();
// Single-quoted string — $ and " are literal inside single quotes
sendCommand(surface, `echo 'SPEC_${marker}_$HOME_"quotes"_done'`);
await sleep(1500);
const screen = readScreen(surface, 50);
assert.ok(
screen.includes(`SPEC_${marker}`),
`Expected special-char output. Got:\n${screen}`,
);
// $ should be literal inside single quotes
assert.ok(
screen.includes("$HOME"),
`Expected literal $HOME in output. Got:\n${screen}`,
);
});
it("sends a long command via script file without truncation", async () => {
const surface = createTrackedSurface(env, "long-cmd-test");
await sleep(1000);
focusSurface(surface);
const marker = uniqueId();
const longValue = "X".repeat(500);
const command = `echo "LONG_${marker}_${longValue}_END"`;
sendLongCommand(surface, command);
await sleep(2000);
const screen = readScreen(surface, 50);
assert.ok(
screen.includes(`LONG_${marker}`),
`Expected long command output. Got:\n${screen.slice(0, 300)}...`,
);
assert.ok(
screen.includes("_END"),
`Expected full output (not truncated). Got:\n${screen.slice(-300)}`,
);
});
it("reads screen asynchronously", async () => {
const surface = createTrackedSurface(env, "async-read-test");
await sleep(1000);
focusSurface(surface);
const marker = uniqueId();
sendCommand(surface, `echo "ASYNC_${marker}"`);
await sleep(1500);
const screen = await readScreenAsync(surface, 50);
assert.ok(
screen.includes(`ASYNC_${marker}`),
`Async read should find marker. Got:\n${screen}`,
);
});
it("manages multiple surfaces concurrently", async () => {
const s1 = createTrackedSurface(env, "multi-1");
const s2 = createTrackedSurface(env, "multi-2");
await sleep(1500);
const m1 = uniqueId();
const m2 = uniqueId();
focusSurface(s1);
sendCommand(s1, `echo "S1_${m1}"`);
await sleep(750);
focusSurface(s2);
sendCommand(s2, `echo "S2_${m2}"`);
await sleep(1500);
const screen1 = readScreen(s1, 50);
const screen2 = readScreen(s2, 50);
assert.ok(screen1.includes(`S1_${m1}`), `Surface 1 missing marker. Got:\n${screen1}`);
assert.ok(screen2.includes(`S2_${m2}`), `Surface 2 missing marker. Got:\n${screen2}`);
});
it("writes output to a file and verifies via surface", async () => {
const surface = createTrackedSurface(env, "file-test");
await sleep(1000);
focusSurface(surface);
const marker = uniqueId();
const filePath = `/tmp/pi-surface-test-${marker}.txt`;
sendCommand(surface, `echo "FILE_${marker}" > ${filePath} && echo "WRITTEN_${marker}"`);
await waitForScreen(surface, new RegExp(`WRITTEN_${marker}`), 10_000, 50);
const content = await waitForFile(filePath, 10_000, new RegExp(`FILE_${marker}`));
assert.ok(content.includes(`FILE_${marker}`), `File content wrong. Got: ${content}`);
// Clean up
try {
unlinkSync(filePath);
} catch {}
});
});
}
@@ -0,0 +1,163 @@
/**
* Smoke tests for the systemPromptMode feature.
* Tests: frontmatter parsing, identity routing, CLI flag generation.
*/
import { mkdtempSync, writeFileSync, rmSync, mkdirSync, readFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
let passed = 0;
let failed = 0;
function assert(condition: boolean, msg: string) {
if (condition) {
console.log(` ✅ ${msg}`);
passed++;
} else {
console.log(` ❌ ${msg}`);
failed++;
}
}
// --- Extracted logic under test ---
function parseFrontmatter(content: string) {
const match = content.match(/^---\n([\s\S]*?)\n---/);
if (!match) return null;
const frontmatter = match[1];
const get = (key: string) => {
const m = frontmatter.match(new RegExp(`^${key}:\\s*(.+)$`, "m"));
return m ? m[1].trim() : undefined;
};
const body = content.replace(/^---\n[\s\S]*?\n---\n*/, "").trim();
const spm = get("system-prompt");
return {
systemPromptMode: spm === "replace" ? "replace" : spm === "append" ? "append" : undefined,
body: body || undefined,
};
}
function simulateRouting(
agentBody: string | undefined,
systemPromptMode: "append" | "replace" | undefined,
paramSystemPrompt: string | undefined,
) {
const identity = agentBody ?? paramSystemPrompt ?? null;
const identityInSystemPrompt = systemPromptMode && identity;
const roleBlock = identity && !identityInSystemPrompt ? `\n\n${identity}` : "";
let cliFlag: string | null = null;
if (identityInSystemPrompt && identity) {
cliFlag = systemPromptMode === "replace" ? "--system-prompt" : "--append-system-prompt";
}
return { roleBlock, cliFlag, identityInSystemPrompt: !!identityInSystemPrompt };
}
// --- Fixtures ---
const AGENT_REPLACE = `---
model: anthropic/claude-sonnet-4-20250514
system-prompt: replace
auto-exit: true
---
You are a specialized agent.`;
const AGENT_APPEND = `---
model: anthropic/claude-sonnet-4-20250514
system-prompt: append
---
You are an appended identity.`;
const AGENT_DEFAULT = `---
model: anthropic/claude-sonnet-4-20250514
---
You are a default agent.`;
const AGENT_INVALID = `---
model: anthropic/claude-sonnet-4-20250514
system-prompt: foobar
---
Body here.`;
// --- Test 1: Frontmatter parsing ---
console.log("\n🧪 Frontmatter parsing of system-prompt field");
const r1 = parseFrontmatter(AGENT_REPLACE)!;
assert(r1.systemPromptMode === "replace", "system-prompt: replace → mode is 'replace'");
assert(r1.body === "You are a specialized agent.", "body extracted correctly");
const r2 = parseFrontmatter(AGENT_APPEND)!;
assert(r2.systemPromptMode === "append", "system-prompt: append → mode is 'append'");
const r3 = parseFrontmatter(AGENT_DEFAULT)!;
assert(r3.systemPromptMode === undefined, "no system-prompt field → mode is undefined");
const r4 = parseFrontmatter(AGENT_INVALID)!;
assert(r4.systemPromptMode === undefined, "system-prompt: foobar → mode is undefined (ignored)");
// --- Test 2: Identity routing ---
console.log("\n🧪 Identity routing (system prompt vs user message)");
const s1 = simulateRouting("You are X.", "replace", undefined);
assert(s1.roleBlock === "", "replace mode: roleBlock empty (not in task)");
assert(s1.cliFlag === "--system-prompt", "replace mode: uses --system-prompt flag");
const s2 = simulateRouting("You are X.", "append", undefined);
assert(s2.roleBlock === "", "append mode: roleBlock empty (not in task)");
assert(s2.cliFlag === "--append-system-prompt", "append mode: uses --append-system-prompt flag");
const s3 = simulateRouting("You are X.", undefined, undefined);
assert(s3.roleBlock === "\n\nYou are X.", "no mode: roleBlock contains identity");
assert(s3.cliFlag === null, "no mode: no CLI flag");
const s4 = simulateRouting(undefined, undefined, undefined);
assert(s4.roleBlock === "", "no identity: roleBlock empty");
assert(s4.cliFlag === null, "no identity: no CLI flag");
const s5 = simulateRouting(undefined, "replace", undefined);
assert(s5.roleBlock === "", "mode set but no body: roleBlock empty");
assert(s5.cliFlag === null, "mode set but no body: no CLI flag");
const s6 = simulateRouting(undefined, "replace", "Param identity");
assert(s6.cliFlag === "--system-prompt", "mode + param systemPrompt: uses CLI flag");
assert(s6.roleBlock === "", "mode + param systemPrompt: roleBlock empty");
// --- Test 3: End-to-end with temp agent files ---
console.log("\n🧪 End-to-end with temp agent files");
const tmpDir = mkdtempSync(join(tmpdir(), "pi-test-spm-"));
const agentsDir = join(tmpDir, ".pi", "agents");
mkdirSync(agentsDir, { recursive: true });
writeFileSync(join(agentsDir, "test-replace.md"), AGENT_REPLACE);
writeFileSync(join(agentsDir, "test-append.md"), AGENT_APPEND);
writeFileSync(join(agentsDir, "test-default.md"), AGENT_DEFAULT);
function loadFromDir(name: string) {
const p = join(agentsDir, `${name}.md`);
if (!existsSync(p)) return null;
return parseFrontmatter(readFileSync(p, "utf8"));
}
const t1 = loadFromDir("test-replace")!;
assert(t1.systemPromptMode === "replace", "file test-replace.md → replace mode");
assert(t1.body === "You are a specialized agent.", "file test-replace.md → body correct");
const t2 = loadFromDir("test-append")!;
assert(t2.systemPromptMode === "append", "file test-append.md → append mode");
const t3 = loadFromDir("test-default")!;
assert(t3.systemPromptMode === undefined, "file test-default.md → no mode");
rmSync(tmpDir, { recursive: true });
// --- Summary ---
console.log(`\n${"=".repeat(40)}`);
console.log(`Results: ${passed} passed, ${failed} failed`);
if (failed > 0) process.exit(1);
console.log("All tests passed! ✅\n");
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"allowImportingTsExtensions": true,
"strict": true,
"noEmit": true,
"skipLibCheck": true,
"types": ["node"]
},
"include": ["pi-extension/**/*.ts", "test/**/*.ts"]
}
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 JB
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+7
View File
@@ -0,0 +1,7 @@
# @_jb/pi-lm-studio
Pi provider extension for a local LM Studio server at `http://127.0.0.1:1234/v1`.
```bash
pi install npm:@_jb/pi-lm-studio
```
+22
View File
@@ -0,0 +1,22 @@
{
"name": "@_jb/pi-lm-studio",
"version": "0.1.0",
"description": "Pi provider registration for a local LM Studio server.",
"type": "module",
"license": "MIT",
"engines": { "node": ">=22.19.0" },
"keywords": ["pi-package", "pi-extension", "provider", "lm-studio"],
"files": ["index.ts", "README.md", "LICENSE"],
"pi": { "extensions": ["./index.ts"] },
"peerDependencies": {
"@earendil-works/pi-coding-agent": "catalog:pi-peer"
},
"publishConfig": {
"registry": "https://git.jb.codes/api/packages/jb/npm/"
},
"repository": {
"type": "git",
"url": "git+ssh://git@git.jb.codes:7722/jb/harness-tools.git",
"directory": "extensions/lm-studio"
}
}
+7
View File
@@ -0,0 +1,7 @@
node_modules/
coverage/
dist/
*.log
.DS_Store
.mesh
.pi
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 JB
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+77
View File
@@ -0,0 +1,77 @@
# pi-native-hooks
A native Pi extension that coordinates trusted personal policies after workspace mutations. It shares one ast-grep analysis snapshot across diagnostic policies and gates settlement until exact, current verification evidence exists or the agent records a reasoned waiver.
## Requirements
- Node.js 22.19 or newer
- Pi 0.84.4 or compatible
- ast-grep 0.45 or newer for diagnostics
## Install
Installation is a separate, user-level mutation and is **not** performed by this repository's tests:
```bash
pi install npm:@_jb/pi-native-hooks
```
For monorepo development, use the curated link policy or load `extensions/native-hooks/extensions/pi-native-hooks/index.ts` directly.
Then use:
```text
/hooks-init --dry-run
/hooks-init --apply
/hooks-doctor
/reload
```
`--apply` displays every destination and asks for interactive confirmation. Existing files are preserved; newly created files are rolled back after a partial failure.
## Trust model
Files under `~/.config/pi-native-hooks/policies/*.ts` are trusted executable code. Importing them runs with Pi's filesystem, process, network, credentials, and outer sandbox authority. Structural validation is **not a sandbox**. The loader accepts only sorted direct-child `.ts` files and requires the config root, policies, ast-grep configuration, and rules to be owned by the current OS user, non-symlinked, regular files/directories, and not group/other writable.
Personal policies use Node's erasable TypeScript subset: type-only syntax, interfaces, and `satisfies` work; enums, namespaces, parameter properties, JSX, and tsconfig transforms do not. Policies reload only with `/reload` or session replacement.
## Diagnostics
The starter policies flag TypeScript/TSX non-null assertions and dynamic imports. Both use `ast-grep:personal`, so one mutation wave runs one scan and fans the immutable snapshot to both policies. A justified exception uses the exact rule ID:
```ts
const plugin = await import(path); // ast-grep-ignore: typescript-no-dynamic-import-ts -- runtime-selected plugin
```
Analyzer failures are visible and never turn a successful mutation into a tool error. Unchanged findings are not repeatedly delivered.
## Verification gate
Starter project policies require these exact standalone commands from the matched Git project root:
```text
bun run test
bun run typecheck
```
Focused, combined, altered, wrong-directory, failed, aborted, concurrent, or stale commands do not count. A state-free session treats existing dirty files as its baseline; requirements arm only after an observed relevant mutation or restoration of armed branch state. At `agent_settled`, outstanding checks trigger another Pi turn.
The agent may call `pi_hooks_waive_verification` with a non-empty reason. A waiver is auditable, revision-scoped, distinguishable from a pass, and expires after the next relevant mutation.
Pi exposes foreground bash completion as `isError` rather than a numeric exit code. If a shell is externally signaled but Pi reports success, this extension cannot independently disprove completion. The settlement gate also cannot prevent a user or external client from closing Pi.
## Commands
- `/hooks-status` — policy digests and paths, trust/load failures, project identity, workspace revision, analyzer health, findings, verification revisions, evidence, waivers, and failures
- `/hooks-init --dry-run` — preview external config writes
- `/hooks-init --apply` — request authorization and initialize missing starter files
- `/hooks-doctor` — validate policy loading and ast-grep availability
## Development
```bash
bun run typecheck
bun run test
```
Architecture and acceptance details are in [`docs/implementation-plan.md`](docs/implementation-plan.md). Event payload and project identity assumptions are pinned in [`docs/event-contract.md`](docs/event-contract.md).
@@ -0,0 +1,9 @@
# ADR 0001: standalone runtime and trusted policies
**Status:** accepted, 27 August 2026
`pi-native-hooks` is one Pi package and extension. It loads only direct-child TypeScript policies from the current OS user's `~/.config/pi-native-hooks/policies`, after owner, mode, regular-file, and no-symlink checks. Policies are trusted executable code with Pi's authority; validation is not a sandbox.
Analyzer execution is package-owned and coalesced by effective analyzer key. The MVP adapter is ast-grep. Verification is exact-command, revision-scoped evidence, persisted on the active session branch. Pending armed checks force an `agent_settled` follow-up; a reasoned agent waiver is visible and expires on the next relevant mutation.
Node-native erasable TypeScript is required on Node >=22.19. Unsupported syntax fails that policy with an actionable load error; no implicit transpiler fallback is used.
@@ -0,0 +1,5 @@
# Runtime event and identity contract
The runtime targets Pi 0.84.4. `tool_execution_start` carries `toolCallId`, `toolName`, and `args`; all parallel sibling starts are emitted in source order before execution. A finalized execution either enters `tool_result` (with final `input`, `content`, `details`, `usage`, and `isError`) followed by `tool_execution_end`, or may enter only `tool_execution_end` when blocked/invalid. Handlers are idempotent by tool-call ID. `agent_settled` is the completion candidate used by the gate.
Project identity is SHA-256 of the canonical git root, initial commit object (or `NO_HEAD`), and package name, NUL-delimited. The mutable HEAD and index belong to the workspace fingerprint, not identity. A different canonical root or initial commit is a different project. This avoids remote-URL instability while preventing evidence transfer to an unrelated checkout.
@@ -0,0 +1,656 @@
# pi-native-hooks implementation plan
## Status
**Approved on 27 August 2026 and implemented as the repository baseline.** Activation remains a separate explicit user-authorized step; the package is not installed and live personal configuration is not created by implementation or tests.
## Purpose
Build one native Pi extension that runs trusted personal policies after relevant workspace mutations. The first policies will:
1. detect TypeScript non-null assertions and dynamic imports with AST-backed ast-grep rules, then append concise steering diagnostics to the mutation execution wave's tool results;
2. track exact required verification commands and prevent Pi from remaining settled while their evidence is missing or stale; and
3. provide an agent-callable, reasoned waiver when verification is intentionally skipped.
The extension will also establish a small toolkit and authoring skill for adding focused policies without duplicating Pi lifecycle, analyzer execution, state, or delivery logic.
## Decisions already made
- Implement a native TypeScript Pi extension with `pi.on(...)`, not `pi-yaml-hooks`, `pi-steering`, or `workflow`.
- Build one standalone repository and Pi package named `pi-native-hooks`, with one extension of the same name.
- Install the package from `/srv/data/code/jb-pi/pi-native-hooks` as a Pi local-path package during development; do not depend on harness-tools link management.
- Load personal hook policies as trusted TypeScript modules from `~/.config/pi-native-hooks/`.
- Use ast-grep rule packs for syntax diagnostics.
- Execute an analyzer once per overlapping mutation wave and analyzer configuration, then fan an immutable normalized result out to every interested policy. Individual policy modules must not spawn AST, lint, or LSP processes through the runtime interface.
- Require exact configured verification commands. A focused, filtered, combined, or otherwise altered command does not satisfy an exact requirement unless separately configured.
- Treat `agent_settled` as Pi's strongest available completion candidate. If armed verification is pending, inject a `followUp` custom message with `triggerTurn: true`.
- Arm verification only after this session observes a relevant mutation or restores an already-armed branch state. A new session treats pre-existing dirty files as its baseline rather than forcing checks during question-only work.
- Accept foreground verification when Pi reports `event.isError === false`. Document the narrow limitation that Pi's public bash result omits the exit code and can be ambiguous if the shell process itself ends through an external signal.
- Allow an agent-callable waiver tool. A waiver must contain a non-empty reason, is scoped to the requirement revisions and outstanding checks it clears, is persisted, and becomes stale after the next relevant mutation.
- Keep command sandboxing, destructive-command policy, permissions, and process isolation out of scope. nono profiles own those controls.
- Treat personal policy modules as executable in-process code with Pi's authority. Load only user-owned configuration from `~/.config/pi-native-hooks`; do not load repository-local policy code.
- Use Node's native erasable-TypeScript support on the Pi-supported Node `>=22.19` runtime. Avoid adding a TypeScript loader dependency unless the Phase 0 proof shows native loading is insufficient.
- Discover policies only at session startup or `/reload`; do not add a file watcher in the MVP.
## Outcomes
The first release is successful when:
- an overlapping parallel mutation wave produces at most one ast-grep process;
- both initial TypeScript policies receive the same normalized analysis snapshot;
- diagnostics identify rule, file, line, guidance, and the supported exception form;
- repeated unchanged findings do not repeatedly consume model context;
- writes made by `write`, `edit`, `bash`, and unknown custom tools are detected through workspace state rather than command-name guesses alone;
- each armed required check is valid only after it fully completes successfully at or after its current required revision;
- a foreground exact `bash` command can provide evidence through Pi's finalized success state;
- Pi is forced into a follow-up turn while required evidence is pending, until checks pass or the agent records a waiver;
- state survives session reload/resume without treating stale evidence as current;
- analyzer failures are visible but do not corrupt successful tool results;
- armed verification uncertainty fails closed to a pending gate;
- the hook-authoring skill can create a typed personal policy and focused fixture using the shared toolkit;
- policy load errors, ownership failures, and analyzer degradation are observable without crashing Pi; and
- the local-path Pi package exposes exactly one `pi-native-hooks` extension and the `native-hook-authoring` skill.
## Boundaries
### In scope
- Pi lifecycle coordination for overlapping tool execution waves, messages, settlement, and session state.
- Git-aware workspace snapshots and changed-path discovery.
- A shared analyzer planner and ast-grep adapter.
- Typed normalized findings and structurally validated diagnostic policy modules.
- Tool-result diagnostic augmentation, deduplication, status, and `/hooks-status` observability.
- Exact foreground `bash` verification evidence.
- Forced completion follow-ups and a reasoned waiver tool.
- Initial personal TypeScript diagnostic policies and verification policies for `pi-native-hooks` and `harness-tools`.
- A package-managed hook-authoring skill, policy type declarations, initializer/doctor commands, and local-path installation documentation.
### Deferred
- Implementations of lint and LSP adapters. The coordinator contract must support them, but the MVP implements only ast-grep.
- Async/background verification adapters. The removed `pi-background-tasks` package is not a runtime dependency or integration target.
- Repository-local or remotely downloaded executable policy modules. Only the fixed personal config root is loaded.
- Automatic inference of verification policy from package scripts.
- Semantic/type-aware TypeScript analysis beyond ast-grep's syntax tree.
- Automatic fixes.
- A universal task-completion protocol for clients other than Pi's `agent_settled` lifecycle.
- Accepting interactive `!`/`!!` commands as verification evidence. Out-of-band changes are detected by workspace snapshots, but required checks must run through an observable tool in the MVP.
### Explicit non-goals
- Shell command filtering or rewriting.
- Destructive-command confirmation.
- Filesystem, network, credential, or process sandboxing.
- Replacing project linters, typecheckers, test runners, or LSP servers.
## Repository and runtime boundaries
- **Repository:** `/srv/data/code/jb-pi/pi-native-hooks`
- **Package and extension:** `pi-native-hooks`
- **Extension entrypoint:** `extensions/pi-native-hooks/index.ts`
- **Personal policy root:** `~/.config/pi-native-hooks/`
- **Installation:** Pi local-path package from `/srv/data/code/jb-pi/pi-native-hooks` during development; registry publication is deferred.
- **Runtime code:** package-owned lifecycle, analyzer, state, delivery, validation, and UI modules.
- **Personal code:** user-owned policy modules and adjacent ast-grep rule files. Personal policies are not committed to this repository.
- **Repository fixtures:** checked-in example policies and test fixtures that mirror the personal module contract without containing the user's live policy set.
The repository may ship an initializer that creates the personal directory, a policy-oriented `tsconfig.json`, and starter modules, but it must perform a dry run and request explicit authorization before writing outside the repository. Package install and personal policy initialization are separate operations.
## Trusted TypeScript policy model
Personal policy modules are arbitrary executable code. Importing one can perform side effects before its export is validated, so schema validation is not a sandbox. Documentation and `/hooks-status` must state that these files run with the same filesystem, process, network, and credential authority as Pi; nono remains responsible for the outer sandbox.
The one-run analyzer guarantee applies to toolkit-managed analyzer keys. The runtime withholds process authority from `PolicyContext`, and the authoring skill requires pure evaluators, but a trusted module can still import `node:child_process` and bypass the toolkit. Hard prevention would require declarative or isolated policies and conflicts with the chosen trusted-TypeScript model.
The loader must:
1. resolve the fixed `<current user's home>/.config/pi-native-hooks` root from the OS user record, without accepting environment or repository overrides;
2. require the root, `sgconfig.yml`, policy files, and rule files to be user-owned regular files/directories, not symlinks, and not writable by group or others;
3. discover only sorted direct-child `.ts` files from the documented `policies/` directory;
4. load each file through native dynamic import with an mtime/content cache token on Node `>=22.19`;
5. document the erasable-TypeScript subset: type-only syntax is supported, while enums, namespaces, parameter properties, JSX, and tsconfig transforms are not;
6. validate the default export structurally after import and reject duplicate IDs;
7. isolate a throwing/invalid module so other policies still load, while surfacing the exact file and error; and
8. reload policies only when Pi recreates the extension on `/reload` or session replacement.
The package exposes `@_jb/pi-native-hooks/policy` type declarations. An authorized initializer writes a personal `tsconfig.json` with a path mapping to the installed package so policies can use `import type` plus `satisfies` without a runtime package import. Runtime validation remains authoritative because type checking can be skipped or stale.
Suggested personal layout:
```text
~/.config/pi-native-hooks/
├── tsconfig.json
├── sgconfig.yml
├── policies/
│ ├── typescript-no-non-null-assertion.ts
│ ├── typescript-no-dynamic-import.ts
│ ├── pi-native-hooks-verification.ts
│ └── harness-tools-verification.ts
└── ast-grep/
└── rules/
└── typescript/*.yml
```
## Architecture
```text
~/.config/pi-native-hooks/policies/*.ts
|
v
+-----------------------+
| TrustedPolicyLoader | ownership checks + structural validation
+-----------+-----------+
| immutable policy registry
v
Pi lifecycle events
|
v
+----------------------------+
| ExecutionWaveCoordinator | records source order and terminal events,
| | compares pre/post workspace snapshots
+-------------+--------------+
| MutationWave
v
+-----------------------+ one invocation per analyzer key
| AnalysisCoordinator |----------------------------------+
+-----------+-----------+ |
| immutable AnalysisSnapshot v
| +--------------------+
+-------------------------------> | ast-grep adapter |
| +--------------------+
v
+-----------------------+
| Diagnostic policies | evaluation receives data, not process authority
+-----------+-----------+
| Diagnostics
v
+-----------------------+ +------------------------------+
| Result delivery | | Verification tracker |
| dedupe + augmentation | | revisions/evidence/waivers |
+-----------------------+ +--------------+---------------+
|
v
agent_settled follow-up gate
```
### Contract shapes
The implementation should converge on contracts equivalent to these, without treating these names as immutable API:
```ts
interface MutationWave {
readonly id: string;
readonly turnIndex: number;
readonly orderedToolCallIds: readonly string[];
readonly changedPaths: readonly string[];
readonly beforeFingerprint: string;
readonly afterFingerprint: string;
readonly workspaceRevision: number;
readonly toolResults: ReadonlyMap<string, ObservedToolResult>;
}
interface AnalysisRequest {
readonly analyzerKey: string;
readonly paths: readonly string[];
}
interface AnalysisSnapshot {
readonly analyzerKey: string;
readonly workspaceRevision: number;
readonly paths: readonly string[];
readonly findings: readonly Finding[];
readonly durationMs: number;
readonly failure?: AnalyzerFailure;
}
interface DiagnosticPolicy {
readonly kind: "diagnostic";
readonly id: string;
readonly analyzerKeys: readonly string[];
appliesTo(project: ProjectContext, paths: readonly string[]): boolean;
evaluate(
analysis: ReadonlyMap<string, AnalysisSnapshot>,
context: PolicyContext,
): readonly Diagnostic[];
}
interface VerificationPolicy {
readonly kind: "verification";
readonly id: string;
readonly project: ProjectMatcher;
readonly requirements: readonly VerificationRequirement[];
}
interface VerificationRequirement {
readonly id: string;
readonly command: string;
readonly cwd: "project-root";
affects(path: string): boolean;
}
type PersonalPolicy = DiagnosticPolicy | VerificationPolicy;
```
Freeze arrays/maps or expose readonly copies before calling policy modules. Do not expose the process runner, Pi API, session context, or mutable tracker through `PolicyContext`; this makes shared analyzer execution enforceable by construction. This limits what the runtime hands to a policy, but it does not sandbox top-level module code or imports.
## Tool execution wave and mutation lifecycle
Pi preflights parallel sibling calls in assistant source order and runs them concurrently, but sequential tools interleave start and completion. An executed call reaches `tool_result` before `tool_execution_end`; a blocked, invalid, or otherwise immediate call can reach `tool_execution_end` without any `tool_result`. Final tool-result messages are emitted later in assistant source order. The extension must coordinate overlapping execution waves rather than assume one batch per turn or one `tool_result` per start.
1. On `turn_start`, reset turn-local ordering metadata without creating a barrier for the whole turn.
2. On each `tool_execution_start`, add `toolCallId`, tool name, input, and source-order position to the current overlapping wave. If no call remains open, begin a new wave. This naturally treats each sequential execution as its own wave while grouping parallel siblings.
3. Before the first potentially mutating call in a wave executes, capture one workspace baseline. Known read-only built-ins can skip this work; `write`, `edit`, `bash`, and unknown custom tools are potentially mutating. The extension's status and waiver tools are explicitly workspace-read-only.
4. On `tool_result`, record final input, content, details, and `isError`, then mark that call terminal for barrier purposes before waiting. A failed command may have partially mutated files, so failure does not skip comparison.
5. On `tool_execution_end`, mark a call terminal only when no preceding `tool_result` did so. This accounts for blocked and argument-invalid calls without waiting for an event Pi will never emit.
6. When every call in the overlapping wave is terminal, compute one post-wave workspace snapshot. A bounded fallback releases waiters if event accounting becomes inconsistent.
7. If fingerprints differ, calculate changed paths and advance the workspace revision once. For each verification requirement whose `affects(path)` predicate matches, advance its required revision and invalidate its evidence or waiver.
8. Build analyzer requests required by applicable policies. Coalesce requests by `analyzerKey`, union and sort paths, and invoke each key once. ast-grep is the only implemented key in the MVP.
9. Fan immutable snapshots to policy evaluators. Deduplicate and format their diagnostics.
10. Attach one combined diagnostic block to a deterministic carrier with a real `tool_result`, preferably the last source-order potentially mutating result. Fall back to the last source-order participating result when the preferred call completed immediately. Preserve original `content`, `details`, `isError`, and `usage`.
11. Release every waiting handler. Use `turn_end` for cleanup and inconsistency detection, not as the normal analysis point.
An extension defect must not deadlock tool delivery. On timeout or inconsistent events, release handlers, surface a coordinator failure, and conservatively mark armed verification state unknown.
A verification command running in a wave with any other potentially mutating call must not count, even if the final workspace fingerprint is unchanged. The check may have raced a transient mutation and must be rerun in a later stable wave.
## Workspace snapshots
Use repository state, not shell-command regexes, as the primary mutation signal.
### Git workspaces
Capture a deterministic, NUL-safe map for tracked changes, staged changes, deletions, renames, symlinks, submodule markers, and untracked non-ignored files. Include the HEAD object ID and index state. Hash current content only for dirty paths relevant to at least one active policy so a second edit to an already-dirty file changes the fingerprint without hashing the clean repository. If HEAD changes, derive affected paths between the old and new commits. Compare before and after maps to derive paths changed by the execution wave.
Use Git plumbing/status commands through `pi.exec` with argv arrays, timeout, and `ctx.signal`, then strictly parse and cap their results. Ignored build artifacts such as `node_modules` and normal coverage output do not invalidate evidence unless a verification policy explicitly marks them relevant.
### Non-Git or failed snapshots
For successful `write`/`edit`, fall back to their explicit target paths. For `bash` or unknown tools in a non-Git workspace, exact changed paths cannot be proven cheaply; advance armed requirements to `unknown` and fail verification closed. Diagnostics may report that no safe path set was available rather than scanning the entire directory.
### Out-of-band changes
On a new branch state with no native-hooks history, capture the current workspace as an unarmed baseline even when it is already dirty. On restored armed state, compare the persisted project identity and fingerprint during `session_start`; also refresh at `agent_settled`. A later mismatch advances affected requirement revisions before completion gating. This catches user shell commands, editor changes, and background writers without forcing verification in a question-only session merely because files were dirty before it began.
## Analyzer coordination and performance
### One-run invariant
For one mutation execution wave, the analyzer coordinator may execute a given `analyzerKey` zero or one time. Multiple policies, languages, or findings do not create additional processes. Tests must inject a counting fake adapter and prove one call when both initial TypeScript policies apply, including parallel writes to several files.
An analyzer key includes the adapter and effective configuration. If future hooks genuinely require incompatible lint configurations, they use different keys and therefore separate documented invocations.
### ast-grep adapter
Invoke the installed executable once with all applicable rule files and changed paths, using project configuration and JSON stream output, conceptually:
```text
ast-grep scan --config ~/.config/pi-native-hooks/sgconfig.yml \
--json=stream --color=never --max-results <bound> -- <paths...>
```
Normalize JSON into rule ID, severity, message, note, path, source range, and matched text hash. Convert ast-grep's zero-based lines and columns at the rendering seam. Validate every decoded record rather than casting untrusted process output. Bound runtime, stdout, file count, finding count, and rendered diagnostics.
Keep steering rules at warning, info, or hint severity so a match does not become a process failure; the authoring skill rejects error-severity steering rules. Resolve and version-check `ast-grep` lazily once per session. A missing, incompatible, timed-out, nonzero, malformed, or killed analyzer produces an `AnalyzerFailure`; it never produces a false clean snapshot.
### Initial personal syntax policies
The initializer creates these policies and their ast-grep rules only after an approved dry run. The runtime itself ships contract fixtures and examples, not the user's live policy files. Both policies request the shared `ast-grep:personal` analyzer key and filter their own rule IDs from the same immutable snapshot.
1. **`typescript-no-non-null-assertion`**
- Match TypeScript/TSX `non_null_expression` nodes.
- Guidance: prefer a guard, refinement, assertion function, or corrected type.
- Exception: use a rule-specific `ast-grep-ignore` comment with a short rationale when an invariant is intentional.
2. **`typescript-no-dynamic-import`**
- Match dynamic `import(...)` call expressions in TypeScript/TSX.
- Guidance: prefer a static import unless lazy loading, code splitting, an optional dependency, or runtime module selection is intentional.
- Exception: use the same rule-specific suppression with a rationale.
Use separate TypeScript and TSX ast-grep rule definitions where the analyzer requires separate language declarations, but map those rule IDs to one user-facing policy ID. ast-grep's supported form allows a trailing rationale, for example:
```ts
const module = await import(path); // ast-grep-ignore: typescript-no-dynamic-import-ts -- runtime-selected plugin
```
Do not add regex-based parsing of TypeScript syntax. A later policy may validate suppression rationale formatting, but that is not required for the first slice.
### Diagnostic deduplication
Fingerprint a finding from policy ID, analyzer rule ID, canonical path, normalized range, and matched-text hash. Keep the last delivered set per file/content fingerprint. Deliver new or changed findings, drop resolved fingerprints, and avoid repeating an unchanged finding after unrelated tool calls. Cap rendered findings and include a count of omitted items.
## Verification model
### Initial personal verification policies
The authorized initializer creates two verification policy modules:
1. **`pi-native-hooks-verification`** matches Git root plus `package.json` name `pi-native-hooks`.
2. **`harness-tools-verification`** matches Git root plus `package.json` name `harness-tools`.
Both initially require these standalone commands from the matched project root:
```text
npm test
npm run typecheck
```
Each requirement owns a pure `affects(path)` predicate instead of introducing a glob dependency. The `pi-native-hooks` policy arms tests for behavior-bearing changes under `extensions/`, `src/`, `tests/`, `examples/`, and `skills/`, plus package and TypeScript manifests; its typecheck requirement covers TypeScript and package/TypeScript manifests. The harness-tools policy retains the repository-specific `extensions/`, `scripts/`, `services/`, `skills/`, `config/`, and manifest predicates documented in its module. Documentation-only changes outside each policy's declared surfaces do not stale evidence.
Trim command leading/trailing whitespace only. Do not parse shell text or accept `npm test -- ...`, `npm test && npm run typecheck`, aliases, focused scripts, or commands from another working directory. Additional exact forms require an explicit personal policy.
### Evidence lifecycle
Maintain one monotonic workspace revision. Each requirement stores its own `requiredRevision`, which remains absent until an observed path first satisfies its `affects(path)` predicate. Irrelevant later changes may advance the workspace revision without invalidating that requirement.
Each requirement is one of:
- `inactive`: this branch has not observed a relevant mutation;
- `not-run`: armed, with no evidence for its required revision;
- `passed`: the exact command fully succeeded at or after its required revision;
- `failed`: the exact command completed unsuccessfully;
- `stale`: older success exists but a later relevant mutation advanced the required revision;
- `waived`: the agent recorded a reason for this required revision; or
- `unknown`: armed workspace or event state could not be proven.
A requirement becomes valid only when all of these hold:
- it is armed;
- command and `ctx.cwd` exactly match its configured command and repository root;
- execution begins at or after the latest required revision;
- no other potentially mutating call shares its overlapping execution wave;
- no relevant mutation occurs before completion;
- Pi emits a finalized tool result with `event.isError === false`; and
- the evidence records the same project identity and required revision.
Output truncation does not imply partial execution; Pi finalizes the process before emitting the result. Normal nonzero exits, Esc aborts, and timeouts produce `isError: true` and do not count. Pi's public bash result omits the numeric exit code, so the MVP documents one narrow limitation: if the shell process itself ends through an external signal and Pi exposes a successful result, the extension cannot independently disprove completion. This is an operational completion gate, not a security attestation.
### Foreground `bash`
Capture the original command and `ctx.cwd` by `toolCallId`, then compare the finalized input after other extension handlers have run. `event.isError === false` is the success signal exposed by Pi's public finalized bash result. Record evidence only after execution-wave workspace comparison proves no relevant mutation and no potentially mutating sibling raced the check.
## Completion gate and waiver
### Forced follow-up
At every `agent_settled`:
1. refresh workspace state and advance affected requirement revisions if it changed;
2. evaluate only armed verification requirements;
3. update the TUI status; and
4. if any armed requirement is not passed or currently waived, send one custom `pi-native-hooks-verification` message with `{ deliverAs: "followUp", triggerTurn: true }`.
The message lists exact outstanding commands and states that the agent must run them or use the waiver tool with a reason. A per-settlement delivery guard prevents duplicates from concurrent callbacks, but a later settlement with the same unresolved revisions sends another reminder. That repeated behavior is intentional: otherwise Pi could settle on the second attempt without evidence.
A `context` handler removes older `pi-native-hooks-verification` messages from the model-facing copy, retaining only the latest live reminder while the gate is pending and none after it is satisfied. Session history remains auditable without allowing repeated forced turns to accumulate stale guidance in model context.
Pi has no literal task-acceptance event. This mechanism prevents the normal agent loop from remaining idle, but it cannot stop a user from closing Pi or an external client from discarding the session. Document that limitation plainly.
The gate may also fire when an agent settles after asking a question rather than claiming completion. The chosen agent-callable waiver is the escape hatch for intentionally deferred verification.
### Waiver tool
Register `pi_hooks_waive_verification` with one required `reason` string. Runtime validation trims and rejects an empty reason.
The tool:
- lists the exact outstanding requirement IDs it waives;
- records reason, timestamp, project identity, requirement IDs/revisions, and tool call in durable extension state;
- returns a visible summary that verification was skipped rather than passed;
- rejects a no-op request when no armed requirement is outstanding;
- clears the completion gate only for those requirement revisions; and
- becomes stale after the next relevant mutation.
A waiver never creates test evidence and must remain distinguishable in `/hooks-status` and session restoration. Any later path matching the requirement's `affects(path)` predicate advances its required revision and expires that waiver.
## State, observability, and failure policy
### Persistence
Append versioned `pi-native-hooks-state-v1` snapshots for meaningful transitions: policy registry digest, workspace/required revision changes, accepted evidence, failures, waivers, and coordinator uncertainty. Restore the latest valid snapshot from `ctx.sessionManager.getBranch()` on `session_start`, then compare policy digest, project identity, and live workspace fingerprint before trusting it. Reconstruct again on `session_tree` so branch navigation cannot inherit state from an abandoned branch. A previously armed policy that is now missing or fails to load remains `unknown` rather than silently clearing its gate.
Avoid persisting every analyzer finding or reminder. Diagnostic deduplication may be session-local; verification and waiver state must be durable and branch-aware.
### UI and commands
- Set a compact `pi-native-hooks` status showing diagnostic count and verification summary.
- Add `/hooks-status` to show config root health, loaded policy paths/digests, load failures, project identity, workspace revision, analyzer timing, inactive or pending checks, required revisions, last success, active waiver, and last failure.
- Keep model-visible diagnostics concise. Put verbose operational detail in the status command or TUI notification.
- Clear status and release pending barriers during `session_shutdown`.
### Failure policy
| Failure | Behavior |
|---|---|
| Config root absent with no restored armed state | Load an empty inactive registry, show initialization guidance, and do not create files automatically. |
| Config ownership/mode check fails | Import no affected file, identify the unsafe path, and keep previously armed policy state unknown. |
| New policy throws or exports an invalid value | Skip that policy, report its file, and continue valid siblings. |
| Previously armed policy is missing or fails to load | Preserve its outstanding requirement state as unknown until restored or waived; never silently accept completion. |
| ast-grep missing, incompatible, timed out, or malformed | Preserve tool success, append one analyzer warning, show degraded status, and do not claim a clean scan. |
| Policy evaluator throws | Isolate that policy, report its ID, continue siblings, and mark its evaluation degraded. |
| Workspace snapshot cannot prove armed state | Release tool results, mark affected requirements `unknown`, and force follow-up until rerun or waiver. |
| Persistence entry malformed | Ignore that entry, use the latest earlier valid state if available, and surface a warning. |
| Barrier inconsistency or timeout | Release waiters, avoid result corruption, mark verification unknown, and surface coordinator failure. |
| Result augmentation fails | Return the original result unchanged and notify the UI; never convert a successful mutation into a tool error. |
## Proposed repository layout
```text
pi-native-hooks/
├── package.json
├── tsconfig.json
├── README.md
├── extensions/
│ └── pi-native-hooks/
│ └── index.ts
├── src/
│ ├── runtime.ts
│ ├── policy.ts
│ ├── policy-loader.ts
│ ├── analysis.ts
│ ├── execution-wave.ts
│ ├── diagnostics.ts
│ ├── project-context.ts
│ ├── state.ts
│ ├── verification.ts
│ ├── workspace.ts
│ └── analyzers/
│ └── ast-grep.ts
├── tests/
│ ├── runtime.test.ts
│ ├── policy-loader.test.ts
│ ├── execution-wave.test.ts
│ ├── ast-grep.test.ts
│ ├── verification.test.ts
│ └── fixtures/
│ └── personal-config/
├── examples/
│ └── policies/
│ ├── typescript-no-non-null-assertion.ts
│ ├── typescript-no-dynamic-import.ts
│ ├── pi-native-hooks-verification.ts
│ └── harness-tools-verification.ts
├── skills/
│ └── native-hook-authoring/
│ ├── SKILL.md
│ └── references/
│ ├── contracts.md
│ └── testing.md
└── docs/
├── implementation-plan.md
└── adr/
```
Prefer colocated focused tests when a module has substantial behavior. The exact split may be simplified during implementation if files remain single-purpose.
## Delivery slices
After the planning baseline is committed, use a new worktree for implementation. Assign ownership before parallel work. Shared runtime, package manifest, and policy contract files belong to the integration owner; policy authors own only example/fixture policy and rule files. No slice may write `~/.config/pi-native-hooks` or install the Pi package until the explicit activation step in Slice 4.
### Slice 0: standalone package and trusted policy-loader proof
**Goal:** prove the repository/package shape and executable TypeScript policy contract before building the event runtime.
**Work:**
- Add the ESM package manifest, Node `>=22.19` engine, Pi extension/skill declarations, strict TypeScript configuration, and focused test command.
- Define the small `PersonalPolicy` interface and runtime structural validators.
- Implement the fixed-root policy loader behind an injected filesystem/import adapter.
- Prove native dynamic import of erasable `.ts`, type-only imports, deterministic ordering, cache-busted reload, duplicate IDs, top-level throws, invalid exports, ownership/mode rejection, and symlink policy.
- Add checked-in example policies and a fixture personal-config tree. Do not create the live config directory.
- Record the standalone package and trusted-code decision in `docs/adr/`.
**Success criteria:**
- `npm run typecheck` and focused loader tests fully execute.
- Tests run against an isolated fake HOME and never inspect or mutate the real personal config.
- One invalid policy does not hide valid siblings, and every failure identifies its path.
- The loader executes no repository-local policy and accepts no environment override for the config root.
- The Node-native loader limitations are documented and proven on the minimum supported runtime before avoiding a loader dependency.
### Slice 1: execution-wave runtime and shared analyzer seam
**Goal:** establish Pi event and one-run analyzer invariants with fakes.
**Work:**
- Add a fake Pi event harness, execution-wave coordinator, and injected fake workspace/analyzer adapters.
- Model parallel source-order starts, reverse-completion results, sequential waves, and immediate `tool_execution_end` without `tool_result`.
- Implement request coalescing, immutable snapshot fan-out, deterministic result carrier selection, barrier timeout, context-reminder pruning, and shutdown cleanup.
**Success criteria:**
- Two parallel mutations and two interested policies call one fake analyzer exactly once.
- Out-of-order results produce one workspace revision and one diagnostic augmentation.
- A blocked sibling with no `tool_result` cannot deadlock the wave.
- Sequential mutators form separate waves without waiting for future starts.
- Read-only waves invoke no workspace snapshot or analyzer.
- Failure/timeout releases every handler and leaves armed verification unknown rather than deadlocking.
### Slice 2: Git workspace, ast-grep adapter, and diagnostic examples
**Goal:** prove real mutation detection and contextual TypeScript steering.
**Work:**
- Implement Git workspace maps/fingerprints and changed-path calculation.
- Implement the one-process `ast-grep:personal` adapter against the personal `sgconfig.yml` and strict JSON normalization.
- Add TypeScript/TSX example rules and policies for non-null assertions and dynamic imports.
- Add deduplication, bounded rendering, analyzer health/status, and `/hooks-status` policy diagnostics.
- Validate `ast-grep-ignore: <rule-id> -- <reason>` exceptions.
**Success criteria:**
- Real fixture scans detect both patterns and suppress only the named rule.
- One wave touching several TS/TSX files and both policies starts one ast-grep process.
- `write`, `edit`, successful/failed `bash`, untracked, staged, deleted, renamed, symlinked, and already-dirty files change the correct fingerprint.
- Unchanged findings are not repeated after unrelated waves.
- Missing, incompatible, nonzero, timed-out, or malformed ast-grep is visible and never reported as a clean scan.
### Slice 3: verification evidence and settlement gate
**Goal:** keep Pi active until exact current evidence or a current waiver exists.
**Work:**
- Implement project matching, pure path predicates, observed-mutation activation, and example policies for `pi-native-hooks` and `harness-tools`.
- Track exact foreground bash attempts and Pi's finalized success state.
- Persist policy digest plus branch-aware workspace/requirement revisions, evidence, failures, and waivers.
- Handle changed, missing, or failed previously-armed policies without silently accepting completion.
- Implement settlement refresh, forced follow-up delivery, context pruning, status, and waiver tool.
**Success criteria:**
- A new state-free session remains unarmed even when relevant files were already dirty at startup.
- Exact standalone checks satisfy only their matching armed project requirements.
- Focused, altered, combined, failed, aborted, timed-out, wrong-cwd, concurrent, and stale commands do not satisfy the gate.
- A potentially mutating sibling invalidates a racing check even when final content is unchanged.
- `agent_settled` sends one follow-up per pending settlement and none after current evidence or waiver.
- A non-empty waiver is visible, branch-aware, revision-scoped, and invalidated by the next relevant mutation.
- Reload, branch navigation, policy digest change, and out-of-band edits cannot revive stale evidence.
### Slice 4: authoring skill, activation, and independent review
**Goal:** validate authoring and real Pi behavior before normal use.
**Work:**
- Create the package-managed `native-hook-authoring` skill with progressive disclosure for policy creation, revision, validation, and troubleshooting.
- Add `/hooks-init --dry-run` and `/hooks-doctor`; any command that writes personal config requires explicit interactive approval and preserves existing files.
- Run an extension integration scenario with parallel writes, one ast-grep run, diagnostics, stale verification, forced follow-up, successful checks, invalidation, and waiver.
- Run unfiltered `npm run typecheck` and `npm test`.
- Have an agent other than the implementer review standards, trust handling, event races, failure behavior, and this approved plan.
- Only after review and explicit user approval, install the local-path Pi package, initialize selected personal policies, run `/hooks-doctor`, and `/reload`.
**Success criteria:**
- The skill points to the package's policy contract rather than duplicating it.
- A fresh agent can create a fixture policy without adding analyzer execution to its evaluator.
- Dry runs enumerate every external file and package-setting change before mutation.
- Existing personal config is never overwritten; partial initialization rolls back newly created files.
- Tests record actual analyzer invocation counts and full command completion.
- Independent review has no unresolved blockers.
- The activated extension reports exactly one runtime, all intended personal policy digests, and a healthy analyzer after `/reload`.
## Focused test matrix
| Area | Required cases |
|---|---|
| Policy loader | fixed root; native TS; type-only import; sorted files; duplicate IDs; invalid export; top-level throw; owner/mode; symlink; cache-busted reload; changed digest |
| Wave ordering | one/many siblings; source-order starts; reverse completion; sequential waves; immediate end without result; read-only; failed partial mutation; missing terminal timeout; cleanup |
| Analyzer coalescing | two policies same key; many paths; no applicable paths; different future keys; adapter throw/timeout/malformed output |
| ast-grep | TS and TSX positives; static import negative; postfix `!` positive; logical `!` negative; justified suppression; Unicode ranges; result cap |
| Workspace | clean/dirty/staged/untracked/ignored/deleted/renamed/symlinked/binary/already-dirty; non-Git fallback; external edit |
| Delivery | original result fields preserved; one carrier; dedupe; resolved finding; truncation; analyzer warning |
| Foreground evidence | exact success; nonzero; abort; timeout; wrong cwd; focused flag; combined command; mutating sibling with/without net diff; documented external-signal limitation |
| Gate | pre-existing dirty baseline inactive; observed relevant mutation armed; irrelevant mutation unchanged; partial pass; failed; stale; unknown; repeated settlements; no duplicate within settlement; idle follow-up; context pruning |
| Waiver | empty rejection; no-op rejection; scoped requirement revisions; branch persistence; status visibility; later relevant invalidation |
| Session | startup without state; valid active-branch restore; malformed/latest-invalid entry; fork/reload/session-tree; project identity mismatch; fingerprint drift |
| Activation | Pi package manifest; local-path dry-run/install; fake HOME initialization; existing config preservation; partial rollback; doctor; exactly one runtime after reload |
## Performance and safety acceptance
- No analyzer process for a read-only execution wave or when no changed path is relevant.
- At most one invocation per analyzer key per overlapping execution wave, enforced by tests.
- Git work is proportional to dirty/untracked paths, not every clean tracked file.
- Analyzer and snapshot commands have abort signals, timeouts, and output/result caps.
- Policy evaluation exceptions cannot prevent sibling policies or original tool-result delivery.
- Policy modules receive immutable analysis data, but their top-level code remains explicitly trusted and unsandboxed inside Pi's outer sandbox.
- Diagnostics never change a successful tool result into an error.
- Verification never becomes current from incomplete, ambiguous, concurrent, or stale execution.
## Documentation and review artifacts
After approval, implementation should add:
- `docs/adr/0001-standalone-runtime-and-trusted-policies-27AUG2026.md` for repository ownership, native policy loading, shared analyzers, and the completion gate;
- concise user documentation for installation, config trust, supported TypeScript syntax, status, waiver semantics, exact commands, exceptions, and known completion limitations;
- the `native-hook-authoring` skill; and
- a repository-local `docs/todos.md` entry for any reproducible defect discovered while testing the extension on itself.
## Planning validation completed
- Pi 0.84.3 documentation and installed source confirm parallel preflight ordering, concurrent execution, `tool_result` result augmentation, source-order final result messages, and `agent_settled` custom-message follow-ups.
- Installed Pi source also confirms the execution-wave correction: blocked or argument-invalid calls can emit `tool_execution_end` without invoking `tool_result`, while sequential tools do not expose future starts before the current result finishes.
- A throwaway ast-grep 0.45.1 fixture detected both initial TypeScript patterns in one `scan --json=stream` invocation, did not confuse logical negation with a non-null expression, and honored a rule-specific suppression with a trailing rationale.
- The ast-grep fixture confirmed warning findings return exit code zero and JSON source ranges are zero-based, both now reflected in the adapter plan.
- Node 26.7 successfully imported an erasable `.ts` policy containing interfaces, `satisfies`, and a type-only import to an unavailable runtime package. Phase 0 still must repeat the contract on the minimum supported Node 22.19 runtime.
- Planning originally created only this repository's README and documentation. Implementation subsequently added the runtime, tests, examples, and authoring skill. No live personal policy, Pi package setting, or global installation is changed without the separate activation approval.
Independent agent review is deferred because direct subagent launches are currently being repaired. It remains the first execution prerequisite when implementation starts from this repository.
## Approval gate
The user accepted these consequences on 27 August 2026:
1. Every personal `.ts` policy is trusted arbitrary code running in Pi's process; structural validation does not sandbox imports or top-level side effects, and a nonconforming policy can bypass the toolkit's one-run analyzer coordinator.
2. The MVP supports only Node's erasable TypeScript subset for personal modules and reloads policies on `/reload`, not continuously.
3. Pending verification forces a new turn at every settlement, including settlements after the agent asks a question.
4. A state-free session treats pre-existing dirty files as an unarmed baseline; only observed relevant mutations or restored armed state require checks.
5. The agent can waive its own verification requirement, but the reason remains visible and the waiver expires on a relevant mutation.
6. Exact command matching intentionally rejects convenient combined or focused commands.
7. Foreground success trusts Pi's public `isError` state and carries the documented external-signal ambiguity.
8. ast-grep failure degrades steering but does not block file mutation; armed workspace uncertainty does block verification completion.
9. Pi's settlement gate cannot prevent a user or external client from closing the session.
10. Local-path package installation and creation of `~/.config/pi-native-hooks` are separate external mutations requiring approval after tests and review.
User approval is recorded. An independent pre-implementation review was completed before runtime changes; its blockers were addressed by `docs/event-contract.md`, the explicit project identity schema, fail-fast native TypeScript loading, and canonical repository paths.
@@ -0,0 +1,13 @@
# Independent implementation review — 27 August 2026
A separate Pi agent reviewed the implementation after the full test and typecheck run.
## Findings and resolution
- **Tool-result patch flow:** reviewer suspected a wave map was returned to Pi. Reinspection and `tests/runtime.test.ts` confirm `ExecutionWaveCoordinator.result()` selects `.get(toolCallId)` and the runtime returns that per-call patch. No defect remained.
- **typebox packaging:** retained as a peer dependency, as required by Pi's package documentation for bundled core packages; it is also a development dependency for standalone typechecking.
- **Initializer path mapping:** fixed. The shipped template contains a placeholder and `/hooks-init --apply` substitutes the installed package root rather than copying a machine-specific path.
- **Equivalent project-root paths:** normalized with `path.resolve` before exact cwd comparison. Subdirectories remain intentionally invalid.
- **Runtime integration coverage:** added a focused registration and per-call tool-result augmentation test.
The reviewer also confirmed the unfiltered test and typecheck commands passed. No unresolved review blocker remains. Activation and creation of live personal configuration remain separate user-authorized steps.
+4
View File
@@ -0,0 +1,4 @@
# Todos
- Validate the native TypeScript loader contract on Node 22.19 in CI (development host currently uses Node 26).
- Consider a user-configurable read-only custom-tool allowlist after MVP evidence is collected.
@@ -0,0 +1,6 @@
id: typescript-no-dynamic-import-ts
language: TypeScript
severity: warning
message: Avoid dynamic imports unless runtime loading is intentional.
rule:
pattern: import($MODULE)
@@ -0,0 +1,6 @@
id: typescript-no-dynamic-import-tsx
language: Tsx
severity: warning
message: Avoid dynamic imports unless runtime loading is intentional.
rule:
pattern: import($MODULE)
@@ -0,0 +1,6 @@
id: typescript-no-non-null-assertion-ts
language: TypeScript
severity: warning
message: Avoid TypeScript non-null assertions.
rule:
kind: non_null_expression
@@ -0,0 +1,6 @@
id: typescript-no-non-null-assertion-tsx
language: Tsx
severity: warning
message: Avoid TypeScript non-null assertions.
rule:
kind: non_null_expression
@@ -0,0 +1,8 @@
import type { VerificationPolicy } from "@_jb/pi-native-hooks/policy";
const manifests = new Set(["package.json", "bun.lock", "tsconfig.json"]);
const behavior = (path: string) => /^(extensions|scripts|services|skills|config)\//.test(path) || manifests.has(path);
const types = (path: string) => /\.tsx?$/.test(path) || manifests.has(path);
export default { kind: "verification", id: "harness-tools-verification", project: { matches: (project) => project.packageName === "harness-tools" }, requirements: [
{ id: "tests", command: "bun run test", cwd: "project-root", affects: behavior },
{ id: "typecheck", command: "bun run typecheck", cwd: "project-root", affects: types },
] } satisfies VerificationPolicy;
@@ -0,0 +1,8 @@
import type { VerificationPolicy } from "@_jb/pi-native-hooks/policy";
const manifests = new Set(["package.json", "bun.lock", "tsconfig.json"]);
const behavior = (path: string) => /^(extensions|src|tests|examples|skills)\//.test(path) || manifests.has(path);
const types = (path: string) => /\.tsx?$/.test(path) || manifests.has(path);
export default { kind: "verification", id: "pi-native-hooks-verification", project: { matches: (project) => project.packageName === "@_jb/pi-native-hooks" }, requirements: [
{ id: "tests", command: "bun run test", cwd: "project-root", affects: behavior },
{ id: "typecheck", command: "bun run typecheck", cwd: "project-root", affects: types },
] } satisfies VerificationPolicy;
@@ -0,0 +1,7 @@
import type { DiagnosticPolicy } from "@_jb/pi-native-hooks/policy";
const ruleIds = new Set(["typescript-no-dynamic-import-ts", "typescript-no-dynamic-import-tsx"]);
export default {
kind: "diagnostic", id: "typescript-no-dynamic-import", analyzerKeys: ["ast-grep:personal"],
appliesTo: (_project, paths) => paths.some((path) => /\.tsx?$/.test(path)),
evaluate: (analysis) => (analysis.get("ast-grep:personal")?.findings ?? []).filter((finding) => ruleIds.has(finding.ruleId)).map((finding) => ({ policyId: "typescript-no-dynamic-import", ruleId: finding.ruleId, path: finding.path, line: finding.range.start.line, column: finding.range.start.column, message: finding.message, guidance: "Prefer a static import unless lazy loading, code splitting, an optional dependency, or runtime module selection is intentional.", exception: `Use ast-grep-ignore: ${finding.ruleId} -- <short rationale> for an intentional dynamic import.`, matchedTextHash: finding.matchedTextHash })),
} satisfies DiagnosticPolicy;
@@ -0,0 +1,7 @@
import type { DiagnosticPolicy } from "@_jb/pi-native-hooks/policy";
const ruleIds = new Set(["typescript-no-non-null-assertion-ts", "typescript-no-non-null-assertion-tsx"]);
export default {
kind: "diagnostic", id: "typescript-no-non-null-assertion", analyzerKeys: ["ast-grep:personal"],
appliesTo: (_project, paths) => paths.some((path) => /\.tsx?$/.test(path)),
evaluate: (analysis) => (analysis.get("ast-grep:personal")?.findings ?? []).filter((finding) => ruleIds.has(finding.ruleId)).map((finding) => ({ policyId: "typescript-no-non-null-assertion", ruleId: finding.ruleId, path: finding.path, line: finding.range.start.line, column: finding.range.start.column, message: finding.message, guidance: "Prefer a guard, refinement, assertion function, or corrected type.", exception: `Use ast-grep-ignore: ${finding.ruleId} -- <short rationale> when the invariant is intentional.`, matchedTextHash: finding.matchedTextHash })),
} satisfies DiagnosticPolicy;
@@ -0,0 +1,2 @@
ruleDirs:
- ast-grep/rules
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"strict": true,
"target": "ES2023",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"allowImportingTsExtensions": true,
"paths": { "@_jb/pi-native-hooks/policy": ["__PI_NATIVE_HOOKS_PACKAGE_ROOT__/src/policy.ts"] }
},
"include": ["policies/*.ts"]
}
@@ -0,0 +1,8 @@
import type { VerificationPolicy } from "@_jb/pi-native-hooks/policy";
const manifests = new Set(["package.json", "bun.lock", "tsconfig.json"]);
const behavior = (path: string) => /^(extensions|scripts|services|skills|config)\//.test(path) || manifests.has(path);
const types = (path: string) => /\.tsx?$/.test(path) || manifests.has(path);
export default { kind: "verification", id: "harness-tools-verification", project: { matches: (project) => project.packageName === "harness-tools" }, requirements: [
{ id: "tests", command: "bun run test", cwd: "project-root", affects: behavior },
{ id: "typecheck", command: "bun run typecheck", cwd: "project-root", affects: types },
] } satisfies VerificationPolicy;
@@ -0,0 +1,8 @@
import type { VerificationPolicy } from "@_jb/pi-native-hooks/policy";
const manifests = new Set(["package.json", "bun.lock", "tsconfig.json"]);
const behavior = (path: string) => /^(extensions|src|tests|examples|skills)\//.test(path) || manifests.has(path);
const types = (path: string) => /\.tsx?$/.test(path) || manifests.has(path);
export default { kind: "verification", id: "pi-native-hooks-verification", project: { matches: (project) => project.packageName === "@_jb/pi-native-hooks" }, requirements: [
{ id: "tests", command: "bun run test", cwd: "project-root", affects: behavior },
{ id: "typecheck", command: "bun run typecheck", cwd: "project-root", affects: types },
] } satisfies VerificationPolicy;
@@ -0,0 +1,7 @@
import type { DiagnosticPolicy } from "@_jb/pi-native-hooks/policy";
const ruleIds = new Set(["typescript-no-dynamic-import-ts", "typescript-no-dynamic-import-tsx"]);
export default {
kind: "diagnostic", id: "typescript-no-dynamic-import", analyzerKeys: ["ast-grep:personal"],
appliesTo: (_project, paths) => paths.some((path) => /\.tsx?$/.test(path)),
evaluate: (analysis) => (analysis.get("ast-grep:personal")?.findings ?? []).filter((finding) => ruleIds.has(finding.ruleId)).map((finding) => ({ policyId: "typescript-no-dynamic-import", ruleId: finding.ruleId, path: finding.path, line: finding.range.start.line, column: finding.range.start.column, message: finding.message, guidance: "Prefer a static import unless lazy loading, code splitting, an optional dependency, or runtime module selection is intentional.", exception: `Use ast-grep-ignore: ${finding.ruleId} -- <short rationale> for an intentional dynamic import.`, matchedTextHash: finding.matchedTextHash })),
} satisfies DiagnosticPolicy;
@@ -0,0 +1,7 @@
import type { DiagnosticPolicy } from "@_jb/pi-native-hooks/policy";
const ruleIds = new Set(["typescript-no-non-null-assertion-ts", "typescript-no-non-null-assertion-tsx"]);
export default {
kind: "diagnostic", id: "typescript-no-non-null-assertion", analyzerKeys: ["ast-grep:personal"],
appliesTo: (_project, paths) => paths.some((path) => /\.tsx?$/.test(path)),
evaluate: (analysis) => (analysis.get("ast-grep:personal")?.findings ?? []).filter((finding) => ruleIds.has(finding.ruleId)).map((finding) => ({ policyId: "typescript-no-non-null-assertion", ruleId: finding.ruleId, path: finding.path, line: finding.range.start.line, column: finding.range.start.column, message: finding.message, guidance: "Prefer a guard, refinement, assertion function, or corrected type.", exception: `Use ast-grep-ignore: ${finding.ruleId} -- <short rationale> when the invariant is intentional.`, matchedTextHash: finding.matchedTextHash })),
} satisfies DiagnosticPolicy;
@@ -0,0 +1,8 @@
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { registerInitializationCommands } from "../../src/initializer.ts";
import { NativeHooksRuntime } from "../../src/runtime.ts";
export default function piNativeHooks(pi: ExtensionAPI): void {
new NativeHooksRuntime(pi);
registerInitializationCommands(pi);
}
+69
View File
@@ -0,0 +1,69 @@
{
"name": "@_jb/pi-native-hooks",
"version": "0.1.0",
"description": "Native Pi hooks runtime for shared diagnostics and verification gates.",
"type": "module",
"license": "MIT",
"homepage": "https://git.jb.codes/jb/harness-tools/src/branch/main/extensions/native-hooks",
"repository": {
"type": "git",
"url": "git+ssh://git@git.jb.codes:7722/jb/harness-tools.git",
"directory": "extensions/native-hooks"
},
"bugs": {
"url": "https://git.jb.codes/jb/harness-tools/issues"
},
"engines": {
"node": ">=22.19.0"
},
"files": [
"extensions/**/*",
"src/**/*",
"skills/**/*",
"examples/**/*",
"docs/**/*",
"README.md",
"LICENSE"
],
"keywords": [
"pi-package",
"pi-extension",
"hooks",
"diagnostics",
"verification"
],
"exports": {
"./policy": {
"types": "./src/policy.ts",
"default": "./src/policy.ts"
}
},
"pi": {
"extensions": [
"./extensions/pi-native-hooks/index.ts"
],
"skills": [
"./skills/native-hook-authoring"
]
},
"scripts": {
"test": "node --import tsx --test --experimental-test-coverage tests/*.test.ts",
"typecheck": "tsc --noEmit",
"prepublishOnly": "bun run typecheck && bun run test"
},
"peerDependencies": {
"@earendil-works/pi-coding-agent": "catalog:pi-peer",
"typebox": "catalog:pi-peer"
},
"devDependencies": {
"@earendil-works/pi-coding-agent": "catalog:",
"@types/node": "catalog:",
"tsx": "catalog:",
"typebox": "catalog:",
"typescript": "catalog:"
},
"publishConfig": {
"access": "public",
"registry": "https://git.jb.codes/api/packages/jb/npm/"
}
}
@@ -0,0 +1,16 @@
---
name: native-hook-authoring
description: Create, revise, validate, or troubleshoot trusted personal pi-native-hooks diagnostic and verification policies and ast-grep fixtures. Use when adding focused post-mutation policies without duplicating lifecycle, process execution, state, or delivery logic.
compatibility: pi-native-hooks on Node >=22.19
---
# Native hook authoring
1. Read [contracts](references/contracts.md). Import policy contracts with `import type` only.
2. Keep evaluators pure. Request analyzer keys and consume immutable snapshots; never spawn analyzers, linters, LSPs, or shell processes from a policy.
3. Add or revise a fixture under the package's `tests/fixtures/personal-config`, not the live personal root.
4. For syntax steering, create warning/info/hint ast-grep rules with language-specific IDs and a rationale-bearing `ast-grep-ignore` example.
5. Run the focused fixture, `npm run typecheck`, and `npm test` as described in [testing](references/testing.md).
6. Preview personal installation with `/hooks-init --dry-run`. Never write `~/.config/pi-native-hooks` without explicit interactive authorization. Existing files must not be overwritten.
Personal modules are trusted arbitrary code with Pi's authority. Validation is not sandboxing. Use Node's erasable TypeScript subset only.
@@ -0,0 +1,16 @@
# Policy contracts
The authoritative declarations are exported by `@_jb/pi-native-hooks/policy` from `src/policy.ts`. Do not copy them into personal config.
A diagnostic policy declares `kind`, a unique `id`, analyzer keys, `appliesTo`, and a pure `evaluate`. `evaluate` receives readonly normalized snapshots and a process-free policy context. Filter findings by exact rule ID and return bounded guidance plus the supported suppression form.
A verification policy declares a project matcher and exact requirements. Each requirement owns an exact command, `cwd: "project-root"`, and a pure `affects(path)` predicate. Do not infer scripts or accept command variants.
Use:
```ts
import type { DiagnosticPolicy } from "@_jb/pi-native-hooks/policy";
export default { /* ... */ } satisfies DiagnosticPolicy;
```
No runtime import is needed. Enums, namespaces, parameter properties, JSX, and tsconfig transforms are unsupported in personal modules.
@@ -0,0 +1,16 @@
# Policy testing
Work only in repository fixtures until activation is explicitly approved.
1. Put policy fixtures in `tests/fixtures/personal-config/policies/`.
2. Put ast-grep rules below `tests/fixtures/personal-config/ast-grep/rules/` and keep severity below `error`.
3. Cover TS and TSX positives, nearby negatives, the exact named suppression with a `-- rationale`, Unicode ranges, and malformed analyzer output where relevant.
4. Assert multiple policies sharing one analyzer key cause one adapter invocation.
5. Run:
```bash
npm run typecheck
npm test
```
Use `/hooks-doctor` only after authorized initialization. Never make a policy evaluator invoke `ast-grep`, a linter, LSP, shell, or Pi API directly.
+21
View File
@@ -0,0 +1,21 @@
import type { AnalysisSnapshot, AnalyzerFailure } from "./policy.ts";
export interface AnalysisRequest { readonly analyzerKey: string; readonly paths: readonly string[] }
export interface AnalyzerAdapter { analyze(paths: readonly string[], workspaceRevision: number, signal?: AbortSignal): Promise<AnalysisSnapshot> }
export class AnalysisCoordinator {
constructor(private readonly adapters: ReadonlyMap<string, AnalyzerAdapter>) {}
async run(requests: readonly AnalysisRequest[], revision: number, signal?: AbortSignal): Promise<ReadonlyMap<string, AnalysisSnapshot>> {
const grouped = new Map<string, Set<string>>();
for (const request of requests) { const paths = grouped.get(request.analyzerKey) ?? new Set<string>(); for (const path of request.paths) paths.add(path); grouped.set(request.analyzerKey, paths); }
const entries = await Promise.all([...grouped].sort(([a], [b]) => a.localeCompare(b)).map(async ([key, paths]) => {
const adapter = this.adapters.get(key);
const sorted = [...paths].sort();
if (!adapter) return [key, failed(key, revision, sorted, { kind: "missing", message: `No analyzer registered for ${key}` })] as const;
try { return [key, deepFreeze(await adapter.analyze(sorted, revision, signal))] as const; }
catch (error) { return [key, failed(key, revision, sorted, { kind: "internal", message: error instanceof Error ? error.message : String(error) })] as const; }
}));
return readonlyMap(entries);
}
}
function failed(key: string, workspaceRevision: number, paths: readonly string[], failure: AnalyzerFailure): AnalysisSnapshot { return Object.freeze({ analyzerKey: key, workspaceRevision, paths: Object.freeze([...paths]), findings: Object.freeze([]), durationMs: 0, failure: Object.freeze(failure) }); }
export function deepFreeze<T>(value: T): T { if (value && typeof value === "object" && !Object.isFrozen(value)) { Object.freeze(value); for (const item of Object.values(value as Record<string, unknown>)) deepFreeze(item); } return value; }
export function readonlyMap<K, V>(entries: readonly (readonly [K, V])[]): ReadonlyMap<K, V> { const map = new Map(entries); Object.freeze(map); return new Proxy(map, { get(target, prop) { if (prop === "set" || prop === "delete" || prop === "clear") return undefined; const value = Reflect.get(target, prop, target) as unknown; return typeof value === "function" ? (value as Function).bind(target) : value; } }); }
@@ -0,0 +1,44 @@
import { createHash } from "node:crypto";
import { isAbsolute, relative, resolve } from "node:path";
import type { AnalyzerAdapter } from "../analysis.ts";
import type { AnalysisSnapshot, AnalyzerFailure, Finding, Severity } from "../policy.ts";
import type { Exec } from "../project-context.ts";
const MAX_PATHS = 200; const MAX_FINDINGS = 200; const MAX_STDOUT = 2_000_000;
export class AstGrepAdapter implements AnalyzerAdapter {
private versionChecked?: Promise<AnalyzerFailure | undefined>;
constructor(private readonly exec: Exec, private readonly root: string, private readonly configPath: string, private readonly executable = "ast-grep") {}
prepare(signal?: AbortSignal): Promise<AnalyzerFailure | undefined> { return this.versionChecked ??= this.checkVersion(signal); }
async analyze(paths: readonly string[], workspaceRevision: number, signal?: AbortSignal): Promise<AnalysisSnapshot> {
const started = performance.now(); const bounded = paths.slice(0, MAX_PATHS);
const versionFailure = await this.prepare(signal);
if (versionFailure) return failure("ast-grep:personal", workspaceRevision, bounded, started, versionFailure);
const scanPaths = bounded.map((path) => isAbsolute(path) ? path : resolve(this.root, path));
const args = ["scan", "--config", this.configPath, "--json=stream", "--color=never", "--max-results", String(MAX_FINDINGS), "--", ...scanPaths];
const result = await this.exec(this.executable, args, { timeout: 20_000, ...(signal ? { signal } : {}) });
if (result.killed) return failure("ast-grep:personal", workspaceRevision, bounded, started, { kind: "killed", message: "ast-grep was killed or timed out" });
if (result.code !== 0) return failure("ast-grep:personal", workspaceRevision, bounded, started, { kind: "nonzero", message: `ast-grep exited ${result.code}: ${result.stderr.slice(0, 500)}` });
if (Buffer.byteLength(result.stdout) > MAX_STDOUT) return failure("ast-grep:personal", workspaceRevision, bounded, started, { kind: "malformed", message: "ast-grep output exceeded 2MB" });
try {
const findings = result.stdout.split("\n").filter(Boolean).slice(0, MAX_FINDINGS).map((line) => normalize(JSON.parse(line), this.root));
return Object.freeze({ analyzerKey: "ast-grep:personal", workspaceRevision, paths: Object.freeze([...bounded]), findings: Object.freeze(findings), durationMs: Math.round(performance.now() - started) });
} catch (error) { return failure("ast-grep:personal", workspaceRevision, bounded, started, { kind: "malformed", message: error instanceof Error ? error.message : String(error) }); }
}
private async checkVersion(signal?: AbortSignal): Promise<AnalyzerFailure | undefined> {
const result = await this.exec(this.executable, ["--version"], { timeout: 5000, ...(signal ? { signal } : {}) });
if (result.code !== 0) return { kind: "missing", message: `ast-grep is unavailable: ${result.stderr || result.stdout}` };
const match = result.stdout.match(/(\d+)\.(\d+)\.(\d+)/); if (!match || Number(match[1]) < 0 || Number(match[2]) < 45) return { kind: "incompatible", message: `ast-grep >=0.45 is required; got ${result.stdout.trim()}` };
return undefined;
}
}
function normalize(value: unknown, root: string): Finding {
if (!value || typeof value !== "object") throw new Error("finding is not an object"); const row = value as Record<string, unknown>;
const range = row.range as Record<string, unknown>; const start = range?.start as Record<string, unknown>; const end = range?.end as Record<string, unknown>;
const text = typeof row.text === "string" ? row.text : ""; const rawPath = requiredString(row.file, "file"); const severity = row.severity;
if (!start || !end || !Number.isInteger(start.line) || !Number.isInteger(start.column) || !Number.isInteger(end.line) || !Number.isInteger(end.column)) throw new Error("finding has invalid range");
if (severity !== "hint" && severity !== "info" && severity !== "warning") throw new Error(`unsupported steering severity ${String(severity)}`);
const path = isAbsolute(rawPath) ? relative(root, rawPath) : relative(root, resolve(root, rawPath));
return Object.freeze({ ruleId: requiredString(row.ruleId, "ruleId"), severity: severity as Severity, message: requiredString(row.message, "message"), ...(typeof row.note === "string" ? { note: row.note } : {}), path, range: Object.freeze({ start: Object.freeze({ line: start.line as number, column: start.column as number }), end: Object.freeze({ line: end.line as number, column: end.column as number }) }), matchedTextHash: createHash("sha256").update(text).digest("hex") });
}
function requiredString(value: unknown, name: string): string { if (typeof value !== "string" || !value) throw new Error(`finding has invalid ${name}`); return value; }
function failure(key: string, revision: number, paths: readonly string[], started: number, analyzerFailure: AnalyzerFailure): AnalysisSnapshot { return Object.freeze({ analyzerKey: key, workspaceRevision: revision, paths: Object.freeze([...paths]), findings: Object.freeze([]), durationMs: Math.round(performance.now() - started), failure: Object.freeze(analyzerFailure) }); }
@@ -0,0 +1,21 @@
import { createHash } from "node:crypto";
import type { Diagnostic } from "./policy.ts";
export class DiagnosticDelivery {
private delivered = new Set<string>();
constructor(private readonly limit = 20) {}
select(diagnostics: readonly Diagnostic[]): readonly Diagnostic[] {
const current = new Set(diagnostics.map(fingerprint));
const fresh = diagnostics.filter((item) => !this.delivered.has(fingerprint(item))).slice(0, this.limit);
this.delivered = current;
return fresh;
}
clear(): void { this.delivered.clear(); }
}
export function fingerprint(diagnostic: Diagnostic): string { return createHash("sha256").update([diagnostic.policyId, diagnostic.ruleId, diagnostic.path, diagnostic.line, diagnostic.column, diagnostic.matchedTextHash].join("\0")).digest("hex"); }
export function renderDiagnostics(diagnostics: readonly Diagnostic[], total = diagnostics.length): string {
if (!diagnostics.length) return "";
const lines = ["\n\n[pi-native-hooks diagnostics]"];
for (const item of diagnostics) { lines.push(`- ${item.path}:${item.line + 1}:${item.column + 1} [${item.ruleId}] ${item.message}`); lines.push(` ${item.guidance}${item.exception ? ` Exception: ${item.exception}` : ""}`); }
if (total > diagnostics.length) lines.push(`- … ${total - diagnostics.length} additional finding(s) omitted`);
return lines.join("\n");
}
@@ -0,0 +1,30 @@
export interface ObservedToolResult { readonly toolCallId: string; readonly toolName: string; readonly input: Record<string, unknown>; readonly content: readonly unknown[]; readonly details: unknown; readonly isError: boolean; readonly usage?: unknown }
export interface WaveCall { readonly id: string; readonly toolName: string; readonly args: unknown; readonly sourceIndex: number; readonly potentiallyMutating: boolean; terminal: boolean; result?: ObservedToolResult }
export interface CompletedWave { readonly id: string; readonly turnIndex: number; readonly calls: readonly WaveCall[]; readonly carrierId?: string; readonly potentiallyMutating: boolean }
export type WavePatches = ReadonlyMap<string, { readonly content?: readonly unknown[]; readonly details?: unknown; readonly isError?: boolean; readonly usage?: unknown }>;
interface ActiveWave { id: string; turnIndex: number; calls: WaveCall[]; finalized: boolean; timer?: NodeJS.Timeout; promise: Promise<WavePatches>; resolve(value: WavePatches): void }
const READ_ONLY = new Set(["read", "grep", "find", "ls", "pi_hooks_waive_verification"]);
export class ExecutionWaveCoordinator {
private wave?: ActiveWave; private serial = 0; private turnIndex = 0; private stopped = false;
constructor(private readonly finalize: (wave: CompletedWave) => Promise<WavePatches>, private readonly onUncertain: (message: string) => void, private readonly timeoutMs = 30_000) {}
turnStarted(turnIndex: number): void { this.turnIndex = turnIndex; }
start(toolCallId: string, toolName: string, args: unknown): { waveId: string; isNew: boolean; potentiallyMutating: boolean } | undefined {
if (this.stopped) return undefined; const isNew = !this.wave || this.wave.calls.every((call) => call.terminal); if (isNew) this.wave = this.createWave();
const wave = this.wave;
if (!wave) return undefined;
if (wave.calls.some((call) => call.id === toolCallId)) return { waveId: wave.id, isNew: false, potentiallyMutating: !READ_ONLY.has(toolName) };
const potentiallyMutating = !READ_ONLY.has(toolName);
wave.calls.push({ id: toolCallId, toolName, args, sourceIndex: wave.calls.length, potentiallyMutating, terminal: false });
return { waveId: wave.id, isNew, potentiallyMutating };
}
async result(result: ObservedToolResult): Promise<{ readonly content?: readonly unknown[]; readonly details?: unknown; readonly isError?: boolean; readonly usage?: unknown } | undefined> {
const wave = this.wave; if (!wave || this.stopped) return undefined; const call = wave.calls.find((item) => item.id === result.toolCallId); if (!call) { this.onUncertain(`tool_result without start: ${result.toolCallId}`); return undefined; }
if (!call.result) call.result = result; call.terminal = true; this.maybeFinalize(wave); return (await wave.promise).get(result.toolCallId);
}
end(toolCallId: string): void { const wave = this.wave; if (!wave || this.stopped) return; const call = wave.calls.find((item) => item.id === toolCallId); if (!call) { this.onUncertain(`tool_execution_end without start: ${toolCallId}`); return; } if (!call.terminal) call.terminal = true; this.maybeFinalize(wave); }
turnEnded(): void { if (this.wave && !this.wave.finalized && this.wave.calls.some((call) => !call.terminal)) this.releaseUncertain(this.wave, "turn ended with unterminated tool calls"); }
shutdown(): void { this.stopped = true; if (this.wave && !this.wave.finalized) this.releaseUncertain(this.wave, "session shutdown"); }
private createWave(): ActiveWave { let resolve!: (value: WavePatches) => void; const promise = new Promise<WavePatches>((done) => { resolve = done; }); const wave: ActiveWave = { id: `wave-${++this.serial}`, turnIndex: this.turnIndex, calls: [], finalized: false, promise, resolve }; wave.timer = setTimeout(() => this.releaseUncertain(wave, "execution-wave barrier timeout"), this.timeoutMs); wave.timer.unref?.(); return wave; }
private maybeFinalize(wave: ActiveWave): void { if (wave.finalized || wave.calls.length === 0 || wave.calls.some((call) => !call.terminal)) return; wave.finalized = true; if (wave.timer) clearTimeout(wave.timer); const mutatingResults = wave.calls.filter((call) => call.potentiallyMutating && call.result); const participating = wave.calls.filter((call) => call.result); const carrier = (mutatingResults.at(-1) ?? participating.at(-1))?.id; const completed = Object.freeze({ id: wave.id, turnIndex: wave.turnIndex, calls: Object.freeze([...wave.calls]), ...(carrier ? { carrierId: carrier } : {}), potentiallyMutating: wave.calls.some((call) => call.potentiallyMutating) }); this.finalize(completed).then(wave.resolve, (error) => { this.onUncertain(`wave finalization failed: ${error instanceof Error ? error.message : String(error)}`); wave.resolve(new Map()); }); }
private releaseUncertain(wave: ActiveWave, message: string): void { if (wave.finalized) return; wave.finalized = true; if (wave.timer) clearTimeout(wave.timer); this.onUncertain(message); wave.resolve(new Map()); }
}
@@ -0,0 +1,21 @@
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { chmod, copyFile, mkdir, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
import { userInfo } from "node:os";
import { dirname, join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { loadPolicies, personalConfigRoot } from "./policy-loader.ts";
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const templateRoot = join(packageRoot, "examples", "personal-config");
async function filesUnder(root: string, prefix = ""): Promise<string[]> { const result: string[] = []; for (const entry of await readdir(join(root, prefix), { withFileTypes: true })) { const path = join(prefix, entry.name); if (entry.isDirectory()) result.push(...await filesUnder(root, path)); else if (entry.isFile()) result.push(path); } return result.sort(); }
export function registerInitializationCommands(pi: ExtensionAPI): void {
pi.registerCommand("hooks-init", { description: "Preview or initialize trusted personal native-hook policies", handler: async (args, ctx) => {
const root = personalConfigRoot(); const files = await filesUnder(templateRoot); const plan = [`Personal config root: ${root}`, "Trusted executable files to create (existing files are preserved):", ...files.map((path) => `- ${join(root, path)}`)].join("\n");
if (args.trim() === "--dry-run" || !args.trim()) { ctx.ui.notify(`${plan}\n\nRun /hooks-init --apply to request authorization.`, "info"); return; }
if (args.trim() !== "--apply") { ctx.ui.notify("Usage: /hooks-init --dry-run | --apply", "warning"); return; }
if (!ctx.hasUI || !await ctx.ui.confirm("Initialize pi-native-hooks?", `${plan}\n\nThese policies execute with Pi's authority. Continue?`)) { ctx.ui.notify("Initialization cancelled.", "info"); return; }
const created: string[] = [];
try { for (const path of files) { const target = join(root, path); try { await stat(target); continue; } catch {} await mkdir(dirname(target), { recursive: true, mode: 0o700 }); if (path === "tsconfig.json") { const template = await readFile(join(templateRoot, path), "utf8"); await writeFile(target, template.replaceAll("__PI_NATIVE_HOOKS_PACKAGE_ROOT__", packageRoot), { mode: 0o600 }); } else await copyFile(join(templateRoot, path), target); await chmod(target, 0o600); created.push(target); } ctx.ui.notify(`Created ${created.length} file(s); preserved ${files.length - created.length} existing file(s). Run /hooks-doctor then /reload.`, "info"); }
catch (error) { for (const path of created.reverse()) await rm(path, { force: true }).catch(() => {}); ctx.ui.notify(`Initialization failed and newly created files were rolled back: ${error instanceof Error ? error.message : String(error)}`, "error"); }
}});
pi.registerCommand("hooks-doctor", { description: "Check native-hooks config trust and ast-grep availability", handler: async (_args, ctx) => { const registry = await loadPolicies(); const version = await pi.exec("ast-grep", ["--version"], { timeout: 5000 }); const lines = [registry.trustedCodeWarning, `Config: ${registry.root}`, `Policies: ${registry.policies.length}`, `Failures: ${registry.failures.length}`, ...registry.failures.map((failure) => `- ${failure.path}: ${failure.error}`), `ast-grep: ${version.code === 0 ? version.stdout.trim() : `unavailable (${version.stderr.trim()})`}`, `OS user: ${userInfo().username} uid ${userInfo().uid}`]; ctx.ui.notify(lines.join("\n"), registry.failures.length || version.code !== 0 ? "warning" : "info"); }});
}

Some files were not shown because too many files have changed in this diff Show More