pi-crew 0.9.62 → 0.9.65

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 (60) hide show
  1. package/CHANGELOG.md +105 -0
  2. package/README.md +47 -1
  3. package/agents/critic.md +1 -1
  4. package/agents/explorer.md +1 -1
  5. package/agents/planner.md +1 -1
  6. package/agents/reviewer.md +1 -1
  7. package/agents/security-reviewer.md +1 -1
  8. package/agents/test-engineer.md +1 -1
  9. package/agents/writer.md +1 -1
  10. package/dist/index.mjs +478 -366
  11. package/package.json +7 -3
  12. package/scripts/analyze-run.mjs +1333 -0
  13. package/scripts/pty_probe.py +10 -8
  14. package/scripts/resource-sampler.mjs +482 -0
  15. package/skills/real-test-pi-crew/SKILL.md +6 -6
  16. package/src/agents/agent-config.ts +4 -0
  17. package/src/agents/agent-serializer.ts +1 -0
  18. package/src/agents/discover-agents.ts +8 -0
  19. package/src/config/role-tools.ts +49 -1
  20. package/src/observability/event-to-metric.ts +29 -0
  21. package/src/observability/metrics-primitives.ts +41 -3
  22. package/src/prompt/prompt-runtime.ts +6 -0
  23. package/src/prompt/scratchpad-lifecycle.ts +605 -0
  24. package/src/runtime/README.md +1 -1
  25. package/src/runtime/broker/crew-broker.ts +0 -16
  26. package/src/runtime/child-pi/child-pi-spawn.ts +42 -1
  27. package/src/runtime/child-pi/child-pi.ts +5 -0
  28. package/src/runtime/effectiveness.ts +23 -1
  29. package/src/runtime/merge-gate.ts +202 -0
  30. package/src/runtime/model/model-fallback.ts +11 -0
  31. package/src/runtime/model/pi-args.ts +1 -1
  32. package/src/runtime/model/provider-extensions.ts +31 -12
  33. package/src/runtime/output/progress-tracker.ts +3 -33
  34. package/src/runtime/recovery/crash-recovery.ts +1 -1
  35. package/src/runtime/scratchpad/README.md +184 -0
  36. package/src/runtime/scratchpad/engine.ts +648 -0
  37. package/src/runtime/scratchpad/guest.ts +360 -0
  38. package/src/runtime/scratchpad/index.ts +22 -0
  39. package/src/runtime/scratchpad/protocol.ts +88 -0
  40. package/src/runtime/scratchpad/snapshot-hmac.ts +161 -0
  41. package/src/runtime/scratchpad/snapshot-lookup.ts +74 -0
  42. package/src/runtime/scratchpad/transform.ts +363 -0
  43. package/src/runtime/task-runner/child-executor.ts +48 -31
  44. package/src/runtime/team-runner.ts +128 -203
  45. package/src/schema/team-tool-schema.ts +2 -0
  46. package/src/teams/discover-teams.ts +2 -0
  47. package/src/teams/team-config.ts +7 -0
  48. package/src/teams/team-serializer.ts +1 -0
  49. package/src/ui/mascot.ts +1 -14
  50. package/teams/default.team.md +1 -0
  51. package/teams/fast-fix.team.md +1 -0
  52. package/src/observability/event-bus.ts +0 -86
  53. package/src/plugins/plugin-define.ts +0 -6
  54. package/src/plugins/plugin-registry.ts +0 -32
  55. package/src/plugins/plugins/index.ts +0 -3
  56. package/src/plugins/plugins/nextjs.ts +0 -19
  57. package/src/plugins/plugins/vite.ts +0 -10
  58. package/src/plugins/plugins/vitest.ts +0 -9
  59. package/src/runtime/child-pi/child-pi-pool.ts +0 -68
  60. package/src/runtime/iteration-hooks.ts +0 -305
@@ -19,11 +19,14 @@
19
19
  import type { SpawnOptions } from "node:child_process";
20
20
  import * as fs from "node:fs";
21
21
  import * as path from "node:path";
22
+ import { isScratchpadEnabledForRole } from "../../config/role-tools.ts";
22
23
  import { WINDOWS_ESSENTIAL_ENV_VARS } from "../../utils/env-allowlist.ts";
23
24
  import { buildScopedAllowList, sanitizeEnvSecrets } from "../../utils/env-filter.ts";
24
25
  import { logInternalError } from "../../utils/internal-error.ts";
25
- import { buildPiWorkerArgs } from "../model/pi-args.ts";
26
+ import { resolveRealContainedPath } from "../../utils/safe-paths.ts";
27
+ import { buildPiWorkerArgs, createSafeTempDir, getPiTempBase } from "../model/pi-args.ts";
26
28
  import { getPiSpawnCommand } from "../pi-spawn.ts";
29
+ import { findLatestScratchpadSnapshot } from "../scratchpad/snapshot-lookup.ts";
27
30
  import type { ChildPiRunInput, ChildPiRunResult } from "./child-pi.ts";
28
31
 
29
32
  // ── Env allowlist (base set always passed to children) ──────────────────
@@ -271,6 +274,44 @@ export function prepareSpawnContext(
271
274
  if (input.runId) built.env.PI_CREW_BROKER_RUN_ID = input.runId;
272
275
  if (input.agentId) built.env.PI_CREW_BROKER_TASK_ID = input.agentId;
273
276
  }
277
+ // Phase 1 scratchpad: opt in the persistent Bun-free JS evaluator (execute
278
+ // tool) for this worker. Gated by role/agent (S-6 read-only roles never;
279
+ // F6 agent.scratchpad===false kills). All keys are PI_CREW_* control vars →
280
+ // pass assertOnlyControlEnvKeys. Only set when there is a task id to bind
281
+ // the snapshot to (always present on the child-executor path).
282
+ if (input.agentId && isScratchpadEnabledForRole(input.role ?? input.agent.name, input.agent)) {
283
+ built.env.PI_CREW_SCRATCHPAD = "1"; // dormant gate (worker conditional registerTool)
284
+ built.env.PI_CREW_TASK_ID = input.agentId; // snapshot relativePath provenance
285
+ built.env.PI_CREW_ATTEMPT = String(input.attempt ?? 0); // C3 per-attempt suffix
286
+ if (input.artifactsRoot) {
287
+ // N2-1: worker reads this DIRECTLY as writeArtifact's artifactsRoot — do
288
+ // NOT derive from the snapshot path (snapshot is in tempDir, not under
289
+ // artifactsRoot after F4/S-1).
290
+ built.env.PI_CREW_ARTIFACTS_ROOT = input.artifactsRoot;
291
+ }
292
+ // F4/S-1: the RAW (unredacted) snapshot must NEVER land in artifactsRoot —
293
+ // point it at a temp dir; the worker reads it then writeArtifact()
294
+ // (redact+atomic) is the ONLY writer into artifactsRoot.
295
+ // R3-1: built.tempDir is only created by buildPiWorkerArgs when the agent
296
+ // has a systemPrompt OR the task exceeds TASK_ARG_LIMIT — guard against
297
+ // undefined (resolveRealContainedPath(undefined) would TypeError and crash
298
+ // spawn). createSafeTempDir auto-tracks the dir for cleanupAllTrackedTempDirs.
299
+ const scratchTempDir = built.tempDir ?? createSafeTempDir(getPiTempBase(), "pi-crew-scratchpad-");
300
+ built.env.PI_CREW_SCRATCHPAD_SNAPSHOT = resolveRealContainedPath(scratchTempDir, `${input.agentId}.snapshot.json`);
301
+ // Phase 2 crash-resume (D1/D1b/D2): locate the latest snapshot artifact of a
302
+ // PREVIOUS attempt (retry round / crash-recovery re-queue / manual re-run)
303
+ // and hand its path to the worker via PI_CREW_SCRATCHPAD_RESTORE. The worker
304
+ // re-validates at READ time (D10) — this env is a hint, not a trust anchor.
305
+ // Latest-mtime wins (model-fallback i resets each retry round); RESTORE_MTIME
306
+ // lets the worker detect a swap between spawn and first execute (MINOR-S1).
307
+ const restoreHit = input.artifactsRoot ? findLatestScratchpadSnapshot(input.artifactsRoot, input.agentId) : null;
308
+ if (restoreHit) {
309
+ built.env.PI_CREW_SCRATCHPAD_RESTORE = restoreHit.path;
310
+ // NIT-CA-1: mtime pin is a SWAP-DETECTION HINT (defense-in-depth), never
311
+ // an integrity/authn control — any same-uid actor can utimesSync it.
312
+ built.env.PI_CREW_SCRATCHPAD_RESTORE_MTIME = String(restoreHit.mtimeMs);
313
+ }
314
+ }
274
315
  // B5: if the parent already aborted before we spawn, do not start the child
275
316
  // at all. Spawning a doomed process wastes resources, and the abort listener
276
317
  // registered below will not re-fire for an already-aborted signal (so the
@@ -147,6 +147,11 @@ export interface ChildPiRunInput {
147
147
  thinkingOverride?: string;
148
148
  /** Root directory for artifacts (used to validate transcriptPath). */
149
149
  artifactsRoot?: string;
150
+ /** Phase 1 scratchpad: model-fallback attempt index (0-based) for per-attempt
151
+ * snapshot relativePath `scratchpad/<taskId>.attempt-<attempt>.snapshot.json`
152
+ * (C3). Optional — worker defaults to attempt 0 when unset (e.g. custom
153
+ * agent spawn outside child-executor). */
154
+ attempt?: number;
150
155
  /**
151
156
  * Optional broker spawn context (Phase 0 inter-pi broker). When present,
152
157
  * `prepareSpawnContext` injects `PI_CREW_BROKER_SOCKET` and
@@ -26,6 +26,24 @@ export function taskHasObservableWorkerActivity(task: TeamTaskState): boolean {
26
26
  );
27
27
  }
28
28
 
29
+ /**
30
+ * True when the task completed but its result artifact is an EMPTY file
31
+ * (0 bytes). Closes the F4 monitoring gap (real-test-2026-08-10 finding):
32
+ * a child worker absorbed by a rate-limit (429) or a model-not-found
33
+ * failure still emits transcript/usage events, so
34
+ * {@link taskHasObservableWorkerActivity} reports "activity" — but the
35
+ * actual result content is empty. A completed task with an empty result
36
+ * has done no real work and must not count toward run effectiveness.
37
+ *
38
+ * Deliberately only flags `sizeBytes === 0` (a written-but-empty file).
39
+ * A missing resultArtifact stays non-flagging (some tasks legitimately
40
+ * produce no result file), and an undefined sizeBytes stays non-flagging
41
+ * (legacy tasks — do not false-positive on absent metadata).
42
+ */
43
+ export function taskHasEmptyResult(task: TeamTaskState): boolean {
44
+ return Boolean(task.resultArtifact && task.resultArtifact.sizeBytes === 0);
45
+ }
46
+
29
47
  export function resolveEffectivenessGuardMode(
30
48
  runtimeConfig: CrewRuntimeConfig | undefined,
31
49
  manifest?: TeamRunManifest,
@@ -43,7 +61,11 @@ export function evaluateRunEffectiveness(input: {
43
61
  runtimeConfig?: CrewRuntimeConfig;
44
62
  }): RunEffectivenessSummary {
45
63
  const completedTasks = input.tasks.filter((task) => task.status === "completed");
46
- const noObservedWorkTasks = completedTasks.filter((task) => !taskHasObservableWorkerActivity(task));
64
+ // F4: a completed task with an EMPTY result artifact (0 bytes) is
65
+ // treated exactly like no-observed-work — the worker may have spun up
66
+ // and streamed transcript events, but it produced no real output
67
+ // (e.g. a 429 rate-limit absorbed by the fallback chain).
68
+ const noObservedWorkTasks = completedTasks.filter((task) => !taskHasObservableWorkerActivity(task) || taskHasEmptyResult(task));
47
69
  const needsAttentionTasks = input.tasks.filter((task) => task.agentProgress?.activityState === "needs_attention");
48
70
  const workerExecution: WorkerExecutionState = input.executeWorkers ? "enabled" : "disabled/scaffold";
49
71
  const guardMode = resolveEffectivenessGuardMode(input.runtimeConfig, input.manifest);
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Monotonic merge gate for parallel task updates.
3
+ *
4
+ * Extracted from team-runner.ts (2026-08-10, improvement-plan Tier 2
5
+ * "team-runner split" — self-contained portion). The merge gate protects
6
+ * terminal task states from being regressed by stale parallel worker
7
+ * snapshots: every terminal->non-terminal transition is rejected, plus a
8
+ * small set of bespoke policies (P2 completed integrity, P3
9
+ * waiting->running stale-snapshot regression) that are stricter than the
10
+ * lifecycle table on the parallel-merge path.
11
+ *
12
+ * Exhaustively tested in test/unit/team-runner-should-merge-table.test.ts.
13
+ */
14
+ import { TEAM_TASK_STATUSES, TEAM_TERMINAL_TASK_STATUSES, type TeamTaskStatus } from "../state/contracts.ts";
15
+ import type { TeamTaskState } from "../state/types.ts";
16
+ import { refreshTaskGraphQueues } from "./scheduling/task-graph-scheduler.ts";
17
+
18
+ export function isNonTerminalTaskStatus(status: TeamTaskState["status"]): boolean {
19
+ return status === "queued" || status === "running" || status === "waiting";
20
+ }
21
+
22
+ export function safeFinishedAt(task: TeamTaskState): number {
23
+ if (!task.finishedAt) return -Infinity;
24
+ const ms = new Date(task.finishedAt).getTime();
25
+ return Number.isNaN(ms) ? Infinity : ms;
26
+ }
27
+
28
+ /**
29
+ * Returns true when the current task has a malformed finishedAt (NaN/Infinity)
30
+ * and the updated task has a valid finite finishedAt. Malformed finishedAt
31
+ * should be replaced rather than persisting corruption.
32
+ */
33
+ export function isMalformedFinishedAtReplacement(currentTime: number, updatedTime: number): boolean {
34
+ return !Number.isFinite(currentTime) && Number.isFinite(updatedTime);
35
+ }
36
+
37
+ /**
38
+ * RT-16: status-level gate for shouldMergeTaskUpdate. Returns the stable
39
+ * "from->to" key used by REJECTED_STATUS_MERGE_TRANSITIONS.
40
+ */
41
+ export function statusMergeKey(from: TeamTaskStatus, to: TeamTaskStatus): string {
42
+ return `${from}->${to}`;
43
+ }
44
+
45
+ /**
46
+ * RT-16 — derived merge-gate transition table.
47
+ *
48
+ * The set of old->new status pairs that shouldMergeTaskUpdate must REJECT based
49
+ * solely on the status transition (before any field-level comparison). It
50
+ * replaces the former 13 hand-written status guards that hand-duplicated the
51
+ * lifecycle table. Built once from the single source-of-truth table
52
+ * (TEAM_TASK_STATUSES + TEAM_TERMINAL_TASK_STATUSES — the terminal half of
53
+ * TEAM_TASK_STATUS_TRANSITIONS) plus two merge-specific policies that are
54
+ * STRICTER than the lifecycle table on the parallel-merge path:
55
+ *
56
+ * P1 Terminal preservation — every terminal->non-terminal pair is rejected.
57
+ * The lifecycle table permits retries (e.g. completed->queued), but a stale
58
+ * worker snapshot must never resurrect a settled task.
59
+ * P2 Completed integrity — five terminal->terminal flips that touch the
60
+ * "completed" success terminal are rejected: completed->failed,
61
+ * completed->needs_attention, failed->completed, cancelled->completed,
62
+ * needs_attention->completed. (completed may still move to
63
+ * cancelled/skipped; that is intentionally allowed, so these are NOT simply
64
+ * "every illegal terminal->terminal flip".)
65
+ * P3 waiting->running regression — the single stale-snapshot case.
66
+ *
67
+ * The decision for every old->new pair is byte-for-byte identical to the former
68
+ * 7 status guards (verified exhaustively in
69
+ * test/unit/team-runner-should-merge-table.test.ts).
70
+ */
71
+ export const REJECTED_STATUS_MERGE_TRANSITIONS: ReadonlySet<string> = (() => {
72
+ const rejected = new Set<string>();
73
+ // P1 — terminal preservation: reject every terminal->non-terminal pair.
74
+ for (const from of TEAM_TASK_STATUSES) {
75
+ if (!TEAM_TERMINAL_TASK_STATUSES.has(from)) continue;
76
+ for (const to of TEAM_TASK_STATUSES) {
77
+ if (!TEAM_TERMINAL_TASK_STATUSES.has(to)) rejected.add(statusMergeKey(from, to));
78
+ }
79
+ }
80
+ // P3 — waiting->running stale-snapshot regression.
81
+ rejected.add(statusMergeKey("waiting", "running"));
82
+ // P2 — completed integrity flips (bespoke terminal->terminal policy).
83
+ const completedIntegrityFlips: ReadonlyArray<[TeamTaskStatus, TeamTaskStatus]> = [
84
+ ["completed", "failed"],
85
+ ["completed", "needs_attention"],
86
+ ["failed", "completed"],
87
+ ["cancelled", "completed"],
88
+ ["needs_attention", "completed"],
89
+ ];
90
+ for (const [from, to] of completedIntegrityFlips) rejected.add(statusMergeKey(from, to));
91
+ return rejected;
92
+ })();
93
+
94
+ export function shouldMergeTaskUpdate(current: TeamTaskState, updated: TeamTaskState): boolean {
95
+ // RT-16: status-level gate — reject stale/dangerous transitions via the
96
+ // derived transition table (REJECTED_STATUS_MERGE_TRANSITIONS) instead of
97
+ // hand-written guards. Parallel workers receive the same input snapshot; a
98
+ // later result may still carry stale copies. The table encodes three
99
+ // merge-specific policies stricter than the lifecycle table: terminal
100
+ // preservation (no terminal->non-terminal resurrection), completed integrity
101
+ // (no flipping the "completed" success terminal to/from failed or
102
+ // needs_attention), and the waiting->running stale-snapshot regression.
103
+ if (REJECTED_STATUS_MERGE_TRANSITIONS.has(statusMergeKey(current.status, updated.status))) return false;
104
+ // Guard: when current is "running" but has resultArtifact (another worker already
105
+ // completed it), a stale updated with status="running" and no resultArtifact
106
+ // must not overwrite the actual completed state.
107
+ if (current.status === updated.status && updated.status === "running" && current.resultArtifact && !updated.resultArtifact)
108
+ return false;
109
+ // Guard: when current is "completed" and has resultArtifact but updated is also
110
+ // "completed" without resultArtifact, block the stale update from overwriting
111
+ // a task that successfully produced output.
112
+ if (current.status === updated.status && current.status === "completed" && current.resultArtifact && !updated.resultArtifact)
113
+ return false;
114
+ // Prevent a stale completed task from overwriting a fresher one.
115
+ // Restructure to handle undefined current.finishedAt as a special case:
116
+ // - undefined current + valid updated: allow the update
117
+ // - valid current + undefined updated: block the update (don't lose completion time)
118
+ // - both undefined: finishedAt guard does not apply, fall through to heartbeat check
119
+ // - both valid: compare timestamps as before
120
+ if (current.finishedAt !== undefined && updated.finishedAt !== undefined) {
121
+ const currentTime = safeFinishedAt(current);
122
+ const updatedTime = safeFinishedAt(updated);
123
+ // Malformed finishedAt (NaN) is treated as Infinity — invalid state should be
124
+ // replaced rather than persisting corruption. Log warning for visibility.
125
+ if (!Number.isFinite(currentTime)) {
126
+ console.warn(`[merge-gate] Task ${current.id} has malformed finishedAt: ${current.finishedAt}`);
127
+ }
128
+ if (isMalformedFinishedAtReplacement(currentTime, updatedTime)) {
129
+ return true;
130
+ }
131
+ if (updatedTime < currentTime) return false;
132
+ }
133
+ // Block if updated is trying to establish a terminal status without a finishedAt
134
+ // timestamp. Heartbeat-only updates (status='running', no finishedAt) are
135
+ // allowed if heartbeat has changed (checked separately in hasMeaningfulUpdate).
136
+ if (!updated.finishedAt && !isNonTerminalTaskStatus(updated.status)) return false;
137
+ // Explicitly enumerate all fields that constitute a meaningful update so that
138
+ // adding a new important field requires updating this list (rather than silently
139
+ // losing data if a field is forgotten in the boolean OR chain below).
140
+ const hasMeaningfulUpdate =
141
+ updated.status !== current.status ||
142
+ updated.finishedAt !== current.finishedAt ||
143
+ updated.startedAt !== current.startedAt ||
144
+ Boolean(updated.resultArtifact) !== Boolean(current.resultArtifact) ||
145
+ (Boolean(updated.resultArtifact) && updated.resultArtifact !== current.resultArtifact) ||
146
+ Boolean(updated.error) ||
147
+ Boolean(updated.modelAttempts?.length) ||
148
+ Boolean(updated.usage) ||
149
+ Boolean(updated.attempts?.length) ||
150
+ updated.heartbeat?.lastSeenAt !== current.heartbeat?.lastSeenAt ||
151
+ updated.jsonEvents !== current.jsonEvents ||
152
+ updated.agentProgress?.lastActivityAt !== current.agentProgress?.lastActivityAt;
153
+ return hasMeaningfulUpdate;
154
+ }
155
+ /** Exposed for the exhaustive status-merge table test (RT-16). */
156
+ export const __test__shouldMergeTaskUpdate = shouldMergeTaskUpdate;
157
+
158
+ // H4 fix: rename to descriptive name. Kept __test__ as alias for backward
159
+ // compat test imports.
160
+ // FIX (perf P10): replace O(N×M) .find() + .map() inside nested loops with a
161
+ // single-pass Map-based merge. Build an index of `merged` once, then for each
162
+ // incoming updated task do O(1) lookup; the final pass reassembles `merged`
163
+ // preserving original order. For a 20-task run × 5-batch merger with
164
+ // ~10 updates per result, this reduces from O(50×20) = 1000 ops to O(120).
165
+ // Behavior is unchanged: skipped updates (shouldMergeTaskUpdate=false) still
166
+ // leave the existing task in place.
167
+ export function mergeTaskUpdatesPreservingTerminal(base: TeamTaskState[], results: Array<{ tasks: TeamTaskState[] }>): TeamTaskState[] {
168
+ // Index current merged state by id for O(1) lookup during the merge pass.
169
+ const indexById = new Map<string, TeamTaskState>();
170
+ for (const task of base) indexById.set(task.id, task);
171
+
172
+ let skipped = 0;
173
+ for (const result of results) {
174
+ for (const updated of result.tasks) {
175
+ const current = indexById.get(updated.id);
176
+ if (!current) continue;
177
+ if (!shouldMergeTaskUpdate(current, updated)) {
178
+ // Log skipped merges for visibility into rejected parallel updates.
179
+ // In distributed systems with parallel workers, rejected merges may
180
+ // indicate bugs (wrong status, timestamp corruption) if they accumulate.
181
+ console.debug("[merge-gate] Skipping stale merge for task", updated.id, {
182
+ currentStatus: current.status,
183
+ updatedStatus: updated.status,
184
+ currentFinishedAt: current.finishedAt,
185
+ updatedFinishedAt: updated.finishedAt,
186
+ });
187
+ skipped += 1;
188
+ continue;
189
+ }
190
+ indexById.set(updated.id, updated);
191
+ }
192
+ }
193
+ // Reassemble in original `base` order so downstream snapshots stay stable.
194
+ const merged = base.map((task) => indexById.get(task.id) ?? task);
195
+ // `skipped` is intentional visibility — currently no caller reads it but
196
+ // we'd rather leave the count available for future instrumentation than
197
+ // remove the cumulative silent-rejection signal it provides.
198
+ void skipped;
199
+ return refreshTaskGraphQueues(merged);
200
+ }
201
+ /** @deprecated Use mergeTaskUpdatesPreservingTerminal. Kept for backward test import compat. */
202
+ export const __test__mergeTaskUpdates = mergeTaskUpdatesPreservingTerminal;
@@ -365,6 +365,17 @@ const RETRYABLE_MODEL_FAILURE_PATTERNS = [
365
365
  /safety/i,
366
366
  /is[_ ]?overloaded/i,
367
367
  /\b408\b/,
368
+ //
369
+ // EPIPE / broken-pipe. In the child-pi worker path this typically means
370
+ // the child `pi` process exited (crash or early exit) while the parent
371
+ // was still writing to its stdin — spawning a fresh child on the next
372
+ // model in the fallback chain usually recovers. In the network path it
373
+ // is a transient pipe close. Both are retryable on a different model.
374
+ // See docs/failure-mode-inventory.md EPIPE gap; NON_RETRYABLE patterns
375
+ // (auth/billing) are checked first, so an auth error mentioning EPIPE
376
+ // stays non-retryable.
377
+ /epipe/i,
378
+ /broken pipe/i,
368
379
  ];
369
380
 
370
381
  // These patterns indicate auth/key/billing issues that will never succeed on retry.
@@ -31,7 +31,7 @@ const createdTempDirs = new Set<string>();
31
31
  * /tmp directory. Uses `userPiRoot()` so the path stays consistent with
32
32
  * the rest of pi-crew (respects PI_TEAMS_HOME / PI_CODING_AGENT_DIR).
33
33
  */
34
- function getPiTempBase(): string {
34
+ export function getPiTempBase(): string {
35
35
  return path.join(userPiRoot(), "tmp");
36
36
  }
37
37
 
@@ -22,7 +22,7 @@
22
22
  */
23
23
  import * as fs from "node:fs";
24
24
  import * as path from "node:path";
25
- import { userPiRoot } from "../../utils/paths.ts";
25
+ import { packageRoot, userPiRoot } from "../../utils/paths.ts";
26
26
 
27
27
  export interface DiscoveredProviderExtension {
28
28
  /** Package specifier as written in settings.json packages (e.g. "npm:pi-commandcode-provider"). */
@@ -95,12 +95,17 @@ function resolvePackageEntry(pkgDir: string): string | undefined {
95
95
 
96
96
  /**
97
97
  * Discover provider extension entry points from Pi's installed package registry.
98
- * Reads `~/.pi/agent/settings.json` → `packages` (npm: specs only — local
99
- * relative paths are the pi-crew extension itself and other local work, which
100
- * children already handle) and resolves each against `~/.pi/agent/npm/node_modules/`.
98
+ * Reads `~/.pi/agent/settings.json` → `packages` and resolves each spec:
99
+ * - `npm:<name>` → `~/.pi/agent/npm/node_modules/<name>/`
100
+ * - local path spec → resolved relative to the settings.json dir (the way
101
+ * `pi install <local-path>` records them), e.g. "../../src/foo"
102
+ * - git:/file: specs → skipped (not resolvable on disk)
101
103
  *
102
- * Non-npm specs (local paths, git URLs) are skipped: they are not part of the
103
- * npm registry dir and resolving them is outside this module's scope.
104
+ * Both npm: and local-path specs are SANCTIONED channels — the user wrote them
105
+ * into settings.json (directly or via `pi install`), so they are trusted at the
106
+ * same level. This is distinct from project-sourced AGENT extensions
107
+ * (`.crew/agents/*.md` `extensions:` frontmatter), which are repo-adjacent
108
+ * untrusted data and stay gated by SEC-1 in discover-agents.ts.
104
109
  */
105
110
  export function discoverProviderExtensions(settingsPath?: string): DiscoveredProviderExtension[] {
106
111
  const root = userPiRoot();
@@ -125,13 +130,27 @@ export function discoverProviderExtensions(settingsPath?: string): DiscoveredPro
125
130
  const npmBase = path.join(baseDir, "npm", "node_modules");
126
131
  for (const spec of settings.packages ?? []) {
127
132
  if (typeof spec !== "string") continue;
128
- // Only npm: specs resolve into the npm registry dir. Skip local path
129
- // packages (e.g. "../../source/my_pi/pi-crew") and git specs.
130
- if (!spec.startsWith("npm:")) continue;
131
- const pkgName = spec.slice(4);
132
- // Scoped packages: "@scope/name" → "@scope/name"; plain: "name".
133
- const pkgDir = path.join(npmBase, pkgName);
133
+ let pkgDir: string;
134
+ if (spec.startsWith("npm:")) {
135
+ // Scoped packages: "@scope/name" → "@scope/name"; plain: "name".
136
+ pkgDir = path.join(npmBase, spec.slice(4));
137
+ } else if (spec.startsWith("./") || spec.startsWith("../") || path.isAbsolute(spec)) {
138
+ // Local path spec — resolve relative to the settings.json dir, matching
139
+ // how `pi install <local-path>` records it. Same trust level as npm:
140
+ // (user wrote it into settings.json). Not to be confused with project
141
+ // AGENT extensions (.crew/agents/* frontmatter) — those stay SEC-1 gated.
142
+ pkgDir = path.resolve(baseDir, spec);
143
+ } else {
144
+ // git:/file:/http: specs etc. — not resolvable on disk, skip.
145
+ continue;
146
+ }
134
147
  if (!fs.existsSync(pkgDir)) continue;
148
+ // Skip self: pi-crew's own package is a settings package (the orchestrator
149
+ // extension the parent loads), but a child WORKER must not re-load it — it
150
+ // would register the team tool / observability / MCP wiring intended for
151
+ // the orchestrator process, not a worker. Provider + adapter extensions
152
+ // (pi-other-provider, pi-mcp-adapter, pi-rlm, ...) stay.
153
+ if (path.resolve(pkgDir) === path.resolve(packageRoot())) continue;
135
154
  const entryPath = resolvePackageEntry(pkgDir);
136
155
  if (entryPath) out.push({ spec, entryPath });
137
156
  }
@@ -1,5 +1,4 @@
1
1
  import type { AgentSessionEvent } from "@earendil-works/pi-coding-agent";
2
- import { crewEventBus } from "../../observability/event-bus.ts";
3
2
 
4
3
  export interface AgentProgress {
5
4
  toolCalls: number;
@@ -25,7 +24,7 @@ export class ProgressTracker {
25
24
  subscribe: (listener: (event: AgentSessionEvent) => void) => () => void;
26
25
  },
27
26
  agentId: string,
28
- runId: string,
27
+ _runId: string,
29
28
  ): AgentProgress {
30
29
  if (this.sessions.has(agentId)) {
31
30
  return this.sessions.get(agentId)!.progress;
@@ -42,26 +41,19 @@ export class ProgressTracker {
42
41
  };
43
42
 
44
43
  const unsubscribe = session.subscribe((event: AgentSessionEvent) => {
45
- this.handleEvent(event, progress, agentId, runId);
44
+ this.handleEvent(event, progress);
46
45
  });
47
46
 
48
47
  this.sessions.set(agentId, { unsubscribe, progress });
49
48
  return progress;
50
49
  }
51
50
 
52
- private handleEvent(event: AgentSessionEvent, progress: AgentProgress, agentId: string, runId: string): void {
51
+ private handleEvent(event: AgentSessionEvent, progress: AgentProgress): void {
53
52
  switch (event.type) {
54
53
  case "tool_execution_start":
55
54
  progress.toolCalls++;
56
55
  progress.currentTool = event.toolName;
57
56
  progress.toolStartTime = Date.now();
58
- crewEventBus.emit({
59
- type: "agent:progress",
60
- runId,
61
- agentId,
62
- payload: { ...progress },
63
- timestamp: Date.now(),
64
- });
65
57
  break;
66
58
 
67
59
  case "tool_execution_end":
@@ -69,21 +61,7 @@ export class ProgressTracker {
69
61
  progress.toolStartTime = null;
70
62
  if (event.isError) {
71
63
  progress.errors.push(String(event.result ?? "Unknown error"));
72
- crewEventBus.emit({
73
- type: "agent:error",
74
- runId,
75
- agentId,
76
- payload: String(event.result ?? "Unknown error"),
77
- timestamp: Date.now(),
78
- });
79
64
  }
80
- crewEventBus.emit({
81
- type: "agent:progress",
82
- runId,
83
- agentId,
84
- payload: { ...progress },
85
- timestamp: Date.now(),
86
- });
87
65
  break;
88
66
 
89
67
  case "turn_start":
@@ -92,13 +70,6 @@ export class ProgressTracker {
92
70
 
93
71
  case "agent_end":
94
72
  progress.status = "completed";
95
- crewEventBus.emit({
96
- type: "agent:complete",
97
- runId,
98
- agentId,
99
- payload: { ...progress },
100
- timestamp: Date.now(),
101
- });
102
73
  break;
103
74
 
104
75
  case "agent_start":
@@ -120,5 +91,4 @@ export class ProgressTracker {
120
91
  }
121
92
  }
122
93
 
123
- // Export singleton instance
124
94
  export const globalProgressTracker = new ProgressTracker();
@@ -98,7 +98,7 @@ export function readManifestWithTransientRetry(manifestPath: string, maxRetries
98
98
  throw new Error(`unreachable: readManifestWithTransientRetry exhausted for ${manifestPath}`);
99
99
  }
100
100
 
101
- function shouldRecoverTask(task: TeamTaskState, deadMs: number): boolean {
101
+ export function shouldRecoverTask(task: TeamTaskState, deadMs: number): boolean {
102
102
  if (task.status !== "running") return false;
103
103
  if (!task.heartbeat) return true;
104
104
  return task.heartbeat.alive === false || isWorkerHeartbeatStale(task.heartbeat, deadMs);