instinct-cli 0.3.9

Shared local memory for coding agents. Save a fix once and find it again in a new session or another connected tool.
# Instinct scoped checks: CLI adapter (the only file reader). Authored as raw Rust lines (TAA); hxc export-rust writes src/check/mod.rs.
TAA # // `instinct check`: scoped deterministic checks. The pure kernel (`kernel`, `json`) is Helix-authored
TAA # // (`*.hlx`, exported to the sibling `.rs` files); this file is only the CLI adapter, the one place that reads input.
TAA # // Pure operations open no bank. Explicit attach writes only its private sidecar; no operation grants authority.
TAA # #[allow(dead_code)]
TAA # pub(crate) mod json;
TAA # #[allow(dead_code)]
TAA # mod kernel;
TAA # mod observe;
TAA # #[allow(dead_code)]
TAA # mod digest;
TAA # #[allow(dead_code)]
TAA # mod receipt;
TAA # mod compose;
TAA # mod pack;
TAA # mod pack_fixtures;
TAA # mod observed_receipt;
TAA # mod dependencies;
TAA # mod refine;
TAA # mod export_boundary;
TAA # mod integrity;
TAA # pub(crate) mod feedback;
TAA # pub(crate) mod mcp;
TAA # pub use kernel::{evaluate, Mode};
TAA # use std::io::Read;
TAA # const USAGE: &str = "usage: instinct check validate|run|explain|receipt|replay|compose|refine|export-boundary|integrity|dependencies --input FILE|-\n  Reads one JSON request (at most 1 MiB) and prints one JSON result. Spec: docs/checks.md.\n  receipt prints a byte-identity receipt for a request; replay recomputes {request, receipt}; neither authenticates or authorizes.\n  observe --root ABS --input FILE|- observes a trusted staging inventory read-only; see check observe --help.\n  attach --lesson ID --store BANK --input FILE|- explicitly attaches a policy.\n  detach --lesson ID --store BANK withdraws a policy without deleting history.\n  pack preview|export|import explicitly handles declarative packages; see check pack --help.\n  Pure operations open no bank; attach/detach explicitly write their sidecar. Exit 0 for pass, fail and abstain, exit 2 for an error result.";
TAA # fn usage_error(why: &str) -> String {
TAA #     kernel::error_json(&format!("usage: {why}"))
TAA # }
TAA # fn read_input(path: &str) -> Result<Vec<u8>, String> {
TAA #     read_input_capped(path, json::MAX_BYTES)
TAA # }
TAA # fn read_input_capped(path: &str, max: usize) -> Result<Vec<u8>, String> {
TAA #     let cap = (max as u64) + 1;
TAA #     let mut buf = Vec::new();
TAA #     let got = if path == "-" {
TAA #         std::io::stdin().lock().take(cap).read_to_end(&mut buf)
TAA #     } else {
TAA #         match std::fs::File::open(path) {
TAA #             Ok(f) => f.take(cap).read_to_end(&mut buf),
TAA #             Err(e) => return Err(format!("input_io: cannot open input ({})", e.kind())),
TAA #         }
TAA #     };
TAA #     if let Err(e) = got {
TAA #         return Err(format!("input_io: cannot read input ({})", e.kind()));
TAA #     }
TAA #     if buf.len() > max {
TAA #         return Err(if max == json::MAX_BYTES { "input_too_large: more than 1 MiB".to_string() } else { "input_too_large: replay envelope exceeds 1 MiB plus 4096 bytes".to_string() });
TAA #     }
TAA #     Ok(buf)
TAA # }
TAA # /// Runs the subcommand and returns the process exit code. Output goes to stdout only, as one JSON object.
TAA # pub fn cli(args: &[String]) -> i32 {
TAA #     if args.first().map(String::as_str) == Some("pack") {
TAA #         return match pack::cli(&args[1..]) { Ok(s) => { println!("{s}"); 0 }, Err(e) => fail(&kernel::error_json(&e)) };
TAA #     }
TAA #     if args.first().map(String::as_str) == Some("attach") {
TAA #         return match feedback::attach_cli(&args[1..]) { Ok(s) => { println!("{s}"); 0 }, Err(e) => fail(&kernel::error_json(&e)) };
TAA #     }
TAA #     if args.first().map(String::as_str) == Some("detach") {
TAA #         return match feedback::detach_cli(&args[1..]) { Ok(s) => { println!("{s}"); 0 }, Err(e) => fail(&kernel::error_json(&e)) };
TAA #     }
TAA #     // Observation requires an explicit --root grant.
TAA #     if args.first().map(String::as_str) == Some("observe") {
TAA #         return observe::cli(&args[1..]);
TAA #     }
TAA #     if matches!(args.first().map(String::as_str), Some("observe-receipt"|"observe-replay")) {
TAA #         return match observed_receipt::cli(&args[1..], args[0]=="observe-replay") { Ok(s) => { println!("{s}"); 0 }, Err(e) => fail(&kernel::error_json(&e)) };
TAA #     }
TAA #     let is_help = |a: &String| a == "--help" || a == "-h";
TAA #     let sub_ok = |a: &String| matches!(a.as_str(), "validate" | "run" | "explain" | "receipt" | "replay" | "compose" | "refine" | "export-boundary" | "integrity" | "dependencies");
TAA #     if (args.len() == 1 && is_help(&args[0])) || (args.len() == 2 && sub_ok(&args[0]) && is_help(&args[1])) {
TAA #         println!("{USAGE}");
TAA #         return 0;
TAA #     }
TAA #     let mode = match args.first().map(String::as_str) {
TAA #         Some("validate") => Mode::Validate,
TAA #         Some("run") => Mode::Run,
TAA #         Some("explain") => Mode::Explain,
TAA #         Some("receipt") | Some("replay") | Some("compose") | Some("refine") | Some("export-boundary") | Some("integrity") | Some("dependencies") => Mode::Run,
TAA #         Some(other) if other.starts_with('-') => return fail(&usage_error("expected validate, run or explain first")),
TAA #         Some(_) => return fail(&usage_error("unknown subcommand; expected validate, run or explain")),
TAA #         None => return fail(&usage_error("missing subcommand; expected validate, run or explain")),
TAA #     };
TAA #     let mut input: Option<String> = None;
TAA #     let mut i = 1;
TAA #     while i < args.len() {
TAA #         let a = &args[i];
TAA #         let value = if a == "--input" {
TAA #             i += 1;
TAA #             match args.get(i) {
TAA #                 Some(v) if v == "-" || !v.starts_with("--") => v.clone(),
TAA #                 _ => return fail(&usage_error("--input needs a file path or -")),
TAA #             }
TAA #         } else if let Some(v) = a.strip_prefix("--input=") {
TAA #             v.to_string()
TAA #         } else if a.starts_with('-') && a != "-" {
TAA #             return fail(&usage_error("unknown option (only --input is accepted; --help is only accepted as `check --help` or `check SUBCOMMAND --help`)"));
TAA #         } else {
TAA #             return fail(&usage_error("unexpected positional argument"));
TAA #         };
TAA #         if input.is_some() {
TAA #             return fail(&usage_error("--input given more than once"));
TAA #         }
TAA #         input = Some(value);
TAA #         i += 1;
TAA #     }
TAA #     let path = match input {
TAA #         Some(p) if !p.is_empty() => p,
TAA #         _ => return fail(&usage_error("--input FILE|- is required")),
TAA #     };
TAA #     let max = if args[0] == "replay" { json::MAX_BYTES + json::REPLAY_EXTRA_BYTES } else { json::MAX_BYTES };
TAA #     let bytes = match read_input_capped(&path, max) {
TAA #         Ok(b) => b,
TAA #         Err(e) => return fail(&kernel::error_json(&e)),
TAA #     };
TAA #     let (out, is_error) = match args[0].as_str() {
TAA #         "receipt" => receipt::create(&bytes),
TAA #         "replay" => receipt::replay(&bytes),
TAA #         "compose" => compose::compose(&bytes),
TAA #         "dependencies" => dependencies::evaluate(&bytes),
TAA #         "refine" => refine::evaluate(&bytes),
TAA #         "export-boundary" => export_boundary::evaluate(&bytes),
TAA #         "integrity" => integrity::evaluate(&bytes),
TAA #         _ => evaluate(mode, &bytes),
TAA #     };
TAA #     println!("{out}");
TAA #     if is_error {
TAA #         2
TAA #     } else {
TAA #         0
TAA #     }
TAA # }
TAA # fn fail(json: &str) -> i32 {
TAA #     println!("{json}");
TAA #     2
TAA # }