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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/agent/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ pub(super) fn refresh(store: &Store, id: &str) -> Result<Value> {
}
struct HeadIndex {
path: std::path::PathBuf,
revision: [u64; 6],
revision: [u64; 7],
jobs: Vec<Value>,
}
static HEADS: std::sync::Mutex<Option<HeadIndex>> = std::sync::Mutex::new(None);
Expand Down Expand Up @@ -1278,6 +1278,67 @@ mod tests {
assert!(refresh_page(&store, list(&store).unwrap()).is_err());
}
#[test]
fn head_index_sees_turns_created_within_one_root_timestamp_tick() {
struct Temp(std::path::PathBuf);
impl Drop for Temp {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
let temp = Temp(std::env::temp_dir().canonicalize().unwrap().join(format!(
"omamail-head-index-tick-{}-{}",
std::process::id(),
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos()
)));
std::fs::create_dir(&temp.0).unwrap();
let store = Store::open_at(&temp.0).unwrap();
let make = |id: &str, conversation: &str, order: u64, state: &str| {
store.create(id).unwrap();
let job = json!({"id":id,"conversationId":conversation,"accountId":"synthetic",
"subject":"Mail","messageId":"m","messageIds":["m"],"draftKey":"","draftFingerprint":"",
"kind":"message","state":state,"created":now(),"updated":now(),"createdOrder":order,"resultReady":false});
store.write_json(id, "job.json", &job).unwrap();
job
};
// The first poll caches an empty index, exactly like the contract
// fixture's paged-listing call before the harness seeds any turn.
assert!(list(&store).unwrap().is_empty());
let before = store.revision().unwrap();

// The fixture seeds turns with a direct mkdir. The cached key is rebuilt
// here from the live root metadata with the link count it observed
// before the turn existed, which is the window a same-tick mkdir leaves
// behind: coarse mtime/ctime unchanged, one more directory link.
let first = format!("{:032x}", 0);
make(&first, &first, 0, "done");
let mut stale = store.revision().unwrap();
*stale
.get_mut(6)
.expect("Store::revision must carry the root link count, not only timestamps") =
*before.last().unwrap();
HEADS.lock().unwrap().as_mut().unwrap().revision = stale;
assert_eq!(
list(&store).unwrap().len(),
1,
"a turn created after the index was cached must invalidate it"
);
assert_eq!(list(&store).unwrap()[0]["id"], first);

// Removal is the mirror signal.
let after_create = store.revision().unwrap();
store.remove(&first).unwrap();
let mut stale = store.revision().unwrap();
*stale.get_mut(6).unwrap() = *after_create.last().unwrap();
HEADS.lock().unwrap().as_mut().unwrap().revision = stale;
assert!(
list(&store).unwrap().is_empty(),
"a removed turn must invalidate the index that still listed it"
);
}
#[test]
fn context_rejects_overrides_controls_and_injected_ids() {
let good = json!({"accountId":"hey:a@example.org","messageId":"1:2","message":"Unicode郵件\nquoted \\\" body","prompt":"Summarize"});
assert!(validate_payload(&good).is_ok());
Expand Down
10 changes: 9 additions & 1 deletion src/agent/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,14 @@ impl Store {
&self.path
}
/// Creating/removing a turn invalidates the in-process conversation index.
pub(super) fn revision(&self) -> Result<[u64; 6]> {
///
/// The root's coarse mtime/ctime only advance once per kernel tick, so a
/// turn directory created inside the same tick as the previous poll leaves
/// both unchanged. The link count moves on every subdirectory create and
/// remove even then, which is what keeps out-of-band turn creation visible.
/// It cannot see a same-tick create that cancels a same-tick remove, so this
/// is a narrower signal than a general invalidation guarantee.
pub(super) fn revision(&self) -> Result<[u64; 7]> {
let m = self.root.metadata().map_err(|_| ioerror())?;
Ok([
m.dev(),
Expand All @@ -207,6 +214,7 @@ impl Store {
m.mtime_nsec() as u64,
m.ctime() as u64,
m.ctime_nsec() as u64,
m.nlink(),
])
}
pub fn contains(&self, id: &str) -> Result<bool> {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/imap/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ mod tests {
0
);
});
let p = json!({"requestToken":"socket-close-test","settings":{"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true},"credential":"synthetic:secret"});
let p = json!({"requestToken":"socket-close-test","settings":{"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true,"testSession":crate::providers::imap::tests::synthetic_account()},"credential":"synthetic:secret"});
let q = p.clone();
let request = tokio::spawn(async move { super::super::call("imap.folders", &q).await });
reached.notified().await;
Expand Down
3 changes: 2 additions & 1 deletion src/providers/imap/mutation/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::providers::imap::tests::synthetic_account;
#[test]
fn encoded_from_name_never_becomes_the_smtp_sender() {
for name in ["工 <victim@example.org>, Alias", "工, Lee"] {
Expand Down Expand Up @@ -69,7 +70,7 @@ async fn initialize(w: &mut Wire) {
write(w,b"* LIST () \"/\" INBOX\r\n* LIST (\\Archive) \"/\" Archive\r\n* LIST (\\Drafts) \"/\" Drafts\r\nO1 OK folders\r\n").await.unwrap();
}
fn params(port: u16) -> Value {
json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true},"credential":"synthetic:secret","oauth":false})
json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true,"testSession":synthetic_account()},"credential":"synthetic:secret","oauth":false})
}
async fn planned_group_failure(failure: &str, use_move: bool, flags_only: bool, stall: bool) {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
Expand Down
7 changes: 4 additions & 3 deletions src/providers/imap/read/search/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::providers::imap::tests::synthetic_account;
use std::sync::atomic::{AtomicUsize, Ordering};
mod paging;

Expand Down Expand Up @@ -117,7 +118,7 @@ async fn large_account_scan_is_parallel_bounded_and_pages_reuse_snapshot() {
}
});
let mut p = json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,
"username":"synthetic","insecure":true,"testPlaintext":true},
"username":"synthetic","insecure":true,"testPlaintext":true,"testSession":synthetic_account()},
"credential":"synthetic:secret","query":"search:ALL","limit":2,"requestToken":"large-account"});
let mut rounds = 0;
let first = loop {
Expand Down Expand Up @@ -325,7 +326,7 @@ async fn generic_all_server_lists_each_message_once_and_verifies_exclusions() {
}
});
let mut p = json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,
"username":"synthetic","insecure":true,"testPlaintext":true},
"username":"synthetic","insecure":true,"testPlaintext":true,"testSession":synthetic_account()},
"credential":"synthetic:generic","query":"search:ALL","limit":10,"requestToken":"generic-all"});
let mut method = "imap.list";
let result = loop {
Expand Down Expand Up @@ -503,7 +504,7 @@ async fn cancelled_parallel_scan_closes_every_busy_socket() {
}
});
let mut p = json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,
"username":"synthetic","insecure":true,"testPlaintext":true},
"username":"synthetic","insecure":true,"testPlaintext":true,"testSession":synthetic_account()},
"credential":"synthetic:secret","query":"search:ALL","requestToken":"cancel-parallel"});
let boxes = parse_folders(
b"* LIST () \"/\" A\r\n* LIST () \"/\" B\r\n* LIST () \"/\" C\r\n* LIST () \"/\" D\r\n",
Expand Down
3 changes: 2 additions & 1 deletion src/providers/imap/read/search/tests/paging.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::providers::imap::tests::synthetic_account;
use std::sync::Mutex;

struct State {
Expand Down Expand Up @@ -55,7 +56,7 @@ impl Server {
state,
task,
params: json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,
"username":"synthetic","insecure":true,"testPlaintext":true},
"username":"synthetic","insecure":true,"testPlaintext":true,"testSession":synthetic_account()},
"credential":"synthetic:secret","query":"search:ALL","limit":2,"requestToken":"paging"}),
}
}
Expand Down
90 changes: 89 additions & 1 deletion src/providers/imap/read/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::providers::imap::tests::synthetic_account;
#[test]
fn octet_literals_do_not_create_responses_or_fetch_fields() {
let raw = b"Subject: test\r\n\r\n* 8 FETCH (UID 999)\r\n\xc3\xa9";
Expand Down Expand Up @@ -74,7 +75,7 @@ async fn select(w: &mut Wire) {
write(w, b"O1 OK selected\r\n").await.unwrap();
}
fn params(port: u16) -> Value {
json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true},"credential":"synthetic:secret","oauth":false,"query":"folder:INBOX UNSEEN","limit":3,"progressive":true,"requestToken":"request-1"})
json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true,"testSession":synthetic_account()},"credential":"synthetic:secret","oauth":false,"query":"folder:INBOX UNSEEN","limit":3,"progressive":true,"requestToken":"request-1"})
}
#[tokio::test]
async fn sparse_search_orders_by_date_before_paging_even_when_progressive() {
Expand Down Expand Up @@ -260,3 +261,90 @@ async fn original_query_controls_are_rejected_before_connecting() {
.is_err()
);
}

/// Serve mailbox discovery, then refuse the SELECT. The refusal drops the
/// authenticated wire instead of pooling it, which is the state the 45 second
/// pool lifetime and the 60 second mailbox lifetime leave behind.
async fn greeting_then_select_refusal(w: &mut Wire) {
greeting(w).await;
assert_eq!(line(w).await.unwrap(), b"O1 SELECT \"INBOX\"\r\n");
write(w, b"O1 NO synthetic refusal\r\n").await.unwrap();
}

#[tokio::test]
async fn independent_mock_lifetimes_on_one_port_never_share_cached_mailboxes() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let first = tokio::spawn(async move {
let (stream, _) = listener.accept().await.unwrap();
let mut w: Wire = BufReader::new(Box::new(stream));
greeting_then_select_refusal(&mut w).await;
drop(listener);
});
assert_eq!(
super::super::call("imap.list", &params(port)).await,
Err("imap_command_failed")
);
first.await.unwrap();

// The second mock lifetime is deliberately given the same port. It is a
// different synthetic account, so the mailbox list cached above belongs to
// someone else: this server must see the whole discovery handshake before
// its SELECT, never a reused mailbox list from the earlier server.
let listener = tokio::net::TcpListener::bind(("127.0.0.1", port))
.await
.unwrap();
let second = tokio::spawn(async move {
let (stream, _) = listener.accept().await.unwrap();
let mut w: Wire = BufReader::new(Box::new(stream));
greeting_then_select_refusal(&mut w).await;
});
assert_eq!(
super::super::call("imap.list", &params(port)).await,
Err("imap_command_failed")
);
second.await.unwrap();
}

#[tokio::test]
async fn one_synthetic_account_reuses_its_cached_mailboxes_on_a_fresh_connection() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let p = params(port);
let first = tokio::spawn(async move {
let (stream, _) = listener.accept().await.unwrap();
let mut w: Wire = BufReader::new(Box::new(stream));
greeting_then_select_refusal(&mut w).await;
drop(listener);
});
assert_eq!(
super::super::call("imap.list", &p).await,
Err("imap_command_failed")
);
first.await.unwrap();

// The same account on the same port keeps reusing the mailbox list it has
// already discovered: a fresh connection runs LOGIN and the post-login
// CAPABILITY, then goes straight to SELECT with no second CAPABILITY or LIST.
let listener = tokio::net::TcpListener::bind(("127.0.0.1", port))
.await
.unwrap();
let second = tokio::spawn(async move {
let (stream, _) = listener.accept().await.unwrap();
let mut w: Wire = BufReader::new(Box::new(stream));
write(&mut w, b"* OK ready\r\n").await.unwrap();
assert!(line(&mut w).await.unwrap().starts_with(b"O1 LOGIN"));
write(&mut w, b"O1 OK login\r\n").await.unwrap();
assert_eq!(line(&mut w).await.unwrap(), b"O1 CAPABILITY\r\n");
write(&mut w, b"* CAPABILITY IMAP4rev1\r\nO1 OK caps\r\n")
.await
.unwrap();
assert_eq!(line(&mut w).await.unwrap(), b"O1 SELECT \"INBOX\"\r\n");
write(&mut w, b"O1 NO synthetic refusal\r\n").await.unwrap();
});
assert_eq!(
super::super::call("imap.list", &p).await,
Err("imap_command_failed")
);
second.await.unwrap();
}
21 changes: 19 additions & 2 deletions src/providers/imap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,25 @@ async fn bridge_tls_refuses_buffered_plaintext_before_writing() {
peer.read_to_end(&mut received).await.unwrap();
assert!(received.is_empty());
}
/// A distinct synthetic account identity for every fixture build.
///
/// Every test in this binary shares one process, so `POOL` and `BOXES` are
/// process-global and keyed by account — settings, credential and oauth, which
/// includes the ephemeral loopback port. When two independent mock lifetimes
/// are handed the same port inside the caches' 45-60 second lifetimes, an
/// identical key lets the later mock skip the `CAPABILITY`+`LIST` discovery it
/// expects and serve a mailbox list discovered from the earlier server. A
/// build-scoped identity removes that overlap. Fixtures that deliberately reuse
/// a connection build their parameters once and keep the same value.
pub(super) fn synthetic_account() -> String {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
format!(
"synthetic-{}",
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
)
}
pub(super) fn params(port: u16) -> Value {
json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true},"credential":"synthetic:password","folder":"INBOX","commands":["UID FETCH 1 (UID BODY.PEEK[])"]})
json!({"settings":{"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true,"testSession":synthetic_account()},"credential":"synthetic:password","folder":"INBOX","commands":["UID FETCH 1 (UID BODY.PEEK[])"]})
}
#[tokio::test]
async fn literal_bytes_cannot_forge_tagged_completion() {
Expand Down Expand Up @@ -523,7 +540,7 @@ fn outlook_settings_ignore_persisted_credential_destination_and_identity() {
// ------------------------------------------------------------ raw export
fn export_params(port: u16, uid: u64) -> Value {
json!({
"settings": {"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true},
"settings": {"imapHost":"127.0.0.1","imapPort":port,"username":"synthetic","insecure":true,"testPlaintext":true,"testSession":synthetic_account()},
"credential": "synthetic:password",
"folder": "INBOX",
"uid": uid,
Expand Down
Loading