Files
pbakaus_impeccable/crates/context/src/provider.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

121 lines
4.4 KiB
Rust

//! Provider identity and skill-directory resolution for the binary.
//!
//! The JS scripts learn their provider at build time (`lib/provider.mjs`,
//! rewritten per harness) and find `../reference/` and `../SKILL.md` relative
//! to their own file. One binary serves every harness, so both are resolved
//! at run time:
//!
//! - **Skill dir**: `IMPECCABLE_SKILL_DIR` when set; otherwise walk up from the
//! executable's path (the binary ships at `<skill>/scripts/bin/<target>/` or
//! is launched via `<skill>/scripts/impeccable`) until a directory holding
//! `reference/ios.md` is found. `None` when neither works (source checkouts
//! running `target/debug/impeccable` need the env var).
//! - **Provider id**: `IMPECCABLE_PROVIDER_ID` when set; otherwise derived from
//! the skill dir's harness folder (`<root>/.codex/skills/impeccable` ->
//! `codex`); otherwise `source`, exactly what the JS reads in a source
//! checkout. The command prefix is `$` for `codex`, `/` for everything else.
//! - **Self command**: the text a directive prints where the JS printed
//! `node <scripts>/<script>.mjs`. `IMPECCABLE_SELF` when set (the launcher
//! exports it), else the executable path. Printed as `<self> <verb>`.
use crate::jsp;
use crate::util::Env;
pub const SOURCE_PROVIDER: &str = "source";
pub struct Provider {
pub id: String,
pub command_prefix: String,
/// `<prefix>impeccable`
pub command: String,
pub skill_dir: Option<String>,
/// How to spell this binary in printed commands.
pub self_cmd: String,
}
fn exe_path() -> Option<String> {
let exe = std::env::current_exe().ok()?;
Some(exe.to_string_lossy().into_owned())
}
fn find_skill_dir_from(start: &str) -> Option<String> {
let mut dir = start.to_string();
loop {
if crate::util::exists(&jsp::join(&[&dir, "reference", "ios.md"])) {
return Some(dir);
}
let parent = jsp::dirname(&dir);
if parent == dir {
return None;
}
dir = parent;
}
}
fn provider_from_skill_dir(skill_dir: &str) -> Option<&'static str> {
// <root>/<harness>/skills/impeccable
let skills = jsp::dirname(skill_dir);
if jsp::basename(&skills) != "skills" {
return None;
}
let harness = jsp::basename(&jsp::dirname(&skills));
Some(match harness.as_str() {
".claude" => "claude-code",
".cursor" => "cursor",
".gemini" => "gemini",
".codex" => "codex",
".agents" => "agents",
".github" => "github",
".kiro" => "kiro",
".opencode" => "opencode",
".pi" => "pi",
".qoder" => "qoder",
".trae" => "trae",
".trae-cn" => "trae-cn",
".rovodev" => "rovo-dev",
".vibe" => "vibe",
".grok" => "grok",
".agent" => "antigravity",
".hermes" => "hermes",
_ => return None,
})
}
pub fn detect(env: &Env, cwd: &str) -> Provider {
let skill_dir = match env.get("IMPECCABLE_SKILL_DIR").filter(|v| !v.trim().is_empty()) {
Some(v) => Some(jsp::resolve(cwd, &[v.trim()])),
None => exe_path().and_then(|p| find_skill_dir_from(&jsp::dirname(&p))),
};
let id = match env.get("IMPECCABLE_PROVIDER_ID").filter(|v| !v.trim().is_empty()) {
Some(v) => v.trim().to_string(),
None => skill_dir
.as_deref()
.and_then(provider_from_skill_dir)
.unwrap_or(SOURCE_PROVIDER)
.to_string(),
};
let command_prefix = if id == "codex" { "$" } else { "/" }.to_string();
let command = format!("{}impeccable", command_prefix);
let self_cmd = match env.get("IMPECCABLE_SELF").filter(|v| !v.trim().is_empty()) {
Some(v) => v.trim().to_string(),
None => exe_path().unwrap_or_else(|| "impeccable".to_string()),
};
Provider { id, command_prefix, command, skill_dir, self_cmd }
}
impl Provider {
/// `<skill>/reference/<name>.md`
pub fn reference_path(&self, name: &str) -> Option<String> {
self.skill_dir.as_ref().map(|d| jsp::join(&[d, "reference", &format!("{}.md", name)]))
}
/// `<skill>/SKILL.md`
pub fn skill_md_path(&self) -> Option<String> {
self.skill_dir.as_ref().map(|d| jsp::join(&[d, "SKILL.md"]))
}
/// The command a directive should print for a sibling verb, in place of
/// `node <scripts>/<verb>.mjs`.
pub fn verb_cmd(&self, verb: &str) -> String {
format!("{} {}", self.self_cmd, verb)
}
}