Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.
Rust ai-agentsai-gatewayclaude-codecodexcoding-agentcost-optimizationdeepseekllm-gatewayllm-routerlocal-firstmodel-routingrusttask-routingtauri
//! A task owns its context descriptor and continuation binding, never the borrowed client root.
//! Native transcripts remain opaque. Only Claude's exact file is inspected for flush/readiness;
//! Codex/Qoder availability is established by exact session/load, not a shared-history scan.
use super::*;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::io::Read;
use unicode_normalization::UnicodeNormalization;
const CONTEXT_FILE: &str = "borrowed-native-context.json";
const SESSION_FILE: &str = "native-session-binding.json";
const METADATA_LIMIT: u64 = 32 * 1024;
/// The original context selected for a task. These paths convey no cleanup ownership.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BorrowedNativeContext {
pub home: PathBuf,
pub config_root: PathBuf,
pub workspace: PathBuf,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub opencode: Option<hiroute_integrations::OpenCodeNativeContext>,
}
#[derive(Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct ContextDescriptor {
version: u32,
harness: WorkerHarnessV1,
context: BorrowedNativeContext,
}
#[derive(Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct SessionBinding {
version: u32,
context_digest: CanonicalDigest,
native_session_id: String,
}
impl TaskSessionRoot {
/// Bind a new task once. Continuing an old private task never implicitly migrates it.
pub fn bind_borrowed_context(
&self,
home: &Path,
config_root: &Path,
workspace: &Path,
) -> Result<(), DelegationErrorV1> {
if !self.newly_created {
return Err(DelegationErrorV1::Conflict);
}
let context = BorrowedNativeContext {
home: canonical_context_path(home, false)?,
config_root: canonical_context_path(config_root, true)?,
workspace: canonical_context_path(workspace, false)?,
opencode: None,
};
if context.config_root.starts_with(&self.path) {
return Err(DelegationErrorV1::InvalidArguments);
}
write_owned_json(
&self.path,
CONTEXT_FILE,
&ContextDescriptor {
version: 1,
harness: self.harness,
context,
},
)
}
/// Only Codex/Claude accept a missing descriptor as the registered legacy private-root
/// format. Malformed, replaced or mismatched metadata never selects a recovery reader.
pub fn borrowed_context(&self) -> Result<Option<BorrowedNativeContext>, DelegationErrorV1> {
read_context(&self.path, self.harness).map(|value| value.map(|value| value.context))
}
pub fn bind_opencode_context(
&self,
native: &hiroute_integrations::OpenCodeNativeContext,
workspace: &Path,
) -> Result<(), DelegationErrorV1> {
if !self.newly_created || self.harness != WorkerHarnessV1::OpenCode {
return Err(DelegationErrorV1::Conflict);
}
let mut native = native.clone();
native.home = canonical_context_path(&native.home, false)?;
for root in [
&mut native.config_root,
&mut native.data_root,
&mut native.cache_root,
&mut native.state_root,
] {
*root = canonical_context_path(root, true)?;
}
if let Some(root) = &mut native.extra_config_root {
*root = canonical_context_path(root, true)?;
}
// Files are selectors, not directories. Resolve their existing parent prefix only.
for file in std::iter::once(&mut native.database).chain(native.config_file.iter_mut()) {
let name = file
.file_name()
.ok_or(DelegationErrorV1::InvalidArguments)?
.to_owned();
*file = canonical_context_path(
file.parent().ok_or(DelegationErrorV1::InvalidArguments)?,
true,
)?
.join(name);
}
validate_opencode_paths(&native, &self.path)?;
let context = BorrowedNativeContext {
home: native.home.clone(),
config_root: native.config_root.clone(),
workspace: canonical_context_path(workspace, false)?,
opencode: Some(native),
};
if context.config_root.starts_with(&self.path) {
return Err(DelegationErrorV1::InvalidArguments);
}
write_owned_json(
&self.path,
CONTEXT_FILE,
&ContextDescriptor {
version: 2,
harness: self.harness,
context,
},
)
}
/// Match the task's persisted ACP identity before attempting Continue. For borrowed Codex/Qoder,
/// this proves only the retained binding: session/load must still confirm native history.
pub fn verify_native_session(&self, native_session_id: &str) -> Result<(), DelegationErrorV1> {
match read_context(&self.path, self.harness)? {
Some(context) => {
let binding = read_binding(&self.path, &context)?;
if binding.native_session_id != native_session_id {
return Err(DelegationErrorV1::ResumeUnavailable);
}
verify_borrowed_history(&self.path, &context, native_session_id)
}
None => legacy_native_history(&self.path, self.harness, native_session_id).map(|_| ()),
}
}
/// Locate an actual transcript for lifecycle observation. A Codex/Qoder borrowed binding
/// is deliberately not returned as a transcript; its contents say nothing about disk flush.
pub(crate) fn native_transcript_path(
root: &Path,
harness: WorkerHarnessV1,
native_session_id: &str,
) -> Result<PathBuf, DelegationErrorV1> {
if let Some(context) = read_context(root, harness)? {
return if harness == WorkerHarnessV1::Pi {
pi_transcript(root, &context, native_session_id)
} else {
claude_transcript(&context, native_session_id)
};
}
legacy_native_history(root, harness, native_session_id)?
.pop()
.map(|relative| root.join(relative))
.ok_or(DelegationErrorV1::ResumeUnavailable)
}
}
pub(super) fn read_context(
root: &Path,
harness: WorkerHarnessV1,
) -> Result<Option<ContextDescriptor>, DelegationErrorV1> {
checked_directory(root).map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
match fs::symlink_metadata(root.join(CONTEXT_FILE)) {
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return match fs::symlink_metadata(root.join(SESSION_FILE)) {
Err(error)
if error.kind() == std::io::ErrorKind::NotFound
&& !matches!(
harness,
WorkerHarnessV1::QoderCli
| WorkerHarnessV1::Pi
| WorkerHarnessV1::DeepseekHarness
| WorkerHarnessV1::OpenCode
) =>
{
Ok(None)
}
_ => Err(DelegationErrorV1::ResumeUnavailable),
};
}
Err(_) => return Err(DelegationErrorV1::ResumeUnavailable),
Ok(_) => {}
}
let descriptor: ContextDescriptor = read_owned_json(root, CONTEXT_FILE)?;
if descriptor.harness != harness
|| if harness == WorkerHarnessV1::OpenCode {
descriptor.version != 2 || descriptor.context.opencode.is_none()
} else {
descriptor.version != 1 || descriptor.context.opencode.is_some()
}
{
return Err(DelegationErrorV1::ResumeUnavailable);
}
let context = &descriptor.context;
if let Some(native) = &context.opencode {
if native.home != context.home || native.config_root != context.config_root {
return Err(DelegationErrorV1::ResumeUnavailable);
}
validate_opencode_paths(native, root)?;
}
for (path, missing) in [
(&context.home, false),
(&context.config_root, true),
(&context.workspace, false),
] {
if canonical_context_path(path, missing)
.map_err(|_| DelegationErrorV1::ResumeUnavailable)?
!= *path
{
return Err(DelegationErrorV1::ResumeUnavailable);
}
}
if context.config_root.starts_with(root) {
return Err(DelegationErrorV1::ResumeUnavailable);
}
Ok(Some(descriptor))
}
pub(super) fn retain_borrowed_session(
root: &Path,
harness: WorkerHarnessV1,
native_session_id: &str,
) -> Result<Vec<String>, DelegationErrorV1> {
let context = read_context(root, harness)?.ok_or(DelegationErrorV1::ResumeUnavailable)?;
verify_borrowed_history(root, &context, native_session_id)?;
write_owned_json(
root,
SESSION_FILE,
&SessionBinding {
version: 1,
context_digest: context_digest(&context)?,
native_session_id: native_session_id.to_owned(),
},
)?;
Ok(if harness == WorkerHarnessV1::Pi {
vec![SESSION_FILE.to_owned(), "native-pi.jsonl".into()]
} else {
vec![SESSION_FILE.to_owned()]
})
}
pub(super) fn verify_continuation_materials(
root: &Path,
harness: WorkerHarnessV1,
required: &[PathBuf],
) -> Result<(), DelegationErrorV1> {
if let Some(context) = read_context(root, harness)? {
let expected = if harness == WorkerHarnessV1::Pi {
vec![
PathBuf::from(SESSION_FILE),
PathBuf::from("native-pi.jsonl"),
]
} else {
vec![PathBuf::from(SESSION_FILE)]
};
if required != expected {
return Err(DelegationErrorV1::ResumeUnavailable);
}
let binding = read_binding(root, &context)?;
verify_borrowed_history(root, &context, &binding.native_session_id)?;
}
Ok(())
}
fn read_binding(
root: &Path,
context: &ContextDescriptor,
) -> Result<SessionBinding, DelegationErrorV1> {
let binding: SessionBinding = read_owned_json(root, SESSION_FILE)?;
if binding.version != 1 || binding.context_digest != context_digest(context)? {
return Err(DelegationErrorV1::ResumeUnavailable);
}
validate_session_id(&binding.native_session_id)?;
Ok(binding)
}
fn context_digest(context: &ContextDescriptor) -> Result<CanonicalDigest, DelegationErrorV1> {
CanonicalDigest::of(context).map_err(|_| DelegationErrorV1::ResumeUnavailable)
}
fn verify_borrowed_history(
root: &Path,
context: &ContextDescriptor,
native_session_id: &str,
) -> Result<(), DelegationErrorV1> {
validate_session_id(native_session_id)?;
if context.harness == WorkerHarnessV1::OpenCode {
let native = context
.context
.opencode
.as_ref()
.ok_or(DelegationErrorV1::ResumeUnavailable)?;
let metadata = std::fs::symlink_metadata(&native.database)
.map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
if !metadata.is_file() || metadata.file_type().is_symlink() {
return Err(DelegationErrorV1::ResumeUnavailable);
}
}
if context.harness == WorkerHarnessV1::Pi {
pi_transcript(root, context, native_session_id)?;
}
if context.harness == WorkerHarnessV1::ClaudeCode {
claude_transcript(context, native_session_id)?;
}
Ok(())
}
/// Pi's exact file is owned by this task; native open must never recreate missing history.
fn pi_transcript(
root: &Path,
context: &ContextDescriptor,
id: &str,
) -> Result<PathBuf, DelegationErrorV1> {
let relative = Path::new("native-pi.jsonl");
check_history(root, relative).map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
let path = root.join(relative);
let file = fs::File::open(&path).map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
use std::io::BufRead;
let mut reader = std::io::BufReader::new(file.take(METADATA_LIMIT));
let mut header = String::new();
reader
.read_line(&mut header)
.map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
let header: serde_json::Value =
serde_json::from_str(&header).map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
if header["type"] != "session"
|| header["version"] != 3
|| header["id"] != id
|| header["cwd"].as_str() != context.context.workspace.to_str()
{
return Err(DelegationErrorV1::ResumeUnavailable);
}
Ok(path)
}
fn claude_transcript(
descriptor: &ContextDescriptor,
native_session_id: &str,
) -> Result<PathBuf, DelegationErrorV1> {
validate_session_id(native_session_id)?;
if descriptor.harness != WorkerHarnessV1::ClaudeCode {
return Err(DelegationErrorV1::ResumeUnavailable);
}
let context = &descriptor.context;
let relative = PathBuf::from("projects")
.join(claude_workspace_key(&context.workspace)?)
.join(format!("{native_session_id}.jsonl"));
// This checks only the exact path's components, never siblings or the shared root's mode.
check_history(&context.config_root, &relative)?;
Ok(context.config_root.join(relative))
}
fn validate_session_id(id: &str) -> Result<(), DelegationErrorV1> {
if id.is_empty()
|| id.len() > 256
|| id
.chars()
.any(|ch| ch.is_control() || matches!(ch, '/' | '\\' | ':'))
{
return Err(DelegationErrorV1::ResumeUnavailable);
}
Ok(())
}
/// Claude Code 2.1.231 and SDK 0.3.215 encode NFC cwd using JavaScript UTF-16 code units.
/// The 200-character key limit and signed 32-bit hash also apply to non-BMP workspace names.
fn claude_workspace_key(workspace: &Path) -> Result<String, DelegationErrorV1> {
let text = workspace
.to_str()
.ok_or(DelegationErrorV1::ResumeUnavailable)?;
let normalized: String = text.nfc().collect();
let mut hash = 0_i32;
let mut key = String::new();
for unit in normalized.encode_utf16() {
hash = hash.wrapping_mul(31).wrapping_add(i32::from(unit));
key.push(if unit <= 127 && (unit as u8).is_ascii_alphanumeric() {
char::from(unit as u8)
} else {
'-'
});
}
if key.len() > 200 {
key.truncate(200);
key.push('-');
key.push_str(&base36(hash.unsigned_abs()));
}
Ok(key)
}
fn base36(mut value: u32) -> String {
let mut digits = Vec::new();
loop {
digits.push(char::from(
b"0123456789abcdefghijklmnopqrstuvwxyz"[(value % 36) as usize],
));
value /= 36;
if value == 0 {
return digits.into_iter().rev().collect();
}
}
}
fn canonical_context_path(path: &Path, allow_missing: bool) -> Result<PathBuf, DelegationErrorV1> {
if !path.is_absolute()
|| path
.to_str()
.is_none_or(|path| path.contains(['\0', '\r', '\n']))
|| path
.components()
.any(|part| matches!(part, Component::ParentDir))
{
return Err(DelegationErrorV1::InvalidArguments);
}
match fs::canonicalize(path) {
Ok(path) if path.is_dir() => Ok(super::super::native_context::native_path(&path)),
Err(error) if allow_missing && error.kind() == std::io::ErrorKind::NotFound => {
// Clients may create a default config root on their first run. Resolve its existing
// prefix without creating, chmodding, copying or adopting anything in the user root.
let parent = path.parent().ok_or(DelegationErrorV1::InvalidArguments)?;
let name = path
.file_name()
.ok_or(DelegationErrorV1::InvalidArguments)?;
Ok(canonical_context_path(parent, true)?.join(name))
}
_ => Err(DelegationErrorV1::ResumeUnavailable),
}
}
fn read_owned_json<T: DeserializeOwned>(root: &Path, name: &str) -> Result<T, DelegationErrorV1> {
let path = root.join(name);
let path_metadata =
fs::symlink_metadata(&path).map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
if linked(&path_metadata) || !path_metadata.is_file() || path_metadata.len() > METADATA_LIMIT {
return Err(DelegationErrorV1::ResumeUnavailable);
}
let file = open_marker(&path)?;
#[cfg(windows)]
{
use hiroute_platform::windows::security::{file_link_count, verify_owner_only_handle};
verify_owner_only_handle(&file, false).map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
if file_link_count(&file).map_err(|_| DelegationErrorV1::ResumeUnavailable)? != 1 {
return Err(DelegationErrorV1::ResumeUnavailable);
}
}
let metadata = file
.metadata()
.map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
if !metadata.is_file() || metadata.len() > METADATA_LIMIT {
return Err(DelegationErrorV1::ResumeUnavailable);
}
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
if metadata.nlink() != 1 {
return Err(DelegationErrorV1::ResumeUnavailable);
}
}
let mut bytes = Vec::new();
file.take(METADATA_LIMIT + 1)
.read_to_end(&mut bytes)
.map_err(|_| DelegationErrorV1::ResumeUnavailable)?;
if bytes.len() as u64 > METADATA_LIMIT {
return Err(DelegationErrorV1::ResumeUnavailable);
}
serde_json::from_slice(&bytes).map_err(|_| DelegationErrorV1::ResumeUnavailable)
}
fn write_owned_json<T: Serialize + DeserializeOwned + PartialEq>(
root: &Path,
name: &str,
value: &T,
) -> Result<(), DelegationErrorV1> {
checked_directory(root)?;
let bytes = serde_json::to_vec(value).map_err(|_| DelegationErrorV1::StorageUnavailable)?;
if bytes.len() as u64 > METADATA_LIMIT {
return Err(DelegationErrorV1::InvalidArguments);
}
match hiroute_platform::durable::write_private_new(&root.join(name), &bytes) {
Ok(()) => Ok(()),
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
if read_owned_json::<T>(root, name)? == *value {
Ok(())
} else {
Err(DelegationErrorV1::Conflict)
}
}
Err(_) => Err(DelegationErrorV1::StorageUnavailable),
}
}
#[cfg(test)]
#[path = "native_history_tests.rs"]
mod tests;
fn validate_opencode_paths(
native: &hiroute_integrations::OpenCodeNativeContext,
root: &Path,
) -> Result<(), DelegationErrorV1> {
let invalid = || DelegationErrorV1::ResumeUnavailable;
native.validate().map_err(|_| invalid())?;
// The child appends `opencode` to each XDG selector. A canonical leaf with another name
// cannot be reconstructed by taking its parent; reject it on New and persisted Continue.
for path in [
&native.config_root,
&native.data_root,
&native.cache_root,
&native.state_root,
] {
if path
.file_name()
.and_then(|name| name.to_str())
.is_none_or(|name| {
if cfg!(windows) {
!name.eq_ignore_ascii_case("opencode")
} else {
name != "opencode"
}
})
{
return Err(invalid());
}
}
for path in [
&native.home,
&native.config_root,
&native.data_root,
&native.cache_root,
&native.state_root,
]
.into_iter()
.chain(native.extra_config_root.iter())
{
if path.starts_with(root)
|| canonical_context_path(path, true).map_err(|_| invalid())? != *path
{
return Err(invalid());
}
}
for path in std::iter::once(&native.database).chain(native.config_file.iter()) {
let parent = path.parent().ok_or_else(invalid)?;
if path.starts_with(root)
|| canonical_context_path(parent, true).map_err(|_| invalid())? != parent
{
return Err(invalid());
}
}
Ok(())
}