forked from PrimeIntellect-ai/prime-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipython.ts
More file actions
760 lines (703 loc) · 27.1 KB
/
Copy pathipython.ts
File metadata and controls
760 lines (703 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
import { existsSync } from "node:fs";
import { join } from "node:path";
import type { AgentTool, AgentToolResult } from "@ponythewhite/base-context-agent";
import type { ImageContent, TextContent } from "@ponythewhite/base-context-ai";
import { type Static, Type } from "typebox";
import { v4 as uuid } from "uuid";
import { PRODUCT } from "../../product-identity.js";
import { IMAGE_MIME_TYPES } from "../../utils/mime.js";
import { resolveKernelBashShell } from "../../utils/shell.js";
import type { ExtensionContext, ToolDefinition } from "../extensions/types.js";
import { withKernelBootPermit } from "../kernel/boot-gate.js";
import type { KernelBootstrapProgressHandler } from "../kernel/bootstrap.js";
import {
type CapturedKernelLifecycle,
type ExecuteOptions,
type ExecuteResult,
type HostRequestHandlers,
type KernelAttachment,
KernelBusyAfterInterruptError,
type KernelClient,
type KernelDiffDisplay,
type KernelSentAgentMessage,
ReplKernelManager,
} from "../kernel/index.js";
import { manifestPathIn, type RestoreResult, snapshotPathIn } from "../kernel/state-snapshot.js";
import { nativeRecoveryMetadata, stringifyNativeRecoveryResponse } from "../selective-recovery.js";
import type { PythonSkillRuntimeInfo } from "../skills.js";
import { admitNativeRecoveryToolResult, nativeRecoveryToolResult } from "./prime-context.js";
import { wrapToolDefinition } from "./tool-definition-wrapper.js";
// Bound before tool/extension callbacks; replacement reader methods cannot publish lifecycle facts.
const captureOwnedReplState = ReplKernelManager.prototype.captureLifecycleState;
const RLM_BOOTSTRAP_HEADER_CODE = `
import asyncio
import os as _prime_agent_os
_prime_agent_os.environ["NO_COLOR"] = "1"
`.trim();
const RLM_BOOTSTRAP_RUNTIME_CODE = `
try:
import rlm as _prime_agent_rlm_module
rlm = _prime_agent_rlm_module.rlm
bash = _prime_agent_rlm_module.bash
import rlm.mcp as mcp
except Exception as _prime_agent_rlm_error:
_PRIME_AGENT_RLM_IMPORT_ERROR = str(_prime_agent_rlm_error)
class _PrimeAgentMissingRlm:
def _raise_missing(self):
raise RuntimeError(
"${PRODUCT.runtimeDistribution} is not installed in this kernel. "
"Rebuild the ${PRODUCT.name} kernel environment, or set "
"BASE_CONTEXT_KERNEL_PYTHON to a kernel environment with ${PRODUCT.runtimeDistribution} installed. "
f"Import error: {_PRIME_AGENT_RLM_IMPORT_ERROR}"
)
async def run(self, prompt, **kwargs):
self._raise_missing()
async def find_models(self, query="", limit=8):
self._raise_missing()
async def list_subagents(self):
self._raise_missing()
async def delete_subagent(self, target):
self._raise_missing()
async def __call__(self, prompt, **kwargs):
return await self.run(prompt, **kwargs)
rlm = _PrimeAgentMissingRlm()
def bash(command):
rlm._raise_missing()
`.trim();
export function buildRlmBootstrapCode(pythonSkills: readonly PythonSkillRuntimeInfo[] = []): string {
const baseCode = [RLM_BOOTSTRAP_HEADER_CODE, RLM_BOOTSTRAP_RUNTIME_CODE].join("\n\n");
const importNames = [...new Set(pythonSkills.map((skill) => skill.importName))];
if (importNames.length === 0) {
return baseCode;
}
return `
${baseCode}
import importlib as _prime_agent_importlib
import inspect as _prime_agent_inspect
import sys as _prime_agent_sys
import types as _prime_agent_types
class _PrimeAgentCallableSkillModule(_prime_agent_types.ModuleType):
async def __call__(self, *args, **kwargs):
result = self.run(*args, **kwargs)
if _prime_agent_inspect.isawaitable(result):
return await result
return result
class _PrimeAgentUnavailableSkill:
def __init__(self, name, error):
self.__name__ = name
self._prime_agent_import_error = error
self.__doc__ = f"Python skill {name} is unavailable: {error}"
async def run(self, *args, **kwargs):
raise RuntimeError(
f"Python skill {self.__name__} is unavailable in this kernel. "
f"Import error: {self._prime_agent_import_error}"
)
async def __call__(self, *args, **kwargs):
return await self.run(*args, **kwargs)
def __repr__(self):
return f"<unavailable Python skill {self.__name__!r}: {self._prime_agent_import_error}>"
def _prime_agent_wrap_skill_module(module):
run = getattr(module, "run", None)
if not callable(run):
return module
if isinstance(module, _PrimeAgentCallableSkillModule):
return module
wrapped = _PrimeAgentCallableSkillModule(module.__name__)
wrapped.__dict__.update(module.__dict__)
try:
wrapped.__signature__ = _prime_agent_inspect.signature(run)
except Exception:
pass
doc = getattr(run, "__doc__", None)
if doc:
wrapped.__doc__ = doc
_prime_agent_sys.modules[module.__name__] = wrapped
return wrapped
_PRIME_AGENT_SKILL_IMPORT_ERRORS = {}
for _prime_agent_skill_name in ${JSON.stringify(importNames)}:
try:
globals()[_prime_agent_skill_name] = _prime_agent_wrap_skill_module(
_prime_agent_importlib.import_module(_prime_agent_skill_name)
)
except Exception as _prime_agent_skill_error:
_PRIME_AGENT_SKILL_IMPORT_ERRORS[_prime_agent_skill_name] = str(_prime_agent_skill_error)
globals()[_prime_agent_skill_name] = _PrimeAgentUnavailableSkill(
_prime_agent_skill_name,
str(_prime_agent_skill_error),
)
`.trim();
}
const ipythonSchema = Type.Object({
code: Type.String({
description:
"Python code to execute in the persistent Python REPL. Use the target project's own environment for project imports, tests, scripts, CLIs, and dependency checks instead of direct kernel imports.",
}),
});
const BUSY_KERNEL_WAIT_CHOICE = "Wait and preserve state";
const BUSY_KERNEL_KILL_CHOICE = "Kill kernel and restart";
const BUSY_KERNEL_PROMPT = [
"Interrupted Python cell is still running",
"Ctrl+C sent an interrupt, but the previous cell has not stopped yet. A new command cannot start until it finishes.",
"Waiting preserves the current kernel state. Killing restarts the kernel and loses in-memory variables, imports, and running tasks.",
].join("\n");
const KERNEL_RESTART_NOTICE = [
"<ipython_kernel_reset>",
"The Python kernel was restarted after a previous interrupted cell kept running. Variables, imports, async tasks, and open resources from before the restart are no longer available; recreate them before using them.",
"</ipython_kernel_reset>",
].join("\n");
function createAbortError(): Error {
return new Error("Python execution aborted");
}
function raceWithAbort<T>(promise: Promise<T>, signal: AbortSignal | undefined, onAbort?: () => void): Promise<T> {
if (!signal) {
return promise;
}
if (signal.aborted) {
onAbort?.();
return Promise.reject(createAbortError());
}
return new Promise<T>((resolve, reject) => {
let settled = false;
const cleanup = () => signal.removeEventListener("abort", abort);
const abort = () => {
if (settled) {
return;
}
settled = true;
cleanup();
onAbort?.();
reject(createAbortError());
};
signal.addEventListener("abort", abort, { once: true });
promise.then(
(value) => {
if (settled) {
return;
}
settled = true;
cleanup();
resolve(value);
},
(error: unknown) => {
if (settled) {
return;
}
settled = true;
cleanup();
reject(error);
},
);
});
}
function createLinkedAbortSignal(sources: readonly (AbortSignal | undefined)[]): {
signal: AbortSignal;
cleanup: () => void;
} {
const controller = new AbortController();
const cleanups: Array<() => void> = [];
const abort = () => controller.abort();
for (const source of sources) {
if (!source) {
continue;
}
if (source.aborted) {
controller.abort();
continue;
}
const listener = () => abort();
source.addEventListener("abort", listener, { once: true });
cleanups.push(() => source.removeEventListener("abort", listener));
}
return {
signal: controller.signal,
cleanup: () => {
for (const cleanup of cleanups) {
cleanup();
}
},
};
}
function setWorkingMessage(ctx: ExtensionContext | undefined, message?: string): void {
try {
ctx?.ui.setWorkingMessage(message);
} catch {
// Stale UI context; cosmetic only.
}
}
export type IpythonToolInput = Static<typeof ipythonSchema>;
export interface IpythonToolDetails {
/** Descriptive selectors only; the exact selected body is in this tool result's content. */
nativeRecoveries?: ReturnType<typeof nativeRecoveryMetadata>[];
durationMs?: number;
status?: "ok" | "error" | "aborted" | "starting";
errorEname?: string;
stdout?: string;
stderr?: string;
result?: string;
/** Output that arrived without this cell's id (threads, other cells' leftovers), shown separately from stdout. */
backgroundOutput?: string;
/** Diffs streamed from file edits, rendered by the cell view. */
diffs?: KernelDiffDisplay[];
/** Media attachments loaded into context (e.g. by the attach-image skill). */
attachments?: KernelAttachment[];
/** Agent messages sent from this cell. */
sentAgentMessages?: KernelSentAgentMessage[];
/** True when this result came after killing and restarting a busy kernel. */
kernelRestarted?: boolean;
error?: {
ename: string;
evalue: string;
traceback: string[];
};
}
export interface IpythonToolOptions {
/** @internal Captured inside an actual owned tool invocation, not inferred from cell output. */
captureNativeRecoveryScope?: () => ExecuteOptions["runNativeRecovery"];
/** Python override. Must have base-context-runtime installed. */
python?: string;
env?: Record<string, string>;
/** Command prefix prepended to every bash() command. */
commandPrefix?: string;
/** Shell used by bash(). */
shellPath?: string;
sessionId?: string;
/** Typed host request handlers for the kernel↔host bridge (rlm.run, goal.*, …). */
hostHandlers?: HostRequestHandlers;
pythonSkills?: readonly PythonSkillRuntimeInfo[];
/** Per-session artifact dir where the kernel namespace snapshot is stored. Omit to disable snapshots. */
snapshotDir?: string;
/** Resolves before this kernel starts — e.g. the previous provisioner's dispose, so a
* /reload's old-kernel snapshot flush can't race the new kernel's restore. */
readyGate?: Promise<unknown>;
/**
* Fires once per kernel start when a previous session's namespace was revived
* (some names restored or some failed), so the session can tell the model.
*/
onRestore?: (result: RestoreResult) => void;
onLateSentAgentMessage?: (toolCallId: string, message: KernelSentAgentMessage) => void;
/** Shared provisioner owning the kernel lifecycle. When provided, the remaining options are ignored. */
provisioner?: IpythonKernelProvisioner;
}
/**
* Owns the lazy create+start+runtime-bootstrap of one session's Python kernel.
*
* Concurrent ensure() calls await the same in-flight startup, a failed startup
* clears the memo so the next call retries fresh, and progress listeners can
* attach mid-flight (a tool call racing a background prewarm()).
*/
export class IpythonKernelProvisioner {
private readonly lifecycleOwner = uuid();
private managerPromise?: Promise<KernelClient>;
private startedManager?: KernelClient;
private readonly startupListeners = new Set<KernelBootstrapProgressHandler>();
private lastStartupMessage?: string;
private _lastRestore?: RestoreResult;
private readonly disposeController = new AbortController();
/** Snapshot policy of the dispose that aborted a startup, honored by startKernel's failure teardown. */
private disposeSnapshot = true;
constructor(
private readonly cwd: string,
private readonly options?: Omit<IpythonToolOptions, "provisioner">,
) {}
/** The kernel manager, once a startup has completed successfully. */
get manager(): KernelClient | undefined {
return this.startedManager;
}
captureKernelState(): CapturedKernelLifecycle {
const manager = this.startedManager;
const pending = this.managerPromise;
const disposed = this.disposeController.signal.aborted;
const captured = manager instanceof ReplKernelManager ? captureOwnedReplState.call(manager) : undefined;
const state = disposed ? "disposed" : pending && !manager ? "provisioning" : "unobserved";
return Object.freeze({
snapshot:
captured?.snapshot ??
Object.freeze({
source: "ipython-provisioner" as const,
owner: this.lifecycleOwner,
generation: null,
state,
}),
isCurrent: () =>
this.startedManager === manager &&
this.managerPromise === pending &&
this.disposeController.signal.aborted === disposed &&
(captured?.isCurrent() ?? true),
});
}
/** Result of reviving a prior session's namespace on the last kernel start, if any. */
get lastRestore(): RestoreResult | undefined {
return this._lastRestore;
}
/** Start the kernel in the background. Failures are swallowed here and surface on the next ensure(). */
prewarm(): void {
void this.ensure().catch(() => {});
}
/** Whether a kernel has finished starting and is currently running. */
get hasRunningKernel(): boolean {
return this.startedManager?.isRunning ?? false;
}
/** Remove live variables above the snapshot's per-variable size limit. */
async pruneOversizedVariables(): Promise<string[] | null> {
const m = this.startedManager ?? (await this.managerPromise?.catch(() => undefined));
const result = await m?.pruneOversizedVariables();
return result ? (result.pruned ?? []) : null;
}
/** Live user-defined names in the kernel namespace, or null if listing failed / no kernel. */
async listNamespaceNames(signal?: AbortSignal): Promise<string[] | null> {
const m = this.startedManager ?? (await this.managerPromise?.catch(() => undefined));
return (await m?.listNamespaceNames(signal)) ?? null;
}
/** Dispose the kernel owned by this provisioner, including one still starting up. */
async dispose(options?: { snapshot?: boolean }): Promise<void> {
this.disposeSnapshot = options?.snapshot ?? true;
// Drops a still-queued boot out of the semaphore and short-circuits an
// in-flight startKernel before it spawns, so a disposed session's boot
// doesn't waste a slot during a fan-out.
this.disposeController.abort();
const pending = this.managerPromise;
this.managerPromise = undefined;
this.startedManager = undefined;
if (!pending) return;
try {
const m = await pending;
await m.shutdown({ snapshot: this.disposeSnapshot, drainHostRequests: true });
} catch {
// a failed startup already cleaned up after itself
}
}
async kill(): Promise<void> {
const pending = this.managerPromise;
this.managerPromise = undefined;
this.startedManager = undefined;
if (!pending) return;
try {
const m = await pending;
await m.kill();
} catch {
// a failed startup already cleaned up after itself
}
}
ensure(onProgress?: KernelBootstrapProgressHandler, signal?: AbortSignal): Promise<KernelClient> {
if (signal?.aborted) {
return Promise.reject(createAbortError());
}
let cleanupProgressListener: (() => void) | undefined;
if (onProgress && !this.startedManager) {
this.startupListeners.add(onProgress);
cleanupProgressListener = () => {
this.startupListeners.delete(onProgress);
signal?.removeEventListener("abort", cleanupProgressListener!);
};
signal?.addEventListener("abort", cleanupProgressListener, { once: true });
// Joining an in-flight startup: replay the current stage.
if (this.managerPromise && this.lastStartupMessage) {
onProgress(this.lastStartupMessage);
}
}
if (!this.managerPromise) {
const startup = this.startKernel(signal);
this.managerPromise = startup;
startup.then(
(m) => {
if (this.managerPromise === startup) {
this.startedManager = m;
}
this.settleStartup();
},
() => {
// Clear the memo so the next ensure() retries instead of
// rethrowing a cached rejection forever.
if (this.managerPromise === startup) {
this.managerPromise = undefined;
}
this.settleStartup();
},
);
}
return raceWithAbort(this.managerPromise, signal).finally(() => {
cleanupProgressListener?.();
});
}
private settleStartup(): void {
this.startupListeners.clear();
this.lastStartupMessage = undefined;
}
private emitStartupProgress(message: string): void {
this.lastStartupMessage = message;
for (const listener of [...this.startupListeners]) {
listener(message);
}
}
private async startKernel(signal?: AbortSignal): Promise<KernelClient> {
const startupAbort = createLinkedAbortSignal([this.disposeController.signal, signal]);
const startupSignal = startupAbort.signal;
// Wait for a previous provisioner (e.g. on /reload) to finish disposing — and
// flushing its final snapshot — before we read that snapshot back, so the two
// kernels can't race over the same on-disk file. Guarded so the common
// no-gate path stays synchronous (callers rely on prompt startup progress).
try {
if (this.options?.readyGate) {
await raceWithAbort(
this.options.readyGate.catch(() => {}),
startupSignal,
);
}
const snapshotDir = this.options?.snapshotDir;
// Always inject an absolute trusted shell (undefined only on win32
// without bash, where the runtime's teaching error fires instead).
const shellPath = resolveKernelBashShell(this.options?.shellPath);
const commandPrefix = this.options?.commandPrefix;
const bootstrapCode = buildRlmBootstrapCode(this.options?.pythonSkills);
const m = new ReplKernelManager({
python: this.options?.python,
cwd: this.cwd,
// bash() reads these to pick its shell and command prefix.
env: {
...this.options?.env,
...(shellPath ? { BASE_CONTEXT_BASH_SHELL: shellPath } : {}),
...(commandPrefix ? { BASE_CONTEXT_BASH_COMMAND_PREFIX: commandPrefix } : {}),
},
sessionId: this.options?.sessionId,
hostHandlers: this.options?.hostHandlers,
pythonSkills: this.options?.pythonSkills,
// Only persistent sessions (which have an artifact dir) get a revivable snapshot.
snapshot: snapshotDir
? { path: snapshotPathIn(snapshotDir), manifestPath: manifestPathIn(snapshotDir) }
: undefined,
stderrLogPath: snapshotDir ? join(snapshotDir, "kernel-stderr.log") : undefined,
bootstrapCode,
});
let pendingRestore: RestoreResult | undefined;
try {
// Emitted synchronously (before the permit await) so a listener attaching
// mid-flight can replay the current stage.
this.emitStartupProgress("Starting Python kernel...");
// Only the process spawn + port resolve contends for OS resources under a
// fan-out, and it is bounded by start()'s own timeouts — so the permit
// covers only start(). Restore/bootstrap run per-kernel afterwards and are
// unbounded execute()s; holding the global permit across them could pin it
// forever on a wedged bootstrap and starve every other session's boot.
await withKernelBootPermit(() => {
// Disposed while queued for the permit — don't spawn a kernel nobody wants.
if (startupSignal.aborted) throw new Error("Kernel provisioner disposed before start");
return m.start({
onBootstrapProgress: (message) => this.emitStartupProgress(message),
signal: startupSignal,
});
}, startupSignal);
// Revive a prior session's namespace before the bootstrap, so the bootstrap
// then overwrites live handles (rlm, skills) on top of anything restored.
if (snapshotDir) {
const snapshotExisted = existsSync(snapshotPathIn(snapshotDir));
this.emitStartupProgress("Restoring Python state...");
const restore = await raceWithAbort(m.restoreState(), startupSignal);
if (snapshotExisted) {
pendingRestore = restore ?? { restored: [], failed: [], path: snapshotPathIn(snapshotDir) };
}
}
this.emitStartupProgress("Preparing Python runtime...");
const bootstrap = await m.execute(bootstrapCode, {
signal: startupSignal,
});
if (bootstrap.status !== "ok") {
const details = [bootstrap.stderr, bootstrap.error?.traceback.join("\n")].filter(Boolean).join("\n");
throw new Error(`Failed to initialize rlm runtime in the Python kernel:\n${details}`);
}
} catch (error) {
// Never leak the kernel process if startup fails after spawn — and never
// surface the failure before the teardown (final snapshot flush included)
// finished, or a replacement provisioner gated on this dispose could
// race the still-flushing kernel over the same snapshot files.
await m.shutdown({ snapshot: this.disposeSnapshot, drainHostRequests: true }).catch(() => undefined);
throw error;
}
// Only tell the model what was revived once the kernel is actually usable —
// a notice claiming restored state must never outlive a failed bootstrap.
if (pendingRestore) {
this._lastRestore = pendingRestore;
this.options?.onRestore?.(pendingRestore);
}
return m;
} finally {
startupAbort.cleanup();
}
}
}
async function chooseBusyKernelAction(
ctx: ExtensionContext | undefined,
signal: AbortSignal | undefined,
): Promise<"wait" | "kill" | "cancel"> {
if (!ctx?.hasUI) {
return "cancel";
}
const choice = await ctx.ui.select(BUSY_KERNEL_PROMPT, [BUSY_KERNEL_WAIT_CHOICE, BUSY_KERNEL_KILL_CHOICE], {
signal,
});
if (choice === BUSY_KERNEL_WAIT_CHOICE) {
return "wait";
}
if (choice === BUSY_KERNEL_KILL_CHOICE) {
return "kill";
}
return "cancel";
}
async function executeWithBusyKernelChoice(
provisioner: IpythonKernelProvisioner,
reportStartupProgress: KernelBootstrapProgressHandler,
toolCallId: string,
code: string,
signal: AbortSignal | undefined,
onStream: (chunk: string, name: "stdout" | "stderr") => void,
onWorkingMessage: (message?: string) => void,
onLateSentAgentMessage: ((toolCallId: string, message: KernelSentAgentMessage) => void) | undefined,
runNativeRecovery: ExecuteOptions["runNativeRecovery"],
ctx: ExtensionContext | undefined,
): Promise<{ result: ExecuteResult; kernelRestarted: boolean }> {
let kernelRestarted = false;
while (true) {
const m = await provisioner.ensure(reportStartupProgress, signal);
try {
return {
result: await m.execute(code, {
signal,
nativeRecovery: true,
runNativeRecovery,
onStream,
onLateSentAgentMessage: onLateSentAgentMessage
? (message) => onLateSentAgentMessage(toolCallId, message)
: undefined,
}),
kernelRestarted,
};
} catch (error) {
if (!(error instanceof KernelBusyAfterInterruptError) || signal?.aborted) {
throw error;
}
const action = await chooseBusyKernelAction(ctx, signal);
if (action === "wait") {
onWorkingMessage("Waiting for Python kernel...");
continue;
}
if (action === "kill") {
onWorkingMessage("Restarting Python kernel...");
await provisioner.kill();
kernelRestarted = true;
continue;
}
throw error;
}
}
}
/** Turn kernel image attachments into `ImageContent` blocks; non-image types are dropped. */
export function imageBlocksFromAttachments(attachments: readonly KernelAttachment[] | undefined): ImageContent[] {
if (!attachments) return [];
return attachments
.filter((a) => IMAGE_MIME_TYPES.has(a.mimeType))
.map((a) => ({ type: "image", data: a.data, mimeType: a.mimeType }));
}
export function createIpythonToolDefinition(
cwd: string,
options?: IpythonToolOptions,
): ToolDefinition<typeof ipythonSchema, IpythonToolDetails> {
const provisioner = options?.provisioner ?? new IpythonKernelProvisioner(cwd, options);
return {
name: "ipython",
label: "ipython",
description:
"Execute Python code in a persistent Python REPL. Top-level `await` is supported. Variables, imports, and loaded data persist across calls, and are revived on a best-effort basis when a session is resumed (objects that cannot be serialized are dropped and reported). Run shell commands with `bash('cmd')` / `await bash('cmd')`. Project imports, tests, scripts, CLIs, and dependency checks should run through the target project's own environment.",
promptSnippet: "ipython - persistent Python REPL for code, state, and bash() orchestration",
// The kernel is single-threaded — pi must not run two ipython calls in parallel within a batch.
executionMode: "sequential",
parameters: ipythonSchema,
execute: async (toolCallId, params, signal, onUpdate, ctx) => {
let hasWorkingMessage = false;
const setToolWorkingMessage = (message?: string) => {
setWorkingMessage(ctx, message);
hasWorkingMessage = message !== undefined;
};
const reportStartupProgress: KernelBootstrapProgressHandler = (message) => {
setToolWorkingMessage(message);
onUpdate?.({
content: [{ type: "text", text: message }],
details: { status: "starting" },
});
};
try {
const { result: r, kernelRestarted } = await executeWithBusyKernelChoice(
provisioner,
reportStartupProgress,
toolCallId,
params.code,
signal,
(chunk) => {
onUpdate?.({
content: [{ type: "text", text: chunk }],
details: { status: "ok" },
});
},
setToolWorkingMessage,
options?.onLateSentAgentMessage,
options?.captureNativeRecoveryScope?.(),
ctx,
);
let text = r.stdout;
if (r.stderr) text += (text ? "\n" : "") + r.stderr;
if (r.result) text += (text ? "\n" : "") + r.result;
if (r.status === "error" && r.error) {
text += (text ? "\n" : "") + r.error.traceback.join("\n");
}
if (r.backgroundOutput) {
text += `${text ? "\n" : ""}[background output (unattributed)]\n${r.backgroundOutput}`;
}
if (kernelRestarted) {
text = text ? `${KERNEL_RESTART_NOTICE}\n\n${text}` : KERNEL_RESTART_NOTICE;
}
const imageBlocks = imageBlocksFromAttachments(r.attachments);
const content: (TextContent | ImageContent)[] = [{ type: "text", text: text || "" }, ...imageBlocks];
// Bypass stdout/trailing-expression truncation. This is the sole selected-body persistence path.
for (const recovery of r.nativeRecoveries ?? []) {
content.push({ type: "text", text: stringifyNativeRecoveryResponse(recovery) });
}
const result: AgentToolResult<IpythonToolDetails> & { isError: boolean } = {
content,
details: {
durationMs: r.durationMs,
status: r.status,
errorEname: r.error?.ename,
stdout: r.stdout,
stderr: r.stderr,
result: r.result,
backgroundOutput: r.backgroundOutput,
diffs: r.diffs,
attachments: r.attachments,
sentAgentMessages: r.sentAgentMessages,
nativeRecoveries: r.nativeRecoveries?.map(nativeRecoveryMetadata),
kernelRestarted,
error: r.error,
},
isError: r.status === "error" || r.status === "aborted",
};
return r.nativeRecoveries?.length
? admitNativeRecoveryToolResult(result, (refusal) => ({
content: nativeRecoveryToolResult(refusal).content,
details: {
status: r.status,
durationMs: r.durationMs,
kernelRestarted,
nativeRecoveries: [nativeRecoveryMetadata(refusal)],
},
isError: true,
}))
: result;
} finally {
if (hasWorkingMessage) {
setToolWorkingMessage();
}
}
},
};
}
export function createIpythonTool(cwd: string, options?: IpythonToolOptions): AgentTool<typeof ipythonSchema> {
return wrapToolDefinition(createIpythonToolDefinition(cwd, options));
}