pi-crew 0.9.12 → 0.9.14

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.
@@ -0,0 +1,400 @@
1
+ /**
2
+ * Concrete `ChainTaskRunner` that executes each chain step as a REAL team run.
3
+ *
4
+ * This is the production wiring that makes `ChainRunner` (and Fix C's
5
+ * `__chainHistoryNotes`) reachable: previously `ChainRunner` had zero production
6
+ * callers and only existed with unit-test mock runners. Each step delegates to
7
+ * the existing `handleRun` (which owns config load, team/workflow discovery,
8
+ * worktree preconditions, manifest creation, and `executeTeamRun`), so the
9
+ * ~200 lines of run machinery are NOT duplicated.
10
+ *
11
+ * Context passage (the whole point of the chain feature): `enrichContextFromHandoffs`
12
+ * places `__chainHistory` + `__chainHistoryNotes` into `packet.context`; this
13
+ * executor serializes them into a `# Previous Steps in This Chain` block that is
14
+ * prepended to the step's goal. `handleRun` → `executeTeamRun` → worker prompt,
15
+ * so step N+1's worker sees step N's handoff summary and the Fix C markers.
16
+ *
17
+ * Circular-import avoidance: `handleRun` is received as an INJECTED function
18
+ * reference (dependency injection) rather than a static import. `run.ts` lazy-
19
+ * imports `chain-dispatch.ts`, which imports this file. A static import of
20
+ * `run.ts` here would create a `run.ts ↔ chain-executor.ts` cycle that races
21
+ * module-record instantiation under jiti. The DI pattern breaks the cycle.
22
+ *
23
+ * @see src/runtime/chain-runner.ts (runChain, enrichContextFromHandoffs — Fix C)
24
+ * @see src/extension/team-tool/run.ts (handleRun — the reused machinery)
25
+ */
26
+
27
+ import type { ChainTaskRunner } from "../../runtime/chain-runner.ts";
28
+ import type { TaskPacket, TaskResult, Decision } from "../../runtime/handoff-manager.ts";
29
+ import { loadRunManifestById, createRunPaths } from "../../state/state-store.ts";
30
+ import { readIfSmallWithTee } from "../../runtime/task-output-context.ts";
31
+ import type { TeamRunManifest, TeamTaskState } from "../../state/types.ts";
32
+ import type { PiTeamsToolResult } from "../tool-result.ts";
33
+ import { textFromToolResult } from "../tool-result.ts";
34
+ import type { TeamToolParamsValue } from "../../schema/team-tool-schema.ts";
35
+ import type { TeamContext } from "./context.ts";
36
+ import * as fs from "node:fs";
37
+ import * as path from "node:path";
38
+
39
+ /**
40
+ * Signature of `handleRun`, declared locally (type-only) to break the
41
+ * import cycle with run.ts. Structurally identical to
42
+ * `(params, ctx) => Promise<PiTeamsToolResult>`.
43
+ */
44
+ export type HandleRunFn = (
45
+ params: TeamToolParamsValue,
46
+ ctx: TeamContext,
47
+ ) => Promise<PiTeamsToolResult>;
48
+
49
+ /** Per-step team/workflow/model overrides forwarded from the chain invocation. */
50
+ export interface ChainExecutorOverrides {
51
+ team?: string;
52
+ workflow?: string;
53
+ model?: string;
54
+ }
55
+
56
+ /** Shape of one entry in `context.__chainHistory` produced by enrichContextFromHandoffs. */
57
+ interface ChainHistoryEntry {
58
+ step?: string;
59
+ outcome?: string;
60
+ filesCreated?: string[];
61
+ filesModified?: string[];
62
+ decisions?: Decision[];
63
+ nextSteps?: string[];
64
+ outputText?: string;
65
+ }
66
+
67
+ /**
68
+ * Serialize `packet.context.__chainHistory` (+ `__chainHistoryNotes`) into a
69
+ * human-readable block prefixed with `# Previous Steps in This Chain`.
70
+ *
71
+ * This is the CRITICAL coupling that makes Fix C's `__chainHistoryNotes`
72
+ * markers visible to step N+1's worker: those markers travel as a sibling
73
+ * array on the context object, and only become worker-visible once formatted
74
+ * into the goal text here. Returns `""` when there is no history.
75
+ *
76
+ * Extracted as a pure function for unit testability (acceptance criterion 6c).
77
+ */
78
+ export function formatChainHistory(context: Record<string, unknown>): string {
79
+ const history = context.__chainHistory;
80
+ if (!Array.isArray(history) || history.length === 0) {
81
+ return "";
82
+ }
83
+
84
+ const lines: string[] = ["# Previous Steps in This Chain", ""];
85
+
86
+ for (const raw of history) {
87
+ if (!raw || typeof raw !== "object") continue;
88
+ const entry = raw as ChainHistoryEntry;
89
+ const stepName = entry.step ?? "unknown";
90
+ const outcome = entry.outcome ?? "unknown";
91
+ lines.push(`## Step ${stepName}: ${outcome}`);
92
+ lines.push(`Files created: ${entry.filesCreated?.length ? entry.filesCreated.join(", ") : "none"}`);
93
+ lines.push(`Files modified: ${entry.filesModified?.length ? entry.filesModified.join(", ") : "none"}`);
94
+ if (entry.decisions?.length) {
95
+ lines.push("Decisions:");
96
+ for (const d of entry.decisions) {
97
+ const rationale = (d && typeof d === "object" && "rationale" in d) ? String(d.rationale) : "(undocumented)";
98
+ const decisionOutcome = (d && typeof d === "object" && "outcome" in d) ? String(d.outcome) : "";
99
+ lines.push(`- ${rationale}${decisionOutcome ? ` → ${decisionOutcome}` : ""}`);
100
+ }
101
+ }
102
+ if (entry.nextSteps?.length) {
103
+ lines.push("Next steps:");
104
+ for (const ns of entry.nextSteps) lines.push(`- ${ns}`);
105
+ }
106
+ if (entry.outputText) {
107
+ lines.push("Output:");
108
+ lines.push(entry.outputText);
109
+ }
110
+ lines.push("");
111
+ }
112
+
113
+ // Append Fix C honesty markers verbatim — these note elided/oversized history.
114
+ const notes = context.__chainHistoryNotes;
115
+ if (Array.isArray(notes)) {
116
+ for (const note of notes) {
117
+ if (typeof note === "string" && note.length > 0) lines.push(note);
118
+ }
119
+ }
120
+
121
+ return lines.join("\n").trimEnd();
122
+ }
123
+
124
+ /**
125
+ * Map a completed team run's manifest+tasks → a chain `TaskResult`.
126
+ *
127
+ * Outcome is derived from the manifest status, cross-checked against task
128
+ * statuses (a run that completed but with a failed task reads as "partial").
129
+ * `usage.totalTokens` sums each task's input+output tokens; `duration` from
130
+ * manifest timestamps; `error` is best-effort from the first failed task or
131
+ * the manifest summary. `filesCreated`/`filesModified` are intentionally left
132
+ * unset — `TeamTaskState` does not carry them, so we never fabricate them.
133
+ *
134
+ * Extracted as a pure function for unit testability (acceptance criterion 6b).
135
+ */
136
+ export function mapRunToTaskResult(
137
+ manifest: TeamRunManifest,
138
+ tasks: TeamTaskState[],
139
+ ): TaskResult {
140
+ // Determine outcome from manifest status.
141
+ let outcome: TaskResult["outcome"];
142
+ if (manifest.status === "completed") {
143
+ outcome = "success";
144
+ } else if (
145
+ manifest.status === "failed" ||
146
+ manifest.status === "blocked" ||
147
+ manifest.status === "cancelled"
148
+ ) {
149
+ outcome = "failure";
150
+ } else {
151
+ // queued / planning / running → not terminal yet
152
+ outcome = "partial";
153
+ }
154
+
155
+ // Cross-check tasks: if a manifest claims completed but a task failed, it is partial.
156
+ const hasFailed = tasks.some((t) => t.status === "failed" || t.status === "needs_attention");
157
+ const hasCompleted = tasks.some((t) => t.status === "completed");
158
+ if (hasFailed && (hasCompleted || outcome === "success")) {
159
+ outcome = "partial";
160
+ }
161
+
162
+ // Token usage: sum task input + output.
163
+ let totalTokens = 0;
164
+ for (const t of tasks) {
165
+ totalTokens += (t.usage?.input ?? 0) + (t.usage?.output ?? 0);
166
+ }
167
+
168
+ // Duration from manifest timestamps.
169
+ let duration = 0;
170
+ const created = Date.parse(manifest.createdAt);
171
+ const updated = Date.parse(manifest.updatedAt);
172
+ if (!Number.isNaN(created) && !Number.isNaN(updated) && updated >= created) {
173
+ duration = updated - created;
174
+ }
175
+
176
+ // Error: best-effort from first failed task, else manifest summary.
177
+ let error: string | undefined;
178
+ if (outcome !== "success") {
179
+ const failedTask = tasks.find((t) => t.status === "failed");
180
+ error = failedTask?.error ?? manifest.summary;
181
+ }
182
+
183
+ const taskResult: TaskResult = {
184
+ outcome,
185
+ usage: { totalTokens },
186
+ duration,
187
+ };
188
+ if (error) taskResult.error = error;
189
+ return taskResult;
190
+ }
191
+
192
+ /**
193
+ * Read completed tasks' result artifacts and concatenate their output text.
194
+ * Mirrors the pattern in task-output-context.ts:collectDependencyOutputContext
195
+ * (readIfSmallWithTee with baseDir = artifactsRoot).
196
+ *
197
+ * Returns the concatenated output text, or undefined if no artifacts were readable.
198
+ */
199
+ export function readChainStepOutput(
200
+ manifest: TeamRunManifest,
201
+ tasks: TeamTaskState[],
202
+ ): string | undefined {
203
+ const parts: string[] = [];
204
+ for (const t of tasks) {
205
+ if (t.status !== "completed" || !t.resultArtifact?.path) continue;
206
+ const read = readIfSmallWithTee(t.resultArtifact.path, {
207
+ baseDir: manifest.artifactsRoot,
208
+ });
209
+ if (read?.content && read.content.trim().length > 0) {
210
+ parts.push(read.content.trim());
211
+ }
212
+ }
213
+ return parts.length > 0 ? parts.join("\n\n") : undefined;
214
+ }
215
+
216
+ /**
217
+ * Concrete `ChainTaskRunner`: each step is a full team run via the injected
218
+ * `handleRun`. Step runIds are captured on `stepRunIds` so the dispatch can
219
+ * surface them in the summary and acceptance verification.
220
+ *
221
+ * Errors are intentionally NOT caught here — `runChain` wraps each step's
222
+ * `runTask` call in try/catch, records `outcome: "failure"`, and respects
223
+ * `continueOnError`. Letting exceptions propagate keeps the chain-runner's
224
+ * existing failure semantics intact.
225
+ */
226
+ export class ChainTeamRunExecutor implements ChainTaskRunner {
227
+ /** RunId captured per executed step, in execution order. */
228
+ readonly stepRunIds: string[] = [];
229
+
230
+ private readonly handleRunRef: HandleRunFn;
231
+ private readonly ctx: TeamContext;
232
+ private readonly overrides: ChainExecutorOverrides;
233
+
234
+ constructor(opts: {
235
+ handleRun: HandleRunFn;
236
+ ctx: TeamContext;
237
+ overrides?: ChainExecutorOverrides;
238
+ }) {
239
+ this.handleRunRef = opts.handleRun;
240
+ this.ctx = opts.ctx;
241
+ this.overrides = opts.overrides ?? {};
242
+ }
243
+
244
+ async runTask(packet: TaskPacket): Promise<TaskResult> {
245
+ const context = packet.context ?? {};
246
+
247
+ // 1. Format chain history into the goal — this is how context reaches the worker.
248
+ // Mirrors the parentContext pattern in child-pi.ts (parent context + task delimiter).
249
+ const historyPrefix = formatChainHistory(context);
250
+ const enrichedGoal = historyPrefix
251
+ ? `${historyPrefix}\n\n---\n# Current Chain Step\n${packet.goal}`
252
+ : packet.goal;
253
+
254
+ // 2. Resolve team/workflow/model: step config (set by executeStep) → overrides → default.
255
+ const stepTeam =
256
+ (context.__chainStepTeam as string | undefined) ??
257
+ this.overrides.team ??
258
+ "default";
259
+ const stepWorkflow =
260
+ (context.__chainStepWorkflow as string | undefined) ??
261
+ this.overrides.workflow;
262
+ const stepModel =
263
+ (context.__chainStepModel as string | undefined) ??
264
+ this.overrides.model;
265
+
266
+ // 3. Call handleRun for the heavy lifting. async:false forces each step to
267
+ // complete synchronously (overrides asyncByDefault) so the chain is sequential.
268
+ // NO `chain` key here — so the run.ts guard does not re-enter chain dispatch.
269
+ const runParams: TeamToolParamsValue = {
270
+ action: "run",
271
+ goal: enrichedGoal,
272
+ team: stepTeam,
273
+ async: false,
274
+ ...(stepWorkflow ? { workflow: stepWorkflow } : {}),
275
+ ...(stepModel ? { model: stepModel } : {}),
276
+ };
277
+ const runRes = await this.handleRunRef(runParams, this.ctx);
278
+
279
+ // 4. Extract runId + map to TaskResult. If no runId, the run was blocked before
280
+ // a manifest existed — map as a failure.
281
+ const runId = runRes.details?.runId;
282
+ if (!runId) {
283
+ return {
284
+ outcome: "failure",
285
+ error: textFromToolResult(runRes) || "Chain step produced no runId",
286
+ };
287
+ }
288
+ this.stepRunIds.push(runId);
289
+
290
+ const cwd = this.ctx.cwd ?? process.cwd();
291
+ const loaded = loadRunManifestById(cwd, runId);
292
+ if (!loaded) {
293
+ // Manifest not loadable — fall back to the tool result's own status.
294
+ return {
295
+ outcome: runRes.details?.status === "error" ? "failure" : "partial",
296
+ };
297
+ }
298
+ const result = mapRunToTaskResult(loaded.manifest, loaded.tasks);
299
+ const outputText = readChainStepOutput(loaded.manifest, loaded.tasks);
300
+ if (outputText) result.outputText = outputText;
301
+ return result;
302
+ }
303
+ }
304
+
305
+ // ─── test helpers (exported for unit/integration tests) ──────────────────
306
+
307
+ /**
308
+ * Write a minimal valid run fixture (manifest + tasks + events) to `cwd`'s
309
+ * state root so `loadRunManifestById(cwd, runId)` validates and returns it.
310
+ * Used by integration tests to exercise `ChainTeamRunExecutor` with a mocked
311
+ * `handleRun` while still going through the REAL manifest loader + result mapper.
312
+ */
313
+ export function writeRunFixture(
314
+ cwd: string,
315
+ runId: string,
316
+ opts: {
317
+ status?: TeamRunManifest["status"];
318
+ tasks?: TeamTaskState[];
319
+ summary?: string;
320
+ /** Worker result text to write to results/<taskId>.txt (sets resultArtifact on tasks). */
321
+ resultText?: string;
322
+ /** Task ID for the result artifact (default: "01_task"). */
323
+ taskId?: string;
324
+ },
325
+ ): TeamRunManifest {
326
+ const paths = createRunPaths(cwd, runId);
327
+ fs.mkdirSync(paths.stateRoot, { recursive: true });
328
+ const now = new Date().toISOString();
329
+ const start = new Date(Date.now() - 10_000).toISOString();
330
+ const taskId = opts.taskId ?? "01_task";
331
+
332
+ // Build tasks: if resultText is provided without explicit tasks, create a
333
+ // default completed task carrying a resultArtifact pointing at the result file.
334
+ let tasks = opts.tasks;
335
+ if (opts.resultText) {
336
+ const resultArtifact = {
337
+ kind: "result" as const,
338
+ path: `results/${taskId}.txt`,
339
+ createdAt: now,
340
+ producer: "worker",
341
+ retention: "run" as const,
342
+ };
343
+ if (!tasks || tasks.length === 0) {
344
+ tasks = [{
345
+ id: taskId,
346
+ runId,
347
+ role: "executor",
348
+ agent: "executor",
349
+ title: "(chain step fixture)",
350
+ status: "completed",
351
+ dependsOn: [],
352
+ cwd,
353
+ resultArtifact,
354
+ }];
355
+ } else {
356
+ // Attach resultArtifact to the first task if it doesn't have one.
357
+ tasks = tasks.map((t, i) => i === 0 && !t.resultArtifact
358
+ ? { ...t, resultArtifact }
359
+ : t);
360
+ }
361
+ }
362
+
363
+ const manifest: TeamRunManifest = {
364
+ schemaVersion: 1,
365
+ runId,
366
+ team: "default",
367
+ workflow: "default",
368
+ goal: "(chain step fixture)",
369
+ status: opts.status ?? "completed",
370
+ workspaceMode: "single",
371
+ createdAt: start,
372
+ updatedAt: now,
373
+ cwd,
374
+ stateRoot: paths.stateRoot,
375
+ artifactsRoot: paths.artifactsRoot,
376
+ tasksPath: paths.tasksPath,
377
+ eventsPath: paths.eventsPath,
378
+ artifacts: [],
379
+ ...(opts.summary ? { summary: opts.summary } : {}),
380
+ };
381
+
382
+ // Write the result artifact file + register on manifest if resultText provided.
383
+ if (opts.resultText) {
384
+ const resultDir = path.join(paths.artifactsRoot, "results");
385
+ fs.mkdirSync(resultDir, { recursive: true });
386
+ fs.writeFileSync(path.join(resultDir, `${taskId}.txt`), opts.resultText, "utf-8");
387
+ manifest.artifacts.push({
388
+ kind: "result",
389
+ path: `results/${taskId}.txt`,
390
+ createdAt: now,
391
+ producer: "worker",
392
+ retention: "run",
393
+ });
394
+ }
395
+
396
+ fs.writeFileSync(paths.manifestPath, JSON.stringify(manifest), "utf-8");
397
+ fs.writeFileSync(paths.tasksPath, JSON.stringify(tasks ?? []), "utf-8");
398
+ fs.writeFileSync(paths.eventsPath, "", "utf-8");
399
+ return manifest;
400
+ }
@@ -113,6 +113,15 @@ function scheduleBackgroundEarlyExitGuard(cwd: string, runId: string, pid: numbe
113
113
  }
114
114
 
115
115
  export async function handleRun(params: TeamToolParamsValue, ctx: TeamContext): Promise<PiTeamsToolResult> {
116
+ // CHAIN DISPATCH: runs before goal validation since a chain has no top-level
117
+ // goal. The injected handleRun reference breaks the run.ts ↔ chain-dispatch.ts
118
+ // import cycle; the lazy import defers the chain-executor cost until a chain is
119
+ // actually requested. Existing run/workflow paths below are unchanged.
120
+ if (params.chain) {
121
+ // LAZY: defer chain-dispatch import until a chain is actually requested.
122
+ const { handleChainRun } = await import("./chain-dispatch.ts");
123
+ return handleChainRun(params, ctx, handleRun);
124
+ }
116
125
  const goal = params.goal ?? params.task;
117
126
  if (!goal) return result("Run requires goal or task.", { action: "run", status: "error" }, true);
118
127
  const intentPrefix = goal.length > 60 ? `${goal.slice(0, 57)}...` : goal;
@@ -43,10 +43,13 @@ export function ensureMemoryDir(memoryDir: string): void {
43
43
 
44
44
  export function readMemoryIndex(memoryDir: string): string | undefined {
45
45
  if (isSymlink(memoryDir)) return undefined;
46
- const content = safeReadMemoryFile(path.join(memoryDir, "MEMORY.md"));
46
+ const memPath = path.join(memoryDir, "MEMORY.md");
47
+ const content = safeReadMemoryFile(memPath);
47
48
  if (content === undefined) return undefined;
48
49
  const lines = content.split(/\r?\n/);
49
- return lines.length > MAX_MEMORY_LINES ? `${lines.slice(0, MAX_MEMORY_LINES).join("\n")}\n... (truncated at 200 lines)` : content;
50
+ return lines.length > MAX_MEMORY_LINES
51
+ ? `${lines.slice(0, MAX_MEMORY_LINES).join("\n")}\n... (truncated at 200 lines). Full file: ${memPath} — use the \`read\` tool if you need entries beyond the head.`
52
+ : content;
50
53
  }
51
54
 
52
55
  export function buildMemoryBlock(agentName: string, scope: AgentMemoryScope, cwd: string, writable: boolean): string {
@@ -25,6 +25,19 @@ import type { executeTeamRun as ExecuteTeamRunFn } from "./team-runner.ts";
25
25
  import type { TeamRunManifest, TeamTaskState } from "../state/types.ts";
26
26
 
27
27
  let _cachedExecuteTeamRun: typeof ExecuteTeamRunFn | undefined;
28
+
29
+ /** Maximum runtime for a single background run before the watchdog force-aborts
30
+ * it. Prevents zombie background-runner processes when a team run hangs forever
31
+ * (e.g. a hung child Pi process, a stuck lock, or a test that spawns a run
32
+ * without cleanup). Default 2h — generous for legitimate long runs (research
33
+ * workflows, goal-loops) but catches true zombies (observed: 10h+ stale test
34
+ * runs). Override via PI_CREW_MAX_RUN_MS env (milliseconds). The watchdog
35
+ * aborts via the shared AbortController, then force-exits after a grace
36
+ * period in case the abort signal does not propagate to all execution paths. */
37
+ const MAX_BACKGROUND_RUN_MS = (() => {
38
+ const env = Number.parseInt(process.env.PI_CREW_MAX_RUN_MS ?? "", 10);
39
+ return Number.isFinite(env) && env > 0 ? env : 2 * 60 * 60 * 1000;
40
+ })();
28
41
  async function executeTeamRun(
29
42
  ...args: Parameters<typeof ExecuteTeamRunFn>
30
43
  ): Promise<Awaited<ReturnType<typeof ExecuteTeamRunFn>>> {
@@ -234,6 +247,7 @@ function runCleanup(
234
247
  stopParentGuard: () => void,
235
248
  stopHeartbeat: () => void,
236
249
  keepAlive: NodeJS.Timeout,
250
+ watchdogTimer: NodeJS.Timeout,
237
251
  exitDueToRejection: boolean,
238
252
  eventsPath?: string,
239
253
  ): void {
@@ -245,6 +259,9 @@ function runCleanup(
245
259
  // FIX: clearInterval FIRST, then kill children. This ensures the heartbeat
246
260
  // interval is always cleaned up even if terminateActiveChildPiProcesses throws.
247
261
  clearInterval(keepAlive);
262
+ // Clear the anti-zombie watchdog so a normally-completing run does not
263
+ // carry a pending force-exit timer into the exit path.
264
+ clearTimeout(watchdogTimer);
248
265
  // FIX Issues #1, #2, #4: Wrap child process termination in try/catch so errors
249
266
  // don't prevent the cleanup from completing. We log but don't re-throw since
250
267
  // we're already exiting.
@@ -563,6 +580,37 @@ async function main(): Promise<void> {
563
580
  // bounded by the 5s interval. The event loop exit is deferred at most 5s.
564
581
  const keepAlive = setInterval(() => {}, 5000);
565
582
 
583
+ // WATCHDOG (anti-zombie): if the run exceeds MAX_BACKGROUND_RUN_MS without
584
+ // completing, abort it and force-exit. Without this, a hung team run
585
+ // (stuck child Pi, deadlocked lock, test that never cleans up) leaves the
586
+ // background-runner alive forever because keepAlive holds the event loop.
587
+ // The watchdog fires once; it is cleared in the finally block via runCleanup.
588
+ const watchdogTimer = setTimeout(() => {
589
+ console.error(`[background-runner] WATCHDOG: run ${runId} exceeded ${MAX_BACKGROUND_RUN_MS}ms — aborting (zombie prevention)`);
590
+ try {
591
+ appendEvent(manifest.eventsPath, {
592
+ type: "async.watchdog_fired",
593
+ runId,
594
+ message: `Run exceeded ${MAX_BACKGROUND_RUN_MS}ms and was force-aborted to prevent a zombie background-runner process.`,
595
+ data: { maxRunMs: MAX_BACKGROUND_RUN_MS },
596
+ });
597
+ } catch { /* best-effort event log */ }
598
+ // Signal the finally block to exit(1) after cleanup.
599
+ exitDueToRejection = true;
600
+ // Abort the in-flight team run via the shared signal (propagates to
601
+ // executeTeamRun → child-pi → kills child processes).
602
+ abortController.abort();
603
+ // Hard-exit safety net: if the abort does not propagate within 15s
604
+ // (e.g. a hung native call), force-kill so the process cannot linger.
605
+ const forceExit = setTimeout(() => {
606
+ console.error(`[background-runner] WATCHDOG: abort did not propagate within grace period — force-exiting`);
607
+ stopParentGuard();
608
+ try { terminateActiveChildPiProcesses(); } catch { /* best-effort */ }
609
+ process.exit(1);
610
+ }, 15_000);
611
+ forceExit.unref();
612
+ }, MAX_BACKGROUND_RUN_MS);
613
+
566
614
  try {
567
615
  debugLog(`[background-runner] about to call discoverAgents`);
568
616
  const agents = allAgents(discoverAgents(cwd));
@@ -798,6 +846,7 @@ async function main(): Promise<void> {
798
846
  stopParentGuard,
799
847
  stopHeartbeat,
800
848
  keepAlive,
849
+ watchdogTimer,
801
850
  exitDueToRejection,
802
851
  manifest.eventsPath,
803
852
  );
@@ -496,25 +496,54 @@ export class ChainRunner {
496
496
  return context;
497
497
  }
498
498
 
499
- // Limit history size to prevent memory leak (H2)
500
- const limitedHandoffs = handoffs.slice(-ChainRunner.MAX_CHAIN_HISTORY_SIZE);
499
+ const notes: string[] = [];
500
+
501
+ // Limit history size to prevent memory leak (H2). Emit a marker so a dropped
502
+ // entry does not vanish silently — this was the only silent-loss path in the
503
+ // output-handling machinery (see research-findings/output-handling-deep-dive.md §F).
504
+ let limitedHandoffs = handoffs;
505
+ if (handoffs.length > ChainRunner.MAX_CHAIN_HISTORY_SIZE) {
506
+ const dropped = handoffs.length - ChainRunner.MAX_CHAIN_HISTORY_SIZE;
507
+ notes.push(`[chain history limited to last ${ChainRunner.MAX_CHAIN_HISTORY_SIZE} entries; ${dropped} older entr${dropped === 1 ? "y" : "ies"} omitted]`);
508
+ limitedHandoffs = handoffs.slice(-ChainRunner.MAX_CHAIN_HISTORY_SIZE);
509
+ }
501
510
 
502
- // Limit per-entry size to prevent memory issues from large artifacts
511
+ // Limit per-entry size to prevent memory issues from large artifacts.
503
512
  const filteredHandoffs = limitedHandoffs.filter(h => {
504
513
  const size = JSON.stringify(h).length;
505
514
  return size <= ChainRunner.MAX_HANDOFF_ENTRY_SIZE;
506
515
  });
516
+ const oversizedDropped = limitedHandoffs.length - filteredHandoffs.length;
517
+ if (oversizedDropped > 0) {
518
+ notes.push(`[${oversizedDropped} handoff entr${oversizedDropped === 1 ? "y" : "ies"} omitted (> ${ChainRunner.MAX_HANDOFF_ENTRY_SIZE} bytes each)]`);
519
+ }
507
520
 
508
- return {
509
- ...context,
510
- __chainHistory: filteredHandoffs.map(h => ({
521
+ // Track array-cap drops so capped lists do not vanish silently. Each field
522
+ // over its cap records the full count so a reader knows data was elided.
523
+ const arrayCapNotes: string[] = [];
524
+ const mapped = filteredHandoffs.map(h => {
525
+ const over: string[] = [];
526
+ if (h.filesCreated && h.filesCreated.length > 50) over.push(`filesCreated=${h.filesCreated.length}`);
527
+ if (h.filesModified && h.filesModified.length > 50) over.push(`filesModified=${h.filesModified.length}`);
528
+ if (h.decisions && h.decisions.length > 20) over.push(`decisions=${h.decisions.length}`);
529
+ if (h.nextSteps && h.nextSteps.length > 20) over.push(`nextSteps=${h.nextSteps.length}`);
530
+ if (over.length > 0) arrayCapNotes.push(`step ${h.taskId}: ${over.join(", ")} (capped)`);
531
+ return {
511
532
  step: h.taskId,
512
533
  outcome: h.outcome,
513
534
  filesCreated: h.filesCreated?.slice(0, 50), // Limit array size
514
535
  filesModified: h.filesModified?.slice(0, 50), // Limit array size
515
536
  decisions: h.decisions?.slice(0, 20), // Limit array size
516
537
  nextSteps: h.nextSteps?.slice(0, 20), // Limit array size
517
- })),
538
+ ...(h.outputText ? { outputText: h.outputText.slice(0, 5000) } : {}), // Size-capped worker output
539
+ };
540
+ });
541
+ if (arrayCapNotes.length > 0) notes.push(`[list caps applied: ${arrayCapNotes.join("; ")}]`);
542
+
543
+ return {
544
+ ...context,
545
+ __chainHistory: mapped,
546
+ ...(notes.length > 0 ? { __chainHistoryNotes: notes } : {}),
518
547
  };
519
548
  }
520
549
 
@@ -525,13 +554,23 @@ export class ChainRunner {
525
554
  config: ChainStep,
526
555
  context: Record<string, unknown>
527
556
  ): Promise<TaskResult> {
557
+ // Pass step config (team/workflow/model) into the packet context so the
558
+ // concrete ChainTaskRunner can resolve which team/workflow to run. Without
559
+ // this, a step parsed to a @teamName reference would lose its team because
560
+ // the only channel from executeStep to runTask is the TaskPacket. Purely
561
+ // additive — adds sibling keys alongside __chainHistory/Notes.
528
562
  const packet: TaskPacket = {
529
563
  taskId: `chain-${Date.now()}-${config.name}`,
530
564
  runId: "chain",
531
565
  goal: config.inlineGoal ?? config.name,
532
566
  summarizeThreshold: 3000,
533
567
  collapseContext: true,
534
- context,
568
+ context: {
569
+ ...context,
570
+ __chainStepTeam: config.team,
571
+ __chainStepWorkflow: config.workflow,
572
+ __chainStepModel: config.model,
573
+ },
535
574
  };
536
575
 
537
576
  return this.taskRunner.runTask(packet);