pi-agent-replicate 0.0.0-stage → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/AGENTS.md +27 -0
  2. package/CHANGELOG.md +147 -0
  3. package/CONTEXT.md +63 -0
  4. package/LICENSE +21 -0
  5. package/README.md +239 -2
  6. package/RELEASING.md +130 -0
  7. package/assets/banner.png +0 -0
  8. package/config.json.example +6 -0
  9. package/docs/README.md +20 -0
  10. package/docs/adr/0003-installable-role-packs.md +29 -0
  11. package/docs/adr/0008-adopt-pi-only-subagent-execution.md +22 -0
  12. package/docs/adr/0009-durable-recursive-subagent-ownership.md +23 -0
  13. package/docs/adr/0010-public-subagent-tree-registry.md +16 -0
  14. package/docs/public-subagent-tree-api.md +58 -0
  15. package/docs/settled-lifecycle-contract.md +71 -0
  16. package/docs/worktree-subagents.md +58 -0
  17. package/examples/role-pack/extension.ts +18 -0
  18. package/examples/role-pack/package.json +16 -0
  19. package/examples/role-pack/roles/example-reviewer.md +12 -0
  20. package/index.d.ts +116 -0
  21. package/index.js +1064 -0
  22. package/index.ts +155 -0
  23. package/package.json +70 -4
  24. package/pi-extension/subagents/activity.js +575 -0
  25. package/pi-extension/subagents/activity.ts +980 -0
  26. package/pi-extension/subagents/completion.js +184 -0
  27. package/pi-extension/subagents/completion.ts +229 -0
  28. package/pi-extension/subagents/delivery-drain.js +15 -0
  29. package/pi-extension/subagents/delivery-drain.ts +20 -0
  30. package/pi-extension/subagents/delivery-files.js +86 -0
  31. package/pi-extension/subagents/delivery-files.ts +105 -0
  32. package/pi-extension/subagents/delivery-log.js +51 -0
  33. package/pi-extension/subagents/delivery-log.ts +51 -0
  34. package/pi-extension/subagents/delivery-payload.ts +55 -0
  35. package/pi-extension/subagents/delivery.ts +219 -0
  36. package/pi-extension/subagents/execution-owner.js +30 -0
  37. package/pi-extension/subagents/execution-owner.ts +38 -0
  38. package/pi-extension/subagents/herdr.js +645 -0
  39. package/pi-extension/subagents/herdr.ts +927 -0
  40. package/pi-extension/subagents/index.ts +3400 -0
  41. package/pi-extension/subagents/launch.js +774 -0
  42. package/pi-extension/subagents/launch.ts +1124 -0
  43. package/pi-extension/subagents/lifecycle.js +477 -0
  44. package/pi-extension/subagents/lifecycle.ts +582 -0
  45. package/pi-extension/subagents/lineage.js +806 -0
  46. package/pi-extension/subagents/lineage.ts +841 -0
  47. package/pi-extension/subagents/replica-background.js +32 -0
  48. package/pi-extension/subagents/replica-background.ts +38 -0
  49. package/pi-extension/subagents/replica-branch.ts +45 -0
  50. package/pi-extension/subagents/replica-directory-settings.ts +51 -0
  51. package/pi-extension/subagents/replica-history.js +22 -0
  52. package/pi-extension/subagents/replica-history.ts +21 -0
  53. package/pi-extension/subagents/replica-work.js +326 -0
  54. package/pi-extension/subagents/replica-work.ts +336 -0
  55. package/pi-extension/subagents/replica-worker.js +74 -0
  56. package/pi-extension/subagents/replica-worker.ts +71 -0
  57. package/pi-extension/subagents/replication.ts +99 -0
  58. package/pi-extension/subagents/result-delivery-queue.ts +33 -0
  59. package/pi-extension/subagents/role-catalog.js +123 -0
  60. package/pi-extension/subagents/role-catalog.ts +24 -0
  61. package/pi-extension/subagents/runtime-routing.js +154 -0
  62. package/pi-extension/subagents/runtime-routing.ts +239 -0
  63. package/pi-extension/subagents/session-binding.js +346 -0
  64. package/pi-extension/subagents/session-binding.ts +339 -0
  65. package/pi-extension/subagents/session-work.js +219 -0
  66. package/pi-extension/subagents/session-work.ts +167 -0
  67. package/pi-extension/subagents/session.js +318 -0
  68. package/pi-extension/subagents/session.ts +448 -0
  69. package/pi-extension/subagents/settled-contract.js +47 -0
  70. package/pi-extension/subagents/settled-contract.ts +175 -0
  71. package/pi-extension/subagents/source-state.js +180 -0
  72. package/pi-extension/subagents/source-state.ts +184 -0
  73. package/pi-extension/subagents/status.ts +493 -0
  74. package/pi-extension/subagents/subagent-done.js +805 -0
  75. package/pi-extension/subagents/subagent-done.ts +878 -0
  76. package/pi-extension/subagents/terminal.js +111 -0
  77. package/pi-extension/subagents/terminal.ts +208 -0
  78. package/pi-extension/subagents/watchdog.js +122 -0
  79. package/pi-extension/subagents/watchdog.ts +171 -0
package/AGENTS.md ADDED
@@ -0,0 +1,27 @@
1
+ # Pi Agent Replicate
2
+
3
+ `CONTEXT.md` holds the product intent and the contracts of the `replicate` tool. Read it before changing the tool, its schema or how it is presented. Read the matching document before changing its contract:
4
+
5
+ - `docs/settled-lifecycle-contract.md`: how child turns settle into one parent result.
6
+ - `docs/worktree-subagents.md`: worktree snapshots, joins and retained worktrees.
7
+ - `docs/public-subagent-tree-api.md`: the package-root API.
8
+ - `docs/adr/`: past decisions; don't revive removed surfaces.
9
+
10
+ Keep exactly one model-facing tool, `replicate`, with only `name` and `task` in its schema.
11
+
12
+ ## Code map
13
+
14
+ - `pi-extension/subagents/index.ts`: tools, discovery, launch/watch lifecycle and delivery.
15
+ - `pi-extension/subagents/launch.ts`: Pi and worktree launch transaction.
16
+ - `pi-extension/subagents/lineage.ts`: durable recursive ownership.
17
+ - `pi-extension/subagents/runtime-routing.ts`: role-to-parent runtime resolution.
18
+ - `pi-extension/subagents/completion.ts`, `session.ts`, `subagent-done.ts`: completion evidence.
19
+ - `index.ts`, `index.js`, `index.d.ts`: package-root API. Edit all three together.
20
+
21
+ TypeScript modules under `pi-extension/subagents` that have JavaScript mirrors must stay aligned.
22
+
23
+ ## Checks
24
+
25
+ - If `index.ts`, `index.js` or `index.d.ts` changes, run `npx tsc --noEmit index.ts` and `node --check index.js`.
26
+ - Inspect package contents with `npm pack --dry-run`.
27
+ - Run lifecycle and Herdr cases through `test/integration/run-isolated.ts` with an explicit file list (see `README.md`). Never point tests at the user's persistent Herdr server.
package/CHANGELOG.md ADDED
@@ -0,0 +1,147 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ Earlier entries were generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
9
+
10
+
11
+ ## v1.4.0 - 2026-11-01
12
+
13
+ First npm release of Pi Agent Replicate under its own package name.
14
+
15
+ ### Highlights
16
+
17
+ - Give a Pi agent a `replicate` tool to work on independent tasks in parallel while it continues its own work. Replica results return to the originating session.
18
+ - Use separate Git worktrees by default, or select a shared project directory with `/replica-directory` when replicas need the same files and dependencies.
19
+ - Recover replica ownership and results across reloads, with durable settlement and cleanup after descendant work drains.
20
+ - Expose the subagent tree through the package-root API for programmatic integrations.
21
+ - Introduce the Pi Agent Replicate mascot and refreshed README.
22
+
23
+ Shared-directory changes are immediately visible and are not automatically rolled back if a replica fails. Coordinate concurrent edits to avoid overwriting work.
24
+
25
+ ## [v1.3.0](https://github.com/giuseppecrj/pi-herdr-agents/compare/v1.2.3...v1.3.0)
26
+
27
+ ### Commits
28
+
29
+ - feat: identify workflow review nodes [`6919ab1`](https://github.com/giuseppecrj/pi-herdr-agents/commit/6919ab1c655f0e6c50944011889df14800cf2021)
30
+
31
+ ## [v1.2.3](https://github.com/giuseppecrj/pi-herdr-agents/compare/v1.2.2...v1.2.3) - 2026-08-18
32
+
33
+ ### Commits
34
+
35
+ - fix: shorten worktree labels [`4caeabb`](https://github.com/giuseppecrj/pi-herdr-agents/commit/4caeabb4db088b8c60d0caf701ec49fe538f8df2)
36
+ - docs: add release workflow skill [`9937f34`](https://github.com/giuseppecrj/pi-herdr-agents/commit/9937f3402ab672023525c19a50dee3cbd7d579bb)
37
+ - chore: release v1.2.3 [`cd5036e`](https://github.com/giuseppecrj/pi-herdr-agents/commit/cd5036e6254ee5d28d8da3ddfd55e8cc4e0db413)
38
+
39
+ ## [v1.2.2](https://github.com/giuseppecrj/pi-herdr-agents/compare/v1.2.1...v1.2.2) - 2026-08-18
40
+
41
+ ### Commits
42
+
43
+ - fix: hide completion tool from auto-exit children [`e4cad6b`](https://github.com/giuseppecrj/pi-herdr-agents/commit/e4cad6bdbe5d1952f5bb838640493c20ab332dbd)
44
+ - chore: release v1.2.2 [`ab7843b`](https://github.com/giuseppecrj/pi-herdr-agents/commit/ab7843b4919574789029c985f27509b968242b05)
45
+
46
+ ## [v1.2.1](https://github.com/giuseppecrj/pi-herdr-agents/compare/v1.2.0...v1.2.1) - 2026-08-13
47
+
48
+ ### Commits
49
+
50
+ - docs: polish package readme and artwork [`908dd76`](https://github.com/giuseppecrj/pi-herdr-agents/commit/908dd76875c91871b8d91113e6d1633d5112d5b3)
51
+ - chore: release v1.2.1 [`df87f12`](https://github.com/giuseppecrj/pi-herdr-agents/commit/df87f126ec5cad4735c441752471aa8bd724f2e9)
52
+
53
+ ## [v1.2.0](https://github.com/giuseppecrj/pi-herdr-agents/compare/v1.1.0...v1.2.0) - 2026-08-13
54
+
55
+ ### Commits
56
+
57
+ - feat: add worktree session handoff [`8b6f928`](https://github.com/giuseppecrj/pi-herdr-agents/commit/8b6f928c0b61a97f66614054340a68c023df0c95)
58
+ - style: format worktree handoff changes [`c1cdc44`](https://github.com/giuseppecrj/pi-herdr-agents/commit/c1cdc4480f0e29268194bc80809959215d13044f)
59
+ - fix: confirm worktree handoff startup [`0e4a97c`](https://github.com/giuseppecrj/pi-herdr-agents/commit/0e4a97c46b6dd1d9fc56d9c74511e380dca6e6d0)
60
+ - feat: simplify worktree command [`4dc88e0`](https://github.com/giuseppecrj/pi-herdr-agents/commit/4dc88e0a962625eec10240d57cf67548510e6a9a)
61
+ - docs: add worktree handoff research [`00f95c6`](https://github.com/giuseppecrj/pi-herdr-agents/commit/00f95c65e0f050492259a0de625c35e4eef9aae8)
62
+ - test: cover failed worktree handoff recovery [`dd27c0a`](https://github.com/giuseppecrj/pi-herdr-agents/commit/dd27c0a8f17e51cff635e5b77e18ff7f05896650)
63
+ - chore: release v1.2.0 [`dba1ab5`](https://github.com/giuseppecrj/pi-herdr-agents/commit/dba1ab5564b9ef64ae1f0f57b6df4bb40502970c)
64
+ - ci: upgrade release actions to node24 [`ec9158d`](https://github.com/giuseppecrj/pi-herdr-agents/commit/ec9158d838cb0fae914c94fc448a6677b4ab1696)
65
+
66
+ ## [v1.1.0](https://github.com/giuseppecrj/pi-herdr-agents/compare/v1.0.0...v1.1.0) - 2026-08-11
67
+
68
+ ### Commits
69
+
70
+ - chore: initialize generated changelog [`77ca8eb`](https://github.com/giuseppecrj/pi-herdr-agents/commit/77ca8eb000e1d06f6d23bfbd8fdcac491bef581d)
71
+ - feat: label coordinated subagents [`e2d15ab`](https://github.com/giuseppecrj/pi-herdr-agents/commit/e2d15abf2cbe9f0557d14ab768dd39e64352577e)
72
+ - chore: release v1.1.0 [`732c0dc`](https://github.com/giuseppecrj/pi-herdr-agents/commit/732c0dcef0f0a85be737b9dd6023a59eb2771178)
73
+
74
+ ## [v1.0.0](https://github.com/giuseppecrj/pi-herdr-agents/compare/v0.1.4...v1.0.0) - 2026-08-11
75
+
76
+ ### Commits
77
+
78
+ - refactor: extract fresh Pi launch transaction [`2241084`](https://github.com/giuseppecrj/pi-herdr-agents/commit/22410845f8b62765fd2ac391cbd94156010abba6)
79
+ - refactor: remove external CLI subagent execution [`9c31fe7`](https://github.com/giuseppecrj/pi-herdr-agents/commit/9c31fe7e566c1fdb3fa8ac3c8a5879fbc25f0fb8)
80
+ - refactor: route Pi session resumes through launch module [`f16f42b`](https://github.com/giuseppecrj/pi-herdr-agents/commit/f16f42b003813852039be9ec1e91b65f28b87bb6)
81
+ - refactor: deliver subagent results through one message [`52c4e02`](https://github.com/giuseppecrj/pi-herdr-agents/commit/52c4e029bbe39170d16a0178b369ab2f68a2d40d)
82
+ - test: stabilize asynchronous subagent integration [`3d2cebb`](https://github.com/giuseppecrj/pi-herdr-agents/commit/3d2cebb41894f2c66038d636998a9b2e0b781431)
83
+ - _No commit message_ [`d5a5c2d`](https://github.com/giuseppecrj/pi-herdr-agents/commit/d5a5c2d0baa1b25a362e0c02de5f6d48342d9393)
84
+ - updates [`e345303`](https://github.com/giuseppecrj/pi-herdr-agents/commit/e345303338857ca19b8ce880c0707173505ad78b)
85
+ - chore: release v1.0.0 [`ccc0045`](https://github.com/giuseppecrj/pi-herdr-agents/commit/ccc0045f3bd6b937bf6acd238b97fe48341bd8ba)
86
+ - merge: deepen Pi subagent launch and resume [`674da9d`](https://github.com/giuseppecrj/pi-herdr-agents/commit/674da9dc5d752deca3538c0d8d7ffdbdecc00a4c)
87
+
88
+ ## [v0.1.4](https://github.com/giuseppecrj/pi-herdr-agents/compare/v0.1.3...v0.1.4) - 2026-08-10
89
+
90
+ ### Commits
91
+
92
+ - chore: release v0.1.4 [`0ce2fe4`](https://github.com/giuseppecrj/pi-herdr-agents/commit/0ce2fe45b3c76a2bffe1df561076d1c62537c139)
93
+
94
+ ## [v0.1.3](https://github.com/giuseppecrj/pi-herdr-agents/compare/v0.1.2...v0.1.3) - 2026-08-07
95
+
96
+ ### Commits
97
+
98
+ - fix: reject empty workflow completions [`338a788`](https://github.com/giuseppecrj/pi-herdr-agents/commit/338a788855c9ea7ac8b3f6214ff8b3130bc4b007)
99
+ - test: verify fallback completion from session evidence [`b2eb4b0`](https://github.com/giuseppecrj/pi-herdr-agents/commit/b2eb4b0fec5bd7d5ec9f473168cf3710e600052d)
100
+ - chore: release v0.1.3 [`a2cd1b5`](https://github.com/giuseppecrj/pi-herdr-agents/commit/a2cd1b5ab424226e371ee6388e79d8ccf3e9a304)
101
+
102
+ ## [v0.1.2](https://github.com/giuseppecrj/pi-herdr-agents/compare/v0.1.1...v0.1.2) - 2026-08-07
103
+
104
+ ### Commits
105
+
106
+ - chore: release v0.1.2 [`67608a5`](https://github.com/giuseppecrj/pi-herdr-agents/commit/67608a5b1950222b522277c2fb4a246cbffb91f8)
107
+
108
+ ## [v0.1.1](https://github.com/giuseppecrj/pi-herdr-agents/compare/v0.1.0...v0.1.1) - 2026-08-07
109
+
110
+ ### Commits
111
+
112
+ - fix: wait for Herdr shell readiness [`58bfa99`](https://github.com/giuseppecrj/pi-herdr-agents/commit/58bfa99c150963b4c7f7cc8348b8e2ead00399af)
113
+ - chore: release v0.1.1 [`9b9a3c9`](https://github.com/giuseppecrj/pi-herdr-agents/commit/9b9a3c9ded45f65be7c2814cb34230ee5bf82532)
114
+
115
+ ## [v0.1.0](https://github.com/giuseppecrj/pi-herdr-agents/compare/v0.0.4...v0.1.0) - 2026-08-05
116
+
117
+ ### Commits
118
+
119
+ - feat: add ordered subagent model fallbacks [`2ce041b`](https://github.com/giuseppecrj/pi-herdr-agents/commit/2ce041b51f1eb1906623bc0c361fea6a3acf019d)
120
+ - chore: release v0.1.0 [`b409457`](https://github.com/giuseppecrj/pi-herdr-agents/commit/b409457f58b738734b48a4e7cf3418da957db271)
121
+
122
+ ## [v0.0.4](https://github.com/giuseppecrj/pi-herdr-agents/compare/v0.0.3...v0.0.4) - 2026-08-05
123
+
124
+ ### Merged
125
+
126
+ - agents: extract Claude CLI adapter [`#5`](https://github.com/giuseppecrj/pi-herdr-agents/pull/5)
127
+
128
+ ### Commits
129
+
130
+ - release: update version to 0.0.4 [`c685c35`](https://github.com/giuseppecrj/pi-herdr-agents/commit/c685c3553ec76d987972c1786d966b704ad9197b)
131
+
132
+ ## [v0.0.3](https://github.com/giuseppecrj/pi-herdr-agents/compare/v0.0.2...v0.0.3) - 2026-08-05
133
+
134
+ ### Commits
135
+
136
+ - docs: enhance documentation for adversarial-reviewer and runtime adapter policies; clarify model selection process and permissions [`ddf4a34`](https://github.com/giuseppecrj/pi-herdr-agents/commit/ddf4a3411e018c7335bc66e191d5d84b187d8d07)
137
+ - agents: remove bundled Claude reviewer adapter [`ed12ddf`](https://github.com/giuseppecrj/pi-herdr-agents/commit/ed12ddf774046dd0a708fd54d53f12d25118d006)
138
+ - docs: align release baseline with 0.0.2 [`c136b16`](https://github.com/giuseppecrj/pi-herdr-agents/commit/c136b1659370bc4265b1ed4c67c2b670c764a8b1)
139
+ - release: update version to 0.0.3 [`c6ce52c`](https://github.com/giuseppecrj/pi-herdr-agents/commit/c6ce52c6af3b9a3157c073aa257ea69f826597ad)
140
+
141
+ ## v0.0.2 - 2026-08-05
142
+
143
+ ### Commits
144
+
145
+ - feat: launch Pi Herdr Agents 0.2.0 [`4fa3f26`](https://github.com/giuseppecrj/pi-herdr-agents/commit/4fa3f26d743defa68ce1c068fbc80cc5f3062934)
146
+ - release: prepare 0.0.2 [`5d6e65d`](https://github.com/giuseppecrj/pi-herdr-agents/commit/5d6e65d4c25a8df427dd057b77267985bb505d1a)
147
+ - update version [`4ffd68f`](https://github.com/giuseppecrj/pi-herdr-agents/commit/4ffd68f55f58cedbfb9fd84cd139f81c107552eb)
package/CONTEXT.md ADDED
@@ -0,0 +1,63 @@
1
+ # Project context
2
+
3
+ `pi-agent-replicate` lets an agent split independent work into copies of its current Pi session in Herdr.
4
+
5
+ ## Shipped surface
6
+
7
+ The experimental model-facing surface is exactly one tool: `replicate({ name, task })`. Its schema never gains continuation parameters. One invocation creates one fresh copy in a pane, using a dedicated worktree by default or the parent directory when configured for the project. A user command selects the directory mode. The package ships no bundled roles, review workflows or orchestration skills.
8
+
9
+ The old model-facing role, completion and help tools are removed. The internal launcher and package-root tree API remain available for callers that need custom roles, bare children or managed worktrees. Their contracts are separate from replication.
10
+
11
+ ## Replication
12
+
13
+ Replication is parallel work by copies of one agent, not delegation. The caller must already be at its intended working location and understand the task. It must identify independent parts, specify the copy's work and expected result, and continue a different part itself. Do not replicate while deciding the task or just to wait. Waiting after completing useful parallel work is allowed.
14
+
15
+ Use progressive disclosure. The initial description explains purpose and use, not Git, worktrees, checkpoints, delivery routing or host mechanics. Results retain a full summary without host reference footers or saved-copy choices. A failure or conflict explains the work problem. The parent continues with that information or creates a fresh replica.
16
+
17
+ A clone inherits the parent's model, thinking level, full system prompt, active tools and current active conversation, including the triggering user turn. Explicit CLI extensions follow it. The snapshot is captured before asynchronous launch work, so another turn or sibling tool result cannot change its context. Inherited pending tool calls are settled without executing them again.
18
+
19
+ Copied extension control state must not wake a parent-side background operation before the replica receives its own assignment. Keep the active conversation, not ownership of the parent's pending operations.
20
+
21
+ The bounded assignment, parallel-copy identity and required summary belong in a normal task message. Internal original-session references stay in host metadata. Replication must not change the system prompt or its cache prefix. There are no role, model, cwd, worktree or batch choices on this tool.
22
+
23
+ By default each clone gets a source snapshot with tracked working files and nonignored untracked files. The host uses a private index and protected checkpoint refs without changing parent HEAD or staging. Ignored files are not copied. Worktrees isolate files, not shared processes, ports or databases. The project can instead select a shared directory for new clones: no source snapshot or join is made, files are immediately shared, and failure does not undo changes. A clone can replicate its own independent work. It remains responsible until its descendants drain, then returns a nonempty final summary. No help or explicit completion tool is registered for clones.
24
+
25
+ The immediate parent's host saves completion and session evidence. In worktree mode it also saves source evidence; only successful children attempt a three-way join after their descendants drain. Sibling joins are serial, including across overlapping Pi incarnations. Costly replica checkpoint, join and cleanup work runs in a separate host process so it cannot block Pi's input or model loop. Durable ownership gates and existing journals recover interrupted work. Revalidate the original parent's location before file application and delivery. Task completion and integration are separate outcomes. A Git-clean join does not prove program correctness. Conflicts apply no child files; failures and empty summaries never auto-join partial work. Saved work remains available for manual resolution and recovery.
26
+
27
+ Every settled replica closes its owned pane and empty Herdr workspace after descendant drain and process exit, including failure and conflict. Failed or unjoined checkouts and saved sessions remain available for recovery. Applied worktrees are removed only after durable integration and checkpointing, verified ownership and process exit. Later edits or uncertain ownership prevent removal. Sessions and source checkpoints remain reconstructable. Closed parents defer successful joins until their exact session reopens.
28
+
29
+ Each invocation uses fresh source and conversation from the immediate parent. Model-facing resume and fork choices are removed; internal saved sessions and checkpoints remain recovery evidence. Terminal model failure is also terminal: report honest error facts and a file of every post-fork textual assistant answer. Exclude inherited answers, tool calls, tool results and hidden reasoning. Do not truncate the history. An empty required summary is a failure.
30
+
31
+ The caller must choose an explicit task-specific name in lowercase kebab-case for every invocation. Reject missing or invalid names before launch. The runtime adds the immediate parent's origin as `task-name ← parent-name`, using the first nonempty parent session name, current Herdr pane name, or project directory name. Do not generate task names or copy numbers. Keep the same combined title in the child session, host surfaces, status, and result; internal IDs still own recovery and delivery.
32
+
33
+ New replica branches must show their origin and supplied task name as `replica/<origin>/<name>`, not an opaque copy ID. Use a lowercase ASCII origin slug, transliterating Cyrillic so Herdr does not drop the origin from the checkout directory. Reserve each branch atomically at its exact snapshot commit. Serialize allocation across the repository and check both branch names and flattened checkout names; occupied names get numeric suffixes without changing session titles or reusing another copy's source. Preserve parent HEAD and staging, and leave existing branches and recovery paths unchanged.
34
+
35
+ ## Durable lifecycle
36
+
37
+ Replication is fire-and-forget. The caller gets an acknowledgement and later automatic delivery that starts a turn. Parents do not poll. Every fully stopped replica returns one truthful result, even after user input or Escape, and closes after durable settlement and descendant drain. Interruption and unsaved closure are failures with preserved post-fork answers and source work. Process loss does not automatically relaunch a replica; its immediate parent continues the work or creates a fresh copy.
38
+
39
+ Routine supervision must leave the parent's input and tools responsive while copies run. Bound-session discovery uses its exact ownership index, attachment and same-session artifacts, not unrelated historical sessions. Reuse validated binding metadata and consume appended records without rereading whole conversations on every tick. File replacement, truncation and identity changes must invalidate that reuse; the cache is not ownership evidence on its own.
40
+
41
+ A missing or damaged answer-history boundary must not prevent terminal failure delivery or pane cleanup. Report the export error rather than guessing which answers belong to the copy. A failed session checkpoint must not prevent saving terminal facts and source work; retain the checkout and block integration when no usable session checkpoint exists.
42
+
43
+ A replica may fail before it can register its own runtime. Record the allocated shell's boot and process incarnation from the parent before dispatch. A confirmed failed startup can close that owned pane without a child location record, while retaining its checkout. Include available loader diagnostics in the parent error. A native launch tool error already saved in the parent is not delivered again as a recovered replica result. An ambiguous dispatch error must not declare a process finished if it actually started.
44
+
45
+ Replica shutdown requires a stopped agent, no active tools, no pending background terminal/request receipts or queued results, no reported live Linux descendants, and drained child replicas. Background completions are processed by the copy before it returns; a new run invalidates an older closure attempt. Persist receipt/process ownership only for the exact session so reload does not abandon its work or transfer another session's jobs. Count reported child processes and their observed descendants, not the host shell or unrelated runtime helpers. Unreported daemons and remote jobs without receipts are outside this guard.
46
+
47
+ Return the settled hook's finalization promise to Pi. Request shutdown before interactive mode checks the settled boundary; an async request started without that promise can leave a finished copy idle forever.
48
+
49
+ Normal settlement follows this drain without an extra model confirmation. Ten continuously idle seconds provide a safety net for missed handoff, not a limit on active work. Queued messages, requests, tools, retries, compaction, movement and descendant drain suspend that clock. A saved settlement can be retried after reload; missing settlement evidence cannot become success. The retained non-replica recovery and intervention policies remain separate.
50
+
51
+ Session identity, not a file path or pane ID, owns delivery. A binding inside each session carries its durable store and child settings. Teleport preserves that entry and session ID. The ownership index and lineage inboxes survive Pi and Herdr process replacement. Reopening the exact parent replays pending answers and starts a turn; a closed parent is never opened automatically. New-ID forks cannot claim the original answers. A delivery is acknowledged only after its exact result entry is saved, not merely queued. Cleanup requires the recorded shell incarnation to still own the pane.
52
+
53
+ Each tool-launched attempt also retains its actual execution ID and visible outer root from Pi's execution events. Persist both with launch metadata and delivery inboxes; do not recover them from ID syntax, names, paths, completion order or timing. Publish the root as `parentToolCallId` on delayed result/help messages so existing consumers can group searchable output without rewriting history. Each fresh invocation records its own owner; automatic recovery preserves it. Missing ownership stays missing. This display linkage must not change result payloads, consumption, wakeups or source integration.
54
+
55
+ ## Retained launch paths
56
+
57
+ For the internal launcher and package-root API, model and thinking resolve independently: use custom role frontmatter when present, otherwise inherit the parent. Role models must be exact authenticated `provider/model-id` values; fallback lists are unsupported.
58
+
59
+ Roles come from project, global or installed role-pack directories, in that priority order. Legacy external CLI roles fail closed. The retained API does not register model-facing completion or help controls.
60
+
61
+ Managed worktrees remain opt-in on these retained paths. Ownership is recorded before Herdr creates resources. They use an exact committed base without copying parent dirty files, retain the workspace after every outcome, and never push, merge or remove it automatically. Replication has its own snapshot and automatic-join contract, separate from these retained worktrees.
62
+
63
+ The package-root API keeps these contracts through `createSubagentTree` and `attachSubagentTree` without registering model-facing tools.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 HazAT
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,240 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <img src="assets/banner.png" alt="PiAgent Replicate: one agent branches into two copies" width="100%">
3
+ </p>
2
4
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
5
+ <h1 align="center">Pi Agent Replicate</h1>
6
+
7
+ <p align="center">One agent. Parallel work.</p>
8
+
9
+ <div align="center">
10
+
11
+ [![npm version](https://img.shields.io/npm/v/pi-agent-replicate)](https://www.npmjs.com/package/pi-agent-replicate)
12
+ [![npm downloads](https://img.shields.io/npm/dm/pi-agent-replicate)](https://www.npmjs.com/package/pi-agent-replicate)
13
+ [![MIT license](https://img.shields.io/npm/l/pi-agent-replicate)](./LICENSE)
14
+
15
+ </div>
16
+
17
+ ## Summary
18
+
19
+ Pi Agent Replicate gives your [Pi](https://github.com/earendil-works/pi) agent a `replicate` tool for parallel work in [Herdr](https://herdr.dev). The original agent keeps working on the task while copies of itself handle independent parts. Each copy inherits the current conversation and tools, then returns its result automatically to the original.
20
+
21
+ Copies work in separate Git worktrees by default. For projects that need the same directory, such as Unity projects, you can choose a shared directory instead.
22
+
23
+ ## Install
24
+
25
+ Install from npm:
26
+
27
+ ```bash
28
+ pi install npm:pi-agent-replicate
29
+ ```
30
+
31
+ Start Pi inside Herdr, then restart Pi or run `/reload`.
32
+
33
+ ## Agent-facing tool
34
+
35
+ The extension registers exactly one model-facing tool:
36
+
37
+ ```typescript
38
+ replicate({ name: "check-auth-risks", task: "Check authentication risks while I implement the settings change. Return concrete findings with file references." });
39
+ ```
40
+
41
+ Use it only from a task-ready state: the agent is already in the intended working location, understands its task, and has identified independent parts that can run in parallel. Before calling, it must know the copy's assignment, the expected result, and the separate work it will continue itself.
42
+
43
+ This is parallel work by one agent, not a tool for handing off a task. Do not replicate while still deciding the task, its scope, or where to work. If the next step would be waiting for the copy, do the work yourself. After finishing real parallel work, the original may end its turn and wait for automatic delivery. Do not poll.
44
+
45
+ The schema always requires only `name` and `task`. Each call starts a fresh copy with the caller's current context. After a result, continue in the original session or create another copy. Routine replies omit host paths and checkpoint details. A work problem is explained when it occurs.
46
+
47
+ The caller must choose a task-specific `name` in lowercase kebab-case, such as `check-auth-risks`. The runtime adds the origin: `check-auth-risks ← Settings`. The origin comes from the parent's session name, then its current Herdr pane name, then the project directory name. The same combined title appears in the copy's session, Herdr workspace and pane, active status, and result. There is no automatic numbering. Internal IDs remain separate.
48
+
49
+ Git branches use `replica/<origin>/<name>`, for example `replica/settings/check-auth-risks`. The origin is lowercased and made safe for a path; Cyrillic is transliterated (`Настройки` becomes `nastroiki`). Herdr uses the branch to name the checkout directory, such as `replica-nastroiki-check-auth-risks`. Repeated or simultaneous launches reserve separate branches with `-2`, `-3`, and so on, without changing the displayed session title. These suffixes also avoid different origin/task pairs that would produce the same checkout directory. Existing branches and worktrees are not renamed.
50
+
51
+ ## Project directory mode
52
+
53
+ Run `/replica-directory` to choose a mode, or use `/replica-directory shared` or `/replica-directory worktree`. The choice is saved in `.pi/replicate.json` in the current project directory and applies to new copies. The default is `worktree`.
54
+
55
+ In `shared` mode, copies use their parent's current directory, including ignored files and dependencies. They do not create a Git worktree or branch, and there is no snapshot or automatic join. Changes are visible immediately and remain even if the copy fails. Coordinate assignments: simultaneous edits to the same files can overwrite each other, and there is no automatic rollback or conflict isolation. This mode can be useful when copies need the same Unity project directory. Changing the mode does not move already-running copies.
56
+
57
+ ## Replication runtime
58
+
59
+ The following details describe the implementation, not instructions added to the agent's prompt.
60
+
61
+ Each call copies the current active conversation, including the current user turn. It inherits the model, thinking level, full system prompt and active tools. Explicit CLI extensions follow the copy too. The bounded assignment and summary requirement are sent as a normal task message, without changing the system prompt.
62
+
63
+ A fresh call requires `name` and `task`. There are no model-facing role, model, cwd, worktree or batch choices. In the default worktree mode, each copy gets tracked working files and nonignored untracked files from the moment of launch. The snapshot does not change the parent's HEAD or staging. Ignored files, dependencies and builds are not copied. Worktrees isolate source files, not shared processes, ports or databases.
64
+
65
+ There are no model-facing continuation, resume or fork choices. Saved sessions and source checkpoints remain recovery evidence; the tool always creates a fresh copy.
66
+
67
+ A copy can call `replicate` for an independent part of its assignment. It must wait for its own copies and return a nonempty final summary. The runtime delivers that summary and closes its pane and empty Herdr workspace after descendants drain and the process exits. Failed and conflicted copies close too; their files and saved sessions stay available for recovery. Copies do not get `caller_ping` or `subagent_done`.
68
+
69
+ The old subagent tools and child completion/help tools are removed. The internal launcher and package-root API remain available without registering model-facing controls. The package ships no workflows, skills or built-in roles.
70
+
71
+ ## Retained runtime policy
72
+
73
+ Model and thinking are resolved independently:
74
+
75
+ 1. Use the value from the selected role's frontmatter.
76
+ 2. If the role omits it, inherit the value from the parent session.
77
+
78
+ This role policy applies only to the retained internal launcher and package-root API. `replicate` uses the parent runtime without resolving a role. Bare spawns remain supported by those retained paths. They inherit both values. Role models must be one exact authenticated `provider/model-id`. Model fallback lists and `config.json` model routing are not supported.
79
+
80
+ ## Custom roles for the retained API
81
+
82
+ The package has no built-in roles. Add roles in one of these places:
83
+
84
+ | Source | Path |
85
+ | --- | --- |
86
+ | Project | `.pi/agents/<name>.md` |
87
+ | Global | `$PI_CODING_AGENT_DIR/agents/<name>.md` (normally `~/.pi/agent/agents/<name>.md`) |
88
+ | Role pack | An installed Pi package registered through the role discovery event |
89
+
90
+ Priority is project, then global, then role pack. Duplicate role-pack names are rejected.
91
+
92
+ Example:
93
+
94
+ ```markdown
95
+ ---
96
+ name: security-reviewer
97
+ description: Reviews a bounded change for security problems
98
+ model: provider/exact-model-id
99
+ thinking: high
100
+ tools: read, bash
101
+ spawning: false
102
+ auto-exit: true
103
+ ---
104
+
105
+ Review only the requested change. Report concrete findings with file references.
106
+ ```
107
+
108
+ Useful frontmatter fields:
109
+
110
+ - `name`, `description`
111
+ - `model`, `thinking`
112
+ - `tools`, `skills`, `deny-tools`
113
+ - `spawning`, `allow-self-spawn`
114
+ - `auto-exit`, `interactive`
115
+ - `session-mode`: `standalone`, `lineage-only`, or `fork`
116
+ - `cwd`
117
+ - `system-prompt`: `append` or `replace`
118
+ - `disable-model-invocation`
119
+
120
+ Requested skills are saved in the child's conversation once. Reloading or resuming that conversation reuses the saved skills instead of adding another initialization message.
121
+
122
+ Definitions that request an external `cli` fail before Herdr creates resources.
123
+
124
+ ### Role packs
125
+
126
+ A Pi package can register one absolute Markdown file or a directory of direct `.md` children through this event:
127
+
128
+ ```typescript
129
+ pi.events.on(
130
+ "pi-herdr-subagents:roles:discover:v1",
131
+ (request: { apiVersion: number; register(path: string): void }) => {
132
+ if (request.apiVersion === 1) request.register(absoluteRolesDirectory);
133
+ },
134
+ );
135
+ ```
136
+
137
+ A role-pack extension must remove its listener on `session_shutdown`.
138
+
139
+ ## Lifecycle
140
+
141
+ `replicate` is fire-and-forget. Do not poll, sleep, tail session files or check panes to wait for it. Continue independent work or end your turn until automatic delivery starts the next turn.
142
+
143
+ Delayed result and help cards keep the launch call that owns that attempt. In Stereo fullscreen they appear beside that call in Activity, not as a second Conversation card. Nested calls link to their actual visible outer call. Saved ownership follows delivery through reload, receiver recovery and Teleport. Older or API-launched work without a recorded tool owner stays unlinked. This changes presentation only: the parent still receives the complete result, and saved history stays chronological.
144
+
145
+ Host metadata retains the saved session and immutable Markdown result-file paths. Routine model-visible results carry the copy's summary, without saved-copy choices or internal references. Terminal model failures report error facts; host metadata also retains a separate file containing every post-fork textual assistant answer in order. That file excludes the original's answers, tool calls, tool results and hidden reasoning. Answers are not truncated. An empty required summary is a failure, not success.
146
+
147
+ Every fully stopped replica reports and closes, including after user input or Escape. An interrupted copy reports interruption; an unsaved close or process loss reports failure with preserved answers, not success or an automatic restart. The parent continues with the returned information or creates a fresh copy.
148
+
149
+ If Pi fails while loading extensions or before its session initializes, the parent uses its saved launch ownership to close the failed attempt pane and reports the available startup error. It does not wait for completion hooks that never started. The failed checkout stays available. A warning alone or a terminal marker printed while Pi is still running does not count as process failure.
150
+
151
+ Missing or damaged answer history does not block a failure report or pane cleanup. The result explains why history is unavailable without exporting answers of unknown origin. If the session itself cannot be checkpointed, source work is still saved where possible and retained, and no changes are applied automatically.
152
+
153
+ Automatic closure waits for active tools, pending background terminal completions, background request receipts, reported Linux child PIDs and their observed descendants, and owned replicas. Completion messages wake the copy so it can process the result before returning. New input or work cancels a stale shutdown decision. This ownership state follows the exact session through reload.
154
+
155
+ The process guard uses reported descendant PIDs, not every process in the terminal or every runtime helper. It cannot discover unreported daemonized jobs or remote CI jobs without a background receipt/completion message. A running open-ended job keeps its copy open until stopped.
156
+
157
+ Once session work is finished, settlement reports without an extra model confirmation. A ten-second safety net retries a missed handoff only while Pi is truly idle, with no queued work, movement or undrained descendants. Active tools, requests, retries and compaction are not subject to that deadline. Durable ownership restores pending results, join plans and cleanup after restart. The retained non-replica API keeps its existing recovery and interactive policies.
158
+
159
+ The binding is saved inside the Pi session and follows its stable session ID through Teleport. A new file, cwd, process, or pane does not change the receiver. A fork with a new session ID does not inherit pending answers. Children restore their own lifecycle and follow their moved immediate parent, including nested launches. A move suspends source-side delivery and completion while the replacement binds; an abandoned child handoff has a bounded recovery gate.
160
+
161
+ Ownership and current locations are indexed under `$PI_CODING_AGENT_DIR/subagent-sessions` (default: `~/.pi/agent/subagent-sessions`). The session carries this store path through moves. Answers stay in durable lineage inboxes until their exact result entry is saved in the parent session. When the parent is closed, nothing opens it automatically: reopening that session restores observers, replays pending answers, and wakes its agent. A Herdr restart does not erase the store. An interrupted active child uses bounded recovery or reports failure; unfinished computation is not a saved answer.
162
+
163
+ Keep the binding store, session artifacts, and saved sessions together when backing up or moving to another machine. Pane IDs alone are never permission to close a pane after a restart.
164
+
165
+ No child receives a model-facing completion or help tool.
166
+
167
+ ## Managed worktrees
168
+
169
+ In the default worktree mode, replication creates a snapshot worktree. The immediate parent's host saves the completed child's source and session, then attempts a three-way join against the original snapshot. Sibling joins run serially through durable ownership gates. Checkpointing, Git joins and owned cleanup run in background host processes, not Pi's input thread. The host revalidates the original session before applying files and delivering its result. Parent HEAD and staging stay unchanged. A clean Git join means no detected text conflict, not that the combined program is correct.
170
+
171
+ Task completion and integration are separate outcomes: `applied`, `conflict`, `blocked`, or `not-attempted`. Conflicts do not apply any of the child's files to the parent. Failed or empty-summary children never auto-join partial work. Their worktrees, sessions and checkpoints stay available for recovery. A new replica starts from the current parent's context and files.
172
+
173
+ Successful worktrees are removed only after durable integration and source/session checkpointing, with ownership, process-exit and unchanged-source checks. Unknown ownership or later edits keep the worktree. Saved sessions and GC-protected source refs remain recoverable. If the parent is closed, successful integration waits for its exact session to reopen; no replacement parent is opened automatically.
174
+
175
+ In shared mode, no source checkpoint or join is performed. The shared files remain as edited regardless of the result.
176
+
177
+ The package-root API and retained internal role launcher have a separate opt-in worktree contract:
178
+
179
+ - `cwd` selects the source repository.
180
+ - `base` resolves to an exact commit and defaults to committed `HEAD`.
181
+ - Parent uncommitted and untracked files are not copied.
182
+ - Ownership is recorded before Herdr resources are created.
183
+ - Success, failure, and help requests retain the workspace.
184
+ - Completion reports branch, commits, changed files, and clean/conflicted state.
185
+ - Unknown Git inspection results remain unknown; they are never guessed clean.
186
+
187
+ See [Worktree subagents](docs/worktree-subagents.md) for review and cleanup commands.
188
+
189
+ ## Package-root API
190
+
191
+ Other Pi extensions can import the side-effect-free package root:
192
+
193
+ ```typescript
194
+ import { createSubagentTree, attachSubagentTree } from "pi-agent-replicate";
195
+ ```
196
+
197
+ The tree API supports nested ownership, exact-parent reattachment, cancellation, worktree children, and one final callback. Its child runtime retains role frontmatter, then parent inheritance; this is separate from session replication. See [Public subagent tree API](docs/public-subagent-tree-api.md).
198
+
199
+ ## Configuration
200
+
201
+ `config.json.example` contains status settings only:
202
+
203
+ ```json
204
+ {
205
+ "status": {
206
+ "enabled": true,
207
+ "quietThresholdMs": 120000
208
+ }
209
+ }
210
+ ```
211
+
212
+ `PI_SUBAGENT_QUIET_THRESHOLD_MS` overrides the quiet threshold for the current process. It must be a positive integer.
213
+
214
+ ## Development
215
+
216
+ ```bash
217
+ npm test
218
+ npm run lint
219
+ npm pack --dry-run
220
+ npx tsc --noEmit index.ts
221
+ node --check index.js
222
+ git diff --check
223
+ ```
224
+
225
+ Run selected isolated real-Pi and Herdr cases from inside Herdr:
226
+
227
+ ```bash
228
+ npm run test:integration -- test/integration/replication.test.ts
229
+ ```
230
+
231
+ For a manual TUI check with a deterministic provider, set `PI_INTEGRATION_TUI_DIR` to a fresh ignored directory and `PI_INTEGRATION_IDE_ENTRY` to the current IDE source entry. Run only this case with the intended Pi binary on `PATH`:
232
+
233
+ ```bash
234
+ node --experimental-strip-types test/integration/run-isolated.ts \
235
+ --test-name-pattern='renders an active isolated replica' test/integration/replication.test.ts
236
+ ```
237
+
238
+ The case reloads Pi and records the loaded IDE in `runtime.json`. The applied and failed cases each write to their own subdirectory. Use its `pane.json` to attach a read-only observer to the private Herdr endpoint. Capture the active pane, then create `release-child` in the inspection directory. Capture the delivered result, then create `finish` so the runner can clean up. Both observation gates time out after five minutes. Do not use the user's persistent Herdr server.
239
+
240
+ See [RELEASING.md](RELEASING.md) for release checks.