Initial public release

Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
Evan Whittaker
2026-10-17 12:08:30 -05:00
commit 09a7de2520
1730 changed files with 412952 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
//! Ask resolution — deny blocks, and an ask round-trips as reject, once or always.
use luxe_core::engine::{Action, Engine};
use luxe_core::hook::HookPoint;
use luxe_core::permission::{
AskResolution, PermValue, PermissionAction, PermissionConfig, PermissionEngine,
PermissionReply, PermissionSettings,
};
use luxe_core::session::{JsonlSessionStore, SessionStore};
use luxe_core::HookRegistry;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::sync::Mutex;
use crate::fixtures::*;
#[tokio::test]
async fn deny_blocks_the_tool() {
let (tx, mut rx, runs) =
spawn(bash_policy(PermissionAction::Deny, AskResolution::Prompt)).await;
let prompts = run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(prompts, 0, "deny should not prompt");
assert_eq!(runs.load(Ordering::SeqCst), 0, "denied tool must not run");
}
#[tokio::test]
async fn ask_reject_blocks_the_tool() {
let (tx, mut rx, runs) = spawn(bash_policy(PermissionAction::Ask, AskResolution::Prompt)).await;
let prompts = run_turn_answering(&tx, &mut rx, "go", Some(PermissionReply::Reject)).await;
assert_eq!(prompts, 1, "ask should prompt once");
assert_eq!(runs.load(Ordering::SeqCst), 0, "rejected tool must not run");
}
#[tokio::test]
async fn ask_once_allows_the_tool() {
let (tx, mut rx, runs) = spawn(bash_policy(PermissionAction::Ask, AskResolution::Prompt)).await;
let prompts = run_turn_answering(&tx, &mut rx, "go", Some(PermissionReply::Once)).await;
assert_eq!(prompts, 1);
assert_eq!(runs.load(Ordering::SeqCst), 1, "approved tool must run once");
}
#[tokio::test]
async fn ask_always_persists_across_the_session() {
let (tx, mut rx, runs) = spawn(bash_policy(PermissionAction::Ask, AskResolution::Prompt)).await;
// First turn: approve "always" — should prompt once and run.
let first = run_turn_answering(&tx, &mut rx, "go", Some(PermissionReply::Always)).await;
assert_eq!(first, 1);
// Second turn, same command family: no prompt, auto-allowed by the session approval.
let second = run_turn_answering(&tx, &mut rx, "again", Some(PermissionReply::Reject)).await;
assert_eq!(second, 0, "always-approved family should not prompt again");
assert_eq!(runs.load(Ordering::SeqCst), 2, "both calls run");
}
#[tokio::test]
async fn ask_prompt_fires_the_observational_permission_hook_points() {
let (tx, mut rx, runs, points) = spawn_with_hooks(
bash_policy(PermissionAction::Ask, AskResolution::Prompt),
HookRegistry::new(),
)
.await;
let prompts = run_turn_answering(&tx, &mut rx, "go", Some(PermissionReply::Once)).await;
assert_eq!(prompts, 1);
assert_eq!(runs.load(Ordering::SeqCst), 1);
// The interactive prompt must bracket the wait with requested → resolved so observers (e.g.
// the Herdr integration) can flip blocked → working around the human decision.
assert_eq!(
*points.lock().await,
vec![HookPoint::PermissionRequested, HookPoint::PermissionResolved],
);
}
#[tokio::test]
async fn auto_allow_fires_no_permission_hook_points() {
let (tx, mut rx, runs, points) = spawn_with_hooks(
bash_policy(PermissionAction::Ask, AskResolution::AutoAllow),
HookRegistry::new(),
)
.await;
let prompts = run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(prompts, 0);
assert_eq!(runs.load(Ordering::SeqCst), 1);
// No interactive prompt → the turn never blocks → no permission hook points fire.
assert!(points.lock().await.is_empty(), "auto-allow must not fire permission hook points");
}
#[tokio::test]
async fn auto_allow_runs_without_prompting() {
let (tx, mut rx, runs) =
spawn(bash_policy(PermissionAction::Ask, AskResolution::AutoAllow)).await;
let prompts = run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(prompts, 0, "auto-allow never prompts");
assert_eq!(runs.load(Ordering::SeqCst), 1, "auto-allowed tool runs");
}
/// "Allow always" writes the approval to the permissions file, so a *new* engine over that file
/// runs the same call without prompting — the session-scoped tier can't do that.
#[tokio::test]
async fn always_persist_writes_a_rule_that_survives_a_restart() {
let tmp = tempfile::tempdir().unwrap();
let permissions_path = tmp.path().join("permissions.json");
// First run: bash asks, the user saves the approval.
let store_dir = tmp.path().join("sessions-a");
let mut store = JsonlSessionStore::new(store_dir);
let session_id = store.create("/tmp/perm-persist").unwrap();
let runs = Arc::new(AtomicUsize::new(0));
let provider = Arc::new(BashCaller::running("echo hi"));
let (tx, mut rx) = Engine::spawn(config(
store,
session_id,
provider,
runs.clone(),
Arc::new(Mutex::new(Vec::new())),
bash_policy(PermissionAction::Ask, AskResolution::Prompt),
HookRegistry::new(),
Some(permissions_path.clone()),
));
let prompts =
run_turn_answering(&tx, &mut rx, "go", Some(PermissionReply::AlwaysPersist)).await;
assert_eq!(prompts, 1);
assert_eq!(runs.load(Ordering::SeqCst), 1);
let _ = tx.send(Action::Close).await;
// The file now carries the approval.
let saved = std::fs::read_to_string(&permissions_path).expect("the approval was written");
let file = luxe_core::permission::PermissionsFile::from_json(&saved).expect("valid json");
let persisted = file.ruleset();
assert!(!persisted.is_empty(), "a rule was recorded: {saved}");
// Second run: the same policy plus the persisted rules — no prompt this time.
let mut store = JsonlSessionStore::new(tmp.path().join("sessions-b"));
let session_id = store.create("/tmp/perm-persist").unwrap();
let runs = Arc::new(AtomicUsize::new(0));
let provider = Arc::new(BashCaller::running("echo hi"));
let mut global = PermissionConfig::Keyed(
[("bash".to_string(), PermValue::Scalar(PermissionAction::Ask))].into_iter().collect(),
)
.to_ruleset();
global.extend(persisted);
let (tx, mut rx) = Engine::spawn(config(
store,
session_id,
provider,
runs.clone(),
Arc::new(Mutex::new(Vec::new())),
PermissionSettings {
engine: PermissionEngine::new(global, vec![], false),
ask: AskResolution::Prompt,
},
HookRegistry::new(),
Some(permissions_path),
));
let prompts = run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(prompts, 0, "the saved approval applies without asking again");
assert_eq!(runs.load(Ordering::SeqCst), 1, "and the tool still runs");
let _ = tx.send(Action::Close).await;
}

View File

@@ -0,0 +1,368 @@
//! Fixtures — the scripted provider, the counting tool and the engine spawners the gate scenarios share.
use futures::stream;
use futures::stream::BoxStream;
use luxe_core::engine::{Action, Engine, EngineConfig};
use luxe_core::event::Event;
use luxe_core::hook::{Hook, HookCtx, HookEvent, HookOutcome, HookPoint};
use luxe_core::message::ContentBlock;
use luxe_core::permission::{
AskResolution, PermValue, PermissionAction, PermissionConfig, PermissionEngine,
PermissionReply, PermissionSettings,
};
use luxe_core::provider::{
CompletionRequest, ModelInfo, Provider, ProviderError, ProviderEvent, StopReason,
};
use luxe_core::session::{JsonlSessionStore, SessionStore};
use luxe_core::tool::{Tool, ToolContext, ToolRegistry, ToolResultData};
use luxe_core::HookRegistry;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::sync::Mutex;
/// A `bash`-named tool that records how many times it actually ran, and with what.
pub(crate) struct CountingBash {
runs: Arc<AtomicUsize>,
seen: Arc<Mutex<Vec<serde_json::Value>>>,
}
#[async_trait::async_trait]
impl Tool for CountingBash {
fn name(&self) -> &str {
"bash"
}
fn schema(&self) -> luxe_core::provider::ToolSchema {
luxe_core::provider::ToolSchema {
name: "bash".to_string(),
description: "Run a command.".to_string(),
parameters: serde_json::json!({
"type": "object",
"properties": { "command": { "type": "string" } },
"required": ["command"],
}),
}
}
async fn run(&self, args: serde_json::Value, _cx: &ToolContext) -> ToolResultData {
self.runs.fetch_add(1, Ordering::SeqCst);
self.seen.lock().await.push(args);
ToolResultData {
content: vec![ContentBlock::Text("BASH_RAN".to_string())],
is_error: false,
}
}
}
/// A `BeforeToolUse` hook that answers with whatever the test handed it.
///
/// Stands in for a command hook without a subprocess, so these run on Windows too — what is under
/// test is the engine's half of the contract, not `sh`.
pub(crate) struct ScriptedGuard {
pub(crate) outcome: HookOutcome,
}
#[async_trait::async_trait]
impl Hook for ScriptedGuard {
fn name(&self) -> &'static str {
"scripted-guard"
}
fn interest(&self) -> &[HookPoint] {
&[HookPoint::BeforeToolUse]
}
async fn on(&self, _point: HookPoint, _ev: &HookEvent, _cx: &HookCtx) -> HookOutcome {
self.outcome.clone()
}
}
/// Scripts each turn: odd provider calls emit a `bash` tool call; even calls end the turn.
pub(crate) struct BashCaller {
calls: Arc<Mutex<usize>>,
command: String,
}
impl BashCaller {
pub(crate) fn running(command: &str) -> Self {
Self { calls: Arc::new(Mutex::new(0)), command: command.to_string() }
}
}
#[async_trait::async_trait]
impl Provider for BashCaller {
fn name(&self) -> &str {
"bash-caller"
}
async fn stream(
&self,
_req: CompletionRequest,
_cancel: tokio::sync::watch::Receiver<bool>,
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
let n = {
let mut c = self.calls.lock().await;
*c += 1;
*c
};
let events = if n % 2 == 1 {
vec![
ProviderEvent::ToolCallStart {
index: 0,
id: format!("call_{n}"),
name: "bash".to_string(),
},
ProviderEvent::ToolCallDelta {
index: 0,
json_delta: serde_json::json!({ "command": self.command }).to_string(),
},
ProviderEvent::ToolCallEnd { index: 0 },
ProviderEvent::Done { reason: StopReason::ToolCall },
]
} else {
vec![
ProviderEvent::TextStart { index: 0 },
ProviderEvent::TextDelta { index: 0, delta: "done".to_string() },
ProviderEvent::TextEnd { index: 0 },
ProviderEvent::Done { reason: StopReason::EndTurn },
]
};
Ok(Box::pin(stream::iter(events)))
}
}
/// Records every hook point it sees, so a test can assert which lifecycle events actually fired.
pub(crate) struct RecordingHook {
points: Arc<Mutex<Vec<HookPoint>>>,
}
#[async_trait::async_trait]
impl Hook for RecordingHook {
fn name(&self) -> &'static str {
"recording"
}
fn interest(&self) -> &[HookPoint] {
&[HookPoint::PermissionRequested, HookPoint::PermissionResolved]
}
async fn on(&self, point: HookPoint, _ev: &HookEvent, _cx: &HookCtx) -> HookOutcome {
self.points.lock().await.push(point);
HookOutcome::Continue
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn config(
store: JsonlSessionStore,
session_id: luxe_core::session::SessionId,
provider: Arc<dyn Provider + Send + Sync>,
runs: Arc<AtomicUsize>,
seen: Arc<Mutex<Vec<serde_json::Value>>>,
permission: PermissionSettings,
hooks: HookRegistry,
permissions_file: Option<std::path::PathBuf>,
) -> EngineConfig {
let mut tools = ToolRegistry::new();
tools.register(Arc::new(CountingBash { runs, seen }));
EngineConfig {
session_store: std::sync::RwLock::new(Box::new(store)),
provider,
providers: std::collections::HashMap::new(),
model: ModelInfo::default(),
tool_registry: tools,
hook_registry: hooks,
session_id,
system_prompt: None,
todo_store: None,
compaction_settings: luxe_core::compaction::CompactionSettings::default(),
permission,
modes: Default::default(),
reviewer: None,
formatter_registry: None,
spend_sink: None,
guided_review_project_root: None,
invoked_from: None,
external_work: None,
review_context_files: Vec::new(),
review_exclude: Vec::new(),
remember_runtime_state: false,
cache_warming: luxe_core::cache_warming::CacheWarming::Off,
review_insight_provider: None,
github: None,
permissions_file,
max_tool_iterations: Some(8),
startup_notices: Vec::new(),
action_tx: None,
}
}
/// Build a policy with a single global `bash` rule at `action`.
pub(crate) fn bash_policy(action: PermissionAction, ask: AskResolution) -> PermissionSettings {
let global = PermissionConfig::Keyed(
[("bash".to_string(), PermValue::Scalar(action))].into_iter().collect(),
)
.to_ruleset();
PermissionSettings { engine: PermissionEngine::new(global, vec![], false), ask }
}
pub(crate) async fn spawn(
permission: PermissionSettings,
) -> (tokio::sync::mpsc::Sender<Action>, tokio::sync::mpsc::Receiver<Event>, Arc<AtomicUsize>) {
let (tx, rx, runs, _points) = spawn_with_hooks(permission, HookRegistry::new()).await;
(tx, rx, runs)
}
/// Spawn behind a `BeforeToolUse` guard, returning the arguments the tool actually received.
pub(crate) async fn spawn_guarded(
permission: PermissionSettings,
guard: HookOutcome,
) -> (
tokio::sync::mpsc::Sender<Action>,
tokio::sync::mpsc::Receiver<Event>,
Arc<AtomicUsize>,
Arc<Mutex<Vec<serde_json::Value>>>,
) {
let mut hooks = HookRegistry::new();
hooks.register(Arc::new(ScriptedGuard { outcome: guard }));
let seen = Arc::new(Mutex::new(Vec::new()));
let (tx, rx, runs, _points) = spawn_inner(permission, hooks, seen.clone()).await;
(tx, rx, runs, seen)
}
/// Like [`spawn`] but injects a hook registry and returns the recording hook's shared log, so a
/// test can assert which hook points fired during the run.
pub(crate) async fn spawn_with_hooks(
permission: PermissionSettings,
hooks: HookRegistry,
) -> (
tokio::sync::mpsc::Sender<Action>,
tokio::sync::mpsc::Receiver<Event>,
Arc<AtomicUsize>,
Arc<Mutex<Vec<HookPoint>>>,
) {
spawn_inner(permission, hooks, Arc::new(Mutex::new(Vec::new()))).await
}
/// Like [`spawn`], but the scripted model runs `command` instead of `echo hi`.
pub(crate) async fn spawn_running(
permission: PermissionSettings,
command: &str,
) -> (tokio::sync::mpsc::Sender<Action>, tokio::sync::mpsc::Receiver<Event>, Arc<AtomicUsize>) {
let (tx, rx, runs, _points) = spawn_inner_running(
permission,
HookRegistry::new(),
Arc::new(Mutex::new(Vec::new())),
command,
)
.await;
(tx, rx, runs)
}
pub(crate) async fn spawn_inner(
permission: PermissionSettings,
hooks: HookRegistry,
seen: Arc<Mutex<Vec<serde_json::Value>>>,
) -> (
tokio::sync::mpsc::Sender<Action>,
tokio::sync::mpsc::Receiver<Event>,
Arc<AtomicUsize>,
Arc<Mutex<Vec<HookPoint>>>,
) {
spawn_inner_running(permission, hooks, seen, "echo hi").await
}
pub(crate) async fn spawn_inner_running(
permission: PermissionSettings,
mut hooks: HookRegistry,
seen: Arc<Mutex<Vec<serde_json::Value>>>,
command: &str,
) -> (
tokio::sync::mpsc::Sender<Action>,
tokio::sync::mpsc::Receiver<Event>,
Arc<AtomicUsize>,
Arc<Mutex<Vec<HookPoint>>>,
) {
let points = Arc::new(Mutex::new(Vec::new()));
hooks.register(Arc::new(RecordingHook { points: points.clone() }));
let tmp = tempfile::tempdir().unwrap();
// Leak the tempdir so the session store outlives the test body without a guard field.
let path = tmp.keep();
let mut store = JsonlSessionStore::new(path.join("sessions"));
let session_id = store.create("/tmp/perm-test").unwrap();
let runs = Arc::new(AtomicUsize::new(0));
let provider = Arc::new(BashCaller::running(command));
let cfg = config(store, session_id, provider, runs.clone(), seen, permission, hooks, None);
let (tx, rx) = Engine::spawn(cfg);
(tx, rx, runs, points)
}
/// Drive one turn; auto-answer any permission prompt with `reply`. Returns how many prompts fired.
pub(crate) async fn run_turn_answering(
tx: &tokio::sync::mpsc::Sender<Action>,
rx: &mut tokio::sync::mpsc::Receiver<Event>,
prompt: &str,
reply: Option<PermissionReply>,
) -> usize {
tx.send(Action::SubmitUserMessage {
content: prompt.to_string(),
images: vec![],
remediation: None,
})
.await
.unwrap();
let mut prompts = 0;
loop {
match tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv()).await {
Ok(Some(Event::PermissionRequested { id, .. })) => {
prompts += 1;
if let Some(reply) = reply {
tx.send(Action::RespondPermission { id, reply }).await.unwrap();
}
}
Ok(Some(Event::TurnEnded { .. })) => break,
Ok(Some(_)) => {}
Ok(None) => panic!("event channel closed"),
Err(_) => panic!("timed out waiting for turn to end"),
}
}
prompts
}
/// Like [`run_turn_answering`] but never answers, and hands back the tool results the model read.
pub(crate) async fn run_turn_collecting_results(
tx: &tokio::sync::mpsc::Sender<Action>,
rx: &mut tokio::sync::mpsc::Receiver<Event>,
prompt: &str,
) -> (usize, Vec<String>) {
use luxe_core::message::{ContentBlock, ToolOutput};
tx.send(Action::SubmitUserMessage {
content: prompt.to_string(),
images: vec![],
remediation: None,
})
.await
.unwrap();
let (mut prompts, mut results) = (0, Vec::new());
loop {
match tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv()).await {
Ok(Some(Event::PermissionRequested { .. })) => prompts += 1,
Ok(Some(Event::MessageCommitted { message, .. })) => {
for block in &message.content {
let ContentBlock::ToolResult { content, .. } = block else { continue };
for output in content {
if let ToolOutput::Text(text) = output {
results.push(text.clone());
}
}
}
}
Ok(Some(Event::TurnEnded { .. })) => break,
Ok(Some(_)) => {}
Ok(None) => panic!("event channel closed"),
Err(_) => panic!("timed out waiting for turn to end"),
}
}
(prompts, results)
}

View File

@@ -0,0 +1,210 @@
//! Hooks and guardrails — a hook's rewrite and escalation reach the gate as arguments and one more check.
use luxe_core::engine::{Action, Engine};
use luxe_core::event::Event;
use luxe_core::hook::HookOutcome;
use luxe_core::message::ContentBlock;
use luxe_core::permission::{
AskResolution, PermValue, PermissionAction, PermissionConfig, PermissionEngine,
PermissionReply, PermissionSettings,
};
use luxe_core::session::{JsonlSessionStore, SessionStore};
use luxe_core::tool::{Tool, ToolContext, ToolRegistry, ToolResultData};
use luxe_core::HookRegistry;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::sync::Mutex;
use crate::fixtures::*;
/// A hook's rewrite is what the permission engine judges, not what the model asked for.
///
/// This is the confused-deputy case, and it is the reason the patch lands above the gate rather
/// than below it. Every comparable harness applies the rewrite after its checks — one documents
/// outright that it does not re-validate — so a guard script with a bug, or a compromised one,
/// gets its substitution executed against rules that cleared a different command. Here the model
/// asks for `echo hi`, the hook turns it into a root wipe, and the catastrophic deny-list that
/// never saw the original still stops it.
#[tokio::test]
async fn a_hook_rewrite_is_what_the_permission_engine_judges() {
let (tx, mut rx, runs, _seen) = spawn_guarded(
bash_policy(PermissionAction::Allow, AskResolution::Prompt),
HookOutcome::Modify { args: serde_json::json!({ "command": "rm -rf /" }), ask: None },
)
.await;
let prompts = run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(prompts, 0, "a denial does not prompt");
assert_eq!(runs.load(Ordering::SeqCst), 0, "the rewritten command must be gated, not run");
}
/// The tool receives the rewritten arguments, and keeps the ones the patch left alone.
#[tokio::test]
async fn a_hook_rewrite_reaches_the_tool_and_spares_the_keys_it_omits() {
let (tx, mut rx, runs, seen) = spawn_guarded(
bash_policy(PermissionAction::Allow, AskResolution::Prompt),
HookOutcome::Modify { args: serde_json::json!({ "command": "echo safe" }), ask: None },
)
.await;
run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(runs.load(Ordering::SeqCst), 1);
let seen = seen.lock().await;
assert_eq!(seen[0]["command"], "echo safe", "the patch reached the tool");
}
/// An escalation from a hook raises the ordinary permission prompt, and a reject stops the call.
///
/// The policy here allows `bash` outright, so the prompt can only have come from the hook — and
/// it is the same prompt, with the same replies, rather than a second path that would have to
/// reimplement them.
#[tokio::test]
async fn a_hook_ask_raises_the_ordinary_prompt() {
let (tx, mut rx, runs, _seen) = spawn_guarded(
bash_policy(PermissionAction::Allow, AskResolution::Prompt),
HookOutcome::Ask("the classifier was not sure".to_string()),
)
.await;
let prompts = run_turn_answering(&tx, &mut rx, "go", Some(PermissionReply::Reject)).await;
assert_eq!(prompts, 1, "the hook's ask reached the user");
assert_eq!(runs.load(Ordering::SeqCst), 0, "a rejected call does not run");
}
/// Unattended, a hook's escalation collapses like every other `ask` instead of wedging the run.
///
/// Free, and that is the point: the escalation is a permission check, so `AskResolution` already
/// governs it. A parallel prompt path would have had to learn this separately, and the failure if
/// it did not would be a headless run blocked forever on a question nobody can see.
#[tokio::test]
async fn a_hook_ask_collapses_in_an_unattended_run() {
let (tx, mut rx, runs, _seen) = spawn_guarded(
bash_policy(PermissionAction::Allow, AskResolution::AutoAllow),
HookOutcome::Ask("the classifier was not sure".to_string()),
)
.await;
let prompts = run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(prompts, 0, "nothing can answer a prompt here");
assert_eq!(runs.load(Ordering::SeqCst), 1, "the call proceeds rather than hanging");
}
/// Unattended and told to refuse, the same escalation is denied — and the model is told why.
///
/// The counterpart to the test above, and the reason `--on-ask deny` exists: `AskResolution` is
/// already the one place that settles this, so choosing the other answer needs no second path. What
/// the refusal *says* is the part worth pinning: a model told only "denied" retries the same call,
/// so the message has to name the condition (nobody is here) and the way out (another route).
#[tokio::test]
async fn a_hook_ask_is_refused_when_an_unattended_run_says_deny() {
let (tx, mut rx, runs, _seen) = spawn_guarded(
bash_policy(PermissionAction::Allow, AskResolution::AutoDeny),
HookOutcome::Ask("the classifier was not sure".to_string()),
)
.await;
let (prompts, results) = run_turn_collecting_results(&tx, &mut rx, "go").await;
assert_eq!(prompts, 0, "nothing can answer a prompt here");
assert_eq!(runs.load(Ordering::SeqCst), 0, "and the call does not run");
let denial = results.iter().find(|text| text.contains("denied")).unwrap_or_else(|| {
panic!("the model has to be told the call was refused; got {results:?}")
});
assert!(denial.contains("unattended"), "the model must learn why: {denial}");
assert!(denial.contains("--on-ask deny"), "and what set it: {denial}");
assert!(
denial.contains("the classifier was not sure"),
"the hook's reason rides along, as it does on the card: {denial}"
);
}
/// A rewrite that lands on an `ask` rule asks — without the hook having to say so.
///
/// The composition that other harnesses need a dedicated field for falls out of the ordering:
/// because the gate judges the rewritten call, a substitution into force-push territory raises the
/// prompt the substituted command deserves, and the reader confirms what will actually run.
#[tokio::test]
async fn a_rewrite_into_riskier_territory_asks_on_its_own() {
let (tx, mut rx, runs, seen) = spawn_guarded(
bash_policy(PermissionAction::Allow, AskResolution::Prompt),
HookOutcome::Modify {
args: serde_json::json!({ "command": "git push --force origin main" }),
ask: None,
},
)
.await;
let prompts = run_turn_answering(&tx, &mut rx, "go", Some(PermissionReply::Once)).await;
assert_eq!(
prompts, 1,
"the rewritten command hit the force-push rule the original never would"
);
assert_eq!(runs.load(Ordering::SeqCst), 1);
assert_eq!(seen.lock().await[0]["command"], "git push --force origin main");
}
/// A hook's escalation offers nothing to remember, and a remembering reply saves nothing.
///
/// A hook judges each call afresh, so "always" had nothing it could honestly promise: it approved
/// the tool's own `bash` rule, wrote that rule to the global permissions file, and the hook asked
/// again on the very next call. Found driving the TUI against a real `hooks.json`.
#[tokio::test]
async fn a_hook_ask_offers_nothing_to_remember() {
let tmp = tempfile::tempdir().unwrap();
let permissions_path = tmp.path().join("permissions.json");
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
let session_id = store.create("/tmp/hook-ask-always").unwrap();
let runs = Arc::new(AtomicUsize::new(0));
let mut hooks = HookRegistry::new();
hooks.register(Arc::new(ScriptedGuard {
outcome: HookOutcome::Ask("the classifier was not sure".to_string()),
}));
let (tx, mut rx) = Engine::spawn(config(
store,
session_id,
Arc::new(BashCaller::running("echo hi")),
runs.clone(),
Arc::new(Mutex::new(Vec::new())),
bash_policy(PermissionAction::Allow, AskResolution::Prompt),
hooks,
Some(permissions_path.clone()),
));
for turn in ["go", "again"] {
tx.send(Action::SubmitUserMessage {
content: turn.to_string(),
images: vec![],
remediation: None,
})
.await
.unwrap();
let mut prompts = 0;
loop {
match tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv()).await {
Ok(Some(Event::PermissionRequested { id, always_patterns, .. })) => {
prompts += 1;
assert!(always_patterns.is_empty(), "offered {always_patterns:?}");
tx.send(Action::RespondPermission {
id,
reply: PermissionReply::AlwaysPersist,
})
.await
.unwrap();
}
Ok(Some(Event::Notice(notice))) => {
assert!(!notice.contains("saved"), "claimed a save: {notice}")
}
Ok(Some(Event::TurnEnded { .. })) => break,
Ok(Some(_)) => {}
Ok(None) => panic!("event channel closed"),
Err(_) => panic!("timed out waiting for turn to end"),
}
}
assert_eq!(prompts, 1, "the hook asks on every call, turn `{turn}`");
}
assert_eq!(runs.load(Ordering::SeqCst), 2, "each approval still runs its call");
assert!(!permissions_path.exists(), "nothing was written to the global file");
let _ = tx.send(Action::Close).await;
}
mod read_hook;

View File

@@ -0,0 +1,129 @@
use super::*;
use luxe_core::tool::FileReadHook;
use std::path::Path;
type ObservedReads = Arc<Mutex<Vec<(String, PermissionAction)>>>;
struct ReadProbeBash {
observed: ObservedReads,
}
#[async_trait::async_trait]
impl Tool for ReadProbeBash {
fn name(&self) -> &str {
"bash"
}
fn schema(&self) -> luxe_core::provider::ToolSchema {
luxe_core::provider::ToolSchema {
name: "bash".into(),
description: "Probe discovered reads.".into(),
parameters: serde_json::json!({"type": "object", "properties": {"command": {"type": "string"}}}),
}
}
async fn run(&self, _: serde_json::Value, _: &ToolContext) -> ToolResultData {
ToolResultData { content: vec![ContentBlock::Text("legacy-run".into())], is_error: false }
}
async fn run_with_read_hook(
&self,
_: serde_json::Value,
_: &ToolContext,
read_hook: &dyn FileReadHook,
) -> ToolResultData {
for path in [".env", ".env.example", "review.md"] {
self.observed.lock().await.push((path.into(), read_hook.decision(Path::new(path))));
}
ToolResultData {
content: vec![ContentBlock::Text("read-hook-run".into())],
is_error: false,
}
}
}
/// Bypassing the read-hook run entry would lose env denial; a discovered file that asks resolves
/// the way this run resolves `ask`, so headless auto-allow does not silently withhold it.
#[tokio::test]
async fn bounded_tool_dispatch_passes_the_current_read_policy_snapshot() {
for (ask, review) in [
(AskResolution::AutoAllow, PermissionAction::Allow),
(AskResolution::AutoDeny, PermissionAction::Deny),
(AskResolution::Prompt, PermissionAction::Ask),
] {
assert_eq!(
discovered_reads(ask).await,
[
(".env".into(), PermissionAction::Deny),
(".env.example".into(), PermissionAction::Allow),
("review.md".into(), review),
],
"{ask:?}"
);
}
}
async fn discovered_reads(ask: AskResolution) -> Vec<(String, PermissionAction)> {
let tmp = tempfile::tempdir().unwrap();
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
let session_id = store.create("/tmp/read-hook-test").unwrap();
let provider = Arc::new(BashCaller::running("echo ok"));
let global = PermissionConfig::Keyed(
[(
"read".into(),
PermValue::Map([("review.md".into(), PermissionAction::Ask)].into_iter().collect()),
)]
.into_iter()
.collect(),
)
.to_ruleset();
let permission =
PermissionSettings { engine: PermissionEngine::new(global, vec![], false), ask };
let mut cfg = config(
store,
session_id,
provider,
Arc::new(AtomicUsize::new(0)),
Arc::new(Mutex::new(Vec::new())),
permission,
HookRegistry::new(),
None,
);
let observed = Arc::new(Mutex::new(Vec::new()));
let mut registry = ToolRegistry::new();
registry.register(Arc::new(ReadProbeBash { observed: observed.clone() }));
cfg.tool_registry = registry;
let (tx, mut rx) = Engine::spawn(cfg);
let (_, results) = run_turn_collecting_results(&tx, &mut rx, "probe reads").await;
assert!(results.iter().any(|result| result.contains("read-hook-run")), "{results:?}");
let observed = observed.lock().await.clone();
observed
}
/// Yolo opts out of default guards without silently discarding a user's read-specific denial.
#[test]
fn the_read_hook_honors_yolo_and_explicit_user_denials() {
let yolo = PermissionEngine::assemble(
luxe_core::permission::PermissionMode::Yolo,
vec![],
vec![],
false,
);
assert_eq!(yolo.decision(Path::new(".env")), PermissionAction::Allow);
let global = PermissionConfig::Keyed(
[(
"read".into(),
PermValue::Map([("blocked.txt".into(), PermissionAction::Deny)].into_iter().collect()),
)]
.into_iter()
.collect(),
)
.to_ruleset();
let guarded = PermissionEngine::assemble(
luxe_core::permission::PermissionMode::Yolo,
global,
vec![],
false,
);
assert_eq!(guarded.decision(Path::new("blocked.txt")), PermissionAction::Deny);
}

View File

@@ -0,0 +1,17 @@
//! Integration: the permission engine gates tool dispatch end-to-end.
//!
//! Drives the real engine with a scripted provider that calls a `bash`-named tool, and asserts:
//! deny blocks the tool (model sees a denial), and the ask round-trip honors reject / once /
//! always (session-persisted) — exercising `Event::PermissionRequested` +
//! `Action::RespondPermission` and the `select!`-serviced turn.
//!
//! It also covers the half of the chain that runs *above* the engine: a `BeforeToolUse` hook's
//! rewrite and escalation, which reach the gate as arguments and as one more check rather than as
//! a second decision path.
#![allow(clippy::unwrap_used)]
mod ask_resolution;
mod fixtures;
mod hooks_and_guardrails;
mod rules_and_modes;

View File

@@ -0,0 +1,136 @@
//! Rules and modes — what an unattended run refuses, and what an allow rule or `--yolo` changes.
use luxe_core::engine::Action;
use luxe_core::permission::{
AskResolution, PermValue, PermissionAction, PermissionConfig, PermissionEngine,
PermissionReply, PermissionSettings,
};
use std::sync::atomic::Ordering;
use crate::fixtures::*;
/// The default policy, resolving `ask` the way an unattended run does.
fn unattended_defaults() -> PermissionSettings {
PermissionSettings { engine: PermissionEngine::permissive(), ask: AskResolution::AutoAllow }
}
/// Nobody is watching, so a merge is refused rather than approved on nobody's behalf — and the
/// model is told why, because a bare "denied" gets the same call retried.
#[tokio::test]
async fn a_merge_is_refused_when_nobody_can_approve_it() {
let (tx, mut rx, runs) = spawn_running(unattended_defaults(), "gh pr merge 3 --squash").await;
let (prompts, results) = run_turn_collecting_results(&tx, &mut rx, "go").await;
assert_eq!(prompts, 0, "nothing can answer a prompt here");
assert_eq!(runs.load(Ordering::SeqCst), 0, "the merge must not run");
let denial = results.iter().find(|text| text.contains("denied")).unwrap_or_else(|| {
panic!("the model has to be told the call was refused; got {results:?}")
});
assert!(denial.contains("Merging a pull request"), "names the act: {denial}");
assert!(denial.contains("nobody"), "and why: {denial}");
}
/// Opening a pull request is the loop's own workflow, so it resolves like every other `ask`.
#[tokio::test]
async fn opening_a_pull_request_still_goes_through_unattended() {
let (tx, mut rx, runs) = spawn_running(unattended_defaults(), "gh pr create --fill").await;
let prompts = run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(prompts, 0);
assert_eq!(runs.load(Ordering::SeqCst), 1, "the loop can open a PR");
}
/// `--auto-approve` is someone walking away, which is the same as nobody being there.
#[tokio::test]
async fn walking_away_does_not_approve_a_merge() {
let settings =
PermissionSettings { engine: PermissionEngine::permissive(), ask: AskResolution::Prompt };
let (tx, mut rx, runs) = spawn_running(settings, "gh issue close 9").await;
tx.send(Action::SetPermissionAutoApprove(true)).await.unwrap();
let prompts = run_turn_answering(&tx, &mut rx, "go", Some(PermissionReply::Once)).await;
assert_eq!(prompts, 0, "auto-approve never prompts");
assert_eq!(runs.load(Ordering::SeqCst), 0, "and does not close the issue either");
}
/// An explicit `allow` rule is a person deciding ahead of time, so it gets through.
#[tokio::test]
async fn an_allow_rule_is_a_person_deciding_ahead_of_time() {
let global = PermissionConfig::Keyed(
[(
"bash".to_string(),
PermValue::Map(
[("gh pr merge *".to_string(), PermissionAction::Allow)].into_iter().collect(),
),
)]
.into_iter()
.collect(),
)
.to_ruleset();
let settings = PermissionSettings {
engine: PermissionEngine::new(global, vec![], false),
ask: AskResolution::AutoAllow,
};
let (tx, mut rx, runs) = spawn_running(settings, "gh pr merge 3").await;
run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(runs.load(Ordering::SeqCst), 1);
}
/// `--yolo` already means everything.
#[tokio::test]
async fn yolo_still_means_everything() {
let settings = PermissionSettings {
engine: PermissionEngine::assemble(
luxe_core::permission::PermissionMode::Yolo,
vec![],
vec![],
false,
),
ask: AskResolution::AutoAllow,
};
let (tx, mut rx, runs) = spawn_running(settings, "gh pr merge 3").await;
run_turn_answering(&tx, &mut rx, "go", None).await;
assert_eq!(runs.load(Ordering::SeqCst), 1);
}
/// With someone at the keyboard, a merge is an ordinary prompt and an approval runs it.
#[tokio::test]
async fn an_interactive_merge_is_an_ordinary_prompt() {
let settings =
PermissionSettings { engine: PermissionEngine::permissive(), ask: AskResolution::Prompt };
let (tx, mut rx, runs) = spawn_running(settings, "gh pr merge 3").await;
let prompts = run_turn_answering(&tx, &mut rx, "go", Some(PermissionReply::Once)).await;
assert_eq!(prompts, 1);
assert_eq!(runs.load(Ordering::SeqCst), 1);
}
/// Luxe does not run commands as root, attended or not, and the refusal says how to hand the
/// command over rather than leaving the model to reword it past the rule.
#[tokio::test]
async fn running_as_root_is_refused_with_the_handoff_in_the_reply() {
for ask in [AskResolution::Prompt, AskResolution::AutoAllow] {
let settings = PermissionSettings { engine: PermissionEngine::permissive(), ask };
let (tx, mut rx, runs) = spawn_running(settings, "sudo pacman -S jq").await;
let (prompts, results) = run_turn_collecting_results(&tx, &mut rx, "go").await;
assert_eq!(prompts, 0, "a refusal, not a question ({ask:?})");
assert_eq!(runs.load(Ordering::SeqCst), 0, "the command must not start ({ask:?})");
let denial = results
.iter()
.find(|text| text.contains("does not run commands as root"))
.unwrap_or_else(|| panic!("the model is told why; got {results:?}"));
assert!(denial.contains("fenced code block"), "and what to do instead: {denial}");
assert!(denial.contains("their own terminal"), "{denial}");
}
}
/// Each refusal says its own thing: a plain policy deny is not dressed up as a repeat or as root.
#[tokio::test]
async fn a_plain_deny_keeps_the_plain_wording() {
let (tx, mut rx, runs) =
spawn_running(bash_policy(PermissionAction::Deny, AskResolution::Prompt), "ls").await;
let (_, results) = run_turn_collecting_results(&tx, &mut rx, "go").await;
assert_eq!(runs.load(Ordering::SeqCst), 0);
let denial = results
.iter()
.find(|text| text.contains("denied by policy"))
.unwrap_or_else(|| panic!("the generic refusal; got {results:?}"));
assert!(!denial.contains("already called"), "{denial}");
assert!(!denial.contains("as root"), "{denial}");
}