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, when `policy.max_iters` is
10//! hit, or when a stuck-detector ([`StuckPolicy`], [`MonotonyPolicy`]) says
11//! the run is spinning.
12
13// ── crates that used to be their own ───────────────────────────────────────
14// Ten of them, and not one carried a dependency a caller would want to avoid.
15// The boundaries bought nothing and cost every release ten more publishes and
16// every host ten more version pins; a crate boundary here has to name a
17// dependency you might not want, and these did not.
18pub mod blueprint;
19pub mod compactor;
20pub mod daemon;
21pub mod experience;
22pub mod hooks;
23pub mod loop_engine;
24pub mod orchestrator;
25pub mod sandbox;
26pub mod scheduler;
27pub mod templates;
28
29pub mod acceptance;
30pub mod goal;
31pub mod learning;
32pub mod memory_layer;
33pub mod meters;
34#[cfg(feature = "otel")]
35pub mod otel;
36pub mod profile_guide;
37pub mod recall_layer;
38pub mod receipt;
39pub mod registry;
40pub mod seal;
41pub use acceptance::{Acceptance, FilesExist, NonEmptyAnswer, Verdict};
42pub use goal::{Goal, GoalStore, Phase, PhaseStatus};
43pub use receipt::{Receipt, ReceiptBuilder};
44pub use seal::{SealBreach, SealSet};
45/// The token `AgentLoop::with_cancellation` takes, re-exported so a caller can
46/// cancel a run without depending on `tokio-util` directly.
47pub use tokio_util::sync::CancellationToken;
48pub mod replay;
49pub mod subagent;
50pub mod telemetry;
51pub mod timeline;
52pub mod trajectory;
53pub use trajectory::{
54 GoldenDiff, Report, Rule, RuleSet, Step, Trajectory, TrajectoryCheck, Violation,
55};
56
57pub use learning::*;
58pub use memory_layer::*;
59pub mod boundary_guide;
60pub use boundary_guide::*;
61pub mod events;
62pub use events::{HostEvent, NoticeGuide};
63pub use profile_guide::*;
64pub use recall_layer::*;
65pub use registry::*;
66pub use replay::*;
67pub use subagent::*;
68pub use telemetry::*;
69
70use crate::compactor::{CALIBRATION_KEY, DefaultCompactor};
71use crate::hooks::HookBus;
72use harness_core::{
73 Action, Block, CompactionStage, Compactor, Context, Event, Guide, HarnessError, HookOutcome,
74 Model, ModelDelta, ModelOutput, ResponseFormat, Sensor, SessionSource, SignalSet, Stage, Task,
75 ToolCall, ToolResult, Turn, TurnRole, World,
76};
77use std::collections::HashMap;
78use std::sync::Arc;
79use std::time::Duration;
80use tracing::Instrument;
81use tracing::field::Empty;
82
83/// What a finished model call leaves behind: fields on its `chat` span, and
84/// its duration and tokens as metrics.
85fn finish_chat(
86 span: &tracing::Span,
87 info: &harness_core::ModelInfo,
88 started: std::time::Instant,
89 result: Option<Result<&ModelOutput, String>>,
90) {
91 let end = match &result {
92 Some(Ok(out)) => {
93 span.record("gen_ai.usage.input_tokens", out.usage.input_tokens)
94 .record("gen_ai.usage.output_tokens", out.usage.output_tokens)
95 .record(
96 "gen_ai.usage.cached_input_tokens",
97 out.usage.cached_input_tokens,
98 )
99 .record(
100 "gen_ai.response.finish_reasons",
101 format!("{:?}", out.stop_reason),
102 )
103 .record("tool_calls", out.tool_calls.len());
104 meters::ModelCallEnd::Done(&out.usage)
105 }
106 Some(Err(e)) => {
107 span.record("otel.status_code", "error")
108 .record("otel.status_message", e.as_str());
109 meters::ModelCallEnd::Failed
110 }
111 None => {
112 span.record("otel.status_message", "cancelled");
113 meters::ModelCallEnd::Cancelled
114 }
115 };
116 meters::model_call(info, started.elapsed(), end);
117}
118
119/// A tool call's result and when it ran, by the World clock — the figures
120/// behind [`Event::ToolTimed`].
121struct Dispatched {
122 result: ToolResult,
123 started_ms: i64,
124 finished_ms: i64,
125}
126
127/// The span one model call runs in. Named `chat {model}` for OTLP backends,
128/// which group by span name; fields per the GenAI conventions.
129fn chat_span(info: &harness_core::ModelInfo) -> tracing::Span {
130 tracing::info_span!(
131 target: "harness.telemetry",
132 "chat",
133 "otel.name" = %format!("chat {}", info.model),
134 "otel.kind" = "client",
135 "otel.status_code" = Empty,
136 "otel.status_message" = Empty,
137 "gen_ai.operation.name" = "chat",
138 "gen_ai.provider.name" = %info.provider,
139 "gen_ai.request.model" = %info.model,
140 "gen_ai.usage.input_tokens" = Empty,
141 "gen_ai.usage.output_tokens" = Empty,
142 "gen_ai.usage.cached_input_tokens" = Empty,
143 "gen_ai.response.finish_reasons" = Empty,
144 tool_calls = Empty,
145 )
146}
147
148/// Governs the loop's stuck-detector. When the model repeats the *same* tool
149/// call (name + args) round after round without making progress, the loop first
150/// nudges it to change approach, then terminates cleanly with [`Outcome::Stuck`]
151/// rather than burning the rest of the budget spinning on a loop.
152///
153/// Enabled by default with conservative thresholds — the calls must be
154/// *byte-identical*, so genuine "read the same file twice" work never trips it.
155#[derive(Debug, Clone)]
156pub struct StuckPolicy {
157 pub enabled: bool,
158 /// Consecutive identical tool-call rounds before injecting a "you are
159 /// repeating yourself, change your approach" feedback signal.
160 pub nudge_after: u32,
161 /// Consecutive identical rounds before terminating with [`Outcome::Stuck`].
162 pub abort_after: u32,
163}
164
165impl Default for StuckPolicy {
166 fn default() -> Self {
167 Self {
168 enabled: true,
169 nudge_after: 3,
170 abort_after: 6,
171 }
172 }
173}
174
175/// A second stuck signal, orthogonal to [`StuckPolicy`]: the model calls *one*
176/// tool round after round — a different argument every time, so nothing ever
177/// repeats — and never moves on to anything else.
178///
179/// [`StuckPolicy`] cannot see this and must not be changed so that it can. It
180/// keys on *byte-identical consecutive* rounds precisely so that genuine "read
181/// the same file twice" work never trips it, and `graph_find("Xen")` followed
182/// by `graph_find("KVM")` is correctly not a repeat — the request changed. Two
183/// identical calls in a row and one tool worked for twenty rounds are different
184/// shapes, and want different thresholds and different reasons.
185///
186/// Measured on a real run: asked which platforms a product integrates with, an
187/// agent that could not retrieve the anchor entity began guessing candidates
188/// and looking them up one at a time — `graph_find("Nomad")`,
189/// `graph_find("Docker")`, `graph_find("VMware")`, `graph_find("Xen")`,
190/// `graph_find("KVM")` — twelve calls to one tool across twenty rounds, each
191/// with a different argument, none of them useful. Every individual call looked
192/// like progress. The *shape* did not, and the shape is something the loop can
193/// see without knowing anything about what `graph_find` means.
194///
195/// **Off by default, and it has to be.** The predicate is "one tool, many
196/// rounds, nothing else", and there is a large class of healthy agents whose
197/// every round is one tool: give an agent a shell and nothing else and
198/// `run_shell` is the only name it will ever emit, thirty rounds running,
199/// legitimately. No threshold rescues that — the false positive is unbounded in
200/// N, not merely rare — so a default-on version of this guard would terminate
201/// working agents, which is worse than the problem it fixes. A host that knows
202/// its agent has several tools and is meant to use them can switch it on;
203/// nobody else pays anything for its existence.
204#[derive(Debug, Clone)]
205pub struct MonotonyPolicy {
206 /// Off by default. See the type docs for why no default-on threshold is
207 /// safe for this predicate.
208 pub enabled: bool,
209 /// Consecutive single-tool rounds before injecting a "you are working one
210 /// tool and nothing else" feedback signal.
211 ///
212 /// 8, argued rather than rounded. The honest single-tool runs are surveys —
213 /// read five files, grep four paths — and a survey ends by using a
214 /// *different* tool, which resets the count; reaching eight rounds of one
215 /// name with nothing else touched is already past what surveying does. The
216 /// cost of being wrong here is one sentence of feedback, not a terminated
217 /// run, which is why the nudge can sit this close to ordinary work. This is
218 /// also the half of the policy that does the work: in the measured case a
219 /// nudge at round eight arrives with two thirds of the budget unspent.
220 pub nudge_after: u32,
221 /// Consecutive single-tool rounds before terminating with [`Outcome::Stuck`].
222 ///
223 /// 16 — twice the nudge, mirroring [`StuckPolicy`]'s own 3→6 ratio, and
224 /// 2.6× its abort so the two can never race: a byte-identical spiral is
225 /// always caught by the older policy first, under its own reason. Deliberately
226 /// generous, because this end of the policy is fatal and its only job is to
227 /// be a backstop for budgets long enough that the nudge was ignored.
228 pub abort_after: u32,
229}
230
231impl Default for MonotonyPolicy {
232 fn default() -> Self {
233 Self {
234 enabled: false,
235 nudge_after: 8,
236 abort_after: 16,
237 }
238 }
239}
240
241/// A ceiling on how much of one tool result reaches the context.
242///
243/// A single call can return more than the whole conversation: a lock file, a
244/// `SELECT *`, an MCP tool the framework does not control. Measured on a real
245/// run, "search these files for a word" cost 53,487 input tokens because one
246/// `read_file` returned a lock file — the model then paid for it on every
247/// subsequent turn, and compaction started throwing away real history to make
248/// room. A per-result ceiling is the only place to stop that: the tools cannot
249/// all be trusted (third-party MCP), and the compactor only runs after the
250/// damage is in the context.
251#[derive(Debug, Clone)]
252pub struct ToolResultPolicy {
253 /// Max serialized bytes of a single tool result. `None` disables the guard.
254 /// Default ~24 KiB — roughly 6k tokens of English, generous for a file page
255 /// or a query result, far below what blows a window.
256 pub max_bytes: Option<usize>,
257 /// Replace the payload of a read-only call that exactly repeats an earlier
258 /// one in the same run, when nothing has modified the world in between.
259 ///
260 /// [`StuckPolicy`] only sees *consecutive* identical rounds. Reading a file
261 /// at iteration 1 and again at iteration 5 is not that, and looks like
262 /// progress — but the same bytes land in the context twice and the model
263 /// learns nothing the second time. Measured on a real run, model wait was
264 /// 36.7s against 3ms of tool execution: a repeat costs context, not time,
265 /// so what is suppressed is the payload, not the call.
266 ///
267 /// Only `ToolRisk::ReadOnly` qualifies (`Network` is a separate risk, and an
268 /// external endpoint may answer differently), and any non-read-only call
269 /// clears the record — after a write, re-reading is the correct move.
270 ///
271 /// **Off by default, on the evidence.** Measured on the completion
272 /// benchmark: ceilings alone solved 6/6 tasks for 160k effective tokens;
273 /// adding repeat-suppression cut that to 32k — and lost a task. An agent
274 /// working through a file larger than one page re-reads it because it cannot
275 /// hold it, gets told it already has the answer, and does not: the content
276 /// was paged away. Five times cheaper is not worth a task a framework could
277 /// otherwise do, so this is opt-in for callers who would rather have the
278 /// tokens. (Suppressing from the *third* identical call rather than the
279 /// second would likely keep most of the saving without the failure — it
280 /// needs measuring before it becomes the default.)
281 pub dedupe_repeats: bool,
282 /// When a result exceeds `max_bytes`, save the *full* payload to a file
283 /// under `.harness/spill/` in the workspace and inline a bounded preview
284 /// plus the path, instead of cutting the tail off and throwing it away.
285 ///
286 /// Truncation destroys information: the model is told "narrow your
287 /// request", but the bytes it needed may be exactly the ones dropped, and
288 /// the only recovery is re-running the call to be truncated again.
289 /// Measured on the completion benchmark, that loop is visible as a single
290 /// task paying 184k input tokens. Spilling keeps the ceiling — the context
291 /// gets a preview, never the flood — while the whole result stays
292 /// retrievable through the file tools the agent already has (`read_file`
293 /// with offset/limit, `grep` on the spill path), because the spill lives
294 /// inside the workspace jail. Borrowed from DeepSeek Harness's `spill`
295 /// family (preview + retrieval locator), which is the same judgement.
296 ///
297 /// Costs nothing until it fires: the write happens only on the oversized
298 /// path, which the default ceiling makes rare. When the write fails (e.g.
299 /// read-only workspace) the guard falls back to plain truncation.
300 pub spill: bool,
301}
302
303impl Default for ToolResultPolicy {
304 fn default() -> Self {
305 Self {
306 max_bytes: Some(24 * 1024),
307 dedupe_repeats: false,
308 spill: true,
309 }
310 }
311}
312
313/// Governs *when* and *how far* the loop compacts context. Hysteresis: only
314/// start compacting once usage crosses `high_water`, and stop as soon as it's
315/// back under `target` — instead of running every stage above a threshold on
316/// every turn. This avoids over-compacting (needlessly reaching the lossy,
317/// main-model `AutoCompact` stage) and, because compaction rewrites history and
318/// invalidates the provider prefix cache, avoids nibbling the context every
319/// single turn.
320#[derive(Debug, Clone)]
321pub struct CompactPolicy {
322 /// Start compacting when `used/window` exceeds this. Default 0.75.
323 pub high_water: f32,
324 /// Stop as soon as `used/window` is back at/under this. Default 0.55.
325 pub target: f32,
326}
327
328impl Default for CompactPolicy {
329 fn default() -> Self {
330 Self {
331 high_water: 0.75,
332 target: 0.55,
333 }
334 }
335}
336
337/// Inline preview size for a spilled result. Deliberately smaller than the
338/// ceiling: the point of spilling is that the context gets a *glimpse* and a
339/// path, not four-fifths of the flood.
340/// How often a streamed model call reports progress / a heartbeat.
341const MODEL_PROGRESS_EVERY: Duration = Duration::from_secs(5);
342const MODEL_HEARTBEAT_EVERY: Duration = Duration::from_secs(30);
343/// Turns in a row the model may be cut off by the output-token limit, with no
344/// tool call to show for it, before the run is ended as [`Outcome::Stuck`].
345/// Each one is sent back with an instruction to stop deliberating and act.
346const MAX_TRUNCATED_TURNS: u32 = 2;
347/// What a turn cut off by the output-token limit is told before it retries.
348const TRUNCATED_TURN_NUDGE: &str = "Your previous turn hit the output token limit before \
349 you produced an answer or a tool call, so nothing from it was used. Do not repeat that \
350 reasoning. Act now: either call the next tool you need, or write the final answer \
351 directly and keep it short.";
352
353const SPILL_PREVIEW_BYTES: usize = 4 * 1024;
354
355/// Told to the model when the step budget ran out and the loop stripped its
356/// tools. See `force_final_synthesis`.
357const BUDGET_SYNTHESIS_PROMPT: &str = "[system: budget exhausted] \
358 You have run out of budget for tool calls. Write your final answer \
359 NOW using only the tool results already in this conversation. Do not \
360 request more tools. Mark facts you could not verify as UNKNOWN. \
361 Include source URLs for every claim that is not UNKNOWN.";
362
363/// Told to the model when [`MonotonyPolicy`] stopped the run. Deliberately
364/// names the shape it was caught in: the failure mode this ends is a model
365/// guessing arguments to one tool, and such a model has usually *found*
366/// something it never got round to reporting. "Say what you could not
367/// determine" is the instruction it needs; "mark it UNKNOWN" is the same
368/// instruction the budget prompt gives for the same reason.
369const MONOTONY_SYNTHESIS_PROMPT: &str = "[system: stopped — no progress] \
370 You have called the same tool many rounds in a row without using any other \
371 tool, so no further tool calls will be granted. Write your final answer NOW \
372 using only the tool results already in this conversation. Do not request \
373 more tools. State plainly which parts you could not determine instead of \
374 guessing at them.";
375
376/// First `n` bytes of `s`, cut on a char boundary.
377fn head_of(s: &str, n: usize) -> &str {
378 let mut end = n.min(s.len());
379 while end > 0 && !s.is_char_boundary(end) {
380 end -= 1;
381 }
382 &s[..end]
383}
384
385/// The string field that dominates an oversized result, if one does.
386///
387/// Most oversized results are one big string in a small envelope — a file
388/// body, a command's stdout — and the right spill for those is the *raw text*:
389/// multi-line, so `read_file`'s line paging and `grep`'s line matching work on
390/// it. Spilling the serialized JSON instead would fold the whole payload onto
391/// one escaped line that line-oriented tools can neither page nor match
392/// (`read_file` pages by line; a 68 KB single-line file is unreachable past
393/// the first 16 KB). Returns the dotted path and the string when one field is
394/// ≥ 80% of the serialized size.
395fn dominant_string(v: &serde_json::Value, total: usize) -> Option<(String, &str)> {
396 fn walk<'a>(v: &'a serde_json::Value, path: &str, best: &mut Option<(String, &'a str)>) {
397 match v {
398 serde_json::Value::String(s) => {
399 if best.as_ref().is_none_or(|(_, b)| s.len() > b.len()) {
400 *best = Some((path.to_string(), s.as_str()));
401 }
402 }
403 serde_json::Value::Object(m) => {
404 for (k, x) in m {
405 let p = if path.is_empty() {
406 k.clone()
407 } else {
408 format!("{path}.{k}")
409 };
410 walk(x, &p, best);
411 }
412 }
413 serde_json::Value::Array(a) => {
414 for (i, x) in a.iter().enumerate() {
415 walk(x, &format!("{path}[{i}]"), best);
416 }
417 }
418 _ => {}
419 }
420 }
421 let mut best = None;
422 walk(v, "", &mut best);
423 best.filter(|(_, s)| s.len() * 5 >= total * 4)
424}
425
426/// Replace the value at a `dominant_string` dotted path with `with`.
427fn replace_at(v: &mut serde_json::Value, path: &str, with: serde_json::Value) {
428 let mut cur = v;
429 let mut rest = path;
430 loop {
431 // Next segment: `key`, `key[i]`, or a bare `[i]`.
432 let (seg, tail) = match rest.find('.') {
433 Some(dot) => (&rest[..dot], &rest[dot + 1..]),
434 None => (rest, ""),
435 };
436 let (key, idx) = match seg.find('[') {
437 Some(b) => (&seg[..b], seg[b + 1..seg.len() - 1].parse::<usize>().ok()),
438 None => (seg, None),
439 };
440 if !key.is_empty() {
441 match cur.get_mut(key) {
442 Some(next) => cur = next,
443 None => return,
444 }
445 }
446 if let Some(i) = idx {
447 match cur.get_mut(i) {
448 Some(next) => cur = next,
449 None => return,
450 }
451 }
452 if tail.is_empty() {
453 *cur = with;
454 return;
455 }
456 rest = tail;
457 }
458}
459
460/// Persist an oversized result under `.harness/spill/` in the workspace and
461/// build the inline marker. `None` on any IO failure — the caller falls back
462/// to truncation, because a guard that can error a run to protect a context
463/// window has its priorities backwards.
464fn spill_oversized(
465 action: &Action,
466 content: &serde_json::Value,
467 serialized: &str,
468 root: &std::path::Path,
469) -> Option<serde_json::Value> {
470 let dir = root.join(".harness").join("spill");
471 std::fs::create_dir_all(&dir).ok()?;
472 let id: String = action
473 .call_id
474 .chars()
475 .filter(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_'))
476 .take(48)
477 .collect();
478 let id = if id.is_empty() { "call".into() } else { id };
479
480 let (rel, preview, meta) = match dominant_string(content, serialized.len()) {
481 // One big string in a small envelope: spill the raw text, keep the
482 // envelope inline with the big field swapped for a pointer.
483 Some((field, text)) => {
484 let rel = format!(".harness/spill/{id}-{}.txt", action.tool);
485 std::fs::write(root.join(&rel), text).ok()?;
486 let mut meta = content.clone();
487 replace_at(
488 &mut meta,
489 &field,
490 serde_json::Value::String(format!("[{} bytes spilled to {rel}]", text.len())),
491 );
492 (
493 rel,
494 head_of(text, SPILL_PREVIEW_BYTES).to_string(),
495 Some(meta),
496 )
497 }
498 // Structured payload (e.g. a long match list): spill pretty-printed,
499 // one element per line, so line-oriented retrieval still works.
500 None => {
501 let rel = format!(".harness/spill/{id}-{}.json", action.tool);
502 let pretty =
503 serde_json::to_string_pretty(content).unwrap_or_else(|_| serialized.to_string());
504 std::fs::write(root.join(&rel), &pretty).ok()?;
505 (rel, head_of(&pretty, SPILL_PREVIEW_BYTES).to_string(), None)
506 }
507 };
508 tracing::warn!(
509 target: "harness.telemetry",
510 event = "tool.result.spilled",
511 "gen_ai.tool.name" = %action.tool,
512 bytes = serialized.len(),
513 path = %rel,
514 );
515 let mut marker = serde_json::json!({
516 "spilled": true,
517 "tool": action.tool,
518 "bytes_total": serialized.len(),
519 "path": rel,
520 "preview": preview,
521 "note": format!(
522 "This result was {} bytes — too large to inline, so the FULL content was \
523 saved to '{rel}' (workspace-relative). Nothing was lost. Retrieve exactly \
524 the part you need: grep(path=\"{rel}\", pattern=...) or \
525 read_file(path=\"{rel}\", offset=..., limit=...). Do not repeat the \
526 original call — it will spill again.",
527 serialized.len()
528 ),
529 });
530 // The envelope metadata (paths, flags) rides along when it is small; a
531 // pathological envelope that is itself oversized is dropped, not inlined.
532 if let Some(meta) = meta
533 && meta.to_string().len() <= SPILL_PREVIEW_BYTES
534 {
535 marker["meta"] = meta;
536 }
537 Some(marker)
538}
539
540/// A stable fingerprint of a round's tool calls (names + args, ignoring the
541/// volatile call id). Two rounds with the same fingerprint asked for the exact
542/// same actions — the signal the stuck-detector keys on.
543fn tool_call_fingerprint(calls: &[ToolCall]) -> String {
544 calls
545 .iter()
546 .map(|c| format!("{}({})", c.name, c.args))
547 .collect::<Vec<_>>()
548 .join("|")
549}
550
551/// The one tool a round asked for, when the whole round asked for exactly one
552/// *name*. `None` when the round mixed names — reaching for a second tool is,
553/// on its own, evidence the model is still moving, and resets
554/// [`MonotonyPolicy`]'s count.
555///
556/// Several parallel calls to the same tool count as one round of that tool:
557/// reading four files in one round is efficient work, not monotony, and the
558/// policy's threshold is in *rounds* precisely so that batching is never
559/// punished.
560fn sole_tool(calls: &[ToolCall]) -> Option<&str> {
561 let first = calls.first()?.name.as_str();
562 calls.iter().all(|c| c.name == first).then_some(first)
563}
564
565/// Cheap identity for one tool result, for the stuck detector's "did anything
566/// change" test. Not a checksum anyone depends on — only equality between two
567/// consecutive rounds of the same process matters.
568fn result_digest(tool: &str, content: &serde_json::Value) -> u64 {
569 use std::hash::{Hash, Hasher};
570 let mut h = std::collections::hash_map::DefaultHasher::new();
571 tool.hash(&mut h);
572 content.to_string().hash(&mut h);
573 h.finish()
574}
575
576/// Where a run finished. Each variant is `#[non_exhaustive]` so new *fields*
577/// don't break downstream matches — always include `..` when destructuring.
578#[derive(Debug, Clone)]
579pub enum Outcome {
580 /// Model returned text with no tool calls (natural end).
581 #[non_exhaustive]
582 Done {
583 text: Option<String>,
584 iters: u32,
585 tools_called: u32,
586 usage: harness_core::Usage,
587 /// What the acceptance checks said. `None` means nothing was asked —
588 /// so "the model stopped" is all this outcome claims.
589 verified: Option<Verdict>,
590 /// The sealed contract as it stood before the model's first turn.
591 ///
592 /// Carried out of the run because a receipt has to say *what* was
593 /// measured, not only that the measurement held. Empty when nothing
594 /// was sealed.
595 contract: crate::seal::SealSet,
596 /// Set when a sealed acceptance contract changed during the run.
597 ///
598 /// Distinct from a plain failed `verified` because the two mean
599 /// different things to a caller: a failed check is work not finished,
600 /// this is the measuring instrument having been moved. A host may want
601 /// to fail a build on the first and page someone on the second.
602 seal_breach: Option<String>,
603 },
604 /// Policy budget exhausted before the model stopped requesting tools.
605 /// Carries everything we know so the caller can recover partial work
606 /// (saved notes, files written by tools, the last assistant text, etc.)
607 /// instead of seeing a single bare "budget out" string.
608 #[non_exhaustive]
609 BudgetExhausted {
610 iters: u32,
611 last_text: Option<String>,
612 tools_called: u32,
613 usage: harness_core::Usage,
614 /// True when `Task::deadline` stopped the run rather than `max_iters`.
615 /// Both end the same way — a forced final answer — but "ran out of
616 /// time" and "ran out of steps" call for different responses from the
617 /// caller, and an unattended long run only ever hits the first.
618 deadline_reached: bool,
619 },
620 /// The agent got stuck and the loop terminated early to save the rest of
621 /// the budget. Carries partial work (last text, files already written by
622 /// tools) like `BudgetExhausted`.
623 ///
624 /// Two independent detectors end here, and `reason` says which: it repeated
625 /// the *same* tool call for `StuckPolicy::abort_after` rounds without
626 /// progress, or it worked a *single* tool for `MonotonyPolicy::abort_after`
627 /// rounds without touching anything else. Only the second forces a final
628 /// synthesis before returning — see the abort site for why.
629 #[non_exhaustive]
630 Stuck {
631 /// Human-readable reason, e.g. "repeated `read_file(...)` 6× without progress"
632 /// or "called `graph_find` for 16 consecutive rounds without using any
633 /// other tool".
634 reason: String,
635 /// How many consecutive rounds the detector counted before aborting.
636 repeated: u32,
637 iters: u32,
638 last_text: Option<String>,
639 tools_called: u32,
640 usage: harness_core::Usage,
641 },
642 /// The caller stopped the run through [`AgentLoop::with_cancellation`]
643 /// before the model reached a natural end. Carries partial work like
644 /// `Stuck` does, because the user who pressed Ctrl-C still wants what was
645 /// done. The loop makes **no** further model call after this — not even
646 /// the forced final synthesis the other early exits perform.
647 ///
648 /// A tool that was mid-flight when the token fired may still finish its
649 /// side effects — a file write on a blocking thread completes, a child
650 /// process keeps running unless its runner kills it on drop — but its
651 /// result is discarded before it reaches history. The dispatch is still
652 /// counted in `tools_called` — the telemetry `run.end` line reconciles
653 /// against that number.
654 #[non_exhaustive]
655 Cancelled {
656 /// Iterations completed before the cancel. `0` means the token was
657 /// already cancelled on entry.
658 iters: u32,
659 last_text: Option<String>,
660 tools_called: u32,
661 usage: harness_core::Usage,
662 },
663}
664
665/// The agent loop.
666pub struct AgentLoop<M: Model> {
667 pub model: M,
668 pub tools: ToolRegistry,
669 pub guides: Vec<Arc<dyn Guide>>,
670 pub sensors: Vec<Arc<dyn Sensor>>,
671 pub hooks: HookBus,
672 pub compactor: Arc<dyn Compactor>,
673 /// A deadline on each individual tool call. One hung call — a network tool
674 /// on a dead endpoint, a shell command waiting on stdin — otherwise takes
675 /// the whole run down with it, and the host's only recourse is a run-level
676 /// timeout that throws away every turn of finished work (measured on the
677 /// completion benchmark: a run that had already done the job was billed as
678 /// a 0-token timeout). A per-call deadline converts the hang into an error
679 /// *result* the model sees and can route around. `None` disables. The
680 /// default is generous — 120s covers a slow build — because a false
681 /// deadline on a legitimately long tool is worse than a late one.
682 pub tool_timeout: Option<Duration>,
683 /// Default response format applied to every run unless overridden by
684 /// `run_typed`. See [`ResponseFormat`].
685 pub response_format: ResponseFormat,
686 /// When `true` (the default), the loop drives each model turn, including
687 /// the forced final synthesis, via `Model::stream()` instead of
688 /// `complete()`, firing `Event::ModelTokenDelta` for each text fragment.
689 /// Tool-call deltas are still assembled inside the loop; only the terminal
690 /// `ModelOutput` shape is observable downstream. `with_streaming(false)`
691 /// is the explicit opt-out.
692 pub streaming: bool,
693 /// Stops the run from outside. Checked at the top of every iteration and
694 /// raced against the model step and every tool dispatch, so a cancel
695 /// lands within one await point rather than at the next iteration
696 /// boundary. Defaults to a token nobody holds, which never fires.
697 pub cancel: CancellationToken,
698 /// Optional cross-session recall store. When set, the loop captures every
699 /// turn and the `session_search` tool is registered. See `with_recall`.
700 pub recall: Option<Arc<dyn harness_core::RecallStore>>,
701 /// When true (and `recall` is set), a `RecallGuide` auto-injects top-k
702 /// past context at session start.
703 pub recall_auto_inject: bool,
704 pub learning: Option<LearningConfig>,
705 /// Loop-detection policy. Enabled by default — see [`StuckPolicy`].
706 pub stuck: StuckPolicy,
707 /// Second, independent loop-detection signal: one tool worked round after
708 /// round with nothing else touched. Off by default — see [`MonotonyPolicy`].
709 pub monotony: MonotonyPolicy,
710 /// Ceiling on context tokens, overriding the model's own window. `None`
711 /// (default) sizes the budget to `ModelInfo::context_window`.
712 pub max_input_tokens: Option<u32>,
713 /// Context-compaction hysteresis. See [`CompactPolicy`].
714 pub compaction: CompactPolicy,
715 /// Ceiling on a single tool result. See [`ToolResultPolicy`].
716 pub tool_results: ToolResultPolicy,
717 /// Conditions the run must satisfy before the loop reports success. The
718 /// model stopping is evidence it *believes* it is finished; these say
719 /// whether it is. See [`acceptance`].
720 pub acceptance: Vec<Arc<dyn Acceptance>>,
721 /// How many times a failed acceptance is handed back to the model before
722 /// the loop gives up and reports what there is. One is usually enough: a
723 /// model that ignores the first correction rarely takes the second.
724 pub acceptance_retries: u32,
725 /// System instruction injected into every run's `Context.system` (unless the
726 /// built context already carries its own). Set via [`with_system`](Self::with_system).
727 pub system: Vec<Block>,
728 /// Named auxiliary models for side tasks — compaction, memory synthesis,
729 /// judging, subagents — so the main conversation stays on one model (and
730 /// keeps its provider prompt-cache prefix intact) while cheap or specialist
731 /// work goes elsewhere. Populated via [`with_model_role`](Self::with_model_role),
732 /// read via [`model_for`](Self::model_for). An unregistered role means
733 /// "use the main model" — components fall back rather than fail.
734 pub model_roles: std::collections::HashMap<String, Arc<dyn Model>>,
735 /// True once the host installed its own compactor via
736 /// [`with_compactor`](Self::with_compactor). Guards the `"compactor"`
737 /// model-role convenience from overwriting an explicit choice.
738 pub compactor_custom: bool,
739}
740
741impl AgentLoop<harness_core::DynModel> {
742 /// Build a loop from a boxed model — what every model factory hands back
743 /// (`ApiKind::build`, a router, anything stored behind a trait object).
744 ///
745 /// `Arc<dyn Model>` deliberately does not implement `Model` (see
746 /// [`DynModel`](harness_core::DynModel) for why), so `AgentLoop::new` cannot
747 /// take one. Without this constructor every caller writes the wrapper
748 /// themselves, and the first thing a new user meets is a trait-bound error
749 /// naming a type they have never heard of.
750 ///
751 /// ```ignore
752 /// let model = ApiKind::OpenAI.build(base_url, model_id, key);
753 /// let agent = AgentLoop::boxed(model).with_tool(Arc::new(ReadFile));
754 /// ```
755 pub fn boxed(model: Arc<dyn Model>) -> Self {
756 Self::new(harness_core::DynModel(model))
757 }
758}
759
760impl<M: Model> AgentLoop<M> {
761 pub fn new(model: M) -> Self {
762 Self {
763 model,
764 tools: ToolRegistry::new(),
765 guides: Vec::new(),
766 sensors: Vec::new(),
767 hooks: HookBus::new(),
768 compactor: Arc::new(DefaultCompactor::new()),
769 tool_timeout: Some(Duration::from_secs(120)),
770 response_format: ResponseFormat::Free,
771 streaming: true,
772 cancel: CancellationToken::new(),
773 recall: None,
774 recall_auto_inject: false,
775 learning: None,
776 stuck: StuckPolicy::default(),
777 monotony: MonotonyPolicy::default(),
778 max_input_tokens: None,
779 compaction: CompactPolicy::default(),
780 // On by default, because the failure it catches is invisible: a
781 // turn that produced nothing is reported as a turn that finished.
782 tool_results: ToolResultPolicy::default(),
783 acceptance: vec![Arc::new(acceptance::NonEmptyAnswer)],
784 acceptance_retries: 1,
785 system: Vec::new(),
786 model_roles: std::collections::HashMap::new(),
787 compactor_custom: false,
788 }
789 }
790
791 /// Register an auxiliary model under a role name — the loop's seam for
792 /// "side tasks don't have to run on the main model".
793 ///
794 /// The main conversation stays pinned to one model, which keeps its
795 /// provider prompt-cache prefix byte-stable; compaction summaries, memory
796 /// synthesis, judging, and subagents can go to a cheaper or specialist
797 /// model instead. Components read roles via [`model_for`](Self::model_for)
798 /// and fall back to the main model when a role is unregistered.
799 ///
800 /// One role is wired automatically: registering `"compactor"` upgrades the
801 /// default structural compactor to a [`ModelBackedCompactor`] on that
802 /// model — unless the host already installed its own via
803 /// [`with_compactor`](Self::with_compactor), which always wins regardless
804 /// of call order.
805 ///
806 /// ```ignore
807 /// let agent = AgentLoop::boxed(main)
808 /// .with_model_role("compactor", cheap.clone())
809 /// .with_model_role("judge", strong);
810 /// ```
811 pub fn with_model_role(mut self, role: impl Into<String>, model: Arc<dyn Model>) -> Self {
812 let role = role.into();
813 if role == "compactor" && !self.compactor_custom {
814 self.compactor = Arc::new(crate::compactor::ModelBackedCompactor::new(model.clone()));
815 }
816 self.model_roles.insert(role, model);
817 self
818 }
819
820 /// Look up an auxiliary model by role. `None` means "no model registered
821 /// for this role — use the main model"; callers fall back rather than fail,
822 /// so wiring stays optional everywhere.
823 pub fn model_for(&self, role: &str) -> Option<Arc<dyn Model>> {
824 self.model_roles.get(role).cloned()
825 }
826
827 /// Set a system instruction applied to every run (into `Context.system`) —
828 /// e.g. "answer only via the governed tools; never claim you can't access
829 /// data; never invent numbers". This is the first-class seam for a system
830 /// prompt; small local models in particular need it to reliably call tools
831 /// instead of refusing or hallucinating.
832 pub fn with_system(mut self, text: impl Into<String>) -> Self {
833 self.system = vec![Block::Text(text.into())];
834 self
835 }
836
837 /// Cap context tokens below the model's own window.
838 ///
839 /// Compaction fires at a fraction of this, so it is also the only way to
840 /// exercise the compaction path without first paying for a hundred
841 /// thousand tokens of real conversation — which is how "does a long run
842 /// survive being compacted" stayed untested.
843 pub fn with_max_input_tokens(mut self, n: u32) -> Self {
844 self.max_input_tokens = Some(n);
845 self
846 }
847
848 /// Override the loop-detection policy (thresholds, or disable entirely).
849 pub fn with_stuck_policy(mut self, policy: StuckPolicy) -> Self {
850 self.stuck = policy;
851 self
852 }
853
854 /// Opt in to (or retune) the single-tool monotony signal. Independent of
855 /// [`with_stuck_policy`](Self::with_stuck_policy): both detectors run, with
856 /// their own thresholds and their own `Outcome::Stuck` reason.
857 ///
858 /// ```ignore
859 /// let agent = AgentLoop::boxed(model)
860 /// .with_monotony_policy(MonotonyPolicy { enabled: true, ..Default::default() });
861 /// ```
862 pub fn with_monotony_policy(mut self, policy: MonotonyPolicy) -> Self {
863 self.monotony = policy;
864 self
865 }
866
867 /// Override the compaction hysteresis policy. See [`CompactPolicy`].
868 /// Set the ceiling on a single tool result. See [`ToolResultPolicy`].
869 pub fn with_tool_result_policy(mut self, policy: ToolResultPolicy) -> Self {
870 self.tool_results = policy;
871 self
872 }
873
874 pub fn with_compact_policy(mut self, policy: CompactPolicy) -> Self {
875 self.compaction = policy;
876 self
877 }
878
879 /// Streaming is the default; pass `false` to opt out and use one
880 /// non-streaming `Model::complete()` per turn. Streamed turns go through
881 /// `Model::stream()`, and hooks subscribed to `Event::ModelTokenDelta` see
882 /// each fragment as it arrives; the rest of the loop is unchanged.
883 pub fn with_streaming(mut self, enable: bool) -> Self {
884 self.streaming = enable;
885 self
886 }
887
888 /// Hand the run a token the caller can cancel from anywhere.
889 ///
890 /// The token belongs to the **loop, not to one run**, and cancellation is
891 /// **one-way**: once `cancel()` has been called, every later `run` (and
892 /// every later `Session::turn`) on this loop returns `Outcome::Cancelled`
893 /// immediately, with `iters: 0`. That is right for a loop built for one
894 /// job and wrong for one that serves many turns. For per-turn cancellation
895 /// give each turn its own token — either rebuild the loop, or keep a parent
896 /// and hand out `parent.child_token()`, dropping the child when the turn
897 /// ends so the parent stays live:
898 ///
899 /// ```ignore
900 /// let token = CancellationToken::new();
901 /// let agent = AgentLoop::new(model).with_cancellation(token.clone());
902 /// // … later, from anywhere — this loop is now finished with, permanently:
903 /// token.cancel();
904 /// ```
905 ///
906 /// The field is public, so the simplest per-turn form is to assign
907 /// `loop_.cancel = fresh_token` before each run, which is what
908 /// `harness code` does.
909 ///
910 /// Cloning a `CancellationToken` shares it, so keep one and pass a clone.
911 pub fn with_cancellation(mut self, token: CancellationToken) -> Self {
912 self.cancel = token;
913 self
914 }
915
916 /// Add a condition the run must satisfy before it can report success.
917 pub fn with_acceptance(mut self, a: Arc<dyn Acceptance>) -> Self {
918 self.acceptance.push(a);
919 self
920 }
921
922 /// Replace the acceptance set outright (including the default).
923 pub fn with_acceptance_set(mut self, set: Vec<Arc<dyn Acceptance>>) -> Self {
924 self.acceptance = set;
925 self
926 }
927
928 pub fn with_acceptance_retries(mut self, n: u32) -> Self {
929 self.acceptance_retries = n;
930 self
931 }
932
933 pub fn with_tool_timeout(mut self, t: Option<Duration>) -> Self {
934 self.tool_timeout = t;
935 self
936 }
937
938 pub fn with_compactor(mut self, c: Arc<dyn Compactor>) -> Self {
939 self.compactor = c;
940 // An explicit compactor always wins over the "compactor" model-role
941 // convenience, in either call order.
942 self.compactor_custom = true;
943 self
944 }
945
946 pub fn with_tool(mut self, t: Arc<dyn harness_core::Tool>) -> Self {
947 self.tools.insert(t);
948 self
949 }
950
951 pub fn with_guide(mut self, g: Arc<dyn Guide>) -> Self {
952 self.guides.push(g);
953 self
954 }
955
956 pub fn with_sensor(mut self, s: Arc<dyn Sensor>) -> Self {
957 self.sensors.push(s);
958 self
959 }
960
961 pub fn with_hook(mut self, h: Arc<dyn harness_core::Hook>) -> Self {
962 self.hooks.register(h);
963 self
964 }
965
966 /// Pull in every `#[hook]`-registered hook.
967 pub fn with_macro_hooks(mut self) -> Self {
968 self.hooks = self.hooks.with_macro_hooks_take();
969 self
970 }
971
972 /// Enable cross-session recall: capture every turn into `store` and
973 /// register the `session_search` tool. Owner + session id are read from
974 /// `world.profile.extra["recall_owner"|"recall_session"]` at run time.
975 pub fn with_recall(mut self, store: Arc<dyn harness_core::RecallStore>) -> Self {
976 self.tools
977 .insert(Arc::new(crate::SessionSearchTool::new(store.clone())));
978 self.recall = Some(store);
979 self
980 }
981
982 /// Capture every turn into `store` **without** registering a search tool.
983 ///
984 /// Ingest and retrieval are separate concerns that [`with_recall`] happens
985 /// to bundle, and the bundling is a trap: capture only ever happens when
986 /// `self.recall` is set, so a host that wants a different search tool — one
987 /// scoped per tenant, or one that copes with a language the backend's index
988 /// does not — has no way to get the writes without also getting
989 /// `session_search`, and ends up offering the model two overlapping tools
990 /// to choose between.
991 ///
992 /// Use this, then register whichever retrieval tool suits the deployment.
993 pub fn with_recall_ingest(mut self, store: Arc<dyn harness_core::RecallStore>) -> Self {
994 self.recall = Some(store);
995 self
996 }
997
998 /// After `with_recall`, also auto-inject top-k relevant past context at
999 /// session start (off by default — tool-only is prompt-cache friendly).
1000 pub fn auto_inject(mut self) -> Self {
1001 self.recall_auto_inject = true;
1002 self
1003 }
1004
1005 /// Enable the self-evolving learning loop: after a session that made
1006 /// `>= cfg.nudge_interval` tool calls, fork a review subagent (white-listed to
1007 /// `cfg.tools`) to update skills + memory from the transcript. Best-effort.
1008 pub fn with_learning_loop(mut self, cfg: LearningConfig) -> Self {
1009 self.learning = Some(cfg);
1010 self
1011 }
1012
1013 /// Set the default response format for all runs through this loop. See
1014 /// [`ResponseFormat`]. For typed deserialisation, prefer `run_typed::<T>()`.
1015 pub fn with_response_format(mut self, fmt: ResponseFormat) -> Self {
1016 self.response_format = fmt;
1017 self
1018 }
1019
1020 /// Shortcut for `with_response_format(ResponseFormat::JsonSchema { name, schema })`.
1021 /// Accepts a raw `serde_json::Value` so callers can hand-roll the schema or
1022 /// pull it from `schemars::schema_for!(T)`.
1023 pub fn with_response_schema(self, name: impl Into<String>, schema: serde_json::Value) -> Self {
1024 self.with_response_format(ResponseFormat::JsonSchema {
1025 name: name.into(),
1026 schema,
1027 })
1028 }
1029
1030 pub async fn run(&self, task: Task, world: &mut World) -> Result<Outcome, HarnessError> {
1031 let max = harness_core::Policy::default().max_iters;
1032 self.run_with_max_iters(task, world, max).await
1033 }
1034
1035 /// Run, and hand back the evidence alongside the result.
1036 ///
1037 /// The [`Receipt`] is built here rather than by the caller because the loop
1038 /// already knows the two things a caller would otherwise have to restate —
1039 /// the task and the model — and restating them is how a receipt ends up
1040 /// describing a different run than the one that happened. `now_ms` stays a
1041 /// parameter: this crate does not read the clock, so a receipt is
1042 /// reproducible in a test.
1043 pub async fn run_receipted(
1044 &self,
1045 task: Task,
1046 world: &mut World,
1047 now_ms: i64,
1048 ) -> Result<(Outcome, Receipt), HarnessError> {
1049 let description = task.description.clone();
1050 let handle = self.model.info().handle;
1051 let outcome = self.run(task, world).await?;
1052 let receipt = ReceiptBuilder::new(description, handle, now_ms).build(&outcome);
1053 Ok((outcome, receipt))
1054 }
1055
1056 /// Advance a [`Goal`] by one phase, recording the result durably.
1057 ///
1058 /// Returns `None` when every phase is done — so a resume loop is
1059 /// `while let Some(..) = loop_.run_goal(..).await?`.
1060 ///
1061 /// The phase is marked `Running` and **saved before the model starts**, so
1062 /// a process that dies mid-run leaves a goal that says where it was rather
1063 /// than one that looks untouched. It is saved again afterwards on both
1064 /// paths. That second save on the failure path is the whole reason this
1065 /// method exists: written out by hand at each call site it is four lines,
1066 /// and the failure branch is the one that gets forgotten — which loses
1067 /// exactly the run you most wanted a record of.
1068 pub async fn run_goal(
1069 &self,
1070 goal: &mut Goal,
1071 store: &GoalStore,
1072 world: &mut World,
1073 now_ms: i64,
1074 ) -> Result<Option<(Outcome, Receipt)>, HarnessError> {
1075 let Some(i) = goal.start_current(now_ms) else {
1076 return Ok(None);
1077 };
1078 let _ = store.save(goal);
1079
1080 let task = Task {
1081 description: goal.brief(),
1082 source: None,
1083 deadline: None,
1084 };
1085 let result = self.run_receipted(task, world, now_ms).await;
1086
1087 match &result {
1088 Ok((_, receipt)) => {
1089 if receipt.passed {
1090 goal.finish(i, receipt.summary(), now_ms);
1091 } else {
1092 goal.fail(i, receipt.summary(), now_ms);
1093 }
1094 }
1095 // A run that errored outright still happened, and the goal has to
1096 // say so or a resume will retry it as though it were untouched.
1097 Err(e) => goal.fail(i, format!("the run errored: {e}"), now_ms),
1098 }
1099 let _ = store.save(goal);
1100
1101 result.map(Some)
1102 }
1103
1104 pub async fn run_with_max_iters(
1105 &self,
1106 task: Task,
1107 world: &mut World,
1108 max_iters: u32,
1109 ) -> Result<Outcome, HarnessError> {
1110 self.run_with_seed_history(task, Vec::new(), world, max_iters)
1111 .await
1112 }
1113
1114 /// Run the agent and deserialise the terminal reply into `T`.
1115 ///
1116 /// The schema for `T` is derived via `schemars::schema_for!(T)` and
1117 /// installed as `ResponseFormat::JsonSchema` for this run only — any
1118 /// pre-existing `self.response_format` is ignored. On success the
1119 /// returned `T` is parsed from `Outcome::Done.text` (or, on budget
1120 /// exhaustion, from `Outcome::BudgetExhausted.last_text`).
1121 ///
1122 /// Errors:
1123 /// - `HarnessError::Other` if the model returns no text at all
1124 /// - `HarnessError::Other` if `serde_json::from_str::<T>(text)` fails —
1125 /// the original text is included in the message for debugging.
1126 pub async fn run_typed<T>(&self, task: Task, world: &mut World) -> Result<T, HarnessError>
1127 where
1128 T: serde::de::DeserializeOwned + schemars::JsonSchema + 'static,
1129 {
1130 let max = harness_core::Policy::default().max_iters;
1131 self.run_typed_with_max_iters::<T>(task, world, max).await
1132 }
1133
1134 /// Like `run_typed` but with explicit `max_iters`.
1135 pub async fn run_typed_with_max_iters<T>(
1136 &self,
1137 task: Task,
1138 world: &mut World,
1139 max_iters: u32,
1140 ) -> Result<T, HarnessError>
1141 where
1142 T: serde::de::DeserializeOwned + schemars::JsonSchema + 'static,
1143 {
1144 let schema_root = schemars::schema_for!(T);
1145 let schema = serde_json::to_value(&schema_root)
1146 .map_err(|e| HarnessError::Other(format!("response schema: {e}")))?;
1147 let name = std::any::type_name::<T>()
1148 .rsplit("::")
1149 .next()
1150 .unwrap_or("response")
1151 .to_string();
1152 let fmt = ResponseFormat::JsonSchema { name, schema };
1153 let outcome = self
1154 .run_with_response_format(task, world, max_iters, fmt)
1155 .await?;
1156 let text = match outcome {
1157 Outcome::Done { text: Some(t), .. }
1158 | Outcome::BudgetExhausted {
1159 last_text: Some(t), ..
1160 }
1161 | Outcome::Stuck {
1162 last_text: Some(t), ..
1163 }
1164 | Outcome::Cancelled {
1165 last_text: Some(t), ..
1166 } => t,
1167 Outcome::Done { text: None, .. } => {
1168 return Err(HarnessError::Other(
1169 "run_typed: model returned no text".into(),
1170 ));
1171 }
1172 Outcome::Stuck {
1173 last_text: None, ..
1174 } => {
1175 return Err(HarnessError::Other(
1176 "run_typed: agent stuck with no text".into(),
1177 ));
1178 }
1179 Outcome::BudgetExhausted {
1180 last_text: None, ..
1181 } => {
1182 return Err(HarnessError::Other(
1183 "run_typed: budget exhausted with no text".into(),
1184 ));
1185 }
1186 Outcome::Cancelled {
1187 last_text: None, ..
1188 } => {
1189 return Err(HarnessError::Other(
1190 "run_typed: run cancelled with no text".into(),
1191 ));
1192 }
1193 };
1194 serde_json::from_str::<T>(&text).map_err(|e| {
1195 HarnessError::Other(format!(
1196 "run_typed: decode {} failed: {e} — raw text was: {text}",
1197 std::any::type_name::<T>()
1198 ))
1199 })
1200 }
1201
1202 /// Run with a one-off `ResponseFormat` override (doesn't touch `self`).
1203 pub async fn run_with_response_format(
1204 &self,
1205 task: Task,
1206 world: &mut World,
1207 max_iters: u32,
1208 fmt: ResponseFormat,
1209 ) -> Result<Outcome, HarnessError> {
1210 // Borrow checker won't let us swap `self.response_format` because
1211 // `self` is `&`. Easiest workaround: hand-roll the same setup that
1212 // `run_with_seed_history` does, but with our `fmt`. We do this by
1213 // calling through a private helper.
1214 self.run_with_seed_history_and_format(task, Vec::new(), world, max_iters, Some(fmt))
1215 .await
1216 }
1217
1218 async fn run_with_seed_history_and_format(
1219 &self,
1220 task: Task,
1221 seed: Vec<Turn>,
1222 world: &mut World,
1223 max_iters: u32,
1224 fmt_override: Option<ResponseFormat>,
1225 ) -> Result<Outcome, HarnessError> {
1226 let mut ctx = Context::new(task);
1227 ctx.policy.max_iters = max_iters;
1228 ctx.tools = self.tools.schemas();
1229 ctx.history = seed;
1230 ctx.response_format = fmt_override.unwrap_or_else(|| self.response_format.clone());
1231 self.run_built_context(ctx, world).await
1232 }
1233
1234 /// Like `run_with_max_iters` but seeds `ctx.history` with `seed` **before**
1235 /// the current user task is appended. Use this for multi-turn REPLs so
1236 /// prior conversation lives in `ctx.history` (where the Compactor can see
1237 /// it) instead of being concatenated into `task.description` (where it
1238 /// previously bypassed compaction entirely — see audit #2).
1239 pub async fn run_with_seed_history(
1240 &self,
1241 task: Task,
1242 seed: Vec<Turn>,
1243 world: &mut World,
1244 max_iters: u32,
1245 ) -> Result<Outcome, HarnessError> {
1246 self.run_with_seed_and_metadata(task, seed, Default::default(), world, max_iters)
1247 .await
1248 }
1249
1250 /// Like [`run_with_seed_history`](Self::run_with_seed_history) but also seeds
1251 /// `ctx.metadata` with per-request key/values. Hooks and a
1252 /// [`ModelRouter`](harness_models::ModelRouter) read this map — e.g.
1253 /// `audit.actor` / `audit.session` for the audit trail, or
1254 /// `router.keep_local` to pin a request to the local model. This is the
1255 /// entry point a serving layer uses to pass caller identity and routing
1256 /// flags into a single, shared, reused loop.
1257 pub async fn run_with_seed_and_metadata(
1258 &self,
1259 task: Task,
1260 seed: Vec<Turn>,
1261 metadata: std::collections::BTreeMap<String, serde_json::Value>,
1262 world: &mut World,
1263 max_iters: u32,
1264 ) -> Result<Outcome, HarnessError> {
1265 let mut ctx = Context::new(task);
1266 ctx.policy.max_iters = max_iters;
1267 ctx.tools = self.tools.schemas();
1268 ctx.history = seed;
1269 ctx.metadata = metadata;
1270 ctx.response_format = self.response_format.clone();
1271 self.run_built_context(ctx, world).await
1272 }
1273
1274 /// Start a persistent multi-turn [`Session`]. Each `turn` re-runs the loop
1275 /// against the accumulated history with a **stable prefix** (system +
1276 /// name-sorted tool schemas), so a provider's prefix cache (e.g. DeepSeek's,
1277 /// ~10% price on cache-hit tokens) hits across turns instead of paying full
1278 /// price to re-read the same bytes every round. For maximum hit rate, keep
1279 /// your guides' output stable (put per-turn volatile context in the message,
1280 /// not a recomputed system guide).
1281 pub fn session(&self) -> Session<'_, M> {
1282 Session {
1283 loop_: self,
1284 history: Vec::new(),
1285 max_iters: harness_core::Policy::default().max_iters,
1286 }
1287 }
1288
1289 /// Inner ReAct loop on an already-prepared `Context`. Use the public
1290 /// `run*` methods unless you need to inject a non-standard `Context`
1291 /// (e.g. `run_with_response_format` does to apply a one-off
1292 /// `ResponseFormat` without mutating `self`).
1293 ///
1294 /// The run executes inside an `agent_run` span, and every model call and
1295 /// tool call inside a child span of it (`chat`, `execute_tool`). The loop
1296 /// owns these rather than a hook, because only the loop knows when a call
1297 /// really starts and ends: read-only tools are dispatched in parallel
1298 /// *before* their `PreToolUse` fires, so a hook timing them would see a
1299 /// zero-length call. Spans cost nothing without a subscriber; with an OTLP
1300 /// layer (feature `otel`) they are the trace waterfall.
1301 async fn run_built_context(
1302 &self,
1303 ctx: Context,
1304 world: &mut World,
1305 ) -> Result<Outcome, HarnessError> {
1306 let info = self.model.info();
1307 let span = tracing::info_span!(
1308 target: "harness.telemetry",
1309 "agent_run",
1310 "gen_ai.operation.name" = "invoke_agent",
1311 "gen_ai.provider.name" = %info.provider,
1312 "gen_ai.request.model" = %info.model,
1313 outcome = Empty,
1314 "otel.status_code" = Empty,
1315 "otel.status_message" = Empty,
1316 );
1317 let started = std::time::Instant::now();
1318 let r = self.run_in_span(ctx, world).instrument(span.clone()).await;
1319 let outcome = match &r {
1320 Ok(Outcome::Done { .. }) => "done",
1321 Ok(Outcome::BudgetExhausted { .. }) => "budget_exhausted",
1322 Ok(Outcome::Stuck { .. }) => "stuck",
1323 Ok(Outcome::Cancelled { .. }) => "cancelled",
1324 Err(e) => {
1325 span.record("otel.status_code", "error")
1326 .record("otel.status_message", e.to_string());
1327 "error"
1328 }
1329 };
1330 span.record("outcome", outcome);
1331 meters::run(outcome, started.elapsed());
1332 r
1333 }
1334
1335 async fn run_in_span(
1336 &self,
1337 mut ctx: Context,
1338 world: &mut World,
1339 ) -> Result<Outcome, HarnessError> {
1340 if ctx.system.is_empty() && !self.system.is_empty() {
1341 ctx.system = self.system.clone();
1342 }
1343
1344 // Size the context budget to the model actually in use.
1345 //
1346 // Compaction fires at a fraction of `max_input_tokens`, so a value
1347 // unrelated to the model is wrong in one of two directions: too high and
1348 // the provider rejects the request before the compactor ever runs (a 32k
1349 // model under the 150k default would need 112,500 tokens to trigger, and
1350 // it cannot hold that many); too low and the loop discards history it
1351 // could have kept. Nothing read `ModelInfo::context_window` before this —
1352 // the framework's own defaults disagreed, 150,000 against 128,000.
1353 //
1354 // Only when the caller left the default in place; an explicit policy is
1355 // a decision and stays untouched. The output allowance is reserved,
1356 // because the window is shared between the prompt and the reply.
1357 if let Some(n) = self.max_input_tokens {
1358 // An explicit cap wins over both the default and the model window.
1359 ctx.policy.max_input_tokens = n.max(1);
1360 } else if ctx.policy.max_input_tokens == harness_core::Policy::default().max_input_tokens {
1361 let window = self.model.info().context_window;
1362 if window > 0 {
1363 // Reserve room for the reply, but never let the reservation eat
1364 // the window: an 8k model with the default 8k output allowance
1365 // would leave 0 tokens for input, and every turn — a 26-token
1366 // one included — would run all five compaction stages against a
1367 // budget of 1. Cap the reservation at a quarter of the window.
1368 let reserve = ctx.policy.max_output_tokens.min(window / 4);
1369 ctx.policy.max_input_tokens = window.saturating_sub(reserve).max(1);
1370 }
1371 }
1372
1373 self.hooks.fire(
1374 &Event::SessionStart {
1375 source: SessionSource::Startup,
1376 },
1377 world,
1378 );
1379
1380 // ── recall: resolve owner/session, ensure the session row ──
1381 let (recall_owner, recall_session) = if self.recall.is_some() {
1382 use std::sync::atomic::Ordering;
1383 let owner = crate::recall_owner(world);
1384 let session = world
1385 .profile
1386 .extra
1387 .get("recall_session")
1388 .and_then(|v| v.as_str())
1389 .map(|s| s.to_string())
1390 .unwrap_or_else(|| {
1391 format!(
1392 "sess-{}-{}",
1393 world.clock.now_ms(),
1394 RECALL_SEQ.fetch_add(1, Ordering::SeqCst)
1395 )
1396 });
1397 if let Some(store) = &self.recall {
1398 let meta = harness_core::SessionMeta::new(&session, world.clock.now_ms());
1399 if let Err(e) = store.ensure_session(&owner, &session, &meta).await {
1400 tracing::warn!(error = %e, "recall ensure_session failed");
1401 }
1402 }
1403 (owner, session)
1404 } else {
1405 (String::new(), String::new())
1406 };
1407
1408 let recall_guide: Option<Arc<dyn Guide>> = if self.recall_auto_inject {
1409 if self.recall.is_none() {
1410 tracing::warn!(
1411 "auto_inject() set but no recall store — call with_recall(store) first; skipping recall guide"
1412 );
1413 None
1414 } else {
1415 self.recall
1416 .clone()
1417 .map(|s| Arc::new(crate::RecallGuide::new(s)) as Arc<dyn Guide>)
1418 }
1419 } else {
1420 None
1421 };
1422 let all_guides: Vec<&Arc<dyn Guide>> =
1423 self.guides.iter().chain(recall_guide.iter()).collect();
1424 for g in &all_guides {
1425 if g.scope().matches(&ctx.task) {
1426 self.hooks.fire(&Event::PreGuide { guide: g.id() }, world);
1427 g.apply(&mut ctx, world).await?;
1428 self.hooks.fire(&Event::PostGuide { guide: g.id() }, world);
1429 }
1430 }
1431
1432 ctx.history.push(Turn {
1433 role: TurnRole::User,
1434 blocks: vec![Block::Text(ctx.task.description.clone())],
1435 });
1436
1437 if self.recall.is_some() {
1438 self.recall_append(
1439 &recall_owner,
1440 &recall_session,
1441 harness_core::RecallMessage::new(
1442 "user",
1443 ctx.task.description.clone(),
1444 world.clock.now_ms(),
1445 ),
1446 )
1447 .await;
1448 }
1449
1450 // Running totals — surface to caller even on BudgetExhausted.
1451 let mut tools_called: u32 = 0;
1452 let mut total_usage = harness_core::Usage::default();
1453 let mut last_text: Option<String> = None;
1454
1455 // Stuck-detector state: the previous round's tool-call fingerprint and
1456 // how many consecutive rounds have repeated it.
1457 let mut last_fingerprint: Option<String> = None;
1458 let mut repeat_count: u32 = 0;
1459 // Monotony state: the tool that has been the round's only tool, and for
1460 // how many consecutive rounds. Separate counter from `repeat_count` on
1461 // purpose — the two detectors answer different questions.
1462 let mut monotone_tool: Option<String> = None;
1463 let mut monotone_rounds: u32 = 0;
1464 // Read-only calls already answered this run, cleared whenever anything
1465 // mutates the world. See `ToolResultPolicy::dedupe_repeats`.
1466 let mut answered: std::collections::HashSet<String> = std::collections::HashSet::new();
1467 // How many times the model has stopped mid-work with nothing to show.
1468 let mut acceptance_retries_left = self.acceptance_retries;
1469 // Consecutive turns cut off by the output-token limit with nothing to
1470 // act on. Such a turn is not an answer, however the loop ends.
1471 let mut truncated_turns: u32 = 0;
1472
1473 // The contract as it stood before the model touched anything. Taken
1474 // now, not at verdict time, because the whole point is to compare
1475 // against a state the model has not had the chance to influence.
1476 // Set once a sealed file is found to have moved; makes the failure
1477 // terminal and carries the reason out to the caller.
1478 let mut seal_breached: Option<String> = None;
1479 let sealed_before: crate::seal::SealSet = {
1480 let paths: Vec<std::path::PathBuf> =
1481 self.acceptance.iter().flat_map(|a| a.seals()).collect();
1482 if paths.is_empty() {
1483 crate::seal::SealSet::default()
1484 } else {
1485 crate::seal::SealSet::capture(&world.repo.root, paths)
1486 }
1487 };
1488
1489 // How far the loop actually got. Equal to `max_iters` on a normal
1490 // budget exhaustion, less when the wall clock stopped it first.
1491 let mut iters_done = ctx.policy.max_iters;
1492 let mut deadline_reached = false;
1493
1494 for iter in 0..ctx.policy.max_iters {
1495 if self.cancel.is_cancelled() {
1496 tracing::info!(iter, "run cancelled by caller");
1497 self.hooks.fire(&Event::Cancelled, world);
1498 self.hooks.fire(&Event::SessionEnd, world);
1499 return Ok(Outcome::Cancelled {
1500 iters: iter,
1501 last_text,
1502 tools_called,
1503 usage: total_usage,
1504 });
1505 }
1506
1507 // ── wall-clock budget ───────────────────────────────────────
1508 // `max_iters` bounds *steps*, which says nothing about elapsed
1509 // time: one iteration can be a 100ms read or a 20-minute build.
1510 // An unattended run is bounded in hours, not rounds, so a task
1511 // may carry a deadline — and reaching it goes through the same
1512 // forced-synthesis exit as a spent step budget, so the caller
1513 // gets the conclusion of the work rather than a dropped future.
1514 if let Some(at) = ctx.task.deadline
1515 && world.clock.now_ms() >= at
1516 {
1517 tracing::warn!(
1518 iter,
1519 deadline = at,
1520 "task deadline reached — forcing a final answer"
1521 );
1522 iters_done = iter;
1523 deadline_reached = true;
1524 break;
1525 }
1526 self.hooks.fire(&Event::Heartbeat { iter }, world);
1527
1528 // Compaction with hysteresis: only once over the high-water mark,
1529 // then escalate stage-by-stage, re-estimating after each, and stop
1530 // the moment we're back under target. Avoids over-compacting to the
1531 // lossy AutoCompact stage and avoids rewriting history (→ prefix
1532 // cache miss) every turn. Token estimate is calibrated against the
1533 // last real `input_tokens` via `CALIBRATION_KEY`.
1534 let mut budget = self.compactor.budget(&ctx);
1535 if budget.ratio() > self.compaction.high_water {
1536 for stage in CompactionStage::ALL {
1537 if budget.ratio() <= self.compaction.target {
1538 break;
1539 }
1540 self.hooks.fire(&Event::PreCompact { stage }, world);
1541 let before = budget.used;
1542 let turns_before = ctx.history.len();
1543 self.compactor.compact(stage, &mut ctx).await?;
1544 budget = self.compactor.budget(&ctx);
1545 // Compaction used to leave no trace anywhere: not in the
1546 // log, not in the transcript. That is a bad property for
1547 // the one mechanism that rewrites the conversation, and it
1548 // is how a compaction bug that killed long runs stayed
1549 // invisible — the only symptom was a provider error with
1550 // no hint of what had touched the history.
1551 tracing::info!(
1552 stage = ?stage,
1553 tokens_before = before,
1554 tokens_after = budget.used,
1555 turns_before,
1556 turns_after = ctx.history.len(),
1557 "compacted context"
1558 );
1559 meters::compaction(stage, before, budget.used);
1560 self.hooks.fire(
1561 &Event::PostCompact {
1562 stage,
1563 before,
1564 after: budget.used,
1565 },
1566 world,
1567 );
1568 }
1569 }
1570
1571 // Per-iteration guides — recall-style adapters that want to
1572 // refresh their injected context every turn (e.g. MemoryGuide
1573 // re-recalling against the latest user message). Default
1574 // `apply_before_iter` is a no-op, so this loop is cheap for
1575 // guides that don't override it.
1576 for g in &all_guides {
1577 if g.scope().matches(&ctx.task)
1578 && let Err(e) = g.apply_before_iter(&mut ctx, world).await
1579 {
1580 tracing::warn!(guide = %g.id(), error = %e, "apply_before_iter failed; continuing");
1581 }
1582 }
1583
1584 self.hooks.fire(&Event::PreModel { ctx: &ctx }, world);
1585 let mut partial = String::new();
1586 let Some(out) = self.model_step(&ctx, world, &mut partial, iter).await? else {
1587 tracing::info!(iter, "run cancelled during model step");
1588 // What the stream delivered before the cancel is what the person
1589 // saw; it belongs to the outcome, not to the dropped future.
1590 if !partial.is_empty() {
1591 last_text = Some(std::mem::take(&mut partial));
1592 }
1593 self.hooks.fire(&Event::Cancelled, world);
1594 self.hooks.fire(&Event::SessionEnd, world);
1595 return Ok(Outcome::Cancelled {
1596 iters: iter + 1,
1597 last_text,
1598 tools_called,
1599 usage: total_usage,
1600 });
1601 };
1602 self.hooks.fire(&Event::PostModel { out: &out }, world);
1603
1604 // Calibrate the compactor against ground truth: `ctx` still holds
1605 // exactly what we just sent, so `budget().used` is the estimate for
1606 // it. Nudge the stored correction so estimate·correction ≈ the
1607 // model's real `input_tokens` next time. Self-correcting (converges
1608 // even as the raw estimate drifts); clamped so one odd turn can't
1609 // blow it up. This is what makes compaction fire at the right moment
1610 // for *this* model + language instead of a blind char heuristic.
1611 if out.usage.input_tokens > 0 {
1612 let used = self.compactor.budget(&ctx).used;
1613 if used > 0 {
1614 let prev = ctx
1615 .metadata
1616 .get(CALIBRATION_KEY)
1617 .and_then(|v| v.as_f64())
1618 .filter(|f| f.is_finite() && *f > 0.0)
1619 .unwrap_or(1.0);
1620 let next =
1621 (prev * out.usage.input_tokens as f64 / used as f64).clamp(0.1, 10.0);
1622 ctx.metadata
1623 .insert(CALIBRATION_KEY.into(), serde_json::json!(next));
1624 }
1625 }
1626
1627 // Accumulate usage even if the run later exhausts budget.
1628 total_usage.input_tokens += out.usage.input_tokens;
1629 total_usage.output_tokens += out.usage.output_tokens;
1630 total_usage.cached_input_tokens += out.usage.cached_input_tokens;
1631 total_usage.cache_write_input_tokens += out.usage.cache_write_input_tokens;
1632 if let Some(t) = &out.text {
1633 last_text = Some(t.clone());
1634 }
1635 ctx.push_model_output(&out);
1636
1637 if self.recall.is_some() {
1638 let calls = if out.tool_calls.is_empty() {
1639 None
1640 } else {
1641 serde_json::to_string(&out.tool_calls).ok()
1642 };
1643 let mut m = harness_core::RecallMessage::new(
1644 "assistant",
1645 out.text.clone().unwrap_or_default(),
1646 world.clock.now_ms(),
1647 );
1648 m.tool_calls = calls;
1649 self.recall_append(&recall_owner, &recall_session, m).await;
1650 }
1651
1652 if out.tool_calls.is_empty() && out.stop_reason == harness_core::StopReason::MaxTokens {
1653 // The provider cut the turn off. Whatever came out (usually
1654 // all reasoning, sometimes half an answer) is not the model
1655 // saying "done", so it must never become `Outcome::Done`.
1656 truncated_turns += 1;
1657 tracing::warn!(
1658 truncated_turns,
1659 max_output_tokens = ctx.policy.max_output_tokens,
1660 "model turn hit the output token limit without a tool call"
1661 );
1662 if truncated_turns <= MAX_TRUNCATED_TURNS && iter + 1 < ctx.policy.max_iters {
1663 ctx.history.push(Turn {
1664 role: TurnRole::User,
1665 blocks: vec![Block::Text(TRUNCATED_TURN_NUDGE.into())],
1666 });
1667 continue;
1668 }
1669 let reason = format!(
1670 "the model hit the output token limit ({} tokens) {truncated_turns} turn(s) \
1671 in a row without producing an answer or a tool call",
1672 ctx.policy.max_output_tokens
1673 );
1674 self.hooks.fire(&Event::SessionEnd, world);
1675 return Ok(Outcome::Stuck {
1676 reason,
1677 repeated: truncated_turns,
1678 iters: iter + 1,
1679 last_text: out.text.filter(|t| !t.trim().is_empty()),
1680 tools_called,
1681 usage: total_usage,
1682 });
1683 }
1684 truncated_turns = 0;
1685
1686 if out.tool_calls.is_empty() {
1687 // The model stopping is its opinion that the work is done.
1688 // Before taking it as fact, run whatever the caller said
1689 // "done" actually means. A failure goes back as an instruction
1690 // and the loop carries on; the retry cap keeps a model that
1691 // ignores corrections from eating the budget.
1692 let mut verdict: Option<Verdict> = None;
1693 if !self.acceptance.is_empty() {
1694 // Record this turn first — the checks read the transcript.
1695 let mut probe = ctx.clone();
1696 probe.history.push(Turn {
1697 role: TurnRole::Assistant,
1698 blocks: out
1699 .text
1700 .as_deref()
1701 .filter(|t| !t.trim().is_empty())
1702 .map(|t| vec![Block::Text(t.to_string())])
1703 .unwrap_or_default(),
1704 });
1705 for check in &self.acceptance {
1706 let v = check.check(&probe, world).await;
1707 self.hooks.fire(
1708 &Event::AcceptanceChecked {
1709 name: check.name(),
1710 passed: v.passed,
1711 reason: &v.reason,
1712 },
1713 world,
1714 );
1715 if !v.passed {
1716 tracing::info!(
1717 check = check.name(),
1718 reason = %v.reason,
1719 "acceptance failed"
1720 );
1721 verdict = Some(v);
1722 break;
1723 }
1724 }
1725 // Everything passed. Say so explicitly: "checked, and it
1726 // holds up" is a different claim from "nobody looked", and
1727 // the host has to be able to tell them apart.
1728 verdict = verdict.or_else(|| Some(Verdict::passed()));
1729 }
1730
1731 // A pass is only worth the contract it was measured against.
1732 // Re-read the sealed files and refuse if any moved.
1733 if !sealed_before.is_empty() && verdict.as_ref().is_some_and(|v| v.passed) {
1734 let paths: Vec<std::path::PathBuf> =
1735 self.acceptance.iter().flat_map(|a| a.seals()).collect();
1736 let now = crate::seal::SealSet::capture(&world.repo.root, paths);
1737 let breaches = sealed_before.breaches(&now);
1738 if !breaches.is_empty() {
1739 let what = breaches
1740 .iter()
1741 .map(|b| b.describe())
1742 .collect::<Vec<_>>()
1743 .join("; ");
1744 tracing::error!(
1745 breaches = %what,
1746 "acceptance contract changed during the run — refusing the pass"
1747 );
1748 self.hooks
1749 .fire(&Event::SealBreached { detail: &what }, world);
1750 // Deliberately NOT a retry. Every other acceptance
1751 // failure is handed back as an instruction because the
1752 // model can act on it; this one would be handing back
1753 // "you edited the gate", to the party that edited it,
1754 // with the file still writable. The run is over.
1755 seal_breached = Some(what);
1756 verdict = Some(Verdict::failed(
1757 "the acceptance contract was modified during this run",
1758 ));
1759 }
1760 }
1761
1762 if let Some(v) = verdict.clone().filter(|v| !v.passed)
1763 && seal_breached.is_none()
1764 && acceptance_retries_left > 0
1765 && iter + 1 < ctx.policy.max_iters
1766 {
1767 acceptance_retries_left -= 1;
1768 ctx.history.push(Turn {
1769 role: TurnRole::User,
1770 blocks: vec![Block::Text(v.reason)],
1771 });
1772 continue;
1773 }
1774
1775 self.hooks.fire(&Event::TaskCompleted, world);
1776 self.hooks.fire(&Event::SessionEnd, world);
1777 self.run_learning_review(&ctx, world, tools_called).await;
1778 // Thinking models (e.g. Qwen3 via Ollama) sometimes emit the
1779 // whole answer into the reasoning channel and leave `text`
1780 // empty. Fall back to the reasoning so the turn isn't blank —
1781 // but `verified` says whether anyone agreed it was done.
1782 let text = out
1783 .text
1784 .filter(|t| !t.trim().is_empty())
1785 .or_else(|| out.reasoning.filter(|r| !r.trim().is_empty()));
1786 return Ok(Outcome::Done {
1787 text,
1788 iters: iter + 1,
1789 tools_called,
1790 usage: total_usage,
1791 verified: verdict,
1792 contract: sealed_before.clone(),
1793 seal_breach: seal_breached,
1794 });
1795 }
1796
1797 // Stuck detection runs *after* dispatch — see the block below the
1798 // tool loop, which needs this round's results to judge progress.
1799
1800 // Parallel-safe prefetch: dispatch the *leading run* of read-only
1801 // tool calls concurrently (a mutating tool is a serial barrier).
1802 // The sequential loop below still processes every call in order —
1803 // hooks, sensors, and history stay ordered — only the dispatch IO
1804 // overlaps. Reads before any write are safe; anything at/after the
1805 // first mutating call runs on the normal path.
1806 // Digest of what each tool handed back this round, for the stuck
1807 // check below. Digests rather than the payloads: results are
1808 // already capped, but a per-round copy of every one of them is
1809 // still a cost the detector does not need to pay.
1810 let mut round_results: Vec<u64> = Vec::new();
1811
1812 let mut prefetched: HashMap<String, Dispatched> = HashMap::new();
1813 {
1814 let lead: Vec<&_> = out
1815 .tool_calls
1816 .iter()
1817 .take_while(|c| {
1818 self.tools.risk(&c.name) == Some(harness_core::ToolRisk::ReadOnly)
1819 })
1820 .collect();
1821 if lead.len() > 1 {
1822 let futs = lead.iter().map(|c| {
1823 let mut w = world.clone();
1824 let action = Action {
1825 tool: c.name.clone(),
1826 call_id: c.id.clone(),
1827 args: c.args.clone(),
1828 };
1829 async move {
1830 let r = self.dispatch_bounded(&action, &mut w).await;
1831 (action.call_id, r)
1832 }
1833 });
1834 let results = tokio::select! {
1835 biased;
1836 _ = self.cancel.cancelled() => Vec::new(),
1837 rs = futures::future::join_all(futs) => rs,
1838 };
1839 for (id, r) in results {
1840 prefetched.insert(id, r);
1841 }
1842 }
1843 }
1844
1845 for call in &out.tool_calls {
1846 let action = Action {
1847 tool: call.name.clone(),
1848 call_id: call.id.clone(),
1849 args: call.args.clone(),
1850 };
1851
1852 // PreToolUse hook can deny destructive actions
1853 if let HookOutcome::Deny { reason } = self
1854 .hooks
1855 .fire(&Event::PreToolUse { action: &action }, world)
1856 {
1857 ctx.history.push(Turn {
1858 role: TurnRole::Tool,
1859 blocks: vec![Block::ToolResult {
1860 call_id: action.call_id.clone(),
1861 content: serde_json::json!({
1862 "ok": false,
1863 "denied_by_hook": reason,
1864 }),
1865 }],
1866 });
1867 if self.recall.is_some() {
1868 self.recall_append(
1869 &recall_owner,
1870 &recall_session,
1871 harness_core::RecallMessage::new(
1872 "tool",
1873 format!("[denied by hook] {reason}"),
1874 world.clock.now_ms(),
1875 )
1876 .with_tool_name(action.tool.clone()),
1877 )
1878 .await;
1879 }
1880 continue;
1881 }
1882
1883 // Use the concurrently-prefetched result if we have one;
1884 // otherwise dispatch now.
1885 let Dispatched {
1886 result,
1887 started_ms,
1888 finished_ms,
1889 } = if let Some(r) = prefetched.remove(&action.call_id) {
1890 r
1891 } else {
1892 self.dispatch_bounded(&action, world).await
1893 };
1894 tools_called += 1;
1895
1896 if self.cancel.is_cancelled() {
1897 tracing::info!(iter, "run cancelled during tool dispatch");
1898 self.hooks.fire(&Event::Cancelled, world);
1899 self.hooks.fire(&Event::SessionEnd, world);
1900 return Ok(Outcome::Cancelled {
1901 iters: iter + 1,
1902 last_text,
1903 tools_called,
1904 usage: total_usage,
1905 });
1906 }
1907
1908 // Decide the final payload *before* announcing the result, so
1909 // hooks, telemetry and the context all describe the same thing:
1910 // an audit that logs a 200 KB blob the model never saw is not an
1911 // audit of what happened.
1912 let result = ToolResult {
1913 content: self.shape_result(&action, &result, &mut answered, &world.repo.root),
1914 ..result
1915 };
1916 self.hooks.fire(
1917 &Event::ToolTimed {
1918 action: &action,
1919 started_ms,
1920 finished_ms,
1921 },
1922 world,
1923 );
1924 self.hooks.fire(
1925 &Event::PostToolUse {
1926 action: &action,
1927 result: &result,
1928 },
1929 world,
1930 );
1931
1932 round_results.push(result_digest(&action.tool, &result.content));
1933
1934 ctx.history.push(Turn {
1935 role: TurnRole::Tool,
1936 blocks: vec![Block::ToolResult {
1937 call_id: action.call_id.clone(),
1938 content: result.content.clone(),
1939 }],
1940 });
1941
1942 if self.recall.is_some() {
1943 let body = serde_json::to_string(&result.content).unwrap_or_default();
1944 self.recall_append(
1945 &recall_owner,
1946 &recall_session,
1947 harness_core::RecallMessage::new("tool", body, world.clock.now_ms())
1948 .with_tool_name(action.tool.clone()),
1949 )
1950 .await;
1951 }
1952
1953 // run self-correct sensors
1954 let mut all_signals = Vec::new();
1955 for s in &self.sensors {
1956 if s.stage() != Stage::SelfCorrect {
1957 continue;
1958 }
1959 self.hooks.fire(&Event::PreSensor { sensor: s.id() }, world);
1960 let sigs = s.observe(&action, world).await.unwrap_or_else(|e| {
1961 tracing::warn!(?e, "sensor failed");
1962 Vec::new()
1963 });
1964 self.hooks.fire(
1965 &Event::PostSensor {
1966 sensor: s.id(),
1967 signals: &sigs,
1968 },
1969 world,
1970 );
1971 all_signals.extend(sigs);
1972 }
1973 if !all_signals.is_empty() {
1974 let bundle = SignalSet::new(all_signals);
1975 let (patches, remaining) = bundle.partition_auto_fix();
1976
1977 // audit #7: each patch goes through PreAutoFix.
1978 // Hooks can Deny (skip silently). Default safelist on
1979 // RunCommand catches the obvious misuses with no hook.
1980 let approved: Vec<harness_core::FixPatch> = patches.into_iter().filter(|p| {
1981 if !is_default_safe_fix(p) {
1982 tracing::warn!(?p, "auto-fix rejected by default safelist (use PreAutoFix hook to override)");
1983 self.hooks.fire(&Event::PostAutoFix { patch: p, applied: false }, world);
1984 return false;
1985 }
1986 match self.hooks.fire(&Event::PreAutoFix { patch: p }, world) {
1987 HookOutcome::Deny { reason } => {
1988 tracing::warn!(?p, %reason, "auto-fix denied by hook");
1989 self.hooks.fire(&Event::PostAutoFix { patch: p, applied: false }, world);
1990 false
1991 }
1992 _ => true,
1993 }
1994 }).collect();
1995
1996 let applied = apply_patches(&approved, world).await;
1997 // Emit PostAutoFix for each approved patch with the application result.
1998 for (i, p) in approved.iter().enumerate() {
1999 self.hooks.fire(
2000 &Event::PostAutoFix {
2001 patch: p,
2002 applied: i < applied.len(),
2003 },
2004 world,
2005 );
2006 }
2007 if !applied.is_empty() {
2008 ctx.push_feedback(vec![harness_core::Signal {
2009 severity: harness_core::Severity::Hint,
2010 origin: "auto-fix".into(),
2011 message: format!(
2012 "applied {} auto-fix patch(es): {applied:?}",
2013 applied.len()
2014 ),
2015 agent_hint: Some(
2016 "re-check the affected files before continuing".into(),
2017 ),
2018 auto_fix: None,
2019 location: None,
2020 }]);
2021 }
2022 if remaining.has_blocking() {
2023 ctx.push_feedback(remaining.signals);
2024 }
2025 }
2026 }
2027
2028 // ── stuck detection ─────────────────────────────────────────
2029 // A round repeats when the request AND what came back were both
2030 // identical. The same call whose *result moved* is the model
2031 // watching something progress — polling a build, a background
2032 // job, a queue — not spinning, which is why this runs after
2033 // dispatch: before it, this round's results do not exist yet and
2034 // the check could only compare the request. Judging on the
2035 // request alone aborted runs that were making progress, and a
2036 // long unattended task is mostly made of such waits.
2037 //
2038 // A poll whose result never changes still counts, and still
2039 // aborts: burning model calls on a thing that is not moving is
2040 // exactly what this guard is for. What bounds a *legitimate*
2041 // long wait is `max_iters`, not this.
2042 if self.stuck.enabled {
2043 let fp = format!(
2044 "{}#{}",
2045 tool_call_fingerprint(&out.tool_calls),
2046 round_results
2047 .iter()
2048 .map(u64::to_string)
2049 .collect::<Vec<_>>()
2050 .join(",")
2051 );
2052 if last_fingerprint.as_ref() == Some(&fp) {
2053 repeat_count += 1;
2054 } else {
2055 repeat_count = 1;
2056 last_fingerprint = Some(fp);
2057 }
2058
2059 if repeat_count >= self.stuck.abort_after {
2060 let reason = format!(
2061 "repeated the same tool call {repeat_count}× with an unchanging result"
2062 );
2063 tracing::warn!(repeated = repeat_count, "stuck: aborting run");
2064 self.hooks.fire(&Event::SessionEnd, world);
2065 return Ok(Outcome::Stuck {
2066 reason,
2067 repeated: repeat_count,
2068 iters: iter + 1,
2069 last_text,
2070 tools_called,
2071 usage: total_usage,
2072 });
2073 }
2074
2075 if repeat_count == self.stuck.nudge_after {
2076 tracing::warn!(
2077 repeated = repeat_count,
2078 "stuck: nudging model to change approach"
2079 );
2080 ctx.push_feedback(vec![harness_core::Signal {
2081 severity: harness_core::Severity::Warn,
2082 origin: "stuck-detector".into(),
2083 message: format!(
2084 "You have issued the same tool call {repeat_count} rounds in a row \
2085 and the result has not changed."
2086 ),
2087 agent_hint: Some(
2088 "Stop repeating it. Inspect the actual tool result/error, try a \
2089 different approach, or give your final answer with no tool call. \
2090 If you are waiting on something, wait inside one call rather than \
2091 polling in a tight loop."
2092 .into(),
2093 ),
2094 auto_fix: None,
2095 location: None,
2096 }]);
2097 }
2098 }
2099
2100 // ── monotony detection ──────────────────────────────────────
2101 // The second signal, and deliberately not part of the block above:
2102 // that one asks "did this round repeat?", this one asks "has the
2103 // model been working a single tool and nothing else?". A guessing
2104 // spiral answers no to the first and yes to the second, which is
2105 // why the byte-identical policy stays exactly as strict as it is.
2106 //
2107 // Off unless the host asked for it — see `MonotonyPolicy` for why
2108 // no threshold is safe by default. Independent of `stuck.enabled`,
2109 // so a host can run either, both, or neither.
2110 if self.monotony.enabled {
2111 match sole_tool(&out.tool_calls) {
2112 // Same single tool as last round: the run continues.
2113 Some(name) if monotone_tool.as_deref() == Some(name) => monotone_rounds += 1,
2114 // A different single tool: a new run of one.
2115 Some(name) => {
2116 monotone_tool = Some(name.to_string());
2117 monotone_rounds = 1;
2118 }
2119 // A mixed round. Reaching for a second tool is movement,
2120 // and movement is what this detector is looking for.
2121 None => {
2122 monotone_tool = None;
2123 monotone_rounds = 0;
2124 }
2125 }
2126
2127 if monotone_rounds >= self.monotony.abort_after {
2128 let name = monotone_tool.clone().unwrap_or_default();
2129 let reason = format!(
2130 "called `{name}` for {monotone_rounds} consecutive rounds \
2131 without using any other tool"
2132 );
2133 tracing::warn!(tool = %name, rounds = monotone_rounds, "monotony: aborting run");
2134
2135 // Force the final answer before returning. A model caught
2136 // here is by definition one that will not stop calling
2137 // tools on its own, so terminating it without asking for
2138 // text hands the caller the same empty partial answer the
2139 // detector exists to prevent — earlier, and therefore
2140 // worse. Stripping the tools and asking once is the exit
2141 // the model could not find; it costs one model call and no
2142 // prompt bytes at all. (The budget-exhausted path has done
2143 // this since it existed; this is the same exit, reached
2144 // sooner.)
2145 match self
2146 .force_final_synthesis(
2147 &mut ctx,
2148 world,
2149 &mut total_usage,
2150 MONOTONY_SYNTHESIS_PROMPT,
2151 )
2152 .await
2153 {
2154 Synthesis::Text(Some(t)) => last_text = Some(t),
2155 Synthesis::Text(None) => {}
2156 // Stop means stop: the synthesis is a model call like
2157 // any other, and the run owes the caller a `Cancelled`
2158 // rather than the `Stuck` it was on its way to.
2159 Synthesis::Cancelled => {
2160 tracing::info!(iter, "run cancelled during final synthesis");
2161 self.hooks.fire(&Event::Cancelled, world);
2162 self.hooks.fire(&Event::SessionEnd, world);
2163 return Ok(Outcome::Cancelled {
2164 iters: iter + 1,
2165 last_text,
2166 tools_called,
2167 usage: total_usage,
2168 });
2169 }
2170 }
2171
2172 self.hooks.fire(&Event::SessionEnd, world);
2173 return Ok(Outcome::Stuck {
2174 reason,
2175 repeated: monotone_rounds,
2176 iters: iter + 1,
2177 last_text,
2178 tools_called,
2179 usage: total_usage,
2180 });
2181 }
2182
2183 if monotone_rounds == self.monotony.nudge_after {
2184 let name = monotone_tool.clone().unwrap_or_default();
2185 tracing::warn!(
2186 tool = %name,
2187 rounds = monotone_rounds,
2188 "monotony: nudging model to change approach"
2189 );
2190 ctx.push_feedback(vec![harness_core::Signal {
2191 severity: harness_core::Severity::Warn,
2192 origin: "monotony-detector".into(),
2193 message: format!(
2194 "You have called `{name}` for {monotone_rounds} rounds in a row \
2195 and used no other tool. A different argument each time is not \
2196 the same thing as making progress."
2197 ),
2198 agent_hint: Some(
2199 "If you are guessing at arguments to see which one lands, stop: \
2200 report what you did find and say plainly what you could not \
2201 determine. Otherwise reach for a different tool, or give your \
2202 final answer with no tool call."
2203 .into(),
2204 ),
2205 auto_fix: None,
2206 location: None,
2207 }]);
2208 }
2209 }
2210 }
2211 // ── Budget exhausted ─────────────────────────────────────────
2212 // Force a final synthesis pass with tools DISABLED. Otherwise the
2213 // model often spins on tool calls right up to the budget cap and
2214 // never emits a text conclusion, leaving the caller with nothing
2215 // but `last_text` from some earlier intermediate turn (or None).
2216 //
2217 // The synthesis call is "free" — it costs one extra model call
2218 // beyond max_iters but doesn't count toward `iters`. The result
2219 // lands in `last_text` so callers display it as the answer.
2220 //
2221 // Signal to any observer (LiveProgressHook, SessionRecorder, custom
2222 // hooks) that we've used 100% of the budget and are about to force
2223 // synthesis. Pre-existing `BudgetWarning` event was unused; this is
2224 // its natural home.
2225 self.hooks.fire(&Event::BudgetWarning { ratio: 1.0 }, world);
2226 match self
2227 .force_final_synthesis(&mut ctx, world, &mut total_usage, BUDGET_SYNTHESIS_PROMPT)
2228 .await
2229 {
2230 Synthesis::Text(Some(t)) => last_text = Some(t),
2231 Synthesis::Text(None) => {}
2232 // A cancel that lands here has to win: this is the last model call
2233 // of the run, and it is made after the loop's last cancel check.
2234 // No learning review either — it is one more model call, and the
2235 // other cancel exits all skip it.
2236 Synthesis::Cancelled => {
2237 tracing::info!(iter = iters_done, "run cancelled during final synthesis");
2238 self.hooks.fire(&Event::Cancelled, world);
2239 self.hooks.fire(&Event::SessionEnd, world);
2240 return Ok(Outcome::Cancelled {
2241 iters: iters_done,
2242 last_text,
2243 tools_called,
2244 usage: total_usage,
2245 });
2246 }
2247 }
2248
2249 self.hooks.fire(&Event::SessionEnd, world);
2250 self.run_learning_review(&ctx, world, tools_called).await;
2251 Ok(Outcome::BudgetExhausted {
2252 iters: iters_done,
2253 last_text,
2254 tools_called,
2255 usage: total_usage,
2256 deadline_reached,
2257 })
2258 }
2259
2260 /// One model call, racing the run's cancellation token.
2261 ///
2262 /// `None` means the token fired first. Dropping the un-awaited future is
2263 /// what cancels the underlying HTTP request (reqwest aborts on drop) or
2264 /// the SSE stream, so the model stops generating rather than finishing
2265 /// into a void. `biased` so a token already cancelled wins a tie.
2266 ///
2267 /// `partial` collects streamed text so a cancel can hand it to the outcome.
2268 async fn model_step(
2269 &self,
2270 ctx: &Context,
2271 world: &mut World,
2272 partial: &mut String,
2273 iter: u32,
2274 ) -> Result<Option<ModelOutput>, HarnessError> {
2275 let info = self.model.info();
2276 let span = chat_span(&info);
2277 let started = std::time::Instant::now();
2278 let r = async {
2279 tokio::select! {
2280 biased;
2281 _ = self.cancel.cancelled() => Ok(None),
2282 r = self.model_call(ctx, world, partial, iter) => r.map(Some),
2283 }
2284 }
2285 .instrument(span.clone())
2286 .await;
2287 let end = match &r {
2288 Ok(Some(out)) => Some(Ok(out)),
2289 Ok(None) => None,
2290 Err(e) => Some(Err(e.to_string())),
2291 };
2292 finish_chat(&span, &info, started, end);
2293 r
2294 }
2295
2296 /// One model request: streamed unless the loop was opted out with
2297 /// `with_streaming(false)`.
2298 async fn model_call(
2299 &self,
2300 ctx: &Context,
2301 world: &mut World,
2302 partial: &mut String,
2303 iter: u32,
2304 ) -> Result<ModelOutput, HarnessError> {
2305 if self.streaming {
2306 self.complete_via_stream(ctx, world, partial, iter).await
2307 } else {
2308 self.model
2309 .complete(ctx)
2310 .await
2311 .map_err(harness_core::HarnessError::Model)
2312 }
2313 }
2314
2315 /// Drive `Model::stream()` and assemble the result into a `ModelOutput`,
2316 /// firing `Event::ModelTokenDelta` for each text fragment along the way.
2317 ///
2318 /// Adapters without a real `stream()` fall back to the default trait impl,
2319 /// which runs `complete()` and emits the whole reply as a single delta, so
2320 /// streaming is safe regardless of which provider the user picked. The
2321 /// assembly itself is [`harness_core::collect_stream_observed`], shared
2322 /// with the adapters' own `complete()`.
2323 ///
2324 /// While the stream runs, a `model_progress` custom event fires about every
2325 /// 5s and a `Heartbeat` about every 30s, both driven by arriving deltas.
2326 ///
2327 /// `sink` is where the text accumulates, owned by the caller so that a
2328 /// cancel — which drops this future mid-stream — does not drop the chunks
2329 /// already delivered along with it. A completed stream leaves it empty.
2330 ///
2331 /// Tool-call deltas need no such rescue: they fire no hook, and no dispatch
2332 /// happens until after the output is pushed, so a cancel mid-tool-call
2333 /// loses nothing anyone observed.
2334 async fn complete_via_stream(
2335 &self,
2336 ctx: &Context,
2337 world: &mut World,
2338 sink: &mut String,
2339 iter: u32,
2340 ) -> Result<ModelOutput, HarnessError> {
2341 let stream = self
2342 .model
2343 .stream(ctx)
2344 .await
2345 .map_err(harness_core::HarnessError::Model)?;
2346 let started = std::time::Instant::now();
2347 let mut last_progress = started;
2348 let mut last_heartbeat = started;
2349 let (mut chars, mut deltas) = (0usize, 0usize);
2350 let hooks = &self.hooks;
2351 let out = harness_core::collect_stream_observed(stream, |delta| {
2352 deltas += 1;
2353 match delta {
2354 ModelDelta::Text(t) if !t.is_empty() => {
2355 chars += t.chars().count();
2356 hooks.fire(&Event::ModelTokenDelta { text: t }, world);
2357 sink.push_str(t);
2358 }
2359 ModelDelta::Reasoning(t) => chars += t.chars().count(),
2360 ModelDelta::ToolCallArgs { partial_json, .. } => {
2361 chars += partial_json.chars().count()
2362 }
2363 _ => {}
2364 }
2365 // Driven by arriving deltas, so a heartbeat means "bytes are
2366 // still flowing", and its absence in a trace means stuck.
2367 let now = std::time::Instant::now();
2368 if now.duration_since(last_heartbeat) >= MODEL_HEARTBEAT_EVERY {
2369 last_heartbeat = now;
2370 hooks.fire(&Event::Heartbeat { iter }, world);
2371 }
2372 if now.duration_since(last_progress) >= MODEL_PROGRESS_EVERY {
2373 last_progress = now;
2374 let elapsed = now.duration_since(started).as_secs_f64();
2375 let data = serde_json::json!({
2376 "elapsed_s": elapsed,
2377 "chars": chars,
2378 "deltas": deltas,
2379 "chars_per_s": if elapsed > 0.0 { chars as f64 / elapsed } else { 0.0 },
2380 });
2381 hooks.fire(
2382 &Event::Custom {
2383 name: "model_progress",
2384 data: &data,
2385 },
2386 world,
2387 );
2388 }
2389 })
2390 .await
2391 .map_err(harness_core::HarnessError::Model)?;
2392 // Completed: the output now owns the text; only a cancel needs the sink.
2393 sink.clear();
2394 Ok(out)
2395 }
2396
2397 /// One tool call, under the per-call deadline and the run's cancellation
2398 /// token. A timeout becomes an error *result* — the model sees it and routes
2399 /// around it — never a hung run. A cancel drops the tool's future unpolled
2400 /// and returns a result the loop discards before it reaches history, so the
2401 /// model never sees that one at all. Errors are folded the same way: the
2402 /// loop's contract is that a tool call always produces a result turn.
2403 ///
2404 /// Dropping the future stops *this* side of the work. What it stops on the
2405 /// other side depends on the tool: an HTTP request aborts, a `spawn_blocking`
2406 /// file write runs to completion, and a child process is only killed if its
2407 /// runner asked for `kill_on_drop`.
2408 ///
2409 /// Runs in an `execute_tool` span, so parallel calls show up as overlapping
2410 /// bars in a trace and a failed one is marked as an error.
2411 async fn dispatch_bounded(&self, action: &Action, world: &mut World) -> Dispatched {
2412 let span = tracing::info_span!(
2413 target: "harness.telemetry",
2414 "execute_tool",
2415 "otel.name" = %format!("execute_tool {}", action.tool),
2416 "otel.status_code" = Empty,
2417 "otel.status_message" = Empty,
2418 "gen_ai.operation.name" = "execute_tool",
2419 "gen_ai.tool.name" = %action.tool,
2420 "gen_ai.tool.call.id" = %action.call_id,
2421 ok = Empty,
2422 );
2423 let clock = world.clock.clone();
2424 let started_ms = clock.now_ms();
2425 let started = std::time::Instant::now();
2426 let r = self
2427 .dispatch_bounded_in_span(action, world)
2428 .instrument(span.clone())
2429 .await;
2430 let finished_ms = clock.now_ms();
2431 meters::tool_call(&action.tool, r.ok, started.elapsed());
2432 span.record("ok", r.ok);
2433 if !r.ok {
2434 span.record("otel.status_code", "error");
2435 if let Some(msg) = r.content.get("error").and_then(|v| v.as_str()) {
2436 span.record("otel.status_message", msg);
2437 }
2438 }
2439 Dispatched {
2440 result: r,
2441 started_ms,
2442 finished_ms,
2443 }
2444 }
2445
2446 async fn dispatch_bounded_in_span(&self, action: &Action, world: &mut World) -> ToolResult {
2447 let fut = self.tools.dispatch(action, world);
2448 let bounded = async {
2449 match self.tool_timeout {
2450 Some(deadline) => match tokio::time::timeout(deadline, fut).await {
2451 Ok(r) => r,
2452 Err(_) => {
2453 tracing::warn!(
2454 target: "harness.telemetry",
2455 event = "tool.deadline",
2456 "gen_ai.tool.name" = %action.tool,
2457 seconds = deadline.as_secs(),
2458 );
2459 Ok(ToolResult {
2460 ok: false,
2461 content: serde_json::json!({
2462 "error": format!(
2463 "tool call exceeded its {}s deadline and was cancelled; \
2464 the operation may be too broad — narrow it or try a \
2465 different approach",
2466 deadline.as_secs()
2467 ),
2468 "timeout": true,
2469 }),
2470 trace: None,
2471 })
2472 }
2473 },
2474 None => fut.await,
2475 }
2476 };
2477 // `biased` so that a token already cancelled wins even when the tool
2478 // future is also ready — the caller said stop, and a result produced
2479 // after that would be work the run is about to throw away anyway.
2480 let dispatched = tokio::select! {
2481 biased;
2482 _ = self.cancel.cancelled() => {
2483 tracing::info!("gen_ai.tool.name" = %action.tool, "tool call dropped: run cancelled");
2484 return ToolResult {
2485 ok: false,
2486 content: serde_json::json!({"error": "run cancelled", "cancelled": true}),
2487 trace: None,
2488 };
2489 }
2490 r = bounded => r,
2491 };
2492 dispatched.unwrap_or_else(|e| ToolResult {
2493 ok: false,
2494 content: serde_json::json!({"error": e.to_string()}),
2495 trace: None,
2496 })
2497 }
2498
2499 /// What a tool result contributes to the context: repeat suppression first,
2500 /// then the size ceiling. A repeat that is also oversized collapses to the
2501 /// pointer rather than to a truncated copy of what the model already holds.
2502 fn shape_result(
2503 &self,
2504 action: &Action,
2505 result: &ToolResult,
2506 answered: &mut std::collections::HashSet<String>,
2507 root: &std::path::Path,
2508 ) -> serde_json::Value {
2509 if !(self.tool_results.dedupe_repeats && result.ok) {
2510 return self.cap_result(action, &result.content, root);
2511 }
2512 match self.tools.risk(&action.tool) {
2513 Some(harness_core::ToolRisk::ReadOnly) => {
2514 let fp = format!("{}({})", action.tool, action.args);
2515 if answered.contains(&fp) {
2516 tracing::info!(
2517 target: "harness.telemetry",
2518 event = "tool.result.repeat",
2519 "gen_ai.tool.name" = %action.tool,
2520 );
2521 serde_json::json!({
2522 "repeat_of_earlier_call": true,
2523 "tool": action.tool,
2524 "note": "You already made this exact call in this run and nothing has \
2525 changed the workspace since. The earlier result above still \
2526 stands — use it rather than asking again.",
2527 })
2528 } else {
2529 answered.insert(fp);
2530 self.cap_result(action, &result.content, root)
2531 }
2532 }
2533 // A write invalidates every earlier read.
2534 _ => {
2535 answered.clear();
2536 self.cap_result(action, &result.content, root)
2537 }
2538 }
2539 }
2540
2541 /// Enforce [`ToolResultPolicy`] on one result before it reaches the context.
2542 ///
2543 /// Over the ceiling, the guard prefers to *spill*: full payload to a file
2544 /// inside the workspace, bounded preview plus the path inline — nothing is
2545 /// lost, and the model retrieves slices with the file tools it already has.
2546 /// Only when spilling is off (or the write fails) does it fall back to
2547 /// destructive truncation: a byte-level cut handed back as a marker object
2548 /// rather than mangled JSON, saying how much was dropped and what to do
2549 /// instead.
2550 fn cap_result(
2551 &self,
2552 action: &Action,
2553 content: &serde_json::Value,
2554 root: &std::path::Path,
2555 ) -> serde_json::Value {
2556 let Some(max) = self.tool_results.max_bytes else {
2557 return content.clone();
2558 };
2559 let serialized = content.to_string();
2560 if serialized.len() <= max {
2561 return content.clone();
2562 }
2563 if self.tool_results.spill
2564 && let Some(marker) = spill_oversized(action, content, &serialized, root)
2565 {
2566 return marker;
2567 }
2568 // Cut on a char boundary so the kept head is valid UTF-8.
2569 let mut end = max;
2570 while end > 0 && !serialized.is_char_boundary(end) {
2571 end -= 1;
2572 }
2573 tracing::warn!(
2574 target: "harness.telemetry",
2575 event = "tool.result.truncated",
2576 "gen_ai.tool.name" = %action.tool,
2577 bytes = serialized.len(),
2578 max_bytes = max,
2579 );
2580 serde_json::json!({
2581 "truncated": true,
2582 "tool": action.tool,
2583 "bytes_total": serialized.len(),
2584 "bytes_kept": end,
2585 "head": serialized[..end],
2586 "note": format!(
2587 "This result was {} bytes and was cut to {} to protect the context window. \
2588 Do not ask for it again unchanged — narrow it: request a smaller range, \
2589 a filter, or a specific field.",
2590 serialized.len(), end
2591 ),
2592 })
2593 }
2594
2595 async fn recall_append(&self, owner: &str, session: &str, msg: harness_core::RecallMessage) {
2596 if let Some(store) = &self.recall
2597 && let Err(e) = store.append(owner, session, &msg).await
2598 {
2599 tracing::warn!(error = %e, "recall append failed");
2600 }
2601 }
2602
2603 /// Best-effort post-session review. Never affects the finished run.
2604 async fn run_learning_review(&self, ctx: &Context, world: &mut World, tools_called: u32) {
2605 let Some(cfg) = &self.learning else { return };
2606 if tools_called < cfg.nudge_interval {
2607 return;
2608 }
2609 let transcript = crate::render_transcript(&ctx.history, 12_000);
2610 let task = harness_core::Task {
2611 description: format!(
2612 "{}\n\n## Conversation transcript\n{}",
2613 cfg.review_prompt, transcript
2614 ),
2615 source: None,
2616 deadline: None,
2617 };
2618 let mut spec =
2619 crate::SubagentSpec::new("learning-review", task).with_max_iters(cfg.max_iters);
2620 for t in &cfg.tools {
2621 spec = spec.with_tool(t.clone());
2622 }
2623 let sub = crate::Subagent::new(harness_core::DynModel(cfg.review_model.clone()), spec);
2624 // Box::pin breaks the recursive async-future cycle: AgentLoop<M> →
2625 // run_learning_review → Subagent<DynModel>::run →
2626 // AgentLoop<Arc<dyn Model>>::run_built_context. Without pinning the
2627 // compiler rejects the infinite-sized future.
2628 if let Err(e) = Box::pin(sub.run(world)).await {
2629 tracing::warn!(error = %e, "learning review failed");
2630 }
2631 }
2632
2633 /// One final model call with tools removed, asking it to write the
2634 /// best-effort conclusion from whatever it has already gathered.
2635 ///
2636 /// Errors from the model are swallowed — observability is best-effort
2637 /// here, and a transport blip during synthesis should not turn a
2638 /// near-complete run into a hard failure.
2639 ///
2640 /// The call is raced against the run's cancellation token like every
2641 /// other model call: a cancel before or during it produces no text,
2642 /// leaves the context as it was, and is reported as
2643 /// [`Synthesis::Cancelled`] so the caller can return `Outcome::Cancelled`
2644 /// instead of pretending the run ended on its own terms.
2645 ///
2646 /// `prompt` says *why* the loop stopped granting tool calls. Budget
2647 /// exhaustion is not the only way a run can end with the model still
2648 /// reaching for tools — a detected loop is another — and the model writes a
2649 /// better final answer when it is told which one happened.
2650 async fn force_final_synthesis(
2651 &self,
2652 ctx: &mut Context,
2653 world: &mut World,
2654 total_usage: &mut harness_core::Usage,
2655 prompt: &str,
2656 ) -> Synthesis {
2657 // Snapshot + clear tool schemas so the model has no choice but text.
2658 let saved_tools = std::mem::take(&mut ctx.tools);
2659 ctx.history.push(Turn {
2660 role: TurnRole::User,
2661 blocks: vec![Block::Text(prompt.into())],
2662 });
2663
2664 self.hooks.fire(&Event::PreModel { ctx }, world);
2665 // `biased` with the token first means an already-cancelled token wins
2666 // the race outright, so no separate `is_cancelled()` pre-check is
2667 // needed here — the select is the check.
2668 let info = self.model.info();
2669 let span = chat_span(&info);
2670 let started = std::time::Instant::now();
2671 let mut partial = String::new();
2672 let iter = ctx.policy.max_iters;
2673 let result = tokio::select! {
2674 biased;
2675 _ = self.cancel.cancelled() => None,
2676 r = self
2677 .model_call(ctx, world, &mut partial, iter)
2678 .instrument(span.clone()) => Some(r),
2679 };
2680 finish_chat(
2681 &span,
2682 &info,
2683 started,
2684 result
2685 .as_ref()
2686 .map(|r| r.as_ref().map_err(|e| e.to_string())),
2687 );
2688 ctx.tools = saved_tools;
2689
2690 let Some(result) = result else {
2691 // Drop the synthesis prompt pushed above. Nothing answered it, and
2692 // leaving it would hand the next reader of this context a dangling
2693 // user turn.
2694 ctx.history.pop();
2695 return Synthesis::Cancelled;
2696 };
2697
2698 match result {
2699 Ok(out) => {
2700 self.hooks.fire(&Event::PostModel { out: &out }, world);
2701 total_usage.input_tokens += out.usage.input_tokens;
2702 total_usage.output_tokens += out.usage.output_tokens;
2703 total_usage.cached_input_tokens += out.usage.cached_input_tokens;
2704 total_usage.cache_write_input_tokens += out.usage.cache_write_input_tokens;
2705 ctx.push_model_output(&out);
2706 Synthesis::Text(out.text)
2707 }
2708 Err(_) => Synthesis::Text(None),
2709 }
2710 }
2711}
2712
2713/// What the forced final synthesis produced.
2714enum Synthesis {
2715 /// The model answered (or errored — errors are swallowed, see the fn doc).
2716 Text(Option<String>),
2717 /// The run's token fired before or during the call; no text was produced
2718 /// and nothing was pushed to the context.
2719 Cancelled,
2720}
2721
2722/// A persistent multi-turn conversation over one [`AgentLoop`].
2723///
2724/// Holds the append-only history and, on each [`turn`](Session::turn), re-runs
2725/// the loop against a **stable prefix** (system + name-sorted tool schemas).
2726/// That byte-stable prefix is what lets a provider's prefix cache hit across
2727/// turns — the difference between paying full price to re-read the same context
2728/// every round and paying ~10% for the cached bytes (DeepSeek).
2729pub struct Session<'a, M: Model> {
2730 loop_: &'a AgentLoop<M>,
2731 history: Vec<Turn>,
2732 max_iters: u32,
2733}
2734
2735impl<'a, M: Model> Session<'a, M> {
2736 pub fn with_max_iters(mut self, n: u32) -> Self {
2737 self.max_iters = n;
2738 self
2739 }
2740 /// Preload prior turns (e.g. resumed from disk).
2741 pub fn with_seed(mut self, seed: Vec<Turn>) -> Self {
2742 self.history = seed;
2743 self
2744 }
2745 /// The accumulated conversation so far.
2746 pub fn history(&self) -> &[Turn] {
2747 &self.history
2748 }
2749 /// Start over (branch): drop the accumulated turns.
2750 pub fn reset(&mut self) {
2751 self.history.clear();
2752 }
2753
2754 /// Send one user message. Runs the ReAct loop against the accumulated
2755 /// history, then appends this user turn + the assistant reply so the next
2756 /// turn extends the same cached prefix.
2757 pub async fn turn(
2758 &mut self,
2759 message: impl Into<String>,
2760 world: &mut World,
2761 ) -> Result<Outcome, HarnessError> {
2762 let message = message.into();
2763 let task = Task {
2764 description: message.clone(),
2765 source: None,
2766 deadline: None,
2767 };
2768 let outcome = self
2769 .loop_
2770 .run_with_seed_history(task, self.history.clone(), world, self.max_iters)
2771 .await?;
2772 let reply = match &outcome {
2773 Outcome::Done { text, .. } => text.clone().unwrap_or_default(),
2774 Outcome::BudgetExhausted { last_text, .. }
2775 | Outcome::Stuck { last_text, .. }
2776 | Outcome::Cancelled { last_text, .. } => last_text.clone().unwrap_or_default(),
2777 };
2778 self.history.push(Turn {
2779 role: TurnRole::User,
2780 blocks: vec![Block::Text(message)],
2781 });
2782 self.history.push(Turn {
2783 role: TurnRole::Assistant,
2784 blocks: vec![Block::Text(reply)],
2785 });
2786 Ok(outcome)
2787 }
2788}
2789
2790/// Audit #7: default safelist for `FixPatch::RunCommand`.
2791///
2792/// Sensors emitting `RunCommand` patches would otherwise be a silent
2793/// arbitrary-code-execution channel. We restrict the *program* by name to a
2794/// short list of well-known, side-effect-bounded formatters/fixers. Anything
2795/// else returns false and the patch is rejected (write your own `PreAutoFix`
2796/// hook returning `HookOutcome::Allow` to widen the policy).
2797///
2798/// `ReplaceFile` and `UnifiedDiff` are not restricted here — they only touch
2799/// files inside the workspace and are covered by the symlink-safe path
2800/// resolution in `harness-tools-fs`.
2801pub fn is_default_safe_fix(patch: &harness_core::FixPatch) -> bool {
2802 use harness_core::FixPatch;
2803 match patch {
2804 FixPatch::ReplaceFile { .. } | FixPatch::UnifiedDiff { .. } => true,
2805 FixPatch::RunCommand { program, args, .. } => match program.as_str() {
2806 // Cargo subcommands proven side-effect-bounded.
2807 "cargo" => matches!(
2808 args.first().map(String::as_str),
2809 Some("fmt" | "clippy" | "fix"),
2810 ),
2811 "rustfmt" | "gofmt" | "prettier" | "ruff" | "black" => true,
2812 _ => false,
2813 },
2814 // Future FixPatch variants: deny by default — review and add to the list above.
2815 _ => false,
2816 }
2817}
2818
2819/// Monotonic counter for `.harness-patch-*.diff` temp filenames — millisecond
2820/// resolution alone collides under parallel agent runs.
2821static PATCH_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
2822
2823/// Monotonic counter for fallback recall session ids (no `uuid` dep).
2824static RECALL_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
2825
2826/// Apply auto-fix patches; return short descriptions of those that succeeded.
2827///
2828/// Made `pub` (was `pub(crate)`) so integration tests can call it directly.
2829pub async fn apply_patches(patches: &[harness_core::FixPatch], world: &mut World) -> Vec<String> {
2830 use harness_core::FixPatch;
2831 let mut applied = Vec::new();
2832 for p in patches {
2833 match p {
2834 FixPatch::ReplaceFile { path, content } => {
2835 let abs = world.repo.root.join(path);
2836 if let Some(parent) = abs.parent() {
2837 let _ = tokio::fs::create_dir_all(parent).await;
2838 }
2839 if tokio::fs::write(&abs, content).await.is_ok() {
2840 applied.push(format!("replaced {}", path.display()));
2841 }
2842 }
2843 FixPatch::UnifiedDiff { diff } => {
2844 if try_apply_diff(world, diff).await {
2845 applied.push("unified diff applied".into());
2846 }
2847 }
2848 FixPatch::RunCommand { program, args, cwd } => {
2849 let cwd_ref = cwd.as_deref().unwrap_or(world.repo.root.as_path());
2850 let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
2851 if let Ok(out) = world.runner.exec(program, &args_ref, Some(cwd_ref)).await
2852 && out.status == 0
2853 {
2854 applied.push(format!("ran `{program} {}`", args.join(" ")));
2855 }
2856 }
2857 // FixPatch is `#[non_exhaustive]`; unknown variants are skipped.
2858 _ => tracing::warn!("apply_patches: unknown FixPatch variant — skipped"),
2859 }
2860 }
2861 applied
2862}
2863
2864/// Write `diff` to a unique temp file and try `patch -p1` first, then `-p0`.
2865/// Returns whether either succeeded. The `-p1`-then-`-p0` order matches the
2866/// reality that most agent-emitted diffs are git-style (need `-p1`) but some
2867/// hand-rolled diffs use repo-relative paths (need `-p0`).
2868async fn try_apply_diff(world: &mut World, diff: &str) -> bool {
2869 use std::sync::atomic::Ordering;
2870 use tokio::io::AsyncWriteExt;
2871
2872 let seq = PATCH_SEQ.fetch_add(1, Ordering::SeqCst);
2873 let pid = std::process::id();
2874 let now = world.clock.now_ms();
2875 let tmp = world
2876 .repo
2877 .root
2878 .join(format!(".harness-patch-{pid}-{now}-{seq}.diff"));
2879
2880 let mut f = match tokio::fs::File::create(&tmp).await {
2881 Ok(f) => f,
2882 Err(e) => {
2883 tracing::warn!(error=%e, path=%tmp.display(), "could not create patch tempfile");
2884 return false;
2885 }
2886 };
2887 if let Err(e) = f.write_all(diff.as_bytes()).await {
2888 tracing::warn!(error=%e, "could not write patch tempfile");
2889 let _ = tokio::fs::remove_file(&tmp).await;
2890 return false;
2891 }
2892 drop(f);
2893
2894 let tmp_str = tmp.to_string_lossy().to_string();
2895 let mut applied = false;
2896 for strip in ["-p1", "-p0"] {
2897 match world
2898 .runner
2899 .exec(
2900 "patch",
2901 &[strip, "--silent", "-i", tmp_str.as_str()],
2902 Some(world.repo.root.as_path()),
2903 )
2904 .await
2905 {
2906 Ok(out) if out.status == 0 => {
2907 tracing::info!(strip, "patch applied");
2908 applied = true;
2909 break;
2910 }
2911 Ok(out) => {
2912 tracing::debug!(strip, stderr=%out.stderr, "patch failed; trying next strip level");
2913 }
2914 Err(e) => {
2915 tracing::warn!(error=%e, "patch command not available");
2916 break; // patch tool missing — no point trying other strip
2917 }
2918 }
2919 }
2920 let _ = tokio::fs::remove_file(&tmp).await;
2921 applied
2922}