mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-12-05 22:39:19 +00:00
Snapshots before every call cost a full heap write per call (128 ms per call with 100k live objects, against 2 ms with the journal), and replay restarts as fast or faster except for compute-heavy scripts. Durable code mode is back to main's journal and replay, run through start() on djs. - durable: src/codemode, tests, spec 7.6 and README as on main (call.ts, snapshots, deliveries and parking are gone), keeping djs, TypeScript, namespace imports and the searchTools() hint. CODEMODE_MODULE_VERSION 3. - pi-codemode: start() is supported again (replay.ts, no deprecation). It numbers the calls it hands out 1, 2, ... (djs counts sync host calls too), lists the un-awaited calls of the last step in run.dropped, and takes modules and missingToolHint. begin()/resume()/save() stay. - p2: the code mode plugin drains its sandbox on reload and replays after a worker stop, as on main; CPU budget kept as CODEMODE_CPU_BUDGET (part of the fingerprint).
63 lines
2.7 KiB
TypeScript
63 lines
2.7 KiB
TypeScript
// Durable code mode: the `codemode` tool runs model-written JavaScript or TypeScript that calls the conversation's
|
|
// other tools as nested durable calls (`await tools.read({ path })`), each script in a worker thread of its own, and
|
|
// survives worker restarts by replaying its journal.
|
|
|
|
import { CodemodeSandbox } from "@earendil-works/pi-codemode";
|
|
import { createCodemodeExtension } from "@earendil-works/pi-durable/codemode";
|
|
import { defineFacet } from "@p2/app/v1";
|
|
import { ExtensionSwitches } from "@p2/app/v1/node";
|
|
import { Extensions, harnessOf, workerOf } from "@p2/app/v1/worker";
|
|
import { WorkerModels } from "../models/contract.ts";
|
|
import { RunningCalls } from "./calls.ts";
|
|
import { CODEMODE_CPU_BUDGET, CODEMODE_MEMORY_LIMIT_BYTES, CodemodeRuntime } from "./contract.ts";
|
|
|
|
export default defineFacet((host) => {
|
|
host.serve(CodemodeRuntime, (_params, self) => {
|
|
const harness = harnessOf(self);
|
|
const worker = workerOf(self);
|
|
// Natives close in reverse order: running calls first, then the sandbox, which ends VMs and worker threads. Only
|
|
// a removal (the object ends) cancels running calls; after a worker stop Durable replays them in the next
|
|
// worker, and a reload drains them: the old sandbox closes once its scripts finished, the new one opens at once.
|
|
const calls = new RunningCalls();
|
|
const sandboxResource = self.open(
|
|
async () =>
|
|
new CodemodeSandbox({
|
|
memoryLimitBytes: CODEMODE_MEMORY_LIMIT_BYTES,
|
|
interruptBudget: CODEMODE_CPU_BUDGET,
|
|
}),
|
|
(sandbox, reason) => {
|
|
if (reason.kind === "reload") void calls.idle().then(() => sandbox.close());
|
|
else return sandbox.close();
|
|
},
|
|
);
|
|
self.open(
|
|
async () => calls,
|
|
async (running, reason, context) => {
|
|
if (reason.kind === "end" && !worker().liveness.stopping) await running.cancel(harness().durable, context);
|
|
},
|
|
);
|
|
// The tool reads the sandbox when a script starts: a running script keeps the sandbox it started on.
|
|
const codemode = createCodemodeExtension({
|
|
get sandbox() {
|
|
return sandboxResource.value;
|
|
},
|
|
});
|
|
// The `codemode` setting (default on in p2) installs and uninstalls the extension live, plugin loaded or not.
|
|
// A script already running keeps its tool; the next request no longer offers it.
|
|
const settings = self.get(WorkerModels, {});
|
|
self.contribute(
|
|
ExtensionSwitches,
|
|
self.derive([settings.settings], ([value]) => ({ [codemode.name]: value.codemode !== false })),
|
|
);
|
|
self.contribute(Extensions, {
|
|
[codemode.name]: {
|
|
...codemode,
|
|
tools: codemode.tools!.map((tool) => ({
|
|
...tool,
|
|
execute: (...args: Parameters<typeof tool.execute>) => calls.track(args[1], () => tool.execute(...args)),
|
|
})),
|
|
},
|
|
});
|
|
return {};
|
|
});
|
|
});
|