@gtheys/pi-interactive-subagents 0.0.0-stage → 0.2.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 (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +462 -2
  3. package/agents/claude-code.md +23 -0
  4. package/agents/planner.md +569 -0
  5. package/agents/reviewer.md +152 -0
  6. package/agents/scout.md +112 -0
  7. package/agents/visual-tester.md +202 -0
  8. package/agents/worker.md +109 -0
  9. package/config.json.example +6 -0
  10. package/config.schema.json +71 -0
  11. package/package.json +59 -4
  12. package/pi-extension/subagents/activity.ts +730 -0
  13. package/pi-extension/subagents/asks.ts +234 -0
  14. package/pi-extension/subagents/assignments.ts +395 -0
  15. package/pi-extension/subagents/config.ts +191 -0
  16. package/pi-extension/subagents/control.ts +172 -0
  17. package/pi-extension/subagents/dataroot.ts +52 -0
  18. package/pi-extension/subagents/delivered.ts +81 -0
  19. package/pi-extension/subagents/flow-env.ts +112 -0
  20. package/pi-extension/subagents/herdr.ts +323 -0
  21. package/pi-extension/subagents/inbox.ts +213 -0
  22. package/pi-extension/subagents/index.ts +5003 -0
  23. package/pi-extension/subagents/lock.ts +318 -0
  24. package/pi-extension/subagents/peer-bindings.ts +175 -0
  25. package/pi-extension/subagents/peers.ts +401 -0
  26. package/pi-extension/subagents/plan-skill.md +219 -0
  27. package/pi-extension/subagents/plugin/.claude-plugin/plugin.json +5 -0
  28. package/pi-extension/subagents/plugin/hooks/hooks.json +15 -0
  29. package/pi-extension/subagents/plugin/hooks/on-stop.sh +68 -0
  30. package/pi-extension/subagents/retention.ts +918 -0
  31. package/pi-extension/subagents/retirement.ts +216 -0
  32. package/pi-extension/subagents/session.ts +194 -0
  33. package/pi-extension/subagents/snapshot.ts +151 -0
  34. package/pi-extension/subagents/spawn.ts +237 -0
  35. package/pi-extension/subagents/spawnseq.ts +116 -0
  36. package/pi-extension/subagents/state-events.ts +152 -0
  37. package/pi-extension/subagents/status.ts +886 -0
  38. package/pi-extension/subagents/store.ts +245 -0
  39. package/pi-extension/subagents/subagent-done.ts +1452 -0
  40. package/pi-extension/subagents/surface.ts +365 -0
  41. package/pi-extension/subagents/task-composition.ts +113 -0
  42. package/pi-extension/subagents/tool-effects.ts +203 -0
  43. package/pi-extension/subagents/usage.ts +330 -0
  44. package/pi-extension/subagents/watermark.ts +126 -0
  45. package/pi-extension/subagents/worktrees.ts +147 -0
  46. package/skills/subagent-orchestration/SKILL.md +57 -0
@@ -0,0 +1,245 @@
1
+ // Durable result store for subagent results. Ported from pi-herdsman
2
+ // storage mechanics (Apache-2.0, ADR 0001): results live as atomic JSON
3
+ // records under <dataRoot>/results/, semantic refs `result:<name>#<n>` are
4
+ // reserved via exclusive-create marker files under
5
+ // <dataRoot>/result-ref-reservations/<name>/<n> — monotonic per name, never
6
+ // reused, safe across concurrent allocators and parent sessions.
7
+ import { randomUUID } from 'node:crypto'
8
+ import {
9
+ closeSync,
10
+ existsSync,
11
+ fsyncSync,
12
+ mkdirSync,
13
+ openSync,
14
+ readdirSync,
15
+ readFileSync,
16
+ renameSync,
17
+ unlinkSync,
18
+ writeSync,
19
+ } from 'node:fs'
20
+ import { join } from 'node:path'
21
+ import { ensureSubagentsDataDir, subagentsDataRoot } from './dataroot.ts'
22
+ import type { UsageTotals } from './usage.ts'
23
+
24
+ // AIDEV-NOTE: per-kind byte ceiling on a serialized result record (herdsman
25
+ // discipline). 1 MiB is generous for a final summary; anything larger is
26
+ // rejected (fail closed) rather than truncated, so callers can fall back to
27
+ // inline delivery without silently losing content.
28
+ export const MAX_RESULT_RECORD_BYTES = 1024 * 1024
29
+
30
+ export interface ResultRecord {
31
+ canonicalId: string
32
+ childId: string
33
+ name: string
34
+ summary: string
35
+ sessionFile: string | null
36
+ artifactPaths: string[]
37
+ /** Session-accumulated token/cost totals (PMR-874G), recorded at settlement. */
38
+ usage?: UsageTotals
39
+ createdAt: string
40
+ }
41
+
42
+ export interface ResultRefBinding {
43
+ ref: string
44
+ canonicalId: string
45
+ }
46
+
47
+ export class ResultRecordTooLargeError extends Error {
48
+ readonly code = 'RESULT_RECORD_TOO_LARGE'
49
+ }
50
+
51
+ /** Canonical on-disk path for a result record. */
52
+ export function resultRecordPath(canonicalId: string, root?: string): string {
53
+ return join(
54
+ ensureSubagentsDataDir('results', root ?? subagentsDataRoot()),
55
+ `${canonicalId}.json`,
56
+ )
57
+ }
58
+
59
+ /** Reservation marker path for a semantic ref counter slot. */
60
+ export function reservationPath(
61
+ name: string,
62
+ n: number,
63
+ root?: string,
64
+ ): string {
65
+ return join(
66
+ ensureSubagentsDataDir(
67
+ 'result-ref-reservations',
68
+ root ?? subagentsDataRoot(),
69
+ ),
70
+ sanitizeName(name),
71
+ String(n),
72
+ )
73
+ }
74
+
75
+ /** Parse `result:<name>#<n>`; returns null on malformed refs. */
76
+ export function parseSemanticRef(
77
+ ref: string,
78
+ ): { name: string; n: number } | null {
79
+ const match = /^result:([^#\s]+)#(\d+)$/.exec(ref)
80
+ if (!match) return null
81
+ const n = Number.parseInt(match[2], 10)
82
+ if (!Number.isSafeInteger(n) || n <= 0) return null
83
+ return { name: match[1], n }
84
+ }
85
+
86
+ function sanitizeName(name: string): string {
87
+ const safe = name.replace(/[^A-Za-z0-9._-]/g, '_')
88
+ if (safe === '' || safe === '.' || safe === '..') return '_'
89
+ return safe
90
+ }
91
+
92
+ // AIDEV-NOTE: atomic record write — write tmp sibling, fsync, rename over the
93
+ // final path. Readers therefore never observe a partial record.
94
+ export function writeResultRecord(
95
+ params: {
96
+ childId: string
97
+ name: string
98
+ summary: string
99
+ sessionFile: string | null
100
+ artifactPaths?: string[]
101
+ usage?: UsageTotals
102
+ },
103
+ root?: string,
104
+ ): { canonicalId: string; path: string } {
105
+ const canonicalId = randomUUID()
106
+ const record: ResultRecord = {
107
+ canonicalId,
108
+ childId: params.childId,
109
+ name: params.name,
110
+ summary: params.summary,
111
+ sessionFile: params.sessionFile,
112
+ artifactPaths: params.artifactPaths ?? [],
113
+ createdAt: new Date().toISOString(),
114
+ }
115
+ if (params.usage) record.usage = params.usage
116
+ const serialized = JSON.stringify(record)
117
+ const bytes = Buffer.byteLength(serialized, 'utf8')
118
+ if (bytes > MAX_RESULT_RECORD_BYTES) {
119
+ throw new ResultRecordTooLargeError(
120
+ `result record is ${bytes} bytes, ceiling is ${MAX_RESULT_RECORD_BYTES}`,
121
+ )
122
+ }
123
+
124
+ const dir = ensureSubagentsDataDir('results', root ?? subagentsDataRoot())
125
+ const finalPath = join(dir, `${canonicalId}.json`)
126
+ const tmpPath = join(dir, `.tmp-${canonicalId}`)
127
+ const fd = openSync(tmpPath, 'w', 0o600)
128
+ try {
129
+ writeSync(fd, serialized)
130
+ fsyncSync(fd)
131
+ } finally {
132
+ closeSync(fd)
133
+ }
134
+ renameSync(tmpPath, finalPath)
135
+ return { canonicalId, path: finalPath }
136
+ }
137
+
138
+ /** Read a result record by canonical path. */
139
+ export function readResultRecord(path: string): ResultRecord {
140
+ return JSON.parse(readFileSync(path, 'utf8')) as ResultRecord
141
+ }
142
+
143
+ // AIDEV-NOTE: exactly-once ref allocation window — the exclusive create of the
144
+ // marker file is the single point that decides which allocator wins slot n.
145
+ // Losers retry at the next counter value, so refs are monotonic per name and
146
+ // never reused, even across parent sessions or crashed allocators (markers are
147
+ // deliberately never cleaned up).
148
+ export function allocateSemanticRef(
149
+ name: string,
150
+ root?: string,
151
+ canonicalId?: string,
152
+ ): string {
153
+ const dir = join(
154
+ ensureSubagentsDataDir(
155
+ 'result-ref-reservations',
156
+ root ?? subagentsDataRoot(),
157
+ ),
158
+ sanitizeName(name),
159
+ )
160
+ mkdirSync(dir, { recursive: true })
161
+
162
+ let n = highestExistingN(dir) + 1
163
+ for (;;) {
164
+ const marker = join(dir, String(n))
165
+ try {
166
+ const fd = openSync(marker, 'wx', 0o600)
167
+ try {
168
+ if (canonicalId !== undefined) writeSync(fd, canonicalId)
169
+ fsyncSync(fd)
170
+ } finally {
171
+ closeSync(fd)
172
+ }
173
+ } catch {
174
+ n += 1
175
+ continue
176
+ }
177
+ return `result:${name}#${n}`
178
+ }
179
+ }
180
+
181
+ function highestExistingN(dir: string): number {
182
+ let highest = 0
183
+ let entries: string[]
184
+ try {
185
+ entries = readdirSync(dir)
186
+ } catch {
187
+ entries = []
188
+ }
189
+ for (const entry of entries) {
190
+ const parsed = Number.parseInt(entry, 10)
191
+ if (
192
+ String(parsed) === entry &&
193
+ Number.isSafeInteger(parsed) &&
194
+ parsed > highest
195
+ ) {
196
+ highest = parsed
197
+ }
198
+ }
199
+ return highest
200
+ }
201
+
202
+ // AIDEV-NOTE: the marker content is the canonicalId, which is what lets a bare
203
+ // semantic ref resolve back to its record path without a separate index.
204
+ export function resolveResultRef(ref: string, root?: string): string | null {
205
+ const inspection = inspectResultRef(ref, root)
206
+ if (inspection.kind !== 'path') return null
207
+ return inspection.path
208
+ }
209
+
210
+ // AIDEV-NOTE (PMR-KT90): markers are never pruned and marker content (the
211
+ // canonicalId) is immutable, so a ref can never silently resolve to different
212
+ // content — it can only loudly expire once its results record is pruned.
213
+ export type ResultRefInspection =
214
+ | { kind: 'path'; path: string }
215
+ | { kind: 'expired' }
216
+ | { kind: 'missing' }
217
+
218
+ export function inspectResultRef(
219
+ ref: string,
220
+ root?: string,
221
+ ): ResultRefInspection {
222
+ const parsed = parseSemanticRef(ref)
223
+ if (!parsed) return { kind: 'missing' }
224
+ const marker = reservationPath(parsed.name, parsed.n, root)
225
+ if (!existsSync(marker)) return { kind: 'missing' }
226
+ const canonicalId = readFileSync(marker, 'utf8').trim()
227
+ if (canonicalId === '') return { kind: 'missing' }
228
+ const path = resultRecordPath(canonicalId, root)
229
+ if (!existsSync(path)) return { kind: 'expired' }
230
+ return { kind: 'path', path }
231
+ }
232
+
233
+ export const RESULT_REF_EXPIRED_MESSAGE =
234
+ 'result expired by retention policy' as const
235
+
236
+ /** Remove an obsolete tmp sibling left behind by a crashed write. */
237
+ export function cleanupTmpRecord(canonicalId: string, root?: string): void {
238
+ const dir = ensureSubagentsDataDir('results', root ?? subagentsDataRoot())
239
+ try {
240
+ unlinkSync(join(dir, `.tmp-${canonicalId}`))
241
+ } catch {}
242
+ }
243
+
244
+ // AIDEV-NOTE: exported for tests — names are filesystem path components.
245
+ export const __storeInternals__ = { sanitizeName }