From cf499ec597a5bd49ea68f93a17453ff953bbf33a Mon Sep 17 00:00:00 2001 From: "nekomimi.pet" Date: Thu, 29 Oct 2026 12:29:38 -0400 Subject: [PATCH] Spare pool, early release, lazy kernels and rest release on by default --- robot-docs/kernel.md | 4 ++-- robot-docs/runtime.md | 9 +++++---- src/albedo/daemon/session.gleam | 13 ++++++++----- src/albedo/harness/runtime.gleam | 5 +++-- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/robot-docs/kernel.md b/robot-docs/kernel.md index d8d9cb1fa..f90b6496a 100644 --- a/robot-docs/kernel.md +++ b/robot-docs/kernel.md @@ -559,13 +559,13 @@ still for it, `generation_over` ends it and replays what waited. ### fresh spare kernels -The runtime keeps a bounded pool of fresh local Python processes. `ALBEDO_KERNEL_SPARES` sets the target from 0 to 32; the default 0 disables the pool until the density measurements justify a default. A spare imports the kernel core without an agent namespace, workspace, plugin setup, or session environment. A spare is consumed once. Released kernels exit instead of returning to the pool. +The runtime keeps a bounded pool of fresh local Python processes. `ALBEDO_KERNEL_SPARES` sets the target from 0 to 32; the default is 2, and 0 disables the pool. A spare imports the kernel core without an agent namespace, workspace, plugin setup, or session environment. A spare is consumed once. Released kernels exit instead of returning to the pool. `boot_next` admits warmup and cleanup requests through the existing worker budget. Warmups wait behind session work and leave one admission slot free. The runtime holds one ticket per queued or running warmup. Admission removes a spare from the ready list before starting an open worker, so concurrent sessions cannot adopt the same process. An empty pool uses the ordinary cold open. Adoption consumes the spare even when an existing kernel reattaches or a remote workspace uses another launch path. The Python owner checks the source bundle before adoption. A stale spare stops before a cold replacement starts. Explicit reloads and upgrades retire ready spares and invalidate in-flight warmups. Failed cleanup retains its resource in the bounded pool accounting. Warmup failures retry after one second and mark the pool unhealthy until the retry. Shutdown drains admitted workers, stops all remaining spares, then closes the ledger. A spare also monitors the runtime actor and stops when that owner dies; spares never survive a daemon restart. -A completed turn asks, without waiting on the runtime actor, whether the pool is healthy: enabled, not shutting down or detaching, and not in a warmup-failure retry. If it is healthy and the session has no attached client, the session runs the ordinary release path and saves its namespace. Release does not wait for a ready spare, so a drained pool costs the next open a cold boot (about 147 ms more on macOS) instead of leaving the kernel resident. The release handler rechecks that the session is resting and has no live cell or job. Unknown live-work observations and snapshots that report unsaved names retain the kernel. Maintenance applies the same early-release policy to idle sessions and unowned kernels, using the 30 s detached grace. `ALBEDO_KERNEL_RELEASE_WITH_SPARE=1` enables this early release (off by default, which keeps kernels resident); the existing idle and memory-budget limits still apply. +A completed turn asks, without waiting on the runtime actor, whether the pool is healthy: enabled, not shutting down or detaching, and not in a warmup-failure retry. If it is healthy and the session has no attached client, the session runs the ordinary release path and saves its namespace. Release does not wait for a ready spare, so a drained pool costs the next open a cold boot (about 147 ms more on macOS) instead of leaving the kernel resident. The release handler rechecks that the session is resting and has no live cell or job. Unknown live-work observations and snapshots that report unsaved names retain the kernel. Maintenance applies the same early-release policy to idle sessions and unowned kernels, using the 30 s detached grace. This early release is the default; `ALBEDO_KERNEL_RELEASE_WITH_SPARE=0` turns it off and keeps kernels resident. The existing idle and memory-budget limits still apply. ## drops diff --git a/robot-docs/runtime.md b/robot-docs/runtime.md index 2a02c7da6..bdb2b7645 100644 --- a/robot-docs/runtime.md +++ b/robot-docs/runtime.md @@ -181,7 +181,7 @@ wakes it immediately; the timer never delays handling a message. Existing ## turns without a kernel -With `ALBEDO_KERNEL_LAZY=1` a turn starts from the session's composition +A turn starts from the session's composition (`session_state.composition`: the held kernel's, else the released one's, else `runtime.prepare_session_async`) and holds no Python kernel. The worker's `acquire` sends `AcquireKernel` before each tool call; the session answers with @@ -189,10 +189,11 @@ its live kernel or parks the request behind `kernel_or_park`, and refuses it onc the run is cancelled. The adoption notice (restored or lost namespace) reaches the model as a user note after that call's result. A turn whose transcript ends in unanswered calls still waits for the kernel, which alone can recover them. +`ALBEDO_KERNEL_LAZY=0` restores opening the kernel before the first request. -With the spare pool on (`ALBEDO_KERNEL_RELEASE_WITH_SPARE=1`), each model -request arms a rest timer: once the wait passes `ALBEDO_KERNEL_REST_MS` (3 s) -with no tool call, the kernel is released through the resting release's guards +While early release is on (the default; see kernel.md), each model request +arms a rest timer: once the wait passes `ALBEDO_KERNEL_REST_MS` (3 s; 0 keeps +the kernel for the whole turn) with no tool call, the kernel is released through the resting release's guards (no live work, no watcher, no transition, pool healthy), rechecked every second while they fail. Releasing at once cost 132 ms per step of a fast tool loop; at 3 s a fast loop never releases. diff --git a/src/albedo/daemon/session.gleam b/src/albedo/daemon/session.gleam index 835dbd6f0..7db2d6dd6 100644 --- a/src/albedo/daemon/session.gleam +++ b/src/albedo/daemon/session.gleam @@ -2804,10 +2804,10 @@ fn prepare_submission( @external(erlang, "albedo_daemon", "env") fn env(name: String) -> String -/// ALBEDO_KERNEL_LAZY=1: a turn starts from the session's composition and -/// acquires its kernel at its first tool call. +/// A turn starts from the session's composition and acquires its kernel at +/// its first tool call, unless ALBEDO_KERNEL_LAZY is "0". fn lazy_kernel() -> Bool { - env("ALBEDO_KERNEL_LAZY") == "1" + env("ALBEDO_KERNEL_LAZY") != "0" } /// Whether a turn can start now. With lazy kernels it needs only the @@ -2934,7 +2934,7 @@ fn hand_kernel( const acquire_refused = "the turn ended before its python kernel was ready" /// ALBEDO_KERNEL_REST_MS, else 3 s: how long a turn waits on its model before -/// its idle kernel is released. +/// its idle kernel is released; 0 keeps it for the whole turn. fn rest_ms() -> Int { case int.parse(env("ALBEDO_KERNEL_REST_MS")) { Ok(ms) if ms >= 0 -> ms @@ -2946,7 +2946,10 @@ fn rest_ms() -> Int { /// `rest_ms`; its next tool call reacquires one. A tool call or the next /// request makes the timer stale. fn arm_rest(state: State, id: String) -> State { - case lazy_kernel() && runtime.release_with_spare(), state.kernel { + case + lazy_kernel() && runtime.release_with_spare() && rest_ms() > 0, + state.kernel + { True, Some(_) -> { let token = state.rest_token + 1 let _ = clock.send_after(state.self, rest_ms(), RestKernel(id, token)) diff --git a/src/albedo/harness/runtime.gleam b/src/albedo/harness/runtime.gleam index 3fa809b89..5231d8d8c 100644 --- a/src/albedo/harness/runtime.gleam +++ b/src/albedo/harness/runtime.gleam @@ -1841,15 +1841,16 @@ pub fn when_pool_healthy(runtime: Runtime, answer: fn(Bool) -> Nil) -> Nil { process.send(runtime.subject, runtime_state.WhenPoolHealthy(answer)) } +/// On unless ALBEDO_KERNEL_RELEASE_WITH_SPARE is "0". pub fn release_with_spare() -> Bool { - env("ALBEDO_KERNEL_RELEASE_WITH_SPARE") == "1" + env("ALBEDO_KERNEL_RELEASE_WITH_SPARE") != "0" } fn spare_target() -> Int { env("ALBEDO_KERNEL_SPARES") |> int.parse |> result.map(int.clamp(_, 0, 32)) - |> result.unwrap(0) + |> result.unwrap(2) } @external(erlang, "albedo_daemon", "env") -- knotmirror 0.0.1