Skip to main content

runtime/
run.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4// Copyright (c) 2026 2youg1 and the sprawling contributors
5
6//! The run driver: dispatch, turns, freeze — the single authority for the
7//! event sequence a run leaves behind. The real city and citysim call this
8//! same code, so the simulator's byte fixtures are evidence about
9//! production rather than about a second implementation of the same idea.
10//!
11//! Time arrives through the `now` hook and is never sampled here. The
12//! caller decides what a clock is: a counter in the simulator, the one
13//! sanctioned wall-clock sample in `bin::assembly`.
14//!
15//! `crates/runtime/spec/Run.lean` proves what the event sequence must
16//! hold (§8-15): `handoff_written` and `run_frozen` close every run, once.
17
18use kernel::{
19    Address, AxError, BuildingPolicy, Carrier, Completion, EventDraft, Ledger, Locator, Model,
20    Payload, RunId, TimeMs, ToolCall, ToolDef,
21};
22
23use kernel::ChatMessage;
24
25use crate::catalog::SkillPin;
26use crate::conversation::{Conversation, Opening};
27use crate::handoff::Handoff;
28use crate::prefix::FrozenPrefix;
29use crate::reminder::ContextGauge;
30use crate::turn::{CallShape, Interrupt};
31
32mod charter;
33mod checkpoint;
34mod harness;
35mod lifecycle;
36
37pub use charter::Charter;
38pub use harness::{Conclusion, Cut, HarnessRun};
39
40/// Everything constant about one run. Assembled by the caller, because
41/// what a prefix contains and which tools exist are decisions of the city
42/// that dispatches, not of the loop that runs.
43pub struct RunPlan {
44    pub run: RunId,
45    pub who: String,
46    pub addr: Address,
47    pub task: String,
48    pub goal: String,
49    /// Whether this session was handed a written task or a person.
50    /// Decided by the city when it laid the brief down, carried here so
51    /// the window and the prefix's run segment cannot disagree about
52    /// which situation the agent is in.
53    pub opening: Opening,
54    pub job: Locator,
55    /// The run that handed this work down, when one did. Written into
56    /// `run_started` so the tree is a fact anybody folding the ledger
57    /// can read, rather than an inference from two lines happening to
58    /// be next to each other.
59    pub parent: Option<RunId>,
60    /// The run this one replaces, when a resident succeeded itself.
61    /// Written into `run_started` beside `parent`, so a lineage is a
62    /// fact folded from the ledger rather than inferred from two runs
63    /// sharing an address.
64    pub predecessor: Option<RunId>,
65    /// Who dispatched this run, written into `run_started`: the line's
66    /// author is always the city's desk, so only the dispatch site
67    /// knows whether the person, the city or a resident sent it.
68    pub dispatched_by: kernel::event::Who,
69    /// The run policy this run was dispatched under, written into
70    /// `run_started` as it was chosen (`crates/kernel/Spec.lean` §8-77), and
71    /// the cell that holds it while the run goes: the one value here
72    /// that changes, and only at `SafePoint::BeforeWave` (§8-62). Whoever
73    /// builds the tools builds the cell first and hands each write gate
74    /// its reader. Named apart from `policy`, which is the building's,
75    /// because the two answer different questions: what the building
76    /// allows, and what this dispatch asked for inside that.
77    pub run_policy: crate::mode::PolicyCell,
78    /// The conversation this run starts from, when it is the first run of
79    /// a session that branched off another. Empty for every other run.
80    ///
81    /// It is the window's material and not the prefix's: a prefix segment
82    /// is a document a person can read and edit, while this is what the
83    /// model said and what the tools answered, and the ledger is where
84    /// those live (`runtime::fork::inherited_indexed` rebuilds them from it).
85    pub inherited: Vec<ChatMessage>,
86    pub shape: CallShape,
87    /// Where the context reminder's second rung sits for this run: the
88    /// configuration ladder's answer, or `None` when no layer stated one
89    /// and the policy default sounds. Frozen with the run like every
90    /// setting here, so "each rung sounds once per run" cannot depend on
91    /// when somebody edited a file.
92    pub second_threshold: Option<kernel::SecondThreshold>,
93    /// Where this run records the provider's count after each call. The
94    /// caller keeps a clone for whatever reports the reading, which is
95    /// `status`: the run writes it and nothing else does.
96    pub context: crate::ContextReading,
97    pub prefix: FrozenPrefix,
98    pub policy: BuildingPolicy,
99    pub tools: Vec<ToolDef>,
100    /// The skills this run's reading room admitted, and what each one
101    /// hashed to when the shelf was read. Written into `run_started`
102    /// beside `parent`, and for the same reason: once the process that
103    /// read the shelf is gone, the ledger is the only thing that can
104    /// still say which bytes this run was given.
105    pub skills: Vec<SkillPin>,
106    /// How many times this run may make a failed call again, as the
107    /// person set it on the endpoint it calls. Frozen with the run for
108    /// the reason every other setting here is: a number changed halfway
109    /// through would make the account of what this run did depend on
110    /// when somebody looked at a form.
111    pub retries: kernel::Retries,
112    /// The identity version this run's session froze, which
113    /// `run_started` records as it is (`crates/runtime/spec/Run.lean` §8-56).
114    pub naming: Option<kernel::B3Hash>,
115}
116
117/// Where the driver stops to ask whether anything arrived. The turn layer
118/// owns three cancellation-safe points; this enum is how the driver names
119/// them to a caller that knows nothing about turn internals.
120#[derive(Debug, Clone, Copy, PartialEq, Eq)]
121pub enum SafePoint {
122    BeforeAssemble {
123        turn: u32,
124    },
125    BeforeCall {
126        turn: u32,
127    },
128    BeforeWave {
129        turn: u32,
130    },
131    /// Before one call of the wave, asked once per call. A wave is as
132    /// many effects as the model asked for, so a halt that arrives
133    /// while the third of eight runs stops the fourth rather than the
134    /// next turn.
135    BeforeToolCall {
136        turn: u32,
137        call: u32,
138    },
139    /// After the wave, before the run acts on what the turn decided.
140    /// The one boundary a run that concluded still passes through, so a
141    /// cancel arriving that late stops the work the turn handed down
142    /// instead of arriving at a run that has already ended.
143    BeforeSpawn {
144        turn: u32,
145    },
146}
147
148/// What one turn did. Exhaustive on purpose: a new ending has to make
149/// every caller say what it does about it.
150#[derive(Debug)]
151pub enum Advance {
152    Turned,
153    Concluded(Completion),
154}
155
156/// The things the driver cannot decide for itself. Closures rather
157/// than traits: no second implementation exists yet, and this
158/// library introduces a trait at a seam that already has one.
159pub struct RunHooks<'a> {
160    /// The clock, called by the driver on its own thread and nowhere else:
161    /// once per dispatch line, once per turn, once when each model attempt
162    /// is sent and once when its reply is whole, once when each tool call
163    /// starts and once when it answers, and once per freeze that is not a
164    /// cancellation.
165    pub now: &'a mut dyn FnMut() -> Result<TimeMs, AxError>,
166    /// Microseconds on a clock that only moves forward, from an origin
167    /// of the caller's choosing, read beside `now` each time a model
168    /// attempt goes out, its first content arrives and its reply is
169    /// whole, and each time a tool call starts and answers. Their
170    /// differences are the durations `model_returned` and `tool_result`
171    /// record (kernel D20); the absolute value is never recorded.
172    pub monotonic_us: &'a mut dyn FnMut() -> u64,
173    /// Answers what arrived at a safe point.
174    pub interrupt: &'a mut dyn FnMut(SafePoint) -> Interrupt,
175    /// The pre-wave checkpoint. `None` runs without a net, which
176    /// the tool layer refuses for anything that can delete.
177    pub checkpoint: Option<&'a mut dyn FnMut(TimeMs) -> Result<Payload, AxError>>,
178    /// What a call may write, by its declared effect, asked before the
179    /// wave runs: a wave whose every call answers `Nothing` changes no
180    /// file, so it needs no checkpoint of its own (§8-45).
181    pub writes: &'a dyn Fn(&ToolCall) -> kernel::Writes,
182    /// Runs a wave's tool calls in three stages (see
183    /// [`crate::ConcurrentInvoke`]). `admit` receives the turn's stamp; a
184    /// call's own moments are read by the turn from `now`, never by the
185    /// tool face, which holds no clock of its own.
186    pub invoke: &'a mut dyn crate::ConcurrentInvoke,
187    /// Holds the run until the moment the watchdog set for the next call
188    /// to a provider that failed, and answers whether that call may go.
189    ///
190    /// `Halted` comes back as soon as a halt reaches the run, without
191    /// waiting the rest out: a backoff grows to a minute, and a brake
192    /// that engages a minute late is not one. A counted clock answers
193    /// `Allowed` at once, because nothing it replays waits in real time.
194    pub wait: &'a mut dyn FnMut(TimeMs) -> crate::NextCall,
195    /// Where text goes while the model is still saying it.
196    ///
197    /// `None` runs the model without asking for a stream, which is what
198    /// citysim and every offline replay do: an increment changes nothing
199    /// a run decides, so a driver with nowhere to put one asks for none
200    /// and the call is byte-identical to what it always was.
201    ///
202    /// It takes no result, because nothing here may fail a call. What
203    /// arrives is a thing to look at; the record of what was said is
204    /// written from `model_returned`, once, afterwards.
205    pub deltas: Option<&'a mut (dyn FnMut(&kernel::Increment) + 'a)>,
206}
207
208/// An active run: turns may still be taken.
209pub struct Active {
210    conversation: Conversation,
211    turns: u32,
212    last_turn_t: Option<TimeMs>,
213    /// How full the window is, read off each call's reported usage.
214    gauge: ContextGauge,
215    /// The cache shape of the request this run sent last, which is what
216    /// the next one is compared against. `None` before the first
217    /// request, and that absence is the `FirstRequest` a record states.
218    prior_shape: Option<crate::prefix::shape::PromptShape>,
219    /// The `prompt_assembled` payload this run wrote last, which a turn
220    /// does not write again (`crates/runtime/spec/Prefix.lean` §8-39, item 5).
221    prompt: crate::turn::PromptRecord,
222    /// Whether the next wave needs a checkpoint (§8-45).
223    checkpoint: checkpoint::CheckpointPolicy,
224    /// The lines the run's turns appended and no barrier has carried
225    /// yet, with the refs of those that one has (runtime D36).
226    lines: crate::turn::HeldLines,
227}
228
229/// A frozen run. There is no method back to [`Active`]: waking an old run
230/// is not something this type can spell, and `resume` takes a [`Handoff`]
231/// rather than a run.
232pub struct Frozen {
233    completion: Completion,
234    turns: u32,
235    /// Kept past the freeze for one reader: the transcript written
236    /// beside the room, which is what this run's model actually saw.
237    conversation: Conversation,
238}
239
240pub struct Run<S> {
241    plan: RunPlan,
242    state: S,
243}
244
245impl Run<Frozen> {
246    pub fn completion(&self) -> &Completion {
247        &self.state.completion
248    }
249
250    /// What this run was dispatched with, for a successor that takes
251    /// the same work on.
252    pub fn plan(&self) -> &RunPlan {
253        &self.plan
254    }
255
256    /// What the model saw, scanned and ready to write beside the room.
257    ///
258    /// # Errors
259    /// Propagates a message that will not serialise.
260    pub fn transcript(&self) -> Result<crate::transcript::Transcript, AxError> {
261        crate::transcript::Transcript::of(self.plan.run, &self.state.conversation)
262    }
263
264    pub fn turns(&self) -> u32 {
265        self.state.turns
266    }
267
268    pub fn run_id(&self) -> RunId {
269        self.plan.run
270    }
271}
272
273/// Dispatch, turn until an ending, freeze. The loop is here rather than in
274/// each caller because the ending rules — an empty wave concludes, a
275/// cancellation is an ending too — are the part that must not drift
276/// between the city and the simulator.
277///
278/// **The loop counts no turns.** There is no ceiling to reach, because
279/// nobody can price a piece of work before it runs. What
280/// ends a run is what it did: a turn that concluded, a failure with a
281/// carrier event, or an interruption a safe point delivered — and what
282/// stops one from outside is `Halt`, which shuts the scope and kills the
283/// backlog members inside it.
284///
285/// # Errors
286/// Propagates ledger and provider failures.
287pub fn drive(
288    plan: RunPlan,
289    ledger: &mut dyn Ledger,
290    model: &mut dyn Model,
291    hooks: &mut RunHooks<'_>,
292    handoff: &Handoff,
293) -> Result<Run<Frozen>, AxError> {
294    let mut watchdog = crate::Watchdog::new(plan.retries, plan.run);
295    let mut run = Run::dispatch(plan, ledger, hooks)?;
296    let ending = loop {
297        match run.advance(ledger, model, hooks) {
298            Ok(Advance::Turned) => watchdog.on_provider_answered(),
299            Ok(Advance::Concluded(completion)) => break completion,
300            // A run always ends. A mid-turn failure whose code has a
301            // carrier event (provider down, budget, watchdog) is written
302            // into history under that carrier and the run freezes as
303            // cancelled, so a 401 from a provider never leaves a run
304            // "started" with an event stream that simply went quiet.
305            //
306            // A loadtime code names no carrier, and it leaves by the
307            // same door: the verdict is written first and the diagnosis
308            // travels afterwards, so the two facts do not compete.
309            // `E_WIRE_MISMATCH` means a provider spelled its dialect
310            // wrong while the ledger is in perfect health. Where the
311            // store really is the casualty, `freeze` fails on its own
312            // append and that failure is what travels, which is more
313            // honest than deciding in advance that nothing can be
314            // written.
315            Err(err) => {
316                // What the failed turn and the one before it held goes
317                // down first, so the carrier event and the watchdog's line
318                // follow them in the ledger as they followed them in time
319                // (runtime D36).
320                run.state.lines.barrier(ledger)?;
321                let Carrier::Event(kind) = err.code().carrier() else {
322                    run.freeze(ledger, handoff, Completion::Cancelled, hooks)?;
323                    return Err(err);
324                };
325                let t = (hooks.now)()?;
326                // A provider that failed for a reason worth retrying is
327                // asked again, and the attempt is recorded rather than
328                // repeated silently: the second `model_called` in the
329                // history is what a person reads the retry off.
330                // The watchdog decides whether there is a next call and
331                // when; the line records that moment before the wait,
332                // and the wait is where a halt reaches a run that has
333                // no turn in flight to stop.
334                let disposal = watchdog.on_provider_failure(&err, t);
335                if let crate::Disposal::BackOff { until, .. } = disposal {
336                    ledger.append(EventDraft {
337                        run: run.plan.run,
338                        t,
339                        who: run.plan.who.clone(),
340                        addr: Some(run.plan.addr.clone()),
341                        kind: kernel::EventKind::WatchdogFired,
342                        data: watchdog.fired_payload(&disposal)?,
343                        ig: false,
344                    })?;
345                    match (hooks.wait)(until) {
346                        crate::NextCall::Allowed => continue,
347                        crate::NextCall::Halted => break Completion::Cancelled,
348                    }
349                }
350                // The failure itself is the payload, through the one
351                // door: an encoding that fails travels as a refusal
352                // rather than as an empty object.
353                let data = Payload::of(&err)?;
354                ledger.append(EventDraft {
355                    run: run.plan.run,
356                    t,
357                    who: run.plan.who.clone(),
358                    addr: Some(run.plan.addr.clone()),
359                    kind,
360                    data,
361                    ig: false,
362                })?;
363                break Completion::Cancelled;
364            }
365        }
366    };
367    run.freeze(ledger, handoff, ending, hooks)
368}