From c406ff8efc70f984a35a6341af4989bfb7a1a895 Mon Sep 17 00:00:00 2001 From: VirtualMachinist Date: Wed, 23 Sep 2026 18:16:14 -0400 Subject: [PATCH 1/2] Run offline jev-router commands on the Rust binary. --- crates/bezel-bridle/src/catalog.rs | 4 +-- packages/jev-router/default.nix | 52 +++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/crates/bezel-bridle/src/catalog.rs b/crates/bezel-bridle/src/catalog.rs index bda1088..b5fba9b 100644 --- a/crates/bezel-bridle/src/catalog.rs +++ b/crates/bezel-bridle/src/catalog.rs @@ -84,6 +84,6 @@ fn schema_json(tool_name: &str, class_id: &str) -> Option { } _ => return None, }; - let (head, tail) = body.split_once('{')?; - Some(format!(r#"{head}{{"title":"{tool_name}",{tail}"#)) + let body = body.strip_suffix('}')?; + Some(format!(r#"{body},"title":"{tool_name}"}}"#)) } diff --git a/packages/jev-router/default.nix b/packages/jev-router/default.nix index f8bf21e..3f73c65 100644 --- a/packages/jev-router/default.nix +++ b/packages/jev-router/default.nix @@ -68,7 +68,8 @@ stdenv.mkDerivation { tar -xzf ${sdk} -C $out/lib/jev-router/node_modules/@typesafe-ai/sdk --strip-components=1 mkdir -p $out/bin - makeWrapper ${lib.getExe nodejs} $out/bin/jev-router \ + # Live commands stay on the JavaScript client. Offline commands run the Rust binary. + makeWrapper ${lib.getExe nodejs} $out/bin/jev-router-node \ --add-flags "$out/lib/jev-router/jev-router.mjs" \ --prefix NODE_PATH : "$out/lib/jev-router/node_modules" # Secrets: never --set TYPESAFE_API_KEY / getEnv at build. @@ -77,10 +78,51 @@ stdenv.mkDerivation { echo "bezel-bridle must be the Rust binary" >&2 exit 1 fi - empty=$(mktemp) - "$out/bin/bezel-bridle" --check --diff-file ${repoRoot}/packages/jev-router/testdata/empty.diff >"$empty" - grep -q '"approval":false' "$empty" - grep -q '"status":"no_diff"' "$empty" + cat > $out/bin/jev-router << EOF +#!/bin/sh +for arg in "\$@"; do + case "\$arg" in + hook|--catalog|--schema|--schema-dump|--check) + exec $out/bin/bezel-bridle "\$@" + ;; + esac +done +exec $out/bin/jev-router-node "\$@" +EOF + chmod 755 $out/bin/jev-router + + export NODE_PATH=$out/lib/jev-router/node_modules + export JEV_MODE=shadow + unset TYPESAFE_API_KEY || true + fixtures=${repoRoot}/packages/jev-router/testdata + match_node() { + set +e + ${lib.getExe nodejs} $out/lib/jev-router/jev-router.mjs "$@" > node.out + node_code=$? + $out/bin/jev-router "$@" > rust.out + rust_code=$? + set -e + if [ "$node_code" != "$rust_code" ] || ! cmp -s node.out rust.out; then + echo "jev-router $* does not match node ($node_code vs $rust_code)" >&2 + echo "--- node ---" >&2 + cat node.out >&2 + echo "--- rust ---" >&2 + cat rust.out >&2 + exit 1 + fi + } + match_node --catalog + match_node --schema Bash + match_node --schema + match_node --check --diff-file "$fixtures/empty.diff" + match_node --check --diff-file "$fixtures/skip-marker.diff" + JEV_MODE=shadow $out/bin/jev-router hook < "$fixtures/grok-pretool-bash.json" > hook.out + printf '%s\n' '{"decision":"defer","reason":"shadow"}' > hook.exp + cmp -s hook.out hook.exp + if grep -q '"decision":"allow"' rust.out hook.out; then + echo "offline jev-router must not allow" >&2 + exit 1 + fi runHook postInstall ''; From 3e064e6fe45126c0d207549fe01e272f26c1fff5 Mon Sep 17 00:00:00 2001 From: VirtualMachinist Date: Wed, 23 Sep 2026 18:25:28 -0400 Subject: [PATCH 2/2] Match jev-router hook stdout to the Node verdict. --- crates/bezel-bridle/src/cli.rs | 59 ++++++++++++++++++++++++--------- packages/jev-router/default.nix | 19 ++++++++--- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/crates/bezel-bridle/src/cli.rs b/crates/bezel-bridle/src/cli.rs index c5c48bd..3fff07a 100644 --- a/crates/bezel-bridle/src/cli.rs +++ b/crates/bezel-bridle/src/cli.rs @@ -3,9 +3,8 @@ use crate::catalog::{schema_dump_json, tiny_catalog_json}; use crate::check::{offline_check_json, run_offline_check}; -use crate::harness::hook_command; +use crate::facts::{gather, GatherOpts}; use crate::transport::{recorded_dir, FixtureTransport}; -use std::io::Read; use std::path::{Path, PathBuf}; pub const LIVE_COMMAND: &str = "facet request run --environment typesafe --no-record"; @@ -42,20 +41,7 @@ pub fn run(args: &[String]) -> (i32, String) { return (0, format!("bezel-bridle {}\n", env!("CARGO_PKG_VERSION"))); } if args.first().map(String::as_str) == Some("hook") { - let mut stdin = String::new(); - let _ = std::io::stdin().read_to_string(&mut stdin); - let owned: Vec<(String, String)> = [ - "JEV_MODE", - "JEV_BYPASS", - "JEV_PERMISSION_MODE", - "JEV_TYPED_CALL_MODE", - "TYPESAFE_API_KEY", - ] - .into_iter() - .map(|key| (key.to_string(), std::env::var(key).unwrap_or_default())) - .collect(); - let pairs: Vec<(&str, &str)> = owned.iter().map(|(key, value)| (key.as_str(), value.as_str())).collect(); - return hook_command(&stdin, &pairs); + return hook_verdict(); } if args.iter().any(|arg| arg == "--catalog") { return (0, format!("{}\n", tiny_catalog_json())); @@ -89,6 +75,47 @@ mod tests { } } +/// `jev-router hook` with no key. Same bytes as the JavaScript shadow verdict. +fn hook_verdict() -> (i32, String) { + let active = std::env::var("JEV_MODE") + .map(|value| value.eq_ignore_ascii_case("active")) + .unwrap_or(false); + let repo = std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")); + let gathered = gather(&GatherOpts { + repo, + base: String::new(), + diff_file: None, + diff_text: None, + write: None, + }); + let present = if gathered.diff.present { "true" } else { "false" }; + let caps = present; + let available = if gathered.diff.present { + "\"check\",\"review\"" + } else { + "" + }; + let catalog = tiny_catalog_json(); + let policy = crate::policy::LOOP_STOP_POLICY_ID; + let route = crate::policy::ROUTING_POLICY_ID; + let facts = format!( + r#"{{"diffPresent":{present},"diffSource":"{}","available":[{available}],"capabilities":{{"check":{caps},"review":{caps}}}}}"#, + gathered.diff.source, + ); + if !active { + let body = format!( + r#"{{"ok":true,"mode":"shadow","bypass":false,"missingKey":true,"choice":"unclassified","gate":"auto","blocked":false,"exec":true,"hitl":false,"autoRetry":false,"autoPromote":false,"intent":"hook","stepDigest":"","loop":{{"policy":"{policy}","selected":"unclassified","outcome":"cannot_tell","reason":"missing_key","hitl":false,"autoRetry":false,"autoPromote":false,"shadow":true,"blocked":false}},"workflow":{{"choice":"unclassified","outcome":"cannot_tell","reason":"missing_key","shadow":true,"blocked":false}},"facts":{facts},"routingPolicy":"{route}","loopStopPolicy":"{policy}","catalog":{catalog}}}"# + ); + return (0, format!("{body}\n")); + } + let _facts = facts; + let body = format!( + r#"{{"ok":false,"mode":"active","missingKey":true,"choice":"escalate","gate":"hold","blocked":true,"exec":false,"hitl":true,"autoRetry":false,"autoPromote":false,"intent":"hook","stepDigest":"","loop":{{"policy":"{policy}","selected":"cannot_tell","outcome":"cannot_tell","reason":"missing_key","confidence":0,"selectedProb":0,"margin":0,"hitl":true,"autoRetry":false,"autoPromote":false}},"catalog":{catalog}}}"# + ); + let _route = route; + (2, format!("{body}\n")) +} + fn diff_argument(args: &[String]) -> String { let Some(index) = args.iter().position(|arg| arg == "--diff-file") else { return String::new(); diff --git a/packages/jev-router/default.nix b/packages/jev-router/default.nix index 3f73c65..f4621fc 100644 --- a/packages/jev-router/default.nix +++ b/packages/jev-router/default.nix @@ -116,10 +116,21 @@ EOF match_node --schema match_node --check --diff-file "$fixtures/empty.diff" match_node --check --diff-file "$fixtures/skip-marker.diff" - JEV_MODE=shadow $out/bin/jev-router hook < "$fixtures/grok-pretool-bash.json" > hook.out - printf '%s\n' '{"decision":"defer","reason":"shadow"}' > hook.exp - cmp -s hook.out hook.exp - if grep -q '"decision":"allow"' rust.out hook.out; then + set +e + ${lib.getExe nodejs} $out/lib/jev-router/jev-router.mjs hook < "$fixtures/grok-pretool-bash.json" > node-hook.out + node_hook=$? + $out/bin/jev-router hook < "$fixtures/grok-pretool-bash.json" > rust-hook.out + rust_hook=$? + set -e + if [ "$node_hook" != "$rust_hook" ] || ! cmp -s node-hook.out rust-hook.out; then + echo "jev-router hook does not match node ($node_hook vs $rust_hook)" >&2 + echo "--- node ---" >&2 + cat node-hook.out >&2 + echo "--- rust ---" >&2 + cat rust-hook.out >&2 + exit 1 + fi + if grep -q '"decision":"allow"' rust.out rust-hook.out; then echo "offline jev-router must not allow" >&2 exit 1 fi