Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bezel-bridle/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ fn schema_json(tool_name: &str, class_id: &str) -> Option<String> {
}
_ => 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}"}}"#))
}
59 changes: 43 additions & 16 deletions crates/bezel-bridle/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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();
Expand Down
63 changes: 58 additions & 5 deletions packages/jev-router/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -77,10 +78,62 @@ 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"
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

runHook postInstall
'';
Expand Down
Loading