Files
pbakaus_impeccable/crates/live/src/paths.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

166 lines
5.1 KiB
Rust

//! JS: lib/impeccable-paths.mjs (the live subset): `.impeccable/live` paths,
//! the live config path, server.json read/write, session ids.
use crate::util::{exists, jsp, pid_reachable, read_json, Env};
use impeccable_context::context::resolve_project_root;
use impeccable_context::target_args::TargetOptions;
use serde_json::Value;
pub const IMPECCABLE_DIR: &str = ".impeccable";
pub const LIVE_DIR: &str = "live";
/// JS: getImpeccableDir(cwd)
pub fn impeccable_dir(cwd: &str, env: &Env) -> String {
jsp::join(&[
&resolve_project_root(cwd, &TargetOptions::default(), env),
IMPECCABLE_DIR,
])
}
/// JS: getLiveDir(cwd)
pub fn live_dir(cwd: &str, env: &Env) -> String {
jsp::join(&[&impeccable_dir(cwd, env), LIVE_DIR])
}
/// JS: getLiveConfigPath(cwd)
pub fn live_config_path(cwd: &str, env: &Env) -> String {
jsp::join(&[&live_dir(cwd, env), "config.json"])
}
/// JS: resolveLiveConfigPath({ cwd, scriptsDir, env }). The legacy
/// `<scriptsDir>/config.json` fallback has no home in a single binary; the
/// primary path is returned when the env override is unset.
pub fn resolve_live_config_path(cwd: &str, env: &Env) -> String {
if let Some(v) = env.get("IMPECCABLE_LIVE_CONFIG") {
let t = impeccable_context::util::js_trim(v);
if !t.is_empty() {
return if jsp::is_absolute(t) {
t.to_string()
} else {
jsp::resolve(cwd, &[t])
};
}
}
live_config_path(cwd, env)
}
/// JS: getLiveServerPath(cwd)
pub fn live_server_path(cwd: &str, env: &Env) -> String {
jsp::join(&[&live_dir(cwd, env), "server.json"])
}
/// JS: getLegacyLiveServerPath(cwd)
pub fn legacy_live_server_path(cwd: &str, env: &Env) -> String {
jsp::join(&[
&resolve_project_root(cwd, &TargetOptions::default(), env),
".impeccable-live.json",
])
}
/// JS: getLiveSessionsDir(cwd)
pub fn live_sessions_dir(cwd: &str, env: &Env) -> String {
jsp::join(&[&live_dir(cwd, env), "sessions"])
}
/// JS: getLegacyLiveSessionsDir(cwd)
pub fn legacy_live_sessions_dir(cwd: &str, env: &Env) -> String {
jsp::join(&[
&resolve_project_root(cwd, &TargetOptions::default(), env),
".impeccable-live",
"sessions",
])
}
/// JS: getLiveAnnotationsDir(cwd)
pub fn live_annotations_dir(cwd: &str, env: &Env) -> String {
jsp::join(&[&live_dir(cwd, env), "annotations"])
}
/// The record `live-server` writes on listen.
#[derive(Debug, Clone)]
pub struct ServerInfo {
/// `pid` as recorded (any JSON value; only a number counts for liveness).
pub pid: Option<i64>,
pub port: Option<i64>,
pub token: Option<String>,
/// The parsed record verbatim.
pub raw: Value,
}
impl ServerInfo {
pub fn from_value(v: &Value) -> ServerInfo {
ServerInfo {
pid: v.get("pid").and_then(|p| p.as_i64()),
port: v.get("port").and_then(|p| p.as_i64()).or_else(|| {
v.get("port")
.and_then(|p| p.as_str())
.and_then(|s| s.parse::<i64>().ok())
}),
token: v.get("token").and_then(|t| t.as_str()).map(String::from),
raw: v.clone(),
}
}
/// `info.pid` was a JSON number.
pub fn pid_is_number(&self) -> bool {
matches!(self.raw.get("pid"), Some(Value::Number(_)))
}
}
/// JS: readLiveServerInfo(cwd): `{ info, path }` or None. A record whose pid
/// is a number and no longer signalable is unlinked and skipped.
pub fn read_live_server_info(cwd: &str, env: &Env) -> Option<(ServerInfo, String)> {
for file in [
live_server_path(cwd, env),
legacy_live_server_path(cwd, env),
] {
let Some(v) = read_json(&file) else { continue };
let info = ServerInfo::from_value(&v);
if info.pid_is_number() {
let alive = info.pid.map(pid_reachable).unwrap_or(false);
if !alive {
let _ = std::fs::remove_file(&file);
continue;
}
}
// JS: `info && typeof info.pid === 'number'` guards the check; any
// parsed JSON (even a scalar) is returned as-is.
return Some((info, file));
}
None
}
/// JS: writeLiveServerInfo(cwd, info)
pub fn write_live_server_info(cwd: &str, env: &Env, info: &Value) -> String {
let file = live_server_path(cwd, env);
let _ = crate::util::write_file(&file, &serde_json::to_string(info).unwrap_or_default());
file
}
/// JS: removeLiveServerInfo(cwd)
pub fn remove_live_server_info(cwd: &str, env: &Env) {
for file in [
live_server_path(cwd, env),
legacy_live_server_path(cwd, env),
] {
let _ = std::fs::remove_file(&file);
}
}
/// JS: safeSessionId(id): `/^[A-Za-z0-9_-]{1,128}$/` or an error message.
pub fn safe_session_id(id: &str) -> Result<&str, String> {
let ok = !id.is_empty()
&& id.len() <= 128
&& id
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-');
if ok {
Ok(id)
} else {
Err(format!("invalid session id: {}", id))
}
}
pub fn file_exists(p: &str) -> bool {
exists(p)
}