pi-crew 0.11.23 → 0.11.25

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 (156) hide show
  1. package/CHANGELOG.md +72 -0
  2. package/README.md +2 -2
  3. package/agents/designer.md +23 -0
  4. package/agents/explorer.md +3 -3
  5. package/agents/librarian.md +3 -3
  6. package/agents/orchestrator.md +17 -0
  7. package/dist/index.mjs +567 -555
  8. package/{workflows/chain.workflow.md → docs/chain-workflow.md} +8 -0
  9. package/docs/dynamic-workflows.md +37 -2
  10. package/docs/publishing.md +34 -5
  11. package/docs/troubleshooting.md +50 -2
  12. package/docs/usage.md +7 -2
  13. package/install.mjs +2 -1
  14. package/package.json +6 -4
  15. package/skills/read-only-explorer/SKILL.md +15 -5
  16. package/skills/real-test-pi-crew/SKILL.md +7 -7
  17. package/src/agents/discover-agents.ts +8 -2
  18. package/src/config/config-validation.ts +61 -1
  19. package/src/config/config.ts +90 -5
  20. package/src/config/env-allowlist-registry.ts +5 -0
  21. package/src/config/env-vars.ts +23 -13
  22. package/src/config/role-tools.ts +14 -11
  23. package/src/config/types.ts +4 -1
  24. package/src/extension/crew-cleanup.ts +6 -0
  25. package/src/extension/management.ts +7 -0
  26. package/src/extension/project-init.ts +19 -1
  27. package/src/extension/register.ts +83 -30
  28. package/src/extension/registration/artifact-cleanup.ts +52 -11
  29. package/src/extension/registration/commands/manage.ts +9 -2
  30. package/src/extension/registration/hook-registration.ts +3 -0
  31. package/src/extension/registration/lazy-configurers.ts +2 -0
  32. package/src/extension/registration/lifecycle-handlers.ts +79 -18
  33. package/src/extension/registration/render-loop.ts +27 -2
  34. package/src/extension/registration/tool-loop-guard.ts +5 -3
  35. package/src/extension/registration/verify-gate-tool.ts +7 -3
  36. package/src/extension/run-index.ts +24 -1
  37. package/src/extension/team-tool/api/read.ts +12 -3
  38. package/src/extension/team-tool/cancel.ts +11 -0
  39. package/src/extension/team-tool/config-patch.ts +56 -11
  40. package/src/extension/team-tool/destructive-gate.ts +268 -0
  41. package/src/extension/team-tool/dispatch/manage.ts +9 -1
  42. package/src/extension/team-tool/goal-wrap.ts +11 -1
  43. package/src/extension/team-tool/goal.ts +13 -1
  44. package/src/extension/team-tool/handle-schedule.ts +38 -4
  45. package/src/extension/team-tool/handle-settings.ts +76 -6
  46. package/src/extension/team-tool/health-monitor.ts +14 -2
  47. package/src/extension/team-tool/lifecycle-actions.ts +28 -10
  48. package/src/extension/team-tool/respond.ts +12 -5
  49. package/src/extension/team-tool/run-deadline.ts +29 -2
  50. package/src/extension/team-tool/run.ts +11 -0
  51. package/src/extension/team-tool/status.ts +64 -8
  52. package/src/extension/team-tool/workflow-manage.ts +5 -1
  53. package/src/extension/team-tool.ts +13 -0
  54. package/src/i18n.ts +18 -14
  55. package/src/observability/exporters/otlp-exporter.ts +11 -1
  56. package/src/prompt/inbox-poll.ts +14 -2
  57. package/src/prompt/message-tool.ts +1 -1
  58. package/src/prompt/prompt-runtime.ts +127 -21
  59. package/src/prompt/scratchpad-lifecycle.ts +14 -13
  60. package/src/prompt/surface-worker.ts +35 -9
  61. package/src/runtime/broker/crew-broker-client.ts +7 -0
  62. package/src/runtime/broker/crew-broker.ts +216 -242
  63. package/src/runtime/broker/delegate/delegate-abort.ts +118 -0
  64. package/src/runtime/broker/delegate/delegate-event.ts +3 -1
  65. package/src/runtime/broker/delegate/delegate-finalize.ts +179 -0
  66. package/src/runtime/broker/protocol/connection-state.ts +159 -1
  67. package/src/runtime/broker/protocol/events-replay.ts +50 -10
  68. package/src/runtime/broker/protocol/wait-resolve.ts +71 -5
  69. package/src/runtime/budget-enforcement.ts +26 -6
  70. package/src/runtime/chain-parser.ts +5 -3
  71. package/src/runtime/chain-runner.ts +41 -22
  72. package/src/runtime/child-pi/child-pi-constants.ts +9 -0
  73. package/src/runtime/child-pi/child-pi-rpc.ts +72 -7
  74. package/src/runtime/child-pi/child-pi-streams.ts +19 -3
  75. package/src/runtime/child-pi/mock-fixtures.ts +12 -5
  76. package/src/runtime/child-pi/postmortem-registry.ts +8 -2
  77. package/src/runtime/classifier/ambient-noise-gate.ts +5 -1
  78. package/src/runtime/compaction/tool-output-pruner.ts +28 -5
  79. package/src/runtime/crew-agent-records.ts +67 -4
  80. package/src/runtime/custom-tools/submit-result-tool.ts +17 -0
  81. package/src/runtime/deadletter.ts +5 -1
  82. package/src/runtime/delegate-spawn.ts +61 -8
  83. package/src/runtime/dispatch-batch.ts +61 -14
  84. package/src/runtime/effectiveness.ts +55 -4
  85. package/src/runtime/finalize-run.ts +12 -1
  86. package/src/runtime/goal-workflow/dynamic-workflow-context.ts +12 -3
  87. package/src/runtime/goal-workflow/goal-evaluator.ts +30 -5
  88. package/src/runtime/goal-workflow/goal-loop-runner.ts +60 -3
  89. package/src/runtime/live-session/live-agent-control.ts +123 -20
  90. package/src/runtime/live-session/live-agent-manager.ts +114 -15
  91. package/src/runtime/live-session/live-session-runtime.ts +75 -83
  92. package/src/runtime/live-session/usage-bridge.ts +66 -2
  93. package/src/runtime/mcp-proxy.ts +10 -0
  94. package/src/runtime/model/model-fallback.ts +74 -9
  95. package/src/runtime/model/pi-args.ts +3 -3
  96. package/src/runtime/model/runtime-policy.ts +22 -6
  97. package/src/runtime/model/runtime-resolver.ts +11 -5
  98. package/src/runtime/orphan-worker-registry.ts +64 -24
  99. package/src/runtime/pi-spawn.ts +4 -1
  100. package/src/runtime/process/orphan-reaper.ts +143 -0
  101. package/src/runtime/process-status.ts +38 -16
  102. package/src/runtime/recovery/crash-recovery.ts +35 -2
  103. package/src/runtime/resilient-edit.ts +170 -93
  104. package/src/runtime/role-permission.ts +3 -1
  105. package/src/runtime/run-tracker.ts +9 -1
  106. package/src/runtime/sanitize/worker-text.ts +98 -0
  107. package/src/runtime/scheduling/batch-barrier.ts +42 -5
  108. package/src/runtime/scheduling/run-coalesced-task-group.ts +82 -28
  109. package/src/runtime/scheduling/scheduler.ts +74 -29
  110. package/src/runtime/spawn-policy.ts +190 -8
  111. package/src/runtime/stale-reconciler.ts +77 -6
  112. package/src/runtime/subagent-manager.ts +29 -0
  113. package/src/runtime/surface/herdr-provider.ts +85 -2
  114. package/src/runtime/surface/launch-script.ts +7 -1
  115. package/src/runtime/surface/resolve-surface.ts +4 -1
  116. package/src/runtime/surface/tmux-provider.ts +32 -2
  117. package/src/runtime/task-output-context.ts +58 -11
  118. package/src/runtime/task-runner/child-executor.ts +3 -2
  119. package/src/runtime/task-runner/post-execution.ts +144 -0
  120. package/src/runtime/task-runner/prompt-builder.ts +41 -8
  121. package/src/runtime/task-runner.ts +16 -3
  122. package/src/runtime/verification/classifier-pre-gate.ts +27 -6
  123. package/src/runtime/verification/verify-gate.ts +153 -30
  124. package/src/runtime/workspace-lock.ts +52 -4
  125. package/src/state/atomic-write.ts +73 -4
  126. package/src/state/contracts.ts +9 -0
  127. package/src/state/coordination/mailbox.ts +40 -11
  128. package/src/state/coordination/process-lock.ts +50 -0
  129. package/src/state/event-log/cursor.ts +10 -1
  130. package/src/state/event-log/event-log-rotation.ts +54 -8
  131. package/src/state/event-log/poison-marker.ts +16 -1
  132. package/src/state/event-log/worker-atomic-writer.ts +5 -0
  133. package/src/state/stores/active-run-registry.ts +56 -5
  134. package/src/state/stores/artifact-store.ts +181 -6
  135. package/src/state/stores/manifest-io.ts +126 -8
  136. package/src/state/stores/ownership-map.ts +26 -7
  137. package/src/state/stores/plan-store.ts +30 -1
  138. package/src/state/stores/sqlite-run-state.ts +91 -1
  139. package/src/state/stores/state-store.ts +30 -31
  140. package/src/state/types.ts +18 -0
  141. package/src/state/usage.ts +41 -0
  142. package/src/ui/dashboard-panes/agents-pane.ts +4 -3
  143. package/src/ui/dashboard-panes/plan-pane.ts +5 -3
  144. package/src/ui/dashboard-panes/transcript-pane.ts +2 -1
  145. package/src/ui/overlays/mailbox-detail-overlay.ts +5 -3
  146. package/src/ui/run-dashboard.ts +9 -7
  147. package/src/ui/run-snapshot-cache.ts +3 -2
  148. package/src/ui/settings-overlay.ts +87 -11
  149. package/src/ui/theme-discovery.ts +26 -7
  150. package/src/ui/transcript-viewer.ts +5 -3
  151. package/src/utils/internal-error.ts +5 -1
  152. package/src/utils/timer-limits.ts +30 -0
  153. package/src/utils/token-counter.ts +47 -0
  154. package/src/utils/visual.ts +84 -0
  155. package/src/workflows/discover-workflows.ts +9 -1
  156. package/src/worktree/worktree-manager.ts +128 -18
@@ -27,6 +27,8 @@ import {
27
27
  } from "../../extension/run-maintenance.ts";
28
28
  import { type BrokerSpawnCredentials, setActiveBrokerIssuer, setActiveBrokerRevoker } from "../../runtime/broker/broker-issuer.ts";
29
29
  import { CrewBroker } from "../../runtime/broker/crew-broker.ts";
30
+ import { setActiveDelegateAborter } from "../../runtime/broker/delegate/delegate-abort.ts";
31
+ import { DEFAULT_LOCK_BUSY_RETRY_DELAYS_MS, withRunLockBusyRetryAsync } from "../../runtime/broker/protocol/lock-busy.ts";
30
32
  import { terminateActiveChildPiProcesses } from "../../runtime/child-pi/child-pi.ts";
31
33
  import type { createManifestCache } from "../../runtime/manifest-cache.ts";
32
34
  import { configuredModelInfosFromPiConfig } from "../../runtime/model/model-fallback.ts";
@@ -40,7 +42,8 @@ import { tryRegisterSessionCleanup } from "../../runtime/session-resources.ts";
40
42
  import { createSessionSnapshot } from "../../runtime/session-snapshot.ts";
41
43
  import { applyCrewSettingsTiersToConfig, loadCrewSettingsTiers, scheduledJobsHiddenCountOf } from "../../runtime/settings-store.ts";
42
44
  import type { ReconcileResult } from "../../runtime/stale-reconciler.ts";
43
- import { loadRunManifestById } from "../../state/stores/state-store.ts";
45
+ import { loadRunManifestById, saveRunManifest } from "../../state/stores/state-store.ts";
46
+ import type { TeamRunManifest } from "../../state/types.ts";
44
47
  import { installInlinePanel } from "../../ui/inline-panel/index.ts";
45
48
  import { clearSessionSwitchInFlight, markSessionSwitchInFlight } from "../../ui/inline-panel/view-session-store.ts";
46
49
  import { registerPiCrewPowerbarSegments, resetPowerbarDedupState, updatePiCrewPowerbar } from "../../ui/powerbar-publisher.ts";
@@ -532,6 +535,56 @@ async function runDeferredSessionCleanup(
532
535
  }
533
536
  }
534
537
 
538
+ /** Manifest keys the scheduler stamps for traceability. They are NOT declared
539
+ * on TeamRunManifest (carried as extra keys, exactly like the pre-fix inline
540
+ * write did) — the provenance stamp is advisory metadata, never read back by
541
+ * any consumer. */
542
+ type SchedulerProvenanceFields = { schedulerJobId: string; schedulerName: string };
543
+
544
+ /**
545
+ * M3-7 (R22-F2 / F22-2, fix-wave 6): stamp scheduler provenance on a scheduled
546
+ * run's manifest WITHOUT racing the runner.
547
+ *
548
+ * Pre-fix the executor did `atomicWriteJson(stateRoot + "/manifest.json",
549
+ * {...loaded.manifest, schedulerJobId, schedulerName})` — a RAW write that
550
+ * (a) bypassed run.lock, so it interleaved with team-runner's saveRunManifest /
551
+ * saveRunManifestAsync, and (b) bypassed the write-layer terminal-preserve
552
+ * guard, so a cancel/reconciler flip landing in the window was RESURRECTED to
553
+ * "running" while usage/artifacts were reverted to the pre-write snapshot. It
554
+ * fired on EVERY scheduled fire.
555
+ *
556
+ * Fix: re-read INSIDE run.lock (bounded busy-retry) and persist through
557
+ * `saveRunManifest`, which applies `preserveDiskTerminalStatus` (never erases a
558
+ * terminal disk status) and invalidates/repopulates the manifest cache. Read +
559
+ * write under the same lock closes the lost-update window by construction.
560
+ *
561
+ * Best-effort by contract (provenance must never break a scheduled fire): a
562
+ * pruned run, exhausted lock budget or write fault logs at warn and returns
563
+ * false.
564
+ *
565
+ * @internal exported for the M3-7 unit pin
566
+ * (test/unit/runtime/scheduling/scheduler-provenance-lock.test.ts).
567
+ */
568
+ export async function stampSchedulerRunProvenance(cwd: string, runId: string, job: { id: string; name: string }): Promise<boolean> {
569
+ try {
570
+ const loaded = loadRunManifestById(cwd, runId);
571
+ if (!loaded) return false; // run pruned/unknown — nothing to stamp
572
+ const provenance: SchedulerProvenanceFields = { schedulerJobId: job.id, schedulerName: job.name };
573
+ await withRunLockBusyRetryAsync(loaded.manifest, DEFAULT_LOCK_BUSY_RETRY_DELAYS_MS, async () => {
574
+ // Fresh reload INSIDE the lock (respond.ts:42-43 discipline): outside
575
+ // would reintroduce the exact lost-update window being closed.
576
+ const fresh = loadRunManifestById(loaded.manifest.cwd, runId);
577
+ if (!fresh) return; // pruned between the outer load and the lock
578
+ const next: TeamRunManifest & SchedulerProvenanceFields = { ...fresh.manifest, ...provenance };
579
+ saveRunManifest(next);
580
+ });
581
+ return true;
582
+ } catch (error) {
583
+ logInternalError("scheduler.stamp-provenance", error, `runId=${runId} jobId=${job.id}`, "warn");
584
+ return false;
585
+ }
586
+ }
587
+
535
588
  /**
536
589
  * Build a CrewScheduler wired to the current session. The scheduler's
537
590
  * executor closure invokes handleTeamTool lazily — the heavy team-tool
@@ -596,22 +649,11 @@ export function setupCrewScheduler(
596
649
  const runId = runResult?.details?.runId;
597
650
  if (runId && typeof runId === "string") {
598
651
  crewScheduler?.recordSpawnedRun(job.id, runId);
599
- // Update run manifest with scheduler provenance for traceability
600
- try {
601
- const cwd = extensionCtx.cwd ?? process.cwd();
602
- const loaded = loadRunManifestById(cwd, runId);
603
- if (loaded) {
604
- // LAZY: defer dynamic import of atomic-write.ts to its call site.
605
- const { atomicWriteJson } = await import("../../state/atomic-write.ts");
606
- atomicWriteJson(loaded.manifest.stateRoot + "/manifest.json", {
607
- ...loaded.manifest,
608
- schedulerJobId: job.id,
609
- schedulerName: job.name,
610
- });
611
- }
612
- } catch {
613
- /* best-effort provenance tracking */
614
- }
652
+ // Update run manifest with scheduler provenance for traceability.
653
+ // M3-7 (R22-F2): lock-held + terminal-preserving (was a raw
654
+ // atomicWriteJson outside run.lock that could resurrect a
655
+ // terminal status). Best-effort by contract.
656
+ await stampSchedulerRunProvenance(extensionCtx.cwd ?? process.cwd(), runId, job);
615
657
  }
616
658
  // R6-2(b) + I6 (round-6 audit): update state FIRST, then persist the
617
659
  // UPDATED job. The old order persisted the fire-time snapshot (still
@@ -690,7 +732,7 @@ export interface CrewBrokerLifecycleController {
690
732
  }
691
733
 
692
734
  function isRootSession(env: NodeJS.ProcessEnv = process.env): boolean {
693
- if (env.PI_CREW_KIND === "subagent") return false;
735
+ if (getCrewEnv("PI_CREW_KIND", env) === "subagent") return false;
694
736
  try {
695
737
  return currentCrewDepth(env) === 0;
696
738
  } catch {
@@ -847,6 +889,24 @@ export function installCrewBrokerLifecycleController(_pi: ExtensionAPI, _ctx: Re
847
889
  };
848
890
  setActiveBrokerRevoker(revokeForTask);
849
891
 
892
+ // M3-5 / R43-N3: publish the delegate canceller alongside the revoker. The
893
+ // run-terminal paths (budget abort / cancellation → budget-enforcement.ts
894
+ // terminaliseRunWithDrain) call it so in-flight DELEGATE grandchildren are
895
+ // aborted instead of outliving the run to their own 24h deadline. Same
896
+ // self-gating as the revoker: no bound broker ⇒ nothing to cancel.
897
+ const abortDelegatesForRun = (runId: string, reason: string): number => {
898
+ if (!runId || typeof runId !== "string") return 0;
899
+ if (!isRootSession(process.env)) return 0;
900
+ if (!broker || brokerSessionId !== cachedSessionId) return 0;
901
+ try {
902
+ return broker.abortDelegatesForRun(runId, reason);
903
+ } catch (error) {
904
+ logInternalError("broker.abort-delegates", error instanceof Error ? error : new Error(String(error)), `runId=${runId}`, "warn");
905
+ return 0;
906
+ }
907
+ };
908
+ setActiveDelegateAborter(abortDelegatesForRun);
909
+
850
910
  // Publish this issuer as the process-local active issuer so runChildPi can
851
911
  // default `brokerIssuer` without the registration context being threaded
852
912
  // through every runner call site. The issuer self-gates (root + flag), so
@@ -858,6 +918,7 @@ export function installCrewBrokerLifecycleController(_pi: ExtensionAPI, _ctx: Re
858
918
  stop: async () => {
859
919
  setActiveBrokerIssuer(undefined);
860
920
  setActiveBrokerRevoker(undefined);
921
+ setActiveDelegateAborter(undefined);
861
922
  if (broker) {
862
923
  try {
863
924
  await broker.stop();
@@ -19,7 +19,7 @@ import { forgetDetachedRun, hasDetachedRuns, peekFinishedDetachedRunResults } fr
19
19
  import { listLiveAgents } from "../../runtime/live-session/live-agent-manager.ts";
20
20
  import type { createManifestCache } from "../../runtime/manifest-cache.ts";
21
21
  import { loadTasksWithRecovery } from "../../state/stores/manifest-io.ts";
22
- import type { TeamRunManifest } from "../../state/types.ts";
22
+ import type { TeamRunManifest, TeamTaskState } from "../../state/types.ts";
23
23
  import { overlayFreshTaskStatuses, summarizeHeartbeats } from "../../ui/heartbeat-aggregator.ts";
24
24
  import { requestRender, setExtensionWidget, toPiWidgetPlacement } from "../../ui/pi-ui-compat.ts";
25
25
  import { requestPowerbarUpdate } from "../../ui/powerbar-publisher.ts";
@@ -27,6 +27,7 @@ import { RenderScheduler } from "../../ui/render-scheduler.ts";
27
27
  import { runEventBus } from "../../ui/run-event-bus.ts";
28
28
  import type { createRunSnapshotCache } from "../../ui/run-snapshot-cache.ts";
29
29
  import { updateCrewWidget } from "../../ui/widget/index.ts";
30
+ import { readJsonFileCoalesced } from "../../utils/file-coalescer.ts";
30
31
  import { logInternalError } from "../../utils/internal-error.ts";
31
32
  import { projectCrewRoot, userCrewRoot } from "../../utils/paths.ts";
32
33
  import { RunWatcherRegistry } from "../../utils/run-watcher-registry.ts";
@@ -128,6 +129,30 @@ export function applyTerminalRunEventToManifests(manifests: TeamRunManifest[], e
128
129
  return isTerminalRunEventType(event.type) ? evictRunFromManifests(manifests, event.runId) : manifests;
129
130
  }
130
131
 
132
+ /**
133
+ * R15-P1: GATE 3 used to call `loadTasksWithRecovery` — an uncached
134
+ * `readFileSync` + `JSON.parse` of the FULL task packet array (~8.4 KB/task) —
135
+ * once per running run on every 160 ms tick. Measured 14.78 ms/call at 200
136
+ * tasks, i.e. ~28% of the tick blocked on the event loop while runs were live.
137
+ *
138
+ * The read now goes through the in-tree coalesced reader (stat-gated on
139
+ * mtime+size, LRU-capped, short TTL): a tick that sees an unchanged file pays a
140
+ * syscall instead of a parse. The TTL is far below the stale (60 s) / dead
141
+ * (5 min) thresholds GATE 3 protects, and the quarantine/reconstruct path stays
142
+ * intact and self-invalidating — a recovered file is rewritten, so its mtime
143
+ * changes and the next tick misses the cache.
144
+ */
145
+ const TASKS_READ_TTL_MS = 200;
146
+
147
+ /**
148
+ * Read a run's tasks.json through the coalesced reader (GATE 3 truth source).
149
+ * Exported so the cache behaviour (and its freshness/quarantine invariants) can
150
+ * be pinned directly — see test/unit/extension/registration/render-loop-tasks-read-cache.test.ts.
151
+ */
152
+ export function readTasksCoalesced(tasksPath: string, eventsPath: string, runId: string): TeamTaskState[] {
153
+ return readJsonFileCoalesced(tasksPath, TASKS_READ_TTL_MS, () => loadTasksWithRecovery(tasksPath, eventsPath, runId));
154
+ }
155
+
131
156
  /**
132
157
  * Build the render scheduler + preload loop + bounded run watchers.
133
158
  *
@@ -385,7 +410,7 @@ export function setupRenderLoop(
385
410
  // "dead worker" (live: team_20260923100114, 01_explore parked on
386
411
  // ask). Overlay FRESH statuses before summarizing; divergence also
387
412
  // schedules an in-place rebuild so the cache syncs off the tick.
388
- const freshTasks = loadTasksWithRecovery(freshManifest.tasksPath, freshManifest.eventsPath, run.runId);
413
+ const freshTasks = readTasksCoalesced(freshManifest.tasksPath, freshManifest.eventsPath, run.runId);
389
414
  const overlaid = overlayFreshTaskStatuses(snapshot, freshTasks);
390
415
  if (overlaid !== snapshot) refreshSnapshotInPlace(run.runId);
391
416
  const summary = summarizeHeartbeats(overlaid, { now });
@@ -215,7 +215,9 @@ export function createLoopGuardState() {
215
215
  /**
216
216
  * Install the loop guard on a Pi instance. Both hooks are best-effort:
217
217
  * older Pi versions without these events are tolerated (same pattern as
218
- * installResourcesDiscoverHook).
218
+ * installResourcesDiscoverHook). R49-F4: `pi.on()` never THROWS on an unknown
219
+ * event name (pi 0.77/1.0.4 verified) — so the try/catch below is defensive
220
+ * only, and the real older-host degradation is a NEVER-FIRING hook.
219
221
  */
220
222
  export function installToolLoopGuard(pi: ExtensionAPI): void {
221
223
  const state = createLoopGuardState();
@@ -228,7 +230,7 @@ export function installToolLoopGuard(pi: ExtensionAPI): void {
228
230
  return undefined;
229
231
  });
230
232
  } catch {
231
- /* older Pi without tool_call events */
233
+ /* R49-F4: defensive only — pi.on() does not throw on unknown events; the hook simply never fires */
232
234
  }
233
235
  try {
234
236
  pi.on("tool_result", (event: { toolName: string; input?: unknown; content?: unknown }) => {
@@ -238,6 +240,6 @@ export function installToolLoopGuard(pi: ExtensionAPI): void {
238
240
  return { content: [...existing, ...appends] };
239
241
  });
240
242
  } catch {
241
- /* older Pi without tool_result events */
243
+ /* R49-F4: defensive only — pi.on() does not throw on unknown events; the hook simply never fires */
242
244
  }
243
245
  }
@@ -33,7 +33,6 @@ import { type Static, Type } from "@sinclair/typebox";
33
33
  import type { MetricRegistry } from "../../observability/metric-registry.ts";
34
34
  import {
35
35
  DEFAULT_VERIFY_GATE_TIMEOUT_MS,
36
- defaultVerifyGateChecks,
37
36
  renderVerifyGateContext,
38
37
  runVerifyGate,
39
38
  type VerifyGateCheck,
@@ -59,7 +58,7 @@ const VerifyGateToolParams = Type.Object({
59
58
  }),
60
59
  {
61
60
  description:
62
- "Explicit checks. Defaults to the repo preset: package.json scripts 'typecheck' + 'test:critical' when present. Add grep evidence lines here (e.g. `grep -c \"<marker>\" dist/index.mjs`).",
61
+ 'Explicit checks — REQUIRED for a meaningful verdict (M1-7: the gate never derives commands from a worker-mutable package.json). Without checks the verdict is INCONCLUSIVE (escalate). Add grep evidence lines here (e.g. `grep -c "<marker>" dist/index.mjs`).',
63
62
  },
64
63
  ),
65
64
  ),
@@ -152,7 +151,12 @@ export function registerVerifyGateTool(pi: ExtensionAPI, deps: VerifyGateToolDep
152
151
  }),
153
152
  async execute(_id, params: Static<typeof VerifyGateToolParams>, signal, _onUpdate, ctx) {
154
153
  const cwd = params.cwd && params.cwd.trim().length > 0 ? params.cwd : ctx.cwd;
155
- const checks: VerifyGateCheck[] = params.checks ?? defaultVerifyGateChecks(cwd);
154
+ // M1-7 (R47-1): NO package.json fallback — the task/session cwd is
155
+ // worker-mutable, so deriving checks from it at call time let a worker
156
+ // grade its own work (tampered `scripts.typecheck: "exit 0"` ⇒ PASS).
157
+ // Callers must pass explicit checks; without them the gate is
158
+ // INCONCLUSIVE (escalate — never a PASS-shaped verdict).
159
+ const checks: VerifyGateCheck[] = params.checks ?? [];
156
160
  // Nested-limit guards — validate BEFORE any executeTool call.
157
161
  if (checks.length > VERIFY_GATE_TOOL_MAX_CHECKS) {
158
162
  return {
@@ -27,9 +27,32 @@ function readManifest(filePath: string): TeamRunManifest | undefined {
27
27
  */
28
28
  const TEMP_DIR_ROOTS = [os.tmpdir(), "/var/tmp", "/tmp"];
29
29
 
30
+ /** R7-P2 (wave-2b): canonicalize BOTH sides of the temp-root comparison.
31
+ * macOS returns /var/... from one API and /private/var/... from another
32
+ * (getcwd is physical); the raw startsWith then misses and phantom test runs
33
+ * leak into production dashboards. realpath'd roots + realpath'd compared
34
+ * paths (best-effort — a vanished path simply doesn't match, as before). */
35
+ const TEMP_DIR_ROOTS_REAL = TEMP_DIR_ROOTS.map((root) => {
36
+ try {
37
+ return fs.realpathSync.native(root);
38
+ } catch {
39
+ return root;
40
+ }
41
+ });
42
+
43
+ function realPathOrNull(p: string): string | null {
44
+ try {
45
+ return fs.realpathSync.native(p);
46
+ } catch {
47
+ return null;
48
+ }
49
+ }
50
+
30
51
  function isTempWorkspacePath(p: string | undefined): boolean {
31
52
  if (!p) return false;
32
- return TEMP_DIR_ROOTS.some((t) => p.startsWith(t + path.sep));
53
+ const real = realPathOrNull(p);
54
+ if (!real) return false; // unreadable/vanished — do not guess
55
+ return TEMP_DIR_ROOTS_REAL.some((t) => real === t || real.startsWith(t + path.sep));
33
56
  }
34
57
 
35
58
  function collectRuns(root: string, maxEntries?: number, signal?: AbortSignal): TeamRunManifest[] {
@@ -15,6 +15,7 @@ import { agentOutputPath, readCrewAgentEventsCursor, readCrewAgentStatus, readCr
15
15
  import { readForegroundControlStatus, writeForegroundInterruptRequest } from "../../../runtime/foreground-control.ts";
16
16
  import { probeLiveSessionRuntime } from "../../../runtime/live-session/live-session-runtime.ts";
17
17
  import { resolveCrewRuntime } from "../../../runtime/model/runtime-resolver.ts";
18
+ import { fenceWorkerText } from "../../../runtime/sanitize/worker-text.ts";
18
19
  import { readEvents, readEventsCursor } from "../../../state/event-log/event-log.ts";
19
20
  import { globMatch } from "../../../utils/glob-match.ts";
20
21
  import { resolveRealContainedPath } from "../../../utils/safe-paths.ts";
@@ -201,7 +202,11 @@ export const handleGetAgentResult: ApiOperationHandler = (hctx) => {
201
202
  true,
202
203
  );
203
204
  const task = loaded.tasks.find((item) => item.id === agent.taskId);
204
- const text = safeReadContainedFile(loaded.manifest.artifactsRoot, task?.resultArtifact?.path) ?? JSON.stringify(agent, null, 2);
205
+ const artifactText = safeReadContainedFile(loaded.manifest.artifactsRoot, task?.resultArtifact?.path);
206
+ // R38-C1 (EP1 seed): result-artifact bytes are worker-authored — fence them
207
+ // before they enter the privileged session. The no-artifact fallback (agent
208
+ // record JSON) is runtime-owned and stays unfenced.
209
+ const text = artifactText !== undefined ? fenceWorkerText(artifactText, { source: "agent-result" }) : JSON.stringify(agent, null, 2);
205
210
  return result(text, {
206
211
  action: "api",
207
212
  status: "ok",
@@ -294,7 +299,9 @@ export const handleReadAgentTranscript: ApiOperationHandler = (hctx) => {
294
299
  const fallbackText = artifactText ? "" : (safeReadContainedFile(loaded.manifest.stateRoot, fallbackPath) ?? "");
295
300
  const transcriptPath = artifactText ? artifactTranscriptPath : fallbackPath;
296
301
  const text = artifactText || fallbackText;
297
- return result(text || `(no transcript at ${transcriptPath})`, {
302
+ // R38-C1 (EP1 seed): transcript/output.log bytes are worker-authored — fence
303
+ // them; the no-transcript fallback is runtime-owned and stays unfenced.
304
+ return result(text ? fenceWorkerText(text, { source: "agent-transcript" }) : `(no transcript at ${transcriptPath})`, {
298
305
  action: "api",
299
306
  status: "ok",
300
307
  runId: loaded.manifest.runId,
@@ -403,7 +410,9 @@ export const handleDiff: ApiOperationHandler = (hctx) => {
403
410
  const content = safeReadContainedFile(loaded.manifest.artifactsRoot, artifact.path);
404
411
  if (content) {
405
412
  const display = content.length > 4000 ? content.slice(0, 4000) + "\n... (truncated)" : content;
406
- parts.push(`\n--- ${artifact.path} ---\n${display}`);
413
+ // R38-C1 (EP1 seed): diff/patch bytes are worker-authored — fence them
414
+ // per artifact (per-op fencing; headers stay runtime-owned).
415
+ parts.push(`\n--- ${artifact.path} ---\n${fenceWorkerText(display, { source: "diff" })}`);
407
416
  }
408
417
  }
409
418
  return result(parts.join("\n"), {
@@ -1,4 +1,5 @@
1
1
  import { appendHookEvent, executeHook } from "../../hooks/registry.ts";
2
+ import { abortRunDelegates } from "../../runtime/broker/delegate/delegate-abort.ts";
2
3
  import { killProcessPid } from "../../runtime/child-pi/child-pi.ts";
3
4
  import { recordFromTask, saveCrewAgents } from "../../runtime/crew-agent-records.ts";
4
5
  import { writeForegroundInterruptRequest } from "../../runtime/foreground-control.ts";
@@ -340,6 +341,16 @@ export async function handleCancel(params: TeamToolParamsValue, ctx: TeamContext
340
341
  // still signals synchronously here — while we hold the lock — so the async
341
342
  // background runner cannot overwrite the cancelled state we write below.
342
343
  if (fresh.manifest.status === "running") {
344
+ // M4-R1(c) (fix-wave 6 residual, M3-A / R43-N3): the run is going
345
+ // terminal — abort its in-flight DELEGATE GRANDCHILDREN too. They live
346
+ // in the broker's abort registry (a separate, process-local lifetime),
347
+ // so without this they outlive the cancel by up to their own 24h
348
+ // deadline while still spending tokens and holding nested slots. Exact
349
+ // wiring budget-enforcement.ts terminaliseRunWithDrain already uses;
350
+ // abortRunDelegates is SYNC + never throws (0 when no broker aborter is
351
+ // installed — headless/worker-side cancels keep the pre-existing
352
+ // degrade, never a new failure path).
353
+ abortRunDelegates(fresh.manifest.runId, "run-cancelled");
343
354
  void terminateLiveAgentsForRun(fresh.manifest.runId, "cancelled", appendEvent, fresh.manifest.eventsPath).catch((error) =>
344
355
  logInternalError("team-tool.handleCancel.terminateLiveAgents", error, `runId=${fresh.manifest.runId}`),
345
356
  );
@@ -1,17 +1,17 @@
1
1
  import { effectiveAutonomousConfig, type PiTeamsAutonomousConfig, type PiTeamsConfig, parseConfig } from "../../config/config.ts";
2
+ import { stripDangerousKeysDeep } from "../../config/config-validation.ts";
3
+ import { collectSensitiveConfigPaths } from "../../schema/sensitive-config-paths.ts";
2
4
 
3
- const DANGEROUS_KEYS = new Set(["__proto__", "constructor", "prototype"]);
4
-
5
- /** Recursively strip dangerous prototype-pollution keys from all levels of an object. */
5
+ /**
6
+ * Recursively strip dangerous prototype-pollution keys from all levels of an
7
+ * object. M1-1: delegates to the shared proto-safe helpers (config-validation.ts,
8
+ * case-insensitive
9
+ * DANGEROUS_OBJECT_KEYS superset) so every config-patch parse boundary applies
10
+ * ONE canonical drop rule — config patch, autonomy patch, and
11
+ * effectiveRunConfig all flow through here or through parseConfig's asRecord.
12
+ */
6
13
  export function sanitizeObject<T>(obj: T): T {
7
- if (obj === null || obj === undefined || typeof obj !== "object") return obj;
8
- if (Array.isArray(obj)) return obj.map((item) => sanitizeObject(item)) as T;
9
- const safe: Record<string, unknown> = {};
10
- for (const key of Object.keys(obj as Record<string, unknown>)) {
11
- if (DANGEROUS_KEYS.has(key)) continue;
12
- safe[key] = sanitizeObject((obj as Record<string, unknown>)[key]);
13
- }
14
- return safe as T;
14
+ return stripDangerousKeysDeep(obj);
15
15
  }
16
16
 
17
17
  export function autonomousPatchFromConfig(config: unknown): PiTeamsAutonomousConfig {
@@ -24,8 +24,53 @@ export function configPatchFromConfig(config: unknown): PiTeamsConfig {
24
24
  return parseConfig(config);
25
25
  }
26
26
 
27
+ /**
28
+ * M1-8 (R45-S2): drop the schema's sensitive-marked config paths from the
29
+ * RUN-PARAM tier. `params.config` is worker/LLM-influenced tool input — the
30
+ * same fields a project file may not set (user-only kill-switches and
31
+ * execution-shaping knobs: executeWorkers, runtime.mode/isolationPolicy,
32
+ * worktree.setupHook/seedPaths, agents.overrides, otlp.*, policy.*,
33
+ * autonomous.*, goalWrap, …) must not be settable through run params either.
34
+ *
35
+ * Drops mutate the FRESH parseConfig output only — never the caller's input
36
+ * and never `base` (the loaded user/project config, already project-tier
37
+ * sanitized at load). Sensitive keys are dropped SILENTLY: the drop is the
38
+ * contract, and the persisted `manifest.runConfig` records the effective
39
+ * (post-drop) config for audit. Precedence note (P4 pin): with
40
+ * `executeWorkers` dropped from the patch, a user's `executeWorkers:false`
41
+ * survives untouched — the kill-switch cannot be flipped back on via run
42
+ * params (runtime-resolver reads the merged effective config).
43
+ *
44
+ * Conditional carve-out (mirrors the project tier's CONDITIONAL_PROJECT_DROPS
45
+ * doctrine: loosening values dropped, tightening values survive): a run param
46
+ * may still TIGHTEN `runtime.mode` to "scaffold" — the documented params
47
+ * dry-run channel ("Set runtime.mode=scaffold … only for dry runs", run tool
48
+ * banner) spawns no workers and is the SAFE direction. Loosening modes
49
+ * (auto/child-process/live-session — the R45-S2 escalation threat) are
50
+ * dropped like every other sensitive value.
51
+ */
52
+ const RUNPARAM_CONDITIONAL_KEEP: Record<string, (value: unknown) => boolean> = {
53
+ "runtime.mode": (value) => value === "scaffold",
54
+ };
55
+
56
+ function dropSensitivePatchPaths(patch: Record<string, unknown>): void {
57
+ for (const dotted of collectSensitiveConfigPaths()) {
58
+ const parts = dotted.split(".");
59
+ let node: Record<string, unknown> | undefined = patch;
60
+ for (let i = 0; node !== undefined && i < parts.length - 1; i++) {
61
+ const next: unknown = node[parts[i]!];
62
+ node = next !== null && typeof next === "object" && !Array.isArray(next) ? (next as Record<string, unknown>) : undefined;
63
+ }
64
+ if (node === undefined) continue;
65
+ const leaf = parts[parts.length - 1]!;
66
+ if (RUNPARAM_CONDITIONAL_KEEP[dotted]?.(node[leaf])) continue;
67
+ delete node[leaf];
68
+ }
69
+ }
70
+
27
71
  export function effectiveRunConfig(base: PiTeamsConfig, rawOverride: unknown): PiTeamsConfig {
28
72
  const patch = sanitizeObject(parseConfig(rawOverride));
73
+ dropSensitivePatchPaths(patch as Record<string, unknown>);
29
74
  return {
30
75
  ...base,
31
76
  ...patch,