Craft Anything: understand how any software works, and craft your own.
Rust
Count cached tokens in usage.json (#958)
SoldierPePe826d3f640bc2
diff --git a/crates/ai/src/agent.rs b/crates/ai/src/agent.rsindex 46537219..92cd2910 100644--- a/crates/ai/src/agent.rs+++ b/crates/ai/src/agent.rs@@ -470,7 +470,7 @@ impl AgentLoop {let backend = self.chain.get(self.current).map(|s| s.name.clone()).unwrap_or_default();self.emit(AgentEvent::TurnDone {backend,- input_tokens: msg_usage.input_tokens,+ input_tokens: msg_usage.input_tokens.saturating_add(msg_usage.cache_read_tokens).saturating_add(msg_usage.cache_write_tokens),output_tokens: msg_usage.output_tokens,cost_usd: msg_cost,cost_is_estimate: false,@@ -1243,7 +1243,8 @@ pub(crate) mod tests {assert!(seen[3].len() < seen[1].len() + 2);}assert!(- matches!(events.last(), Some(AgentEvent::TurnDone { input_tokens: 60, output_tokens: 19, cost_usd: Some(cost), .. }) if (cost - 0.00010125).abs() < 1e-12),+ // Cached tokens count as input (R5.4: Ollama 0.40 reports them, and usage.json left them out).+ matches!(events.last(), Some(AgentEvent::TurnDone { input_tokens: 70, output_tokens: 19, cost_usd: Some(cost), .. }) if (cost - 0.00010125).abs() < 1e-12),"{events:?}");{diff --git a/crates/ai/src/lib.rs b/crates/ai/src/lib.rsindex 259351ec..32d0a4d7 100644--- a/crates/ai/src/lib.rs+++ b/crates/ai/src/lib.rs@@ -266,6 +266,7 @@ pub enum AgentEvent {/// One assistant turn finished.TurnDone {backend: String,+ /// Every prompt token, cached ones included, as agent apps report them.input_tokens: u64,output_tokens: u64,cost_usd: Option<f64>,diff --git a/crates/ai/src/tests.rs b/crates/ai/src/tests.rsindex b8a9a58a..343405f3 100644--- a/crates/ai/src/tests.rs+++ b/crates/ai/src/tests.rs@@ -738,7 +738,8 @@ fn anthropic_streaming_tool_loop_through_the_engine() {assert!(ev.iter().any(|e| matches!(e, AgentEvent::ToolFinished { tool, ok: true, .. } if tool == "core_session_show")));assert!(ev.iter().any(|e| matches!(e, AgentEvent::ToolFinished { tool, ok: true, .. } if tool == "core_targets_list")));let AgentEvent::TurnDone { backend, input_tokens, output_tokens, cost_usd, .. } = ev.last().unwrap().clone() else { panic!() };- assert_eq!((backend.as_str(), input_tokens, output_tokens), ("Claude", 300, 50));+ // The 90 cached tokens count as input, as agent apps report them.+ assert_eq!((backend.as_str(), input_tokens, output_tokens), ("Claude", 390, 50));assert!(cost_usd.unwrap() > 0.0);let seen = seen.lock().unwrap();diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex f11146b6..bc3011b1 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -297,8 +297,12 @@ been run live.- [ ] The model reads file paths from the project folder and commands from `app/` as one base:it ran `cargo init app` and `cd app && cargo build` after writing `app/Cargo.toml`. Say thefolder in `build_command_run`'s result (or its description) so the two can't be mixed up-- [ ] `usage.json` after a local build says about 4,200 input tokens and 6.5 s of work, while- Ollama logged about six requests of 4,000-5,000 tokens each: find what it leaves out+- [x] `usage.json` after a local build says about 4,200 input tokens and 6.5 s of work, while+ Ollama logged about six requests of 4,000-5,000 tokens each: find what it leaves out.+ Cached tokens: Ollama 0.40 reports them apart (`cached_tokens`), and the built-in loop's+ `TurnDone` left them out while coding agents count them. It counts them now; a live run on+ Ollama 0.40.7 had recorded 5,323 input tokens of 75,014+ — claimed: SoldierPePe, branch bugfix/r5.4-usage-cached-tokens- [x] An assistant turn with no text sends an empty string, not `content: null`, which Ollamarefused as `invalid message content type: <nil>` (#403, #536)- [x] Understand a tool call a model writes as JSON in its text (#417; implementation `dfe9a4db2dcb3e27edbd67311cbb4202584e7af6`). The fallback protocol readsdiff --git a/docs/STATUS.md b/docs/STATUS.mdindex c77b8ae8..c5707f88 100644--- a/docs/STATUS.md+++ b/docs/STATUS.md@@ -68,7 +68,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi| Websites as targets | `web_site_open` keeps a private browser for the session and deletes its temporary profile when the browser closes or fails to start; every browser tool inspects that page; `web_site_capture` records screenshots at each width (desktop, tablet and phone), rendered HTML, design tokens (colours in hex, spacing, content widths), the CSS breakpoints, the layout at each width (page width, heading sizes, each section's padding, gap, content column and items per row), page health (script and console errors, failed requests) and an accessibility outline, layout outline, assets with font files and requests, links and form outlines. In Clean room the capture keeps only what a visitor sees (no HTML, CSS rules, scripts, requests or errors), and opening a site reports no load errors | Live runs on example.com and a local page with Chromium on Linux, including profile cleanup after success and failed startup; unit tests. Brave on macOS was not run || Electron window comparisons | `app_graphs_compare` retains every unassigned window and every construction with a reused local binding. Windows are paired by module, binding and occurrence, so inserting comments does not hide preference changes. Colliding display labels receive a separate `#N` suffix without changing literal binding names or occurrence indices | Synthetic applications parsed through the ordinary analyzer exercise anonymous and reused bindings, shifted source lines, unequal counts, literal names resembling occurrence or collision suffixes, and stable labels when only one window changes. Pairing remains positional within each binding group; reordered constructions and real application releases were not checked for this correction || Rebuild workflow (Study → Build → Check → Refine) | `crates/ai/src/rebuild/` (kinds, checks and stacks are pluggable: docs/extending.md). Three kinds: website, app, game (a project whose stack is a game engine is always a game). The request decides how to build: a language, framework, engine or platform it names wins over the kind's default stack (Rust + egui for apps, Unity for games, the original's for websites), and the build check, the recorded stack and Play follow what was built (an Electron app opens in its own Electron; a plain static site has nothing to build and passes the build check as it is). Websites: Study also captures up to 4 other pages of the site; menus are opened on a computer and on a phone; the check serves the build on loopback and compares every page at 1440, 768 and 390 px (likeness = mean of layout and perceptual detail, SSIM, averaged over pages, leaving out the parts of the original that change by themselves, found by photographing it three times; the note adds text, page structure, the colours, font, text sizes and spacing the build misses, and per width a page that scrolls sideways, heading sizes and sections whose padding, items per row or content column differ, the errors, failed requests and accessibility gaps the build adds, and the opened menus and pop-ups). A stack or library named in the request (Next.js, shadcn/ui, Tailwind CSS…, `stacks.toml`) is what the builder uses; with none named, a website's stack is the builder's choice (told to match what the original is built with) and the stack it used is recorded from app/package.json. The builder leads: it studies, asks the tools follow-up questions, records what it can't find out as unknowns (`core_unknown_add`; the run reports the open ones), and tests its own build mid-turn with `rebuild_check` (the same checks, for every kind); a check with anything it couldn't compare, or any claim that fails (a website page that scrolls sideways, logs new errors, fails to load a file, lacks a landmark, or a menu that doesn't open; for games each self-test step, for Deep-mode sheets each original number in the code, for console apps the same exit and output as the original's runs), isn't good enough; apps and games by whether they build. Up to 6 refinement rounds, or until a website reaches 90% likeness. A turn cut off by a time limit, crash or dropped connection resumes (up to 3 times); builder turns may run 24 h. A project copied from another computer (or moved to another folder) keeps building with Claude Code: the project keeps a copy of the builder's session (`agent-session.jsonl`) after every turn and puts it back where Claude Code finds it on a computer without it, so the same conversation carries on (#451); when the session still isn't there, a new one starts from the brief, what's in app/ and the kept requests and replies (or the last summary), and questions about the build put the kept session back too, else are answered from the files and forget the missing session (#452). Live runs with Claude Code 2.1.316 on one Mac (2026-11-02), a second computer stood in for by a separate signed-in `CLAUDE_CONFIG_DIR`: a copied project's session was put back and resumed (the builder recalled its first reply from memory); a kept copy of another session was refused and the new session used the handoff; a real project synced from Windows whose session was missing started afresh and reached Ready. Not yet run between two real computers | Unit tests for prompts, stack choice, site detection, feed folding; live runs reached Build and decompiling (below) |-| Context-summary usage | Successful API context summaries contribute to the message totals consumed by build usage and receive a `model_turn` journal entry with `purpose: context_summary`. Cached-token pricing is retained; an unknown summary price makes the total unknown, and a failed call adds no invented usage | Scripted-model tests exercise the real `ApiConversation`, transcript resumption, exact input/output and cache-price totals, journal entries, empty summaries, local zero cost, unknown prices and failed summaries. No live provider or end-to-end rebuild was used for this correction |+| Context-summary usage | Successful API context summaries contribute to the message totals consumed by build usage and receive a `model_turn` journal entry with `purpose: context_summary`. Input tokens include cached ones, as coding agents report them (Ollama 0.40 sends them apart). Cached-token pricing is retained; an unknown summary price makes the total unknown, and a failed call adds no invented usage | Scripted-model tests exercise the real `ApiConversation`, transcript resumption, exact input/output and cache-price totals, journal entries, empty summaries, local zero cost, unknown prices and failed summaries. No live provider or end-to-end rebuild was used for this correction || Local model builds | Not yet: Ollama 0.40.7 with `qwen2.5-coder:14b` (32k context) writes its calls as JSON text, which run whole or after a sentence; by check 3 its project compiles, but `main.rs` is still cargo's "Hello, world!", so the screenshot check fails it at the round limit (R5.4). A lean local builder's prompt asks for no memory tools it isn't offered, and a project one folder down is named in the check | Four live CLI builds of "a scientific calculator" on Windows (RTX 5090), 2026-11-04, one fix added per run || Backend context budgets | `context_tokens` in `[[ai.backends]]` controls when an API conversation is trimmed and summarised. Zero or omission retains local/cloud defaults; a nonzero conversation override takes priority. Before each attempt, including a fallback, fitting adds estimated history growth since the last successful request to its reported input size. Trimming resets that reference, and successful requests refresh it; summary usage remains in message totals | Regressions cover settings and model construction, defaults/overrides, smaller fallback budgets, large-budget arithmetic and new tool output growing beyond a fallback budget even when the history estimate alone is below the reported input. The latest tool results are preserved, and a short follow-up does not count already-sent history twice. This does not set server context parameters or guarantee that a complete request fits; no live provider was used || Games | Engine detection for 12 engines (Unreal, Unity, Godot, Source, GameMaker, RPG Maker, Ren'Py, MonoGame, LÖVE, CryEngine, Cocos, Cube) with versions. Unreal and Godot games are rebuilt in the same engine, others in Unity; installed engines are found, Godot installs itself, others get guided installs. Picking an inner folder (Engine, Binaries, Paks…) finds the game's folder. Just look inside a game shows what it's made of (`core_target_triage` on its folder: engine and version, how its code ships, the libraries its Unity assembly names give away, its own code, scenes and size) and opens it in the chat to ask about it. The builder keeps `app/MECHANICS.md` and its structured twin `app/mechanics.json` (checked after each build), in Deep mode only, may reuse the original's readable files for private use (never decrypting; generated pictures and 3D models replace its art when the person turns them on; Clean room copies none of its files), lists them in `CREDITS.md` (checked, with the private-use line in `README.md`); a game is ready only when its `--self-test` shows the player moving, acting and the HUD drawn, and the result can be started from the project page | Unit tests; live runs on Windows |diff --git a/docs/pitfalls.md b/docs/pitfalls.mdindex 19eb14ab..74eba8cb 100644--- a/docs/pitfalls.md+++ b/docs/pitfalls.md@@ -559,6 +559,11 @@ run, while the round numbers carried over, so "Try again" bought six more checksworked reached check 40 and 49 (#736). A limit that must hold across runs is a count saved with the project, andit's reset only by a new request. Test it with several runs in a row, not one.+**One field, two meanings.** `TurnDone`'s `input_tokens` included cached tokens from every coding agent but not+from the built-in loop, which keeps them apart as Anthropic reports them. Once Ollama 0.40 reported its cache, a+local build's `usage.json` said 5,323 input tokens for 75,014. When several producers fill one field, say on the+field what it counts, and test each producer with the case where they differ (here, a cached prompt).+**Docs that drift.** `STATUS.md` described a branch 25 commits old and the tool count was stale.Update STATUS, the roadmap and generated docs (`cargo xtask tools`, `cargo xtask schemas`) in thesame PR as the change.