Skip to main content

harness_loop/
lib.rs

1//! ReAct agent loop with self-correction.
2//!
3//! Minimal v0.0.1 implementation:
4//! - Applies guides once at the start.
5//! - Sends `Context` (with `tools`) to the model.
6//! - Dispatches each returned tool call via [`ToolRegistry`].
7//! - Runs `Sensor::SelfCorrect` sensors after each action; auto-fix patches are
8//!   applied directly to the world, blocking signals are fed back to the model.
9//! - Stops when the model returns no tool calls, or when `policy.max_iters` is hit.
10
11pub mod acceptance;
12pub mod goal;
13pub mod learning;
14pub mod memory_layer;
15#[cfg(feature = "otel")]
16pub mod otel;
17pub mod profile_guide;
18pub mod recall_layer;
19pub mod receipt;
20pub mod registry;
21pub mod seal;
22pub use acceptance::{Acceptance, FilesExist, NonEmptyAnswer, Verdict};
23pub use goal::{Goal, GoalStore, Phase, PhaseStatus};
24pub use receipt::{Receipt, ReceiptBuilder};
25pub use seal::{SealBreach, SealSet};
26pub mod replay;
27pub mod subagent;
28pub mod telemetry;
29
30pub use learning::*;
31pub use memory_layer::*;
32pub mod boundary_guide;
33pub use boundary_guide::*;
34pub use profile_guide::*;
35pub use recall_layer::*;
36pub use registry::*;
37pub use replay::*;
38pub use subagent::*;
39pub use telemetry::*;
40
41use harness_compactor::{CALIBRATION_KEY, DefaultCompactor};
42use harness_core::{
43    Action, Block, CompactionStage, Compactor, Context, Event, Guide, HarnessError, HookOutcome,
44    Model, ModelDelta, ModelOutput, ResponseFormat, Sensor, SessionSource, SignalSet, Stage,
45    StopReason, Task, ToolCall, ToolResult, Turn, TurnRole, Usage, World,
46};
47use harness_hooks::HookBus;
48use std::collections::HashMap;
49use std::sync::Arc;
50use std::time::Duration;
51
52/// Governs the loop's stuck-detector. When the model repeats the *same* tool
53/// call (name + args) round after round without making progress, the loop first
54/// nudges it to change approach, then terminates cleanly with [`Outcome::Stuck`]
55/// rather than burning the rest of the budget spinning on a loop.
56///
57/// Enabled by default with conservative thresholds — the calls must be
58/// *byte-identical*, so genuine "read the same file twice" work never trips it.
59#[derive(Debug, Clone)]
60pub struct StuckPolicy {
61    pub enabled: bool,
62    /// Consecutive identical tool-call rounds before injecting a "you are
63    /// repeating yourself, change your approach" feedback signal.
64    pub nudge_after: u32,
65    /// Consecutive identical rounds before terminating with [`Outcome::Stuck`].
66    pub abort_after: u32,
67}
68
69impl Default for StuckPolicy {
70    fn default() -> Self {
71        Self {
72            enabled: true,
73            nudge_after: 3,
74            abort_after: 6,
75        }
76    }
77}
78
79/// A ceiling on how much of one tool result reaches the context.
80///
81/// A single call can return more than the whole conversation: a lock file, a
82/// `SELECT *`, an MCP tool the framework does not control. Measured on a real
83/// run, "search these files for a word" cost 53,487 input tokens because one
84/// `read_file` returned a lock file — the model then paid for it on every
85/// subsequent turn, and compaction started throwing away real history to make
86/// room. A per-result ceiling is the only place to stop that: the tools cannot
87/// all be trusted (third-party MCP), and the compactor only runs after the
88/// damage is in the context.
89#[derive(Debug, Clone)]
90pub struct ToolResultPolicy {
91    /// Max serialized bytes of a single tool result. `None` disables the guard.
92    /// Default ~24 KiB — roughly 6k tokens of English, generous for a file page
93    /// or a query result, far below what blows a window.
94    pub max_bytes: Option<usize>,
95    /// Replace the payload of a read-only call that exactly repeats an earlier
96    /// one in the same run, when nothing has modified the world in between.
97    ///
98    /// [`StuckPolicy`] only sees *consecutive* identical rounds. Reading a file
99    /// at iteration 1 and again at iteration 5 is not that, and looks like
100    /// progress — but the same bytes land in the context twice and the model
101    /// learns nothing the second time. Measured on a real run, model wait was
102    /// 36.7s against 3ms of tool execution: a repeat costs context, not time,
103    /// so what is suppressed is the payload, not the call.
104    ///
105    /// Only `ToolRisk::ReadOnly` qualifies (`Network` is a separate risk, and an
106    /// external endpoint may answer differently), and any non-read-only call
107    /// clears the record — after a write, re-reading is the correct move.
108    ///
109    /// **Off by default, on the evidence.** Measured on the completion
110    /// benchmark: ceilings alone solved 6/6 tasks for 160k effective tokens;
111    /// adding repeat-suppression cut that to 32k — and lost a task. An agent
112    /// working through a file larger than one page re-reads it because it cannot
113    /// hold it, gets told it already has the answer, and does not: the content
114    /// was paged away. Five times cheaper is not worth a task a framework could
115    /// otherwise do, so this is opt-in for callers who would rather have the
116    /// tokens. (Suppressing from the *third* identical call rather than the
117    /// second would likely keep most of the saving without the failure — it
118    /// needs measuring before it becomes the default.)
119    pub dedupe_repeats: bool,
120    /// When a result exceeds `max_bytes`, save the *full* payload to a file
121    /// under `.harness/spill/` in the workspace and inline a bounded preview
122    /// plus the path, instead of cutting the tail off and throwing it away.
123    ///
124    /// Truncation destroys information: the model is told "narrow your
125    /// request", but the bytes it needed may be exactly the ones dropped, and
126    /// the only recovery is re-running the call to be truncated again.
127    /// Measured on the completion benchmark, that loop is visible as a single
128    /// task paying 184k input tokens. Spilling keeps the ceiling — the context
129    /// gets a preview, never the flood — while the whole result stays
130    /// retrievable through the file tools the agent already has (`read_file`
131    /// with offset/limit, `grep` on the spill path), because the spill lives
132    /// inside the workspace jail. Borrowed from DeepSeek Harness's `spill`
133    /// family (preview + retrieval locator), which is the same judgement.
134    ///
135    /// Costs nothing until it fires: the write happens only on the oversized
136    /// path, which the default ceiling makes rare. When the write fails (e.g.
137    /// read-only workspace) the guard falls back to plain truncation.
138    pub spill: bool,
139}
140
141impl Default for ToolResultPolicy {
142    fn default() -> Self {
143        Self {
144            max_bytes: Some(24 * 1024),
145            dedupe_repeats: false,
146            spill: true,
147        }
148    }
149}
150
151/// Governs *when* and *how far* the loop compacts context. Hysteresis: only
152/// start compacting once usage crosses `high_water`, and stop as soon as it's
153/// back under `target` — instead of running every stage above a threshold on
154/// every turn. This avoids over-compacting (needlessly reaching the lossy,
155/// main-model `AutoCompact` stage) and, because compaction rewrites history and
156/// invalidates the provider prefix cache, avoids nibbling the context every
157/// single turn.
158#[derive(Debug, Clone)]
159pub struct CompactPolicy {
160    /// Start compacting when `used/window` exceeds this. Default 0.75.
161    pub high_water: f32,
162    /// Stop as soon as `used/window` is back at/under this. Default 0.55.
163    pub target: f32,
164}
165
166impl Default for CompactPolicy {
167    fn default() -> Self {
168        Self {
169            high_water: 0.75,
170            target: 0.55,
171        }
172    }
173}
174
175/// Inline preview size for a spilled result. Deliberately smaller than the
176/// ceiling: the point of spilling is that the context gets a *glimpse* and a
177/// path, not four-fifths of the flood.
178const SPILL_PREVIEW_BYTES: usize = 4 * 1024;
179
180/// First `n` bytes of `s`, cut on a char boundary.
181fn head_of(s: &str, n: usize) -> &str {
182    let mut end = n.min(s.len());
183    while end > 0 && !s.is_char_boundary(end) {
184        end -= 1;
185    }
186    &s[..end]
187}
188
189/// The string field that dominates an oversized result, if one does.
190///
191/// Most oversized results are one big string in a small envelope — a file
192/// body, a command's stdout — and the right spill for those is the *raw text*:
193/// multi-line, so `read_file`'s line paging and `grep`'s line matching work on
194/// it. Spilling the serialized JSON instead would fold the whole payload onto
195/// one escaped line that line-oriented tools can neither page nor match
196/// (`read_file` pages by line; a 68 KB single-line file is unreachable past
197/// the first 16 KB). Returns the dotted path and the string when one field is
198/// ≥ 80% of the serialized size.
199fn dominant_string(v: &serde_json::Value, total: usize) -> Option<(String, &str)> {
200    fn walk<'a>(v: &'a serde_json::Value, path: &str, best: &mut Option<(String, &'a str)>) {
201        match v {
202            serde_json::Value::String(s) => {
203                if best.as_ref().is_none_or(|(_, b)| s.len() > b.len()) {
204                    *best = Some((path.to_string(), s.as_str()));
205                }
206            }
207            serde_json::Value::Object(m) => {
208                for (k, x) in m {
209                    let p = if path.is_empty() {
210                        k.clone()
211                    } else {
212                        format!("{path}.{k}")
213                    };
214                    walk(x, &p, best);
215                }
216            }
217            serde_json::Value::Array(a) => {
218                for (i, x) in a.iter().enumerate() {
219                    walk(x, &format!("{path}[{i}]"), best);
220                }
221            }
222            _ => {}
223        }
224    }
225    let mut best = None;
226    walk(v, "", &mut best);
227    best.filter(|(_, s)| s.len() * 5 >= total * 4)
228}
229
230/// Replace the value at a `dominant_string` dotted path with `with`.
231fn replace_at(v: &mut serde_json::Value, path: &str, with: serde_json::Value) {
232    let mut cur = v;
233    let mut rest = path;
234    loop {
235        // Next segment: `key`, `key[i]`, or a bare `[i]`.
236        let (seg, tail) = match rest.find('.') {
237            Some(dot) => (&rest[..dot], &rest[dot + 1..]),
238            None => (rest, ""),
239        };
240        let (key, idx) = match seg.find('[') {
241            Some(b) => (&seg[..b], seg[b + 1..seg.len() - 1].parse::<usize>().ok()),
242            None => (seg, None),
243        };
244        if !key.is_empty() {
245            match cur.get_mut(key) {
246                Some(next) => cur = next,
247                None => return,
248            }
249        }
250        if let Some(i) = idx {
251            match cur.get_mut(i) {
252                Some(next) => cur = next,
253                None => return,
254            }
255        }
256        if tail.is_empty() {
257            *cur = with;
258            return;
259        }
260        rest = tail;
261    }
262}
263
264/// Persist an oversized result under `.harness/spill/` in the workspace and
265/// build the inline marker. `None` on any IO failure — the caller falls back
266/// to truncation, because a guard that can error a run to protect a context
267/// window has its priorities backwards.
268fn spill_oversized(
269    action: &Action,
270    content: &serde_json::Value,
271    serialized: &str,
272    root: &std::path::Path,
273) -> Option<serde_json::Value> {
274    let dir = root.join(".harness").join("spill");
275    std::fs::create_dir_all(&dir).ok()?;
276    let id: String = action
277        .call_id
278        .chars()
279        .filter(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_'))
280        .take(48)
281        .collect();
282    let id = if id.is_empty() { "call".into() } else { id };
283
284    let (rel, preview, meta) = match dominant_string(content, serialized.len()) {
285        // One big string in a small envelope: spill the raw text, keep the
286        // envelope inline with the big field swapped for a pointer.
287        Some((field, text)) => {
288            let rel = format!(".harness/spill/{id}-{}.txt", action.tool);
289            std::fs::write(root.join(&rel), text).ok()?;
290            let mut meta = content.clone();
291            replace_at(
292                &mut meta,
293                &field,
294                serde_json::Value::String(format!("[{} bytes spilled to {rel}]", text.len())),
295            );
296            (
297                rel,
298                head_of(text, SPILL_PREVIEW_BYTES).to_string(),
299                Some(meta),
300            )
301        }
302        // Structured payload (e.g. a long match list): spill pretty-printed,
303        // one element per line, so line-oriented retrieval still works.
304        None => {
305            let rel = format!(".harness/spill/{id}-{}.json", action.tool);
306            let pretty =
307                serde_json::to_string_pretty(content).unwrap_or_else(|_| serialized.to_string());
308            std::fs::write(root.join(&rel), &pretty).ok()?;
309            (rel, head_of(&pretty, SPILL_PREVIEW_BYTES).to_string(), None)
310        }
311    };
312    tracing::warn!(
313        target: "harness.telemetry",
314        event = "tool.result.spilled",
315        "gen_ai.tool.name" = %action.tool,
316        bytes = serialized.len(),
317        path = %rel,
318    );
319    let mut marker = serde_json::json!({
320        "spilled": true,
321        "tool": action.tool,
322        "bytes_total": serialized.len(),
323        "path": rel,
324        "preview": preview,
325        "note": format!(
326            "This result was {} bytes — too large to inline, so the FULL content was \
327             saved to '{rel}' (workspace-relative). Nothing was lost. Retrieve exactly \
328             the part you need: grep(path=\"{rel}\", pattern=...) or \
329             read_file(path=\"{rel}\", offset=..., limit=...). Do not repeat the \
330             original call — it will spill again.",
331            serialized.len()
332        ),
333    });
334    // The envelope metadata (paths, flags) rides along when it is small; a
335    // pathological envelope that is itself oversized is dropped, not inlined.
336    if let Some(meta) = meta
337        && meta.to_string().len() <= SPILL_PREVIEW_BYTES
338    {
339        marker["meta"] = meta;
340    }
341    Some(marker)
342}
343
344/// A stable fingerprint of a round's tool calls (names + args, ignoring the
345/// volatile call id). Two rounds with the same fingerprint asked for the exact
346/// same actions — the signal the stuck-detector keys on.
347fn tool_call_fingerprint(calls: &[ToolCall]) -> String {
348    calls
349        .iter()
350        .map(|c| format!("{}({})", c.name, c.args))
351        .collect::<Vec<_>>()
352        .join("|")
353}
354
355/// Cheap identity for one tool result, for the stuck detector's "did anything
356/// change" test. Not a checksum anyone depends on — only equality between two
357/// consecutive rounds of the same process matters.
358fn result_digest(tool: &str, content: &serde_json::Value) -> u64 {
359    use std::hash::{Hash, Hasher};
360    let mut h = std::collections::hash_map::DefaultHasher::new();
361    tool.hash(&mut h);
362    content.to_string().hash(&mut h);
363    h.finish()
364}
365
366/// Where a run finished. Each variant is `#[non_exhaustive]` so new *fields*
367/// don't break downstream matches — always include `..` when destructuring.
368#[derive(Debug, Clone)]
369pub enum Outcome {
370    /// Model returned text with no tool calls (natural end).
371    #[non_exhaustive]
372    Done {
373        text: Option<String>,
374        iters: u32,
375        tools_called: u32,
376        usage: harness_core::Usage,
377        /// What the acceptance checks said. `None` means nothing was asked —
378        /// so "the model stopped" is all this outcome claims.
379        verified: Option<Verdict>,
380        /// The sealed contract as it stood before the model's first turn.
381        ///
382        /// Carried out of the run because a receipt has to say *what* was
383        /// measured, not only that the measurement held. Empty when nothing
384        /// was sealed.
385        contract: crate::seal::SealSet,
386        /// Set when a sealed acceptance contract changed during the run.
387        ///
388        /// Distinct from a plain failed `verified` because the two mean
389        /// different things to a caller: a failed check is work not finished,
390        /// this is the measuring instrument having been moved. A host may want
391        /// to fail a build on the first and page someone on the second.
392        seal_breach: Option<String>,
393    },
394    /// Policy budget exhausted before the model stopped requesting tools.
395    /// Carries everything we know so the caller can recover partial work
396    /// (saved notes, files written by tools, the last assistant text, etc.)
397    /// instead of seeing a single bare "budget out" string.
398    #[non_exhaustive]
399    BudgetExhausted {
400        iters: u32,
401        last_text: Option<String>,
402        tools_called: u32,
403        usage: harness_core::Usage,
404        /// True when `Task::deadline` stopped the run rather than `max_iters`.
405        /// Both end the same way — a forced final answer — but "ran out of
406        /// time" and "ran out of steps" call for different responses from the
407        /// caller, and an unattended long run only ever hits the first.
408        deadline_reached: bool,
409    },
410    /// The agent got stuck: it repeated the *same* tool call for
411    /// `StuckPolicy::abort_after` consecutive rounds without progress, so the
412    /// loop terminated early to save the rest of the budget. Carries partial
413    /// work (last text, files already written by tools) like `BudgetExhausted`.
414    #[non_exhaustive]
415    Stuck {
416        /// Human-readable reason, e.g. "repeated `read_file(...)` 6× without progress".
417        reason: String,
418        /// How many consecutive identical rounds were observed.
419        repeated: u32,
420        iters: u32,
421        last_text: Option<String>,
422        tools_called: u32,
423        usage: harness_core::Usage,
424    },
425}
426
427/// The agent loop.
428pub struct AgentLoop<M: Model> {
429    pub model: M,
430    pub tools: ToolRegistry,
431    pub guides: Vec<Arc<dyn Guide>>,
432    pub sensors: Vec<Arc<dyn Sensor>>,
433    pub hooks: HookBus,
434    pub compactor: Arc<dyn Compactor>,
435    /// A deadline on each individual tool call. One hung call — a network tool
436    /// on a dead endpoint, a shell command waiting on stdin — otherwise takes
437    /// the whole run down with it, and the host's only recourse is a run-level
438    /// timeout that throws away every turn of finished work (measured on the
439    /// completion benchmark: a run that had already done the job was billed as
440    /// a 0-token timeout). A per-call deadline converts the hang into an error
441    /// *result* the model sees and can route around. `None` disables. The
442    /// default is generous — 120s covers a slow build — because a false
443    /// deadline on a legitimately long tool is worse than a late one.
444    pub tool_timeout: Option<Duration>,
445    /// Default response format applied to every run unless overridden by
446    /// `run_typed`. See [`ResponseFormat`].
447    pub response_format: ResponseFormat,
448    /// When `true`, the loop drives each model turn via `Model::stream()`
449    /// instead of `complete()`, firing `Event::ModelTokenDelta` for each
450    /// text fragment. Tool-call deltas are still assembled inside the loop;
451    /// only the terminal `ModelOutput` shape is observable downstream.
452    pub streaming: bool,
453    /// Optional cross-session recall store. When set, the loop captures every
454    /// turn and the `session_search` tool is registered. See `with_recall`.
455    pub recall: Option<Arc<dyn harness_core::RecallStore>>,
456    /// When true (and `recall` is set), a `RecallGuide` auto-injects top-k
457    /// past context at session start.
458    pub recall_auto_inject: bool,
459    pub learning: Option<LearningConfig>,
460    /// Loop-detection policy. Enabled by default — see [`StuckPolicy`].
461    pub stuck: StuckPolicy,
462    /// Ceiling on context tokens, overriding the model's own window. `None`
463    /// (default) sizes the budget to `ModelInfo::context_window`.
464    pub max_input_tokens: Option<u32>,
465    /// Context-compaction hysteresis. See [`CompactPolicy`].
466    pub compaction: CompactPolicy,
467    /// Ceiling on a single tool result. See [`ToolResultPolicy`].
468    pub tool_results: ToolResultPolicy,
469    /// Conditions the run must satisfy before the loop reports success. The
470    /// model stopping is evidence it *believes* it is finished; these say
471    /// whether it is. See [`acceptance`].
472    pub acceptance: Vec<Arc<dyn Acceptance>>,
473    /// How many times a failed acceptance is handed back to the model before
474    /// the loop gives up and reports what there is. One is usually enough: a
475    /// model that ignores the first correction rarely takes the second.
476    pub acceptance_retries: u32,
477    /// System instruction injected into every run's `Context.system` (unless the
478    /// built context already carries its own). Set via [`with_system`](Self::with_system).
479    pub system: Vec<Block>,
480    /// Named auxiliary models for side tasks — compaction, memory synthesis,
481    /// judging, subagents — so the main conversation stays on one model (and
482    /// keeps its provider prompt-cache prefix intact) while cheap or specialist
483    /// work goes elsewhere. Populated via [`with_model_role`](Self::with_model_role),
484    /// read via [`model_for`](Self::model_for). An unregistered role means
485    /// "use the main model" — components fall back rather than fail.
486    pub model_roles: std::collections::HashMap<String, Arc<dyn Model>>,
487    /// True once the host installed its own compactor via
488    /// [`with_compactor`](Self::with_compactor). Guards the `"compactor"`
489    /// model-role convenience from overwriting an explicit choice.
490    pub compactor_custom: bool,
491}
492
493impl AgentLoop<harness_core::DynModel> {
494    /// Build a loop from a boxed model — what every model factory hands back
495    /// (`ApiKind::build`, a router, anything stored behind a trait object).
496    ///
497    /// `Arc<dyn Model>` deliberately does not implement `Model` (see
498    /// [`DynModel`](harness_core::DynModel) for why), so `AgentLoop::new` cannot
499    /// take one. Without this constructor every caller writes the wrapper
500    /// themselves, and the first thing a new user meets is a trait-bound error
501    /// naming a type they have never heard of.
502    ///
503    /// ```ignore
504    /// let model = ApiKind::OpenAI.build(base_url, model_id, key);
505    /// let agent = AgentLoop::boxed(model).with_tool(Arc::new(ReadFile));
506    /// ```
507    pub fn boxed(model: Arc<dyn Model>) -> Self {
508        Self::new(harness_core::DynModel(model))
509    }
510}
511
512impl<M: Model> AgentLoop<M> {
513    pub fn new(model: M) -> Self {
514        Self {
515            model,
516            tools: ToolRegistry::new(),
517            guides: Vec::new(),
518            sensors: Vec::new(),
519            hooks: HookBus::new(),
520            compactor: Arc::new(DefaultCompactor::new()),
521            tool_timeout: Some(Duration::from_secs(120)),
522            response_format: ResponseFormat::Free,
523            streaming: false,
524            recall: None,
525            recall_auto_inject: false,
526            learning: None,
527            stuck: StuckPolicy::default(),
528            max_input_tokens: None,
529            compaction: CompactPolicy::default(),
530            // On by default, because the failure it catches is invisible: a
531            // turn that produced nothing is reported as a turn that finished.
532            tool_results: ToolResultPolicy::default(),
533            acceptance: vec![Arc::new(acceptance::NonEmptyAnswer)],
534            acceptance_retries: 1,
535            system: Vec::new(),
536            model_roles: std::collections::HashMap::new(),
537            compactor_custom: false,
538        }
539    }
540
541    /// Register an auxiliary model under a role name — the loop's seam for
542    /// "side tasks don't have to run on the main model".
543    ///
544    /// The main conversation stays pinned to one model, which keeps its
545    /// provider prompt-cache prefix byte-stable; compaction summaries, memory
546    /// synthesis, judging, and subagents can go to a cheaper or specialist
547    /// model instead. Components read roles via [`model_for`](Self::model_for)
548    /// and fall back to the main model when a role is unregistered.
549    ///
550    /// One role is wired automatically: registering `"compactor"` upgrades the
551    /// default structural compactor to a [`ModelBackedCompactor`] on that
552    /// model — unless the host already installed its own via
553    /// [`with_compactor`](Self::with_compactor), which always wins regardless
554    /// of call order.
555    ///
556    /// ```ignore
557    /// let agent = AgentLoop::boxed(main)
558    ///     .with_model_role("compactor", cheap.clone())
559    ///     .with_model_role("judge", strong);
560    /// ```
561    pub fn with_model_role(mut self, role: impl Into<String>, model: Arc<dyn Model>) -> Self {
562        let role = role.into();
563        if role == "compactor" && !self.compactor_custom {
564            self.compactor = Arc::new(harness_compactor::ModelBackedCompactor::new(model.clone()));
565        }
566        self.model_roles.insert(role, model);
567        self
568    }
569
570    /// Look up an auxiliary model by role. `None` means "no model registered
571    /// for this role — use the main model"; callers fall back rather than fail,
572    /// so wiring stays optional everywhere.
573    pub fn model_for(&self, role: &str) -> Option<Arc<dyn Model>> {
574        self.model_roles.get(role).cloned()
575    }
576
577    /// Set a system instruction applied to every run (into `Context.system`) —
578    /// e.g. "answer only via the governed tools; never claim you can't access
579    /// data; never invent numbers". This is the first-class seam for a system
580    /// prompt; small local models in particular need it to reliably call tools
581    /// instead of refusing or hallucinating.
582    pub fn with_system(mut self, text: impl Into<String>) -> Self {
583        self.system = vec![Block::Text(text.into())];
584        self
585    }
586
587    /// Cap context tokens below the model's own window.
588    ///
589    /// Compaction fires at a fraction of this, so it is also the only way to
590    /// exercise the compaction path without first paying for a hundred
591    /// thousand tokens of real conversation — which is how "does a long run
592    /// survive being compacted" stayed untested.
593    pub fn with_max_input_tokens(mut self, n: u32) -> Self {
594        self.max_input_tokens = Some(n);
595        self
596    }
597
598    /// Override the loop-detection policy (thresholds, or disable entirely).
599    pub fn with_stuck_policy(mut self, policy: StuckPolicy) -> Self {
600        self.stuck = policy;
601        self
602    }
603
604    /// Override the compaction hysteresis policy. See [`CompactPolicy`].
605    /// Set the ceiling on a single tool result. See [`ToolResultPolicy`].
606    pub fn with_tool_result_policy(mut self, policy: ToolResultPolicy) -> Self {
607        self.tool_results = policy;
608        self
609    }
610
611    pub fn with_compact_policy(mut self, policy: CompactPolicy) -> Self {
612        self.compaction = policy;
613        self
614    }
615
616    /// Opt in to streaming the model's terminal turn token-by-token via
617    /// `Model::stream()`. Hooks subscribed to `Event::ModelTokenDelta` see
618    /// each fragment as it arrives; the rest of the loop is unchanged.
619    pub fn with_streaming(mut self, enable: bool) -> Self {
620        self.streaming = enable;
621        self
622    }
623
624    /// Add a condition the run must satisfy before it can report success.
625    pub fn with_acceptance(mut self, a: Arc<dyn Acceptance>) -> Self {
626        self.acceptance.push(a);
627        self
628    }
629
630    /// Replace the acceptance set outright (including the default).
631    pub fn with_acceptance_set(mut self, set: Vec<Arc<dyn Acceptance>>) -> Self {
632        self.acceptance = set;
633        self
634    }
635
636    pub fn with_acceptance_retries(mut self, n: u32) -> Self {
637        self.acceptance_retries = n;
638        self
639    }
640
641    pub fn with_tool_timeout(mut self, t: Option<Duration>) -> Self {
642        self.tool_timeout = t;
643        self
644    }
645
646    pub fn with_compactor(mut self, c: Arc<dyn Compactor>) -> Self {
647        self.compactor = c;
648        // An explicit compactor always wins over the "compactor" model-role
649        // convenience, in either call order.
650        self.compactor_custom = true;
651        self
652    }
653
654    pub fn with_tool(mut self, t: Arc<dyn harness_core::Tool>) -> Self {
655        self.tools.insert(t);
656        self
657    }
658
659    pub fn with_guide(mut self, g: Arc<dyn Guide>) -> Self {
660        self.guides.push(g);
661        self
662    }
663
664    pub fn with_sensor(mut self, s: Arc<dyn Sensor>) -> Self {
665        self.sensors.push(s);
666        self
667    }
668
669    pub fn with_hook(mut self, h: Arc<dyn harness_core::Hook>) -> Self {
670        self.hooks.register(h);
671        self
672    }
673
674    /// Pull in every `#[hook]`-registered hook.
675    pub fn with_macro_hooks(mut self) -> Self {
676        self.hooks = self.hooks.with_macro_hooks_take();
677        self
678    }
679
680    /// Enable cross-session recall: capture every turn into `store` and
681    /// register the `session_search` tool. Owner + session id are read from
682    /// `world.profile.extra["recall_owner"|"recall_session"]` at run time.
683    pub fn with_recall(mut self, store: Arc<dyn harness_core::RecallStore>) -> Self {
684        self.tools
685            .insert(Arc::new(crate::SessionSearchTool::new(store.clone())));
686        self.recall = Some(store);
687        self
688    }
689
690    /// Capture every turn into `store` **without** registering a search tool.
691    ///
692    /// Ingest and retrieval are separate concerns that [`with_recall`] happens
693    /// to bundle, and the bundling is a trap: capture only ever happens when
694    /// `self.recall` is set, so a host that wants a different search tool — one
695    /// scoped per tenant, or one that copes with a language the backend's index
696    /// does not — has no way to get the writes without also getting
697    /// `session_search`, and ends up offering the model two overlapping tools
698    /// to choose between.
699    ///
700    /// Use this, then register whichever retrieval tool suits the deployment.
701    pub fn with_recall_ingest(mut self, store: Arc<dyn harness_core::RecallStore>) -> Self {
702        self.recall = Some(store);
703        self
704    }
705
706    /// After `with_recall`, also auto-inject top-k relevant past context at
707    /// session start (off by default — tool-only is prompt-cache friendly).
708    pub fn auto_inject(mut self) -> Self {
709        self.recall_auto_inject = true;
710        self
711    }
712
713    /// Enable the self-evolving learning loop: after a session that made
714    /// `>= cfg.nudge_interval` tool calls, fork a review subagent (white-listed to
715    /// `cfg.tools`) to update skills + memory from the transcript. Best-effort.
716    pub fn with_learning_loop(mut self, cfg: LearningConfig) -> Self {
717        self.learning = Some(cfg);
718        self
719    }
720
721    /// Set the default response format for all runs through this loop. See
722    /// [`ResponseFormat`]. For typed deserialisation, prefer `run_typed::<T>()`.
723    pub fn with_response_format(mut self, fmt: ResponseFormat) -> Self {
724        self.response_format = fmt;
725        self
726    }
727
728    /// Shortcut for `with_response_format(ResponseFormat::JsonSchema { name, schema })`.
729    /// Accepts a raw `serde_json::Value` so callers can hand-roll the schema or
730    /// pull it from `schemars::schema_for!(T)`.
731    pub fn with_response_schema(self, name: impl Into<String>, schema: serde_json::Value) -> Self {
732        self.with_response_format(ResponseFormat::JsonSchema {
733            name: name.into(),
734            schema,
735        })
736    }
737
738    pub async fn run(&self, task: Task, world: &mut World) -> Result<Outcome, HarnessError> {
739        let max = harness_core::Policy::default().max_iters;
740        self.run_with_max_iters(task, world, max).await
741    }
742
743    /// Run, and hand back the evidence alongside the result.
744    ///
745    /// The [`Receipt`] is built here rather than by the caller because the loop
746    /// already knows the two things a caller would otherwise have to restate —
747    /// the task and the model — and restating them is how a receipt ends up
748    /// describing a different run than the one that happened. `now_ms` stays a
749    /// parameter: this crate does not read the clock, so a receipt is
750    /// reproducible in a test.
751    pub async fn run_receipted(
752        &self,
753        task: Task,
754        world: &mut World,
755        now_ms: i64,
756    ) -> Result<(Outcome, Receipt), HarnessError> {
757        let description = task.description.clone();
758        let handle = self.model.info().handle;
759        let outcome = self.run(task, world).await?;
760        let receipt = ReceiptBuilder::new(description, handle, now_ms).build(&outcome);
761        Ok((outcome, receipt))
762    }
763
764    /// Advance a [`Goal`] by one phase, recording the result durably.
765    ///
766    /// Returns `None` when every phase is done — so a resume loop is
767    /// `while let Some(..) = loop_.run_goal(..).await?`.
768    ///
769    /// The phase is marked `Running` and **saved before the model starts**, so
770    /// a process that dies mid-run leaves a goal that says where it was rather
771    /// than one that looks untouched. It is saved again afterwards on both
772    /// paths. That second save on the failure path is the whole reason this
773    /// method exists: written out by hand at each call site it is four lines,
774    /// and the failure branch is the one that gets forgotten — which loses
775    /// exactly the run you most wanted a record of.
776    pub async fn run_goal(
777        &self,
778        goal: &mut Goal,
779        store: &GoalStore,
780        world: &mut World,
781        now_ms: i64,
782    ) -> Result<Option<(Outcome, Receipt)>, HarnessError> {
783        let Some(i) = goal.start_current(now_ms) else {
784            return Ok(None);
785        };
786        let _ = store.save(goal);
787
788        let task = Task {
789            description: goal.brief(),
790            source: None,
791            deadline: None,
792        };
793        let result = self.run_receipted(task, world, now_ms).await;
794
795        match &result {
796            Ok((_, receipt)) => {
797                if receipt.passed {
798                    goal.finish(i, receipt.summary(), now_ms);
799                } else {
800                    goal.fail(i, receipt.summary(), now_ms);
801                }
802            }
803            // A run that errored outright still happened, and the goal has to
804            // say so or a resume will retry it as though it were untouched.
805            Err(e) => goal.fail(i, format!("the run errored: {e}"), now_ms),
806        }
807        let _ = store.save(goal);
808
809        result.map(Some)
810    }
811
812    pub async fn run_with_max_iters(
813        &self,
814        task: Task,
815        world: &mut World,
816        max_iters: u32,
817    ) -> Result<Outcome, HarnessError> {
818        self.run_with_seed_history(task, Vec::new(), world, max_iters)
819            .await
820    }
821
822    /// Run the agent and deserialise the terminal reply into `T`.
823    ///
824    /// The schema for `T` is derived via `schemars::schema_for!(T)` and
825    /// installed as `ResponseFormat::JsonSchema` for this run only — any
826    /// pre-existing `self.response_format` is ignored. On success the
827    /// returned `T` is parsed from `Outcome::Done.text` (or, on budget
828    /// exhaustion, from `Outcome::BudgetExhausted.last_text`).
829    ///
830    /// Errors:
831    /// - `HarnessError::Other` if the model returns no text at all
832    /// - `HarnessError::Other` if `serde_json::from_str::<T>(text)` fails —
833    ///   the original text is included in the message for debugging.
834    pub async fn run_typed<T>(&self, task: Task, world: &mut World) -> Result<T, HarnessError>
835    where
836        T: serde::de::DeserializeOwned + schemars::JsonSchema + 'static,
837    {
838        let max = harness_core::Policy::default().max_iters;
839        self.run_typed_with_max_iters::<T>(task, world, max).await
840    }
841
842    /// Like `run_typed` but with explicit `max_iters`.
843    pub async fn run_typed_with_max_iters<T>(
844        &self,
845        task: Task,
846        world: &mut World,
847        max_iters: u32,
848    ) -> Result<T, HarnessError>
849    where
850        T: serde::de::DeserializeOwned + schemars::JsonSchema + 'static,
851    {
852        let schema_root = schemars::schema_for!(T);
853        let schema = serde_json::to_value(&schema_root)
854            .map_err(|e| HarnessError::Other(format!("response schema: {e}")))?;
855        let name = std::any::type_name::<T>()
856            .rsplit("::")
857            .next()
858            .unwrap_or("response")
859            .to_string();
860        let fmt = ResponseFormat::JsonSchema { name, schema };
861        let outcome = self
862            .run_with_response_format(task, world, max_iters, fmt)
863            .await?;
864        let text = match outcome {
865            Outcome::Done { text: Some(t), .. }
866            | Outcome::BudgetExhausted {
867                last_text: Some(t), ..
868            }
869            | Outcome::Stuck {
870                last_text: Some(t), ..
871            } => t,
872            Outcome::Done { text: None, .. } => {
873                return Err(HarnessError::Other(
874                    "run_typed: model returned no text".into(),
875                ));
876            }
877            Outcome::Stuck {
878                last_text: None, ..
879            } => {
880                return Err(HarnessError::Other(
881                    "run_typed: agent stuck with no text".into(),
882                ));
883            }
884            Outcome::BudgetExhausted {
885                last_text: None, ..
886            } => {
887                return Err(HarnessError::Other(
888                    "run_typed: budget exhausted with no text".into(),
889                ));
890            }
891        };
892        serde_json::from_str::<T>(&text).map_err(|e| {
893            HarnessError::Other(format!(
894                "run_typed: decode {} failed: {e} — raw text was: {text}",
895                std::any::type_name::<T>()
896            ))
897        })
898    }
899
900    /// Run with a one-off `ResponseFormat` override (doesn't touch `self`).
901    pub async fn run_with_response_format(
902        &self,
903        task: Task,
904        world: &mut World,
905        max_iters: u32,
906        fmt: ResponseFormat,
907    ) -> Result<Outcome, HarnessError> {
908        // Borrow checker won't let us swap `self.response_format` because
909        // `self` is `&`. Easiest workaround: hand-roll the same setup that
910        // `run_with_seed_history` does, but with our `fmt`. We do this by
911        // calling through a private helper.
912        self.run_with_seed_history_and_format(task, Vec::new(), world, max_iters, Some(fmt))
913            .await
914    }
915
916    async fn run_with_seed_history_and_format(
917        &self,
918        task: Task,
919        seed: Vec<Turn>,
920        world: &mut World,
921        max_iters: u32,
922        fmt_override: Option<ResponseFormat>,
923    ) -> Result<Outcome, HarnessError> {
924        let mut ctx = Context::new(task);
925        ctx.policy.max_iters = max_iters;
926        ctx.tools = self.tools.schemas();
927        ctx.history = seed;
928        ctx.response_format = fmt_override.unwrap_or_else(|| self.response_format.clone());
929        self.run_built_context(ctx, world).await
930    }
931
932    /// Like `run_with_max_iters` but seeds `ctx.history` with `seed` **before**
933    /// the current user task is appended. Use this for multi-turn REPLs so
934    /// prior conversation lives in `ctx.history` (where the Compactor can see
935    /// it) instead of being concatenated into `task.description` (where it
936    /// previously bypassed compaction entirely — see audit #2).
937    pub async fn run_with_seed_history(
938        &self,
939        task: Task,
940        seed: Vec<Turn>,
941        world: &mut World,
942        max_iters: u32,
943    ) -> Result<Outcome, HarnessError> {
944        self.run_with_seed_and_metadata(task, seed, Default::default(), world, max_iters)
945            .await
946    }
947
948    /// Like [`run_with_seed_history`](Self::run_with_seed_history) but also seeds
949    /// `ctx.metadata` with per-request key/values. Hooks and a
950    /// [`ModelRouter`](harness_models::ModelRouter) read this map — e.g.
951    /// `audit.actor` / `audit.session` for the audit trail, or
952    /// `router.keep_local` to pin a request to the local model. This is the
953    /// entry point a serving layer uses to pass caller identity and routing
954    /// flags into a single, shared, reused loop.
955    pub async fn run_with_seed_and_metadata(
956        &self,
957        task: Task,
958        seed: Vec<Turn>,
959        metadata: std::collections::BTreeMap<String, serde_json::Value>,
960        world: &mut World,
961        max_iters: u32,
962    ) -> Result<Outcome, HarnessError> {
963        let mut ctx = Context::new(task);
964        ctx.policy.max_iters = max_iters;
965        ctx.tools = self.tools.schemas();
966        ctx.history = seed;
967        ctx.metadata = metadata;
968        ctx.response_format = self.response_format.clone();
969        self.run_built_context(ctx, world).await
970    }
971
972    /// Start a persistent multi-turn [`Session`]. Each `turn` re-runs the loop
973    /// against the accumulated history with a **stable prefix** (system +
974    /// name-sorted tool schemas), so a provider's prefix cache (e.g. DeepSeek's,
975    /// ~10% price on cache-hit tokens) hits across turns instead of paying full
976    /// price to re-read the same bytes every round. For maximum hit rate, keep
977    /// your guides' output stable (put per-turn volatile context in the message,
978    /// not a recomputed system guide).
979    pub fn session(&self) -> Session<'_, M> {
980        Session {
981            loop_: self,
982            history: Vec::new(),
983            max_iters: harness_core::Policy::default().max_iters,
984        }
985    }
986
987    /// Inner ReAct loop on an already-prepared `Context`. Use the public
988    /// `run*` methods unless you need to inject a non-standard `Context`
989    /// (e.g. `run_with_response_format` does to apply a one-off
990    /// `ResponseFormat` without mutating `self`).
991    async fn run_built_context(
992        &self,
993        mut ctx: Context,
994        world: &mut World,
995    ) -> Result<Outcome, HarnessError> {
996        if ctx.system.is_empty() && !self.system.is_empty() {
997            ctx.system = self.system.clone();
998        }
999
1000        // Size the context budget to the model actually in use.
1001        //
1002        // Compaction fires at a fraction of `max_input_tokens`, so a value
1003        // unrelated to the model is wrong in one of two directions: too high and
1004        // the provider rejects the request before the compactor ever runs (a 32k
1005        // model under the 150k default would need 112,500 tokens to trigger, and
1006        // it cannot hold that many); too low and the loop discards history it
1007        // could have kept. Nothing read `ModelInfo::context_window` before this —
1008        // the framework's own defaults disagreed, 150,000 against 128,000.
1009        //
1010        // Only when the caller left the default in place; an explicit policy is
1011        // a decision and stays untouched. The output allowance is reserved,
1012        // because the window is shared between the prompt and the reply.
1013        if let Some(n) = self.max_input_tokens {
1014            // An explicit cap wins over both the default and the model window.
1015            ctx.policy.max_input_tokens = n.max(1);
1016        } else if ctx.policy.max_input_tokens == harness_core::Policy::default().max_input_tokens {
1017            let window = self.model.info().context_window;
1018            if window > 0 {
1019                // Reserve room for the reply, but never let the reservation eat
1020                // the window: an 8k model with the default 8k output allowance
1021                // would leave 0 tokens for input, and every turn — a 26-token
1022                // one included — would run all five compaction stages against a
1023                // budget of 1. Cap the reservation at a quarter of the window.
1024                let reserve = ctx.policy.max_output_tokens.min(window / 4);
1025                ctx.policy.max_input_tokens = window.saturating_sub(reserve).max(1);
1026            }
1027        }
1028
1029        self.hooks.fire(
1030            &Event::SessionStart {
1031                source: SessionSource::Startup,
1032            },
1033            world,
1034        );
1035
1036        // ── recall: resolve owner/session, ensure the session row ──
1037        let (recall_owner, recall_session) = if self.recall.is_some() {
1038            use std::sync::atomic::Ordering;
1039            let owner = crate::recall_owner(world);
1040            let session = world
1041                .profile
1042                .extra
1043                .get("recall_session")
1044                .and_then(|v| v.as_str())
1045                .map(|s| s.to_string())
1046                .unwrap_or_else(|| {
1047                    format!(
1048                        "sess-{}-{}",
1049                        world.clock.now_ms(),
1050                        RECALL_SEQ.fetch_add(1, Ordering::SeqCst)
1051                    )
1052                });
1053            if let Some(store) = &self.recall {
1054                let meta = harness_core::SessionMeta::new(&session, world.clock.now_ms());
1055                if let Err(e) = store.ensure_session(&owner, &session, &meta).await {
1056                    tracing::warn!(error = %e, "recall ensure_session failed");
1057                }
1058            }
1059            (owner, session)
1060        } else {
1061            (String::new(), String::new())
1062        };
1063
1064        let recall_guide: Option<Arc<dyn Guide>> = if self.recall_auto_inject {
1065            if self.recall.is_none() {
1066                tracing::warn!(
1067                    "auto_inject() set but no recall store — call with_recall(store) first; skipping recall guide"
1068                );
1069                None
1070            } else {
1071                self.recall
1072                    .clone()
1073                    .map(|s| Arc::new(crate::RecallGuide::new(s)) as Arc<dyn Guide>)
1074            }
1075        } else {
1076            None
1077        };
1078        let all_guides: Vec<&Arc<dyn Guide>> =
1079            self.guides.iter().chain(recall_guide.iter()).collect();
1080        for g in &all_guides {
1081            if g.scope().matches(&ctx.task) {
1082                self.hooks.fire(&Event::PreGuide { guide: g.id() }, world);
1083                g.apply(&mut ctx, world).await?;
1084                self.hooks.fire(&Event::PostGuide { guide: g.id() }, world);
1085            }
1086        }
1087
1088        ctx.history.push(Turn {
1089            role: TurnRole::User,
1090            blocks: vec![Block::Text(ctx.task.description.clone())],
1091        });
1092
1093        if self.recall.is_some() {
1094            self.recall_append(
1095                &recall_owner,
1096                &recall_session,
1097                harness_core::RecallMessage::new(
1098                    "user",
1099                    ctx.task.description.clone(),
1100                    world.clock.now_ms(),
1101                ),
1102            )
1103            .await;
1104        }
1105
1106        // Running totals — surface to caller even on BudgetExhausted.
1107        let mut tools_called: u32 = 0;
1108        let mut total_usage = harness_core::Usage::default();
1109        let mut last_text: Option<String> = None;
1110
1111        // Stuck-detector state: the previous round's tool-call fingerprint and
1112        // how many consecutive rounds have repeated it.
1113        let mut last_fingerprint: Option<String> = None;
1114        let mut repeat_count: u32 = 0;
1115        // Read-only calls already answered this run, cleared whenever anything
1116        // mutates the world. See `ToolResultPolicy::dedupe_repeats`.
1117        let mut answered: std::collections::HashSet<String> = std::collections::HashSet::new();
1118        // How many times the model has stopped mid-work with nothing to show.
1119        let mut acceptance_retries_left = self.acceptance_retries;
1120
1121        // The contract as it stood before the model touched anything. Taken
1122        // now, not at verdict time, because the whole point is to compare
1123        // against a state the model has not had the chance to influence.
1124        // Set once a sealed file is found to have moved; makes the failure
1125        // terminal and carries the reason out to the caller.
1126        let mut seal_breached: Option<String> = None;
1127        let sealed_before: crate::seal::SealSet = {
1128            let paths: Vec<std::path::PathBuf> =
1129                self.acceptance.iter().flat_map(|a| a.seals()).collect();
1130            if paths.is_empty() {
1131                crate::seal::SealSet::default()
1132            } else {
1133                crate::seal::SealSet::capture(&world.repo.root, paths)
1134            }
1135        };
1136
1137        // How far the loop actually got. Equal to `max_iters` on a normal
1138        // budget exhaustion, less when the wall clock stopped it first.
1139        let mut iters_done = ctx.policy.max_iters;
1140        let mut deadline_reached = false;
1141
1142        for iter in 0..ctx.policy.max_iters {
1143            // ── wall-clock budget ───────────────────────────────────────
1144            // `max_iters` bounds *steps*, which says nothing about elapsed
1145            // time: one iteration can be a 100ms read or a 20-minute build.
1146            // An unattended run is bounded in hours, not rounds, so a task
1147            // may carry a deadline — and reaching it goes through the same
1148            // forced-synthesis exit as a spent step budget, so the caller
1149            // gets the conclusion of the work rather than a dropped future.
1150            if let Some(at) = ctx.task.deadline
1151                && world.clock.now_ms() >= at
1152            {
1153                tracing::warn!(
1154                    iter,
1155                    deadline = at,
1156                    "task deadline reached — forcing a final answer"
1157                );
1158                iters_done = iter;
1159                deadline_reached = true;
1160                break;
1161            }
1162            self.hooks.fire(&Event::Heartbeat { iter }, world);
1163
1164            // Compaction with hysteresis: only once over the high-water mark,
1165            // then escalate stage-by-stage, re-estimating after each, and stop
1166            // the moment we're back under target. Avoids over-compacting to the
1167            // lossy AutoCompact stage and avoids rewriting history (→ prefix
1168            // cache miss) every turn. Token estimate is calibrated against the
1169            // last real `input_tokens` via `CALIBRATION_KEY`.
1170            let mut budget = self.compactor.budget(&ctx);
1171            if budget.ratio() > self.compaction.high_water {
1172                for stage in CompactionStage::ALL {
1173                    if budget.ratio() <= self.compaction.target {
1174                        break;
1175                    }
1176                    self.hooks.fire(&Event::PreCompact { stage }, world);
1177                    let before = budget.used;
1178                    let turns_before = ctx.history.len();
1179                    self.compactor.compact(stage, &mut ctx).await?;
1180                    budget = self.compactor.budget(&ctx);
1181                    // Compaction used to leave no trace anywhere: not in the
1182                    // log, not in the transcript. That is a bad property for
1183                    // the one mechanism that rewrites the conversation, and it
1184                    // is how a compaction bug that killed long runs stayed
1185                    // invisible — the only symptom was a provider error with
1186                    // no hint of what had touched the history.
1187                    tracing::info!(
1188                        stage = ?stage,
1189                        tokens_before = before,
1190                        tokens_after = budget.used,
1191                        turns_before,
1192                        turns_after = ctx.history.len(),
1193                        "compacted context"
1194                    );
1195                    self.hooks.fire(
1196                        &Event::PostCompact {
1197                            stage,
1198                            before,
1199                            after: budget.used,
1200                        },
1201                        world,
1202                    );
1203                }
1204            }
1205
1206            // Per-iteration guides — recall-style adapters that want to
1207            // refresh their injected context every turn (e.g. MemoryGuide
1208            // re-recalling against the latest user message). Default
1209            // `apply_before_iter` is a no-op, so this loop is cheap for
1210            // guides that don't override it.
1211            for g in &all_guides {
1212                if g.scope().matches(&ctx.task)
1213                    && let Err(e) = g.apply_before_iter(&mut ctx, world).await
1214                {
1215                    tracing::warn!(guide = %g.id(), error = %e, "apply_before_iter failed; continuing");
1216                }
1217            }
1218
1219            self.hooks.fire(&Event::PreModel { ctx: &ctx }, world);
1220            let out = if self.streaming {
1221                self.complete_via_stream(&ctx, world).await?
1222            } else {
1223                self.model.complete(&ctx).await?
1224            };
1225            self.hooks.fire(&Event::PostModel { out: &out }, world);
1226
1227            // Calibrate the compactor against ground truth: `ctx` still holds
1228            // exactly what we just sent, so `budget().used` is the estimate for
1229            // it. Nudge the stored correction so estimate·correction ≈ the
1230            // model's real `input_tokens` next time. Self-correcting (converges
1231            // even as the raw estimate drifts); clamped so one odd turn can't
1232            // blow it up. This is what makes compaction fire at the right moment
1233            // for *this* model + language instead of a blind char heuristic.
1234            if out.usage.input_tokens > 0 {
1235                let used = self.compactor.budget(&ctx).used;
1236                if used > 0 {
1237                    let prev = ctx
1238                        .metadata
1239                        .get(CALIBRATION_KEY)
1240                        .and_then(|v| v.as_f64())
1241                        .filter(|f| f.is_finite() && *f > 0.0)
1242                        .unwrap_or(1.0);
1243                    let next =
1244                        (prev * out.usage.input_tokens as f64 / used as f64).clamp(0.1, 10.0);
1245                    ctx.metadata
1246                        .insert(CALIBRATION_KEY.into(), serde_json::json!(next));
1247                }
1248            }
1249
1250            // Accumulate usage even if the run later exhausts budget.
1251            total_usage.input_tokens += out.usage.input_tokens;
1252            total_usage.output_tokens += out.usage.output_tokens;
1253            total_usage.cached_input_tokens += out.usage.cached_input_tokens;
1254            total_usage.cache_write_input_tokens += out.usage.cache_write_input_tokens;
1255            if let Some(t) = &out.text {
1256                last_text = Some(t.clone());
1257            }
1258            ctx.push_model_output(&out);
1259
1260            if self.recall.is_some() {
1261                let calls = if out.tool_calls.is_empty() {
1262                    None
1263                } else {
1264                    serde_json::to_string(&out.tool_calls).ok()
1265                };
1266                let mut m = harness_core::RecallMessage::new(
1267                    "assistant",
1268                    out.text.clone().unwrap_or_default(),
1269                    world.clock.now_ms(),
1270                );
1271                m.tool_calls = calls;
1272                self.recall_append(&recall_owner, &recall_session, m).await;
1273            }
1274
1275            if out.tool_calls.is_empty() {
1276                // The model stopping is its opinion that the work is done.
1277                // Before taking it as fact, run whatever the caller said
1278                // "done" actually means. A failure goes back as an instruction
1279                // and the loop carries on; the retry cap keeps a model that
1280                // ignores corrections from eating the budget.
1281                let mut verdict: Option<Verdict> = None;
1282                if !self.acceptance.is_empty() {
1283                    // Record this turn first — the checks read the transcript.
1284                    let mut probe = ctx.clone();
1285                    probe.history.push(Turn {
1286                        role: TurnRole::Assistant,
1287                        blocks: out
1288                            .text
1289                            .as_deref()
1290                            .filter(|t| !t.trim().is_empty())
1291                            .map(|t| vec![Block::Text(t.to_string())])
1292                            .unwrap_or_default(),
1293                    });
1294                    for check in &self.acceptance {
1295                        let v = check.check(&probe, world).await;
1296                        self.hooks.fire(
1297                            &Event::AcceptanceChecked {
1298                                name: check.name(),
1299                                passed: v.passed,
1300                                reason: &v.reason,
1301                            },
1302                            world,
1303                        );
1304                        if !v.passed {
1305                            tracing::info!(
1306                                check = check.name(),
1307                                reason = %v.reason,
1308                                "acceptance failed"
1309                            );
1310                            verdict = Some(v);
1311                            break;
1312                        }
1313                    }
1314                    // Everything passed. Say so explicitly: "checked, and it
1315                    // holds up" is a different claim from "nobody looked", and
1316                    // the host has to be able to tell them apart.
1317                    verdict = verdict.or_else(|| Some(Verdict::passed()));
1318                }
1319
1320                // A pass is only worth the contract it was measured against.
1321                // Re-read the sealed files and refuse if any moved.
1322                if !sealed_before.is_empty() && verdict.as_ref().is_some_and(|v| v.passed) {
1323                    let paths: Vec<std::path::PathBuf> =
1324                        self.acceptance.iter().flat_map(|a| a.seals()).collect();
1325                    let now = crate::seal::SealSet::capture(&world.repo.root, paths);
1326                    let breaches = sealed_before.breaches(&now);
1327                    if !breaches.is_empty() {
1328                        let what = breaches
1329                            .iter()
1330                            .map(|b| b.describe())
1331                            .collect::<Vec<_>>()
1332                            .join("; ");
1333                        tracing::error!(
1334                            breaches = %what,
1335                            "acceptance contract changed during the run — refusing the pass"
1336                        );
1337                        self.hooks
1338                            .fire(&Event::SealBreached { detail: &what }, world);
1339                        // Deliberately NOT a retry. Every other acceptance
1340                        // failure is handed back as an instruction because the
1341                        // model can act on it; this one would be handing back
1342                        // "you edited the gate", to the party that edited it,
1343                        // with the file still writable. The run is over.
1344                        seal_breached = Some(what);
1345                        verdict = Some(Verdict::failed(
1346                            "the acceptance contract was modified during this run",
1347                        ));
1348                    }
1349                }
1350
1351                if let Some(v) = verdict.clone().filter(|v| !v.passed)
1352                    && seal_breached.is_none()
1353                    && acceptance_retries_left > 0
1354                    && iter + 1 < ctx.policy.max_iters
1355                {
1356                    acceptance_retries_left -= 1;
1357                    ctx.history.push(Turn {
1358                        role: TurnRole::User,
1359                        blocks: vec![Block::Text(v.reason)],
1360                    });
1361                    continue;
1362                }
1363
1364                self.hooks.fire(&Event::TaskCompleted, world);
1365                self.hooks.fire(&Event::SessionEnd, world);
1366                self.run_learning_review(&ctx, world, tools_called).await;
1367                // Thinking models (e.g. Qwen3 via Ollama) sometimes emit the
1368                // whole answer into the reasoning channel and leave `text`
1369                // empty. Fall back to the reasoning so the turn isn't blank —
1370                // but `verified` says whether anyone agreed it was done.
1371                let text = out
1372                    .text
1373                    .filter(|t| !t.trim().is_empty())
1374                    .or_else(|| out.reasoning.filter(|r| !r.trim().is_empty()));
1375                return Ok(Outcome::Done {
1376                    text,
1377                    iters: iter + 1,
1378                    tools_called,
1379                    usage: total_usage,
1380                    verified: verdict,
1381                    contract: sealed_before.clone(),
1382                    seal_breach: seal_breached,
1383                });
1384            }
1385
1386            // Stuck detection runs *after* dispatch — see the block below the
1387            // tool loop, which needs this round's results to judge progress.
1388
1389            // Parallel-safe prefetch: dispatch the *leading run* of read-only
1390            // tool calls concurrently (a mutating tool is a serial barrier).
1391            // The sequential loop below still processes every call in order —
1392            // hooks, sensors, and history stay ordered — only the dispatch IO
1393            // overlaps. Reads before any write are safe; anything at/after the
1394            // first mutating call runs on the normal path.
1395            // Digest of what each tool handed back this round, for the stuck
1396            // check below. Digests rather than the payloads: results are
1397            // already capped, but a per-round copy of every one of them is
1398            // still a cost the detector does not need to pay.
1399            let mut round_results: Vec<u64> = Vec::new();
1400
1401            let mut prefetched: HashMap<String, ToolResult> = HashMap::new();
1402            {
1403                let lead: Vec<&_> = out
1404                    .tool_calls
1405                    .iter()
1406                    .take_while(|c| {
1407                        self.tools.risk(&c.name) == Some(harness_core::ToolRisk::ReadOnly)
1408                    })
1409                    .collect();
1410                if lead.len() > 1 {
1411                    let futs = lead.iter().map(|c| {
1412                        let mut w = world.clone();
1413                        let action = Action {
1414                            tool: c.name.clone(),
1415                            call_id: c.id.clone(),
1416                            args: c.args.clone(),
1417                        };
1418                        async move {
1419                            let r = self.dispatch_bounded(&action, &mut w).await;
1420                            (action.call_id, r)
1421                        }
1422                    });
1423                    for (id, r) in futures::future::join_all(futs).await {
1424                        prefetched.insert(id, r);
1425                    }
1426                }
1427            }
1428
1429            for call in &out.tool_calls {
1430                let action = Action {
1431                    tool: call.name.clone(),
1432                    call_id: call.id.clone(),
1433                    args: call.args.clone(),
1434                };
1435
1436                // PreToolUse hook can deny destructive actions
1437                if let HookOutcome::Deny { reason } = self
1438                    .hooks
1439                    .fire(&Event::PreToolUse { action: &action }, world)
1440                {
1441                    ctx.history.push(Turn {
1442                        role: TurnRole::Tool,
1443                        blocks: vec![Block::ToolResult {
1444                            call_id: action.call_id.clone(),
1445                            content: serde_json::json!({
1446                                "ok": false,
1447                                "denied_by_hook": reason,
1448                            }),
1449                        }],
1450                    });
1451                    if self.recall.is_some() {
1452                        self.recall_append(
1453                            &recall_owner,
1454                            &recall_session,
1455                            harness_core::RecallMessage::new(
1456                                "tool",
1457                                format!("[denied by hook] {reason}"),
1458                                world.clock.now_ms(),
1459                            )
1460                            .with_tool_name(action.tool.clone()),
1461                        )
1462                        .await;
1463                    }
1464                    continue;
1465                }
1466
1467                // Use the concurrently-prefetched result if we have one;
1468                // otherwise dispatch now.
1469                let result = if let Some(r) = prefetched.remove(&action.call_id) {
1470                    r
1471                } else {
1472                    self.dispatch_bounded(&action, world).await
1473                };
1474                tools_called += 1;
1475
1476                // Decide the final payload *before* announcing the result, so
1477                // hooks, telemetry and the context all describe the same thing:
1478                // an audit that logs a 200 KB blob the model never saw is not an
1479                // audit of what happened.
1480                let result = ToolResult {
1481                    content: self.shape_result(&action, &result, &mut answered, &world.repo.root),
1482                    ..result
1483                };
1484                self.hooks.fire(
1485                    &Event::PostToolUse {
1486                        action: &action,
1487                        result: &result,
1488                    },
1489                    world,
1490                );
1491
1492                round_results.push(result_digest(&action.tool, &result.content));
1493
1494                ctx.history.push(Turn {
1495                    role: TurnRole::Tool,
1496                    blocks: vec![Block::ToolResult {
1497                        call_id: action.call_id.clone(),
1498                        content: result.content.clone(),
1499                    }],
1500                });
1501
1502                if self.recall.is_some() {
1503                    let body = serde_json::to_string(&result.content).unwrap_or_default();
1504                    self.recall_append(
1505                        &recall_owner,
1506                        &recall_session,
1507                        harness_core::RecallMessage::new("tool", body, world.clock.now_ms())
1508                            .with_tool_name(action.tool.clone()),
1509                    )
1510                    .await;
1511                }
1512
1513                // run self-correct sensors
1514                let mut all_signals = Vec::new();
1515                for s in &self.sensors {
1516                    if s.stage() != Stage::SelfCorrect {
1517                        continue;
1518                    }
1519                    self.hooks.fire(&Event::PreSensor { sensor: s.id() }, world);
1520                    let sigs = s.observe(&action, world).await.unwrap_or_else(|e| {
1521                        tracing::warn!(?e, "sensor failed");
1522                        Vec::new()
1523                    });
1524                    self.hooks.fire(
1525                        &Event::PostSensor {
1526                            sensor: s.id(),
1527                            signals: &sigs,
1528                        },
1529                        world,
1530                    );
1531                    all_signals.extend(sigs);
1532                }
1533                if !all_signals.is_empty() {
1534                    let bundle = SignalSet::new(all_signals);
1535                    let (patches, remaining) = bundle.partition_auto_fix();
1536
1537                    // audit #7: each patch goes through PreAutoFix.
1538                    // Hooks can Deny (skip silently). Default safelist on
1539                    // RunCommand catches the obvious misuses with no hook.
1540                    let approved: Vec<harness_core::FixPatch> = patches.into_iter().filter(|p| {
1541                        if !is_default_safe_fix(p) {
1542                            tracing::warn!(?p, "auto-fix rejected by default safelist (use PreAutoFix hook to override)");
1543                            self.hooks.fire(&Event::PostAutoFix { patch: p, applied: false }, world);
1544                            return false;
1545                        }
1546                        match self.hooks.fire(&Event::PreAutoFix { patch: p }, world) {
1547                            HookOutcome::Deny { reason } => {
1548                                tracing::warn!(?p, %reason, "auto-fix denied by hook");
1549                                self.hooks.fire(&Event::PostAutoFix { patch: p, applied: false }, world);
1550                                false
1551                            }
1552                            _ => true,
1553                        }
1554                    }).collect();
1555
1556                    let applied = apply_patches(&approved, world).await;
1557                    // Emit PostAutoFix for each approved patch with the application result.
1558                    for (i, p) in approved.iter().enumerate() {
1559                        self.hooks.fire(
1560                            &Event::PostAutoFix {
1561                                patch: p,
1562                                applied: i < applied.len(),
1563                            },
1564                            world,
1565                        );
1566                    }
1567                    if !applied.is_empty() {
1568                        ctx.push_feedback(vec![harness_core::Signal {
1569                            severity: harness_core::Severity::Hint,
1570                            origin: "auto-fix".into(),
1571                            message: format!(
1572                                "applied {} auto-fix patch(es): {applied:?}",
1573                                applied.len()
1574                            ),
1575                            agent_hint: Some(
1576                                "re-check the affected files before continuing".into(),
1577                            ),
1578                            auto_fix: None,
1579                            location: None,
1580                        }]);
1581                    }
1582                    if remaining.has_blocking() {
1583                        ctx.push_feedback(remaining.signals);
1584                    }
1585                }
1586            }
1587
1588            // ── stuck detection ─────────────────────────────────────────
1589            // A round repeats when the request AND what came back were both
1590            // identical. The same call whose *result moved* is the model
1591            // watching something progress — polling a build, a background
1592            // job, a queue — not spinning, which is why this runs after
1593            // dispatch: before it, this round's results do not exist yet and
1594            // the check could only compare the request. Judging on the
1595            // request alone aborted runs that were making progress, and a
1596            // long unattended task is mostly made of such waits.
1597            //
1598            // A poll whose result never changes still counts, and still
1599            // aborts: burning model calls on a thing that is not moving is
1600            // exactly what this guard is for. What bounds a *legitimate*
1601            // long wait is `max_iters`, not this.
1602            if self.stuck.enabled {
1603                let fp = format!(
1604                    "{}#{}",
1605                    tool_call_fingerprint(&out.tool_calls),
1606                    round_results
1607                        .iter()
1608                        .map(u64::to_string)
1609                        .collect::<Vec<_>>()
1610                        .join(",")
1611                );
1612                if last_fingerprint.as_ref() == Some(&fp) {
1613                    repeat_count += 1;
1614                } else {
1615                    repeat_count = 1;
1616                    last_fingerprint = Some(fp);
1617                }
1618
1619                if repeat_count >= self.stuck.abort_after {
1620                    let reason = format!(
1621                        "repeated the same tool call {repeat_count}× with an unchanging result"
1622                    );
1623                    tracing::warn!(repeated = repeat_count, "stuck: aborting run");
1624                    self.hooks.fire(&Event::SessionEnd, world);
1625                    return Ok(Outcome::Stuck {
1626                        reason,
1627                        repeated: repeat_count,
1628                        iters: iter + 1,
1629                        last_text,
1630                        tools_called,
1631                        usage: total_usage,
1632                    });
1633                }
1634
1635                if repeat_count == self.stuck.nudge_after {
1636                    tracing::warn!(
1637                        repeated = repeat_count,
1638                        "stuck: nudging model to change approach"
1639                    );
1640                    ctx.push_feedback(vec![harness_core::Signal {
1641                        severity: harness_core::Severity::Warn,
1642                        origin: "stuck-detector".into(),
1643                        message: format!(
1644                            "You have issued the same tool call {repeat_count} rounds in a row \
1645                             and the result has not changed."
1646                        ),
1647                        agent_hint: Some(
1648                            "Stop repeating it. Inspect the actual tool result/error, try a \
1649                             different approach, or give your final answer with no tool call. \
1650                             If you are waiting on something, wait inside one call rather than \
1651                             polling in a tight loop."
1652                                .into(),
1653                        ),
1654                        auto_fix: None,
1655                        location: None,
1656                    }]);
1657                }
1658            }
1659        }
1660        // ── Budget exhausted ─────────────────────────────────────────
1661        // Force a final synthesis pass with tools DISABLED. Otherwise the
1662        // model often spins on tool calls right up to the budget cap and
1663        // never emits a text conclusion, leaving the caller with nothing
1664        // but `last_text` from some earlier intermediate turn (or None).
1665        //
1666        // The synthesis call is "free" — it costs one extra model call
1667        // beyond max_iters but doesn't count toward `iters`. The result
1668        // lands in `last_text` so callers display it as the answer.
1669        let synthesised = self
1670            .force_final_synthesis(&mut ctx, world, &mut total_usage)
1671            .await;
1672        if let Some(t) = synthesised {
1673            last_text = Some(t);
1674        }
1675
1676        self.hooks.fire(&Event::SessionEnd, world);
1677        self.run_learning_review(&ctx, world, tools_called).await;
1678        Ok(Outcome::BudgetExhausted {
1679            iters: iters_done,
1680            last_text,
1681            tools_called,
1682            usage: total_usage,
1683            deadline_reached,
1684        })
1685    }
1686
1687    /// Drive `Model::stream()` and assemble the result into a `ModelOutput`,
1688    /// firing `Event::ModelTokenDelta` for each text fragment along the way.
1689    ///
1690    /// Adapters that don't implement real streaming (e.g. `GeminiNative` /
1691    /// `AnthropicNative` today) fall back to the default trait impl, which
1692    /// runs `complete()` and emits the whole reply as a single delta. That
1693    /// works — the loop sees one big `ModelDelta::Text(...)` followed by
1694    /// `Stop`, fires one big `ModelTokenDelta`, and proceeds. So enabling
1695    /// `streaming` is safe regardless of which provider the user picked.
1696    async fn complete_via_stream(
1697        &self,
1698        ctx: &Context,
1699        world: &mut World,
1700    ) -> Result<ModelOutput, HarnessError> {
1701        use futures::StreamExt;
1702        let mut stream = self
1703            .model
1704            .stream(ctx)
1705            .await
1706            .map_err(harness_core::HarnessError::Model)?;
1707        let mut text = String::new();
1708        let mut reasoning = String::new();
1709        let mut usage = Usage::default();
1710        let mut stop_reason = StopReason::EndTurn;
1711        // Insertion-ordered map: index → (id, name, args). We can't use the
1712        // tool-call id as the primary key because the stream may emit args
1713        // chunks before the first chunk that carries the id; the OpenAI-compat
1714        // SSE parser already does its own buffering and surfaces `id` in
1715        // ToolCallStart, but be lenient with adapters that may interleave.
1716        let mut tool_starts: HashMap<String, (String, String)> = HashMap::new();
1717        let mut tool_order: Vec<String> = Vec::new();
1718        while let Some(item) = stream.next().await {
1719            let delta = item.map_err(harness_core::HarnessError::Model)?;
1720            match delta {
1721                ModelDelta::Text(t) => {
1722                    if !t.is_empty() {
1723                        self.hooks.fire(&Event::ModelTokenDelta { text: &t }, world);
1724                        text.push_str(&t);
1725                    }
1726                }
1727                ModelDelta::ToolCallStart { id, name } => {
1728                    if !tool_starts.contains_key(&id) {
1729                        tool_order.push(id.clone());
1730                    }
1731                    tool_starts
1732                        .entry(id)
1733                        .or_insert_with(|| (name, String::new()));
1734                }
1735                ModelDelta::ToolCallArgs { id, partial_json } => {
1736                    let entry = tool_starts
1737                        .entry(id.clone())
1738                        .or_insert_with(|| (String::new(), String::new()));
1739                    if !tool_order.iter().any(|k| k == &id) {
1740                        tool_order.push(id);
1741                    }
1742                    entry.1.push_str(&partial_json);
1743                }
1744                ModelDelta::ToolCallEnd { .. } => {}
1745                ModelDelta::Usage(u) => usage = u,
1746                ModelDelta::Stop(r) => stop_reason = r,
1747                ModelDelta::Reasoning(s) => {
1748                    // Streamed reasoning arrives as token fragments, not lines —
1749                    // concatenate verbatim (same as `text`), don't insert newlines.
1750                    reasoning.push_str(&s);
1751                }
1752                // ModelDelta is `#[non_exhaustive]`; ignore future variants
1753                // we don't yet understand.
1754                _ => {}
1755            }
1756        }
1757        let tool_calls: Vec<ToolCall> = tool_order
1758            .into_iter()
1759            .filter_map(|id| {
1760                tool_starts.remove(&id).map(|(name, args)| {
1761                    let args_v = serde_json::from_str::<serde_json::Value>(&args)
1762                        .unwrap_or(serde_json::Value::String(args));
1763                    ToolCall {
1764                        id,
1765                        name,
1766                        args: args_v,
1767                    }
1768                })
1769            })
1770            .collect();
1771        // Reconcile stop_reason with what actually came out — adapters
1772        // sometimes emit `Stop(EndTurn)` even after tool_calls, which would
1773        // confuse downstream consumers that branch on stop_reason alone.
1774        let stop_reason = if !tool_calls.is_empty() {
1775            StopReason::ToolUse
1776        } else {
1777            stop_reason
1778        };
1779        Ok(ModelOutput {
1780            text: if text.is_empty() { None } else { Some(text) },
1781            tool_calls,
1782            usage,
1783            stop_reason,
1784            reasoning: if reasoning.is_empty() {
1785                None
1786            } else {
1787                Some(reasoning)
1788            },
1789            // `ModelDelta` has no image variant: the verified image-output
1790            // path (Gemini image models over chat) answers non-streamed, so
1791            // there is nothing to accumulate here. A streaming provider that
1792            // emits images would need a `ModelDelta::Image` first.
1793            images: Vec::new(),
1794        })
1795    }
1796
1797    /// Best-effort append to the recall store. Never fails the turn.
1798    /// One tool call, under the per-call deadline. A timeout becomes an error
1799    /// *result* — the model sees it and routes around it — never a hung run.
1800    /// Errors are folded the same way: the loop's contract is that a tool call
1801    /// always produces a result turn.
1802    async fn dispatch_bounded(&self, action: &Action, world: &mut World) -> ToolResult {
1803        let fut = self.tools.dispatch(action, world);
1804        let dispatched = match self.tool_timeout {
1805            Some(deadline) => match tokio::time::timeout(deadline, fut).await {
1806                Ok(r) => r,
1807                Err(_) => {
1808                    tracing::warn!(
1809                        target: "harness.telemetry",
1810                        event = "tool.deadline",
1811                        "gen_ai.tool.name" = %action.tool,
1812                        seconds = deadline.as_secs(),
1813                    );
1814                    return ToolResult {
1815                        ok: false,
1816                        content: serde_json::json!({
1817                            "error": format!(
1818                                "tool call exceeded its {}s deadline and was cancelled; \
1819                                 the operation may be too broad — narrow it or try a \
1820                                 different approach",
1821                                deadline.as_secs()
1822                            ),
1823                            "timeout": true,
1824                        }),
1825                        trace: None,
1826                    };
1827                }
1828            },
1829            None => fut.await,
1830        };
1831        dispatched.unwrap_or_else(|e| ToolResult {
1832            ok: false,
1833            content: serde_json::json!({"error": e.to_string()}),
1834            trace: None,
1835        })
1836    }
1837
1838    /// What a tool result contributes to the context: repeat suppression first,
1839    /// then the size ceiling. A repeat that is also oversized collapses to the
1840    /// pointer rather than to a truncated copy of what the model already holds.
1841    fn shape_result(
1842        &self,
1843        action: &Action,
1844        result: &ToolResult,
1845        answered: &mut std::collections::HashSet<String>,
1846        root: &std::path::Path,
1847    ) -> serde_json::Value {
1848        if !(self.tool_results.dedupe_repeats && result.ok) {
1849            return self.cap_result(action, &result.content, root);
1850        }
1851        match self.tools.risk(&action.tool) {
1852            Some(harness_core::ToolRisk::ReadOnly) => {
1853                let fp = format!("{}({})", action.tool, action.args);
1854                if answered.contains(&fp) {
1855                    tracing::info!(
1856                        target: "harness.telemetry",
1857                        event = "tool.result.repeat",
1858                        "gen_ai.tool.name" = %action.tool,
1859                    );
1860                    serde_json::json!({
1861                        "repeat_of_earlier_call": true,
1862                        "tool": action.tool,
1863                        "note": "You already made this exact call in this run and nothing has \
1864                                 changed the workspace since. The earlier result above still \
1865                                 stands — use it rather than asking again.",
1866                    })
1867                } else {
1868                    answered.insert(fp);
1869                    self.cap_result(action, &result.content, root)
1870                }
1871            }
1872            // A write invalidates every earlier read.
1873            _ => {
1874                answered.clear();
1875                self.cap_result(action, &result.content, root)
1876            }
1877        }
1878    }
1879
1880    /// Enforce [`ToolResultPolicy`] on one result before it reaches the context.
1881    ///
1882    /// Over the ceiling, the guard prefers to *spill*: full payload to a file
1883    /// inside the workspace, bounded preview plus the path inline — nothing is
1884    /// lost, and the model retrieves slices with the file tools it already has.
1885    /// Only when spilling is off (or the write fails) does it fall back to
1886    /// destructive truncation: a byte-level cut handed back as a marker object
1887    /// rather than mangled JSON, saying how much was dropped and what to do
1888    /// instead.
1889    fn cap_result(
1890        &self,
1891        action: &Action,
1892        content: &serde_json::Value,
1893        root: &std::path::Path,
1894    ) -> serde_json::Value {
1895        let Some(max) = self.tool_results.max_bytes else {
1896            return content.clone();
1897        };
1898        let serialized = content.to_string();
1899        if serialized.len() <= max {
1900            return content.clone();
1901        }
1902        if self.tool_results.spill
1903            && let Some(marker) = spill_oversized(action, content, &serialized, root)
1904        {
1905            return marker;
1906        }
1907        // Cut on a char boundary so the kept head is valid UTF-8.
1908        let mut end = max;
1909        while end > 0 && !serialized.is_char_boundary(end) {
1910            end -= 1;
1911        }
1912        tracing::warn!(
1913            target: "harness.telemetry",
1914            event = "tool.result.truncated",
1915            "gen_ai.tool.name" = %action.tool,
1916            bytes = serialized.len(),
1917            max_bytes = max,
1918        );
1919        serde_json::json!({
1920            "truncated": true,
1921            "tool": action.tool,
1922            "bytes_total": serialized.len(),
1923            "bytes_kept": end,
1924            "head": serialized[..end],
1925            "note": format!(
1926                "This result was {} bytes and was cut to {} to protect the context window. \
1927                 Do not ask for it again unchanged — narrow it: request a smaller range, \
1928                 a filter, or a specific field.",
1929                serialized.len(), end
1930            ),
1931        })
1932    }
1933
1934    async fn recall_append(&self, owner: &str, session: &str, msg: harness_core::RecallMessage) {
1935        if let Some(store) = &self.recall
1936            && let Err(e) = store.append(owner, session, &msg).await
1937        {
1938            tracing::warn!(error = %e, "recall append failed");
1939        }
1940    }
1941
1942    /// Best-effort post-session review. Never affects the finished run.
1943    async fn run_learning_review(&self, ctx: &Context, world: &mut World, tools_called: u32) {
1944        let Some(cfg) = &self.learning else { return };
1945        if tools_called < cfg.nudge_interval {
1946            return;
1947        }
1948        let transcript = crate::render_transcript(&ctx.history, 12_000);
1949        let task = harness_core::Task {
1950            description: format!(
1951                "{}\n\n## Conversation transcript\n{}",
1952                cfg.review_prompt, transcript
1953            ),
1954            source: None,
1955            deadline: None,
1956        };
1957        let mut spec =
1958            crate::SubagentSpec::new("learning-review", task).with_max_iters(cfg.max_iters);
1959        for t in &cfg.tools {
1960            spec = spec.with_tool(t.clone());
1961        }
1962        let sub = crate::Subagent::new(harness_core::DynModel(cfg.review_model.clone()), spec);
1963        // Box::pin breaks the recursive async-future cycle: AgentLoop<M> →
1964        // run_learning_review → Subagent<DynModel>::run →
1965        // AgentLoop<Arc<dyn Model>>::run_built_context. Without pinning the
1966        // compiler rejects the infinite-sized future.
1967        if let Err(e) = Box::pin(sub.run(world)).await {
1968            tracing::warn!(error = %e, "learning review failed");
1969        }
1970    }
1971
1972    /// One final model call with tools removed, asking it to write the
1973    /// best-effort conclusion from whatever it has already gathered.
1974    ///
1975    /// Errors from the model are swallowed — observability is best-effort
1976    /// here, and a transport blip during synthesis should not turn a
1977    /// near-complete run into a hard failure.
1978    async fn force_final_synthesis(
1979        &self,
1980        ctx: &mut Context,
1981        world: &mut World,
1982        total_usage: &mut harness_core::Usage,
1983    ) -> Option<String> {
1984        const SYNTHESIS_PROMPT: &str = "[system: budget exhausted] \
1985            You have run out of budget for tool calls. Write your final answer \
1986            NOW using only the tool results already in this conversation. Do not \
1987            request more tools. Mark facts you could not verify as UNKNOWN. \
1988            Include source URLs for every claim that is not UNKNOWN.";
1989
1990        // Signal to any observer (LiveProgressHook, SessionRecorder, custom
1991        // hooks) that we've used 100% of the budget and are about to force
1992        // synthesis. Pre-existing `BudgetWarning` event was unused; this is
1993        // its natural home.
1994        self.hooks.fire(&Event::BudgetWarning { ratio: 1.0 }, world);
1995
1996        // Snapshot + clear tool schemas so the model has no choice but text.
1997        let saved_tools = std::mem::take(&mut ctx.tools);
1998        ctx.history.push(Turn {
1999            role: TurnRole::User,
2000            blocks: vec![Block::Text(SYNTHESIS_PROMPT.into())],
2001        });
2002
2003        self.hooks.fire(&Event::PreModel { ctx }, world);
2004        let result = self.model.complete(ctx).await;
2005        ctx.tools = saved_tools;
2006
2007        match result {
2008            Ok(out) => {
2009                self.hooks.fire(&Event::PostModel { out: &out }, world);
2010                total_usage.input_tokens += out.usage.input_tokens;
2011                total_usage.output_tokens += out.usage.output_tokens;
2012                total_usage.cached_input_tokens += out.usage.cached_input_tokens;
2013                total_usage.cache_write_input_tokens += out.usage.cache_write_input_tokens;
2014                ctx.push_model_output(&out);
2015                out.text
2016            }
2017            Err(_) => None,
2018        }
2019    }
2020}
2021
2022/// A persistent multi-turn conversation over one [`AgentLoop`].
2023///
2024/// Holds the append-only history and, on each [`turn`](Session::turn), re-runs
2025/// the loop against a **stable prefix** (system + name-sorted tool schemas).
2026/// That byte-stable prefix is what lets a provider's prefix cache hit across
2027/// turns — the difference between paying full price to re-read the same context
2028/// every round and paying ~10% for the cached bytes (DeepSeek).
2029pub struct Session<'a, M: Model> {
2030    loop_: &'a AgentLoop<M>,
2031    history: Vec<Turn>,
2032    max_iters: u32,
2033}
2034
2035impl<'a, M: Model> Session<'a, M> {
2036    pub fn with_max_iters(mut self, n: u32) -> Self {
2037        self.max_iters = n;
2038        self
2039    }
2040    /// Preload prior turns (e.g. resumed from disk).
2041    pub fn with_seed(mut self, seed: Vec<Turn>) -> Self {
2042        self.history = seed;
2043        self
2044    }
2045    /// The accumulated conversation so far.
2046    pub fn history(&self) -> &[Turn] {
2047        &self.history
2048    }
2049    /// Start over (branch): drop the accumulated turns.
2050    pub fn reset(&mut self) {
2051        self.history.clear();
2052    }
2053
2054    /// Send one user message. Runs the ReAct loop against the accumulated
2055    /// history, then appends this user turn + the assistant reply so the next
2056    /// turn extends the same cached prefix.
2057    pub async fn turn(
2058        &mut self,
2059        message: impl Into<String>,
2060        world: &mut World,
2061    ) -> Result<Outcome, HarnessError> {
2062        let message = message.into();
2063        let task = Task {
2064            description: message.clone(),
2065            source: None,
2066            deadline: None,
2067        };
2068        let outcome = self
2069            .loop_
2070            .run_with_seed_history(task, self.history.clone(), world, self.max_iters)
2071            .await?;
2072        let reply = match &outcome {
2073            Outcome::Done { text, .. } => text.clone().unwrap_or_default(),
2074            Outcome::BudgetExhausted { last_text, .. } | Outcome::Stuck { last_text, .. } => {
2075                last_text.clone().unwrap_or_default()
2076            }
2077        };
2078        self.history.push(Turn {
2079            role: TurnRole::User,
2080            blocks: vec![Block::Text(message)],
2081        });
2082        self.history.push(Turn {
2083            role: TurnRole::Assistant,
2084            blocks: vec![Block::Text(reply)],
2085        });
2086        Ok(outcome)
2087    }
2088}
2089
2090/// Audit #7: default safelist for `FixPatch::RunCommand`.
2091///
2092/// Sensors emitting `RunCommand` patches would otherwise be a silent
2093/// arbitrary-code-execution channel. We restrict the *program* by name to a
2094/// short list of well-known, side-effect-bounded formatters/fixers. Anything
2095/// else returns false and the patch is rejected (write your own `PreAutoFix`
2096/// hook returning `HookOutcome::Allow` to widen the policy).
2097///
2098/// `ReplaceFile` and `UnifiedDiff` are not restricted here — they only touch
2099/// files inside the workspace and are covered by the symlink-safe path
2100/// resolution in `harness-tools-fs`.
2101pub fn is_default_safe_fix(patch: &harness_core::FixPatch) -> bool {
2102    use harness_core::FixPatch;
2103    match patch {
2104        FixPatch::ReplaceFile { .. } | FixPatch::UnifiedDiff { .. } => true,
2105        FixPatch::RunCommand { program, args, .. } => match program.as_str() {
2106            // Cargo subcommands proven side-effect-bounded.
2107            "cargo" => matches!(
2108                args.first().map(String::as_str),
2109                Some("fmt" | "clippy" | "fix"),
2110            ),
2111            "rustfmt" | "gofmt" | "prettier" | "ruff" | "black" => true,
2112            _ => false,
2113        },
2114        // Future FixPatch variants: deny by default — review and add to the list above.
2115        _ => false,
2116    }
2117}
2118
2119/// Monotonic counter for `.harness-patch-*.diff` temp filenames — millisecond
2120/// resolution alone collides under parallel agent runs.
2121static PATCH_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
2122
2123/// Monotonic counter for fallback recall session ids (no `uuid` dep).
2124static RECALL_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
2125
2126/// Apply auto-fix patches; return short descriptions of those that succeeded.
2127///
2128/// Made `pub` (was `pub(crate)`) so integration tests can call it directly.
2129pub async fn apply_patches(patches: &[harness_core::FixPatch], world: &mut World) -> Vec<String> {
2130    use harness_core::FixPatch;
2131    let mut applied = Vec::new();
2132    for p in patches {
2133        match p {
2134            FixPatch::ReplaceFile { path, content } => {
2135                let abs = world.repo.root.join(path);
2136                if let Some(parent) = abs.parent() {
2137                    let _ = tokio::fs::create_dir_all(parent).await;
2138                }
2139                if tokio::fs::write(&abs, content).await.is_ok() {
2140                    applied.push(format!("replaced {}", path.display()));
2141                }
2142            }
2143            FixPatch::UnifiedDiff { diff } => {
2144                if try_apply_diff(world, diff).await {
2145                    applied.push("unified diff applied".into());
2146                }
2147            }
2148            FixPatch::RunCommand { program, args, cwd } => {
2149                let cwd_ref = cwd.as_deref().unwrap_or(world.repo.root.as_path());
2150                let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
2151                if let Ok(out) = world.runner.exec(program, &args_ref, Some(cwd_ref)).await
2152                    && out.status == 0
2153                {
2154                    applied.push(format!("ran `{program} {}`", args.join(" ")));
2155                }
2156            }
2157            // FixPatch is `#[non_exhaustive]`; unknown variants are skipped.
2158            _ => tracing::warn!("apply_patches: unknown FixPatch variant — skipped"),
2159        }
2160    }
2161    applied
2162}
2163
2164/// Write `diff` to a unique temp file and try `patch -p1` first, then `-p0`.
2165/// Returns whether either succeeded. The `-p1`-then-`-p0` order matches the
2166/// reality that most agent-emitted diffs are git-style (need `-p1`) but some
2167/// hand-rolled diffs use repo-relative paths (need `-p0`).
2168async fn try_apply_diff(world: &mut World, diff: &str) -> bool {
2169    use std::sync::atomic::Ordering;
2170    use tokio::io::AsyncWriteExt;
2171
2172    let seq = PATCH_SEQ.fetch_add(1, Ordering::SeqCst);
2173    let pid = std::process::id();
2174    let now = world.clock.now_ms();
2175    let tmp = world
2176        .repo
2177        .root
2178        .join(format!(".harness-patch-{pid}-{now}-{seq}.diff"));
2179
2180    let mut f = match tokio::fs::File::create(&tmp).await {
2181        Ok(f) => f,
2182        Err(e) => {
2183            tracing::warn!(error=%e, path=%tmp.display(), "could not create patch tempfile");
2184            return false;
2185        }
2186    };
2187    if let Err(e) = f.write_all(diff.as_bytes()).await {
2188        tracing::warn!(error=%e, "could not write patch tempfile");
2189        let _ = tokio::fs::remove_file(&tmp).await;
2190        return false;
2191    }
2192    drop(f);
2193
2194    let tmp_str = tmp.to_string_lossy().to_string();
2195    let mut applied = false;
2196    for strip in ["-p1", "-p0"] {
2197        match world
2198            .runner
2199            .exec(
2200                "patch",
2201                &[strip, "--silent", "-i", tmp_str.as_str()],
2202                Some(world.repo.root.as_path()),
2203            )
2204            .await
2205        {
2206            Ok(out) if out.status == 0 => {
2207                tracing::info!(strip, "patch applied");
2208                applied = true;
2209                break;
2210            }
2211            Ok(out) => {
2212                tracing::debug!(strip, stderr=%out.stderr, "patch failed; trying next strip level");
2213            }
2214            Err(e) => {
2215                tracing::warn!(error=%e, "patch command not available");
2216                break; // patch tool missing — no point trying other strip
2217            }
2218        }
2219    }
2220    let _ = tokio::fs::remove_file(&tmp).await;
2221    applied
2222}