diff --git a/docs/architecture/FLOW-IMPLEMENTATION-PLAN.md b/docs/architecture/FLOW-IMPLEMENTATION-PLAN.md new file mode 100644 index 0000000000..404aa3a07e --- /dev/null +++ b/docs/architecture/FLOW-IMPLEMENTATION-PLAN.md @@ -0,0 +1,401 @@ +# Flow implementation plan: making the concurrent mind real (2026-10-28) + +> The design already exists. [CONCURRENT-MIND-AND-GOVERNOR](CONCURRENT-MIND-AND-GOVERNOR.md) §1 and +> §3.1-3.3 and [SUBSTRATE-DOCTRINE-ORGANIC-FLOW](SUBSTRATE-DOCTRINE-ORGANIC-FLOW.md) ("Flow not RPC", +> "Demand not FIFO") are precedence-winning. This plan implements them against the code as it is, +> using a measured inventory of every place that queues or gates work (appendix, +> [FLOW-INVENTORY-2026-10-28](../planning/FLOW-INVENTORY-2026-10-28.md)). The code violated the +> doctrine in 160 places, and the gap between the two is the slowness. +> +> Joel, 2026-10-28: "Its got to flow or have MANY queues"; "Semaphore really only exists for queue +> inserts and signals"; "You can't let inference pile up"; "I'd always take the most recent"; +> "Think of live conversations"; "Simulate all cases". + +## 1. What the measurements say (M5, Qwen3.6-35B-A3B, 4 residents) + +- **The engine is not slow.** + - Our server matches stock `llama-bench` at 64k context: prefill 322 vs 304 tok/s, decode 30.5 vs 29.5. + - Four streams decode 101 tok/s together, 3.4x one stream. + - The engine was occupied 99.6% of the wall clock. +- **Minds waited on gates in front of it.** A 155 s median act held about 50 s of permit waits: + - a reserved directed lane that was never lent, a median 25.7 s; + - a prefill throttle guarding a spike one engine never draws, a median 24.6 s. + - Removing both: act median 155 s -> 80 s, around-model 70 s -> 21 s (4579b4711, live). +- **Removing the next layer without a plan regressed** (90ed24a22, rolled back): + - the in-flight gauge went dark; + - directed priority vanished; + - the wait moved inside the model call (slot wait median 17.2 s, because a reserved scratch slot served 6 of 400 requests). + - Removing scratch would have switched warm-ahead off (the test caught it). + + The pieces are coupled. They move together, under one design, with a simulator. +- **Context length is the largest per-token cost.** Stock prefill goes 968 -> 304 tok/s and decode 59 -> 29 tok/s from 0 to 64k. The resident median prompt is 82k, and an epoch turnover re-prefills about 65k tokens. + +## 2. The model (each line traces to the doctrine) + +1. **Content and signals are different things.** + - Content (chat lines, tool results, perception) appends to the mind's stream in the context codec (`continuum-context`, positron), which consolidates it: superseded items collapse, older ones fold to handles, the newest stays whole (§3.3 ChannelDigest). + - What flows to the scheduler is only a coalescing signal per mind: "she has fresh input at class X". Twenty lines while she is busy are one wakeup. + - Distinct committed inputs and tool results are never dropped. They are content, kept by the codec (Codex). +2. **Govern resources, never minds** (invariant 1). A mind always may want. The flow governs only capacities: + - engine compute (decode/prefill share); + - KV memory; + - training memory; + - CPU workers; + - external APIs. +3. **One authority per capacity, consumed by every caller** (Codex: "consume one resource authority rather than nesting independent capacity promises"). Memory is admission: `MindMemory::seat` for KV, the training lease for training. Compute is the preemptible share inside it. +4. **Quality-of-service classes**, GCD-style: + - **interactive**: someone waits on the answer; + - **work**: her act on her card; + - **utility**: ambient thought, perception enrichers, gene exams; + - **background**: dreams, training, consolidation. +5. **Each owner is a serial queue** that targets a class. Her newer signal supersedes her waiting one and keeps her place. Utility and background queues are bounded and shed their oldest. Nothing piles up (Joel; "Backpressure is intrinsic"). +6. **One event-driven dispatcher per capacity**, the `continuum-flow` primitive. + - It owns its ready set, wakes on events (submitted, freed, page landed, aging instant), and never polls. + - It is a pure scheduler (decisions, testable in microseconds) plus a thin actor. +7. **Two preemption verbs** (Cormac): *yield compute* (stop at a decode step or batch boundary, keep memory) and *release memory* (page out or stop). A mind's act yields compute by finishing its step and pages out only if memory is wanted. A training job pauses at a batch boundary and keeps its graph. +8. **Aging bounds every class's worst-case wait** (Cormac: a paused training job has no guarantee it ever runs). + - Each class has a stated maximum wait, which the simulator proves and the live read measures. + - Under permanent interactive saturation (Codex), work still gets a measured minimum share. Interactive cannot starve a mind's own act forever. +9. **Late binding, captured at send** (Cormac). + - The prompt is rendered when the grant arrives, so she reads the newest. + - The recorder captures the bytes actually sent, because learning (L1) trains on captured turns. + - Settle still writes the room turn. +10. **Prefill is an enricher** (§3.1). + - The context is append-only within an epoch, so appended items are prefilled as they arrive: during a tool call, during a person's speech. + - At grant time only the newest fragment remains. + - This is how a live reply starts within its budget. +11. **Waits on predicates, not re-arm loops** (Cormac). "A lane serving base X is ready", "Y bytes of memory free" are conditions the dispatcher evaluates on the events that can change them. The examiner's NotNow and CapacityParked become such waits. +12. **No second scheduler.** The fork's training yield windows, TrainingHolds-as-scheduler, the reserved lanes, the prefill throttle and per-module `max_concurrency` limiters are deleted. The engine executes what the flow decides. Pause stays as an operator override. + +## 2a. Stages are short: CBAR's `QueueThread`, in Rust + +Joel, 2026-10-28: "Your goal is for modules to make this stuff trivial like cbar's pipeline/*.cpp stages. +They're short." The reference is `cb-mobile-sdk/cpp/utility/Threads.h` `QueueThread` and +`cbar/CBAR_VideoThread` (default `queueSize = 1`): +- `addItem` locks only to pop the oldest until there is room, push the newest, and signal. The + lock exists for the insert and the signal, nothing else, and the newest item always wins. +- Each stage sets a priority, declares its cadence (`needsRealTime`, `videoOnly`), fetches + dependencies by type (`getAnalyzerOfType()`), and implements one handler. `CBP_ElevationAnalyzer` + is 71 lines, most of them boilerplate. + +`continuum-flow` therefore ships a **`Stage`** alongside the capacity dispatcher: + +```rust +pub trait Stage: Send + 'static { + type Input: Send; // what wakes it (a signal, a digest handle, a frame) + const QOS: Qos; // its class + const DEPTH: usize = 1; // bounded queue; when full, the oldest is dropped (keep-latest) + async fn handle(&mut self, input: Self::Input); +} +// spawn_stage(stage) -> StageInput: push() inserts, drops the oldest past DEPTH, and signals. +// A stage that needs a shared capacity (the engine, a TTS model) submits to that capacity's Flow. +``` + +The module owns every concern that today is copied into each subsystem: +- the queue and its bound; +- keep-latest and coalescing; +- wake-on-signal (no polling); +- class and aging; +- yield; +- shutdown drain; +- probes. + +A stage is its handler and nothing else. **The rule for every migration in §5:** the migrated code +holds no lock across an await, no timer, no backoff, no limiter and no queue of its own. The receipt +for each step is lines removed versus added, per subsystem, with stage files measured like CBAR's (most +under ~100 lines). + +## 2b. Scheduling math, episodes, and learning the policy (Joel, 2026-10-28) + +> "Isn't there math for this? … a traffic light problem with clusters of cars … never leaving +> someone in the left hand turn lane for too long (never a threshold) or just reinforcement +> learning … the things you're measuring as scoring." "Usually you want groupings like code reads +> and edits … like a group of semis." "Just realize this is general for other asks." + +**The index, not thresholds: the generalized cμ rule.** +- Each class has a delay cost that grows with waiting, `C_i(w)`: steep for a live reply, shallow for + a dream. +- When capacity frees, serve the waiter with the largest `μ_i · C_i'(w_i)`, where `μ_i` is + 1 / predicted service time. This is optimal for convex delay costs in heavy traffic; the traffic-light + form is max-pressure control. +- Priority rises smoothly with the wait, so nobody is stuck in the turn lane, and there is no class bump + at a quantum (`continuum-flow`'s current step aging is that threshold and is replaced). + +**Predicted service time.** A request's cost is known before it runs: +- prefill = tokens to prefill ÷ the engine's rate at that depth; +- decode = the owner's expected reply length ÷ the per-stream rate. + +These come from the same calibrated curve the simulator uses. The scheduler sees that a 2k reply costs +~3 s and a turnover ~60 s and orders them. The head-of-line blocking fork #69 fixes inside the engine is +this same mistake. + +**Episodes: a concern's working set travels as a convoy and is cleared at its close.** +- Nothing is independent. A concern (a code change, a research thread, a document, a game, a + conversation thread, a media session) needs a working set loaded for several steps. +- Loading it is a setup cost (the prefill of those reads). Interleaving or preemption pays it again on + every return. +- **Boundaries come from the activity's own events:** + - a card claimed or done, a commit, tests green after edits; + - files or sources that no longer overlap; + - a topic change in the room, the recipe step's completion, or her own words. +- **Each item an act adds is tagged with its episode.** At close, the episode's bulky items collapse to + their outline level plus a handle, while conclusions (decision, commit, result) stay whole. That is a + planned turnover at a natural seam, its re-prefill scheduled while she runs a tool or idles (prefill + as an enricher), so forced turnovers at the window limit mostly stop. +- **The flow schedules episodes as the unit.** + - Affinity to one slot. + - Preemption charges the switch's setup cost in the index: a dream never breaks a convoy; a live reply + may, paying ~0.1 s with RAM paging. + - MindMemory pages an episode's KV as a unit. +- **An expand of a collapsed handle is the measured cost of collapsing too much.** Its rate feeds the + epoch-start optimum below, so the policy tunes itself from her behavior. + +**The epoch start has a closed form.** +- With grounding floor `g` (re-prefilled every turnover; measured 12-20k tokens on the dyno), start `s`, + window `W` and appends per act `a`, turnover prefill per act is `(g + s) · a / (W − g − s)`. That always + favors a small `s`. +- Against it stands the expansion cost of what a small start collapses. +- Minimizing the sum gives `s*` from measured `g`, `a` and the expand rate, per resident. The dyno already + shows the direction: a 1/4 start beat 1/2 by 10-25% prefill at 48k. +- The grounding floor `g` is the next target: blocks that rarely change should not re-prefill on every + turnover. + +**Learning the policy.** +- The reward is what we already measure: wait between acts, first-token latency for live and directed + calls, aggregate tokens per second, a penalty on any class's worst wait, and expands of collapsed + handles. +- The simulator is the training environment (thousands of simulated hours per minute); the dyno replays + her real traces; the live read validates. +- Policy parameters: the cμ cost slopes, the start-share model, the episode-boundary detector. Learning + starts from the math above, never from nothing (plan line 14, "learned policies on our own receipts"). + +## 2c. Corrections from review (Codex and Cormac on 6d18b4564) + +1. **Signals and reliable inputs are different types** (Codex). + - A *signal* stage keeps the newest and may drop older entries, because its input only *references* + content already committed to its owner (the codec, a store). + - A *reliable* stage never drops: when full, the producer gets backpressure (an `Err` it must handle, + or an awaited send), and a restart recovers durable work before re-subscribing. + - `continuum-flow::Stage` encodes this as its overflow policy, so a tool result can never ride a + dropping queue. Fixture: a full queue receiving distinct tool results and a cancellation keeps every + committed result reachable; only wake signals collapse. +2. **A bounded wait for background needs an explicit share** (Codex). + - "Training completes within a bounded wait" and "work is never preempted for it" cannot both hold + while work is always ready. Rising priority alone does not bound the wait. + - So background has a measured minimum service share, granted at its own batch and step boundaries. + It is non-preemptible for at most one batch, whose duration is measured. + - The bound holds under a stated admitted load. Simulator case: continuous work plus continuous + interactive arrivals, and background still meets its bound. + - Joel's "a dream still happens irregularly and doesn't eat into work" is this minimum share, sized + from measurement, not a guess. +3. **Every bound names its measurement** (Cormac). The stated maximum waits in §6 come from receipts or + replays, never from someone picking a constant. A simulator that proves a guessed number proves + nothing. +4. **Training's cost is forward plus trainable tokens** (Cormac; card 32cc831f: 48 trainable of 146,044 + tokens held a node 3.3 h), so the background share buys lessons, not re-reads. +5. **The fork keeps its batch-boundary stop** and loses only its yield *policy* (`share_ppm`, + `max_slowdown_ppm`). The engine executes; the flow decides. +6. **The admission and pager seams** (Codex): + - one physical-memory ledger backs both KV and training leases, and a compute yield keeps them; + - subscribe before checking a predicate, and recheck at grant; + - speculative pages and prefill are keyed by context generation, so a stale completion never + replaces newer state; + - an uncertain external effect is never replayed because its wake signal was coalesced. +7. **Inventory additions** (Cormac): + - the persisted pause holds (`~/.continuum/state/training-holds.json`); + - `CapacityParked` (`job_create.rs:116`); + - partition parking (#5557). + + All become §2.11 predicate waits. Codex: `TrainingTriggerModule` has no event subscriptions and a + 30 s tick, while `ResourceDaemon::subscribe` already exposes board changes; the all-parked partition + path (a failed prepared job) is distinct from `DispatchPhase::Parked`, so each gets its own fixture + before the timer goes. + +## 2d. Focus: the flexbox budget, with full compliance (Joel, 2026-10-28) + +The owner of this contract is [FOCUS-CONTRACT.md](FOCUS-CONTRACT.md); this section is the flow plan's view of it, and where the two differ, FOCUS-CONTRACT wins. + +> "We called this flexbox budgeting." "Ideally it even helps us shrink down during what we've seen the +> coding models excel at, like as it starts to develop it's not a cluttered mind." "In human terms this +> is focus." "Others shrink. She still has all of the context. Requires adherence." + +**The rule** (already law; saved 2026-10-18; `PERSONA-COGNITION-PIPELINE.md`: +`FlexboxRagBudgetAdapter`/`flex_allocate` is the only budgeter): +- Every part of a turn is a claim in **one** flexbox allocation per turn, over one window: + - framing, tools, required input, conversation, results/actions, grounding, recall, working memory; + - the reply. +- Each claim has a priority from the turn's intent, a floor of real need, and a want of its whole. +- Sources deliver *levels* (whole, summary, pointer), never a size. + +**Focus is a brain process, and it is hers** (Joel: "I don't choose my focus, my mind just does"; +"it can still be 'her' but in reality it's a brain process"). The priorities select themselves: +- **Recent use.** Items her own acts touched (files read and edited, tests run, the card she's on) are + the open episode's working set and take the high claims. Untouched items decay toward summary, then + pointer. +- **Salience.** The amygdala's outlier detector (being addressed, affect, a sudden change) raises a + claim on its own. A directed line also takes the interactive class. +- **Relevance.** Recall ranks by nearness to her current thought, so related engrams surface at summary + level. +- **`cognition/focus.rs` `FocusState`** (her self-set level, thread cursor and mutes) is an override, not + a requirement. A recipe's preset is where the process starts. Nothing is computed *for* her: it is her + own activity, reflected. + +**What focus does to the view.** +- The current episode's working set gets the high claims. +- Everything else shrinks to summary or pointer: still in her view, reachable on demand, never clipped. +- She keeps all of her context; only its level changes. +- A directed line raises its claim and the flow's interactive class; the episode stays warm and resumes. + +**Measured non-compliance, as of 2026-10-28** (`fable/working-window`; the full map is on card 23ad5675). Since then #5564 (one allocation sizes her prompt and every reader complies) and #5573 (results storage never decides what she sees) replaced two items below: the results share (`collapse_results_over_cap`) and the tools' fixed window/4 claim (now the core hands' floor); read this as the 10-28 baseline, not the current state: +~40 production sites size prompt content outside the allocation. +- **Conversation** runs a second flexbox in `compose_for_turn` over the 159k served window, plus a + per-turn cap and head cuts. +- **Results and actions:** `collapse_results_over_cap`, `latest_action_chars`, `split_action_result` + and `RESULT_SHARE_DENOM`. +- **Grounding** budget is frozen at spawn from the affordable window; doctrine, map and wall head cuts; + board 12/5 caps. +- **Tools** take window/4 as a fixed claim. +- **Recall:** count tiers, frozen at spawn. +- **Dispatch and tool-output** slices, echo and trail heads. +- **A per-call cache credit** swung `ContextBudget` from 22k to 159k between calls + (`cognition.budget.prefill_bound` fired 739 times in an hour). +- My 48k working window on `prompt_fit` alone was another non-compliant patch: the prompt fit 48k while + the sources sized for 159k, and Aiko turned over every act. + +**The change, shipped as one:** +1. The `prompt_self_sizing` ratchet first (~40). It counts `ContextBudget::live|live_or_floor|from_window*`, + the fraction methods, `_SHARE_DENOM`, and any `flex_allocate`/`FlexboxRagBudgetAdapter` outside the + one allocation. +2. Migrate by prompt mass: conversation (one allocation, `compose_for_turn` delivers items), results, + grounding, tools, recall, small sites. Delete dead code. +3. RAM and disk limits become a named storage budget that never decides what she sees. +4. The total window is one parameter of the allocation: the dyno's measured knee. +5. Ship when the ratchet reads 0, all cognition tests pass, and the dyno on all four residents shows + prefill per request no worse with zero handles dropped. Then the fraction methods go private, and + the compiler holds the line. + +## 3. Live conversation, end to end + +The budget: first audio within about 1 s of the speaker stopping. The path and its timestamps (Codex's list, each a probe): + +1. ingress (a word, a line); +2. mind wake; +3. context ready; +4. admission queued and granted; +5. page restored; +6. engine first token; +7. delivery (TTS first audio, UI first character). + +What the plan does at each point: +- **Ingress and wake.** Streaming STT partials are perception events; her signal is interactive at once. + - The STT Semaphore(2) that *drops* a third utterance becomes a queue. + - The single Whisper mutex and the one mutex per TTS model become capacities in the flow, with interactive class (inventory #124-126). +- **Context ready.** The codec appends partials as they arrive, and prefill follows them. +- **Admission.** A mind in a call is seated with its KV pinned; nothing pages it out mid-call. Interactive class preempts lower classes' compute at the next decode step. +- **Barge-in.** A new utterance during her reply raises the yield on her generation. The stream is dropped, and what she said so far stays in her context. +- **Delivery.** + - The 250 ms token flush timer and the positron `min_interval` sleep become event-driven: flush on sentence boundary or first token (#95, #159). + - The call join's fixed sleeps go (#132). +- **Several people.** Every utterance is perception. Whether she speaks is her decision, one coalesced signal per mind. + +## 4. Inventory mapping (160 places; full list in the appendix) + +| category | count | what happens | +|---|---|---| +| A: belongs in the flow | 74 | migrates to the capacity it protects; its local semaphore/heap/backoff is deleted | +| B: content for the codec | 13 | becomes a codec source or consolidation (rings, dedup, digests) | +| C: real locks on shared data | 28 | stay; any held across an await on a cognition path is narrowed (#79, #78) | +| D: polling loops | 34 | become event-driven (watch/notify on the thing that changes) | +| E: dead or duplicate | 11 | deleted, including the kernel's ConcurrentPriorityQueue, inference-grpc's unused priority queue, the unused TokioConcurrencyPolicy | + +The top live-latency items, in order: +1. the serial per-mind loop with no preemption (a directed line waits for a whole self-turn); +2. the per-mind act gate; +3. the classless adapter semaphore; +4. slot wait and page IO when minds outnumber slots; +5. the relaunch drain barrier; +6. the adapter registry RwLock held across generation; +7. the voice path's drops and mutexes; +8. RAG work inline on the turn path; +9. remote minds' refusal and 2 s reply poll; +10. token flush timers. + +## 5. Order of work (each step: simulator cases pass, dyno replay no worse, 15-minute live read no worse) + +| step | what | owner | proves | +|---|---|---|---| +| 0 | Hold #5558 commits 3-5; keep 4579b4711 live | Fable | done 14:20Z | +| 1 | `continuum-flow` primitive + **simulator** (pure scheduler, fake engine, virtual clock; §6 cases) | Fable | every case in §6 green in seconds | +| 2 | Engine compute as the first capacity: one dispatcher replaces lanes, adapter semaphore and the act gate; in-flight gauge and directed class move into it; warm-ahead becomes a utility-class use of free capacity (no reserved scratch slot) | Fable (dispatcher), Codex (admission/allocator/pager seams) | sim: directed waits < one decode step, gauge exact, warm-ahead intact; live: slot wait, act median | +| 3 | KV memory admission: `MindMemory::seat` + RAM paging (fork #79, #70) as the release-memory verb | Fable, Codex | sim: no resident overwritten unsaved; live: switch freeze ~0.1 s | +| 4 | Training and examiner as consumers: compute yield at batch boundary, memory lease kept; fork yield windows and TrainingHolds-as-scheduler deleted; S7 stop-drain fixture (920619c5) | Cormac | sim: training's worst-case wait bounded; live: resident decode during training | +| 5 | The mind's stages on the flow: coalesced signal per mind, codec consolidation, late render with capture at send, prefill as enricher | Fable | sim: 20-line burst = one wake, newest read; live: act median, prefill per act | +| 6 | Live path: STT/TTS capacities, barge-in yield, event-driven flush, call-join sleeps gone | Fable + BigMama (live) | sim: first token ≤ 1 s after silence; live: the seven timestamps | +| 7 | The rest of category A by subsystem; D polls to events; E deleted | each owner by area | per-subsystem receipts, lines removed | + +## 6. The simulator: every case, deterministic, in seconds + +Pure scheduler + MindMemory + a fake engine on a virtual clock. Each case states its pass rule. + +| case | must hold | +|---|---| +| 4 residents, 4 slots, all working | no waits; every slot busy | +| 6 residents, 4 slots | each class within its stated max wait; pages swap without loss | +| directed call mid-act, engine full | lowest class yields within one decode step; first-token time recorded | +| live call: streaming partials, speaker stops | prefill keeps pace; first token ≤ budget after silence | +| barge-in during her reply | her generation yields; her context keeps what she said | +| 20 chat lines to one busy mind | one coalesced wake; she reads all 20 consolidated; no stale request runs | +| permanent interactive saturation | work still gets its minimum share (Codex) | +| dream + training under steady work | both complete within their stated worst-case wait; work never preempted for them (Cormac) | +| training holds memory, needs compute | yields compute at batch boundary, keeps memory; never deadlocks memory-vs-compute (Cormac) | +| warm-ahead with no reserved slot | next mind's page restored ahead; no resident overwritten | +| engine relaunch mid-act | in-flight work re-queued or failed cleanly; pages intact | +| capacity shrinks 4 -> 2 | no new starts past capacity; running work finishes | +| KV memory full | admission pages by class; never an OOM | +| remote mind leased from another node | same classes, remote capacity; lost reply recovered by event, not poll | +| relaunch drain | new turns not blocked behind the longest in-flight turn (inventory #5) | + +The simulator reports what the live read reports: each resident's wait between acts, first-token latency +for directed and live calls, switch freeze, aggregate tokens per second, and each class's worst wait. + +## 6a. The dyno and the simulator: how to use them (both proved their worth on 2026-10-28) + +Answer "will this be faster?" here before touching the live node. Live reads under load are road +tests in traffic. + +**The dyno** replays each resident's real captured prompts through her view, offline, with serving +down (no residents involved): +- Captures: `~/.continuum/fixtures/prompt-captures/.jsonl`, plus `.prev.jsonl` for the + previous segment. Resident ids come from the core log, never from memory. +- Run: + `cargo run --release -p continuum-context --example replay -- --append-tail [--window ] [--generational] ` +- Read the `TABLE` lines: prefill tokens and seconds per request, turnovers, re-prefill per turnover, + and handles dropped. A change that drops handles loses ability and is out. +- Receipt that set the working window: 48k cut prefill per request about 3x for all four residents + with zero handles dropped; 32k doubled turnovers. Evidence: + `~/.continuum/evidence/2026-10-28-working-window/` (`replay_window.sh` is the serving-down + window script: trap and watchdog, restart on any exit). + +**The simulator** (`continuum-flow::sim`) puts scheduling and engine changes on a virtual clock: +- The engine model is calibrated from `llama-bench` at depth and from the measured multi-stream + aggregate. +- It reproduced the live gate removal (simulated 76 -> 57 s, live 122 -> 84 s) before forecasting + anything. +- Run with `cargo test -p continuum-flow --release -- --nocapture`. Each case prints what a live read + reports. + +**Order for any speed change:** first the simulator or dyno forecast, then the dyno on real code, +then the 15-minute live read against the same numbers. + +## 7. What gets deleted when this lands (mandatory: a migration that leaves its old mechanism beside the new one is not done) + +- Lane admission (`resource_admission/lanes.rs`, `progress.rs`, `serving_lane_permit.rs`). +- The adapter concurrency semaphore and its grow/shrink task. +- The scratch reservation. +- The prefill throttle (done). +- The kernel `ConcurrentPriorityQueue`, `TokioConcurrencyPolicy` and `message_processor`. +- `inference-grpc`'s priority queue and worker pool. +- Per-module `max_concurrency` limiters on the typed path. +- The fork's training yield windows, and TrainingHolds as a scheduler. +- The act clock's queue-time accounting. +- The 34 polling loops' timers, wherever an event exists. diff --git a/docs/planning/FLOW-INVENTORY-2026-10-28.md b/docs/planning/FLOW-INVENTORY-2026-10-28.md new file mode 100644 index 0000000000..a8f08639d5 --- /dev/null +++ b/docs/planning/FLOW-INVENTORY-2026-10-28.md @@ -0,0 +1,191 @@ +# Flow inventory: every place that queues, gates or polls (2026-10-28) + +A static read of `core/` at `fable/lanes-flow` 01ffb5ca0. No cargo was run; test-only code is excluded. +Each row gives: location | what it gates or queues | the resource it protects | whether a live conversation waits on it | its category. +Categories: **A** = flow primitive, **B** = context codec, **C** = real lock (keep), **D** = poll to make event-driven, **E** = dead or duplicate. +The plan is [FLOW-IMPLEMENTATION-PLAN](../architecture/FLOW-IMPLEMENTATION-PLAN.md). + +**Found by the read:** +- **The lane admission is dead.** After 7f9226d89, `LanePriority`, the directed reserve, the work ticket and ambient aging (`resource_admission/lanes.rs:399-617`, `progress.rs:171-219`) have no production caller. +- **The in-flight gauge reads 0 forever.** `inflight_model_calls()` was fed only at `lanes.rs:490`. Its readers: `activity_gate.rs:173` (dreams), `placement_reservation.rs:93` and `sse_stream.rs:97/125`. +- **`directed_pending::wait`** (`directed_pending.rs:81`) has no production caller. +- The live node was rolled back to 4579b4711 for these. + +## Inference and serving +- A `mind_gate.rs:28,51` (taken at `turn_admission.rs:280`): one act per mind, FIFO Semaphore(1) | mind sequence | Y +- A `turn_admission.rs:288` + `openai_adapter/runtime.rs:60`: adapter Semaphore(lanes), FIFO, no class | engine lanes | Y +- A `openai_adapter/provider.rs:202-240`: permit grow/shrink task | engine lanes | indirect +- A `slots/engine_generation.rs:83-140`: `admit_when_ready` | engine readiness | Y +- A `slots/engine_generation.rs:149,175,202,216`: RwLock drain barrier (write-preferring) | engine generation | Y +- A `turn_admission.rs:334-371`: park on `pool.released` when all slots are pinned | KV slots | Y +- A `slots/kv_slot_pool.rs:152-232`: lease, evict, retry | KV slots | Y +- C `slots/kv_slot_pool.rs:55,257`: per-slot op Semaphore(1) | one slot's IO | Y +- A `turn_admission.rs:434-466,676-701`: page save/restore before the turn | engine+disk | Y +- A `turn_admission.rs:485-556`: transient/scratch borrow | slot | probes +- A `slots/kv_slot_pool.rs:459,497`: paging JoinSet | disk | N +- C `slots/citizen_slots.rs:243`: single-flight seed | disk | N +- D `llama_server/progress.rs:395-420`: checkpoint wait loop | engine | Y +- A `serving_guard.rs:293`: `await_ready_serving` | lane | Y +- D `lane_send.rs:256-285`: linear backoff on 503 | engine | Y +- A `measured_hold.rs:94,234-289`: background defers while held | GPU | N +- E `openai_adapter/stream.rs:171`: duplicate `should_defer` backstop +- A `resource_admission/lanes.rs:10,384`: ambient permit (skip when busy) | lanes | ambient +- A `resource_admission/lanes.rs:17`: bounded wait for a starving mind | lanes | N +- E `resource_admission/lanes.rs:399-617`, `progress.rs:171-219`: dead QoS/aging (the blueprint) +- E `resource_admission/lanes.rs:512,561`: 1 s missed-wake guard +- E `resource_admission/lanes.rs:31-41` + `start_server.rs:55`: `DIRECTED_PENDING` install +- E `resource_admission/serving_lane_permit.rs:10-44`: `InflightModelCall` (BUG: reads 0) +- C `resource_admission/lanes.rs:107,184,207`: resize accounting +- A `resource_admission/gate.rs:48-125` (`generate_response.rs:101,366`): lease registry, max 4, refuses | lanes | Y +- A `training_admission.rs:76-146`: wait for training memory | VRAM | N +- A `serving_holds.rs:17,77` (`exam_serving.rs:151,163`): holds suppress re-home | lane | indirect +- A `lane_demand.rs:45,102`: quiesce overrides | lane plan | N +- A `backends/llamacpp_scheduler.rs:134,291,429`: in-process batch driver, 1 ms idle sleep | GPU | Y if used +- D `llama_cpp_backend.rs:626`: 2 ms spin +- D `llama_server/readiness.rs:196`, `launch.rs:101`, `snapshot.rs:453`, `adopted_retirement.rs:88`: load/liveness polls +- D `lane_process/kill9.rs:231`, `parse_tasklist_image_name.rs:101,243`: exit polls +- D `single_purpose_lane/memory.rs:116`: 5 s headroom poll +- D `stderr_relay.rs:242`, `follow.rs:58,116`: log follow +- D `airc_remote/transport.rs:279,346`: 2 s reply-recovery poll | remote reply | Y +- C `capacity/gossip.rs:318,349` +- A `coordinator.rs:96`, `coordinator_pool.rs:179`, `inference_coordinator_module.rs:151`: per-module max_concurrency (legacy route) +- A `plasticity/mod.rs:51`: compaction max 1 +- A `embedding/try_neural_embedder.rs:92-117`: single-flight load + 60 s cooldown | embedder | first recall +- D `embedding_cache_max.rs:388`: flush interval +- A `planning/adaptive_throughput.rs:113,168`: `coalesce_by_identity` (the coalescing precedent) +- C `resource_admission/metrics.rs:207,407`, `coordinator.rs:361`, `prefill_knee.rs:174`: sample rings +- D `serving_daemon.rs:64` (5 s), `policy.rs:190`, `reconcile.rs:384-398,719`, `launch.rs:250`: reconcile ticks/cooldowns +- D `serving_daemon/academy_batch.rs:151,378,399`: Mutex + 100 ms polls +- C `serving_daemon/launch.rs:483,503,558`, `serving_daemon.rs:386` + +## Kernel +- E `concurrency/policy.rs:56-95`: `TokioConcurrencyPolicy` (no users) +- E `concurrency/priority_queue.rs:18-39` +- E `concurrency/message_processor.rs:39` +- A `runtime/per_key_gate.rs:109` (`training_trigger.rs:182,192`): per-key serial gate (precedent) +- B `runtime/shared_compute.rs:54`: compute once, share | CPU | Y +- C `paging/pool.rs:419,492`: single-flight loader +- A `runtime/runtime/shutdown_phase.rs:131-134,485,569`: per-module Semaphore(max_concurrency) | legacy route +- E `shutdown_phase.rs:446` + `ai_provider/a_i_provider_module.rs:16`: limiter bypassed by typed commands +- D `runtime/runtime/runtime.rs:189-279`: ServiceModule tick +- D `runtime/daemon.rs:250`: Daemon ticker +- C `runtime/admission_gate.rs` (`turn_ingress.rs:69`): shutdown door +- D `turn_ingress.rs:184` (200 ms), `continuum-mind/src/modules/cognition.rs:246-257` (50 ms): drain polls +- C `runtime/message_bus.rs:90,139` +- A `routing/airc_event_transport.rs:113,313`: bounded delivery 256 | network | Y +- C `routing/probe_router.rs:166` +- A `resources/daemon.rs:71,214,258`: ResourceDaemon | VRAM/RAM | N +- A `resources/broker.rs:41-71,255-266`, `throughput_lease.rs`: per-class throughput lanes +- A `resources/governor.rs:153`: ledger acquire +- D pressure monitors: `memory_pressure/poll_interval.rs:6`, `disk_pressure.rs:448`, `gpu/memory_manager.rs:262`, `gpu/monitor.rs:209`, `gpu/tracker.rs:226`, `absence_watch.rs:41`, `docker_tier_pool.rs:304`, `paging/broker.rs:125` +- D `bounded_command.rs:133,159,225`: try_wait polls +- C `orm/connection_manager.rs:64,179,202` +- A `orm/sqlite/open_connection.rs:79-127`: writer Mutex; round-robin readers block on a busy reader | disk | Y +- A `orm/migration.rs:453`: throttle +- C `identity/hands_gate.rs:52,121-198`; C `hands_lease.rs:12` +- D `pressure_broker_module.rs:129,307` + +## Cognition +- A `llm_deliberation_faculty/admission.rs:44`: pre-gate `defer_while_held` +- A `turn_ledger.rs:11-13,162-186`: starving-mind bounded wait (aging) +- E `directed_pending.rs:35,81`: `wait()` has no caller +- D `activity_gate.rs:53,162`: 1 s gate tick +- A `activity_gate.rs:238-338`: dream cancellation (preemption) +- A `deferred_faculty.rs:81-127`: latest-wins watch (keep-latest precedent) +- B `cached_source.rs:150`: Mutex held across `deliver().await` | RAG | Y +- C `check_redundancy.rs:160` (+ `should_respond.rs:230`, `validate_response.rs:183`, `rate_proposals/orchestrator.rs:125`, `shared_analysis/mod.rs:297`, `generate_recipe/orchestrator.rs:150`, `tool_embedding.rs:296,390`, `inference_session.rs:351`, `request_replay.rs:313`): adapter registry RwLock read held across generation | Y +- A `act_observe/settle/tick_deadline.rs:33-70`: act deadline | Y +- A `workspace/capacity.rs:79-160`: parked until capacity changes +- B `deliberation_budget.rs:136-239`: room speech rings +- B `working_memory.rs:253,292`, `action_log.rs:67`, `bounded_room_ledger.rs:57`, `seen_ids.rs:13`, `recall_faculty.rs:243`, `rag_source_faculty.rs:163` +- C `dispatch_listener.rs:84`, `model_binding.rs:58`, `prompt_capture.rs:279`, `workspace_capture.rs:174` +- D `handoff.rs:365`: 5 ms poll | Y + +## Persona loop and mind +- A `service_loop/wake_select.rs:23,52-112`: biased select per mind (the per-owner dispatcher today) | Y +- A `serve_persona_loop_inner.rs:158-173`: `Wake::Tick` runs the self-turn inline; a directed line waits | Y +- A `wake_select.rs:107,118`: quiesce gate +- C `serve_persona_loop_inner.rs:141`: shutdown door +- B `serve_persona_loop_inner.rs:232` + `loop_dedup.rs:149`: loop-filler defer | Y +- C `serve_persona_loop_inner.rs:330`, `obligation.rs:377-382`: obligation write | Y +- A `turn_cycle.rs:160-176`: `active_claims().await` bypass | Y +- A `turn_cycle.rs:179`: ambient permit or skip +- A `burst_fingerprint.rs:310`: `hold_ambient_turn_for` +- A `burst_fingerprint.rs:100,126`: 250 ms token flush | stream/TTS | Y +- A `wake_backlog.rs:27-29,174,217-238,280`: pending-input custody, per-beat sleep | Y +- A `persona/inbox.rs:47` (`enqueue_message.rs:56`, `turn_frame/execute.rs:148,367`): BinaryHeap inbox | Y +- A `channel_queue.rs`, `channel_registry.rs:45`, `modules/channel.rs:57,162,206`: per-domain queue, 60 s tick +- E `persona/service_module.rs:644`: max 1 + 250 ms tick over an empty map +- A `airc_persona_conversation/polled.rs:491,607` (`INBOX_CAPACITY` 8192): live inbox | Y +- D `polled.rs:16,684`, `inbox_capacity.rs:175,214`: 60 s catch-up | Y +- D `inbox_capacity.rs:24`: resubscribe backoff | Y +- B `polled.rs:80-82,348,415`: text/seen rings, backlogs +- A `perception_feed.rs:212-229`: `try_send` drops the NEW event when full | Y +- C `perception_region.rs:174`, `stream_rail.rs:42` +- D `command_inbound_pump.rs:415,438,545`; D `persona_airc_runtime.rs:313,796` +- A `substrate_governor.rs:124,280,400` + kernel `cadence_table.rs`, `share_controller.rs`: 1 s pass, sequential region ticks +- A `airc_runtime_registry.rs:402-432`: fleet quiesce +- A `placement_reservation.rs:41,165,213-283`: seat lease + backoff +- A `placement_switch/decision.rs:31`, `grid/placement.rs:39`, `placement/opportunity.rs:44`: move cooldowns +- A `card_carry.rs:15` + `modules/card_carry.rs:50` +- B `message_cache.rs:94,186`, `work_board/events.rs:83,130` +- D `chat/chat_data_handle.rs:430-439` + +## Perception, RAG, memory, embedding, store +- A `continuum-embedding/src/module.rs:106`: embedding max 1 | Y by command +- B `continuum-memory/src/memory/mod.rs:302-330`: up to 64 embeds inline per recall | Y +- C `memory/cache.rs:29` +- A `store/data/max_concurrent_queries.rs:12,148`, `data_state.rs:99,152,248`: query Semaphore(16) | Y +- C `data_state_2.rs:344`; C `adapters.rs:42`, `data_state_2.rs:513-624` + +## Tools and hands +- A `tool_executor/command_executor.rs:615`: unbounded `join_all` | Y +- C `hands/code/ports.rs:227,248` +- D shell polls: `shell_session.rs:149,278`, `run_shell_command.rs:401`, `code_shell_kill_result.rs:297`, `modules/code.rs:326`, `python_adapter.rs:252` | Y + +## Live media +- A `call_server/text_preview_length.rs:9-13` + `call_manager.rs:476`: STT Semaphore(2), drops the utterance | Y +- A `audio/stt/whisper.rs:29,169`: one Whisper Mutex | Y +- A `audio/tts/kokoro.rs:27,311`, `piper.rs:22,84`, `orpheus.rs:118,790`, `pocket.rs:47,385`: one Mutex per TTS model | Y +- C `call_manager.rs:112,126`; C calls/participants RwLocks (about 50 sites); C `audio/router.rs:122,130`; C `vad/production.rs:79,179` +- B `transcription_entry.rs:13`, `live_kit_agent_manager.rs:560`, `perception_buffer.rs:72` +- D `voice_module/session.rs:119,148,209`: fixed 1 s/3 s/2 s-per-agent sleeps at call start | Y +- D `audio/resource_lifecycle.rs:31-47,134,173` +- A `avatar/render_loop.rs:28,214,329,449`: avatar slot pool, 100 ms waits | Y +- D `video/source.rs:92,154,160`, `bevy_renderer/api.rs:150,191`, `consumers/avatar.rs:115,149,258` +- A `livekit-bridge/src/agent.rs:48,137,595`, `server.rs:36,42`: video Mutex, 1 fps throttle, unbounded channels | Y +- A `ipc/start_server/live_call.rs:219`: human audio mpsc(128) | Y + +## AIRC and grid +- A `ipc/command_ingress.rs:439,686`, `served_generations.rs:169`: leased-in admission, refuses past served lanes | Y +- B `airc/realtime_store.rs:124` +- D `inbound_attach.rs:109-130`, `reattach.rs:48,174`, `daemon_liveness.rs:52,85`, `discovery_aggregate.rs:122,245,302`, `fd_relief.rs:18` +- A `ipc/ws.rs:188,203`: ws mpsc(64) | Y +- D `grid_allocator.rs:586`, `grid/mod.rs:201`, `connection.rs:33,37`, `capacity/gossip.rs:299`, `modules/grid_capacity.rs:86` +- C `transports/tailscale.rs:39,52`, `grid/audit.rs:67,92`, `provider_bridge.rs:261` + +## Genome, training, dreams, academy +- D `training_trigger.rs:908`: 30 s tick +- A `genome/lease_pull.rs:404-418`: per-peer Semaphore +- D `lease_pull.rs:447`, `grid_peer_adapter.rs:486`: 10 ms polls +- C `job_board.rs:146`, `job_actor.rs:263`, `native_jobs.rs:149`, `examiner/run.rs:39` +- D `engine_lora_adapter/train_status.rs:727-780`: 2 s steering poll; hold read every tick +- E `governed_training_free.rs:100`: `hold_training` (no callers) +- A `engine_lane.rs:66-72`, `engine_lora_fine_tuner.rs:44,513`, `cuda_lora_adapter.rs:195`, `mlx_lora_adapter.rs:334,752`: trainer admission/memory waits +- D `genome_fitness_sentinel.rs:50`, `training_completion_sentinel.rs:90` +- A `dream_consolidation/region.rs:84,92,169,278`, `consolidate.rs:8`: dream cooldowns +- A `dream_consolidation/distiller.rs:391`: `defer_while_held` +- D `eval/warm_gate.rs:42`, `fork_eval_cycle_waiting.rs:31` +- A `eval/cognition_eval.rs:23`, `exam_serving.rs:109`: fleet quiesce +- A `swe_bench/fetch_hf_rows.rs:152-199`: rate-limit backoff | external API + +## Sentinel, positron, miscellany +- A `sentinel_module.rs:437,480`, `steps/llm.rs:146-152`, `executor.rs:505` (the watchdog is D) +- D positron sampling ticks: `positron_presence.rs:321,615`, `positron_metrics_source.rs:119`, `positron_live_source.rs:53`, `positron_serving_source.rs:256`, `vitals_emitter.rs:264` +- A `continuum-positron/src/broadcast.rs:126,159`, `session_task.rs:152-181`: latest-wins watch + `min_interval` sleep | UI | Y +- B `positron_source/rather.rs:329`; C `positron_nav_source.rs:326,421` +- D `memory_guard.rs:15,54`, `rehome_reconciler.rs:43`, `ai_provider/block.rs:30,80`, `auth.rs:653`, `floor_fetch.rs:122`, `deploy_tracker.rs:409`, `benchmark_grade.rs:63`, `rest_seats.rs:194` +- A `forge/custodian_supervisor.rs:138`, `bin/forge_custodian.rs:98,116`: slots Semaphore +- E `inference-grpc/src/priority_queue.rs:193-291`, `worker_pool.rs:61-198`: unused (its own header says so) + +Totals: A 74, B 13, C 28, D 34, E 11 (160).