pi-background-tasks 0.7.3 → 0.7.4
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/PUBLISHING.md +10 -7
- package/README.md +78 -6
- package/TESTING.md +13 -3
- package/TEST_PLAN.md +10 -5
- package/package.json +10 -8
- package/src/core/attested-pi-run.ts +19 -35
- package/src/core/common.ts +124 -8
- package/src/core/durable-fs.ts +400 -0
- package/src/core/fusion/artifacts.ts +20 -36
- package/src/core/fusion/budget.ts +372 -0
- package/src/core/fusion/config.ts +4 -36
- package/src/core/fusion/context.ts +508 -47
- package/src/core/fusion/orchestrator.ts +41 -4
- package/src/core/fusion/pi-child.ts +28 -6
- package/src/core/fusion/prompts.ts +21 -7
- package/src/core/fusion/types.ts +258 -4
- package/src/core/pi-launch.ts +225 -0
- package/src/core/registry.ts +507 -56
- package/src/core/windows-taskkill.ts +250 -0
- package/src/fusion-extension.ts +4 -1
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { spawn as nodeSpawn, type SpawnOptions } from 'node:child_process';
|
|
2
|
+
import { win32 } from 'node:path';
|
|
3
|
+
|
|
4
|
+
export type WindowsKillPhase = 'terminate' | 'force';
|
|
5
|
+
|
|
6
|
+
export interface TaskkillOutcome {
|
|
7
|
+
readonly exitCode: number | null;
|
|
8
|
+
readonly signal: NodeJS.Signals | null;
|
|
9
|
+
readonly stdout: string;
|
|
10
|
+
readonly stderr: string;
|
|
11
|
+
readonly stdoutTruncated: boolean;
|
|
12
|
+
readonly stderrTruncated: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface TaskkillOutputStream {
|
|
16
|
+
on(event: 'data', listener: (data: Buffer | string) => void): unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface WindowsTaskkillChildProcess {
|
|
20
|
+
readonly stdout?: TaskkillOutputStream | null | undefined;
|
|
21
|
+
readonly stderr?: TaskkillOutputStream | null | undefined;
|
|
22
|
+
kill(signal?: NodeJS.Signals): boolean;
|
|
23
|
+
on(event: 'error', listener: (error: Error) => void): unknown;
|
|
24
|
+
on(
|
|
25
|
+
event: 'close',
|
|
26
|
+
listener: (code: number | null, signal: NodeJS.Signals | null) => void,
|
|
27
|
+
): unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type WindowsTaskkillSpawn = (
|
|
31
|
+
command: string,
|
|
32
|
+
args: string[],
|
|
33
|
+
options: SpawnOptions,
|
|
34
|
+
) => WindowsTaskkillChildProcess;
|
|
35
|
+
|
|
36
|
+
export interface WindowsTaskkillOptions {
|
|
37
|
+
readonly spawn?: WindowsTaskkillSpawn;
|
|
38
|
+
readonly env?: NodeJS.ProcessEnv;
|
|
39
|
+
readonly timeoutMs?: number;
|
|
40
|
+
readonly signal?: AbortSignal;
|
|
41
|
+
readonly maxCaptureBytes?: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const DEFAULT_TIMEOUT_MS = 5000;
|
|
45
|
+
const DEFAULT_MAX_CAPTURE_BYTES = 8 * 1024;
|
|
46
|
+
|
|
47
|
+
class BoundedCapture {
|
|
48
|
+
private readonly chunks: Buffer[] = [];
|
|
49
|
+
private capturedBytes = 0;
|
|
50
|
+
private truncated = false;
|
|
51
|
+
|
|
52
|
+
constructor(private readonly maxBytes: number) {}
|
|
53
|
+
|
|
54
|
+
append(data: Buffer | string): void {
|
|
55
|
+
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data, 'utf8');
|
|
56
|
+
if (buffer.length === 0) return;
|
|
57
|
+
const remaining = this.maxBytes - this.capturedBytes;
|
|
58
|
+
if (remaining > 0) {
|
|
59
|
+
const kept = buffer.length <= remaining ? buffer : buffer.subarray(0, remaining);
|
|
60
|
+
this.chunks.push(kept);
|
|
61
|
+
this.capturedBytes += kept.length;
|
|
62
|
+
}
|
|
63
|
+
if (buffer.length > Math.max(0, remaining)) this.truncated = true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
text(): string {
|
|
67
|
+
return Buffer.concat(this.chunks, this.capturedBytes).toString('utf8');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
isTruncated(): boolean {
|
|
71
|
+
return this.truncated;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function lookupEnv(env: NodeJS.ProcessEnv, name: string): string | undefined {
|
|
76
|
+
const direct = env[name];
|
|
77
|
+
if (direct !== undefined) return direct;
|
|
78
|
+
const lowerName = name.toLowerCase();
|
|
79
|
+
for (const key of Object.keys(env)) {
|
|
80
|
+
if (key.toLowerCase() === lowerName) return env[key];
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function validateWindowsRoot(raw: string, label: string): string {
|
|
86
|
+
const value = raw.trim();
|
|
87
|
+
if (value.length === 0) throw new Error(`${label} is empty; cannot resolve taskkill.exe`);
|
|
88
|
+
if (value.includes('\0')) throw new Error(`${label} contains a NUL byte; cannot resolve taskkill.exe`);
|
|
89
|
+
if (!win32.isAbsolute(value)) {
|
|
90
|
+
throw new Error(`${label} must be an absolute Windows path; cannot resolve taskkill.exe`);
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function resolveTaskkillPath(env: NodeJS.ProcessEnv = process.env): string {
|
|
96
|
+
const systemRoot = lookupEnv(env, 'SystemRoot');
|
|
97
|
+
if (systemRoot !== undefined) {
|
|
98
|
+
return win32.join(validateWindowsRoot(systemRoot, 'SystemRoot'), 'System32', 'taskkill.exe');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const windir = lookupEnv(env, 'WINDIR');
|
|
102
|
+
if (windir !== undefined) {
|
|
103
|
+
return win32.join(validateWindowsRoot(windir, 'WINDIR'), 'System32', 'taskkill.exe');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
throw new Error('Cannot resolve taskkill.exe: SystemRoot is missing and WINDIR fallback is missing');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function validatePid(pid: number): void {
|
|
110
|
+
if (!Number.isSafeInteger(pid) || pid <= 0) {
|
|
111
|
+
throw new Error(`Invalid Windows taskkill pid ${String(pid)}; expected a positive safe integer`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function validatePhase(phase: WindowsKillPhase): void {
|
|
116
|
+
if (phase !== 'terminate' && phase !== 'force') {
|
|
117
|
+
throw new Error(`Invalid Windows taskkill phase ${String(phase)}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function positiveFiniteInteger(value: number | undefined, fallback: number, label: string): number {
|
|
122
|
+
const candidate = value ?? fallback;
|
|
123
|
+
if (!Number.isFinite(candidate) || candidate <= 0) {
|
|
124
|
+
throw new Error(`${label} must be a positive finite number`);
|
|
125
|
+
}
|
|
126
|
+
return Math.max(1, Math.floor(candidate));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function outcome(
|
|
130
|
+
exitCode: number | null,
|
|
131
|
+
signal: NodeJS.Signals | null,
|
|
132
|
+
stdout: BoundedCapture,
|
|
133
|
+
stderr: BoundedCapture,
|
|
134
|
+
): TaskkillOutcome {
|
|
135
|
+
return {
|
|
136
|
+
exitCode,
|
|
137
|
+
signal,
|
|
138
|
+
stdout: stdout.text(),
|
|
139
|
+
stderr: stderr.text(),
|
|
140
|
+
stdoutTruncated: stdout.isTruncated(),
|
|
141
|
+
stderrTruncated: stderr.isTruncated(),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function defaultSpawn(command: string, args: string[], options: SpawnOptions): WindowsTaskkillChildProcess {
|
|
146
|
+
return nodeSpawn(command, args, options);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function runWindowsTaskkill(
|
|
150
|
+
pid: number,
|
|
151
|
+
phase: WindowsKillPhase,
|
|
152
|
+
options: WindowsTaskkillOptions = {},
|
|
153
|
+
): Promise<TaskkillOutcome> {
|
|
154
|
+
validatePid(pid);
|
|
155
|
+
validatePhase(phase);
|
|
156
|
+
const env = options.env ?? process.env;
|
|
157
|
+
const taskkill = resolveTaskkillPath(env);
|
|
158
|
+
const timeoutMs = positiveFiniteInteger(options.timeoutMs, DEFAULT_TIMEOUT_MS, 'timeoutMs');
|
|
159
|
+
const maxCaptureBytes = positiveFiniteInteger(
|
|
160
|
+
options.maxCaptureBytes,
|
|
161
|
+
DEFAULT_MAX_CAPTURE_BYTES,
|
|
162
|
+
'maxCaptureBytes',
|
|
163
|
+
);
|
|
164
|
+
const spawn = options.spawn ?? defaultSpawn;
|
|
165
|
+
const abortSignal = options.signal;
|
|
166
|
+
|
|
167
|
+
const args = ['/PID', String(pid), '/T'];
|
|
168
|
+
if (phase === 'force') args.push('/F');
|
|
169
|
+
|
|
170
|
+
if (abortSignal?.aborted) {
|
|
171
|
+
const stdout = new BoundedCapture(maxCaptureBytes);
|
|
172
|
+
const stderr = new BoundedCapture(maxCaptureBytes);
|
|
173
|
+
stderr.append('Windows taskkill was aborted before launch');
|
|
174
|
+
return Promise.resolve(outcome(null, null, stdout, stderr));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return new Promise<TaskkillOutcome>((resolve) => {
|
|
178
|
+
const stdout = new BoundedCapture(maxCaptureBytes);
|
|
179
|
+
const stderr = new BoundedCapture(maxCaptureBytes);
|
|
180
|
+
let settled = false;
|
|
181
|
+
let timeout: NodeJS.Timeout | undefined;
|
|
182
|
+
let child: WindowsTaskkillChildProcess | undefined;
|
|
183
|
+
let abortListener: (() => void) | undefined;
|
|
184
|
+
|
|
185
|
+
const settle = (result: TaskkillOutcome): void => {
|
|
186
|
+
if (settled) return;
|
|
187
|
+
settled = true;
|
|
188
|
+
if (timeout !== undefined) clearTimeout(timeout);
|
|
189
|
+
if (abortSignal !== undefined && abortListener !== undefined) {
|
|
190
|
+
abortSignal.removeEventListener('abort', abortListener);
|
|
191
|
+
}
|
|
192
|
+
resolve(result);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const stopHelper = (reason: string): void => {
|
|
196
|
+
stderr.append(reason);
|
|
197
|
+
if (child !== undefined) {
|
|
198
|
+
try {
|
|
199
|
+
child.kill('SIGKILL');
|
|
200
|
+
} catch (error) {
|
|
201
|
+
stderr.append(`; helper kill failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
settle(outcome(null, null, stdout, stderr));
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const spawnOptions: SpawnOptions = {
|
|
208
|
+
env,
|
|
209
|
+
shell: false,
|
|
210
|
+
windowsVerbatimArguments: false,
|
|
211
|
+
windowsHide: true,
|
|
212
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
child = spawn(taskkill, args, spawnOptions);
|
|
217
|
+
} catch (error) {
|
|
218
|
+
stderr.append(`Windows taskkill spawn failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
219
|
+
settle(outcome(null, null, stdout, stderr));
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
child.stdout?.on('data', (data) => {
|
|
224
|
+
stdout.append(data);
|
|
225
|
+
});
|
|
226
|
+
child.stderr?.on('data', (data) => {
|
|
227
|
+
stderr.append(data);
|
|
228
|
+
});
|
|
229
|
+
child.on('error', (error) => {
|
|
230
|
+
stderr.append(`Windows taskkill spawn error: ${error.message}`);
|
|
231
|
+
settle(outcome(null, null, stdout, stderr));
|
|
232
|
+
});
|
|
233
|
+
child.on('close', (code, signal) => {
|
|
234
|
+
settle(outcome(code, signal, stdout, stderr));
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
abortListener = () => {
|
|
238
|
+
stopHelper('Windows taskkill was aborted');
|
|
239
|
+
};
|
|
240
|
+
abortSignal?.addEventListener('abort', abortListener, { once: true });
|
|
241
|
+
|
|
242
|
+
// This timeout is the settlement guarantee for a taskkill helper that never
|
|
243
|
+
// exits. It must keep the event loop alive: an unref'd timer lets the loop
|
|
244
|
+
// drain first and leaves this promise pending forever. `settle()` always
|
|
245
|
+
// clears it, so keeping it referenced cannot leak.
|
|
246
|
+
timeout = setTimeout(() => {
|
|
247
|
+
stopHelper(`Windows taskkill timed out after ${String(timeoutMs)}ms`);
|
|
248
|
+
}, timeoutMs);
|
|
249
|
+
});
|
|
250
|
+
}
|
package/src/fusion-extension.ts
CHANGED
|
@@ -129,7 +129,9 @@ function errorArtifactSuffix(error: unknown): string {
|
|
|
129
129
|
function toolFailureMessage(error: unknown): string {
|
|
130
130
|
const coordinates: string[] = [];
|
|
131
131
|
if (error instanceof FusionError) {
|
|
132
|
-
|
|
132
|
+
const budget = error.budget;
|
|
133
|
+
if (budget !== undefined) coordinates.push(`stage=${budget.budget_stage}`);
|
|
134
|
+
else if (error.stage !== undefined) coordinates.push(`stage=${error.stage}`);
|
|
133
135
|
if (error.slot !== undefined) coordinates.push(`slot=${String(error.slot)}`);
|
|
134
136
|
if (error.attempt !== undefined) coordinates.push(`attempt=${String(error.attempt)}`);
|
|
135
137
|
}
|
|
@@ -358,6 +360,7 @@ export function registerFusionExtension(pi: ExtensionAPI): void {
|
|
|
358
360
|
sessionId,
|
|
359
361
|
canonicalInput: built.input,
|
|
360
362
|
canonicalInputSerialized: built.serialized,
|
|
363
|
+
contextLedger: built.ledger,
|
|
361
364
|
config: loaded.config,
|
|
362
365
|
models,
|
|
363
366
|
signal: controller.signal,
|