Craft Anything: understand how any software works, and craft your own.
Rust
Report unavailable cached bodies in stand-in replay (#543)
Afloatd5e07a826275
diff --git a/crates/analysis/src/standin.rs b/crates/analysis/src/standin.rsindex d0933504..30d76376 100644--- a/crates/analysis/src/standin.rs+++ b/crates/analysis/src/standin.rs@@ -1035,6 +1035,8 @@ pub enum NoMatchReason {MethodNotRecorded,/// The URL was recorded, but not this GraphQL operation.OperationNotRecorded,+ /// The matching responses need a cached body that is unavailable in the recording.+ CachedBodyUnavailable,}/// A route close to what was asked for.@@ -1165,7 +1167,7 @@ pub fn answer(standin: &StandIn, turns: &mut Turns, method: &str, path: &str, qu}}- let Some((ri, how, fit)) = picked else {+ let Some((ri, how, mut fit)) = picked else {let path_known = standin.routes.iter().any(|r| template_fits(&r.template, &clean_path) || r.samples.iter().any(|s| s.path == clean_path));let reason = if operation_missing {NoMatchReason::OperationNotRecorded@@ -1179,6 +1181,13 @@ pub fn answer(standin: &StandIn, turns: &mut Turns, method: &str, path: &str, qulet Some(route) = standin.routes.get(ri) else {return Answer::NoMatch(NoMatch { reason: NoMatchReason::PathNotRecorded, nearest: Vec::new() });};+ // Usable cached 304 bodies become 200 at build time. Keep unavailable samples in the file+ // as evidence, but a fresh client cannot use them. Filter only after choosing the best match:+ // another query, resource or GraphQL operation is not a substitute for its missing body.+ fit.retain(|i| route.samples.get(*i).is_some_and(|s| s.status != 304));+ if fit.is_empty() {+ return Answer::NoMatch(NoMatch { reason: NoMatchReason::CachedBodyUnavailable, nearest: nearest(standin, look_for, &clean_path) });+ }let first = fit.first().copied().unwrap_or(0);let counter = turns.0.entry((ri, first, how)).or_insert(0);let at = counter.checked_rem(fit.len()).unwrap_or(0);diff --git a/crates/analysis/src/standin/tests.rs b/crates/analysis/src/standin/tests.rsindex 8fb4c1af..e6e23330 100644--- a/crates/analysis/src/standin/tests.rs+++ b/crates/analysis/src/standin/tests.rs@@ -685,6 +685,104 @@ fn a_304_with_its_cached_body_answers_as_200() {assert_eq!((e.status, e.was_not_modified), (304, false), "nothing to stand in for it");}+#[test]+fn a_304_without_its_cached_body_has_no_replayable_answer() {+ for recorded in [false, true] {+ let mut e = entry("GET", &format!("{A}/cached"), 304, "");+ if !recorded {+ e["response"]["content"].as_object_mut().unwrap().remove("text");+ }+ let s = built(vec![e]);+ let no = missing(get(&s, &mut Turns::default(), "/cached", ""));+ assert_eq!(serde_json::to_value(no.reason).unwrap(), "cached_body_unavailable");+ assert_eq!(no.nearest, [NearbyRoute { method: "GET".into(), template: "/cached".into() }]);+ }+}++#[test]+fn polling_skips_304_samples_without_a_cached_body() {+ let s = built(vec![+ entry("GET", &format!("{A}/poll"), 304, ""),+ entry("GET", &format!("{A}/poll"), 200, r#"{"n":1}"#),+ entry("GET", &format!("{A}/poll"), 304, ""),+ entry("GET", &format!("{A}/poll"), 200, r#"{"n":2}"#),+ ]);+ let mut turns = Turns::default();+ for expected in [1, 2, 1, 2] {+ let f = found(get(&s, &mut turns, "/poll", ""));+ assert_eq!((f.status, f.of), (200, 2));+ assert_eq!(body(&f), format!("{{\"n\":{expected}}}"));+ }+}++#[test]+fn a_missing_cached_answer_does_not_fall_back_to_a_different_query_or_id() {+ let s = built(vec![+ entry("GET", &format!("{A}/users/1?view=summary"), 304, ""),+ entry("GET", &format!("{A}/users/1?view=details"), 200, r#"{"details":true}"#),+ entry("GET", &format!("{A}/users/2?view=summary"), 200, r#"{"id":2}"#),+ ]);+ let no = missing(get(&s, &mut Turns::default(), "/users/1", "view=summary"));+ assert_eq!(serde_json::to_value(no.reason).unwrap(), "cached_body_unavailable");+ assert_eq!(body(&found(get(&s, &mut Turns::default(), "/users/2", "view=summary"))), r#"{"id":2}"#);+}++#[test]+fn a_missing_cached_graphql_answer_keeps_its_operation() {+ let mut cached = entry("POST", &format!("{A}/graphql"), 304, "");+ cached["request"]["postData"] = json!({"text": r#"{"operationName":"GetUser"}"#});+ let mut other = entry("POST", &format!("{A}/graphql"), 200, r#"{"feed":[]}"#);+ other["request"]["postData"] = json!({"text": r#"{"operationName":"GetFeed"}"#});+ let s = built(vec![cached, other]);+ let no = missing(answer(&s, &mut Turns::default(), "POST", "/graphql", "", br#"{"operationName":"GetUser"}"#));+ assert_eq!(serde_json::to_value(no.reason).unwrap(), "cached_body_unavailable");+ assert_eq!(body(&found(answer(&s, &mut Turns::default(), "POST", "/graphql", "", br#"{"operationName":"GetFeed"}"#))), r#"{"feed":[]}"#);+}++#[test]+fn a_304_whose_cached_body_was_left_out_is_unavailable() {+ let mut invalid = entry("GET", &format!("{A}/cached"), 304, "@@@");+ invalid["response"]["content"]["encoding"] = json!("base64");+ let fake_pem_header = ["-----BEGIN ", "PRIVATE KEY-----"].concat();+ let cases = [+ (entry("GET", &format!("{A}/cached"), 304, "large"), BuildOptions { max_body_bytes: 1, ..Default::default() }, BodyLeftOut::TooLarge),+ (entry("GET", &format!("{A}/cached"), 304, "{}"), BuildOptions { max_total_body_bytes: 1, ..Default::default() }, BodyLeftOut::FileLimit),+ (invalid, BuildOptions::default(), BodyLeftOut::Undecodable),+ (entry("GET", &format!("{A}/cached"), 304, &fake_pem_header), BuildOptions::default(), BodyLeftOut::HeldSecret),+ ];+ for (e, options, reason) in cases {+ let s = build_from_bytes(&har_of(vec![e]), &options).unwrap();+ assert_eq!(s.routes[0].samples[0].body_left_out, Some(reason));+ let no = missing(get(&s, &mut Turns::default(), "/cached", ""));+ assert_eq!(serde_json::to_value(no.reason).unwrap(), "cached_body_unavailable");+ }+}++#[test]+fn cached_text_and_binary_304_bodies_still_replay_as_200() {+ let mut binary = entry("GET", &format!("{A}/binary"), 304, "//4=");+ binary["response"]["content"]["mimeType"] = json!("application/octet-stream");+ binary["response"]["content"]["encoding"] = json!("base64");+ let s = built(vec![entry("GET", &format!("{A}/text"), 304, r#"{"cached":true}"#), binary]);+ let text = found(get(&s, &mut Turns::default(), "/text", ""));+ assert_eq!((text.status, body(&text).as_str()), (200, r#"{"cached":true}"#));+ let binary = found(get(&s, &mut Turns::default(), "/binary", ""));+ assert_eq!((binary.status, binary.body.as_slice()), (200, &b"\xff\xfe"[..]));+}++#[test]+fn an_existing_v1_file_does_not_replay_a_bare_304() {+ let s: StandIn = serde_json::from_str(+ r#"{"version":1,"routes":[{"method":"GET","template":"/cached","samples":[{"path":"/cached","status":304,"body_left_out":"not_recorded"}]}]}"#,+ )+ .unwrap();+ let no = missing(get(&s, &mut Turns::default(), "/cached", ""));+ assert_eq!(serde_json::to_value(no.reason).unwrap(), "cached_body_unavailable");+ let controls = built(vec![entry("GET", &format!("{A}/empty"), 200, ""), entry("GET", &format!("{A}/no-content"), 204, "")]);+ assert_eq!(found(get(&controls, &mut Turns::default(), "/empty", "")).status, 200);+ assert_eq!(found(get(&controls, &mut Turns::default(), "/no-content", "")).status, 204);+}+#[test]fn a_saved_stand_in_reads_back_the_same() {let opts = BuildOptions { source_name: Some("site.har".into()), source_sha256: Some("abc".into()), ..Default::default() };diff --git a/crates/engine/src/tools/standin_tools.rs b/crates/engine/src/tools/standin_tools.rsindex da9d44a8..c0ff6843 100644--- a/crates/engine/src/tools/standin_tools.rs+++ b/crates/engine/src/tools/standin_tools.rs@@ -153,6 +153,7 @@ fn route_row(r: &standin::StandInRoute) -> Value {"requests": r.requests,"samples": r.samples.len(),"samples_left_out": r.samples_left_out,+ "unavailable_cached_answers": r.samples.iter().filter(|s| s.status == 304).count(),"bodies_left_out": r.samples.iter().filter(|s| s.body_left_out.is_some_and(|b| b != BodyLeftOut::NotRecorded)).count(),})}@@ -196,6 +197,7 @@ fn web_standin_build(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Toolet rows: Vec<Value> = built.routes.iter().skip(p.offset).take(limit).map(route_row).collect();let dropped = built.dropped.total();let samples: usize = built.routes.iter().map(|r| r.samples.len()).sum();+ let unavailable_cached_answers = built.routes.iter().flat_map(|r| &r.samples).filter(|s| s.status == 304).count();let summary = format!("{total} route(s) on {} host(s) from {} recorded request(s), with {samples} recorded answer(s); {dropped} secret(s) dropped. Written to {}.",built.hosts.len(),@@ -211,6 +213,7 @@ fn web_standin_build(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Too"routes_total": total,"routes": rows,"samples": samples,+ "unavailable_cached_answers": unavailable_cached_answers,"dropped": built.dropped,"skipped": built.skipped,"source": built.source,@@ -228,6 +231,12 @@ fn web_standin_build(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Too.limitation(Limitation::new("secrets_dropped", format!("{dropped} credential or secret-shaped value(s) were dropped, so anything that needs a real session won't work. Other response text is kept as recorded, and may hold personal data: name it in hidden_values, and don't commit this file."))).coverage(Coverage::page(shown, total as u64, p.offset as u64)).location(Location::new("path", out.display().to_string()));+ if unavailable_cached_answers > 0 {+ b = b.limitation(Limitation::new(+ "cached_body_unavailable",+ format!("{unavailable_cached_answers} recorded 304 response(s) have no usable cached body. They are kept in the file but cannot be replayed; record the full responses to supply these answers."),+ ));+ }if built.source.cut_short {b = b.limitation(Limitation::new("cut_short",@@ -327,7 +336,7 @@ impl Answerer for Replay {"method": r.method,"path": path,"nearest": n.nearest,- "message": "The recording behind this stand-in has no answer for this request.",+ "message": "The recording behind this stand-in has no replayable answer for this request.",}),);response.headers.push((ANSWER_HEADER.into(), "no-recorded-answer".into()));diff --git a/crates/engine/src/tools/standin_tools/tests.rs b/crates/engine/src/tools/standin_tools/tests.rsindex a434862a..af7b0782 100644--- a/crates/engine/src/tools/standin_tools/tests.rs+++ b/crates/engine/src/tools/standin_tools/tests.rs@@ -129,6 +129,45 @@ fn build_start_request_show_stop_through_the_engine() {});}+#[test]+fn a_fresh_client_gets_a_missing_cache_reason_instead_of_a_bare_304() {+ within(60, || {+ let dir = crate::testing::temp_dir();+ let har = dir.join("cache.har");+ let entries = [+ entry("GET", "https://api.example.test/cached", 304, ""),+ entry("GET", "https://api.example.test/mixed", 304, ""),+ entry("GET", "https://api.example.test/mixed", 200, r#"{"n":1}"#),+ entry("GET", "https://api.example.test/known", 304, r#"{"cached":true}"#),+ ];+ std::fs::write(&har, json!({"log": {"version": "1.2", "entries": entries}}).to_string()).unwrap();+ let e = Engine::new(crate::testing::session());+ let out = dir.join("standin");+ let built = call(&e, "web_standin_build", json!({"path": har, "output_dir": out})).unwrap();+ assert_eq!(built["result"]["unavailable_cached_answers"], 2);+ assert!(built["limitations"].as_array().unwrap().iter().any(|l| l["code"] == "cached_body_unavailable"));+ let cached = built["result"]["routes"].as_array().unwrap().iter().find(|r| r["template"] == "/cached").unwrap();+ assert_eq!(cached["unavailable_cached_answers"], 1);+ let started = call(&e, "web_standin_start", json!({"path": out})).unwrap();+ let url = started["result"]["url"].as_str().unwrap();+ let response = get(url, "/cached");+ assert!(response.starts_with("HTTP/1.1 404"), "{response}");+ assert!(response.contains("X-Craft-Anything-Stand-In: no-recorded-answer"), "{response}");+ let body: Value = serde_json::from_str(response.split("\r\n\r\n").nth(1).unwrap()).unwrap();+ assert_eq!(body["reason"], "cached_body_unavailable");+ assert_eq!(body["nearest"][0]["template"], "/cached");+ for path in ["/mixed", "/mixed", "/known"] {+ let response = get(url, path);+ assert!(response.starts_with("HTTP/1.1 200"), "{path}: {response}");+ }+ let shown = call(&e, "web_standin_show", json!({})).unwrap();+ assert_eq!(shown["result"]["servers"][0]["requests_answered"], 3);+ assert_eq!(shown["result"]["servers"][0]["requests_with_no_answer"], 1);+ assert_eq!(shown["result"]["no_answer"]["items"][0]["reason"], "cached_body_unavailable");+ call(&e, "web_standin_stop", json!({"all": true})).unwrap();+ });+}+#[test]fn starting_twice_reuses_and_a_session_ending_stops_it() {within(60, || {diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex c72aca33..19b1828f 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -466,8 +466,10 @@ Owner: open · Status: in progress- [ ] Stand-in backend: writes that change later reads (recorded answers are replayed as they are)- [ ] Tell the builder about the stand-in backend in a website's brief, and start one from arecording-- [ ] Stand-in backend: a 304 recorded with no body is replayed as a bare 304, which a fresh browser- can't use; say so or leave it out+- [x] Stand-in backend: retain 304 samples without a usable cached body for diagnostics, skip them+ during replay, and report `cached_body_unavailable` when the best matching group has no+ usable answer. Build results count them; cached bodies still answer as 200 (#543)+ — claimed: Afloat16, branch bugfix/r3.11-standin-empty-304-20261103- [ ] Godot format 4 and IL2CPP metadata 38 and 39 are header-only; read their tables once a realfile can be checked- [ ] Bun: take source maps and bytecode out as files; a standalone `.pyz`diff --git a/docs/STATUS.md b/docs/STATUS.mdindex c95a40df..a21332b7 100644--- a/docs/STATUS.md+++ b/docs/STATUS.md@@ -19,7 +19,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi| esbuild module dependencies | Recognised CommonJS and ESM wrappers keep their dependency edges after identifier minification; semantic bindings distinguish shadowed and reused names, and repeated calls produce one edge | 44 JS unit tests pass. New synthetic regressions cover short names, forward declarations, both wrapper kinds, shadowed parameters, reused names, unrelated calls and duplicates. No real minified application was run for this correction; sources above the existing semantic-analysis size limit retain name-based resolution || Program facts across builds | `native_library_calls_list` (delay-load too), `native_offered_symbols_list`, `native_publisher_show` (checks the file hash and the signature, not Windows trust), `app_builds_line_up` pairs functions across two builds and says how, `native_source_folder_write` writes pseudo-C to a folder and picks up where it stopped, `android_components_list` | Unit tests. Real PE, ELF and Mach-O files; two builds of a 276 MB program; a signed Microsoft DLL; Android components from one real APK, spot-checked || ARM32 value flow | A32 and Thumb calls define ARM scratch registers, and word results flow through `r0` into returns in `native_value_flow_show` | Synthetic instruction fixtures for leaf results and indirect calls in both modes, with AArch64 and x86-64 controls; disassembler tests. This correction was not checked on real ARM binaries or devices |-| Stand-in backends | `web_standin_build` turns a HAR into `standin.json` (secrets dropped, varying path parts templated); `web_standin_start` answers from it on loopback; `web_standin_show` lists what it had no answer for. Writes replay what was recorded and don't change later reads. App maps list GraphQL and tRPC operations; `web_walkthrough_check` checks a scripted session without a browser | Unit tests, real loopback requests, and a run with curl on a hand-written HAR. No browser-made recording or real front end yet |+| Stand-in backends | `web_standin_build` turns a HAR into `standin.json` (secrets dropped, varying path parts templated); `web_standin_start` answers from it on loopback; `web_standin_show` lists what it had no answer for. A 304 without a usable cached body is counted at build time and skipped during replay; if its matching group has no usable answer, requests get a 404 with `cached_body_unavailable`, also shown in the request log. Writes replay what was recorded and don't change later reads. App maps list GraphQL and tRPC operations; `web_walkthrough_check` checks a scripted session without a browser | Unit tests, real loopback requests, and a run with curl on a hand-written HAR. The 304 correction passes 99 analysis tests and a real-loopback regression, covering mixed samples, omitted bodies, matching boundaries and existing v1 files. [Hosted CI for #543](https://github.com/storytold/craft-anything/actions/runs/41585168291) also passed the full engine tests and workspace gates. No browser-made recording or real front end yet || Websites as targets | `web_site_open` keeps a private browser for the session and 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 run on example.com; unit tests || 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 | Synthetic application graphs exercise changes in either anonymous window, unique and reused bindings, shifted source lines and unequal counts. 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) |diff --git a/docs/file-formats.md b/docs/file-formats.mdindex 8dcdf25d..c84b8067 100644--- a/docs/file-formats.md+++ b/docs/file-formats.md@@ -22,6 +22,15 @@ version up to the current one, then turn the document into Rust types. The stepOther JSON in a project (`assets.json`, `inbox/`, `checks/`, evidence records) has no version yet;add one the first time its shape changes.+## Stand-in cached responses++Version 1 stand-in files retain a recorded 304 when no usable cached body was saved. Those samples+remain in the file for diagnostics, but replay skips them within the best matching path, query and+operation group. If that group has no usable answer, it reports `cached_body_unavailable` instead+of sending a bare 304 to a fresh client. A usable cached body is still stored and replayed as a 200+with `was_not_modified: true`. This interpretation also applies to existing v1 files; the persisted+shape and version are unchanged.+## Newer filesA file from a newer Craft Anything is never downgraded or overwritten, because the fields we don't