From 430d3f03673172ea81e55ab255a71ff47a9cd5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Sat, 21 Nov 2026 11:52:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E6=A9=9F=E9=BB=9E=E9=80=B2=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E4=B8=80=E6=AC=A1=E6=9C=89=E8=A8=98=E9=8C=84=E3=80=81?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E5=85=A8=E7=A9=BA=EF=BC=9A=E6=89=8B=E6=A9=9F?= =?UTF-8?q?=E8=AA=AA=E5=AE=83=E9=82=A3=E4=BB=BD=E5=BE=9E=E5=93=AA=E4=B8=80?= =?UTF-8?q?=E5=89=87=E9=96=8B=E5=A7=8B=EF=BC=88w0=EF=BC=8C=E8=BC=89?= =?UTF-8?q?=E5=85=A5=E6=9B=B4=E6=97=A9=E7=9A=84=20from=EF=BC=89=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E6=98=AF=E9=82=A3=E5=89=87=E5=9C=A8=E6=95=B4=E6=AE=B5?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E8=A3=A1=E7=9A=84=E4=BD=8D=E7=BD=AE=EF=BC=88?= =?UTF-8?q?index=EF=BC=8C=E6=95=B8=E5=88=B0=20Bot=20=E6=AF=8F=E5=80=8B?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E6=AD=A5=E9=A9=9F=EF=BC=89=EF=BC=8C=E5=A4=A7?= =?UTF-8?q?=E8=85=A6=E5=8D=BB=E7=95=B6=E6=88=90=E3=80=8C=E7=99=BC=E5=B8=83?= =?UTF-8?q?=E6=B8=85=E5=96=AE=E8=A3=A1=E7=AC=AC=E5=B9=BE=E5=89=87=E3=80=8D?= =?UTF-8?q?=E2=80=94=E2=80=94=E6=B8=85=E5=96=AE=E5=8F=AA=E6=98=AF=E6=9C=80?= =?UTF-8?q?=E6=96=B0=20PHONE=5FTAIL=EF=BC=88120=EF=BC=89=E5=89=87=E5=8E=BB?= =?UTF-8?q?=E6=8E=89=E5=B7=A5=E5=85=B7=E6=AD=A5=E9=A9=9F=EF=BC=8C=E7=9F=AD?= =?UTF-8?q?=E5=BE=97=E5=A4=9A=E3=80=82=E6=9C=89=E5=B7=A5=E5=85=B7=E6=AD=A5?= =?UTF-8?q?=E9=A9=9F=E7=9A=84=E8=81=8A=E5=A4=A9=E5=B0=B1=E5=88=87=E5=88=B0?= =?UTF-8?q?=E6=B8=85=E5=96=AE=E5=A4=96=EF=BC=8C=E5=9B=9E=E4=B8=80=E4=BB=BD?= =?UTF-8?q?=E7=A9=BA=E7=9A=84=EF=BC=9B=E4=B8=8B=E4=B8=80=E6=AC=A1=E6=89=8B?= =?UTF-8?q?=E6=A9=9F=E6=89=8B=E4=B8=8A=E6=B2=92=E6=9D=B1=E8=A5=BF=E3=80=81?= =?UTF-8?q?=E4=B8=8D=E5=B8=B6=20w0=EF=BC=8C=E5=8F=88=E6=8B=BF=E5=88=B0?= =?UTF-8?q?=E6=95=B4=E6=AE=B5=E2=80=94=E2=80=94=E4=B8=80=E6=AC=A1=E6=9C=89?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E7=A9=BA=EF=BC=8C=E4=B8=80=E7=9B=B4=E4=BA=A4?= =?UTF-8?q?=E6=9B=BF=EF=BC=88jenny-Host=2011/21=EF=BC=9A1133=20=E5=89=87?= =?UTF-8?q?=E7=9A=84=E8=81=8A=E5=A4=A9=E3=80=81index=201105=EF=BC=8C?= =?UTF-8?q?=E6=B8=85=E5=96=AE=2040=20=E5=89=87=E5=B7=A6=E5=8F=B3=EF=BC=89?= =?UTF-8?q?=E3=80=8211/20=207648c92=E3=80=8C=E8=81=8A=E5=A4=A9=E5=88=86?= =?UTF-8?q?=E9=A0=81=E3=80=8D=E5=B8=B6=E9=80=B2=E4=BE=86=E7=9A=84=EF=BC=8C?= =?UTF-8?q?Jenny=20=E5=8D=8A=E5=A4=9C=E5=8D=87=E7=B4=9A=E6=AD=A3=E5=A5=BD?= =?UTF-8?q?=E5=8D=87=E5=88=B0=E5=AE=83=E3=80=82=E5=81=87=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E4=B8=8D=E5=8F=AB=E5=B7=A5=E5=85=B7=EF=BC=88index=20=E7=AD=89?= =?UTF-8?q?=E6=96=BC=E6=B8=85=E5=96=AE=E4=BD=8D=E7=BD=AE=EF=BC=89=EF=BC=8C?= =?UTF-8?q?=E6=89=80=E4=BB=A5=20scratch=20=E9=87=8D=E7=8F=BE=E4=B8=8D?= =?UTF-8?q?=E5=87=BA=E4=BE=86=EF=BC=8C=E5=8E=9F=E6=9C=AC=E7=9A=84=E6=B8=AC?= =?UTF-8?q?=E8=A9=A6=20index=20=E4=B9=9F=E6=98=AF=200..100=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 現在 from_index 把 index 換成清單裡第一則 ≥ 它的位置,三個地方都用:api/state 的開著的聊天、api/bots/、api/bots//older。手機頁面不用改(舊頁面送的也是 index)。另外 httpd 那個測試拿掉「停下後埠可以再綁」那一行:整套測試一起跑時,別的測試會立刻拿走剛放掉的埠(第二次整套跑失敗過一次),要驗的是它停下來,那一行還在。FEATURES.md 補一行。 已驗證: cargo test 全綠(826,連跑兩次);新測試 a_chat_with_steps_comes_back_every_time(40 則、index 從 1013 起每則隔 3:打開拿最新 30、再拿三次都是同一個窗不會空、有新的一則只送那一則、載入更早從那則往前、index 太舊從頭)——把換算拿掉它會失敗;clippy 錯誤數跟之前一樣(12)。 未驗證: Jenny 的手機實機(大腦換新版之後請她點幾次)。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm --- FEATURES.md | 1 + src/httpd.rs | 4 +-- src/remote.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/FEATURES.md b/FEATURES.md index 37d9a7e..8e89d00 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -262,6 +262,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。 - 手機打開聊天先只拿最新 30 條訊息(慢線上開頁省流量)。 - 有更早的,頂上出現「載入更早的對話」,點一次往前補 60 條;翻到最頂按鈕消失。 +- 手機說它手上那份從哪一則開始(`w0`、載入更早的 `from`),用的是那則在整段聊天裡的位置(`index`,數到 Bot 每個工具步驟);大腦照這個位置找到清單裡對應的那則再開始。以前大腦把它當成「清單裡第幾則」——清單只是最新 120 則去掉工具步驟,短得多——有工具步驟的聊天就切到清單外、回一份空的,下一次手機手上沒東西、又拿到整段:點進去一次有聊天記錄、一次全空(jenny-Host 11/21,1133 則的聊天、清單 40 則左右)。 - Bot 回答中,手機每秒最多更新一次聊天(慢線上畫面落後約一秒);打完的整句一定補齊。 - 電腦開對話照樣整段;直接查聊底的指令和腳本也照舊拿全部。 diff --git a/src/httpd.rs b/src/httpd.rs index 1eda254..6f8ba9a 100644 --- a/src/httpd.rs +++ b/src/httpd.rs @@ -1053,12 +1053,12 @@ mod tests { } let each = t.elapsed() / 10 - Duration::from_millis(30); assert!(each < Duration::from_millis(15), "{each:?} an answer on a new connection"); - // Told to stop: it stops, and the port is free again. + // Told to stop: it stops. (Whether the port is free again isn't asked: with the other + // tests running, another listener may take a freed port at once.) on.store(false, std::sync::atomic::Ordering::SeqCst); let t = std::time::Instant::now(); done.join().unwrap(); assert!(t.elapsed() < Duration::from_secs(2), "stopped in {:?}", t.elapsed()); - assert!(TcpListener::bind(("127.0.0.1", port)).is_ok()); } #[test] diff --git a/src/remote.rs b/src/remote.rs index d413deb..1766f19 100644 --- a/src/remote.rs +++ b/src/remote.rs @@ -734,9 +734,12 @@ pub fn handle(req: Req, via_relay: bool) -> Resp { // A page that says where its copy starts gets the window from there; a page // opening the chat (first=1, or no word) gets the newest PHONE_FIRST. let n = msgs.as_array().map(Vec::len).unwrap_or(0); - let w0 = w0q.unwrap_or(n.saturating_sub(PHONE_FIRST)); + let off = match w0q { + Some(ix) => from_index(msgs.as_array().map(Vec::as_slice).unwrap_or(&[]), ix), + None => n.saturating_sub(PHONE_FIRST), + }; let del = json!(vv.deliveries.get(&open_id).cloned().unwrap_or_default()); - let chat = chat_for(open_id, msgs, &del, &held, part, Some(w0)); + let chat = chat_for(open_id, msgs, &del, &held, part, Some(off)); let now_ok = since == 0 || chat.get("messages").is_none() || slot_open(&LAST_CHAT, CHAT_EVERY_MS); out["chat"] = chat; if now_ok { @@ -787,8 +790,10 @@ pub fn handle(req: Req, via_relay: bool) -> Resp { Some(m) => { let msgs = messages_for_role(m, &role); let n = msgs.as_array().map(Vec::len).unwrap_or(0); - let w0 = req.query.get("w0").and_then(|x| x.parse::().ok()) - .or(req.query.get("first").map(|_| n.saturating_sub(PHONE_FIRST))); + let w0 = match req.query.get("w0").and_then(|x| x.parse::().ok()) { + Some(ix) => Some(from_index(msgs.as_array().map(Vec::as_slice).unwrap_or(&[]), ix)), + None => req.query.get("first").map(|_| n.saturating_sub(PHONE_FIRST)), + }; let del = json!(v.deliveries.get(&id).cloned().unwrap_or_default()); let mut out = chat_for(id, msgs, &del, held, part, w0); out["version"] = json!(v.version); @@ -808,7 +813,7 @@ pub fn handle(req: Req, via_relay: bool) -> Resp { Some(m) => { let all = messages_for_role(m, &role); let arr = all.as_array().cloned().unwrap_or_default(); - let from = from.min(arr.len()); + let from = from_index(&arr, from); let start = from.saturating_sub(count); Resp::json(&json!({"bot": id, "messages": &arr[start..from], "more": start > 0})) } @@ -1465,6 +1470,16 @@ fn state_for(mut out: Value, held: &str, stable: bool) -> Value { /// = the whole chat, `first=1` on a poll = the newest `PHONE_FIRST`). 交付確認 rides beside the /// messages (`deliveries`), never inside them: a device reporting a file must not change the /// chat's fingerprint. +/// Where in the chat the phone sends (`w0`, an older page's `from`) — its first message's +/// `index`, the place in the bot's whole chat — as a place in the list it is sent from (the +/// newest PHONE_TAIL of the chat without the steps' results, so far shorter): the first message +/// at or after it. Read as a place in that list, it ran past its end on any chat with steps +/// (jenny-Host: index 1105 in a list of 40) and the window came back empty — the next ask, with +/// nothing held, got it all again: the chat was there one time and empty the next. +fn from_index(list: &[Value], index: usize) -> usize { + list.iter().position(|m| m["index"].as_u64().is_some_and(|i| i as usize >= index)).unwrap_or(list.len()) +} + fn chat_for(bot: u64, msgs: Value, delivery: &Value, held: &str, part: bool, w0: Option) -> Value { let list = msgs.as_array().map(Vec::as_slice).unwrap_or(&[]); let dfp = fp(delivery); @@ -3631,6 +3646,53 @@ mod tests { assert_eq!(wide["messages"][0]["index"], json!(off - 20)); } + /// A chat with steps, as the phone sees it: the list it is sent is the newest PHONE_TAIL of the + /// chat without the steps' results, each message carrying its place in the whole chat + /// (`index`, with gaps where the steps were). The page says where its copy starts with that + /// `index` — the chat comes back from there, every time (it came back empty every other time: + /// jenny-Host, 2026-11-21), and 載入更早 walks back from there. + #[test] + fn a_chat_with_steps_comes_back_every_time() { + let bot = 987_001u64; + // 40 messages shown, from the newest 120 of a chat of 1133 (two steps after each reply). + let list: Vec = (0..40).map(|i| json!({"index": 1013 + i * 3, "text": format!("第 {i} 則")})).collect(); + std::sync::Arc::make_mut(&mut *hub().view.lock().unwrap()).messages.insert(bot, list.clone()); + let ask = |q: &str| -> Value { + let (path, query) = q.split_once('?').unwrap_or((q, "")); + let query = query.split('&').filter_map(|kv| kv.split_once('=')).map(|(k, v)| (k.to_string(), v.to_string())).collect(); + let headers = [("x-anybot-role".to_string(), "owner".to_string())].into_iter().collect(); + let r = handle(Req { method: "GET".into(), path: path.into(), query, headers, body: vec![], peer: String::new() }, true); + assert_eq!(r.status, 200, "{q}"); + serde_json::from_slice(&r.body).unwrap() + }; + // Opened: the newest PHONE_FIRST. + let first = ask(&format!("/api/bots/{bot}?first=1")); + let win = first["messages"].as_array().unwrap().clone(); + assert_eq!(win.len(), PHONE_FIRST); + let (start, sig) = (win[0]["index"].as_u64().unwrap(), first["sig"].as_str().unwrap().to_string()); + assert_eq!(start, 1013 + 10 * 3); + // Opened again with what it holds (its first index, way past the list's length): the same + // window, not an empty one — said unchanged, or whole. + for _ in 0..3 { + let again = ask(&format!("/api/bots/{bot}?chat_sig={sig}&chat_delta=1&w0={start}")); + assert!(again["unchanged"] == true || again["messages"].as_array().is_some_and(|m| m.len() == PHONE_FIRST && m[0]["index"] == json!(start)), "{again}"); + } + // Something new: only that, onto the window it holds. + let mut more = list.clone(); + more.push(json!({"index": 1013 + 40 * 3, "text": "新的"})); + std::sync::Arc::make_mut(&mut *hub().view.lock().unwrap()).messages.insert(bot, more); + let delta = ask(&format!("/api/bots/{bot}?chat_sig={sig}&chat_delta=1&w0={start}")); + assert_eq!((delta["base"].as_str(), delta["from"].as_u64(), delta["messages"].as_array().map(Vec::len)), (Some(sig.as_str()), Some(PHONE_FIRST as u64), Some(1)), "{delta}"); + // 載入更早 from its first index: the messages just before it. + let older = ask(&format!("/api/bots/{bot}/older?from={start}&count=60")); + let o = older["messages"].as_array().unwrap(); + assert_eq!((o.len(), o[0]["index"].as_u64(), o[9]["index"].as_u64(), older["more"].as_bool()), (10, Some(1013), Some(start - 3), Some(false))); + // An index older than the list: all of it from the start. + assert_eq!(from_index(&list, 5), 0); + assert_eq!(from_index(&list, usize::MAX), list.len()); + std::sync::Arc::make_mut(&mut *hub().view.lock().unwrap()).messages.remove(&bot); + } + /// A message tried again on a bad network (the same id) reaches the bot once. #[test] fn a_message_tried_again_is_taken_once() {