From df0b64c0bcb4ab0207bec175d67998eeb69e2b64 Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Fri, 2 Oct 2026 19:12:58 -0500 Subject: [PATCH] ai: Fix HEADS conversation-index invalidation and isolate IMAP mock caches Two pre-existing test-determinism defects, both reproduced in isolation before this change: - `Store::revision()` keyed the in-process conversation index on the store root's dev/ino and coarse mtime/ctime only. A turn directory created inside the same kernel tick as the previous poll leaves those unchanged, so `agent.jobsList` kept serving the index it built before the turn existed. The fingerprint now also carries the root's link count, which moves on every subdirectory create and remove even when the timestamps do not. - Every IMAP test shares one process, and both the connection pool and the mailbox cache are keyed by account (settings + credential + oauth, which includes the ephemeral loopback port). Two independent mock lifetimes handed the same port inside the caches' 45-60 second lifetimes reused one another's discovered mailbox list, so the later mock saw `SELECT` where it expected `CAPABILITY`. Every fixture build now claims a distinct synthetic account, while fixtures that deliberately reuse a connection keep the parameter value they built once. Verification (synthetic credentials, loopback only). Both new regression tests fail on the un-fixed tree - the IMAP one with the same `O1 SELECT "INBOX"` vs `O1 CAPABILITY` signature as CI - and pass after; 20x targeted runs and 5x the full library suite are green; `cargo test --locked --no-default-features --features standalone,integration-test-credentials` (all targets), `cargo check --locked --all-targets --no-default-features --features standalone`, `make test-rust`, `make test-js`, the Backend API 7 contract against the standalone and the default-feature backends, and `tests/test_agent_native_bridge.py` all pass locally. Residual: a same-tick remove cancelled by a same-tick create restores both the link count and the coarse timestamps, so this fingerprint cannot see it, and the signal depends on the filesystem tracking subdirectories in a directory's link count. Internal Rust and test changes only; no API revision or release. ## Release Notes - The AI conversation list shows a turn created right after a poll immediately instead of possibly waiting for the next poll. --- src/agent/jobs.rs | 63 ++++++++++++- src/agent/storage.rs | 10 ++- src/providers/imap/cancel.rs | 2 +- src/providers/imap/mutation/tests.rs | 3 +- src/providers/imap/read/search/tests.rs | 7 +- .../imap/read/search/tests/paging.rs | 3 +- src/providers/imap/read/tests.rs | 90 ++++++++++++++++++- src/providers/imap/tests.rs | 21 ++++- 8 files changed, 188 insertions(+), 11 deletions(-) diff --git a/src/agent/jobs.rs b/src/agent/jobs.rs index 38f21d1..31762fc 100644 --- a/src/agent/jobs.rs +++ b/src/agent/jobs.rs @@ -470,7 +470,7 @@ pub(super) fn refresh(store: &Store, id: &str) -> Result { } struct HeadIndex { path: std::path::PathBuf, - revision: [u64; 6], + revision: [u64; 7], jobs: Vec, } static HEADS: std::sync::Mutex> = std::sync::Mutex::new(None); @@ -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()); diff --git a/src/agent/storage.rs b/src/agent/storage.rs index acf34b3..e68628a 100644 --- a/src/agent/storage.rs +++ b/src/agent/storage.rs @@ -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(), @@ -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 { diff --git a/src/providers/imap/cancel.rs b/src/providers/imap/cancel.rs index edcd2b8..fa212b7 100644 --- a/src/providers/imap/cancel.rs +++ b/src/providers/imap/cancel.rs @@ -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; diff --git a/src/providers/imap/mutation/tests.rs b/src/providers/imap/mutation/tests.rs index bf68856..c99a166 100644 --- a/src/providers/imap/mutation/tests.rs +++ b/src/providers/imap/mutation/tests.rs @@ -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 ["工 , Alias", "工, Lee"] { @@ -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(); diff --git a/src/providers/imap/read/search/tests.rs b/src/providers/imap/read/search/tests.rs index 6160835..01844f6 100644 --- a/src/providers/imap/read/search/tests.rs +++ b/src/providers/imap/read/search/tests.rs @@ -1,4 +1,5 @@ use super::*; +use crate::providers::imap::tests::synthetic_account; use std::sync::atomic::{AtomicUsize, Ordering}; mod paging; @@ -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 { @@ -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 { @@ -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", diff --git a/src/providers/imap/read/search/tests/paging.rs b/src/providers/imap/read/search/tests/paging.rs index f286d6c..b72ecb5 100644 --- a/src/providers/imap/read/search/tests/paging.rs +++ b/src/providers/imap/read/search/tests/paging.rs @@ -1,4 +1,5 @@ use super::*; +use crate::providers::imap::tests::synthetic_account; use std::sync::Mutex; struct State { @@ -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"}), } } diff --git a/src/providers/imap/read/tests.rs b/src/providers/imap/read/tests.rs index c1243ab..0b41f9b 100644 --- a/src/providers/imap/read/tests.rs +++ b/src/providers/imap/read/tests.rs @@ -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"; @@ -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() { @@ -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", ¶ms(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", ¶ms(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(); +} diff --git a/src/providers/imap/tests.rs b/src/providers/imap/tests.rs index 2fb0c99..e0224f4 100644 --- a/src/providers/imap/tests.rs +++ b/src/providers/imap/tests.rs @@ -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() { @@ -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,