From 14d02ec4e40cf6fd221376686da544d544d01ad6 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom Date: Tue, 17 Nov 2026 22:18:53 +0100 Subject: [PATCH 1/9] test(local): expose guest FIFO ahead of owner requests (#688) --- oxa/oxc-local/tests/queue.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/oxa/oxc-local/tests/queue.rs b/oxa/oxc-local/tests/queue.rs index 045b7438f..86b81739f 100644 --- a/oxa/oxc-local/tests/queue.rs +++ b/oxa/oxc-local/tests/queue.rs @@ -220,3 +220,26 @@ fn queue_clones_serialize_concurrent_engine_work() { assert_eq!(queue.waiting(), 0); assert!(queue.try_acquire().is_ok()); } + +#[test] +fn queued_owner_runs_before_guest_without_preempting_active_guest() { + let queue = RequestQueue::new(3); + let active_guest = queue.try_acquire().unwrap(); + let mut guest = Box::pin(queue.acquire()); + let mut first_owner = Box::pin(queue.acquire()); + let mut second_owner = Box::pin(queue.acquire()); + + assert!(poll(guest.as_mut()).is_pending()); + assert!(poll(first_owner.as_mut()).is_pending()); + assert!(poll(second_owner.as_mut()).is_pending()); + + drop(active_guest); + assert!(poll(guest.as_mut()).is_pending()); + assert!(poll(second_owner.as_mut()).is_pending()); + let first = ready(poll(first_owner.as_mut())).unwrap(); + drop(first); + assert!(poll(guest.as_mut()).is_pending()); + let second = ready(poll(second_owner.as_mut())).unwrap(); + drop(second); + assert!(ready(poll(guest.as_mut())).is_ok()); +} From f5c1ae283e5cd1b0dc4acb45ca9d044d1ff8c600 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom Date: Tue, 17 Nov 2026 22:28:58 +0100 Subject: [PATCH 2/9] feat(local): prioritize owner leases over waiting guests (#688) --- oxa/oxc-local/src/queue.rs | 110 +++++++++++++++++++++++++++++------ oxa/oxc-local/tests/queue.rs | 75 +++++++++++++++++++++++- 2 files changed, 165 insertions(+), 20 deletions(-) diff --git a/oxa/oxc-local/src/queue.rs b/oxa/oxc-local/src/queue.rs index 379cbfe46..499b8c290 100644 --- a/oxa/oxc-local/src/queue.rs +++ b/oxa/oxc-local/src/queue.rs @@ -1,7 +1,7 @@ -//! A bounded FIFO of requests for one engine. No task or thread is spawned. +//! Bounded request admission for one engine. No task or thread is spawned. use std::fmt; -use std::sync::Arc; -use tokio::sync::{OwnedSemaphorePermit, Semaphore, TryAcquireError}; +use std::sync::{Arc, OnceLock}; +use tokio::sync::{Notify, OwnedSemaphorePermit, Semaphore, TryAcquireError}; /// Failed admission. Queue overflow is distinct from permanent shutdown. #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -20,6 +20,15 @@ impl fmt::Display for AdmissionError { } impl std::error::Error for AdmissionError {} +impl From for AdmissionError { + fn from(error: TryAcquireError) -> Self { + match error { + TryAcquireError::Closed => Self::Closed, + TryAcquireError::NoPermits => Self::Full, + } + } +} + /// Exclusive engine access, including cancellation cleanup and model operations. /// /// Keep this alive until the engine request has finished or cancellation is @@ -29,6 +38,25 @@ impl std::error::Error for AdmissionError {} #[must_use = "retain the lease until engine work and cancellation cleanup finish"] pub struct Lease { _permit: OwnedSemaphorePermit, + // Drop after the permit so a notified guest can try to acquire the engine. + _notify_guest: NotifyGuest, +} + +struct Guests { + // Waiting guests cannot occupy the last slot reserved for owner admission. + slots: Semaphore, + turn: Semaphore, + changed: Notify, +} + +struct NotifyGuest(Arc>); + +impl Drop for NotifyGuest { + fn drop(&mut self) { + if let Some(guests) = self.0.get() { + guests.changed.notify_one(); + } + } } /// One active request plus at most `max_waiters` suspended acquisitions. @@ -36,7 +64,9 @@ pub struct Lease { /// FIFO order begins when an acquisition future is first polled. Dropping a /// waiting future cancels that ticket and returns its capacity immediately. /// A cancelled request that retries joins the back of the queue. The underlying -/// Tokio semaphore supplies FIFO admission and cancellation-safe wakeups. +/// Tokio semaphore supplies owner FIFO admission and cancellation-safe wakeups. +/// Guests wait in their own FIFO and take the same engine permit once no owner +/// holds a grant. An owner never interrupts an active guest. /// /// Clones refer to the same engine. Use the same queue for model operations and /// generation; snapshots of `waiting()` are for status, never idle admission. @@ -45,6 +75,7 @@ pub struct RequestQueue { engine: Arc, slots: Arc, max_waiters: usize, + guests: Arc>, } impl RequestQueue { @@ -55,6 +86,14 @@ impl RequestQueue { engine: Arc::new(Semaphore::new(1)), slots: Arc::new(Semaphore::new(max_waiters)), max_waiters, + guests: Arc::new(OnceLock::new()), + } + } + + fn lease(&self, permit: OwnedSemaphorePermit) -> Lease { + Lease { + _permit: permit, + _notify_guest: NotifyGuest(self.guests.clone()), } } @@ -64,11 +103,8 @@ impl RequestQueue { self.engine .clone() .try_acquire_owned() - .map(|permit| Lease { _permit: permit }) - .map_err(|error| match error { - TryAcquireError::Closed => AdmissionError::Closed, - TryAcquireError::NoPermits => AdmissionError::Full, - }) + .map(|permit| self.lease(permit)) + .map_err(AdmissionError::from) } /// No polling timer is used: release, shutdown and cancellation drive wakes. @@ -80,14 +116,10 @@ impl RequestQueue { } // Capacity is taken only by work that actually has to wait. This guard // lives inside the acquisition future and is dropped on cancellation. - let slot = self - .slots - .clone() - .try_acquire_owned() - .map_err(|error| match error { - TryAcquireError::Closed => AdmissionError::Closed, - TryAcquireError::NoPermits => AdmissionError::Full, - })?; + let slot = self.slots.clone().try_acquire_owned()?; + // If cancellation returns a grant before we can build a Lease, the + // waiting guest must also see that the engine permit became available. + let _notify_guest = NotifyGuest(self.guests.clone()); let permit = self .engine .clone() @@ -95,7 +127,44 @@ impl RequestQueue { .await .map_err(|_| AdmissionError::Closed)?; drop(slot); - Ok(Lease { _permit: permit }) + Ok(self.lease(permit)) + } + + /// Wait for the shared engine at guest priority without passing queued owners. + pub async fn acquire_guest(&self) -> Result { + let guests = self.guests.get_or_init(|| Guests { + slots: Semaphore::new(self.max_waiters.saturating_sub(1)), + turn: Semaphore::new(1), + changed: Notify::new(), + }); + let turn = match guests.turn.try_acquire() { + Ok(turn) => { + match self.try_acquire() { + Ok(lease) => return Ok(lease), + Err(AdmissionError::Closed) => return Err(AdmissionError::Closed), + Err(AdmissionError::Full) => (), + } + Some(turn) + } + Err(TryAcquireError::Closed) => return Err(AdmissionError::Closed), + Err(TryAcquireError::NoPermits) => None, + }; + let _slot = self.slots.clone().try_acquire_owned()?; + let _guest_slot = guests.slots.try_acquire()?; + let _turn = match turn { + Some(turn) => turn, + None => guests.turn.acquire().await.map_err(|_| AdmissionError::Closed)?, + }; + + loop { + let mut changed = std::pin::pin!(guests.changed.notified()); + changed.as_mut().enable(); + match self.try_acquire() { + Ok(lease) => return Ok(lease), + Err(AdmissionError::Closed) => return Err(AdmissionError::Closed), + Err(AdmissionError::Full) => changed.await, + } + } } /// Rejects new work and wakes all waiters. An active lease remains owned @@ -103,6 +172,11 @@ impl RequestQueue { pub fn close(&self) { self.slots.close(); self.engine.close(); + if let Some(guests) = self.guests.get() { + guests.slots.close(); + guests.turn.close(); + guests.changed.notify_waiters(); + } } /// Approximate status, including a granted ticket not yet resumed by its diff --git a/oxa/oxc-local/tests/queue.rs b/oxa/oxc-local/tests/queue.rs index 86b81739f..c45af71b2 100644 --- a/oxa/oxc-local/tests/queue.rs +++ b/oxa/oxc-local/tests/queue.rs @@ -224,8 +224,8 @@ fn queue_clones_serialize_concurrent_engine_work() { #[test] fn queued_owner_runs_before_guest_without_preempting_active_guest() { let queue = RequestQueue::new(3); - let active_guest = queue.try_acquire().unwrap(); - let mut guest = Box::pin(queue.acquire()); + let active_guest = ready(poll(Box::pin(queue.acquire_guest()).as_mut())).unwrap(); + let mut guest = Box::pin(queue.acquire_guest()); let mut first_owner = Box::pin(queue.acquire()); let mut second_owner = Box::pin(queue.acquire()); @@ -243,3 +243,74 @@ fn queued_owner_runs_before_guest_without_preempting_active_guest() { drop(second); assert!(ready(poll(guest.as_mut())).is_ok()); } + +#[test] +fn guests_preserve_owner_capacity_and_fifo_after_cancellation() { + let queue = RequestQueue::new(3); + let active = queue.try_acquire().unwrap(); + let mut first_guest = Box::pin(queue.acquire_guest()); + let mut second_guest = Box::pin(queue.acquire_guest()); + assert!(poll(first_guest.as_mut()).is_pending()); + assert!(poll(second_guest.as_mut()).is_pending()); + assert!(matches!( + poll(Box::pin(queue.acquire_guest()).as_mut()), + Poll::Ready(Err(AdmissionError::Full)) + )); + let mut owner = Box::pin(queue.acquire()); + assert!(poll(owner.as_mut()).is_pending()); + assert_eq!(queue.waiting(), 3); + + drop(first_guest); + let mut retry = Box::pin(queue.acquire_guest()); + assert!(poll(retry.as_mut()).is_pending()); + drop(active); + assert!(poll(second_guest.as_mut()).is_pending()); + drop(owner); // Return the semaphore grant without constructing an owner lease. + assert!(poll(retry.as_mut()).is_pending()); + let guest = ready(poll(second_guest.as_mut())).unwrap(); + assert!(poll(retry.as_mut()).is_pending()); + drop(guest); + assert!(ready(poll(retry.as_mut())).is_ok()); +} + +#[test] +fn guest_shutdown_wakes_both_the_head_and_other_waiters() { + let queue = RequestQueue::new(3); + let active = queue.try_acquire().unwrap(); + let mut head = Box::pin(queue.acquire_guest()); + let mut tail = Box::pin(queue.acquire_guest()); + assert!(poll(head.as_mut()).is_pending()); + assert!(poll(tail.as_mut()).is_pending()); + queue.close(); + assert!(matches!( + poll(head.as_mut()), + Poll::Ready(Err(AdmissionError::Closed)) + )); + assert!(matches!( + poll(tail.as_mut()), + Poll::Ready(Err(AdmissionError::Closed)) + )); + assert_eq!(queue.waiting(), 0); + drop(active); +} + +#[test] +fn cancellation_of_granted_owner_wakes_guest_without_polling() { + let queue = RequestQueue::new(2); + let active = queue.try_acquire().unwrap(); + let wakes = Arc::new(Wakes(AtomicUsize::new(0))); + let waker = Waker::from(wakes.clone()); + let mut context = Context::from_waker(&waker); + let mut guest = Box::pin(queue.acquire_guest()); + let mut owner = Box::pin(queue.acquire()); + + assert!(guest.as_mut().poll(&mut context).is_pending()); + assert!(poll(owner.as_mut()).is_pending()); + drop(active); + assert!(guest.as_mut().poll(&mut context).is_pending()); + + let observed = wakes.0.load(Ordering::SeqCst); + drop(owner); + assert!(wakes.0.load(Ordering::SeqCst) > observed); + assert!(ready(guest.as_mut().poll(&mut context)).is_ok()); +} From a2dd086177d86b6b4071bbaa674846dca363ee48 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom Date: Tue, 17 Nov 2026 22:41:16 +0100 Subject: [PATCH 3/9] feat(local): admit native guest chat through the shared lifecycle (#688) --- oxa/oxc-local/src/lifecycle.rs | 16 +++++---- oxa/oxc-local/src/native.rs | 63 +++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/oxa/oxc-local/src/lifecycle.rs b/oxa/oxc-local/src/lifecycle.rs index cfa8e4e16..b47c93231 100644 --- a/oxa/oxc-local/src/lifecycle.rs +++ b/oxa/oxc-local/src/lifecycle.rs @@ -158,10 +158,13 @@ impl RequestGate { if !self.ready.load(Ordering::Acquire) { return Err(AdmissionError::Full); } - Ok(RequestLease { + Ok(self.operation(lease)) + } + fn operation(&self, lease: Lease) -> RequestLease { + RequestLease { lease: Some(lease), clock: self.clock.clone(), - }) + } } pub fn try_acquire(&self) -> Result { self.granted(self.queue.try_acquire()?) @@ -172,10 +175,11 @@ impl RequestGate { /// Reserve the same FIFO slot for load/switch followed by generation. /// Unlike acquire(), this permits an unloaded engine; hand it to load_reserved. pub async fn acquire_operation(&self) -> Result { - Ok(RequestLease { - lease: Some(self.queue.acquire().await?), - clock: self.clock.clone(), - }) + Ok(self.operation(self.queue.acquire().await?)) + } + /// Reserve the same load/generation lease at guest priority. + pub async fn acquire_guest_operation(&self) -> Result { + Ok(self.operation(self.queue.acquire_guest().await?)) } } diff --git a/oxa/oxc-local/src/native.rs b/oxa/oxc-local/src/native.rs index 4c338f595..98cb1f669 100644 --- a/oxa/oxc-local/src/native.rs +++ b/oxa/oxc-local/src/native.rs @@ -2,6 +2,7 @@ use crate::child_auth::ChildAuth; use crate::lifecycle::{Lifecycle, MemorySample, Model, Policy, RequestGate, RequestLease, StopReason}; use crate::memory::MemorySource; +use crate::queue::AdmissionError; use crate::transport::{Monitor, Transport}; use crate::State; use oxa_engine::{http_request, HttpRequest, StreamOpts, Url}; @@ -369,9 +370,30 @@ impl Engine for LocalEngine { request: &ChatRequest<'_>, cancel: &CancelToken, on: &mut dyn FnMut(StreamEvent), + ) -> Result { + self.chat_with_lease(request, cancel, on, self.gate.acquire_operation()) + } +} +impl LocalEngine { + /// Run a validated guest request through the owner's native engine lifecycle. + pub fn chat_guest( + &self, + request: &ChatRequest<'_>, + cancel: &CancelToken, + on: &mut dyn FnMut(StreamEvent), + ) -> Result { + self.chat_with_lease(request, cancel, on, self.gate.acquire_guest_operation()) + } + + fn chat_with_lease( + &self, + request: &ChatRequest<'_>, + cancel: &CancelToken, + on: &mut dyn FnMut(StreamEvent), + acquire: impl Future>, ) -> Result { let model = self.models.get(request.model).ok_or_else(invalid)?; - let operation = wait_operation(&self.gate, cancel)?; + let operation = wait_operation(acquire, cancel)?; let mut state = self.inner.state.lock().unwrap_or_else(|e| e.into_inner()); self.ensure_driver(&mut state)?; if cancel.is_cancelled() { @@ -591,14 +613,17 @@ impl Wake for Signal { self.wake.notify_one(); } } -fn wait_operation(gate: &RequestGate, cancel: &CancelToken) -> Result { +fn wait_operation( + acquire: impl Future>, + cancel: &CancelToken, +) -> Result { let signal = Arc::new(Signal { ready: Mutex::new(false), wake: Condvar::new(), }); let waker = Waker::from(signal.clone()); let mut context = Context::from_waker(&waker); - let mut acquire = std::pin::pin!(gate.acquire_operation()); + let mut acquire = std::pin::pin!(acquire); let mut cancelled = std::pin::pin!(cancel.cancelled()); loop { *signal.ready.lock().unwrap_or_else(|e| e.into_inner()) = false; @@ -837,7 +862,7 @@ mod tests { } assert_eq!(engine.status().pid, None); assert_eq!(engine.local_memory_status("fake-model"), None); - assert!(wait_operation(&engine.gate, &CancelToken::new()).is_ok()); + assert!(wait_operation(engine.gate.acquire_operation(), &CancelToken::new()).is_ok()); assert!(!PathBuf::from(format!("/proc/{}", scope.pid.load(Ordering::Acquire))).exists()); } } @@ -929,6 +954,36 @@ mod tests { assert_eq!(std::fs::read_dir(env.work().join("private")).unwrap().count(), 0); drop(engine); } + #[test] + fn guest_admission_cancellation_leaves_owner_lease_held() { + let env = TestEnv::new(); + let engine = engine(&env, Policy::default()); + let active = wait_operation(engine.gate.acquire_operation(), &CancelToken::new()).unwrap(); + let cancel = CancelToken::new(); + std::thread::scope(|scope| { + let guest = scope.spawn(|| { + engine.chat_guest( + &ChatRequest::new(&prefix(), &[], "fake-model"), + &cancel, + &mut |_| panic!("guest ran while the owner held the engine lease"), + ) + }); + let deadline = Instant::now() + Duration::from_secs(5); + while engine.gate.waiting() == 0 && !guest.is_finished() && Instant::now() < deadline { + std::thread::yield_now(); + } + let waiting = engine.gate.waiting(); + cancel.cancel(); + assert_eq!(guest.join().unwrap().unwrap_err().code, codes::ABORTED); + assert_eq!(waiting, 1, "guest must join the owner's engine queue"); + }); + assert_eq!(engine.gate.waiting(), 0); + assert!(matches!(engine.gate.try_acquire(), Err(AdmissionError::Full))); + assert_eq!(engine.status().state, State::Stopped); + drop(active); + assert!(wait_operation(engine.gate.acquire_operation(), &CancelToken::new()).is_ok()); + } + #[test] fn cancellation_reaps_the_child_before_a_fresh_request_reuses_the_queue() { let env = TestEnv::new(); From 45ce49211dd0bc9bbdf39e1ed703d69ad1a40ab0 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom Date: Tue, 17 Nov 2026 22:48:57 +0100 Subject: [PATCH 4/9] feat(share): serve selected native local models over LAN (#688) --- oxa/docs/pages/cli.md | 3 + oxa/oxa-cli/src/dispatch.rs | 13 ++- oxa/oxa-cli/src/help.rs | 10 +- oxa/oxa-cli/src/lib.rs | 2 + oxa/oxa-cli/src/share_cmd.rs | 146 ++++++++++++++++++++++++ oxa/oxa-cli/tests/share_keys.rs | 130 +++++++++++++++++++++ oxa/oxc-gateway/src/share/README.md | 15 ++- oxa/oxc-gateway/src/share/mod.rs | 1 + oxa/oxc-gateway/src/share/native.rs | 141 +++++++++++++++++++++++ oxa/oxc-gateway/tests/share_listener.rs | 84 ++++++++++++++ oxa/oxc-local/src/configured.rs | 9 ++ oxa/oxc-local/src/lib.rs | 2 +- 12 files changed, 544 insertions(+), 12 deletions(-) create mode 100644 oxa/oxa-cli/src/share_cmd.rs create mode 100644 oxa/oxc-gateway/src/share/native.rs diff --git a/oxa/docs/pages/cli.md b/oxa/docs/pages/cli.md index 8a4570ee2..14da645ed 100644 --- a/oxa/docs/pages/cli.md +++ b/oxa/docs/pages/cli.md @@ -104,6 +104,7 @@ Commands take the place of the message. They do not start a session. | `oxa config [-l]` | print every setting with its value and where it comes from | | `oxa mcp ` | manage MCP servers, see below | | `oxa identity [--refresh] [--offline]` | show the identity text of the system prompt; `--refresh` rewrites it from this machine and the engine, and `--offline` skips the engine | +| `oxa share serve --model ALIAS` | start the authenticated streaming OpenAI-compatible listener for explicitly selected native local models in a build with the `share` feature | | `oxa share key ` | owner-only guest key and daily limit management, in a build compiled with the `share` feature | | `oxa report-issue ` | draft a scrubbed issue for the ox-llm repository, or post a draft after confirmation | | `oxa wipe --session \| --all [--yes] [--engine]` | remove the stored session files and spill files of one session or of all sessions. See [sessions.md](sessions.md#what-oxa-stores-and-how-to-remove-it) | @@ -117,6 +118,8 @@ $ oxa share key add guest --daily-requests 100 --daily-tokens 200000 $ oxa share key list ``` +`oxa share serve` starts a foreground listener after the owner enables the native local engine and selects one or more local aliases with `--model`. It binds to the Tailscale interface by default and prints its `/v1` base URL. Set `--port` to use a fixed port, or `--bind 127.0.0.1` to put the listener behind Tailscale Serve. `--same-owner` requires the connecting peer to pass the Tailscale owner check. Clients send a guest bearer and can list models or stream text chat. Ctrl+C stops admission and joins active cleanup. + `oxa update --self`, `--all` and `--models` report that they are not supported: oxa does not update itself and does not refresh model catalogues. Update oxa by installing a new build. `oxa report-issue draft` takes `--title ` (required), and the notes either inline with `--notes ` or from a file with `--notes-file ` (relative paths are taken from the current directory). It writes the draft under `issue-drafts/` in the agent directory and prints its path; it posts nothing. `oxa report-issue submit ` posts it after you type `yes`. diff --git a/oxa/oxa-cli/src/dispatch.rs b/oxa/oxa-cli/src/dispatch.rs index 37ee8cb7d..bad0e1123 100644 --- a/oxa/oxa-cli/src/dispatch.rs +++ b/oxa/oxa-cli/src/dispatch.rs @@ -147,6 +147,7 @@ pub fn run(argv: &[String], env: &Env, io: Stdio) -> i32 { let result = match sub { #[cfg(feature = "share")] Subcommand::Share(words) => { + let serve = words.first().is_some_and(|word| word == "serve"); // Env::get treats empty values as unset; owner-command policy // refuses marker presence, including an empty value. if env.pairs().iter().any(|(name, _)| { @@ -159,10 +160,20 @@ pub fn run(argv: &[String], env: &Env, io: Stdio) -> i32 { &mut *stderr, &OxaError::new( codes::INVALID, - "Guest key commands require the owner outside an agent tool", + if serve { + "Guest serving requires the owner outside an agent tool" + } else { + "Guest key commands require the owner outside an agent tool" + }, ), ); } + if serve { + return match crate::share_cmd::run(&words[1..], &dirs, &cwd, &mut *stdout) { + Ok(()) => 0, + Err(error) => report(&mut *stderr, &error), + }; + } oxc_gateway::share::command::run(&words, &dirs.home.join(".oxa/share"), &mut *stdout) .map(|()| 0) .map_err(|error| { diff --git a/oxa/oxa-cli/src/help.rs b/oxa/oxa-cli/src/help.rs index e581e174e..57d95b931 100644 --- a/oxa/oxa-cli/src/help.rs +++ b/oxa/oxa-cli/src/help.rs @@ -202,10 +202,16 @@ const BASE: &[Entry] = &[ const SHARE: &[Entry] = &[ Entry { path: &["share"], - summary: "Manage private guest keys and daily limits", - usage: &["oxa share key "], + summary: "Serve native local models and manage private guest keys", + usage: &["oxa share "], example: "oxa share key list", }, + Entry { + path: &["share", "serve"], + summary: "Serve selected native local models to authenticated guest devices", + usage: &["oxa share serve --model ALIAS [--model ALIAS] [--port PORT] [--bind ADDRESS] [--tailscale-socket PATH] [--same-owner]"], + example: "oxa share serve --model local-model --port 8080", + }, Entry { path: &["share", "key"], summary: "Manage private guest keys and daily limits", diff --git a/oxa/oxa-cli/src/lib.rs b/oxa/oxa-cli/src/lib.rs index 2ab4a2bce..5cc5013e8 100644 --- a/oxa/oxa-cli/src/lib.rs +++ b/oxa/oxa-cli/src/lib.rs @@ -43,6 +43,8 @@ mod pkg_cmd; mod report_cmd; mod result_facts; mod sandbox_layout; +#[cfg(feature = "share")] +mod share_cmd; mod status; mod themes; mod wipe_cmd; diff --git a/oxa/oxa-cli/src/share_cmd.rs b/oxa/oxa-cli/src/share_cmd.rs new file mode 100644 index 000000000..a2cc41429 --- /dev/null +++ b/oxa/oxa-cli/src/share_cmd.rs @@ -0,0 +1,146 @@ +//! Foreground opt-in native LAN listener. Only user settings can select its engine. +use oxa_config::{Dirs, Settings}; +use oxa_proto::{codes, CancelToken, ChatRequest, Engine, OxaError, StreamEvent}; +use oxc_gateway::share::{ + keys::Store, + listener::{Access, Binding, Config, Failure, Model}, + native, + tailnet::LocalApi, +}; +use std::{ + io::Write, + path::{Path, PathBuf}, + sync::Arc, + time::Duration, +}; + +pub(crate) fn run(args: &[String], dirs: &Dirs, cwd: &Path, out: &mut dyn Write) -> Result<(), OxaError> { + oxa_proto::check_online("starting the LAN sharing listener")?; + let (config, aliases, socket) = options(args)?; + let settings = Settings::load(dirs, cwd, false); + if !settings.bool(oxc_local::LOCAL_SETTING, false) { + return Err(OxaError::new( + codes::INVALID, + "Enable the native local engine in user settings before serving models.", + )); + } + let keys = Arc::new( + Store::open(&dirs.home.join(".oxa/share")) + .map_err(|error| OxaError::new(codes::INVALID, error.to_string()))?, + ); + let local = oxc_local::configured_local_engine( + settings.get("oxa.local"), + dirs.agent_dir.join("local-engine"), + oxa_engine::StreamOpts { + silence_limit_ms: crate::dispatch::stream_silence_timeout(&settings) + .unwrap_or(365 * 24 * 3600 * 1000), + ..oxa_engine::StreamOpts::default() + }, + )?; + let catalog = local.info(None)?; + let models = aliases + .into_iter() + .map(|alias| { + if !catalog.models.contains(&alias) { + return Err(OxaError::new( + codes::NOT_FOUND, + "The selected guest model is unavailable in the native local catalog.", + )); + } + let info = local.info(Some(&alias))?; + if info.caps.remote || info.limits_for != alias { + return Err(OxaError::new( + codes::INVALID, + "The selected guest model has no local limits.", + )); + } + Ok(Model { + alias, + context_window: info.n_ctx, + max_output: info.max_output, + access: Access::Local, + }) + }) + .collect::, OxaError>>()?; + let answer = native::Native::new( + models, + move |request: &ChatRequest<'_>, cancel: &CancelToken, on: &mut dyn FnMut(StreamEvent)| { + local.chat_guest(request, cancel, on) + }, + ) + .map_err(service_error)?; + let tailnet = LocalApi::new(socket, Duration::from_secs(5)) + .map_err(|error| OxaError::new(codes::INVALID, error.to_string()))?; + + let stop = CancelToken::new(); + let mut signals = + signal_hook::iterator::Signals::new([signal_hook::consts::SIGINT, signal_hook::consts::SIGTERM]) + .map_err(|error| OxaError::io("cannot subscribe to listener shutdown signals", &error))?; + let signal_handle = signals.handle(); + let signal_stop = stop.clone(); + let signal_thread = std::thread::Builder::new() + .spawn(move || { + if signals.forever().next().is_some() { + signal_stop.cancel(); + } + }) + .map_err(|error| OxaError::io("cannot start listener shutdown handler", &error))?; + + let result = native::serve( + keys, + Arc::new(answer), + Arc::new(tailnet), + config, + stop, + |address| { + writeln!(out, "Guest OpenAI API listening at http://{address}/v1") + .and_then(|()| out.flush()) + .map_err(|_| Failure::Failed) + }, + ); + signal_handle.close(); + let _ = signal_thread.join(); + result.map_err(service_error) +} + +fn options(args: &[String]) -> Result<(Config, Vec, PathBuf), OxaError> { + let mut config = Config::default(); + let mut aliases = Vec::new(); + let mut socket = PathBuf::from("/var/run/tailscale/tailscaled.sock"); + let mut args = args.iter(); + while let Some(arg) = args.next() { + if arg == "--same-owner" { + config.same_owner = true; + continue; + } + let value = args.next().filter(|value| !value.is_empty()).ok_or_else(usage)?; + match arg.as_str() { + "--model" if !aliases.contains(value) && aliases.len() < 64 => aliases.push(value.clone()), + "--bind" => config.binding = Binding::Explicit(value.parse().map_err(|_| usage())?), + "--port" => config.port = value.parse().map_err(|_| usage())?, + "--tailscale-socket" => socket = PathBuf::from(value), + _ => return Err(usage()), + } + } + if aliases.is_empty() { + return Err(usage()); + } + Ok((config, aliases, socket)) +} + +fn usage() -> OxaError { + OxaError::new( + codes::USAGE, + "Choose at least one native local guest model with --model ALIAS. See oxa share serve --help.", + ) +} + +fn service_error(error: Failure) -> OxaError { + let message = match error { + Failure::Forbidden => "No eligible guest model or usable tailnet address.", + Failure::BadRequest => "Invalid guest listener configuration.", + Failure::Busy => "Guest listener could not bind or tailscaled is unavailable.", + _ => "Guest listener stopped unexpectedly.", + }; + OxaError::new(codes::INVALID, message) +} diff --git a/oxa/oxa-cli/tests/share_keys.rs b/oxa/oxa-cli/tests/share_keys.rs index 15c6d7c59..a8efe4e5e 100644 --- a/oxa/oxa-cli/tests/share_keys.rs +++ b/oxa/oxa-cli/tests/share_keys.rs @@ -334,3 +334,133 @@ fn output_failure_keeps_one_durable_key_without_retrying_or_leaking_diagnostics( assert!(!rows[0].revoked); assert!(store.revoke(&rows[0].id).unwrap()); } + +#[cfg(feature = "share")] +#[test] +fn serving_refuses_agent_and_offline_before_opening_guest_storage() { + let home = Home::new(); + let serve = ["share", "serve", "--model", "synthetic-model"]; + let agent = home.marked(&serve, "OXA_CODING_AGENT", ""); + assert!(!agent.status.success()); + assert!(String::from_utf8_lossy(&agent.stderr).contains("Guest serving requires the owner")); + + let offline = home.run(&["--offline", "share", "serve", "--model", "synthetic-model"]); + assert!(!offline.status.success()); + assert!(String::from_utf8_lossy(&offline.stderr).contains("offline")); + assert!(!home.0.join(".oxa/share").exists()); +} + +#[cfg(feature = "share")] +#[test] +fn serving_requires_an_owner_selected_alias_and_enabled_native_engine() { + let home = Home::new(); + let missing_alias = home.run(&["share", "serve"]); + assert!(!missing_alias.status.success()); + assert!(String::from_utf8_lossy(&missing_alias.stderr).contains("--model ALIAS")); + + let disabled = home.run(&["share", "serve", "--model", "synthetic-model"]); + assert!(!disabled.status.success()); + assert!(String::from_utf8_lossy(&disabled.stderr).contains("Enable the native local engine")); + assert!(!home.0.join(".oxa/share").exists()); +} + +#[cfg(feature = "share")] +#[test] +fn foreground_serve_lists_only_the_owner_selected_local_models_on_loopback() { + use std::io::{BufRead, BufReader, Read, Write}; + use std::net::{SocketAddr, TcpStream}; + use std::process::{Child, Stdio}; + use std::time::Duration; + + struct Serving(Child); + impl Drop for Serving { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } + } + + let home = Home::new(); + let agent = home.0.join(".oxa/agent"); + fs::create_dir_all(&agent).unwrap(); + let limit = serde_json::json!({ + "context_window":4096,"max_output":64, + "allocation_budget":{"kv_bytes_per_position":1,"scratch_bytes":0,"mtp_state_bytes":0,"margin_bytes":1044480} + }); + let settings = serde_json::json!({"oxa":{ + "engine":{"inProcessLocal":true}, + "local":{"binary":"/synthetic/oxe-server", + "models":{ + "synthetic-visible":{"model":"/synthetic/visible.gguf","arguments":["--engine","stub"]}, + "synthetic-hidden":{"model":"/synthetic/hidden.gguf","arguments":["--engine","stub"]} + }, + "limits":{"synthetic-visible":limit,"synthetic-hidden":limit} + } + }}); + fs::write(agent.join("settings.json"), settings.to_string()).unwrap(); + let added = home.run(&[ + "share", + "key", + "add", + "Device", + "--daily-requests", + "10", + "--daily-tokens", + "10000", + ]); + assert!(added.status.success()); + let key: serde_json::Value = serde_json::from_slice(&added.stdout).unwrap(); + let bearer = key["api_key"].as_str().unwrap(); + + let mut serving = Serving( + Command::new(env!("CARGO_BIN_EXE_oxa")) + .args([ + "share", + "serve", + "--model", + "synthetic-visible", + "--bind", + "127.0.0.1", + ]) + .env_clear() + .env("HOME", &home.0) + .env("OXA_TEST_ISOLATION", "1") + .current_dir(&home.0) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .spawn() + .unwrap(), + ); + let stdout = serving.0.stdout.take().unwrap(); + let (send, recv) = std::sync::mpsc::channel(); + let reader = std::thread::spawn(move || { + let mut line = String::new(); + let result = BufReader::new(stdout).read_line(&mut line).map(|_| line); + let _ = send.send(result); + }); + let line = recv.recv_timeout(Duration::from_secs(5)).unwrap().unwrap(); + reader.join().unwrap(); + let address: SocketAddr = line + .trim() + .strip_prefix("Guest OpenAI API listening at http://") + .unwrap() + .strip_suffix("/v1") + .unwrap() + .parse() + .unwrap(); + assert!(address.ip().is_loopback()); + + let mut client = TcpStream::connect_timeout(&address, Duration::from_secs(2)).unwrap(); + client.set_read_timeout(Some(Duration::from_secs(2))).unwrap(); + write!( + client, + "GET /v1/models HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer {bearer}\r\n\r\n" + ) + .unwrap(); + let mut reply = String::new(); + client.read_to_string(&mut reply).unwrap(); + assert!(reply.starts_with("HTTP/1.1 200")); + assert!(reply.contains("synthetic-visible")); + assert!(!reply.contains("synthetic-hidden")); + assert!(!reply.contains(bearer)); +} diff --git a/oxa/oxc-gateway/src/share/README.md b/oxa/oxc-gateway/src/share/README.md index f1eb3eefd..5044f3534 100644 --- a/oxa/oxc-gateway/src/share/README.md +++ b/oxa/oxc-gateway/src/share/README.md @@ -2,8 +2,8 @@ The `share` feature is off by default. It supplies private keys, durable guest admission and an explicit HTTP listener. The CLI's opt-in share feature adds -owner key commands. Importing it creates no files, sockets or threads. Real -gateway integration follows after the native controller seams. +owner key commands and foreground serving for the native local engine. +Importing the module creates no files, sockets or threads. `keys::Store::open` binds an existing mode-0700 directory outside repositories. Every path component is opened without following links. Registry and lock files @@ -75,9 +75,9 @@ cancellation and join cleanup instead of abandoning generation. Streaming connections accept one request and treat client EOF/pipelining as cancellation. Cancel and await `serve` for orderly shutdown; do not detach it or its backend. -Real gateway routing, owner-priority local leases, native request-ledger rows, -and owner status-surface wiring are -subsequent work. The counters here are quota authority, not a second copy of +The native guest backend uses the controller's local lifecycle and guest-priority +lease. Sharing a running owner session's engine, recording native request-ledger +rows, and wiring the owner status surface remain subsequent work. The counters here are quota authority, not a second copy of cloud billing or a complete request ledger. TLS can terminate at `tailscale serve`; direct certificate support is not implemented in this slice. @@ -103,9 +103,8 @@ creating a replacement; the bearer cannot be recovered. Both limits are required positive integers. Storage is `~/.oxa/share`, independent of project settings and `OXA_AGENT_DIR`. Missing directories are created privately through anchored no-follow descriptors, with parent syncs on creation and retry. Existing unsafe -permissions, symlinks and repository paths are refused without repair. No model, -provider credentials or listener are opened by these commands. Serving still -requires the separately coordinated native gateway/owner-priority integration. +permissions, symlinks and repository paths are refused without repair. These commands touch only the key registry. Use the explicit foreground serve +command to start the listener with owner-selected native local aliases. ## Owner live activity diff --git a/oxa/oxc-gateway/src/share/mod.rs b/oxa/oxc-gateway/src/share/mod.rs index 51e79d52a..938b12432 100644 --- a/oxa/oxc-gateway/src/share/mod.rs +++ b/oxa/oxc-gateway/src/share/mod.rs @@ -3,6 +3,7 @@ pub mod admission; pub mod command; pub mod keys; pub mod listener; +pub mod native; mod protocol; pub mod status; pub mod tailnet; diff --git a/oxa/oxc-gateway/src/share/native.rs b/oxa/oxc-gateway/src/share/native.rs new file mode 100644 index 000000000..a9889a283 --- /dev/null +++ b/oxa/oxc-gateway/src/share/native.rs @@ -0,0 +1,141 @@ +//! Bridge bounded guest HTTP requests to a synchronous native engine entry point. +use super::listener::{ + Access, Answer, CancelToken, Completed, Failure, Finish, Guest, Model, Output, Request, Usage, +}; +use oxa_proto::{AssistantTurn, ChatRequest, FrozenPrefix, OxaError, Role, Sampling, StreamEvent, WireMsg}; +use sha2::{Digest, Sha256}; +use std::sync::Arc; + +pub struct Native { + models: Vec, + chat: Arc, +} + +impl Native { + pub fn new(models: Vec, chat: C) -> Result { + if models.is_empty() || models.iter().any(|model| model.access != Access::Local) { + return Err(Failure::Forbidden); + } + Ok(Self { + models, + chat: Arc::new(chat), + }) + } +} + +impl Answer for Native +where + C: Fn(&ChatRequest<'_>, &CancelToken, &mut dyn FnMut(StreamEvent)) -> Result + + Send + + Sync + + 'static, +{ + fn models(&self) -> &[Model] { + &self.models + } + + async fn chat( + &self, + request: Request, + _guest: Guest<'_>, + output: Output, + cancel: CancelToken, + ) -> Result { + let chat = self.chat.clone(); + let runtime = tokio::runtime::Handle::current(); + // The listener joins this future even after disconnect or shutdown. The + // synchronous engine keeps its lease until cancellation has finished. + tokio::task::spawn_blocking(move || { + let messages = request + .messages + .into_iter() + .map(|message| { + let content = message["content"].as_str().ok_or(Failure::BadRequest)?; + match message["role"].as_str() { + Some("system") => Ok(WireMsg::system(content)), + Some("user") => Ok(WireMsg::user(content)), + Some("assistant") => Ok(WireMsg::assistant(content)), + _ => Err(Failure::BadRequest), + } + }) + .collect::, _>>()?; + // The native serializer emits its prefix as the first system + // message. Move leading guest instructions into that prefix. + let first_chat = messages + .iter() + .take_while(|message| message.role == Role::System) + .count(); + let (system, messages) = messages.split_at(first_chat); + let mut prefix = FrozenPrefix { + sections: system + .iter() + .filter_map(|message| message.content.clone().map(|text| ("system".into(), text))) + .collect(), + tools_json: "[]".into(), + sha256: [0; 32], + }; + prefix.sha256 = Sha256::digest(prefix.hash_input()).into(); + let sampling = (request.temperature.is_some() || request.top_p.is_some()).then_some(Sampling { + temperature: request.temperature.unwrap_or(1.0) as f32, + top_p: request.top_p.unwrap_or(1.0) as f32, + top_k: 0, + min_p: 0.0, + presence_penalty: 0.0, + seed: None, + }); + let request = ChatRequest { + max_tokens: request.max_output, + sampling, + ..ChatRequest::new(&prefix, messages, &request.model) + }; + let reply = chat(&request, &cancel, &mut |event| match event { + StreamEvent::Text(text) => { + if runtime.block_on(output.text(&text)).is_err() { + cancel.cancel(); + } + } + StreamEvent::ToolArgs { .. } => cancel.cancel(), + _ => {} + }) + .map_err(|_| Failure::Failed)?; + if cancel.is_cancelled() || !reply.msg.tool_calls.is_empty() { + return Err(Failure::Failed); + } + let finish = match reply.finish { + oxa_proto::Finish::Stop => Finish::Stop, + oxa_proto::Finish::Length => Finish::Length, + oxa_proto::Finish::ContentFilter => Finish::Refusal, + oxa_proto::Finish::ToolCalls => return Err(Failure::Failed), + }; + Ok(Completed { + finish, + usage: (reply.usage.prompt > 0).then_some(Usage { + input: u64::from(reply.usage.prompt), + output: u64::from(reply.usage.completion), + }), + }) + }) + .await + .map_err(|_| Failure::Failed)? + } +} + +/// Run the listener to completion in the caller's foreground process. +pub fn serve( + keys: Arc, + answer: Arc, + tailnet: Arc, + config: super::listener::Config, + stop: CancelToken, + ready: impl FnOnce(std::net::SocketAddr) -> Result<(), Failure>, +) -> Result<(), Failure> { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .map_err(|_| Failure::Failed)?; + runtime.block_on(async { + let server = super::listener::Server::bind(keys, answer, tailnet, config).await?; + ready(server.address()?)?; + server.serve(stop).await + }) +} diff --git a/oxa/oxc-gateway/tests/share_listener.rs b/oxa/oxc-gateway/tests/share_listener.rs index ba88255aa..3a69d2b03 100644 --- a/oxa/oxc-gateway/tests/share_listener.rs +++ b/oxa/oxc-gateway/tests/share_listener.rs @@ -736,3 +736,87 @@ async fn successful_stream_retries_contended_settlement_and_finishes() { serving.await.unwrap().unwrap(); } } + +#[tokio::test(flavor = "current_thread")] +async fn authenticated_native_bridge_streams_only_guest_text_and_settles_usage() { + use oxa_proto::{AssistantTurn, ChatRequest, StreamEvent, ThinkingLevel, WireMsg}; + use oxc_gateway::share::native::Native; + + let directory = Directory::new(); + let keys = Arc::new(directory.store()); + let key = keys.create("Device", limits()).unwrap(); + let answer = Native::new( + vec![Model { + alias: "local-model".into(), + context_window: 100, + max_output: 10, + access: Access::Local, + }], + |request: &ChatRequest<'_>, _: &CancelToken, on: &mut dyn FnMut(StreamEvent)| { + assert_eq!(request.model, "local-model"); + assert_eq!(request.prefix.tools_json, "[]"); + assert_eq!(request.prefix.system_text(), "Synthetic guest instructions"); + assert_eq!(request.messages, &[WireMsg::user("Synthetic request")]); + assert_eq!(request.max_tokens, 10); + on(StreamEvent::Reasoning("Synthetic private reasoning".into())); + on(StreamEvent::Text("Synthetic native reply".into())); + Ok(AssistantTurn { + msg: WireMsg::assistant("Synthetic native reply"), + finish: oxa_proto::Finish::Stop, + usage: oxa_proto::Usage { + prompt: 3, + completion: 2, + ..oxa_proto::Usage::default() + }, + timings: None, + model: "local-model".into(), + level: ThinkingLevel::Off, + }) + }, + ) + .unwrap(); + let server = Server::bind( + keys.clone(), + Arc::new(answer), + Arc::new(Network::local()), + config(), + ) + .await + .unwrap(); + let address = server.address().unwrap(); + let stop = CancelToken::new(); + let task = tokio::spawn(server.serve(stop.clone())); + let mut request = body("local-model"); + request["messages"].as_array_mut().unwrap().insert( + 0, + json!({"role":"system","content":"Synthetic guest instructions"}), + ); + let request = request.to_string(); + let response = exchange( + address, + format!( + "POST /v1/chat/completions HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{request}", + key.bearer.expose(), request.len() + ), + ).await; + assert!(response.starts_with("HTTP/1.1 200")); + assert!(response.contains("Synthetic native reply")); + assert!(response.contains("data: [DONE]")); + assert!(response.contains("\"prompt_tokens\":3")); + assert!(response.contains("\"completion_tokens\":2")); + assert!(!response.contains("Synthetic private reasoning")); + assert!(!response.contains(key.bearer.expose())); + + stop.cancel(); + tokio::time::timeout(Duration::from_secs(3), task) + .await + .unwrap() + .unwrap() + .unwrap(); + let day = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs() + / 86400; + assert_eq!(keys.guest_usage(day, &key.info.id).unwrap(), (1, 5)); +} diff --git a/oxa/oxc-local/src/configured.rs b/oxa/oxc-local/src/configured.rs index 5ca396704..8e8c8a0bb 100644 --- a/oxa/oxc-local/src/configured.rs +++ b/oxa/oxc-local/src/configured.rs @@ -24,6 +24,15 @@ pub fn configured_engine( private_dir: PathBuf, opts: StreamOpts, ) -> Result, OxaError> { + configured_local_engine(local, private_dir, opts).map(|engine| engine as Arc) +} + +/// Build the same user-only native engine while retaining its guest entry point. +pub fn configured_local_engine( + local: Option<&Value>, + private_dir: PathBuf, + opts: StreamOpts, +) -> Result, OxaError> { let local = local.and_then(Value::as_object).ok_or_else(invalid)?; if local.keys().any(|k| { !matches!( diff --git a/oxa/oxc-local/src/lib.rs b/oxa/oxc-local/src/lib.rs index b72e49742..856311879 100644 --- a/oxa/oxc-local/src/lib.rs +++ b/oxa/oxc-local/src/lib.rs @@ -4,7 +4,7 @@ mod admission; mod child_auth; mod configured; -pub use configured::{configured_engine, LOCAL_SETTING}; +pub use configured::{configured_engine, configured_local_engine, LOCAL_SETTING}; pub mod launch_profile; pub mod lifecycle; pub mod memory; From ff7ecaca034fbc7e87d8f4a1c78d658ad6a1e399 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom Date: Tue, 17 Nov 2026 22:58:38 +0100 Subject: [PATCH 5/9] fix(cli): include share serving in the command inventory --- oxa/oxa-cli/src/args.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/oxa/oxa-cli/src/args.rs b/oxa/oxa-cli/src/args.rs index 912ad6d58..9ee1afa78 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -221,6 +221,11 @@ const COMMANDS: &[(&str, &str)] = &[ "oxa share key ", "Manage private guest keys and daily limits", ), + #[cfg(feature = "share")] + ( + "oxa share serve --model ALIAS", + "Serve selected native local models to authenticated guest devices", + ), ( "oxa status ", "Read a saved session's current progress and stall signal", From 00aff0a55f35e4fb734bed36e733e3f44d5ec4e4 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom Date: Tue, 17 Nov 2026 23:09:57 +0100 Subject: [PATCH 6/9] fix(share): dispatch leading offline flags to the listener guard --- oxa/oxa-cli/src/args.rs | 7 ++++--- oxa/oxa-cli/tests/share_keys.rs | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/oxa/oxa-cli/src/args.rs b/oxa/oxa-cli/src/args.rs index 9ee1afa78..53114038e 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -589,11 +589,12 @@ pub fn parse_args(argv: &[String], ext_flags: &[FlagSpec]) -> Result Date: Tue, 17 Nov 2026 23:23:12 +0100 Subject: [PATCH 7/9] feat(share): let the owner session share its native engine (#688) --- oxa/docs/pages/cli.md | 4 + oxa/oxa-cli/src/args.rs | 29 +++++ oxa/oxa-cli/src/dispatch.rs | 63 ++++++++-- oxa/oxa-cli/src/in_process.rs | 35 +++--- oxa/oxa-cli/src/share_cmd.rs | 206 ++++++++++++++++++++++++++++---- oxa/oxa-cli/tests/share_keys.rs | 21 ++++ 6 files changed, 305 insertions(+), 53 deletions(-) diff --git a/oxa/docs/pages/cli.md b/oxa/docs/pages/cli.md index 14da645ed..ecb821070 100644 --- a/oxa/docs/pages/cli.md +++ b/oxa/docs/pages/cli.md @@ -120,6 +120,8 @@ $ oxa share key list `oxa share serve` starts a foreground listener after the owner enables the native local engine and selects one or more local aliases with `--model`. It binds to the Tailscale interface by default and prints its `/v1` base URL. Set `--port` to use a fixed port, or `--bind 127.0.0.1` to put the listener behind Tailscale Serve. `--same-owner` requires the connecting peer to pass the Tailscale owner check. Clients send a guest bearer and can list models or stream text chat. Ctrl+C stops admission and joins active cleanup. +Start an interactive owner session with `oxa --share-model ALIAS` to share that session's native engine. A queued owner request goes before a waiting guest, and an active guest keeps its lease through cleanup. The startup notice gives the guest API address. Exiting the owner session drains the listener. + `oxa update --self`, `--all` and `--models` report that they are not supported: oxa does not update itself and does not refresh model catalogues. Update oxa by installing a new build. `oxa report-issue draft` takes `--title ` (required), and the notes either inline with `--notes ` or from a file with `--notes-file ` (relative paths are taken from the current directory). It writes the draft under `issue-drafts/` in the agent directory and prints its path; it posts nothing. `oxa report-issue submit ` posts it after you type `yes`. @@ -236,6 +238,8 @@ See [tools.md](tools.md) for the tool names. | `--yolo` | permission mode yolo, full access: no prompts, bash runs without the sandbox, no deny-list or protected paths for file tools ([security.md](security.md#standard-auto-and-yolo)) | | `--approve`, `-a` | trust the project's configuration and resources for this run | | `--no-approve`, `-na` | ignore the project's configuration and resources for this run | +| `--share-model ` | share one native local model during the interactive owner session in a build with the `share` feature, repeat to select more aliases | +| `--share-bind
` | set an explicit bind address for `--share-model`. Sharing uses the Tailscale interface by default | | `--offline` | offline mode: oxa's own non-local connections are refused, bash has no network, `web_search` is off, and `!cmd` and stdio MCP servers run without network. What it does not cover is in [security.md](security.md#offline-mode) | See [security.md](security.md). diff --git a/oxa/oxa-cli/src/args.rs b/oxa/oxa-cli/src/args.rs index 53114038e..41390eb03 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -201,6 +201,12 @@ pub struct Args { pub version: bool, /// The hard offline switch (see the help text). pub offline: bool, + /// Explicitly expose only these native local aliases for the current owner session. + #[cfg(feature = "share")] + pub share_models: Vec, + /// Override the Tailscale bind for this owner session. + #[cfg(feature = "share")] + pub share_bind: Option, /// Flags registered by extensions (`registerFlag`): name -> value. pub extension_flags: Vec<(String, Value)>, /// Unknown `--long` flags seen before the extension flags are known (resolved by the @@ -439,6 +445,10 @@ const OPTIONS: &[(&str, &str)] = &[ "Permission mode: standard, auto, yolo, plan (or workspace, ask); default: permissions.defaultMode, else auto", ), ("--verbose", "Show verbose startup information"), + #[cfg(feature = "share")] + ("--share-model ", "Share one native local model during the interactive owner session (repeatable)"), + #[cfg(feature = "share")] + ("--share-bind
", "Explicitly bind interactive guest sharing to this address"), ( "--offline", "Hard offline switch: non-local connections refused, bash has no network, web_search off", @@ -869,6 +879,25 @@ fn parse_flag( no_value(inline, flag)?; a.approve = Some(false); } + #[cfg(feature = "share")] + "--share-model" => { + let alias = value(argv, i, inline, flag, "a native local alias")?; + if alias.is_empty() || a.share_models.len() >= 64 || a.share_models.contains(&alias) { + return Err(usage( + "specify between one and 64 distinct native local sharing aliases", + )); + } + a.share_models.push(alias); + } + #[cfg(feature = "share")] + "--share-bind" => { + let address = value(argv, i, inline, flag, "an IP address")?; + a.share_bind = Some( + address + .parse() + .map_err(|_| usage("invalid --share-bind address"))?, + ); + } "--offline" => { no_value(inline, flag)?; a.offline = true; diff --git a/oxa/oxa-cli/src/dispatch.rs b/oxa/oxa-cli/src/dispatch.rs index 8e502945c..cb139948a 100644 --- a/oxa/oxa-cli/src/dispatch.rs +++ b/oxa/oxa-cli/src/dispatch.rs @@ -139,6 +139,31 @@ pub fn run(argv: &[String], env: &Env, io: Stdio) -> i32 { crate::netguard::install(a.offline, env, a.session_dir.as_deref(), &cwd); + #[cfg(feature = "share")] + if !a.share_models.is_empty() || a.share_bind.is_some() { + let refusal = if crate::share_cmd::is_agent(env) { + Some(OxaError::new( + codes::INVALID, + "Guest serving requires the owner outside an agent tool", + )) + } else if a.share_models.is_empty() { + Some(OxaError::new( + codes::USAGE, + "--share-bind requires --share-model ALIAS", + )) + } else if !loads_extensions || choose_mode(&a, stdin_is_tty, stdout_is_tty) != RunMode::Interactive { + Some(OxaError::new( + codes::USAGE, + "--share-model requires an interactive owner session", + )) + } else { + oxa_proto::check_online("starting the LAN sharing listener").err() + }; + if let Some(error) = refusal { + return report(&mut *stderr, &error); + } + } + if let Some(sub) = a.subcommand.clone() { let dirs = match dirs_from_env(env, None) { Ok(d) => d, @@ -148,14 +173,7 @@ pub fn run(argv: &[String], env: &Env, io: Stdio) -> i32 { #[cfg(feature = "share")] Subcommand::Share(words) => { let serve = words.first().is_some_and(|word| word == "serve"); - // Env::get treats empty values as unset; owner-command policy - // refuses marker presence, including an empty value. - if env.pairs().iter().any(|(name, _)| { - matches!( - name.as_str(), - "OXA_CODING_AGENT" | "AI_AGENT" | "OXA_SUBAGENT_DEPTH" - ) - }) { + if crate::share_cmd::is_agent(env) { return report( &mut *stderr, &OxaError::new( @@ -402,6 +420,8 @@ pub fn run(argv: &[String], env: &Env, io: Stdio) -> i32 { /// Everything decided before a session is opened. struct Prep { built: Built, + #[cfg(feature = "share")] + sharing: Option, open: Open, session_id: Option, notices: Vec<(Level, String)>, @@ -709,12 +729,23 @@ fn prepare( settings.string("defaultModel") } }); + #[cfg(feature = "share")] + let sharing = (!a.share_models.is_empty()) + .then(|| { + crate::share_cmd::prepare_session(&user_settings, dirs, a.share_models.clone(), a.share_bind) + }) + .transpose()?; + #[cfg(feature = "share")] + let shared_local = sharing.as_ref().map(|prepared| prepared.local.clone()); + #[cfg(not(feature = "share"))] + let shared_local = None; let engine = child.wrap(crate::in_process::select_observed( &user_settings, &settings, dirs, requested_model.as_deref(), (!child.nested() && mode != RunMode::Rpc).then(|| cloud_diagnostics.clone()), + shared_local, || engine_with_settings(&endpoint, &settings), )?); @@ -938,6 +969,8 @@ fn prepare( }; Ok(Prep { built, + #[cfg(feature = "share")] + sharing, open, session_id, notices, @@ -1458,6 +1491,8 @@ fn run_tui(a: &Args, env: &Env, dirs: &Dirs, cwd: &Path, prep: Prep, stderr: &mu let Prep { built, + #[cfg(feature = "share")] + sharing, open, session_id, mut notices, @@ -1469,6 +1504,18 @@ fn run_tui(a: &Args, env: &Env, dirs: &Dirs, cwd: &Path, prep: Prep, stderr: &mu Ok(s) => s, Err(e) => return report(stderr, &e), }; + #[cfg(feature = "share")] + let _guest_listener = match sharing.map(crate::share_cmd::Prepared::start).transpose() { + Ok(Some((running, address))) => { + notices.push(( + Level::Info, + format!("Guest OpenAI API listening at http://{address}/v1"), + )); + Some(running) + } + Ok(None) => None, + Err(error) => return report(stderr, &error), + }; notices.extend(startup_notices(&built, &session)); let yolo = built.yolo.clone(); // Its diagnostics are among the startup notices (`prepare`). diff --git a/oxa/oxa-cli/src/in_process.rs b/oxa/oxa-cli/src/in_process.rs index 84445c58a..96da4c5e7 100644 --- a/oxa/oxa-cli/src/in_process.rs +++ b/oxa/oxa-cli/src/in_process.rs @@ -93,7 +93,7 @@ pub(crate) fn select( requested: Option<&str>, http: impl FnOnce() -> Result, OxaError>, ) -> Result, OxaError> { - select_observed(user, effective, dirs, requested, None, http) + select_observed(user, effective, dirs, requested, None, None, http) } pub(crate) fn select_observed( @@ -102,8 +102,21 @@ pub(crate) fn select_observed( dirs: &Dirs, requested: Option<&str>, diagnostics: Option, + shared_local: Option>, http: impl FnOnce() -> Result, OxaError>, ) -> Result, OxaError> { + let local_engine = || match shared_local { + Some(engine) => Ok(engine as Arc), + None => oxc_local::configured_engine( + user.get("oxa.local"), + dirs.agent_dir.join("local-engine"), + oxa_engine::StreamOpts { + silence_limit_ms: crate::dispatch::stream_silence_timeout(effective) + .unwrap_or(365 * 24 * 3600 * 1000), + ..oxa_engine::StreamOpts::default() + }, + ), + }; if user.bool(oxc_local::LOCAL_SETTING, false) && user.bool(SETTING, false) { let aliases = |path| { user.get(path) @@ -112,15 +125,7 @@ pub(crate) fn select_observed( .unwrap_or_default() }; return routing::Routed { - local: oxc_local::configured_engine( - user.get("oxa.local"), - dirs.agent_dir.join("local-engine"), - oxa_engine::StreamOpts { - silence_limit_ms: crate::dispatch::stream_silence_timeout(effective) - .unwrap_or(365 * 24 * 3600 * 1000), - ..oxa_engine::StreamOpts::default() - }, - ), + local: local_engine(), cloud: if oxa_proto::offline() { Err(oxa_proto::offline_error("the in-process cloud gateway")) } else { @@ -134,15 +139,7 @@ pub(crate) fn select_observed( .into_engine(requested); } if user.bool(oxc_local::LOCAL_SETTING, false) { - return oxc_local::configured_engine( - user.get("oxa.local"), - dirs.agent_dir.join("local-engine"), - oxa_engine::StreamOpts { - silence_limit_ms: crate::dispatch::stream_silence_timeout(effective) - .unwrap_or(365 * 24 * 3600 * 1000), - ..oxa_engine::StreamOpts::default() - }, - ); + return local_engine(); } if !user.bool(SETTING, false) { return http(); diff --git a/oxa/oxa-cli/src/share_cmd.rs b/oxa/oxa-cli/src/share_cmd.rs index a2cc41429..c11ef06c5 100644 --- a/oxa/oxa-cli/src/share_cmd.rs +++ b/oxa/oxa-cli/src/share_cmd.rs @@ -1,5 +1,5 @@ //! Foreground opt-in native LAN listener. Only user settings can select its engine. -use oxa_config::{Dirs, Settings}; +use oxa_config::{Dirs, Env, Settings}; use oxa_proto::{codes, CancelToken, ChatRequest, Engine, OxaError, StreamEvent}; use oxc_gateway::share::{ keys::Store, @@ -9,15 +9,43 @@ use oxc_gateway::share::{ }; use std::{ io::Write, + net::{IpAddr, SocketAddr}, path::{Path, PathBuf}, sync::Arc, time::Duration, }; -pub(crate) fn run(args: &[String], dirs: &Dirs, cwd: &Path, out: &mut dyn Write) -> Result<(), OxaError> { +const TAILSCALE_SOCKET: &str = "/var/run/tailscale/tailscaled.sock"; + +pub(crate) fn is_agent(env: &Env) -> bool { + // Marker presence, including an empty value, is enough to refuse. + env.pairs().iter().any(|(name, _)| { + matches!( + name.as_str(), + "OXA_CODING_AGENT" | "AI_AGENT" | "OXA_SUBAGENT_DEPTH" + ) + }) +} + +pub(crate) struct Prepared { + pub local: Arc, + keys: Arc, + models: Vec, + tailnet: Arc, + config: Config, +} + +fn prepare( + settings: &Settings, + dirs: &Dirs, + config: Config, + aliases: Vec, + socket: PathBuf, +) -> Result { oxa_proto::check_online("starting the LAN sharing listener")?; - let (config, aliases, socket) = options(args)?; - let settings = Settings::load(dirs, cwd, false); + if aliases.is_empty() || aliases.len() > 64 { + return Err(usage()); + } if !settings.bool(oxc_local::LOCAL_SETTING, false) { return Err(OxaError::new( codes::INVALID, @@ -62,16 +90,100 @@ pub(crate) fn run(args: &[String], dirs: &Dirs, cwd: &Path, out: &mut dyn Write) }) }) .collect::, OxaError>>()?; - let answer = native::Native::new( + let tailnet = Arc::new( + LocalApi::new(socket, Duration::from_secs(5)) + .map_err(|error| OxaError::new(codes::INVALID, error.to_string()))?, + ); + Ok(Prepared { + local, + keys, models, - move |request: &ChatRequest<'_>, cancel: &CancelToken, on: &mut dyn FnMut(StreamEvent)| { - local.chat_guest(request, cancel, on) - }, - ) - .map_err(service_error)?; - let tailnet = LocalApi::new(socket, Duration::from_secs(5)) - .map_err(|error| OxaError::new(codes::INVALID, error.to_string()))?; + tailnet, + config, + }) +} + +pub(crate) fn prepare_session( + settings: &Settings, + dirs: &Dirs, + aliases: Vec, + bind: Option, +) -> Result { + let config = Config { + binding: bind.map_or(Binding::Tailnet, Binding::Explicit), + ..Config::default() + }; + prepare(settings, dirs, config, aliases, PathBuf::from(TAILSCALE_SOCKET)) +} + +impl Prepared { + fn serve( + self, + stop: CancelToken, + ready: impl FnOnce(SocketAddr) -> Result<(), Failure>, + ) -> Result<(), OxaError> { + let Self { + local, + keys, + models, + tailnet, + config, + } = self; + let answer = native::Native::new( + models, + move |request: &ChatRequest<'_>, cancel: &CancelToken, on: &mut dyn FnMut(StreamEvent)| { + local.chat_guest(request, cancel, on) + }, + ) + .map_err(service_error)?; + native::serve(keys, Arc::new(answer), tailnet, config, stop, ready).map_err(service_error) + } + pub(super) fn start(self) -> Result<(Serving, SocketAddr), OxaError> { + let stop = CancelToken::new(); + let thread_stop = stop.clone(); + let (send, receive) = std::sync::mpsc::channel(); + let thread = std::thread::Builder::new() + .spawn(move || { + self.serve(thread_stop, |address| { + send.send(address).map_err(|_| Failure::Failed) + }) + }) + .map_err(|error| OxaError::io("cannot start the guest listener", &error))?; + match receive.recv() { + Ok(address) => Ok(( + Serving { + stop, + thread: Some(thread), + }, + address, + )), + Err(_) => match thread.join() { + Ok(Err(error)) => Err(error), + _ => Err(service_error(Failure::Failed)), + }, + } + } +} + +pub(super) struct Serving { + stop: CancelToken, + thread: Option>>, +} + +impl Drop for Serving { + fn drop(&mut self) { + self.stop.cancel(); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } +} + +pub(crate) fn run(args: &[String], dirs: &Dirs, cwd: &Path, out: &mut dyn Write) -> Result<(), OxaError> { + let (config, aliases, socket) = options(args)?; + let settings = Settings::load(dirs, cwd, false); + let prepared = prepare(&settings, dirs, config, aliases, socket)?; let stop = CancelToken::new(); let mut signals = signal_hook::iterator::Signals::new([signal_hook::consts::SIGINT, signal_hook::consts::SIGTERM]) @@ -86,27 +198,20 @@ pub(crate) fn run(args: &[String], dirs: &Dirs, cwd: &Path, out: &mut dyn Write) }) .map_err(|error| OxaError::io("cannot start listener shutdown handler", &error))?; - let result = native::serve( - keys, - Arc::new(answer), - Arc::new(tailnet), - config, - stop, - |address| { - writeln!(out, "Guest OpenAI API listening at http://{address}/v1") - .and_then(|()| out.flush()) - .map_err(|_| Failure::Failed) - }, - ); + let result = prepared.serve(stop, |address| { + writeln!(out, "Guest OpenAI API listening at http://{address}/v1") + .and_then(|()| out.flush()) + .map_err(|_| Failure::Failed) + }); signal_handle.close(); let _ = signal_thread.join(); - result.map_err(service_error) + result } fn options(args: &[String]) -> Result<(Config, Vec, PathBuf), OxaError> { let mut config = Config::default(); let mut aliases = Vec::new(); - let mut socket = PathBuf::from("/var/run/tailscale/tailscaled.sock"); + let mut socket = PathBuf::from(TAILSCALE_SOCKET); let mut args = args.iter(); while let Some(arg) = args.next() { if arg == "--same-owner" { @@ -144,3 +249,52 @@ fn service_error(error: Failure) -> OxaError { }; OxaError::new(codes::INVALID, message) } + +#[cfg(test)] +mod tests { + use super::*; + use oxc_local::native::LocalEngine; + + #[test] + fn owner_router_and_listener_use_the_same_local_engine() { + let home = oxa_fake::TestEnv::new(); + let dirs = Dirs::under(home.root()); + let settings = Settings::from_values( + serde_json::json!({"oxa":{ + "engine":{"inProcessLocal":true}, + "local":{"binary":"/synthetic/oxe-server", + "models":{"synthetic":{"model":"/synthetic/model.gguf", + "arguments":["--engine","stub"]}}, + "limits":{"synthetic":{"context_window":4096,"max_output":64, + "allocation_budget":{"kv_bytes_per_position":1,"scratch_bytes":0, + "mtp_state_bytes":0,"margin_bytes":1044480}}}} + }}), + serde_json::json!({}), + ); + Store::initialize(&dirs.home.join(".oxa/share")).unwrap(); + let prepared = prepare_session( + &settings, + &dirs, + vec!["synthetic".into()], + Some(std::net::Ipv4Addr::LOCALHOST.into()), + ) + .unwrap(); + let local: Arc = prepared.local.clone(); + let owner = crate::in_process::select_observed( + &settings, + &settings, + &dirs, + Some("synthetic"), + None, + Some(local.clone()), + || panic!("owner should use the shared native engine"), + ) + .unwrap(); + let local_api: Arc = local; + assert!(Arc::ptr_eq(&owner, &local_api)); + let (running, address) = prepared.start().unwrap(); + assert!(std::net::TcpStream::connect_timeout(&address, Duration::from_secs(2)).is_ok()); + drop(running); + assert!(std::net::TcpStream::connect_timeout(&address, Duration::from_secs(2)).is_err()); + } +} diff --git a/oxa/oxa-cli/tests/share_keys.rs b/oxa/oxa-cli/tests/share_keys.rs index 923b74c66..b310cca8c 100644 --- a/oxa/oxa-cli/tests/share_keys.rs +++ b/oxa/oxa-cli/tests/share_keys.rs @@ -468,3 +468,24 @@ fn foreground_serve_lists_only_the_owner_selected_local_models_on_loopback() { assert!(!reply.contains("synthetic-hidden")); assert!(!reply.contains(bearer)); } + +#[cfg(feature = "share")] +#[test] +fn owner_session_sharing_flags_refuse_noninteractive_and_agent_invocations() { + let home = Home::new(); + let args = [ + "--share-model", + "synthetic-visible", + "--share-bind", + "127.0.0.1", + "-p", + "probe", + ]; + let headless = home.run(&args); + assert!(!headless.status.success()); + assert!(String::from_utf8_lossy(&headless.stderr).contains("requires an interactive owner session")); + let agent = home.marked(&args, "OXA_CODING_AGENT", ""); + assert!(!agent.status.success()); + assert!(String::from_utf8_lossy(&agent.stderr).contains("requires the owner outside an agent tool")); + assert!(!home.0.join(".oxa/share").exists()); +} From f836e2431b23fb1174be4e8033904426f276b01d Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom Date: Wed, 18 Nov 2026 00:52:53 +0100 Subject: [PATCH 8/9] Test project stream timeout when sharing the native engine --- oxa/oxa-cli/src/dispatch.rs | 8 +++++++- oxa/oxa-cli/src/in_process.rs | 21 +++++++++++++++++++++ oxa/oxa-cli/src/share_cmd.rs | 25 ++++++++++++++----------- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/oxa/oxa-cli/src/dispatch.rs b/oxa/oxa-cli/src/dispatch.rs index b6d2156ad..41d5909c7 100644 --- a/oxa/oxa-cli/src/dispatch.rs +++ b/oxa/oxa-cli/src/dispatch.rs @@ -769,7 +769,13 @@ fn prepare( #[cfg(feature = "share")] let sharing = (!a.share_models.is_empty()) .then(|| { - crate::share_cmd::prepare_session(&user_settings, dirs, a.share_models.clone(), a.share_bind) + crate::share_cmd::prepare_session( + &user_settings, + &settings, + dirs, + a.share_models.clone(), + a.share_bind, + ) }) .transpose()?; #[cfg(feature = "share")] diff --git a/oxa/oxa-cli/src/in_process.rs b/oxa/oxa-cli/src/in_process.rs index 96da4c5e7..879f58108 100644 --- a/oxa/oxa-cli/src/in_process.rs +++ b/oxa/oxa-cli/src/in_process.rs @@ -96,6 +96,27 @@ pub(crate) fn select( select_observed(user, effective, dirs, requested, None, None, http) } +#[cfg(any(feature = "share", test))] +pub(crate) fn local_engine_settings<'a>( + user: &'a Settings, + _effective: &Settings, +) -> (Option<&'a Value>, oxa_engine::StreamOpts) { + let stream = oxa_engine::StreamOpts { + silence_limit_ms: crate::dispatch::stream_silence_timeout(user).unwrap_or(365 * 24 * 3600 * 1000), + ..oxa_engine::StreamOpts::default() + }; + (user.get("oxa.local"), stream) +} + +#[test] +fn shared_local_engine_uses_effective_stream_timeout() { + let user_values = serde_json::json!({"httpIdleTimeoutMs": 1700}); + let user = Settings::from_values(user_values.clone(), serde_json::json!({})); + let effective = Settings::from_values(user_values, serde_json::json!({"httpIdleTimeoutMs": 2400})); + let (_, stream) = local_engine_settings(&user, &effective); + assert_eq!(stream.silence_limit_ms, 2400); +} + pub(crate) fn select_observed( user: &Settings, effective: &Settings, diff --git a/oxa/oxa-cli/src/share_cmd.rs b/oxa/oxa-cli/src/share_cmd.rs index c11ef06c5..4a31147e0 100644 --- a/oxa/oxa-cli/src/share_cmd.rs +++ b/oxa/oxa-cli/src/share_cmd.rs @@ -37,6 +37,7 @@ pub(crate) struct Prepared { fn prepare( settings: &Settings, + effective: &Settings, dirs: &Dirs, config: Config, aliases: Vec, @@ -56,15 +57,8 @@ fn prepare( Store::open(&dirs.home.join(".oxa/share")) .map_err(|error| OxaError::new(codes::INVALID, error.to_string()))?, ); - let local = oxc_local::configured_local_engine( - settings.get("oxa.local"), - dirs.agent_dir.join("local-engine"), - oxa_engine::StreamOpts { - silence_limit_ms: crate::dispatch::stream_silence_timeout(&settings) - .unwrap_or(365 * 24 * 3600 * 1000), - ..oxa_engine::StreamOpts::default() - }, - )?; + let (launch, stream) = crate::in_process::local_engine_settings(settings, effective); + let local = oxc_local::configured_local_engine(launch, dirs.agent_dir.join("local-engine"), stream)?; let catalog = local.info(None)?; let models = aliases .into_iter() @@ -105,6 +99,7 @@ fn prepare( pub(crate) fn prepare_session( settings: &Settings, + effective: &Settings, dirs: &Dirs, aliases: Vec, bind: Option, @@ -113,7 +108,14 @@ pub(crate) fn prepare_session( binding: bind.map_or(Binding::Tailnet, Binding::Explicit), ..Config::default() }; - prepare(settings, dirs, config, aliases, PathBuf::from(TAILSCALE_SOCKET)) + prepare( + settings, + effective, + dirs, + config, + aliases, + PathBuf::from(TAILSCALE_SOCKET), + ) } impl Prepared { @@ -183,7 +185,7 @@ impl Drop for Serving { pub(crate) fn run(args: &[String], dirs: &Dirs, cwd: &Path, out: &mut dyn Write) -> Result<(), OxaError> { let (config, aliases, socket) = options(args)?; let settings = Settings::load(dirs, cwd, false); - let prepared = prepare(&settings, dirs, config, aliases, socket)?; + let prepared = prepare(&settings, &settings, dirs, config, aliases, socket)?; let stop = CancelToken::new(); let mut signals = signal_hook::iterator::Signals::new([signal_hook::consts::SIGINT, signal_hook::consts::SIGTERM]) @@ -273,6 +275,7 @@ mod tests { ); Store::initialize(&dirs.home.join(".oxa/share")).unwrap(); let prepared = prepare_session( + &settings, &settings, &dirs, vec!["synthetic".into()], From dc6fb8617b57c764591e74dd7bf1d5769d035a75 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom Date: Wed, 18 Nov 2026 00:53:04 +0100 Subject: [PATCH 9/9] Preserve effective timeout when sharing the native engine --- oxa/oxa-cli/src/in_process.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/oxa/oxa-cli/src/in_process.rs b/oxa/oxa-cli/src/in_process.rs index 879f58108..e8ae07462 100644 --- a/oxa/oxa-cli/src/in_process.rs +++ b/oxa/oxa-cli/src/in_process.rs @@ -96,13 +96,14 @@ pub(crate) fn select( select_observed(user, effective, dirs, requested, None, None, http) } -#[cfg(any(feature = "share", test))] +/// Native launch configuration is user-only. Stream policy follows the effective session. pub(crate) fn local_engine_settings<'a>( user: &'a Settings, - _effective: &Settings, + effective: &Settings, ) -> (Option<&'a Value>, oxa_engine::StreamOpts) { let stream = oxa_engine::StreamOpts { - silence_limit_ms: crate::dispatch::stream_silence_timeout(user).unwrap_or(365 * 24 * 3600 * 1000), + silence_limit_ms: crate::dispatch::stream_silence_timeout(effective) + .unwrap_or(365 * 24 * 3600 * 1000), ..oxa_engine::StreamOpts::default() }; (user.get("oxa.local"), stream) @@ -128,15 +129,10 @@ pub(crate) fn select_observed( ) -> Result, OxaError> { let local_engine = || match shared_local { Some(engine) => Ok(engine as Arc), - None => oxc_local::configured_engine( - user.get("oxa.local"), - dirs.agent_dir.join("local-engine"), - oxa_engine::StreamOpts { - silence_limit_ms: crate::dispatch::stream_silence_timeout(effective) - .unwrap_or(365 * 24 * 3600 * 1000), - ..oxa_engine::StreamOpts::default() - }, - ), + None => { + let (launch, stream) = local_engine_settings(user, effective); + oxc_local::configured_engine(launch, dirs.agent_dir.join("local-engine"), stream) + } }; if user.bool(oxc_local::LOCAL_SETTING, false) && user.bool(SETTING, false) { let aliases = |path| {