pi-crew 0.9.42 → 0.9.44

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 (41) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +14 -0
  3. package/dist/build-meta.json +6692 -13022
  4. package/dist/index.mjs +54585 -65621
  5. package/dist/index.mjs.map +4 -4
  6. package/package.json +1 -1
  7. package/scripts/build-bundle.mjs +2 -1
  8. package/src/agents/agent-config.ts +1 -1
  9. package/src/config/config.ts +1 -1
  10. package/src/config/types.ts +0 -2
  11. package/src/extension/crew-vibes/config.ts +1 -1
  12. package/src/extension/crew-vibes/font-detect.ts +16 -3
  13. package/src/extension/cross-extension-rpc.ts +8 -4
  14. package/src/extension/registration/commands.ts +5 -1
  15. package/src/extension/registration/foreground-run-controller.ts +28 -0
  16. package/src/extension/team-tool.ts +5 -0
  17. package/src/prompt/prompt-runtime.ts +15 -0
  18. package/src/runtime/child-pi-constants.ts +42 -0
  19. package/src/runtime/child-pi-kill.ts +180 -0
  20. package/src/runtime/child-pi-spawn.ts +234 -0
  21. package/src/runtime/child-pi-steering.ts +128 -0
  22. package/src/runtime/child-pi-streams.ts +296 -0
  23. package/src/runtime/child-pi-transcript.ts +169 -0
  24. package/src/runtime/child-pi.ts +75 -837
  25. package/src/runtime/compact-stages/tail-capture-stage.ts +1 -1
  26. package/src/runtime/dwf-state-store.ts +5 -0
  27. package/src/runtime/dynamic-workflow-context.ts +78 -18
  28. package/src/runtime/dynamic-workflow-runner.ts +18 -2
  29. package/src/runtime/goal-evaluator.ts +59 -27
  30. package/src/runtime/goal-loop-runner.ts +40 -11
  31. package/src/runtime/live-agent-manager.ts +18 -0
  32. package/src/runtime/pi-json-output.ts +2 -1
  33. package/src/runtime/pi-spawn.ts +7 -1
  34. package/src/runtime/run-coalesced-task-group.ts +18 -10
  35. package/src/runtime/task-runner/prompt-builder.ts +2 -2
  36. package/src/runtime/team-runner.ts +6 -1
  37. package/src/state/event-log.ts +1 -0
  38. package/src/state/locks.ts +132 -49
  39. package/src/state/worker-atomic-writer.ts +1 -0
  40. package/src/utils/paths.ts +1 -1
  41. package/src/worktree/worktree-manager.ts +47 -19
@@ -0,0 +1,169 @@
1
+ /**
2
+ * child-pi-transcript.ts — Transcript batching module for child Pi worker output.
3
+ *
4
+ * Extracted from child-pi.ts (H-7 decomposition, step 1). Zero behavior change.
5
+ *
6
+ * Architecture:
7
+ * appendTranscript() validates the transcriptPath against the artifactsRoot
8
+ * containment boundary, redacts the line, and pushes it to a module-scoped
9
+ * batch buffer keyed by safePath. A debounced 50ms timer flushes the buffer
10
+ * in one open/write/close per path (O_NOFOLLOW | O_CREAT | O_APPEND).
11
+ *
12
+ * Lifecycle boundaries (ChildPiLineObserver.flush, runChildPi settle) must
13
+ * call flushPendingTranscriptWrites() so callers that immediately read the
14
+ * transcript file see complete content.
15
+ *
16
+ * Ordering: lines are appended in call order. The flush writes the joined
17
+ * array preserving intra-batch order. Inter-batch order is not guaranteed
18
+ * but transcript is append-only telemetry.
19
+ */
20
+
21
+ import * as fs from "node:fs";
22
+ import { DEFAULT_CHILD_PI } from "../config/defaults.ts";
23
+ import { logInternalError } from "../utils/internal-error.ts";
24
+ import { redactJsonLine } from "../utils/redaction.ts";
25
+ import { resolveRealContainedPath } from "../utils/safe-paths.ts";
26
+ import type { ChildPiRunInput } from "./child-pi.ts";
27
+ import { applyCompactPipeline } from "./compact-pipeline.ts";
28
+ import { TruncationStage } from "./compact-stages/truncation-stage.ts";
29
+
30
+ // ── Transcript batch buffer (OPT-PHASE3) ────────────────────────────────
31
+ // Instead of open/write/close per line (3 syscalls × N), accumulate lines
32
+ // in a module-scoped buffer and flush them in one open/write/close per path
33
+ // every TRANSCRIPT_FLUSH_MS. Lifecycle boundaries (observer.flush, settle)
34
+ // force-flush the buffer before returning so transcript reads are complete.
35
+ const transcriptBatches = new Map<string, string[]>();
36
+ let transcriptFlushTimer: ReturnType<typeof setTimeout> | undefined;
37
+ const TRANSCRIPT_FLUSH_MS = 50;
38
+
39
+ export function appendTranscript(input: ChildPiRunInput, line: string): void {
40
+ if (!input.transcriptPath) return;
41
+ // SECURITY FIX (Issue #1): Validate transcriptPath against artifactsRoot to prevent
42
+ // arbitrary file writes and symlink traversal attacks. An attacker who can influence
43
+ // the task graph could set transcriptPath to /etc/passwd or similar, and mkdirSync
44
+ // with recursive:true would create parent directories. Additionally, appendFileSync
45
+ // follows symlinks, potentially writing to sensitive files.
46
+ let safePath: string;
47
+ try {
48
+ const artifactsRoot = input.artifactsRoot ?? input.cwd;
49
+ safePath = resolveRealContainedPath(artifactsRoot, input.transcriptPath);
50
+ } catch (error) {
51
+ logInternalError("child-pi.transcript-path-rejected", error as Error, `transcriptPath=${input.transcriptPath}`);
52
+ return;
53
+ }
54
+ trackTranscriptWrite(safePath, line);
55
+ }
56
+
57
+ function scheduleTranscriptFlush(): void {
58
+ if (transcriptFlushTimer) return;
59
+ transcriptFlushTimer = setTimeout(() => {
60
+ transcriptFlushTimer = undefined;
61
+ void flushTranscriptBatches();
62
+ }, TRANSCRIPT_FLUSH_MS);
63
+ transcriptFlushTimer.unref?.();
64
+ }
65
+
66
+ async function flushTranscriptBatches(): Promise<void> {
67
+ const entries = [...transcriptBatches.entries()];
68
+ transcriptBatches.clear();
69
+ await Promise.allSettled(
70
+ entries.map(async ([safePath, lines]) => {
71
+ if (lines.length === 0) return;
72
+ const content = lines.join("");
73
+ try {
74
+ const fd = await fs.promises.open(
75
+ safePath,
76
+ fs.constants.O_WRONLY | fs.constants.O_NOFOLLOW | fs.constants.O_CREAT | fs.constants.O_APPEND,
77
+ 0o600,
78
+ );
79
+ try {
80
+ await fd.write(content, undefined, "utf-8");
81
+ } finally {
82
+ await fd.close();
83
+ }
84
+ } catch (error) {
85
+ logInternalError("child-pi.transcript-write-failed", error as Error, `path=${safePath}`);
86
+ }
87
+ }),
88
+ );
89
+ }
90
+
91
+ function trackTranscriptWrite(safePath: string, line: string): void {
92
+ const content = `${redactJsonLine(line)}\n`;
93
+ let batch = transcriptBatches.get(safePath);
94
+ if (!batch) {
95
+ batch = [];
96
+ transcriptBatches.set(safePath, batch);
97
+ }
98
+ batch.push(content);
99
+ scheduleTranscriptFlush();
100
+ }
101
+
102
+ /**
103
+ * Drain the transcript batch buffer and await any remaining in-flight writes.
104
+ * Called by lifecycle boundaries (ChildPiLineObserver.flush, runChildPi settle)
105
+ * so that transcript files are complete before callers read them.
106
+ *
107
+ * Uses a while loop to re-check the buffer after each flush — new lines may
108
+ * arrive during the async I/O window (trackTranscriptWrite → scheduleTranscriptFlush).
109
+ */
110
+ export async function flushPendingTranscriptWrites(): Promise<void> {
111
+ if (transcriptFlushTimer) {
112
+ clearTimeout(transcriptFlushTimer);
113
+ transcriptFlushTimer = undefined;
114
+ }
115
+ while (transcriptBatches.size > 0) {
116
+ await flushTranscriptBatches();
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Reset the module-scoped transcript batch state. Exported for test isolation
122
+ * only — production code should never call this.
123
+ */
124
+ export function resetTranscriptBatchState(): void {
125
+ if (transcriptFlushTimer) {
126
+ clearTimeout(transcriptFlushTimer);
127
+ transcriptFlushTimer = undefined;
128
+ }
129
+ transcriptBatches.clear();
130
+ }
131
+
132
+ // ── Compaction helpers (moved from child-pi.ts; these don't need batching) ──
133
+
134
+ export function compactString(
135
+ value: string,
136
+ maxChars = DEFAULT_CHILD_PI.maxCompactContentChars,
137
+ opts: { preserveImportant?: boolean } = {},
138
+ ): string {
139
+ if (value.length <= maxChars) return value;
140
+ const result = applyCompactPipeline(value, [
141
+ new TruncationStage(maxChars, {
142
+ preserveImportant: opts.preserveImportant,
143
+ }),
144
+ ]);
145
+ return result.text;
146
+ }
147
+
148
+ export function compactValue(value: unknown): unknown {
149
+ if (typeof value === "string") return compactString(value);
150
+ if (Array.isArray(value)) {
151
+ // BUG-4: silent .slice(0, 20) lost items 21-50 with no marker.
152
+ if (value.length > 20) {
153
+ return [...value.slice(0, 20).map(compactValue), `[pi-crew truncated ${value.length - 20} entries]`];
154
+ }
155
+ return value.map(compactValue);
156
+ }
157
+ const record = asRecord(value);
158
+ if (!record) return value;
159
+ const entries = Object.entries(record);
160
+ const compacted: Record<string, unknown> = {};
161
+ for (const [key, entry] of entries.slice(0, 20)) compacted[key] = compactValue(entry);
162
+ if (entries.length > 20) compacted["[truncated]"] = `${entries.length - 20} entries`;
163
+ return compacted;
164
+ }
165
+
166
+ function asRecord(value: unknown): Record<string, unknown> | undefined {
167
+ if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
168
+ return value as Record<string, unknown>;
169
+ }