Files
pbakaus_impeccable/crates/live/src/live_complete.rs
T
Paul BakausandClaude Fable 5.1 0547ed6a63 reorg C: the open Rust runtime joins this repo as one Cargo workspace
The engine no longer lives in a separate repo. `crates/` is a snapshot of the
open crates (foundation, core, common, context, live, hook, skills, comp,
comp-verbs, html, browser, detect, cli) plus `Cargo.lock`, taken as a git
archive of the engine repo at the commit that finished the boundary split.
None of that repo's history comes with it, and none of it should: the closed
half stays private.

The closed half is the rule engine. It ships as a prebuilt native archive per
target, `libimpeccable_detector.a`, published as a `detector-v<X>` GitHub
Release on this repo. `crates/core/build.rs` resolves and links it three ways:
`IMPECCABLE_DETECTOR_LIB=<dir>` for a local detector build, else the
`~/.impeccable/detector/<version>/<target>/` cache, else a download verified
against its `.sha256` sidecar. `crates/core` is a thin shim over a three-symbol
C ABI; nothing above it knows the boundary exists.

What changed versus the engine repo copy:

- Every crate manifest moves from `license-file.workspace` to
  `license.workspace` (this workspace declares Apache-2.0), and the workspace
  gains the `postcard` dependency the boundary encoding needs.
- The launcher contract test reads `skill/scripts/impeccable{,.cmd}` instead of
  a sibling `launcher/` dir, and `engine_binary` downloads from
  `github.com/pbakaus/impeccable/releases/download/engine-v<version>/` instead
  of the retired dist repo. No oracle golden carried the old URL, so no
  re-recording was owed.
- The tests that hunted for a public repo through `IMPECCABLE_PUBLIC_REPO`,
  `../impeccable-second` or a hardcoded home directory now resolve the root as
  `CARGO_MANIFEST_DIR/../..`, because they are in it. The env var stays as an
  override for an out-of-tree checkout.
- The in-page bundle (`detect-antipatterns-browser.js`, 2 MB of generated wasm
  glue) is no longer tracked. `crates/core/build.rs` resolves it beside the
  archive, hands the path to `impeccable_core::browser::IN_PAGE_BUNDLE_JS`, and
  live mode serves that. `scripts/check-detector-release.mjs` now requires it
  and its `.sha256` in a detector release.
- The live crate embeds `skill/scripts/live-browser*.js` and
  `modern-screenshot.umd.js` directly rather than through vendored copies, so
  the binary and the installed skill cannot drift.
- `crates/browser/assets/` (an unused second copy of the bundle) is gone.
- `tests/lib/engine-bin.mjs` also accepts `target/release/impeccable`, so a
  plain `cargo build --release -p impeccable` is enough to run `bun run test`.

Verified with the archive from a local detector build: `cargo test --workspace`
267 pass, oracle 795 pass / 0 fail / 0 missing, `bun run build` clean, the
default suite green, and the launcher's `engine-probe` handshake answering
through `skill/scripts/impeccable`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vau2X53xGTjjTCXWMVBoNY
2026-09-01 15:31:26 -07:00

188 lines
6.7 KiB
Rust

//! JS: live-complete.mjs -> `impeccable live-complete`. The canonical durable
//! completion acknowledgement, gated on the accepted source being clean.
use crate::accept_verify::verify_accepted_file;
use crate::paths::read_live_server_info;
use crate::roots::enter_live_root;
use crate::server::post_poll;
use crate::session::create_live_session_store;
use crate::util::{json_pretty, jsp, println};
use impeccable_common::Io;
use serde_json::{json, Value};
const USAGE: &str = "Usage: impeccable live-complete --id SESSION_ID [--discarded|--error MESSAGE] [--force]\n\nAppend the final durable session acknowledgement. Use after accept/discard cleanup is verified.\nCompletion is refused while the session's source file still carries live-mode leftovers\n(markers, data-p-* attributes, unbaked --p-* vars); fix the file or pass --force.";
struct Args {
id: Option<String>,
status: &'static str,
message: Option<String>,
force: bool,
help: bool,
}
fn parse_args(argv: &[String]) -> Args {
let mut out = Args {
id: None,
status: "complete",
message: None,
force: false,
help: false,
};
let mut i = 0;
while i < argv.len() {
let a = &argv[i];
if a == "--id" {
i += 1;
out.id = argv.get(i).cloned();
} else if let Some(v) = a.strip_prefix("--id=") {
out.id = Some(v.to_string());
} else if a == "--discarded" || a == "--discard" {
out.status = "discarded";
} else if a == "--error" {
out.status = "agent_error";
i += 1;
out.message = Some(
argv.get(i)
.cloned()
.filter(|m| !m.is_empty())
.unwrap_or_else(|| "unknown error".to_string()),
);
} else if let Some(v) = a.strip_prefix("--error=") {
out.status = "agent_error";
out.message = Some(v.to_string());
} else if a == "--force" {
out.force = true;
} else if a == "--help" || a == "-h" {
out.help = true;
}
i += 1;
}
out
}
pub fn run(args: &[String], io: &mut Io) -> i32 {
let mut argv: Vec<String> = args.to_vec();
if let Err(code) = enter_live_root(&mut argv, io) {
return code;
}
complete_cli(&argv, io)
}
fn complete_cli(argv: &[String], io: &mut Io) -> i32 {
let args = parse_args(argv);
let id = args.id.clone().filter(|s| !s.is_empty());
if args.help || id.is_none() {
println(io, USAGE);
return if args.help { 0 } else { 1 };
}
let id = id.unwrap();
let cwd = io.cwd.to_string_lossy().into_owned();
let env = io.env.clone();
if args.status == "complete" && !args.force {
let store = create_live_session_store(&cwd, &env, Some(&id));
let snapshot = match store.get_snapshot(&id, true) {
Ok(s) => s,
Err(e) => {
crate::util::eprintln(io, &format!("Error: {}", e));
return 1;
}
};
let source_file = snapshot
.as_ref()
.and_then(|s| s.get("sourceFile"))
.and_then(|v| v.as_str())
.filter(|s| !s.is_empty())
.map(String::from);
if let Some(sf) = &source_file {
let abs = jsp::resolve(&cwd, &[sf]);
let rel = jsp::relative("/", &cwd, &abs);
let inside = !rel.is_empty() && !rel.starts_with("..") && !jsp::is_absolute(&rel);
if inside
&& !rel.starts_with(&format!("node_modules{}", jsp::SEP))
&& !rel.starts_with("node_modules/")
{
let (clean, findings, _missing) = verify_accepted_file(&abs);
if !clean {
println(
io,
&json_pretty(&json!({
"ok": false,
"error": "source_dirty",
"id": id,
"file": sf,
"findings": findings,
"hint": "The accepted source still carries live-mode leftovers. Finish the carbonize cleanup (bake params, remove markers and data-p-* attributes), then run live-complete again. Use --force only if a finding is a false positive.",
})),
);
return 1;
}
}
}
}
let server_info = read_live_server_info(&cwd, &env).map(|(i, _)| i);
let server_result: Option<Value> = server_info.as_ref().and_then(|info| {
let ty = match args.status {
"discarded" => "discarded",
"agent_error" => "error",
_ => "complete",
};
let mut body = json!({ "token": info.token.clone().map(Value::String).unwrap_or(Value::Null), "id": id, "type": ty });
if let Some(m) = &args.message {
body["message"] = json!(m);
}
if info.token.is_none() {
body.as_object_mut().map(|o| o.shift_remove("token"));
}
info.port.and_then(|p| post_poll(p, &body))
});
if server_result
.as_ref()
.and_then(|r| r.get("ok"))
.map(crate::inject::detect_utils::truthy)
.unwrap_or(false)
{
let store = create_live_session_store(&cwd, &env, Some(&id));
let snapshot = store.get_snapshot(&id, true).ok().flatten();
let phase = snapshot
.as_ref()
.and_then(|s| s.get("phase"))
.filter(|p| crate::inject::detect_utils::truthy(p))
.cloned()
.unwrap_or_else(|| json!(args.status));
println(
io,
&json_pretty(
&json!({ "ok": true, "id": id, "phase": phase, "snapshot": snapshot.map(Value::Object).unwrap_or(Value::Null) }),
),
);
return 0;
}
let store = create_live_session_store(&cwd, &env, Some(&id));
let event = match args.status {
"discarded" => json!({ "type": "discarded", "id": id }),
"agent_error" => {
json!({ "type": "agent_error", "id": id, "message": args.message.clone().unwrap_or_else(|| "unknown error".to_string()) })
}
_ => json!({ "type": "complete", "id": id }),
};
match store.append_event(&event) {
Ok(snapshot) => {
let phase = snapshot.get("phase").cloned().unwrap_or(Value::Null);
println(
io,
&json_pretty(
&json!({ "ok": true, "id": id, "phase": phase, "snapshot": Value::Object(snapshot) }),
),
);
0
}
Err(e) => {
crate::util::eprintln(io, &format!("Error: {}", e));
1
}
}
}