Skip to content

docs: in-process learning on the persona engine (before the lanes go) - #6147

Merged
joelteply merged 2 commits into
canaryfrom
fable/in-process-learning-design
Nov 18, 2026
Merged

joelteply merged 2 commits into
canaryfrom
fable/in-process-learning-design

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

A design for review before any code. Today a mind learns only inside llama-server: the trainer finds its engine through lane_registry::live_lane(), and the fork's C++ (server-context-learn.inc, 647 lines at pin a88f255cc) runs llama_opt_learn_*. So deleting the lanes first would stop learning.

The doc sets the order:

  • L1: a Learner in the llama crate.
  • L2: Job::Learn chunks interleaved with turns on the one persona-scheduler driver, never in parallel.
  • L3: the trainer through a LearnPort, with HTTP and in-process adapters during the migration.
  • Then the lanes and llama-server go.

Each slice has a validator. The live gate is one admitted step on a resident, with throughput held, on the M5 and on BigMama.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…ting the lanes

Learning runs only inside llama-server today (the trainer finds it via live_lane; the fork's C++
runs llama_opt_learn_*). This sets the order: llama crate Learner (L1), Job::Learn chunks
interleaved with turns on the one driver (L2), the trainer through a LearnPort with two adapters
(L3), then the lanes and llama-server go.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…r training_admission/ResourceDaemon, one versioned grant from the existing issuer, max_chunk_us a target proven by traces, one LearnPort cutover with typed Unavailable, L3 validators

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply
joelteply merged commit 1291cb3 into canary Nov 18, 2026
10 checks passed
@joelteply
joelteply deleted the fable/in-process-learning-design branch November 18, 2026 20:05
joelteply added a commit that referenced this pull request Nov 18, 2026
…ing L1) (#6148)

* llama: Learner over the fork's resumable llama_opt_learn_* (in-process learning L1, no policy)

A reply trains on one live sequence of a serving context, one chunk per step, then restores her
served tail under her stable set. The run owns its admission and optimizer parameters in one boxed
binding the native callbacks reach, from shadow selection until a confirmed restoration, and hands
the admission back either way (Finished::Learned / Failed, Refused before it began). finish() is
explicit and fallible (drain, then step until restored); dropping an unfinished run drains it and
records the custody loss on its Learner, which refuses the next reply until the owner takes it.
ReplyAdmission: optimizer_buffer (an increment, exact aligned size) and workspace (validates a lease
already held, never takes one, so a repeated measurement is never charged twice). Typed refusals in
Rust; buffer-type names and bools only at the FFI boundary; a panicking admission refuses instead of
unwinding into native code. Design: docs/architecture/IN-PROCESS-LEARNING.md (#6147).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: the whole-window horizon is llama.h's 0 at the boundary, named, never a map_or (unknown_as_value 216 -> 215)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: the context owns the callback holder; a failure is terminal and blocks until fenced (Codex on #6148)

- Native code keeps the callback pointers past a reply (llama-context-opt.cpp :192-193, :275-276,
  :330-331, :365-366), so ONE CallbackHolder lives on Context (declared after ptr, freed after
  llama_free). A reply places its admission there and takes it back; an empty holder refuses.
- Lengths and the result are validated before llama_opt_init.
- RunState {Running, Restored, Failed(refusal)}: a failed step stays failed; step() never calls
  native past a terminal status; finish() returns Failed and blocks the learner.
- Lost custody keeps a dropped run's admission (bytes retained); reading it never clears it; only
  Learner::fence (memory_seq_rm of the sequence) does, handing that admission back.
- Tests: failure-after-step through later steps and finish, terminal restoration, horizon encoding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: quarantine, shadow and model live on the Context; recovery proves native release (Codex on #6148)

- The quarantine lives in the context's CallbackHolder, not the temporary Learner: a new Learner on
  the same context is refused while it holds, and the failed run's admission (bytes retained)
  stays in it. Finished::Failed no longer hands the admission back.
- Learner::recover() lifts it only after llama_opt_release_workspace (workspace replaced) and
  llama_opt_release(shadow) (no optimizer references it) both succeed, then hands back the held
  admission. It no longer clears the sequence: memory_seq_rm proved nothing about optimizer memory
  and destroyed recoverable state; the owner restores her sequence from its own preserved state.
- The trained shadow is an Arc<LoraAdapter> the holder keeps until a release proves no optimizer
  references it; the model is the context's own (Context::model), not an independent reference.
- Test: a quarantine keeps the failed run's admission and the holder admits nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: quarantine_after borrows the refusal (contract_shape 2188 -> 2187: no clone passed into a call)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: every shadow, with its own optimizer lease, retained on the context until a confirmed release (Codex on #6148)

Native code keeps one optimizer per adapter across shadow switches (llama-context-opt.cpp
opt_minds), so the holder kept only the current Arc and a switch or a refused selection could drop
an adapter its optimizer still references. Now:
- OptimizerLease (per shadow, its buffers' increments) is split from ReplyAdmission (per reply,
  workspace lease validation): optimizer residency is never charged to, or released with, a reply.
- Learner::retain_shadow(Arc<LoraAdapter>, lease) keeps the adapter and its lease in the context's
  Shadows registry; begin() refuses a shadow not retained and only marks it selected;
  release_shadow() hands the lease back only after llama_opt_release confirms no optimizer
  references the adapter. recover() releases the workspace only.
- Test: A -> B -> A and a refused B selection keep both shadows and A's leased bytes; only a
  release takes one out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: a restored reply is Learned only when her shadow's own count rose (Codex on #6148)

A begin then an immediate finish drains before any gradient, and a confirmed restoration was
returned as Learned with zero committed updates. finish now reads llama_opt_shadow_steps for her
shadow before begin and after restoration, and llama_opt_shadow_quarantined (as
server-context-learn.inc:582-589 does):
- Learned { steps: NonZeroU64, loss } only for actual commits (a drain after a commit keeps them);
- Unchanged when nothing committed, with no loss;
- Uncertain { steps } when native code marks the shadow's effects uncertain.

Recovery order made explicit: opt_release_workspace refuses while any shadow keeps an optimizer
(opt_configured), so recover() refuses with the retained count, before any native call and keeping
the quarantine and every retained shadow and lease, until each is released through release_shadow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: needs_gradient takes &mut self, as Context::as_ptr does (E0596, Codex on #6148)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: begin refuses a pooled context before any native state (Codex's BigMama replay)

ContextParams::default() pools (Mean, for embeddings); a reply trains on per-token logits, and native
code refused only at the first labelled step. begin now reads llama_pooling_type and refuses with
step "context" (the admission handed back) unless it is None. The persona scheduler's context
already sets PoolingType::None (driver.rs:154 on #6140).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
joelteply added a commit that referenced this pull request Nov 18, 2026
…, never increments (L2 accounting; stacked on #6148) (#6149)

* llama: Learner over the fork's resumable llama_opt_learn_* (in-process learning L1, no policy)

A reply trains on one live sequence of a serving context, one chunk per step, then restores her
served tail under her stable set. The run owns its admission and optimizer parameters in one boxed
binding the native callbacks reach, from shadow selection until a confirmed restoration, and hands
the admission back either way (Finished::Learned / Failed, Refused before it began). finish() is
explicit and fallible (drain, then step until restored); dropping an unfinished run drains it and
records the custody loss on its Learner, which refuses the next reply until the owner takes it.
ReplyAdmission: optimizer_buffer (an increment, exact aligned size) and workspace (validates a lease
already held, never takes one, so a repeated measurement is never charged twice). Typed refusals in
Rust; buffer-type names and bools only at the FFI boundary; a panicking admission refuses instead of
unwinding into native code. Design: docs/architecture/IN-PROCESS-LEARNING.md (#6147).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: the whole-window horizon is llama.h's 0 at the boundary, named, never a map_or (unknown_as_value 216 -> 215)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: the context owns the callback holder; a failure is terminal and blocks until fenced (Codex on #6148)

- Native code keeps the callback pointers past a reply (llama-context-opt.cpp :192-193, :275-276,
  :330-331, :365-366), so ONE CallbackHolder lives on Context (declared after ptr, freed after
  llama_free). A reply places its admission there and takes it back; an empty holder refuses.
- Lengths and the result are validated before llama_opt_init.
- RunState {Running, Restored, Failed(refusal)}: a failed step stays failed; step() never calls
  native past a terminal status; finish() returns Failed and blocks the learner.
- Lost custody keeps a dropped run's admission (bytes retained); reading it never clears it; only
  Learner::fence (memory_seq_rm of the sequence) does, handing that admission back.
- Tests: failure-after-step through later steps and finish, terminal restoration, horizon encoding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: quarantine, shadow and model live on the Context; recovery proves native release (Codex on #6148)

- The quarantine lives in the context's CallbackHolder, not the temporary Learner: a new Learner on
  the same context is refused while it holds, and the failed run's admission (bytes retained)
  stays in it. Finished::Failed no longer hands the admission back.
- Learner::recover() lifts it only after llama_opt_release_workspace (workspace replaced) and
  llama_opt_release(shadow) (no optimizer references it) both succeed, then hands back the held
  admission. It no longer clears the sequence: memory_seq_rm proved nothing about optimizer memory
  and destroyed recoverable state; the owner restores her sequence from its own preserved state.
- The trained shadow is an Arc<LoraAdapter> the holder keeps until a release proves no optimizer
  references it; the model is the context's own (Context::model), not an independent reference.
- Test: a quarantine keeps the failed run's admission and the holder admits nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: quarantine_after borrows the refusal (contract_shape 2188 -> 2187: no clone passed into a call)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: every shadow, with its own optimizer lease, retained on the context until a confirmed release (Codex on #6148)

Native code keeps one optimizer per adapter across shadow switches (llama-context-opt.cpp
opt_minds), so the holder kept only the current Arc and a switch or a refused selection could drop
an adapter its optimizer still references. Now:
- OptimizerLease (per shadow, its buffers' increments) is split from ReplyAdmission (per reply,
  workspace lease validation): optimizer residency is never charged to, or released with, a reply.
- Learner::retain_shadow(Arc<LoraAdapter>, lease) keeps the adapter and its lease in the context's
  Shadows registry; begin() refuses a shadow not retained and only marks it selected;
  release_shadow() hands the lease back only after llama_opt_release confirms no optimizer
  references the adapter. recover() releases the workspace only.
- Test: A -> B -> A and a refused B selection keep both shadows and A's leased bytes; only a
  release takes one out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: a restored reply is Learned only when her shadow's own count rose (Codex on #6148)

A begin then an immediate finish drains before any gradient, and a confirmed restoration was
returned as Learned with zero committed updates. finish now reads llama_opt_shadow_steps for her
shadow before begin and after restoration, and llama_opt_shadow_quarantined (as
server-context-learn.inc:582-589 does):
- Learned { steps: NonZeroU64, loss } only for actual commits (a drain after a commit keeps them);
- Unchanged when nothing committed, with no loss;
- Uncertain { steps } when native code marks the shadow's effects uncertain.

Recovery order made explicit: opt_release_workspace refuses while any shadow keeps an optimizer
(opt_configured), so recover() refuses with the retained count, before any native call and keeping
the quarantine and every retained shadow and lease, until each is released through release_shadow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: needs_gradient takes &mut self, as Context::as_ptr does (E0596, Codex on #6148)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: begin refuses a pooled context before any native state (Codex's BigMama replay)

ContextParams::default() pools (Mean, for embeddings); a reply trains on per-token logits, and native
code refused only at the first labelled step. begin now reads llama_pooling_type and refuses with
step "context" (the admission handed back) unless it is None. The persona scheduler's context
already sets PoolingType::None (driver.rs:154 on #6140).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: a shadow's optimizer buffers are slots native code sizes, never increments (Codex's A/B/A trace)

ggml_opt_static_allocation_allowed (ggml-opt.cpp:558-565 at the pin) passes the WHOLE size of the
buffer it is about to allocate, and the 32-byte optimizer_host buffer is freed (:627) and asked
for again (:908) on every init, so the old increment contract charged a revisited shadow again
for memory that was already gone.

New safe/optimizer_slots.rs (pure, tested without a model):
- Role::{Static, Host}, parsed from the two native names; any other name is refused.
- Slot { role, device: BufferType }: the boundary type. BufferType gains device() and is_host().
- OptimizerLease { admit(slot, bytes), release(slot, bytes) }: told growth and proven frees.
- Slots per retained shadow: a request means 'this slot is about to be N'; the growth is
  admitted before native allocates, a shrink is released, and a revisit at the same size charges
  nothing. A refused host request releases the host bytes (already freed). A refusal later in the
  same init rolls back that init's earlier charges: every request precedes every allocation
  (:906-908, then :912/:921).
- learn.rs: the callback charges the selected shadow's slots; begin settles them after
  llama_opt_init; release_shadow releases every slot once llama_opt_release confirms.

Tests: a revisit at the same sizes charges nothing new (270368 + 32 once); a host refusal rolls
back the same init's static charge; a refused host revisit releases the freed host and keeps the
static; a shrink releases the difference and a release frees every slot; only the two native
names have a role.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: restore learn.rs's first doc line (a bad multi-line edit pasted a test into it; Codex on #6149)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: optimizer charges are held until native code's own buffers prove what survived (Codex on #6149)

Requests come from llama_opt_init and from the graph builds inside learn steps, so inferring which
requests form one group (the settle/rollback in the last head) was fragile. Now:
- Slots::ask admits only growth past what is charged and holds it; nothing is released on
  inference.
- After each optimizer operation (llama_opt_init, every learn step and graph step, and a confirmed
  release_shadow), never on ordinary decode, the Learner visits llama_opt_visit_memory on the
  context's owner and reconciles every retained shadow's slots to the bytes native code owns
  (ggml_opt_visit_owned_buffers names only optimizer_static and optimizer_host). Charged but not
  resident is released.
- Resident but never admitted, an unknown kind, or bytes owned for a shadow the context does not
  retain is an accounting defect: Learner::accounting() names it and begin() refuses until an
  owner looks.
- settle, the per-init 'before' journal and the inferred rollback are deleted.

Tests: the revisit charges nothing new; a refused group is released only when the visit proves
nothing survived; a refused host revisit releases the freed host and keeps the static; a proven
shrink releases the difference; resident bytes never admitted are a defect; a visit attributes by
shadow and role or names the defect.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* llama learn: a release never exceeds what was admitted; an accounting defect stops the run and is never Learned (Codex on #6149 at 69c07dd)

- Slots::reconcile kept native excess in the charge (charged = resident), so admit 32, observe 64,
  free to 0 released 64 for a lease granted 32. Now the charge stays what was admitted; bytes
  resident beyond it are reported as Unadmitted on each visit and never released, through
  reconcile and release_all alike.
- An accounting defect found after a learn step or graph step drains the reply (no further
  gradient; only the restoration runs), and finish reports Uncertain, never Learned, while
  accounting is untrusted.
- The visitor no longer reads a size past u64 as u64::MAX (unknown_as_value): it is an
  unattributable buffer, so an accounting defect.
- contract_shape: the visitor's seven parameters are llama.cpp's fixed callback, so one existing
  site is retired to offset it: mtmd eval_media's text, bytes and kind travel together as one Media.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant