← Commit history
Merge PR #686: 0.2.151: KiCad 10 check before the schematic (kicad-bridge#163) (kicad-10-schematic-check -> main) merge
Showing changes introduced by the merge (against its first parent).
9 files changed
+198−16
Cargo.lock+11−11@@ -4,7 +4,7 @@ version = 4 [[package]] name = "adom-aiflow"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-analyze", "aiflow-board",@@ -28,7 +28,7 @@ dependencies = [ [[package]] name = "aiflow-analyze"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-board", "aiflow-sys",@@ -39,7 +39,7 @@ dependencies = [ [[package]] name = "aiflow-board"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-sys", "roxmltree",@@ -49,7 +49,7 @@ dependencies = [ [[package]] name = "aiflow-bridge"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-board", "aiflow-sys",@@ -59,7 +59,7 @@ dependencies = [ [[package]] name = "aiflow-copper"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-board", "aiflow-grid",@@ -70,7 +70,7 @@ dependencies = [ [[package]] name = "aiflow-grid"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-board", "serde",@@ -79,7 +79,7 @@ dependencies = [ [[package]] name = "aiflow-place"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-board", "aiflow-sys",@@ -89,7 +89,7 @@ dependencies = [ [[package]] name = "aiflow-pours"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-board", "aiflow-copper",@@ -100,7 +100,7 @@ dependencies = [ [[package]] name = "aiflow-router"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-board", "aiflow-grid",@@ -111,7 +111,7 @@ dependencies = [ [[package]] name = "aiflow-run"-version = "0.2.150"+version = "0.2.151" dependencies = [ "aiflow-sys", "serde",@@ -120,7 +120,7 @@ dependencies = [ [[package]] name = "aiflow-sys"-version = "0.2.150"+version = "0.2.151" dependencies = [ "serde_json", ]
Cargo.toml+1−1@@ -15,7 +15,7 @@ members = [ ] [workspace.package]-version = "0.2.150"+version = "0.2.151" edition = "2021" license = "MIT" repository = "https://wiki.adom.inc/adom/adom-aiflow"
crates/adom-aiflow/src/kicad_versions.rsadded+119@@ -0,0 +1,119 @@+//! KiCad Bridge 1.1.15 (kicad-bridge#163, Jaxson Leiskau, KiCad 9.0.6): the bridge writes schematics in KiCad 10's+//! format and refuses to create one (`kicad_too_old_for_schematic_format`) when the KiCad the call uses is older than+//! 10; edits succeed with a `kicadFormatWarning`. A KiCad 9 kicad-cli cannot load such a sheet, so `schematic check`+//! failed on a missing ERC report. AI Flow reads the desktop's KiCad installs (`kicad_list_versions`) at intake and at+//! `step schematic`, says up front what the schematic step needs, and remembers the version to pin.+use serde_json::{json, Value};++/// What the desktop's KiCad installs mean for the schematic step.+#[derive(Debug, Clone, PartialEq)]+pub enum Verdict {+ /// The main install is KiCad 10 or newer.+ Ready,+ /// KiCad 10 is installed beside an older main install: pass this `kicadVersion` on every schematic call.+ Pin(String),+ /// Only KiCad older than 10 (the newest one named): KiCad 10 must be installed beside it.+ Upgrade(String),+ /// No KiCad installed.+ NoKicad,+ /// The bridge did not answer (an older bridge, an offline desktop): nothing is said.+ Unknown,+}++fn major(v: &str) -> u32 { v.trim().split('.').next().and_then(|m| m.parse().ok()).unwrap_or(0) }++fn body(reply: &Value) -> &Value { if reply["installs"].is_array() || reply["data"].is_null() { reply } else { &reply["data"] } }++fn named(i: &Value) -> String { i["cliVersion"].as_str().or(i["version"].as_str()).unwrap_or("?").to_string() }++pub fn verdict(reply: &Value) -> Verdict {+ let b = body(reply);+ let Some(installs) = b["installs"].as_array() else { return Verdict::Unknown };+ if installs.is_empty() { return if b["count"] == 0 || reply["status"] == "ok" { Verdict::NoKicad } else { Verdict::Unknown }; }+ let ver = |i: &Value| i["version"].as_str().or(i["cliVersion"].as_str()).map(major).unwrap_or(0);+ let primary = if b["primary"].is_object() { &b["primary"] } else { &installs[0] };+ if ver(primary) >= 10 { return Verdict::Ready; }+ if let Some(ten) = installs.iter().filter(|i| ver(i) >= 10).max_by_key(|i| ver(i)) {+ return Verdict::Pin(ten["version"].as_str().map(str::to_string).unwrap_or_else(|| format!("{}.0", ver(ten))));+ }+ Verdict::Upgrade(installs.iter().max_by_key(|i| ver(i)).map(named).unwrap_or_default())+}++/// The run's record (`kicadSchematic`): the installs, the verdict and the version to pin.+pub fn record(reply: &Value, target: &str, at: &str) -> Value {+ let b = body(reply);+ let v = verdict(reply);+ json!({"checkedAt": at, "target": target,+ "installs": b["installs"].as_array().into_iter().flatten().map(|i| json!({"version": i["version"], "cliVersion": i["cliVersion"]})).collect::<Vec<_>>(),+ "primary": b["primary"]["cliVersion"].as_str().or(b["primary"]["version"].as_str()),+ "verdict": match &v { Verdict::Ready => "ready", Verdict::Pin(_) => "pin", Verdict::Upgrade(_) => "upgrade", Verdict::NoKicad => "none", Verdict::Unknown => "unknown" },+ "kicadVersion": match &v { Verdict::Pin(p) => json!(p), _ => Value::Null }})+}++/// What to tell the AI (and through it the human), or nothing when the schematic step can run as it is.+pub fn message(v: &Verdict, target: &str) -> Option<(String, String)> {+ match v {+ Verdict::Ready | Verdict::Unknown => None,+ Verdict::Pin(p) => Some((format!("{target} has KiCad {p} beside an older main KiCad: the KiCad Bridge writes schematics in KiCad 10's format, which the older one cannot open"),+ format!("Pass kicadVersion \"{p}\" on every schematic call (kicad_sch_create, kicad_sch_sheet, kicad_sch_* edits, kicad_project_create, kicad_make_part_project) and open the schematic in KiCad {p}; `schematic check` runs ERC on KiCad {p} by itself."))),+ Verdict::Upgrade(old) => Some((format!("{target}'s only KiCad is {old}: the KiCad Bridge writes schematics in KiCad 10's format and refuses to create one for an older KiCad (kicad_too_old_for_schematic_format), and KiCad {old} could not open it, so the schematic step cannot run there yet"),+ format!("Tell the human now, before the schematic: it needs KiCad 10 installed beside KiCad {old} (`kicad_upgrade` asks them first, and {old} stays installed). Once it is in, `step schematic` checks again and remembers to pass kicadVersion \"10.0\" on every schematic call. Meanwhile the steps before the schematic (sourcing, design, simulation) go on as usual."))),+ Verdict::NoKicad => Some((format!("no KiCad is installed on {target}, and the schematic step needs KiCad 10"),+ "Ask the human to install KiCad 10 (`kicad_upgrade` asks them first) before the schematic.".to_string())),+ }+}++/// A `schematic check` ERC failure that is the format, not the design ("Failed to load schematic").+pub fn load_failure_hint(erc_error: &str, rec: &Value) -> Option<String> {+ if !erc_error.contains("Failed to load schematic") { return None; }+ let installed: Vec<String> = rec["installs"].as_array().into_iter().flatten().filter_map(|i| i["cliVersion"].as_str().or(i["version"].as_str()).map(str::to_string)).collect();+ Some(format!("\"Failed to load schematic\" is usually the format, not the design: the KiCad Bridge writes KiCad 10 schematics, and the kicad-cli that ran ERC is older{}. Install KiCad 10 beside it (`kicad_upgrade`, asks the human) and run `step schematic` again, which pins it (kicad-bridge#163).",+ if installed.is_empty() { String::new() } else { format!(" (installed: {})", installed.join(", ")) }))+}++#[cfg(test)]+mod tests {+ use super::*;++ fn install(v: &str, cli: &str) -> Value { json!({"version": v, "cliVersion": cli, "kicadCli": format!("C:/KiCad/{v}/bin/kicad-cli.exe")}) }++ #[test]+ fn a_kicad_9_box_is_told_to_add_kicad_10_up_front() {+ // Jaxson's machine (kicad-bridge#163): only KiCad 9.0.6+ let reply = json!({"status": "ok", "count": 1, "installs": [install("9.0", "9.0.6")], "primary": install("9.0", "9.0.6")});+ let v = verdict(&reply);+ assert_eq!(v, Verdict::Upgrade("9.0.6".into()));+ let (what, hint) = message(&v, "JAXSON-PC").unwrap();+ assert!(what.contains("9.0.6") && what.contains("kicad_too_old_for_schematic_format"), "{what}");+ assert!(hint.contains("kicad_upgrade") && hint.contains("10.0") && hint.contains("stays installed"), "{hint}");+ assert_eq!(record(&reply, "JAXSON-PC", "t")["verdict"], "upgrade");+ }++ #[test]+ fn kicad_10_beside_an_older_main_install_is_pinned() {+ let reply = json!({"status": "ok", "count": 2, "installs": [install("9.0", "9.0.6"), install("10.0", "10.0.8")], "primary": install("9.0", "9.0.6")});+ assert_eq!(verdict(&reply), Verdict::Pin("10.0".into()));+ let rec = record(&reply, "BOX", "t");+ assert_eq!((rec["verdict"].as_str(), rec["kicadVersion"].as_str(), rec["primary"].as_str()), (Some("pin"), Some("10.0"), Some("9.0.6")));+ assert!(message(&verdict(&reply), "BOX").unwrap().1.contains("kicadVersion \"10.0\""));+ }++ #[test]+ fn a_kicad_10_box_needs_nothing_and_an_old_bridge_says_nothing() {+ // the test laptop's live answer (KiCad Bridge 1.1.15)+ let live = json!({"status": "ok", "count": 1, "installs": [install("10.0", "10.0.8")], "primary": install("10.0", "10.0.8")});+ assert_eq!(verdict(&live), Verdict::Ready);+ assert!(message(&Verdict::Ready, "X").is_none());+ assert_eq!(verdict(&json!({"status": "error", "errorCode": "unknown_verb"})), Verdict::Unknown);+ assert_eq!(verdict(&json!({"status": "ok", "count": 0, "installs": []})), Verdict::NoKicad);+ assert_eq!(verdict(&json!({"data": {"installs": [install("10.0", "10.0.1")], "primary": install("10.0", "10.0.1")}})), Verdict::Ready, "an enveloped reply");+ }++ #[test]+ fn a_failed_load_points_at_the_format() {+ let rec = json!({"installs": [{"version": "9.0", "cliVersion": "9.0.6"}]});+ let h = load_failure_hint("ERC produced no output. stderr: Failed to load schematic", &rec).unwrap();+ assert!(h.contains("9.0.6") && h.contains("kicad_upgrade"), "{h}");+ assert!(load_failure_hint("3 errors: pin not connected", &rec).is_none());+ }+}
crates/adom-aiflow/src/main.rs+36−1@@ -2,6 +2,7 @@ //! Every reply starts with OK: or ERROR: and carries Hint: lines that depend on this board and run. mod bare_lands; mod part_follow;+mod kicad_versions; mod connector_mating; mod continuation; mod model_fit;@@ -3638,6 +3639,20 @@ fn refine_hints(fields: &Value, board_text: &str, spec: &Value) -> Vec<String> { out } +/// kicad-bridge#163 (KiCad Bridge 1.1.15): which KiCad on the run's desktop can open the schematic the bridge writes+/// (KiCad 10's format). Records `kicadSchematic` (the installs, the verdict, the version to pin) and returns what to+/// tell the human, if anything. An older bridge, or a desktop that does not answer, records and says nothing.+fn kicad_schematic_preflight(r: &mut Run, br: &Bridge) -> Option<(String, String)> {+ let reply = br.raw_call_bounded("kicad_list_versions", &json!({"reason": "AI Flow: which KiCad can open the schematic"}), std::time::Duration::from_secs(60));+ let v = kicad_versions::verdict(&reply);+ if v != kicad_versions::Verdict::Unknown {+ let rec = kicad_versions::record(&reply, &br.target, &now());+ r.log("kicad-versions", rec.clone());+ r.data["kicadSchematic"] = rec;+ }+ kicad_versions::message(&v, &br.target)+}+ fn bridge_of(r: &Run, cli: &Cli) -> Bridge { let target = r.data["target"].as_str().unwrap_or_else(|| err("the run has no desktop target", &["Start the run with --target <box> --remote-board <path on the box>.".into()])).to_string(); Bridge { ai_thread: thread(cli), target }@@ -4837,6 +4852,12 @@ fn main() { record_usage(&mut r, "start"); r.save().unwrap(); let _ = TURN.set((dir.clone(), idx));+ // kicad-bridge#163: a desktop whose only KiCad is older than 10 cannot take the schematic the bridge writes; say it now+ if let Some(tg) = target.as_deref().filter(|_| r.data["kind"] != "pattern" && sequence::lane(&dir, &r.data) == "kicad") {+ let br = Bridge { ai_thread: t.clone(), target: tg.to_string() };+ if let Some((what, hint)) = kicad_schematic_preflight(&mut r, &br) { note(&what, &[hint]); }+ r.save().unwrap();+ } let req_v = requirements_of(&r); let profile = preboard::named_profile(&req_v); let fixture_hint = testplan::intake_fixture_hint(&req_v);@@ -5473,7 +5494,13 @@ fn main() { let kc = std::env::var("ADOM_AIFLOW_KICAD_CLI").unwrap_or_else(|_| "kicad-cli".into()); let sdir = dir.join("schematic"); let _ = std::fs::create_dir_all(&sdir); let erc = sdir.join("erc.json");- let (_, erc_errors) = schematic_check::run_erc(&kc, sch, &erc).unwrap_or_else(|e| err(&format!("schematic check: {e}"), &["ERC must run and its report come back before the schematic can pass. In a container: ADOM_AIFLOW_KICAD_CLI=adom-aiflow-kicad-cli-remote KICAD_REMOTE_TARGET=<desktop> (it runs `sch erc` on the desktop's KiCad).".into()]));+ // kicad-bridge#163: the KiCad 10 the run pinned beside an older main install runs ERC (the remote wrapper reads it)+ if let Some(p) = r.data["kicadSchematic"]["kicadVersion"].as_str() { if std::env::var("KICAD_REMOTE_KICAD_VERSION").is_err() { std::env::set_var("KICAD_REMOTE_KICAD_VERSION", p); } }+ let (_, erc_errors) = schematic_check::run_erc(&kc, sch, &erc).unwrap_or_else(|e| {+ let mut hints = vec!["ERC must run and its report come back before the schematic can pass. In a container: ADOM_AIFLOW_KICAD_CLI=adom-aiflow-kicad-cli-remote KICAD_REMOTE_TARGET=<desktop> (it runs `sch erc` on the desktop's KiCad).".to_string()];+ if let Some(h) = kicad_versions::load_failure_hint(&e, &r.data["kicadSchematic"]) { hints.insert(0, h); }+ err(&format!("schematic check: {e}"), &hints)+ }); let net = sdir.join("netlist.net"); export_sch_netlist(sch, &net).unwrap_or_else(|e| err(&e, &[])); let on_sheet = schematic_check::sheet_symbols(&std::fs::read_to_string(&net).unwrap_or_default()).unwrap_or_else(|e| err(&format!("netlist: {e}"), &[]));@@ -9511,6 +9538,14 @@ fn main() { } if let Some(v) = r.data["steps"][name.as_str()]["visits"].as_array_mut().and_then(|a| a.last_mut()) { v["hashes"] = vh; } if !seq.is_empty() { sequence::write_timeline(&dir, &sequence::timeline(&dir, &r.data, &flow_full, &seq, &r.step_report()), TIMELINE_PAGE); }+ // kicad-bridge#163: before drawing, the KiCad that will open the schematic (and the version to pin)+ if name == "schematic" && sequence::lane(&dir, &r.data) == "kicad" {+ if let Some(target) = r.data["target"].as_str().map(str::to_string) {+ let br = Bridge { ai_thread: thread(&cli), target };+ if let Some((what, hint)) = kicad_schematic_preflight(&mut r, &br) { note(&what, &[hint]); }+ r.save().ok();+ }+ } // per-step clips: the previous step's recording stops, this step's starts (window capture of the editor) let mut clip_note = String::new(); if let Some(target) = r.data["target"].as_str().map(str::to_string) {
docs/release-0.2.151.mdadded+10@@ -0,0 +1,10 @@+# adom-aiflow 0.2.151++Adopts KiCad Bridge 1.1.15 (kicad-bridge#163, Jaxson Leiskau, KiCad 9.0.6). The bridge writes schematics in KiCad 10's format and now refuses to create one when the KiCad the call uses is older than 10 (`kicad_too_old_for_schematic_format`); a KiCad 9 kicad-cli cannot load such a sheet, so `schematic check` failed on a missing ERC report.++- **Said up front.** `intake --target` (KiCad lane) and `step schematic` read the desktop's KiCad installs (`kicad_list_versions`) and record them in the run (`kicadSchematic`: installs, main install, verdict, version to pin). When the only KiCad is older than 10, a note says so before any drawing: the human is asked to install KiCad 10 beside it (`kicad_upgrade` asks them, the old one stays), and the steps before the schematic go on meanwhile. Nothing is said when KiCad 10 is the main install, or when an older bridge does not answer.+- **KiCad 10 beside an older main install** is pinned: the note says to pass `kicadVersion: "10.0"` on every schematic call, and `schematic check` runs ERC and the netlist export on KiCad 10 (the remote wrapper picks the install named by `KICAD_REMOTE_KICAD_VERSION` from `kicad_list_versions`, and refuses a version that is not installed).+- **"Failed to load schematic"** in `schematic check` now leads with what it usually is (the format, not the design), the installed versions, and the fix.+- Skill: aiflow-schematic-to-board says the schematic needs KiCad 10 and what to do on an older desktop.++Whether the bridge should ever write KiCad 9 schematics is open on kicad-bridge#163.
package.json+1−1@@ -1,7 +1,7 @@ { "slug": "adom-aiflow", "type": "app",- "version": "0.2.150",+ "version": "0.2.151", "title": "AI Flow", "description": "Adom's AI Flow: a tool to help the AI follow all of the steps it takes to build a board. One Rust binary with a crate per step (placement helpers, a grid router with Kelvin taps, pours with keepouts, KiCad's DRC gate, live landing through the KiCad Bridge, copper measurement, current and thermal analysis) and a finish line that refuses an unfinished board. Every command answers with hints for the AI; every turn, its thinking time and every rework loop go into run.jsonl, so Claude, Codex and any other engine are compared on the same flow. KiCad, Fusion 360 and Altium Designer today (the same steps and gates through each tool's bridge); Adom's own web apps next.", "summary": "Adom's AI Flow: a tool to help the AI follow all of the steps it takes to build a board. The AI thinks its way from placement through routing, pours, current and thermal analysis to a delivered video; the binary does the fast, deterministic parts of every step, hands back hints, and keeps a ledger of every turn, every return to an earlier step, and the clock from the prompt to done.",
page.json+1−1@@ -1,7 +1,7 @@ { "slug": "adom-aiflow", "type": "app",- "version": "0.2.150",+ "version": "0.2.151", "title": "AI Flow", "description": "Adom's AI Flow: a tool to help the AI follow all of the steps it takes to build a board. One Rust binary with a crate per step (placement helpers, a grid router with Kelvin taps, pours with keepouts, KiCad's DRC gate, live landing through the KiCad Bridge, copper measurement, current and thermal analysis) and a finish line that refuses an unfinished board. Every command answers with hints for the AI; every turn, its thinking time and every rework loop go into run.jsonl, so Claude, Codex and any other engine are compared on the same flow. KiCad, Fusion 360 and Altium Designer today (the same steps and gates through each tool's bridge); Adom's own web apps next.", "summary": "Adom's AI Flow: a tool to help the AI follow all of the steps it takes to build a board. The AI thinks its way from placement through routing, pours, current and thermal analysis to a delivered video; the binary does the fast, deterministic parts of every step, hands back hints, and keeps a ledger of every turn, every return to an earlier step, and the clock from the prompt to done.",
skills/aiflow-schematic-to-board/SKILL.md+5@@ -35,6 +35,11 @@ The KiCad Bridge has no "update PCB from schematic" verb. So do not draw a schem ## 2. The schematic: gen_schematic.py +**First, the desktop's KiCad must be 10 or newer** (kicad-bridge#163). A schematic is KiCad 10's format whether you write it yourself or with the KiCad Bridge (whose `kicad_sch_create`, `kicad_sch_sheet add`, `kicad_project_create` and `kicad_make_part_project` refuse with `kicad_too_old_for_schematic_format` when the KiCad the call uses is older than 10; edits carry a `kicadFormatWarning`), and KiCad 9 cannot open it, so ERC fails with "Failed to load schematic". `intake --target` and `step schematic` read the desktop's installs (`kicad_list_versions`) and record them in the run (`kicadSchematic`):+- **only KiCad 9 or older**: tell the human before the schematic that it needs KiCad 10 installed beside it (`kicad_upgrade` asks them first, the old one stays), then `step schematic` again;+- **KiCad 10 beside an older main install**: pass `kicadVersion: "10.0"` on every schematic call and open the sheet in KiCad 10; `schematic check` runs ERC on KiCad 10 by itself (the remote wrapper reads `KICAD_REMOTE_KICAD_VERSION`).++ Write the `.kicad_sch` directly as s-expressions (a small reader/writer, `sexp.py`, is enough). - **Embed the symbols** in `lib_symbols`. A symbol that `extends` another must be flattened (copy the base body, merge the child's properties, rename the unit sub-symbols); KiCad will not resolve `extends` inside a schematic.
tools/kicad-cli-remote+14−1@@ -28,8 +28,10 @@ set -euo pipefail T="${KICAD_REMOTE_TARGET:-}" TH="${KICAD_REMOTE_THREAD:-kicad-cli-remote}"-# The desktop's native kicad-cli: KICAD_REMOTE_CLI, else what the KiCad bridge reports (install.kicadCli).+# The desktop's native kicad-cli: KICAD_REMOTE_CLI, else the install of KICAD_REMOTE_KICAD_VERSION (adom-aiflow sets it+# when KiCad 10 sits beside an older main install, kicad-bridge#163), else what the KiCad bridge reports (install.kicadCli). CLI="${KICAD_REMOTE_CLI:-}"+KVER="${KICAD_REMOTE_KICAD_VERSION:-}" ROS="${KICAD_REMOTE_OS:-}"; ROS="${ROS,,}"; KSTATUS_OS=""; RTMP=""; RTMP_OURS="" if [[ "${1:-}" == "--version" ]]; then echo "kicad-cli-remote -> ${T:-this machine} (native KiCad via Adom Bridge)"; exit 0; fi@@ -42,6 +44,17 @@ except Exception: pass'; } absp() { realpath -m -- "$1"; } find_cli() {+ if [[ -z "$CLI" && -n "$KVER" ]]; then+ CLI=$(ab kicad_list_versions '{"reason":"find the pinned KiCad for adom-aiflow"}' | python3 -c 'import sys,json+want=sys.argv[1]+try: d=json.load(sys.stdin)+except Exception: d={}+for i in d.get("installs") or []:+ v=str(i.get("version") or ""); c=str(i.get("cliVersion") or "")+ if v==want or c==want or c.startswith(want+".") or v.startswith(want+"."):+ print(i.get("kicadCli") or ""); break' "$KVER" || true)+ [[ -n "$CLI" ]] || { echo "kicad-cli-remote: KiCad $KVER is not installed on ${T:-this machine} (kicad_list_versions)" >&2; exit 2; }+ fi if [[ -z "$CLI" ]]; then local found found=$(ab kicad_status '{"reason":"find the native kicad-cli for adom-aiflow"}' | python3 -c 'import sys,json