From 343f2a8a6148b8004d6f4d74fb779b4f30c9686a Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 03:11:22 +0100 Subject: M1-M5: sessions, six tools, approvals, prompt, Ctrl-C, target gate - append-only JSONL sessions with --continue/--session; reasoning stored, stripped on replay - read/write/edit/grep/glob + bash with timeout and capped output - diff-preview approval for the first edit of a session, per-call approval for bash - system prompt frozen per session with AGENTS.md snapshot and change warning - SIGINT aborts the turn, second Ctrl-C exits - glibc gate ignores weak symbols; verified in linux/386 debian:12 - acceptance test (calculator in C) passes on both x86-64 and i386 --- src/provider.rs | 64 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 23 deletions(-) (limited to 'src/provider.rs') diff --git a/src/provider.rs b/src/provider.rs index a2aed64..bc1bd99 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -44,7 +44,35 @@ pub struct Outcome { pub tool_calls: Vec, pub finish: Option, pub usage: Option, - pub opencode: Option, +} + +impl Outcome { + pub fn allowance(&self) -> Option { + self.usage + .as_ref()? + .get("opencode")? + .get("allowance")? + .as_f64() + } + + pub fn list_cost(&self) -> Option { + self.usage + .as_ref()? + .get("opencode")? + .get("list_cost")? + .as_f64() + } + + pub fn tokens(&self) -> (Option, Option) { + let usage = match &self.usage { + Some(u) => u, + None => return (None, None), + }; + ( + usage.get("prompt_tokens").and_then(|v| v.as_u64()), + usage.get("completion_tokens").and_then(|v| v.as_u64()), + ) + } } pub trait Sink { @@ -92,19 +120,22 @@ impl SseDecoder { pub fn chat( cfg: &Config, messages: &[Value], + tools: &[Value], cancel: &AtomicBool, sink: &mut dyn Sink, ) -> Result { - // serde_json::Value is ordered by key, not by insertion, so the serialised - // bytes are stable for the same input. That is what keeps the cache warm; - // do not switch this to a struct with fields in a different order, and do - // not add anything session-varying to the system message. - let body = serde_json::json!({ + // serde_json::Value orders objects by key, so the serialised bytes are + // stable for the same input. That is what keeps the provider's prompt cache + // warm. Do not add anything session-varying here. + let mut body = serde_json::json!({ "model": cfg.model, "messages": messages, "stream": true, "stream_options": { "include_usage": true }, }); + if !tools.is_empty() { + body["tools"] = Value::Array(tools.to_vec()); + } let bytes = serde_json::to_vec(&body).map_err(|e| Error::Transport(e.to_string()))?; let agent: ureq::Agent = ureq::Agent::config_builder() @@ -169,23 +200,15 @@ pub fn chat( } let chunk: Chunk = match serde_json::from_str(&data) { Ok(c) => c, - // A malformed frame is dropped rather than killing the turn; the - // stream is the only recovery path and it is still valid SSE. + // A malformed frame is dropped rather than killing the turn. Err(_) => continue, }; apply_chunk(&chunk, &mut outcome, sink); } } - if let Some(usage) = &outcome.usage { - if usage.is_null() { - outcome.usage = None; - } - } - if let Some(opencode) = &outcome.opencode { - if opencode.is_null() { - outcome.opencode = None; - } + if outcome.usage.as_ref().is_some_and(|u| u.is_null()) { + outcome.usage = None; } sink.done(&outcome, started.elapsed()); @@ -196,9 +219,6 @@ fn apply_chunk(chunk: &Chunk, outcome: &mut Outcome, sink: &mut dyn Sink) { if let Some(usage) = &chunk.usage { outcome.usage = Some(usage.clone()); } - if let Some(opencode) = &chunk.opencode { - outcome.opencode = Some(opencode.clone()); - } for choice in &chunk.choices { if let Some(r) = &choice.delta.reasoning_content { if !r.is_empty() { @@ -266,7 +286,7 @@ fn error_message(body: &str) -> String { truncate(body.trim(), 500) } -fn truncate(s: &str, max: usize) -> String { +pub fn truncate(s: &str, max: usize) -> String { match s.char_indices().nth(max) { Some((i, _)) => format!("{}…", &s[..i]), None => s.to_string(), @@ -279,8 +299,6 @@ struct Chunk { choices: Vec, #[serde(default)] usage: Option, - #[serde(default)] - opencode: Option, } #[derive(Debug, Deserialize)] -- cgit v1.2.3