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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ One doc per subject under `docs/architecture/`, each short enough to read in one
| [Identity](docs/architecture/identity.md) | Key pairs and the identity device, pairing and unpairing, Devices and Runners, what the relay sees, the account's provider credentials |
| [Relay](docs/architecture/relay.md) | `crates/relay`: storage on SQLite or Postgres, files, housekeeping, quotas, metrics, rate limits, auth, tables and migrations, the blob, sync socket, and push APIs, deploys |
| [Protocols](docs/architecture/protocols.md) | The app ↔ CLI websocket and the CLI ↔ relay requests and blobs |
| [Remote requests](docs/architecture/remote-requests.md) | Device-to-Runner request signatures, authority, envelopes and deadlines |
| [Managed policy receipts and replay](docs/architecture/managed-policy-replay.md) | Staged opaque policy/roster commits, durable receipt retention, authenticated finite replay cuts and teardown boundaries |
| [Local API](docs/architecture/local-api.md) | The `/ws` socket: trust boundary, field errors, every method with params and results, events, secret-bearing methods, memory and setup bodies |
| [CLI (runtime)](docs/architecture/runtime.md) | Commands, local websocket access, proxy/certificate trust, diagnostics, agent loop and notifications |
Expand Down
24 changes: 16 additions & 8 deletions crates/agent/src/tools/bash_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,15 +1397,23 @@ mod tests {

let more = call(&t.output, json!({"session_id": id, "wait_seconds": 0.3})).await.unwrap();
assert!(more.text_content().starts_with("tick\n"), "{}", more.text_content());
// Ctrl-C must end the loop (SIGINT). The wait cap only bounds a hang if it does not;
// do not accept a still-running outcome here — that would hide a delivery bug.
let stopped = tokio::time::timeout(
BACKGROUND_SETTLE + Duration::from_secs(5),
call(&t.input, json!({"session_id": id, "text": "\u{3}", "enter": false})),
)
let session = t.host.get(&id).unwrap();
assert!(session.end().is_none(), "the printing loop is alive before Ctrl-C");
// bash_input may return at its background cap before the shell is reaped.
// Keep one bounded deadline for delivery, reap, and observing SIGINT through a tool.
let stopped = tokio::time::timeout(BACKGROUND_SETTLE + Duration::from_secs(5), async {
let reply = call(&t.input, json!({"session_id": id, "text": "\u{3}", "enter": false})).await.unwrap();
session.ended().await;
assert_eq!(session.end(), Some(SessionEnd::Signaled(2)));
if t.host.get(&id).is_some() {
// The input tool must also observe an end recorded after its first reply.
call(&t.input, json!({"session_id": id, "text": "", "enter": false})).await.unwrap()
} else {
reply
}
})
.await
.expect("bash_input returned after Ctrl-C")
.unwrap();
.expect("Ctrl-C ended the printing loop and its result was read");
assert!(
stopped.text_content().ends_with("Command terminated by signal 2"),
"{}",
Expand Down
40 changes: 32 additions & 8 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,10 +1000,12 @@ impl App {

/// Every turn in flight this Device knows of, by job id: what the other Devices list while
/// the relay last saw them online, then this Device's own jobs, including one it sent to a
/// Runner that has not listed it yet.
/// Runner that has not listed it yet. A job sent to a Runner the relay's last machine list
/// shows offline is left out, as that Runner's own list is: its wait goes on, so a result
/// that comes later still counts.
fn known_turns(&self) -> std::collections::BTreeMap<String, LiveTurn> {
let mut turns = std::collections::BTreeMap::new();
{
let offline_runners: std::collections::HashSet<String> = {
let state = self.state.lock().unwrap();
for (device_id, listed) in &state.device_turns {
if state.turns_online.contains(device_id) {
Expand All @@ -1012,8 +1014,12 @@ impl App {
}
}
}
}
state.listed_machines.keys().filter(|id| !state.turns_online.contains(*id)).cloned().collect()
};
for (id, job) in self.running_jobs.lock().unwrap().iter() {
if job.runner_id.as_ref().is_some_and(|runner| offline_runners.contains(runner)) {
continue;
}
turns.entry(id.clone()).or_insert_with(|| job.turn(id));
}
turns
Expand All @@ -1028,11 +1034,7 @@ impl App {
}
let mut announced = self.announced_turns.lock().unwrap();
let known = self.known_turns();
for (id, turn) in announced.iter() {
if !known.contains_key(id) {
self.emit(Event::JobFinished { chat_id: turn.chat_id.clone(), bot_id: turn.bot_id.clone(), job_id: id.clone(), routine_id: turn.routine_id.clone() });
}
}
self.announce_ended(&announced, &known);
for (id, turn) in &known {
let before = announced.get(id);
if before.is_none() {
Expand All @@ -1047,6 +1049,28 @@ impl App {
*announced = known;
}

/// Tells the local app at once which of the turns it was told of this Device no longer
/// knows, and announces none: a Device the relay now shows offline lists nothing, and its
/// turns end here without waiting for the rest of the sync round, which a relay that drops
/// the session may never finish. What starts waits for `turns_changed` after the pull.
pub fn turns_ended(&self) {
if self.bulk_sync.load(Ordering::Relaxed) {
return;
}
let mut announced = self.announced_turns.lock().unwrap();
let known = self.known_turns();
self.announce_ended(&announced, &known);
announced.retain(|id, _| known.contains_key(id));
}

fn announce_ended(&self, announced: &std::collections::BTreeMap<String, LiveTurn>, known: &std::collections::BTreeMap<String, LiveTurn>) {
for (id, turn) in announced.iter() {
if !known.contains_key(id) {
self.emit(Event::JobFinished { chat_id: turn.chat_id.clone(), bot_id: turn.bot_id.clone(), job_id: id.clone(), routine_id: turn.routine_id.clone() });
}
}
}

/// A turn here started, ended, or changed what it is doing: the other Devices hear it
/// through this Device's machine blob, and the local app through `turns_changed`.
pub fn local_turns_changed(&self) {
Expand Down
22 changes: 15 additions & 7 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,36 +280,44 @@ impl StorageAdmission {
use std::os::unix::ffi::OsStrExt;
let (mut directory, normalized, mut ancestors) = Self::platform_entry(home)?;
let path = normalized.as_ref();
for ancestor in &ancestors { Self::lock(ancestor, libc::LOCK_SH)?; }
for ancestor in &ancestors { Self::lock(ancestor, libc::LOCK_SH, "platform-prefix")?; }
let segments: Vec<_> = path.components().filter_map(|part| match part {
std::path::Component::Normal(segment) => Some(Ok(segment)),
std::path::Component::RootDir | std::path::Component::CurDir => None,
_ => Some(Err(anyhow::anyhow!("Storage admission refuses parent traversal"))),
}).collect::<anyhow::Result<_>>()?;
for segment in segments {
Self::lock(&directory, libc::LOCK_SH)?;
Self::lock(&directory, libc::LOCK_SH, "ancestor")?;
let name = std::ffi::CString::new(segment.as_bytes())?;
let fd = unsafe { libc::openat(directory.as_raw_fd(), name.as_ptr(), libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC) };
if fd < 0 {
let error = std::io::Error::last_os_error();
if allow_missing && error.kind() == std::io::ErrorKind::NotFound {
Self::lock(&directory, libc::LOCK_EX)?;
Self::lock(&directory, libc::LOCK_EX, "creation-parent")?;
return Ok(Self { directory, home: home.into(), ancestors });
}
return Err(error.into());
}
ancestors.push(directory);
directory = unsafe { std::fs::File::from_raw_fd(fd) };
}
Self::lock(&directory, libc::LOCK_EX)?;
Self::lock(&directory, libc::LOCK_EX, "home")?;
Ok(Self { directory, home: home.into(), ancestors })
}

#[cfg(unix)]
fn lock(file: &std::fs::File, mode: i32) -> anyhow::Result<()> {
fn lock(file: &std::fs::File, mode: i32, stage: &'static str) -> anyhow::Result<()> {
use std::os::fd::AsRawFd;
if unsafe { libc::flock(file.as_raw_fd(), mode | libc::LOCK_NB) } != 0 {
anyhow::bail!("Storage admission is busy or uncertain");
// Capture errno before formatting or any other system call. Keep the source for
// callers, without putting home paths or account identifiers in diagnostics.
let error = std::io::Error::last_os_error();
let kind = error.kind();
let errno = error.raw_os_error();
let lock = if mode == libc::LOCK_SH { "shared" } else { "exclusive" };
return Err(anyhow::Error::new(error).context(format!(
"Storage admission is busy or uncertain (stage={stage}, lock={lock}, kind={kind:?}, errno={errno:?})"
)));
}
Ok(())
}
Expand All @@ -320,7 +328,7 @@ impl StorageAdmission {
let directory = Self::open_directory(&self.home)?;
if directory.metadata()?.dev() != self.directory.metadata()?.dev()
|| directory.metadata()?.ino() != self.directory.metadata()?.ino() {
Self::lock(&directory, libc::LOCK_EX)?;
Self::lock(&directory, libc::LOCK_EX, "created-home")?;
let ancestor = std::mem::replace(&mut self.directory, directory);
self.ancestors.push(ancestor);
}
Expand Down
120 changes: 120 additions & 0 deletions crates/cli/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ async fn refresh_presence(app: &Arc<App>, url: &str, token: &str) -> Result<(),
tracing::error!(%error, "dropping another Device's turns");
}
}
app.turns_ended();
Ok(())
}

Expand Down Expand Up @@ -1621,6 +1622,125 @@ mod tests {
}
}

/// A Runner that leaves the relay mid-turn ends its turns here as soon as the presence
/// refresh hears it, without waiting for the rest of the sync round: the turn it lists, and
/// the job this Device sent it and still waits on. Its eventual result reaches the waiter
/// while offline, and a reconnect advertises a still-tracked turn once.
#[cfg(feature = "server")]
#[tokio::test]
async fn a_runner_leaving_the_relay_ends_its_turns_here_at_once() {
use axum::{routing::get, Json, Router};
use std::sync::atomic::AtomicBool;
let scratch = scratch_app();
let app = &scratch.0;
let online = Arc::new(AtomicBool::new(true));
let listed = online.clone();
let relay = Router::new().route("/v1/machines", get(move || {
let online = listed.load(Ordering::Relaxed);
async move { Json(serde_json::json!({ "machines": [{ "machine_pubkey": "mac", "box_pubkey": "box", "last_seen": 1, "created_at": 1, "online": online }], "now": crate::config::now_unix() })) }
}));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
let server = tokio::spawn(async move { axum::serve(listener, relay).await.unwrap() });
let turn = |job_id: &str| LiveTurn { job_id: job_id.into(), chat_id: "chat".into(), bot_id: "bot".into(), routine_id: None, activity: None };
let mut events = app.events.subscribe();
let mut heard = |pick: fn(Event) -> Option<String>| -> Vec<String> {
let mut ids: Vec<_> = std::iter::from_fn(|| events.try_recv().ok()).filter_map(pick).collect();
ids.sort();
ids
};
let started = |event: Event| match event { Event::JobStarted { job_id, .. } => Some(job_id), _ => None };
let finished = |event: Event| match event { Event::JobFinished { job_id, .. } => Some(job_id), _ => None };
let ids = |ids: &[&str]| {
let mut ids: Vec<_> = ids.iter().map(|id| id.to_string()).collect();
ids.sort();
ids
};

app.running_jobs.lock().unwrap().insert(
"sent-job".into(),
crate::app::RunningJob { chat_id: "chat".into(), bot_id: "bot".into(), routine_id: None, runner_id: Some("mac".into()), cancel: Default::default(), activity: None },
);
refresh_presence(app, &url, "test").await.unwrap();
assert!(app.device_is_online("mac"), "the fixture establishes the Runner online before it leaves");
app.set_device_turns("mac", vec![turn("mac-job"), turn("sent-job")]);
assert_eq!(heard(started), ids(&["mac-job", "sent-job"]));

let (tx, mut result) = tokio::sync::oneshot::channel();
app.pending_results.lock().unwrap().insert("sent-job".into(), tx);
let cancel = app.running_jobs.lock().unwrap()["sent-job"].cancel.clone();
online.store(false, Ordering::Relaxed);
app.bulk_sync.store(true, Ordering::Relaxed);
refresh_presence(app, &url, "test").await.unwrap();
assert!(heard(finished).is_empty(), "presence refresh respects bulk batching");
app.bulk_sync.store(false, Ordering::Relaxed);
refresh_presence(app, &url, "test").await.unwrap();
assert_eq!(heard(finished), ids(&["mac-job", "sent-job"]), "the refresh alone tells the app");
assert!(app.running_turns().is_empty());
assert!(app.running_jobs.lock().unwrap().contains_key("sent-job"), "the wait for the result goes on");

assert!(app.pending_results.lock().unwrap().contains_key("sent-job"));
assert!(!cancel.is_cancelled());
assert!(matches!(result.try_recv(), Err(tokio::sync::oneshot::error::TryRecvError::Empty)));
crate::runtime::deliver_job_result(app, JobResult {
job_id: "sent-job".into(), chat_id: "chat".into(), bot_id: "bot".into(), outcome: "sent".into(),
});
assert_eq!(result.await.unwrap(), "sent", "an offline Runner's eventual result is accepted");
assert!(!app.pending_results.lock().unwrap().contains_key("sent-job"));

online.store(true, Ordering::Relaxed);
refresh_presence(app, &url, "test").await.unwrap();
assert!(heard(started).is_empty(), "presence alone must not announce new starts");
app.set_device_turns("mac", vec![turn("sent-job")]);
assert_eq!(heard(started), ids(&["sent-job"]));
app.turns_changed();
assert!(heard(started).is_empty(), "reconnect advertises once");
app.set_device_turns("mac", Vec::new());
app.running_jobs.lock().unwrap().remove("sent-job");
app.turns_changed();
assert_eq!(heard(finished), ids(&["sent-job"]));
server.abort();
}

#[tokio::test]
async fn offline_turn_visibility_preserves_unknown_local_and_bulk_jobs() {
let scratch = scratch_app();
let app = &scratch.0;
for (id, runner) in [("local", None), ("unknown", Some("unlisted")), ("online", Some("online")), ("offline", Some("offline"))] {
app.running_jobs.lock().unwrap().insert(id.into(), crate::app::RunningJob {
chat_id: "chat".into(), bot_id: "bot".into(), routine_id: None,
runner_id: runner.map(str::to_string), cancel: Default::default(), activity: None,
});
}
let mut events = app.events.subscribe();
app.turns_changed();
let starts: Vec<_> = std::iter::from_fn(|| events.try_recv().ok())
.filter(|e| matches!(e, Event::JobStarted { .. })).collect();
assert_eq!(starts.len(), 4, "initial presence is unknown, not offline");
{
let mut state = app.state.lock().unwrap();
state.listed_machines.insert("offline".into(), 1);
state.listed_machines.insert("online".into(), 1);
state.turns_online.insert("online".into());
}
app.bulk_sync.store(true, Ordering::Relaxed);
app.turns_ended();
app.turns_changed();
assert!(events.try_recv().is_err(), "bulk mode defers turn events");
app.bulk_sync.store(false, Ordering::Relaxed);
app.emit(Event::Snapshot(app.snapshot()));
app.turns_changed();
assert!(matches!(events.try_recv().unwrap(), Event::Snapshot(_)));
assert!(matches!(events.try_recv().unwrap(), Event::JobFinished { job_id, .. } if job_id == "offline"));
assert!(events.try_recv().is_err());
let visible: std::collections::BTreeSet<_> = app.running_turns().iter()
.map(|t| t["job_id"].as_str().unwrap().to_string()).collect();
assert_eq!(visible, ["local", "unknown", "online"].into_iter().map(str::to_string).collect());
assert_eq!(app.running_jobs.lock().unwrap().len(), 4);
app.turns_ended();
assert!(events.try_recv().is_err(), "ended visibility emits once");
}

fn scratch_app() -> ScratchApp {
let home = std::env::temp_dir().join(format!("beans-sync-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&home).unwrap();
Expand Down
Loading