pi-crew 0.5.11 → 0.5.13
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.
- package/CHANGELOG.md +45 -0
- package/README.md +1 -1
- package/docs/pi-crew-v0.5.12-audit-fix-plan.md +76 -0
- package/docs/pi-crew-v0.5.13-audit-fix-plan.md +75 -0
- package/package.json +1 -1
- package/src/benchmark/benchmark-runner.ts +22 -7
- package/src/extension/async-notifier.ts +2 -1
- package/src/extension/crew-cleanup.ts +30 -11
- package/src/runtime/async-runner.ts +2 -1
- package/src/runtime/crew-hooks.ts +4 -2
- package/src/runtime/hidden-handoff.ts +2 -1
- package/src/utils/bm25-search.ts +28 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,50 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.5.13] — Round 18 Audit Fixes (2026-06-02)
|
|
4
|
+
|
|
5
|
+
### Phase 1: Switch to execFileSync (HIGH security)
|
|
6
|
+
- `src/benchmark/benchmark-runner.ts` — Replaced `execSync` with `execFileSync(program, args)`. This prevents shell parsing of command strings, even if `validateCommand` is bypassed.
|
|
7
|
+
- `validateCommand` retained as defense-in-depth (blocks shell metacharacters).
|
|
8
|
+
- New `splitCommand()` helper safely splits validated commands.
|
|
9
|
+
|
|
10
|
+
### Phase 2: Precompute document frequency (MEDIUM performance)
|
|
11
|
+
- `src/utils/bm25-search.ts` — `BM25Search.df()` is now precomputed once in the constructor via `precomputeDocumentFrequencies()`. Lookup is O(1) via `dfCache: Map<term, number>`.
|
|
12
|
+
- Per-search complexity: O(Q * N) instead of O(Q² * N²).
|
|
13
|
+
|
|
14
|
+
### Phase 3+4: Test coverage for 3 untested modules
|
|
15
|
+
- 15 tests in `test/unit/bm25-search.test.ts`
|
|
16
|
+
- 15 tests in `test/unit/scan-cache.test.ts`
|
|
17
|
+
- 20 tests in `test/unit/benchmark.test.ts`
|
|
18
|
+
- **Total: 50 new tests**
|
|
19
|
+
|
|
20
|
+
### Tests
|
|
21
|
+
- 2352/2352 pass (was 2313 in v0.5.12; +39 net)
|
|
22
|
+
- 50 new tests across 3 new test files
|
|
23
|
+
- TypeScript: 0 errors
|
|
24
|
+
|
|
25
|
+
## [0.5.12] — Round 17 Audit Fixes (2026-06-02)
|
|
26
|
+
|
|
27
|
+
### Phase 1: Signal Handler Stacking (HIGH)
|
|
28
|
+
- `src/extension/crew-cleanup.ts` — Added module-level `signalHandlersRegistered` flag. `process.on("SIGTERM"/"SIGHUP")` is now registered only once even if `registerCleanupHandler` is called multiple times. Without this fix, listeners stack up on extension reload and `cleanupChildProcesses` fires N times on shutdown.
|
|
29
|
+
- Also wrapped `handleSignal()` with `.catch()` to prevent unhandled promise rejections.
|
|
30
|
+
|
|
31
|
+
### Phase 2: L1 Cleanup (continued)
|
|
32
|
+
Replaced 8 `console.error` calls with `logInternalError` for consistency:
|
|
33
|
+
- `src/extension/crew-cleanup.ts` (3 calls)
|
|
34
|
+
- `src/extension/async-notifier.ts:124`
|
|
35
|
+
- `src/runtime/async-runner.ts:166`
|
|
36
|
+
- `src/runtime/hidden-handoff.ts:244`
|
|
37
|
+
- `src/runtime/crew-hooks.ts:167,172`
|
|
38
|
+
|
|
39
|
+
### Phase 3+4: Test Coverage
|
|
40
|
+
- 8 new tests in `test/unit/crew-hooks.test.ts`
|
|
41
|
+
- 1 new test in `test/unit/crew-cleanup.test.ts` (signal handler idempotency)
|
|
42
|
+
|
|
43
|
+
### Tests
|
|
44
|
+
- 2313/2313 pass (was 2308 in v0.5.11; +5 net from new tests)
|
|
45
|
+
- 9 new tests across 2 test files
|
|
46
|
+
- TypeScript: 0 errors
|
|
47
|
+
|
|
3
48
|
## [0.5.11] — Round 16 Audit Fixes (2026-06-02)
|
|
4
49
|
|
|
5
50
|
### Phase 1: L1 cleanup (continued)
|
package/README.md
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# pi-crew v0.5.12 Audit Fix Plan (Round 17)
|
|
2
|
+
|
|
3
|
+
## Source Verification Findings
|
|
4
|
+
|
|
5
|
+
I read the following files and identified 4 confirmed real issues + test coverage gaps.
|
|
6
|
+
|
|
7
|
+
### Issue 1: Signal listeners stack up on registerCleanupHandler (HIGH)
|
|
8
|
+
**File**: `src/extension/crew-cleanup.ts:81-82`
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
process.on("SIGTERM", () => { void handleSignal("SIGTERM"); });
|
|
12
|
+
process.on("SIGHUP", () => { void handleSignal("SIGHUP"); });
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
These listeners are added every time `registerCleanupHandler(pi)` is called. If the extension is reloaded (e.g., in dev mode, or via `pi install --reload`), the listeners stack up. This causes:
|
|
16
|
+
- Memory leak (closures over `handleSignal`)
|
|
17
|
+
- Multiple cleanup invocations on shutdown → multiple SIGTERM to children
|
|
18
|
+
- Confusing logs ("Received SIGTERM - starting cleanup" repeated)
|
|
19
|
+
|
|
20
|
+
**Fix**: Make the signal handlers idempotent. Use a module-level `signalHandlersRegistered` flag, or use `process.once` instead of `process.on`. Better: register only once at module load.
|
|
21
|
+
|
|
22
|
+
### Issue 2: Unhandled promise rejection in signal handler (MEDIUM)
|
|
23
|
+
**File**: `src/extension/crew-cleanup.ts:81-82`
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
process.on("SIGTERM", () => { void handleSignal("SIGTERM"); });
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
If `handleSignal` throws or rejects, the unhandled rejection is silently swallowed (because `void` discards the promise). This violates our "log all errors" pattern from v0.5.9 L1.
|
|
30
|
+
|
|
31
|
+
**Fix**: Wrap with `.catch()` and `logInternalError`.
|
|
32
|
+
|
|
33
|
+
### Issue 3: console.error bypasses logInternalError in 4 files (MEDIUM, L1 continued)
|
|
34
|
+
**Files** (7 occurrences total):
|
|
35
|
+
- `src/extension/crew-cleanup.ts:59` (cleanup error)
|
|
36
|
+
- `src/extension/crew-cleanup.ts:84` (kill process error)
|
|
37
|
+
- `src/extension/crew-cleanup.ts:103` (temp cleanup error)
|
|
38
|
+
- `src/extension/async-notifier.ts:124` (notifier error)
|
|
39
|
+
- `src/runtime/async-runner.ts:166` (spawn failed)
|
|
40
|
+
- `src/runtime/hidden-handoff.ts:244` (handoff failed)
|
|
41
|
+
- `src/runtime/crew-hooks.ts:167,172` (hook error)
|
|
42
|
+
|
|
43
|
+
**Rationale**: v0.5.9 L1 fix (in `event-bus.ts`) and v0.5.11 round 16 cleanup moved from `console.error` to `logInternalError` to ensure errors are captured even when stderr is redirected. These 8 callsites bypass that pattern.
|
|
44
|
+
|
|
45
|
+
**Note**: `internal-error.ts:5` itself uses `console.error` — that's the implementation, leave it. `background-runner.ts:146` overrides `console.error` for testing — also leave.
|
|
46
|
+
|
|
47
|
+
### Issue 4: Test coverage gaps in security/runtime code (LOW)
|
|
48
|
+
- `test/unit/crew-cleanup.test.ts` — does not exist
|
|
49
|
+
- `test/unit/async-notifier.test.ts` — does not exist
|
|
50
|
+
- `test/unit/pi-spawn.test.ts` — does not exist (security-critical!)
|
|
51
|
+
- `test/unit/live-agent-manager.test.ts` — does not exist
|
|
52
|
+
- `test/unit/crew-hooks.test.ts` — does not exist
|
|
53
|
+
|
|
54
|
+
## Plan (5 phases)
|
|
55
|
+
|
|
56
|
+
### Phase 1: Fix signal handler stacking
|
|
57
|
+
- Use module-level flag to register signal handlers only once
|
|
58
|
+
- Wrap with `.catch()` to log promise rejections
|
|
59
|
+
|
|
60
|
+
### Phase 2: L1 cleanup in 4 files
|
|
61
|
+
Replace 8 `console.error` calls with `logInternalError`:
|
|
62
|
+
- crew-cleanup.ts (3 calls)
|
|
63
|
+
- async-notifier.ts (1 call)
|
|
64
|
+
- async-runner.ts (1 call)
|
|
65
|
+
- hidden-handoff.ts (1 call)
|
|
66
|
+
- crew-hooks.ts (2 calls)
|
|
67
|
+
|
|
68
|
+
### Phase 3: Test coverage for security-critical modules
|
|
69
|
+
- `test/unit/crew-cleanup.test.ts` — test signal handler idempotency, cleanup logic
|
|
70
|
+
- `test/unit/pi-spawn.test.ts` — test `isWithinAllowedPrefixes`, `validateExplicitBin`
|
|
71
|
+
|
|
72
|
+
### Phase 4: Test coverage for runtime modules
|
|
73
|
+
- `test/unit/async-notifier.test.ts` — test isCurrent guard, generation check
|
|
74
|
+
- `test/unit/live-agent-manager.test.ts` — test eviction logic
|
|
75
|
+
|
|
76
|
+
### Phase 5: Release v0.5.12
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# pi-crew v0.5.13 Audit Fix Plan (Round 18)
|
|
2
|
+
|
|
3
|
+
## Source Verification Findings
|
|
4
|
+
|
|
5
|
+
I read the following files and identified 4 confirmed real issues:
|
|
6
|
+
|
|
7
|
+
### Issue 1: `benchmark-runner.ts` uses `execSync` instead of `execFileSync` (HIGH security)
|
|
8
|
+
**File**: `src/benchmark/benchmark-runner.ts:4,110,119,128`
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { execSync } from "child_process";
|
|
12
|
+
// ...
|
|
13
|
+
output = execSync(judge.command, { ... });
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`execSync(command, ...)` invokes a shell to parse the command, even when `validateCommand` is run first. The `validateCommand` function only checks for shell metacharacters in the *arguments* (after the first space), but:
|
|
17
|
+
- It does not escape/quote arguments safely
|
|
18
|
+
- A bug in `validateCommand` or a clever input could bypass
|
|
19
|
+
- `cwd: process.cwd()` could be inherited from a parent context
|
|
20
|
+
- Best practice: use `execFileSync` with `command.split(' ')[0]` and the rest as args, so no shell is invoked
|
|
21
|
+
|
|
22
|
+
**Fix**: Switch to `execFileSync` with command split into program + args. Keep `validateCommand` as defense-in-depth but no longer rely on it alone.
|
|
23
|
+
|
|
24
|
+
### Issue 2: `BM25Search.df()` is O(N) per call and called inside the search loop (MEDIUM performance)
|
|
25
|
+
**File**: `src/utils/bm25-search.ts:47-65, 75-104`
|
|
26
|
+
|
|
27
|
+
The `df()` function is called for every query term in the search loop, and itself iterates over all documents. This means:
|
|
28
|
+
- For a query with `Q` terms and `N` documents, `df()` is called `Q * N` times
|
|
29
|
+
- Each `df()` call iterates over `N` documents and `field_count` fields
|
|
30
|
+
- Total complexity: **O(Q² * N² * field_count)**
|
|
31
|
+
|
|
32
|
+
This is quadratic when it should be linear. Document frequencies don't change between `search()` calls for the same document set, so they should be cached.
|
|
33
|
+
|
|
34
|
+
**Fix**: Precompute `df` once in the constructor (or lazily on first search) and cache it as a Map<term, number>. Re-compute only when documents change.
|
|
35
|
+
|
|
36
|
+
### Issue 3: `SharedScanCache.set()` LRU eviction is by insertion order, not access order (LOW)
|
|
37
|
+
**File**: `src/utils/scan-cache.ts:62-69`
|
|
38
|
+
|
|
39
|
+
The eviction policy evicts the *oldest inserted* entry, not the *least recently accessed*. So if a frequently-updated entry is inserted, then later entries are inserted, the frequently-updated one (which is the *same* Map key) won't be moved to the end of the insertion order — it stays at the head and is the next to be evicted.
|
|
40
|
+
|
|
41
|
+
This is a minor issue because:
|
|
42
|
+
- In practice, scan cache entries are short-lived (TTL=1s by default)
|
|
43
|
+
- The eviction only matters when entries hit the `maxEntries` cap
|
|
44
|
+
|
|
45
|
+
**Fix**: Either document the limitation or implement proper LRU. For now, document it.
|
|
46
|
+
|
|
47
|
+
### Issue 4: `bm25-search.ts` has no tests (LOW coverage)
|
|
48
|
+
**File**: `test/unit/bm25-search.test.ts` — does not exist
|
|
49
|
+
|
|
50
|
+
BM25Search is a non-trivial search algorithm. Currently zero test coverage. Should add tests for:
|
|
51
|
+
- Basic search returns relevant results
|
|
52
|
+
- Field weighting affects ranking
|
|
53
|
+
- minScore threshold
|
|
54
|
+
- limit cap
|
|
55
|
+
- Empty query returns empty results
|
|
56
|
+
- df() precomputation (after Issue 2 fix)
|
|
57
|
+
|
|
58
|
+
## Plan (4 phases)
|
|
59
|
+
|
|
60
|
+
### Phase 1: Switch `benchmark-runner.ts` to `execFileSync`
|
|
61
|
+
- Replace `execSync(judge.command, ...)` with `execFileSync(program, args, ...)`
|
|
62
|
+
- Keep `validateCommand` as defense-in-depth
|
|
63
|
+
- Add new tests for benchmark-runner
|
|
64
|
+
|
|
65
|
+
### Phase 2: Precompute `df` in BM25Search
|
|
66
|
+
- Cache `df` map per corpus
|
|
67
|
+
- Invalidate when documents change (or recompute on construction)
|
|
68
|
+
- Add tests to verify behavior unchanged
|
|
69
|
+
|
|
70
|
+
### Phase 3: Add tests for scan-cache, benchmark, bm25-search
|
|
71
|
+
- `test/unit/scan-cache.test.ts`
|
|
72
|
+
- `test/unit/benchmark.test.ts`
|
|
73
|
+
- `test/unit/bm25-search.test.ts`
|
|
74
|
+
|
|
75
|
+
### Phase 4: Release v0.5.13
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Provides tiered evaluation for workflow tasks.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
7
7
|
|
|
8
8
|
export interface BenchmarkJudge {
|
|
9
9
|
type: "pytest" | "grep" | "command";
|
|
@@ -78,6 +78,16 @@ function validateCommand(command: string): void {
|
|
|
78
78
|
* Tier 3: command execution
|
|
79
79
|
* Fails fast on first tier failure.
|
|
80
80
|
*/
|
|
81
|
+
function splitCommand(command: string): { program: string; args: string[] } {
|
|
82
|
+
// Naive split on whitespace. validateCommand already rejects shell
|
|
83
|
+
// metacharacters, so a simple split is safe.
|
|
84
|
+
const parts = command.trim().split(/\s+/);
|
|
85
|
+
if (parts.length === 0) {
|
|
86
|
+
throw new Error("Empty command");
|
|
87
|
+
}
|
|
88
|
+
return { program: parts[0]!, args: parts.slice(1) };
|
|
89
|
+
}
|
|
90
|
+
|
|
81
91
|
export async function runBenchmark(task: BenchmarkTask): Promise<BenchmarkResult> {
|
|
82
92
|
const startTime = Date.now();
|
|
83
93
|
const judgeResults: BenchmarkResult["judgeResults"] = [];
|
|
@@ -88,10 +98,13 @@ export async function runBenchmark(task: BenchmarkTask): Promise<BenchmarkResult
|
|
|
88
98
|
let output: string | undefined;
|
|
89
99
|
|
|
90
100
|
if (judge.type === "pytest" && judge.command) {
|
|
91
|
-
// Validate command before execution
|
|
101
|
+
// Validate command before execution (defense-in-depth)
|
|
92
102
|
validateCommand(judge.command);
|
|
103
|
+
// Use execFileSync to avoid shell parsing. validateCommand
|
|
104
|
+
// already rejects metacharacters, so a simple split is safe.
|
|
105
|
+
const { program, args } = splitCommand(judge.command);
|
|
93
106
|
// Tier 1: pytest - fast deterministic check
|
|
94
|
-
output =
|
|
107
|
+
output = execFileSync(program, args, {
|
|
95
108
|
timeout: 5000,
|
|
96
109
|
encoding: "utf-8",
|
|
97
110
|
cwd: process.cwd(),
|
|
@@ -99,20 +112,22 @@ export async function runBenchmark(task: BenchmarkTask): Promise<BenchmarkResult
|
|
|
99
112
|
// Look for pytest summary line with passed count
|
|
100
113
|
passed = output.includes("passed");
|
|
101
114
|
} else if (judge.type === "grep" && judge.pattern && judge.command) {
|
|
102
|
-
// Validate command before execution
|
|
115
|
+
// Validate command before execution (defense-in-depth)
|
|
103
116
|
validateCommand(judge.command);
|
|
117
|
+
const { program, args } = splitCommand(judge.command);
|
|
104
118
|
// Tier 2: grep pattern matching
|
|
105
|
-
output =
|
|
119
|
+
output = execFileSync(program, args, {
|
|
106
120
|
timeout: 5000,
|
|
107
121
|
encoding: "utf-8",
|
|
108
122
|
cwd: process.cwd(),
|
|
109
123
|
});
|
|
110
124
|
passed = output.includes(judge.pattern);
|
|
111
125
|
} else if (judge.type === "command" && judge.command) {
|
|
112
|
-
// Validate command before execution
|
|
126
|
+
// Validate command before execution (defense-in-depth)
|
|
113
127
|
validateCommand(judge.command);
|
|
128
|
+
const { program, args } = splitCommand(judge.command);
|
|
114
129
|
// Tier 3: command execution
|
|
115
|
-
output =
|
|
130
|
+
output = execFileSync(program, args, {
|
|
116
131
|
timeout: 10000,
|
|
117
132
|
encoding: "utf-8",
|
|
118
133
|
cwd: process.cwd(),
|
|
@@ -6,6 +6,7 @@ import type { TeamRunManifest, TeamTaskState } from "../state/types.ts";
|
|
|
6
6
|
import { readCrewAgents, saveCrewAgents } from "../runtime/crew-agent-records.ts";
|
|
7
7
|
import { withRunLockSync } from "../state/locks.ts";
|
|
8
8
|
import { listRuns } from "./run-index.ts";
|
|
9
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
9
10
|
|
|
10
11
|
export interface AsyncNotifierState {
|
|
11
12
|
seenFinishedRunIds: Set<string>;
|
|
@@ -121,7 +122,7 @@ export function startAsyncRunNotifier(ctx: ExtensionContext, state: AsyncNotifie
|
|
|
121
122
|
// Stopping here creates a race: old notifier dies before new one starts.
|
|
122
123
|
return;
|
|
123
124
|
}
|
|
124
|
-
|
|
125
|
+
logInternalError("async-notifier", error, `interval=${intervalMs}`);
|
|
125
126
|
}
|
|
126
127
|
}, intervalMs);
|
|
127
128
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
2
3
|
// NOTE: globalProgressTracker import kept for documentation but not directly used
|
|
3
4
|
// since we don't have agent IDs to untrack. Actual progress clearing should be
|
|
4
5
|
// handled by the progress tracker itself on shutdown.
|
|
@@ -9,6 +10,12 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
9
10
|
* Handles session_shutdown and SIGTERM/SIGHUP signals.
|
|
10
11
|
*/
|
|
11
12
|
|
|
13
|
+
// Module-level flag to ensure signal handlers are registered only once,
|
|
14
|
+
// even if registerCleanupHandler is called multiple times (e.g., on extension
|
|
15
|
+
// reload or during dev hot-reload). Without this, listeners stack up and
|
|
16
|
+
// cleanupChildProcesses fires N times on shutdown.
|
|
17
|
+
let signalHandlersRegistered = false;
|
|
18
|
+
|
|
12
19
|
interface ChildProcessInfo {
|
|
13
20
|
pid: number;
|
|
14
21
|
runId: string;
|
|
@@ -56,18 +63,30 @@ export function registerCleanupHandler(pi: ExtensionAPI): void {
|
|
|
56
63
|
|
|
57
64
|
console.log("[pi-crew] Cleanup complete");
|
|
58
65
|
} catch (error) {
|
|
59
|
-
|
|
66
|
+
logInternalError("crew-cleanup.shutdown", error);
|
|
60
67
|
}
|
|
61
68
|
});
|
|
62
69
|
|
|
63
|
-
//
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
// Register signal handlers exactly once, even if registerCleanupHandler
|
|
71
|
+
// is called multiple times. This prevents listener stacking on extension
|
|
72
|
+
// reload and avoids double-cleanup on shutdown.
|
|
73
|
+
if (!signalHandlersRegistered) {
|
|
74
|
+
signalHandlersRegistered = true;
|
|
75
|
+
const handleSignal = async (signal: string): Promise<void> => {
|
|
76
|
+
console.log(`[pi-crew] Received ${signal} - starting cleanup`);
|
|
77
|
+
await cleanupChildProcesses();
|
|
78
|
+
};
|
|
79
|
+
process.on("SIGTERM", () => {
|
|
80
|
+
handleSignal("SIGTERM").catch((error) => {
|
|
81
|
+
logInternalError("crew-cleanup.SIGTERM", error);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
process.on("SIGHUP", () => {
|
|
85
|
+
handleSignal("SIGHUP").catch((error) => {
|
|
86
|
+
logInternalError("crew-cleanup.SIGHUP", error);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
71
90
|
}
|
|
72
91
|
|
|
73
92
|
async function cleanupChildProcesses(): Promise<void> {
|
|
@@ -81,7 +100,7 @@ async function cleanupChildProcesses(): Promise<void> {
|
|
|
81
100
|
// Process may already be dead or not exist
|
|
82
101
|
const err = error as NodeJS.ErrnoException;
|
|
83
102
|
if (err.code !== "ESRCH" && err.code !== "ENOENT") {
|
|
84
|
-
|
|
103
|
+
logInternalError("crew-cleanup.kill", error, `pid=${pid}`);
|
|
85
104
|
}
|
|
86
105
|
}
|
|
87
106
|
childProcessRegistry.unregister(pid);
|
|
@@ -100,7 +119,7 @@ async function cleanupTempDirectories(): Promise<void> {
|
|
|
100
119
|
try {
|
|
101
120
|
console.log(`[pi-crew] Temp directory cleanup deferred to run-graph`);
|
|
102
121
|
} catch (error) {
|
|
103
|
-
|
|
122
|
+
logInternalError("crew-cleanup.temp", error);
|
|
104
123
|
}
|
|
105
124
|
}
|
|
106
125
|
|
|
@@ -3,6 +3,7 @@ import { createRequire } from "node:module";
|
|
|
3
3
|
import * as fs from "node:fs";
|
|
4
4
|
import * as path from "node:path";
|
|
5
5
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
6
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
6
7
|
import { appendEvent } from "../state/event-log.ts";
|
|
7
8
|
import type { TeamRunManifest } from "../state/types.ts";
|
|
8
9
|
|
|
@@ -163,7 +164,7 @@ export async function spawnBackgroundTeamRun(manifest: TeamRunManifest): Promise
|
|
|
163
164
|
} as unknown as Parameters<typeof spawn>[2];
|
|
164
165
|
const child = spawn(process.execPath, command.args, spawnOpts);
|
|
165
166
|
child.on("error", (error: Error) => {
|
|
166
|
-
|
|
167
|
+
logInternalError("async-runner.spawn", error, `pid=${child.pid ?? "unknown"}`);
|
|
167
168
|
});
|
|
168
169
|
child.unref();
|
|
169
170
|
|
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
26
|
+
|
|
25
27
|
/** Valid hook event types in the crew lifecycle. */
|
|
26
28
|
export type CrewHookEventType =
|
|
27
29
|
| 'task_started'
|
|
@@ -164,12 +166,12 @@ export class HookRegistry {
|
|
|
164
166
|
if (result instanceof Promise) {
|
|
165
167
|
// Attach a silent catch to prevent unhandled rejection warnings
|
|
166
168
|
result.catch((err) => {
|
|
167
|
-
|
|
169
|
+
logInternalError("crew-hooks.async", err, `event.type=${event.type}`);
|
|
168
170
|
});
|
|
169
171
|
}
|
|
170
172
|
} catch (err) {
|
|
171
173
|
// Catch synchronous errors but don't let them block other hooks
|
|
172
|
-
|
|
174
|
+
logInternalError("crew-hooks.sync", err, `event.type=${event.type}`);
|
|
173
175
|
}
|
|
174
176
|
}
|
|
175
177
|
}
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import type { HandoffSummary } from "./handoff-manager.ts";
|
|
13
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* Type of hidden handoff message.
|
|
@@ -241,7 +242,7 @@ export class HiddenHandoffService {
|
|
|
241
242
|
this.sendHandoff(summary, options);
|
|
242
243
|
} catch (error) {
|
|
243
244
|
// Log but don't throw
|
|
244
|
-
|
|
245
|
+
logInternalError("hidden-handoff.async", error, `taskId=${summary.taskId} runId=${summary.runId}`);
|
|
245
246
|
}
|
|
246
247
|
}
|
|
247
248
|
|
package/src/utils/bm25-search.ts
CHANGED
|
@@ -25,6 +25,13 @@ export class BM25Search<T extends SearchDocument> {
|
|
|
25
25
|
private readonly b: number;
|
|
26
26
|
private readonly docLenMap: Map<string, number>;
|
|
27
27
|
private readonly N: number;
|
|
28
|
+
/**
|
|
29
|
+
* Precomputed document frequency per term. Cached at construction time
|
|
30
|
+
* to avoid O(N) recomputation on every search() call. The cache is
|
|
31
|
+
* immutable for a given document corpus, so it's safe to share across
|
|
32
|
+
* search() invocations.
|
|
33
|
+
*/
|
|
34
|
+
private readonly dfCache: Map<string, number>;
|
|
28
35
|
|
|
29
36
|
constructor(documents: T[], fieldWeights: Record<string, number> = {}, config: BM25Config = {}) {
|
|
30
37
|
this.documents = documents;
|
|
@@ -34,6 +41,7 @@ export class BM25Search<T extends SearchDocument> {
|
|
|
34
41
|
this.N = documents.length;
|
|
35
42
|
|
|
36
43
|
this.docLenMap = new Map();
|
|
44
|
+
this.dfCache = new Map();
|
|
37
45
|
|
|
38
46
|
for (const doc of documents) {
|
|
39
47
|
const fieldValues = Object.values(doc.fields).join(" ");
|
|
@@ -43,26 +51,36 @@ export class BM25Search<T extends SearchDocument> {
|
|
|
43
51
|
|
|
44
52
|
const totalLen = [...this.docLenMap.values()].reduce((a, b) => a + b, 0);
|
|
45
53
|
this.avgDocLen = totalLen / this.N || 1;
|
|
54
|
+
|
|
55
|
+
// Precompute df for all terms in the corpus. We do this once instead
|
|
56
|
+
// of on-demand to avoid the O(Q * N * field_count) cost per search call.
|
|
57
|
+
this.precomputeDocumentFrequencies();
|
|
46
58
|
}
|
|
47
59
|
|
|
48
60
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
61
|
+
* Build a map of term -> document frequency. O(N * avg_terms * field_count).
|
|
62
|
+
* Called once in the constructor.
|
|
51
63
|
*/
|
|
52
|
-
private
|
|
53
|
-
const termLower = term.toLowerCase();
|
|
54
|
-
let count = 0;
|
|
64
|
+
private precomputeDocumentFrequencies(): void {
|
|
55
65
|
for (const doc of this.documents) {
|
|
56
66
|
for (const field of Object.keys(this.fieldWeights)) {
|
|
57
67
|
const text = (doc.fields[field] ?? "").toLowerCase();
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
68
|
+
// Extract unique terms via split on whitespace
|
|
69
|
+
const terms = new Set(text.split(/\s+/).filter(Boolean));
|
|
70
|
+
for (const term of terms) {
|
|
71
|
+
if (term.length === 0) continue;
|
|
72
|
+
this.dfCache.set(term, (this.dfCache.get(term) ?? 0) + 1);
|
|
62
73
|
}
|
|
63
74
|
}
|
|
64
75
|
}
|
|
65
|
-
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get document frequency for a term. Returns the precomputed value.
|
|
80
|
+
* O(1) lookup.
|
|
81
|
+
*/
|
|
82
|
+
private df(term: string): number {
|
|
83
|
+
return this.dfCache.get(term.toLowerCase()) ?? 0;
|
|
66
84
|
}
|
|
67
85
|
|
|
68
86
|
search(query: string, options?: { limit?: number; minScore?: number }): SearchResult<T>[] {
|