Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
4.0 kB · 103 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104// Locations and result shapes for the kernel's persisted user namespace, which// is revived when a session resumes. The kernel is otherwise spawned fresh on// resume, leaving the model believing it still has access to variables/imports// it defined earlier.//// Snapshotting is best-effort and per-variable: each top-level name is pickled// with `dill` independently, so a single unpicklable object (open file, socket,// GPU tensor, …) is skipped and reported rather than aborting the whole snapshot.import { readFileSync, statSync, writeFileSync } from "node:fs";import { isAbsolute, join } from "node:path";
/** Default ceiling on a snapshot payload. Over-cap variables are skipped + reported. */export const DEFAULT_SNAPSHOT_MAX_BYTES = 256 * 1024 * 1024;/** Default ceiling for one serialized variable. */export const DEFAULT_SNAPSHOT_MAX_VARIABLE_BYTES = 16 * 1024 * 1024;
/** Base filename for the kernel snapshot within a session's artifact directory. */const KERNEL_STATE_BASENAME = "kernel-state";
/** * Base filename for the kernel's working directory. * * Separate from the snapshot on purpose: a working directory is session state, and the * snapshot is variable state written and re-read more than once per handoff. Recording * the directory in its own file means it is written once at shutdown and read once at * boot, so no ordering in the handoff can lose or overwrite it. */const KERNEL_CWD_BASENAME = "kernel-cwd";
export interface SnapshotResult { /** Working directory the kernel was in when it snapshotted. */ cwd?: string; /** Top-level names successfully serialized into the payload. */ saved: string[]; /** Names that could not be serialized, with a short reason. */ skipped: { name: string; reason: string }[]; /** Oversized live variables removed by an explicit compaction snapshot. */ pruned?: string[]; /** Payload size on disk, in bytes. */ bytes: number; path: string;}
export interface RestoreResult { /** Names successfully revived into the kernel namespace. */ restored: string[]; /** Names present in the snapshot that failed to revive, with a short reason. */ failed: { name: string; reason: string }[]; path: string; /** Working directory the snapshot was taken in, re-applied on restore. */ cwd?: string; /** The snapshot's working directory no longer exists; the kernel kept its own. */ cwdMissing?: string;}
/** Absolute path to the dill payload within a session's artifact directory. */export function snapshotPathIn(artifactDir: string): string { return join(artifactDir, `${KERNEL_STATE_BASENAME}.dill`);}
/** Absolute path to the JSON manifest within a session's artifact directory. */export function manifestPathIn(artifactDir: string): string { return join(artifactDir, `${KERNEL_STATE_BASENAME}.json`);}
/** Absolute path to the kernel working directory recorded for a session. */export function kernelCwdPathIn(artifactDir: string): string { return join(artifactDir, KERNEL_CWD_BASENAME);}
/** * The working directory recorded for a session, or undefined when none is recorded or * the directory is gone. A stale record is reported through `missing`, never used. */export function readKernelCwd(artifactDir: string): { cwd?: string; missing?: string } { let recorded: string; try { recorded = readFileSync(kernelCwdPathIn(artifactDir), "utf8").trim(); } catch { return {}; } if (!recorded || !isAbsolute(recorded)) { return {}; } try { if (!statSync(recorded).isDirectory()) { return { missing: recorded }; } } catch { return { missing: recorded }; } return { cwd: recorded };}
/** Record the kernel's working directory for the next boot. Best-effort. */export function writeKernelCwd(artifactDir: string, cwd: string | undefined): void { if (!cwd || !isAbsolute(cwd)) return; try { writeFileSync(kernelCwdPathIn(artifactDir), `${cwd}\n`, { mode: 0o600 }); } catch { // A session that cannot record its directory still runs; it just resumes elsewhere. }}