pi-crew 0.7.4 → 0.7.6

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 (56) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/README.md +11 -11
  3. package/docs/commands-reference.md +14 -10
  4. package/docs/troubleshooting.md +131 -0
  5. package/docs/usage.md +9 -4
  6. package/package.json +1 -1
  7. package/src/config/config.ts +11 -4
  8. package/src/config/types.ts +2 -0
  9. package/src/errors.ts +66 -0
  10. package/src/extension/action-suggestions.ts +71 -0
  11. package/src/extension/context-status-injection.ts +174 -0
  12. package/src/extension/knowledge-injection.ts +29 -1
  13. package/src/extension/register.ts +81 -65
  14. package/src/extension/team-tool/api.ts +3 -2
  15. package/src/extension/team-tool/cancel.ts +5 -4
  16. package/src/extension/team-tool/explain.ts +2 -1
  17. package/src/extension/team-tool/failure-patterns.ts +124 -0
  18. package/src/extension/team-tool/inspect.ts +10 -6
  19. package/src/extension/team-tool/lifecycle-actions.ts +5 -4
  20. package/src/extension/team-tool/respond.ts +4 -3
  21. package/src/extension/team-tool/run-not-found.ts +54 -0
  22. package/src/extension/team-tool/run.ts +26 -4
  23. package/src/extension/team-tool/status.ts +58 -4
  24. package/src/extension/team-tool.ts +5 -3
  25. package/src/runtime/async-runner.ts +7 -0
  26. package/src/runtime/background-runner.ts +7 -1
  27. package/src/runtime/chain-parser.ts +13 -5
  28. package/src/runtime/checkpoint.ts +13 -1
  29. package/src/runtime/child-pi.ts +9 -1
  30. package/src/runtime/live-session-runtime.ts +15 -1
  31. package/src/runtime/parent-guard.ts +2 -2
  32. package/src/runtime/pipeline-runner.ts +3 -1
  33. package/src/runtime/stale-reconciler.ts +28 -4
  34. package/src/runtime/task-runner.ts +50 -20
  35. package/src/runtime/team-runner.ts +19 -2
  36. package/src/runtime/verification-gates.ts +21 -1
  37. package/src/runtime/workspace-tree.ts +28 -2
  38. package/src/schema/team-tool-schema.ts +9 -0
  39. package/src/state/blob-store.ts +12 -10
  40. package/src/state/event-log-rotation.ts +114 -93
  41. package/src/state/event-log.ts +83 -23
  42. package/src/state/health-store.ts +6 -1
  43. package/src/state/locks.ts +66 -16
  44. package/src/state/state-store.ts +46 -2
  45. package/src/ui/card-colors.ts +7 -3
  46. package/src/ui/dashboard-panes/agents-pane.ts +15 -2
  47. package/src/ui/live-duration.ts +58 -0
  48. package/src/ui/tool-render.ts +7 -11
  49. package/src/ui/tool-renderers/index.ts +6 -3
  50. package/src/ui/widget/widget-formatters.ts +2 -13
  51. package/src/utils/fs-watch.ts +11 -60
  52. package/src/utils/run-watcher-registry.ts +164 -0
  53. package/src/workflows/discover-workflows.ts +2 -1
  54. package/src/workflows/workflow-config.ts +5 -0
  55. package/src/runtime/dynamic-script-runner.ts +0 -497
  56. package/src/runtime/sandbox.ts +0 -335
@@ -1,9 +1,17 @@
1
1
  import * as fs from "node:fs";
2
- import * as path from "node:path";
3
2
  import type { FSWatcher, WatchListener } from "node:fs";
4
3
 
5
- /** @internal */
6
- const FS_WATCH_RETRY_DELAY_MS = 5000;
4
+ /**
5
+ * Filesystem watcher helpers (slimmed down — pts/2 hang fix 2026-06-16).
6
+ *
7
+ * The recursive-watcher helpers (createRecursiveWatcher / watchCrewState /
8
+ * runIdFromStateRelativePath) were REMOVED: a recursive fs.watch on the run
9
+ * state tree exploded to O(total run history) inotify watches on Linux and
10
+ * caused a permanent interactive-session busy-loop. The bounded
11
+ * {@link RunWatcherRegistry} (one non-recursive watcher per ACTIVE run) now
12
+ * replaces them. Only the two primitives below survive — they are still used by
13
+ * manifest-cache, result-watcher, and run-watcher-registry.
14
+ */
7
15
 
8
16
  export function closeWatcher(watcher: FSWatcher | null | undefined): void {
9
17
  if (!watcher) {
@@ -31,60 +39,3 @@ export function watchWithErrorHandler(
31
39
  return null;
32
40
  }
33
41
  }
34
-
35
- /**
36
- * 1.3 — Watch a directory recursively and invoke `onChange` when any file
37
- * inside changes. Falls back to `null` on systems where `fs.watch` rejects
38
- * recursive mode (e.g., Linux when running on older kernels via FUSE/network FS).
39
- *
40
- * Callers MUST handle null and fall back to polling. The watcher emits the
41
- * filename relative to `rootDir` (forward-slash normalised on Windows).
42
- */
43
- export function createRecursiveWatcher(
44
- rootDir: string,
45
- onChange: (relativePath: string) => void,
46
- onError: (error: unknown) => void,
47
- ): FSWatcher | null {
48
- try {
49
- if (!fs.existsSync(rootDir)) fs.mkdirSync(rootDir, { recursive: true });
50
- const watcher = fs.watch(rootDir, { recursive: true }, (_eventType, filename) => {
51
- if (typeof filename !== "string" || filename.length === 0) return;
52
- onChange(filename.replace(/\\/g, "/"));
53
- });
54
- watcher.on("error", (error) => {
55
- try { watcher.close(); } catch { /* ignore */ }
56
- onError(error);
57
- });
58
- return watcher;
59
- } catch (error) {
60
- onError(error);
61
- return null;
62
- }
63
- }
64
-
65
- /**
66
- * Given a path relative to `<crewRoot>/state`, return the runId that owns
67
- * the change, or undefined if the path doesn't match any tracked run layout.
68
- */
69
- export function runIdFromStateRelativePath(relativePath: string): string | undefined {
70
- const parts = relativePath.split("/");
71
- // Layout is `runs/{runId}/...` — see docs/architecture.md state layer.
72
- if (parts.length >= 2 && parts[0] === "runs" && parts[1]) return parts[1];
73
- return undefined;
74
- }
75
-
76
- /** Convenience: combine the two helpers for `<crewRoot>/state` watching. */
77
- export function watchCrewState(
78
- stateDir: string,
79
- onRunChange: (runId: string) => void,
80
- onError: (error: unknown) => void,
81
- ): FSWatcher | null {
82
- return createRecursiveWatcher(stateDir, (relativePath) => {
83
- const runId = runIdFromStateRelativePath(relativePath);
84
- if (runId) onRunChange(runId);
85
- }, onError);
86
- }
87
-
88
- // Re-export path helper so callers don't pull node:path just for join.
89
- /** @internal */
90
- const joinPath = path.join;
@@ -0,0 +1,164 @@
1
+ /**
2
+ * RunWatcherRegistry — bounded per-run filesystem watcher registry.
3
+ *
4
+ * PROBLEM (pts/2 interactive-session hang, /home/bom/pts2-hang-investigation-2026-06-16.md):
5
+ * `watchCrewState` used `fs.watch(<crewRoot>/state, { recursive: true })`. On
6
+ * Linux, Node implements "recursive" by creating ONE inotify watch PER
7
+ * SUBDIRECTORY. With many historical runs under `.crew/state/runs/`, this
8
+ * ballooned to hundreds of watches (109→339 observed) — one per run dir ever —
9
+ * and the resulting event volume + render amplification produced a permanent
10
+ * busy-loop (71% CPU, 400KB/s read) even with no active work.
11
+ *
12
+ * FIX: instead of recursively watching the whole history, watch a SINGLE
13
+ * non-recursive watcher on the `runs/` root (to detect new run dirs appearing)
14
+ * PLUS one non-recursive watcher PER ACTIVE RUN. Total inotify cost is now
15
+ * O(active runs) — typically 1–5 — not O(total history). Completed runs stop
16
+ * being watched as soon as they leave the active set (reconciled by buildFrame,
17
+ * which reads manifest statuses each preload tick).
18
+ *
19
+ * The registry is intentionally small and directly unit-testable (a Map of
20
+ * watchers with add/remove/reconcile/close semantics).
21
+ */
22
+ import type { FSWatcher } from "node:fs";
23
+ import { closeWatcher, watchWithErrorHandler } from "./fs-watch.ts";
24
+
25
+ export interface ReconcileResult {
26
+ added: string[];
27
+ removed: string[];
28
+ }
29
+
30
+ export interface ActiveRun {
31
+ runId: string;
32
+ runDir: string;
33
+ }
34
+
35
+ export type RunChangeCallback = (runId: string) => void;
36
+ export type ErrorCallback = (error: unknown) => void;
37
+
38
+ export class RunWatcherRegistry {
39
+ private readonly runWatchers = new Map<string, FSWatcher>();
40
+ private rootWatcher: FSWatcher | undefined;
41
+ private closed = false;
42
+
43
+ /**
44
+ * Watch the `runs/` root directory (non-recursive) and invoke `onNewRun`
45
+ * whenever a new run subdirectory appears. This is the only way to detect a
46
+ * brand-new run, because `crew.run.created` is never emitted by the runtime
47
+ * (confirmed: only `crew.run.completed/failed/cancelled` are emitted).
48
+ */
49
+ setRootWatcher(
50
+ runsDir: string,
51
+ onNewRun: RunChangeCallback,
52
+ onError?: ErrorCallback,
53
+ ): void {
54
+ if (this.closed) return;
55
+ // Replace any prior root watcher.
56
+ closeWatcher(this.rootWatcher);
57
+ this.rootWatcher = watchWithErrorHandler(
58
+ runsDir,
59
+ (_eventType, filename) => {
60
+ if (typeof filename !== "string" || filename.length === 0) return;
61
+ // fs.watch reports directory entries as bare names (no slash on Linux).
62
+ // A new run dir appears as `runs/<runId>` → filename = "<runId>".
63
+ // Filter obviously-not-run-id noise (files, temp, etc.) defensively.
64
+ const candidate = filename.replace(/\\/g, "/").split("/")[0];
65
+ if (candidate.length === 0) return;
66
+ onNewRun(candidate);
67
+ },
68
+ (error) => {
69
+ if (onError) onError(error);
70
+ },
71
+ ) ?? undefined;
72
+ }
73
+
74
+ /**
75
+ * Add a NON-RECURSIVE watcher on a single run directory. Costs exactly ONE
76
+ * inotify watch. If a watcher for this runId already exists, close + replace.
77
+ * Returns true if a watcher is now active for this runId.
78
+ */
79
+ addRunWatcher(
80
+ runId: string,
81
+ runDir: string,
82
+ onChange: RunChangeCallback,
83
+ onError?: ErrorCallback,
84
+ ): boolean {
85
+ if (this.closed) return false;
86
+ const existing = this.runWatchers.get(runId);
87
+ if (existing) closeWatcher(existing);
88
+ const watcher = watchWithErrorHandler(
89
+ runDir,
90
+ () => onChange(runId),
91
+ (error) => {
92
+ if (onError) onError(error);
93
+ },
94
+ );
95
+ if (watcher) {
96
+ this.runWatchers.set(runId, watcher);
97
+ return true;
98
+ }
99
+ // watchWithErrorHandler returned null (fs.watch unsupported / dir missing).
100
+ // Remove any stale entry so hasWatcher() stays honest.
101
+ this.runWatchers.delete(runId);
102
+ return false;
103
+ }
104
+
105
+ /** Remove and close a specific run's watcher. No-op if not watched. */
106
+ removeRunWatcher(runId: string): void {
107
+ const watcher = this.runWatchers.get(runId);
108
+ if (watcher) {
109
+ closeWatcher(watcher);
110
+ this.runWatchers.delete(runId);
111
+ }
112
+ }
113
+
114
+ /** Is a run currently being watched? */
115
+ hasWatcher(runId: string): boolean {
116
+ return this.runWatchers.has(runId);
117
+ }
118
+
119
+ /**
120
+ * Reconcile against the current active-run set: add watchers for active runs
121
+ * not yet watched, remove watchers for runs that left the active set. Returns
122
+ * which runIds were added / removed (useful for logging + tests).
123
+ */
124
+ reconcile(
125
+ activeRuns: ActiveRun[],
126
+ onChange: RunChangeCallback,
127
+ onError?: ErrorCallback,
128
+ ): ReconcileResult {
129
+ if (this.closed) return { added: [], removed: [] };
130
+ const activeIds = new Set(activeRuns.map((r) => r.runId));
131
+ const added: string[] = [];
132
+ const removed: string[] = [];
133
+ // Remove watchers for runs no longer active.
134
+ for (const runId of [...this.runWatchers.keys()]) {
135
+ if (!activeIds.has(runId)) {
136
+ this.removeRunWatcher(runId);
137
+ removed.push(runId);
138
+ }
139
+ }
140
+ // Add watchers for newly-active runs.
141
+ for (const { runId, runDir } of activeRuns) {
142
+ if (!this.runWatchers.has(runId)) {
143
+ if (this.addRunWatcher(runId, runDir, onChange, onError)) {
144
+ added.push(runId);
145
+ }
146
+ }
147
+ }
148
+ return { added, removed };
149
+ }
150
+
151
+ /** Close ALL watchers (per-run + root). Safe to call multiple times. */
152
+ closeAll(): void {
153
+ this.closed = true;
154
+ for (const watcher of this.runWatchers.values()) closeWatcher(watcher);
155
+ this.runWatchers.clear();
156
+ closeWatcher(this.rootWatcher);
157
+ this.rootWatcher = undefined;
158
+ }
159
+
160
+ /** Number of active PER-RUN watchers (excludes the root watcher). */
161
+ get size(): number {
162
+ return this.runWatchers.size;
163
+ }
164
+ }
@@ -11,7 +11,7 @@ export interface WorkflowDiscoveryResult {
11
11
  project: WorkflowConfig[];
12
12
  }
13
13
 
14
- const STEP_CONFIG_KEYS = new Set(["role", "dependsOn", "parallelGroup", "output", "reads", "model", "skills", "progress", "worktree", "verify", "task", "seedPaths", "preStepScript", "preStepArgs", "preStepTimeout"]);
14
+ const STEP_CONFIG_KEYS = new Set(["role", "dependsOn", "parallelGroup", "output", "reads", "model", "skills", "progress", "worktree", "verify", "task", "seedPaths", "preStepScript", "preStepArgs", "preStepTimeout", "preStepOptional"]);
15
15
 
16
16
  function parseStepSection(id: string, body: string): WorkflowStep | undefined {
17
17
  const lines = body.trim().split("\n");
@@ -54,6 +54,7 @@ function parseStepSection(id: string, body: string): WorkflowStep | undefined {
54
54
  preStepScript: config.preStepScript || undefined,
55
55
  preStepArgs: parseCsv(config.preStepArgs) || undefined,
56
56
  preStepTimeout: parseOptionalInteger(config.preStepTimeout) ?? undefined,
57
+ preStepOptional: config.preStepOptional === "true" || config.preStepOptional === "1",
57
58
  };
58
59
  }
59
60
 
@@ -25,6 +25,11 @@ export interface WorkflowStep {
25
25
  preStepArgs?: string[];
26
26
  /** Timeout in ms for preStepScript. Default: 30000. */
27
27
  preStepTimeout?: number;
28
+ /** Round 21 (E4): if true, a failing preStepScript does NOT abort the task.
29
+ * The failure is logged as a warning and the task proceeds without the
30
+ * pre-step output. Use for advisory hooks (e.g. optional test runs) whose
31
+ * failure shouldn't block the workflow. Default: false (fail-fast). */
32
+ preStepOptional?: boolean;
28
33
  }
29
34
 
30
35
  export interface WorkflowConfig {