pi-crew 0.11.22 → 0.11.24

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 (85) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/agents/explorer.md +3 -3
  3. package/agents/librarian.md +3 -3
  4. package/dist/index.mjs +523 -520
  5. package/docs/dynamic-workflows.md +14 -2
  6. package/package.json +4 -4
  7. package/src/agents/discover-agents.ts +8 -2
  8. package/src/config/config-validation.ts +3 -1
  9. package/src/config/env-allowlist-registry.ts +5 -0
  10. package/src/config/env-vars.ts +9 -6
  11. package/src/config/role-tools.ts +7 -6
  12. package/src/extension/management.ts +7 -0
  13. package/src/extension/register.ts +15 -0
  14. package/src/extension/run-index.ts +24 -1
  15. package/src/extension/team-tool/destructive-gate.ts +208 -0
  16. package/src/extension/team-tool/goal-wrap.ts +11 -1
  17. package/src/extension/team-tool/goal.ts +13 -1
  18. package/src/extension/team-tool/respond.ts +8 -6
  19. package/src/extension/team-tool/run.ts +4 -4
  20. package/src/extension/team-tool.ts +10 -0
  21. package/src/observability/exporters/otlp-exporter.ts +11 -1
  22. package/src/prompt/prompt-runtime.ts +102 -18
  23. package/src/prompt/surface-worker.ts +13 -1
  24. package/src/runtime/broker/crew-broker-client.ts +7 -0
  25. package/src/runtime/broker/crew-broker.ts +113 -152
  26. package/src/runtime/broker/protocol/connection-state.ts +159 -1
  27. package/src/runtime/broker/protocol/events-replay.ts +50 -10
  28. package/src/runtime/broker/protocol/request-parsers.ts +68 -0
  29. package/src/runtime/child-pi/child-pi-constants.ts +9 -0
  30. package/src/runtime/child-pi/child-pi-rpc.ts +72 -7
  31. package/src/runtime/child-pi/child-pi-streams.ts +19 -3
  32. package/src/runtime/child-pi/postmortem-registry.ts +8 -2
  33. package/src/runtime/classifier/ambient-noise-gate.ts +5 -1
  34. package/src/runtime/compaction/tool-output-pruner.ts +20 -2
  35. package/src/runtime/crew-agent-records.ts +92 -17
  36. package/src/runtime/custom-tools/submit-result-tool.ts +17 -0
  37. package/src/runtime/deadletter.ts +5 -1
  38. package/src/runtime/dispatch-batch.ts +60 -16
  39. package/src/runtime/effectiveness.ts +24 -1
  40. package/src/runtime/finalize-run.ts +12 -1
  41. package/src/runtime/goal-workflow/dynamic-workflow-context.ts +12 -3
  42. package/src/runtime/goal-workflow/goal-evaluator.ts +30 -5
  43. package/src/runtime/goal-workflow/goal-loop-runner.ts +60 -3
  44. package/src/runtime/live-session/live-agent-control.ts +34 -14
  45. package/src/runtime/live-session/live-agent-manager.ts +20 -5
  46. package/src/runtime/live-session/live-session-runtime.ts +66 -81
  47. package/src/runtime/live-session/usage-bridge.ts +66 -2
  48. package/src/runtime/mcp-proxy.ts +10 -0
  49. package/src/runtime/model/model-fallback.ts +68 -0
  50. package/src/runtime/model/runtime-policy.ts +20 -6
  51. package/src/runtime/model/runtime-resolver.ts +5 -3
  52. package/src/runtime/orphan-worker-registry.ts +64 -24
  53. package/src/runtime/pi-spawn.ts +4 -1
  54. package/src/runtime/process/zombie-scanner.ts +6 -2
  55. package/src/runtime/recovery/crash-recovery.ts +35 -2
  56. package/src/runtime/scheduling/batch-barrier.ts +42 -5
  57. package/src/runtime/scheduling/run-coalesced-task-group.ts +9 -3
  58. package/src/runtime/scheduling/scheduler.ts +74 -29
  59. package/src/runtime/stale-reconciler.ts +45 -6
  60. package/src/runtime/surface/herdr-provider.ts +54 -1
  61. package/src/runtime/surface/launch-script.ts +7 -1
  62. package/src/runtime/surface/resolve-surface.ts +4 -1
  63. package/src/runtime/surface/tmux-provider.ts +32 -2
  64. package/src/runtime/task-runner/child-executor.ts +3 -2
  65. package/src/runtime/workspace-lock.ts +52 -4
  66. package/src/state/contracts.ts +2 -0
  67. package/src/state/coordination/process-lock.ts +50 -0
  68. package/src/state/event-log/cursor.ts +24 -3
  69. package/src/state/event-log/event-log-rotation.ts +26 -2
  70. package/src/state/event-log/worker-atomic-writer.ts +5 -0
  71. package/src/state/stores/active-run-registry.ts +56 -5
  72. package/src/state/stores/ownership-map.ts +26 -7
  73. package/src/state/stores/plan-store.ts +30 -1
  74. package/src/state/stores/sqlite-run-state.ts +91 -1
  75. package/src/state/types.ts +5 -0
  76. package/src/ui/dashboard-panes/agents-pane.ts +4 -3
  77. package/src/ui/dashboard-panes/plan-pane.ts +5 -3
  78. package/src/ui/dashboard-panes/transcript-pane.ts +2 -1
  79. package/src/ui/overlays/mailbox-detail-overlay.ts +5 -3
  80. package/src/ui/run-dashboard.ts +6 -4
  81. package/src/ui/run-snapshot-cache.ts +3 -2
  82. package/src/ui/settings-overlay.ts +68 -10
  83. package/src/ui/theme-discovery.ts +26 -7
  84. package/src/ui/transcript-viewer.ts +5 -3
  85. package/src/utils/visual.ts +84 -0
@@ -292,6 +292,43 @@ function setAsyncAgentReaderCache(
292
292
  }
293
293
  }
294
294
 
295
+ /** FS2 (2026-10-17): sentinel returned by `readCrewAgents` when agents.json
296
+ * EXISTS but could not be read or parsed. Empty array by identity — every
297
+ * read-only caller keeps its "no agents" degrade behavior — but persisting
298
+ * callers compare `=== CREW_AGENTS_READ_FAILED` and SKIP the save so an
299
+ * unreadable agents.json is never overwritten with []. */
300
+ export const CREW_AGENTS_READ_FAILED: CrewAgentRecord[] = [];
301
+
302
+ /** FS2: strict disk read for the sync agents path. ENOENT is a legitimate
303
+ * first-write [] — every other read/parse failure returns the sentinel after
304
+ * logging (atomic-write.ts readJsonFile conflates both into undefined, so the
305
+ * errno/JSON.parse classification happens here). */
306
+ function readAgentsRecordsStrict(agentsFilePath: string): CrewAgentRecord[] {
307
+ let raw: string;
308
+ try {
309
+ raw = fs.readFileSync(agentsFilePath, "utf-8");
310
+ } catch (error) {
311
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
312
+ logInternalError(
313
+ "crew-agents.read-failed",
314
+ error instanceof Error ? error : new Error(String(error)),
315
+ `agentsPath=${agentsFilePath}`,
316
+ );
317
+ return CREW_AGENTS_READ_FAILED;
318
+ }
319
+ try {
320
+ const parsed = JSON.parse(raw) as unknown;
321
+ return Array.isArray(parsed) ? (parsed as CrewAgentRecord[]) : [];
322
+ } catch (error) {
323
+ logInternalError(
324
+ "crew-agents.parse-failed",
325
+ error instanceof Error ? error : new Error(String(error)),
326
+ `agentsPath=${agentsFilePath}`,
327
+ );
328
+ return CREW_AGENTS_READ_FAILED;
329
+ }
330
+ }
331
+
295
332
  export function readCrewAgents(manifest: TeamRunManifest): CrewAgentRecord[] {
296
333
  // F06 / RR-017: read-after-write WITHOUT forcing the pending coalesced write
297
334
  // to disk. `upsertCrewAgent` always reads first, so the previous scoped
@@ -303,12 +340,20 @@ export function readCrewAgents(manifest: TeamRunManifest): CrewAgentRecord[] {
303
340
  // written, so overlaying it preserves the "intra-process readers see the
304
341
  // latest data" contract (and cross-process readers still see the file within
305
342
  // one coalesce window) with zero I/O.
343
+ //
344
+ // FS2 (2026-10-17): a MISSING file (ENOENT) is a legitimate [] — first
345
+ // write. A file that EXISTS but cannot be read (EACCES/EIO) or parsed
346
+ // (corrupt JSON) must NOT degrade to [] (atomic-write.ts readJsonFile
347
+ // returns undefined for BOTH cases, so we classify ourselves): the next
348
+ // upsert would persist that [] and WIPE agents.json. Such reads return the
349
+ // CREW_AGENTS_READ_FAILED sentinel (identity ===, iterable-empty) and
350
+ // persisting callers (upsertCrewAgent / removeCrewAgent) skip the save.
306
351
  const agentsFilePath = agentsPath(manifest);
307
352
  const pending = peekPendingCoalescedWrite<CrewAgentRecord[]>(agentsFilePath);
308
353
  try {
309
354
  const records =
310
- pending ??
311
- readJsonFileCoalesced(agentsFilePath, AGENT_READER_TTL_MS, () => readJsonFile<CrewAgentRecord[]>(agentsFilePath) ?? []);
355
+ pending ?? readJsonFileCoalesced(agentsFilePath, AGENT_READER_TTL_MS, () => readAgentsRecordsStrict(agentsFilePath));
356
+ if (records === CREW_AGENTS_READ_FAILED) return CREW_AGENTS_READ_FAILED;
312
357
  // Validate schema and deduplicate by id to handle concurrent write conflicts
313
358
  const seen = new Set<string>();
314
359
  const deduped = records.filter((r) => {
@@ -581,8 +626,20 @@ export function upsertCrewAgent(manifest: TeamRunManifest, record: CrewAgentReco
581
626
  asyncAgentReaderCache.delete(agentsPath(manifest));
582
627
  return;
583
628
  }
584
- // Read current state
629
+ // Read current state. FS2 (2026-10-17): when agents.json exists but is
630
+ // unreadable/unparseable, readCrewAgents returns the CREW_AGENTS_READ_FAILED
631
+ // sentinel — persisting the merged (empty) array would WIPE the authoritative
632
+ // index over state we could not read. Skip the save entirely (log already
633
+ // emitted by the reader) and let the next healthy read repair it.
585
634
  const existing = readCrewAgents(manifest);
635
+ if (existing === CREW_AGENTS_READ_FAILED) {
636
+ logInternalError(
637
+ "crew-agents.upsert-skipped-unreadable",
638
+ new Error("skipping agents.json persist — current records unreadable (read/parse failure)"),
639
+ `agentsPath=${agentsPath(manifest)} taskId=${record.taskId}`,
640
+ );
641
+ return;
642
+ }
586
643
  // Deduplicate by id: keep newer record when same id appears
587
644
  const idIndex = new Map(existing.map((item, i) => [item.id, i]));
588
645
  const merged: CrewAgentRecord[] = existing.map((item) => (item.id === record.id ? record : item));
@@ -698,6 +755,21 @@ function writeSeqToSidecar(filePath: string, seq: number): void {
698
755
  }
699
756
  }
700
757
 
758
+ function scanMaxAgentEventSeq(filePath: string): number {
759
+ let max = 0;
760
+ for (const line of fs.readFileSync(filePath, "utf-8").split(/\r?\n/)) {
761
+ if (!line.trim()) continue;
762
+ try {
763
+ const parsed = JSON.parse(line) as { seq?: unknown };
764
+ if (typeof parsed.seq === "number" && Number.isFinite(parsed.seq)) max = Math.max(max, parsed.seq);
765
+ else max += 1;
766
+ } catch {
767
+ max += 1;
768
+ }
769
+ }
770
+ return max;
771
+ }
772
+
701
773
  function nextAgentEventSeq(filePath: string): number {
702
774
  if (!fs.existsSync(filePath)) {
703
775
  // Clean up stale sidecar when main file is gone.
@@ -714,24 +786,21 @@ function nextAgentEventSeq(filePath: string): number {
714
786
  // FIX: Try sidecar file for O(1) lookup before falling back to O(n) scan.
715
787
  const sidecarSeq = readSeqFromSidecar(filePath);
716
788
  if (sidecarSeq !== undefined) {
789
+ // FS1 (fix, EL-1 port from event-log sequence-cache.ts): a REGRESSED
790
+ // sidecar (crash between append and sidecar advance, or lost best-effort
791
+ // durability) must never re-mint used seqs — trust it only as
792
+ // max(sidecar, tail-scan), else cursor consumers (seq > sinceSeq)
793
+ // permanently drop the duplicate-seq events.
794
+ const safeSeq = Math.max(sidecarSeq, scanMaxAgentEventSeq(filePath));
717
795
  setAgentEventSeqCache(filePath, {
718
796
  size: stat.size,
719
797
  mtimeMs: stat.mtimeMs,
720
- seq: sidecarSeq,
798
+ seq: safeSeq,
721
799
  });
722
- return sidecarSeq + 1;
723
- }
724
- let max = 0;
725
- for (const line of fs.readFileSync(filePath, "utf-8").split(/\r?\n/)) {
726
- if (!line.trim()) continue;
727
- try {
728
- const parsed = JSON.parse(line) as { seq?: unknown };
729
- if (typeof parsed.seq === "number" && Number.isFinite(parsed.seq)) max = Math.max(max, parsed.seq);
730
- else max += 1;
731
- } catch {
732
- max += 1;
733
- }
800
+ if (safeSeq !== sidecarSeq) writeSeqToSidecar(filePath, safeSeq);
801
+ return safeSeq + 1;
734
802
  }
803
+ const max = scanMaxAgentEventSeq(filePath);
735
804
  setAgentEventSeqCache(filePath, {
736
805
  size: stat.size,
737
806
  mtimeMs: stat.mtimeMs,
@@ -988,7 +1057,13 @@ export function readCrewAgentEventsCursor(
988
1057
  const limit = typeof options.limit === "number" && Number.isInteger(options.limit) && options.limit >= 0 ? options.limit : undefined;
989
1058
 
990
1059
  const filtered = readAgentEventMatches(filePath, sinceSeq);
991
- const events = limit !== undefined ? filtered.slice(0, limit) : filtered;
1060
+ // R8-L2 (wave-2b, FS3 twin): seq-sort BEFORE the limit slice. File-order
1061
+ // slicing on a non-monotonic file can deliver an early high seq inside the
1062
+ // first `limit` events; nextSeq=max-delivered then permanently skips every
1063
+ // undelivered lower-seq event for paging consumers. Sort ONLY under a
1064
+ // limit — the no-limit path delivers everything (nextSeq = true max) and
1065
+ // keeps its historical FILE-order contract (mirrors cursor.ts FS3).
1066
+ const events = limit !== undefined ? [...filtered].sort((a, b) => (a.seq as number) - (b.seq as number)).slice(0, limit) : filtered;
992
1067
  const returnedMaxSeq = events.reduce((max, event) => (typeof event.seq === "number" ? Math.max(max, event.seq) : max), sinceSeq);
993
1068
  return {
994
1069
  path: filePath,
@@ -91,10 +91,27 @@ export function createSubmitResultTool(
91
91
  structuredData: params.structuredData,
92
92
  },
93
93
  };
94
+ // R2-L-B1 (wave-2b): a THROWING onYield previously still returned the
95
+ // success text — the model believed its result was recorded and ended
96
+ // the turn, silently dropping it. The error tool-result makes the
97
+ // failure visible so the worker retries or falls back to plain text.
94
98
  try {
95
99
  onYield(result);
96
100
  } catch (error) {
97
101
  logInternalError("submit-result-tool.yield", error, toolCallId);
102
+ return {
103
+ content: [
104
+ {
105
+ type: "text",
106
+ text: `Result submission FAILED: ${error instanceof Error ? error.message : String(error)} — the result was NOT recorded. Try again or state the result in your final message.`,
107
+ },
108
+ ],
109
+ details: {
110
+ summary: params.summary,
111
+ ...(params.artifacts ? { artifacts: params.artifacts } : {}),
112
+ ...(params.structuredData ? { structuredData: params.structuredData } : {}),
113
+ },
114
+ };
98
115
  }
99
116
  return response;
100
117
  },
@@ -3,8 +3,9 @@ import * as path from "node:path";
3
3
  import type { TeamRunManifest } from "../state/types.ts";
4
4
  import { logInternalError } from "../utils/internal-error.ts";
5
5
  import { projectCrewRoot } from "../utils/paths.ts";
6
+ import { assertSafePathId } from "../utils/safe-paths.ts";
6
7
 
7
- export type DeadletterReason = "max-retries" | "heartbeat-dead" | "manual";
8
+ export type DeadletterReason = "max-retries" | "heartbeat-dead" | "manual" | "dispatch-fallback"; // FS7: catch-fallback failure (dispatch-batch)
8
9
 
9
10
  export interface DeadletterEntry {
10
11
  taskId: string;
@@ -35,6 +36,9 @@ export function deadletterPath(manifest: TeamRunManifest): string {
35
36
  * this is the durable out-of-band record for post-mortem analysis.
36
37
  */
37
38
  export function deadletterIndexPath(manifest: TeamRunManifest): string {
39
+ // R2-L-A8 (wave-2b): runId is interpolated into a project-level path —
40
+ // a manifest with ".."-bearing runId would escape the deadletter dir.
41
+ assertSafePathId("runId", manifest.runId);
38
42
  return path.join(projectCrewRoot(manifest.cwd), "state", "deadletter", `${manifest.runId}.jsonl`);
39
43
  }
40
44
 
@@ -147,13 +147,20 @@ export async function sweepExpiredWaitingTasks(
147
147
  if (task.status !== "waiting" || task.waiting === undefined) continue;
148
148
  if (task.waiting.deadline > now) continue;
149
149
  const questionId = task.waiting.questionId;
150
- if (emittedAskTimedoutQuestionIds.has(questionId)) continue; // exactly-once per process
150
+ // FS6 (2026-10-17): liveness BEFORE the emittedAskTimedout exactly-once
151
+ // guard. The guard used to mark the questionId on the FIRST sweep
152
+ // regardless of liveness — a worker ALIVE at sweep 1 (event emitted, task
153
+ // left waiting) that DIED before sweep 2 was skipped forever by the guard
154
+ // and never requeued. Now only the alive-worker "surfaced in-tool"
155
+ // event is exactly-once; a dead worker always proceeds to the requeue
156
+ // (the status transition makes the next sweep a natural no-op).
157
+ const workerAlive = isWaitingWorkerAlive(task, now);
158
+ if (workerAlive && emittedAskTimedoutQuestionIds.has(questionId)) continue; // exactly-once per process (alive-worker events only)
151
159
  if (emittedAskTimedoutQuestionIds.size >= MAX_EMITTED_ASK_TIMEDOUT) {
152
160
  const oldest = emittedAskTimedoutQuestionIds.keys().next().value;
153
161
  if (oldest !== undefined) emittedAskTimedoutQuestionIds.delete(oldest);
154
162
  }
155
163
  emittedAskTimedoutQuestionIds.add(questionId);
156
- const workerAlive = isWaitingWorkerAlive(task, now);
157
164
  timedOutQuestionIds.push(questionId);
158
165
  pendingEvents.push({ taskId: task.id, questionId, workerAlive });
159
166
  if (!workerAlive) {
@@ -202,12 +209,15 @@ export async function sweepExpiredWaitingTasks(
202
209
  }
203
210
  try {
204
211
  const existingRuntimes = new Map(readCrewAgents(fresh.manifest).map((a) => [a.taskId, a.runtime]));
205
- saveCrewAgents(
206
- fresh.manifest,
207
- tasks
208
- .filter((t) => requeuedTaskIds.includes(t.id))
209
- .map((t) => recordFromTask(fresh.manifest, t, existingRuntimes.get(t.id) ?? "child-process")),
210
- );
212
+ // R4-A1 (fix): agents.json is a full-index overwrite — merge the requeued
213
+ // records into the existing set (team-runner.ts:525 pattern) instead of
214
+ // persisting the subset alone (siblings incl. RUNNING agents were wiped).
215
+ const mergedById = new Map(readCrewAgents(fresh.manifest).map((record) => [record.id, record] as const));
216
+ for (const task of tasks.filter((t) => requeuedTaskIds.includes(t.id))) {
217
+ const record = recordFromTask(fresh.manifest, task, existingRuntimes.get(task.id) ?? "child-process");
218
+ mergedById.set(record.id, record);
219
+ }
220
+ saveCrewAgents(fresh.manifest, [...mergedById.values()]);
211
221
  } catch (error) {
212
222
  logInternalError("dispatch-batch.ask-timeout.crewAgents", error, `runId=${runId}`);
213
223
  }
@@ -1015,14 +1025,48 @@ export async function dispatchBatch(ctx: SchedulerContext, decision: DispatchBat
1015
1025
  const freshTasks = fresh?.tasks ?? ctx.tasks;
1016
1026
  const freshTask = freshTasks.find((item) => item.id === task.id) ?? task;
1017
1027
  if (freshTask.status !== "queued" && freshTask.status !== "running") return { manifest: freshManifest, tasks: freshTasks };
1018
- return withCorrelation(childCorrelation(freshManifest.runId, task.id), () =>
1019
- runTeamTask({
1020
- ...baseInput,
1021
- manifest: freshManifest,
1022
- tasks: freshTasks,
1023
- task: freshTask,
1024
- }),
1025
- );
1028
+ // FS7 (2026-10-17): the final fallback attempt previously ran OUTSIDE the
1029
+ // accounting/deadletter bookkeeping the retry-loop attempts get
1030
+ // (onRetryGivenUp above) — a throw here escaped with NO deadletter, no
1031
+ // metric, no event. Wrap it so a fallback failure lands in the SAME
1032
+ // bookkeeping as the loop attempts.
1033
+ try {
1034
+ return await withCorrelation(childCorrelation(freshManifest.runId, task.id), () =>
1035
+ runTeamTask({
1036
+ ...baseInput,
1037
+ manifest: freshManifest,
1038
+ tasks: freshTasks,
1039
+ task: freshTask,
1040
+ }),
1041
+ );
1042
+ } catch (fallbackError) {
1043
+ const fallbackMessage = fallbackError instanceof Error ? fallbackError.message : String(fallbackError);
1044
+ appendDeadletter(freshManifest, {
1045
+ runId: freshManifest.runId,
1046
+ taskId: task.id,
1047
+ reason: "dispatch-fallback",
1048
+ attempts: 1,
1049
+ lastError: fallbackMessage,
1050
+ timestamp: new Date().toISOString(),
1051
+ agent: freshTask.agent,
1052
+ role: freshTask.role,
1053
+ modelAttempts: freshTask.modelAttempts?.length,
1054
+ runStatus: freshManifest.status,
1055
+ });
1056
+ input.metricRegistry
1057
+ ?.counter("crew.task.deadletter_total", "Deadletter triggers by reason")
1058
+ .inc({ reason: "dispatch-fallback" });
1059
+ const nowIso = new Date().toISOString();
1060
+ const failedTasks = freshTasks.map((item) =>
1061
+ item.id === task.id && (item.status === "queued" || item.status === "running")
1062
+ ? { ...item, status: "failed" as const, finishedAt: nowIso, error: fallbackMessage }
1063
+ : item,
1064
+ );
1065
+ return {
1066
+ manifest: updateRunStatus(freshManifest, "failed", `dispatch fallback failed: ${fallbackMessage}`),
1067
+ tasks: failedTasks,
1068
+ };
1069
+ }
1026
1070
  }
1027
1071
  };
1028
1072
  // ── OPT-01 streaming dispatch: dispatch units into ctx.pendingUnits ──
@@ -16,6 +16,24 @@ export interface RunEffectivenessSummary {
16
16
  severity: RunEffectivenessSeverity;
17
17
  }
18
18
 
19
+ /** R9-F3 (2026-10-17): task ids with a `delegate.completed ok:true` event
20
+ * (data.subId). A completed grandchild delegate task carries no
21
+ * usage/transcript of its own — its real output flowed through the broker
22
+ * mailbox to the parent — so the guard previously misflagged it as
23
+ * "no observable worker activity" and escalated effective runs to blocked
24
+ * (R8-OPS M3 contributor). Callers pass this set in when events are
25
+ * available (finalize-run reads the log anyway). */
26
+ export function delegateCompletedTaskIdsFromEvents(events: ReadonlyArray<{ type?: string; data?: unknown }>): Set<string> {
27
+ const ids = new Set<string>();
28
+ for (const event of events) {
29
+ if (event.type !== "delegate.completed") continue;
30
+ const data = (event.data ?? {}) as { subId?: unknown; ok?: unknown };
31
+ if (data.ok !== true) continue;
32
+ if (typeof data.subId === "string") ids.add(data.subId);
33
+ }
34
+ return ids;
35
+ }
36
+
19
37
  export function taskHasObservableWorkerActivity(task: TeamTaskState): boolean {
20
38
  return Boolean(
21
39
  (task.agentProgress?.toolCount ?? 0) > 0 ||
@@ -68,13 +86,18 @@ export function evaluateRunEffectiveness(input: {
68
86
  tasks: TeamTaskState[];
69
87
  executeWorkers: boolean;
70
88
  runtimeConfig?: CrewRuntimeConfig;
89
+ /** R9-F3: task ids with delegate.completed ok:true events — counted as
90
+ * observable work (see delegateCompletedTaskIdsFromEvents). */
91
+ delegateCompleted?: ReadonlySet<string>;
71
92
  }): RunEffectivenessSummary {
72
93
  const completedTasks = input.tasks.filter((task) => task.status === "completed");
73
94
  // F4: a completed task with an EMPTY result artifact (0 bytes) is
74
95
  // treated exactly like no-observed-work — the worker may have spun up
75
96
  // and streamed transcript events, but it produced no real output
76
97
  // (e.g. a 429 rate-limit absorbed by the fallback chain).
77
- const noObservedWorkTasks = completedTasks.filter((task) => !taskHasObservableWorkerActivity(task) || taskHasEmptyResult(task));
98
+ const noObservedWorkTasks = completedTasks.filter(
99
+ (task) => (!input.delegateCompleted?.has(task.id) && !taskHasObservableWorkerActivity(task)) || taskHasEmptyResult(task),
100
+ );
78
101
  const needsAttentionTasks = input.tasks.filter((task) => task.agentProgress?.activityState === "needs_attention");
79
102
  const workerExecution: WorkerExecutionState = input.executeWorkers ? "enabled" : "disabled/scaffold";
80
103
  const guardMode = resolveEffectivenessGuardMode(input.runtimeConfig, input.manifest);
@@ -27,7 +27,12 @@ import type { ArtifactDescriptor, PolicyDecision, TeamRunManifest, TeamTaskState
27
27
  import { aggregateUsage, formatUsage } from "../state/usage.ts";
28
28
  import { logInternalError } from "../utils/internal-error.ts";
29
29
  import { checkBranchFreshness } from "../worktree/branch-freshness.ts";
30
- import { effectivenessPolicyDecision, evaluateRunEffectiveness, formatRunEffectivenessLines } from "./effectiveness.ts";
30
+ import {
31
+ delegateCompletedTaskIdsFromEvents,
32
+ effectivenessPolicyDecision,
33
+ evaluateRunEffectiveness,
34
+ formatRunEffectivenessLines,
35
+ } from "./effectiveness.ts";
31
36
  import { isRunTerminalPreserved } from "./merge-loop.ts";
32
37
  import { evaluateCrewPolicy, summarizePolicyDecisions } from "./policy-engine.ts";
33
38
  import { buildRecoveryLedger } from "./recovery/recovery-recipes.ts";
@@ -51,6 +56,8 @@ function runEffectivenessLines(
51
56
  tasks,
52
57
  executeWorkers,
53
58
  runtimeConfig,
59
+ // R9-F3: same delegate.completed guard as the policy path (line ~374).
60
+ delegateCompleted: manifest.eventsPath ? delegateCompletedTaskIdsFromEvents(readEvents(manifest.eventsPath)) : undefined,
54
61
  }),
55
62
  );
56
63
  }
@@ -376,6 +383,10 @@ export async function finalizeRun(ctx: SchedulerContext): Promise<{ manifest: Te
376
383
  tasks,
377
384
  executeWorkers: input.executeWorkers,
378
385
  runtimeConfig: input.runtimeConfig,
386
+ // R9-F3: count delegate.completed ok:true grandchild tasks as observable
387
+ // work — the events log is already read once at finalize (scratchpad
388
+ // summary); reuse it instead of a second pass.
389
+ delegateCompleted: manifest.eventsPath ? delegateCompletedTaskIdsFromEvents(readEvents(manifest.eventsPath)) : undefined,
379
390
  });
380
391
  const effectivenessDecision = effectivenessPolicyDecision(effectiveness);
381
392
  if (effectivenessDecision) {
@@ -375,6 +375,14 @@ export function makeWorkflowCtx(manifest: TeamRunManifest, opts: MakeWorkflowCtx
375
375
  // values are the FULL AgentResult (text + usage + structured + artifactPath +
376
376
  // actual durationMs) so replay does not drop structured output / artifact linkage.
377
377
  const completedAgentCalls: Record<string, AgentResult> = opts.resumedState?.completedAgentCalls ?? {};
378
+ // R2-M-B5 + R5-M-A2 (Wave-3): the identical-call cache is RESUME-ONLY.
379
+ // Only calls HYDRATED from resumedState (this Set) are eligible for a
380
+ // cache hit; results added during a LIVE run are still recorded for a
381
+ // future resume's checkpoint, but identical call options within the SAME
382
+ // run RE-SPAWN. The previous within-run hit made the documented
383
+ // `while(true){review; if accept break; retry}` loop replay round-1's
384
+ // verdict forever (stale-verdict hazard, docs/dynamic-workflows.md).
385
+ const hydratedCallIds = new Set<string>(Object.keys(opts.resumedState?.completedAgentCalls ?? {}));
378
386
  // round-14 P1-2: frozen budget surface. The closures read wfState.spent so the
379
387
  // object stays live after Object.freeze(ctx). total is a snapshot primitive.
380
388
  const budget = Object.freeze({
@@ -442,8 +450,9 @@ export function makeWorkflowCtx(manifest: TeamRunManifest, opts: MakeWorkflowCtx
442
450
  }
443
451
  // PERS-1: per-agent-call idempotency. Compute a deterministic call ID
444
452
  // from the call arguments and check if this call was already completed
445
- // (e.g. during a previous run before a crash). If so, return the cached
446
- // result without re-spawning the agent.
453
+ // in a PREVIOUS run (hydrated from resumedState — R2-M-B5: resume-only;
454
+ // identical calls within a LIVE run re-spawn). If hydrated, return the
455
+ // cached result without re-spawning the agent.
447
456
  // R6-3 (DR-03 round-6, HIGH): the key now covers ALL call-shaping fields
448
457
  // (inputs/systemPrompt/disableTools/maxTurns/graceTurns/worktree/skill/…
449
458
  // and the schema BY VALUE) — previously only {role,agent,prompt,model,
@@ -452,7 +461,7 @@ export function makeWorkflowCtx(manifest: TeamRunManifest, opts: MakeWorkflowCtx
452
461
  // returned the first call's result — inputs provably alter the dispatched
453
462
  // task via composeAgentTask). See computeCallId for the normalization.
454
463
  const callId = computeCallId(call);
455
- const cached = completedAgentCalls[callId];
464
+ const cached = hydratedCallIds.has(callId) ? completedAgentCalls[callId] : undefined;
456
465
  if (cached) {
457
466
  // Full-result replay (R6-3): structured + artifactPath survive the
458
467
  // round-trip through the checkpoint. ok is forced true — only successful
@@ -104,8 +104,9 @@ RULES:
104
104
  Respond with ONLY a single JSON object, no prose, no markdown fences:
105
105
  {"achieved": <true|false>, "reason": "<one concise sentence>", "evidenceRefs": ["<artifact path or transcript quote>", ...]}`;
106
106
 
107
- /** Build the judge task prompt: objective + scope + verification + evidence. */
108
- function buildJudgeTask(input: EvaluateGoalInput): string {
107
+ /** Build the judge task prompt: objective + scope + verification + evidence.
108
+ * @internal — exported for the R3-M-B2 fence-spoof pinning test. */
109
+ export function buildJudgeTask(input: EvaluateGoalInput): string {
109
110
  const lines: string[] = ["# Goal to evaluate", input.objective];
110
111
  if (input.scope) lines.push("", "# Scope (allowed changes)", input.scope);
111
112
  if (input.verification?.commands?.length) {
@@ -144,16 +145,40 @@ function buildJudgeTask(input: EvaluateGoalInput): string {
144
145
  lines.push(...summary);
145
146
  }
146
147
  lines.push("", "## Worker transcript tail (bounded ~8 KiB)");
147
- lines.push("```");
148
+ // R3-M-B2 (Wave-3): the fence must be LONGER than any backtick run inside
149
+ // the (redacted) transcript tail — a ``` run in worker-authored output
150
+ // previously closed the fence early, letting worker-forged text after the
151
+ // run read as loop-authored (false `achieved:true`). Compute the longest
152
+ // run and emit max(3, longestRun+1) backticks.
153
+ const redactedSlice = redactSecretString(input.evidence.transcriptSlice || "(no transcript available)");
154
+ const fence = "`".repeat(Math.max(3, longestBacktickRun(redactedSlice) + 1));
155
+ lines.push(fence);
148
156
  // P1f (RFC v0.5 §P1f): redact the 8 KiB transcript slice — this path is NOT truncated,
149
157
  // so it catches long secrets (JWT, GH PAT, AWS, etc.) fully. This is an existing leak path
150
158
  // Phase 1 leaves unchanged today; v0.5 closes it.
151
- lines.push(redactSecretString(input.evidence.transcriptSlice || "(no transcript available)"));
152
- lines.push("```");
159
+ lines.push(redactedSlice);
160
+ lines.push(fence);
153
161
  lines.push("", "Now respond with the JSON verdict per the system prompt.");
154
162
  return lines.join("\n");
155
163
  }
156
164
 
165
+ /** R3-M-B2 (Wave-3): length of the longest consecutive backtick run — the
166
+ * transcript fence must exceed it so worker-authored backtick runs can never
167
+ * close the fence early. Exported for the fence-spoof pinning test. */
168
+ export function longestBacktickRun(text: string): number {
169
+ let longest = 0;
170
+ let current = 0;
171
+ for (const ch of text) {
172
+ if (ch === "`") {
173
+ current += 1;
174
+ if (current > longest) longest = current;
175
+ } else {
176
+ current = 0;
177
+ }
178
+ }
179
+ return longest;
180
+ }
181
+
157
182
  function truncate(s: string, n: number): string {
158
183
  return s.length > n ? `${s.slice(0, n)}…` : s;
159
184
  }
@@ -525,6 +525,24 @@ export function detectOscillation(verdicts: Array<{ reason: string }>, opts?: {
525
525
  return true;
526
526
  }
527
527
 
528
+ /** R3-M-B4 (period-2 oscillation): A,B,A,B alternation never trips the
529
+ * pairwise window-3 detector — consecutive verdicts differ, yet the loop
530
+ * goes in circles with period 2. Detect lag-2 similarity: across a tail of
531
+ * `window + 1` verdicts, EVERY verdict is near-identical to the one two
532
+ * steps back. Exported for unit testing. */
533
+ export function detectPeriod2Oscillation(verdicts: Array<{ reason: string }>, opts?: { window?: number; threshold?: number }): boolean {
534
+ const window = Math.max(2, opts?.window ?? 3);
535
+ const threshold = opts?.threshold ?? 0.8;
536
+ if (verdicts.length < window + 1) return false;
537
+ const tail = verdicts.slice(-(window + 1)).map((v) => normalizeForSimilarity(v.reason));
538
+ // Every verdict in the tail must match the one `lag` steps back, for BOTH
539
+ // parities of a period-2 cycle (even↔even, odd↔odd).
540
+ for (let i = 2; i < tail.length; i++) {
541
+ if (jaccardSimilarity(tail[i], tail[i - 2]) < threshold) return false;
542
+ }
543
+ return true;
544
+ }
545
+
528
546
  function normalizeForSimilarity(s: string): Set<string> {
529
547
  // Lowercase, split into word 3-shingles (trigrams of words). Skip non-word tokens.
530
548
  const words = s
@@ -586,6 +604,10 @@ export interface StartWorkspaceLockHeartbeatRefreshOptions {
586
604
  refreshMs?: number;
587
605
  /** Test injection: override the refresh implementation (default: refreshWorkspaceLockHeartbeat). */
588
606
  refresh?: (lockPath: string, goalId: string) => boolean;
607
+ /** R3-M-B1 (Wave-3): fires ONCE when a refresh returns false — the lock
608
+ * was lost (stolen/reclaimed by another goal or unwritable). The heartbeat
609
+ * loop STOPS itself before invoking this; wire it to block the goal loop. */
610
+ onOwnershipLost?: () => void;
589
611
  }
590
612
 
591
613
  /**
@@ -609,7 +631,17 @@ export function startWorkspaceLockHeartbeatRefresh(
609
631
  const refresh = opts.refresh ?? refreshWorkspaceLockHeartbeat;
610
632
  const timer = setInterval(() => {
611
633
  try {
612
- refresh(handle.lockPath, handle.goalId);
634
+ // R3-M-B1 (Wave-3): a false refresh means OWNERSHIP LOST (lock stolen,
635
+ // reclaimed, or unwritable — workspace-lock.ts contract :191-201).
636
+ // Previously the return value was discarded and the loop kept
637
+ // heartbeating a lock another goal now owned → both owners mutated the
638
+ // same cwd (lost writes, corrupted verification snapshots). Stop the
639
+ // loop and hand the loss to the caller (goal-loop blocks + exits).
640
+ if (refresh(handle.lockPath, handle.goalId) === false) {
641
+ clearInterval(timer);
642
+ opts.onOwnershipLost?.();
643
+ return;
644
+ }
613
645
  } catch (error) {
614
646
  // Never throw into the loop (interval callbacks crashing node:test aside,
615
647
  // an escaping throw here would kill the background goal process).
@@ -668,7 +700,30 @@ export async function runGoalLoop(input: RunGoalLoopInput): Promise<RunGoalLoopR
668
700
  // without this, isLockStale clause (b) force-steals the lock after 60s even
669
701
  // though this pid is alive (multi-goal clobber). Started immediately after
670
702
  // acquire; the timer is unref()-ed and never throws into the loop.
671
- lockHeartbeat = startWorkspaceLockHeartbeatRefresh(workspaceLock);
703
+ lockHeartbeat = startWorkspaceLockHeartbeatRefresh(workspaceLock, {
704
+ // R3-M-B1 (Wave-3): refresh()===false → ownership lost. Block the goal +
705
+ // emit an event + stop heartbeating; the while-loop's `goal.state !==
706
+ // "running"` check then exits ("break") BEFORE spawning/mutating further
707
+ // turns against a cwd another goal now owns. Idempotent — the interval
708
+ // fires once (it clears itself) and runGoalLoop's own failures can set
709
+ // blocked again harmlessly.
710
+ onOwnershipLost: () => {
711
+ try {
712
+ goal = safeSetStatus(store, goal.goalId, "blocked", goal, eventsPath);
713
+ } catch (error) {
714
+ logInternalError("goal-loop.workspaceLockLost.setStatus", error, `goalId=${goal.goalId}`, "warn");
715
+ }
716
+ appendEventBuffered(eventsPath, {
717
+ type: "goal.workspace_lock_lost",
718
+ runId: manifest.runId,
719
+ data: {
720
+ goalId: goal.goalId,
721
+ message:
722
+ "Workspace lock heartbeat refresh returned false — ownership lost (stolen/reclaimed). Goal blocked; no further turns were run against this cwd.",
723
+ },
724
+ }).catch((e) => logInternalError("goal-loop.buffered", e, "type=goal.workspace_lock_lost"));
725
+ },
726
+ });
672
727
  } catch (error) {
673
728
  logInternalError("goal-loop.workspaceLock", error, `goalId=${goal.goalId} cwd=${goal.cwd}`);
674
729
  goal = safeSetStatus(store, goal.goalId, "blocked", goal, eventsPath);
@@ -987,7 +1042,9 @@ export async function runGoalLoop(input: RunGoalLoopInput): Promise<RunGoalLoopR
987
1042
  // 'stuck' is re-hintable via `goal resume config.hint=...` (no double-execution: the
988
1043
  // loop is single-threaded per goal; resume re-spawns it). Default metric: shingle-Jaccard
989
1044
  // (cheap, local). Env PI_CREW_GOAL_OSCILLATION_EMBEDDINGS=1 enables embedding-based (P1.5).
990
- if (detectOscillation(goal.verdicts)) {
1045
+ // R3-M-B4: period-2 alternation (A,B,A,B) — pairwise window-3 misses it,
1046
+ // but every verdict matching the one two steps back is equally circular.
1047
+ if (detectOscillation(goal.verdicts) || detectPeriod2Oscillation(goal.verdicts)) {
991
1048
  const stuck = store.compareAndSetStatus(goal.goalId, "running", "stuck", eventsPath);
992
1049
  if (stuck) {
993
1050
  goal = stuck;
@@ -1,6 +1,7 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as path from "node:path";
3
3
  import type { TeamRunManifest } from "../../state/types.ts";
4
+ import { logInternalError } from "../../utils/internal-error.ts";
4
5
  import { agentStateFile, ensureAgentStateDir } from "../crew-agent-records.ts";
5
6
 
6
7
  export type LiveAgentControlOperation = "steer" | "follow-up" | "stop" | "resume";
@@ -96,20 +97,39 @@ export async function applyLiveAgentControlRequest(input: {
96
97
  if (seenRequestIds?.has(request.id)) return false;
97
98
  if (request.agentId && request.agentId !== agentId && request.agentId !== taskId) return false;
98
99
  seenRequestIds?.add(request.id);
99
- if (request.operation === "steer") await session.steer?.(request.message ?? "Please report current status and wrap up if possible.");
100
- else if (request.operation === "follow-up")
101
- await session.prompt?.(request.message ?? "Please continue with the follow-up request.", {
102
- source: "api",
103
- expandPromptTemplates: false,
104
- streamingBehavior: "followUp",
105
- });
106
- else if (request.operation === "resume")
107
- await session.prompt?.(request.message ?? "Please resume and report final status.", {
108
- source: "api",
109
- expandPromptTemplates: false,
110
- streamingBehavior: "followUp",
111
- });
112
- else if (request.operation === "stop") await session.abort?.();
100
+ // R3A-M1 (2026-10-17): a rejecting steer/prompt/abort used to escape this
101
+ // await into fire-and-forget `void` call sites (live-session-runtime) →
102
+ // unhandledRejection → host session crash. Every operation is now guarded;
103
+ // the failure is recorded on the request (`error`, + processedAt for
104
+ // observability) and logged — the control request is CONSUMED either way so
105
+ // a poisoned request cannot crash the loop that polls for it.
106
+ try {
107
+ if (request.operation === "steer")
108
+ await session.steer?.(request.message ?? "Please report current status and wrap up if possible.");
109
+ else if (request.operation === "follow-up")
110
+ await session.prompt?.(request.message ?? "Please continue with the follow-up request.", {
111
+ source: "api",
112
+ expandPromptTemplates: false,
113
+ streamingBehavior: "followUp",
114
+ });
115
+ else if (request.operation === "resume")
116
+ await session.prompt?.(request.message ?? "Please resume and report final status.", {
117
+ source: "api",
118
+ expandPromptTemplates: false,
119
+ streamingBehavior: "followUp",
120
+ });
121
+ else if (request.operation === "stop") await session.abort?.();
122
+ request.processedAt = new Date().toISOString();
123
+ } catch (error) {
124
+ const message = error instanceof Error ? error.message : String(error);
125
+ request.error = message;
126
+ request.processedAt = new Date().toISOString();
127
+ logInternalError(
128
+ "live-agent-control.apply-failed",
129
+ error instanceof Error ? error : new Error(message),
130
+ `runId=${request.runId} taskId=${request.taskId} operation=${request.operation} requestId=${request.id}`,
131
+ );
132
+ }
113
133
  return true;
114
134
  }
115
135