readings-window
aien-dev/readings-window
Brought by drakestapleton
GitHub aien-dev/readings-window#5 (code only)
Apply it
git checkout -b patch-8c467301148d5a40 742d5d307dd5ea0c05680149fcd5d346f96e75ec curl -fsSL https://interplane.io/drakestapleton/readings-window/patches/8c467301148d5a40.patch | git am
Provenance
github-import 1
aien-dev/readings-window
5
92fb223c5966567022d8b9e4becbdcee703eca9d
{"kind":"github-import","owner":"aien-dev","repo":"readings-window","number":5,"url":"https://github.com/aien-dev/readings-window/pull/5","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:40:29Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 7cb8939..3782cb5 100644 --- a/.crumb +++ b/.crumb @@ -19,21 +19,21 @@ "purpose": "Directory scripts. Purpose not yet described by a human." }, { - "digest": "6fec168497c47fb9a79824aa8ecee67eda1513bbe82ce63996e336473be2edf8", - "last_commit": "d910ecb", + "digest": "e78825dc73e683ad8e895ff83781e41fd823ea6dc789a7438d6ace131f8423eb", + "last_commit": "742d5d3", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." } ], - "children_root": "sha256:2744808a6e52aeb290efba03e11d2f22fe4c862e855a1d6dd797b7d5c6a252e9", - "compiled_at": "2026-10-04T20:38:02Z", + "children_root": "sha256:6002b5cf15d625e5eacfedb19ebcd50e6401397d8eb96a61981f3bb3775740a7", + "compiled_at": "2026-10-04T20:48:31Z", "compiler": "crumb-compile/1", - "digest": "sha256:cbf98a95a4f58a082c642b10d45ecfae1ece37ea4a27d70335809657bd090204", + "digest": "sha256:687e262dc4a20ed046feea2af879ba956717e2d807b4bfddcbd593e42910b5fa", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "6191d9bf53be78428bd3f5a8181b2d8eb5af3abd", - "source_tree": "sha256:1b298e3b4e3c3ae6b173724dd2b2d971ddf947a08df1a15affc9f15c8d5e994e" + "generated_at_commit": "742d5d307dd5ea0c05680149fcd5d346f96e75ec", + "source_tree": "sha256:2f86e95490c25c9b336bd4bbb713c35452beb77b0df3e056c7d7a10fe19b1c98" }, "owner_lane": "", "seed": { diff --git a/NOTES.md b/NOTES.md index 11d1fe2..f2c3784 100644 --- a/NOTES.md +++ b/NOTES.md @@ -43,3 +43,10 @@ tiny_http 0.12 pulls rustls 0.20 (older); acceptable for read-only data, UNVERIF - `GET /v1/chat/claude` serves `/home/drakestapleton/shared/agents/claude-chat.txt`, `GET /v1/chat/claude.jsonl` serves `claude-chat.jsonl` (Bearer token). `?lines=N` (default 500, clamped 1..50000) runs `tail -n N` on the Spark; `?all=1` runs `cat`. Only constant paths and a parsed integer reach the remote shell. - Proxied live over ssh on each request. NOTHING is written or cached on the hub, because its root disk is a fragile USB stick. Spark unreachable gives 502 `{"error":"spark unreachable"}`. - Each request blocks the single-threaded server for the ssh round trip (up to ~5 s connect timeout per host); `?all=1` on a large file is a big response. UNVERIFIED under load. + +## Per-agent chat routes (added 2026-10-04, replaces the Claude-only feed above) +- Why: Drake 2026-10-04 wants every agent's chat visible to Muse from one archive. Design: `~/handoffs/2026-10-04-agent-archive.md` (program `agent-archive`). +- Allow-list, matched exactly, each name mapped to constant paths in code (the name is never put into a command): `claude`, `codex`, `opencode`, `gemini`, `muse`. `GET /v1/chat` lists them. +- Spark files: `/home/drakestapleton/archive/agent-chats/latest/<agent>.txt|.jsonl` (rolling, last 20000 lines) and `records/<agent>/<YYYY-MM-DD>.jsonl` (complete history). +- `?lines=N` runs `tail -n N` on the latest file. `?all=1`: plain runs `cat latest/<agent>.txt`; jsonl runs `cat records/<agent>/*.jsonl` (shell glob on the Spark, date order because file names sort by date). A missing file or an empty records folder makes the remote command fail, which shows as 502 "spark unreachable" (same code path as a real outage). +- After the archive cutover `~/shared/agents/claude-chat.*` become symlinks to `latest/claude.*`; the hub no longer reads those paths. The hub must run this build and the archive must exist before `/v1/chat/*` returns data. Not deployed by this change. diff --git a/README.md b/README.md index 3c5b9ac..f9486b1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ A small Rust service: it collects one JSON line of readings from the Spark every - `GET /health`: liveness plus an `exposure` field (`ok|changed|unknown`). - `GET /v1/readings/current`, `GET /v1/readings/history?hours=N` (default 24, max 720). - `GET /v1/status`, `GET /v1/results`, `GET /v1/exposure`. -- `GET /v1/chat/claude?lines=N` (text, last N lines, default 500, max 50000; `?all=1` for the whole file) and `GET /v1/chat/claude.jsonl` (same, raw JSON lines, `application/x-ndjson`): the live Claude chat feed, proxied from the Spark on every request (no hub disk writes). 502 `{"error":"spark unreachable"}` if the Spark cannot be reached. +- `GET /v1/chat` returns `{"agents":["claude","codex","opencode","gemini","muse"]}` so a client can discover the feeds. +- `GET /v1/chat/<agent>` (plain text) and `GET /v1/chat/<agent>.jsonl` (common JSON records, `application/x-ndjson`) for exactly those five agents; any other name is 404. `?lines=N` gives the last N lines (default 500, max 50000) of the rolling window of the agent archive. `?all=1` gives the whole rolling window (plain) or the complete record history, all dates in order (jsonl). Proxied live from the Spark on every request (no hub disk writes). 502 `{"error":"spark unreachable"}` if the Spark cannot be reached. - All `/v1/*` routes need `Authorization: Bearer <token>`. The token and the TLS key live on the hub only (`/etc/readings-window/`), never in this repository. ## Commands diff --git a/src/.crumb b/src/.crumb index 5dc4609..ce95c6a 100644 --- a/src/.crumb +++ b/src/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T20:38:02Z", + "compiled_at": "2026-10-04T20:48:31Z", "compiler": "crumb-compile/1", - "digest": "sha256:6fec168497c47fb9a79824aa8ecee67eda1513bbe82ce63996e336473be2edf8", + "digest": "sha256:e78825dc73e683ad8e895ff83781e41fd823ea6dc789a7438d6ace131f8423eb", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 1, - "generated_at_commit": "6191d9bf53be78428bd3f5a8181b2d8eb5af3abd", - "source_tree": "sha256:0abf3a5d75ea5c394ccd88672eafc37ef9572b8dbe13c79ff0e173bca258ca88" + "generated_at_commit": "742d5d307dd5ea0c05680149fcd5d346f96e75ec", + "source_tree": "sha256:fde923cdc14467c8641e0e9bed6d2b0cd02c5fdd35eaeb9e211f57da68783632" }, "owner_lane": "", "seed": { diff --git a/src/main.rs b/src/main.rs index 1ae6c62..12c1251 100644 --- a/src/main.rs +++ b/src/main.rs @@ -187,14 +187,45 @@ fn last_line() -> Option<String> { fs::read_to_string(HISTORY).ok()?.lines().rev().find(|l| ts_of(l).is_some()).map(String::from) } -const CHAT_TXT: &str = "/home/drakestapleton/shared/agents/claude-chat.txt"; -const CHAT_JSONL: &str = "/home/drakestapleton/shared/agents/claude-chat.jsonl"; const CHAT_DEFAULT_LINES: u64 = 500; const CHAT_MAX_LINES: u64 = 50000; const JSON_TYPE: &str = "application/json"; const TEXT_TYPE: &str = "text/plain; charset=utf-8"; const NDJSON_TYPE: &str = "application/x-ndjson"; +/// One allowed agent: every remote path is a constant, never built from request input. +struct Agent { + name: &'static str, + latest_txt: &'static str, + latest_jsonl: &'static str, + records_glob: &'static str, +} + +macro_rules! agent { + ($n:literal) => { + Agent { + name: $n, + latest_txt: concat!("/home/drakestapleton/archive/agent-chats/latest/", $n, ".txt"), + latest_jsonl: concat!("/home/drakestapleton/archive/agent-chats/latest/", $n, ".jsonl"), + records_glob: concat!("/home/drakestapleton/archive/agent-chats/records/", $n, "/*.jsonl"), + } + }; +} + +/// The fixed allow-list, matched exactly. +const AGENTS: [Agent; 5] = [agent!("claude"), agent!("codex"), agent!("opencode"), agent!("gemini"), agent!("muse")]; +const AGENTS_JSON: &str = r#"{"agents":["claude","codex","opencode","gemini","muse"]}"#; + +/// What one chat request reads on the Spark. +#[derive(Debug, PartialEq)] +struct ChatFeed { + /// Rolling window file, read with `tail` (and with `cat` for `?all=1` on plain text). + latest: &'static str, + /// What `?all=1` reads: the same file (plain) or the records glob (jsonl). + all: &'static str, + ctype: &'static str, +} + /// Number of lines asked for in `?lines=N`: default 500, clamped to 1..=50000. fn clamp_lines(q: Option<&str>) -> u64 { let n = q @@ -204,30 +235,36 @@ fn clamp_lines(q: Option<&str>) -> u64 { n.clamp(1, CHAT_MAX_LINES) } -/// Remote command for a chat feed: only a constant path and a validated integer reach the shell. -fn chat_command(path: &'static str, q: Option<&str>) -> String { +/// Remote command for a chat feed: only constant paths and a validated integer reach the shell. +fn chat_command(feed: &ChatFeed, q: Option<&str>) -> String { if q.is_some_and(|q| q.split('&').any(|kv| kv == "all=1")) { - format!("cat {path}") + format!("cat {}", feed.all) } else { - format!("tail -n {} {path}", clamp_lines(q)) + format!("tail -n {} {}", clamp_lines(q), feed.latest) } } -/// Chat feed paths: (file on the Spark, content type). -fn chat_route(path: &str) -> Option<(&'static str, &'static str)> { - match path { - "/v1/chat/claude" => Some((CHAT_TXT, TEXT_TYPE)), - "/v1/chat/claude.jsonl" => Some((CHAT_JSONL, NDJSON_TYPE)), - _ => None, - } +/// `/v1/chat/<agent>` and `/v1/chat/<agent>.jsonl` for allow-listed agents only. +fn chat_route(path: &str) -> Option<ChatFeed> { + let rest = path.strip_prefix("/v1/chat/")?; + let (name, jsonl) = match rest.strip_suffix(".jsonl") { + Some(n) => (n, true), + None => (rest, false), + }; + let a = AGENTS.iter().find(|a| a.name == name)?; + Some(if jsonl { + ChatFeed { latest: a.latest_jsonl, all: a.records_glob, ctype: NDJSON_TYPE } + } else { + ChatFeed { latest: a.latest_txt, all: a.latest_txt, ctype: TEXT_TYPE } + }) } /// Proxy a chat feed live from the Spark; nothing is written or cached on the hub. -fn chat_feed(path: &'static str, ctype: &'static str, q: Option<&str>) -> (u16, String, &'static str) { - match fetch(&chat_command(path, q)) { - Ok(o) => (200, String::from_utf8_lossy(&o.stdout).into_owned(), ctype), +fn chat_feed(feed: &ChatFeed, q: Option<&str>) -> (u16, String, &'static str) { + match fetch(&chat_command(feed, q)) { + Ok(o) => (200, String::from_utf8_lossy(&o.stdout).into_owned(), feed.ctype), Err(e) => { - eprintln!("readings-window: chat feed {path}: {e}"); + eprintln!("readings-window: chat feed {}: {e}", feed.latest); (502, r#"{"error":"spark unreachable"}"#.into(), JSON_TYPE) } } @@ -244,7 +281,8 @@ fn handle(method: &Method, url: &str, auth: Option<&str>, token: &str) -> (u16, Some((p, q)) => (p, Some(q)), None => (url, None), }; - let known = matches!(path, "/health" | "/v1/readings/current" | "/v1/readings/history" | "/v1/status" | "/v1/results" | "/v1/exposure" | "/v1/chat/claude" | "/v1/chat/claude.jsonl"); + let chat = chat_route(path); + let known = chat.is_some() || matches!(path, "/health" | "/v1/readings/current" | "/v1/readings/history" | "/v1/status" | "/v1/results" | "/v1/exposure" | "/v1/chat"); if !known { return (404, r#"{"error":"not found"}"#.into(), JSON_TYPE); } @@ -271,8 +309,11 @@ fn handle(method: &Method, url: &str, auth: Option<&str>, token: &str) -> (u16, if path == "/v1/results" { return (200, feed_body(RESULTS, validate_results, RESULTS_DEFAULT), JSON_TYPE); } - if let Some((file, ctype)) = chat_route(path) { - return chat_feed(file, ctype, query); + if path == "/v1/chat" { + return (200, AGENTS_JSON.into(), JSON_TYPE); + } + if let Some(feed) = &chat { + return chat_feed(feed, query); } if path == "/v1/readings/current" { return match last_line() { @@ -396,29 +437,65 @@ mod tests { assert_eq!(clamp_lines(Some("lines=1;rm -rf /")), 500); assert_eq!(clamp_lines(Some("lines=-5")), 500); } + const A: &str = "/home/drakestapleton/archive/agent-chats"; + fn route(p: &str) -> ChatFeed { + chat_route(p).unwrap() + } #[test] - fn chat_commands() { - assert_eq!(chat_command(CHAT_TXT, None), "tail -n 500 /home/drakestapleton/shared/agents/claude-chat.txt"); - assert_eq!(chat_command(CHAT_TXT, Some("lines=60000")), "tail -n 50000 /home/drakestapleton/shared/agents/claude-chat.txt"); - assert_eq!(chat_command(CHAT_TXT, Some("all=1")), "cat /home/drakestapleton/shared/agents/claude-chat.txt"); - assert_eq!(chat_command(CHAT_JSONL, Some("lines=7&all=1")), "cat /home/drakestapleton/shared/agents/claude-chat.jsonl"); - assert_eq!(chat_command(CHAT_JSONL, Some("lines=7")), "tail -n 7 /home/drakestapleton/shared/agents/claude-chat.jsonl"); - assert_eq!(chat_command(CHAT_JSONL, Some("all=1;id")), "tail -n 500 /home/drakestapleton/shared/agents/claude-chat.jsonl"); + fn chat_commands_plain() { + let f = route("/v1/chat/codex"); + let t = format!("{A}/latest/codex.txt"); + assert_eq!(chat_command(&f, None), format!("tail -n 500 {t}")); + assert_eq!(chat_command(&f, Some("lines=60000")), format!("tail -n 50000 {t}")); + assert_eq!(chat_command(&f, Some("lines=7")), format!("tail -n 7 {t}")); + assert_eq!(chat_command(&f, Some("all=1")), format!("cat {t}")); + assert_eq!(chat_command(&f, Some("all=1;id")), format!("tail -n 500 {t}")); } #[test] - fn chat_routes_known_and_typed() { - assert_eq!(chat_route("/v1/chat/claude"), Some((CHAT_TXT, "text/plain; charset=utf-8"))); - assert_eq!(chat_route("/v1/chat/claude.jsonl"), Some((CHAT_JSONL, "application/x-ndjson"))); - assert_eq!(chat_route("/v1/chat/other"), None); - let tok = "s3cret"; - for p in ["/v1/chat/claude", "/v1/chat/claude.jsonl"] { - assert_eq!(handle(&Method::Get, p, None, tok).0, 401); - assert_eq!(handle(&Method::Get, &format!("{p}?lines=5"), Some("Bearer wrong"), tok).0, 401); - assert_eq!(handle(&Method::Post, p, None, tok).0, 405); + fn chat_commands_jsonl() { + let f = route("/v1/chat/opencode.jsonl"); + assert_eq!(chat_command(&f, None), format!("tail -n 500 {A}/latest/opencode.jsonl")); + assert_eq!(chat_command(&f, Some("lines=7&all=1")), format!("cat {A}/records/opencode/*.jsonl")); + assert_eq!(chat_command(&f, Some("all=1")), format!("cat {A}/records/opencode/*.jsonl")); + } + #[test] + fn chat_allow_list() { + for a in ["claude", "codex", "opencode", "gemini", "muse"] { + assert_eq!(route(&format!("/v1/chat/{a}")).ctype, "text/plain; charset=utf-8"); + assert_eq!(route(&format!("/v1/chat/{a}.jsonl")).ctype, "application/x-ndjson"); + assert_eq!(route(&format!("/v1/chat/{a}")).latest, format!("{A}/latest/{a}.txt")); } + for bad in ["/v1/chat/other", "/v1/chat/claude.txt", "/v1/chat/", "/v1/chat/claude/", "/v1/chat/Claude", + "/v1/chat/../claude", "/v1/chat/claude.jsonl.jsonl", "/v1/chat/.jsonl", "/v1/chat/claude;id"] { + assert_eq!(chat_route(bad), None, "{bad}"); + } + let tok = "s3cret"; + assert_eq!(handle(&Method::Get, "/v1/chat/other", Some("Bearer s3cret"), tok).0, 404); assert_eq!(handle(&Method::Get, "/v1/chat/claude.txt", None, tok).0, 404); } #[test] + fn chat_auth_and_method() { + let tok = "s3cret"; + let mut paths: Vec<String> = vec!["/v1/chat".into()]; + for a in ["claude", "codex", "opencode", "gemini", "muse"] { + paths.push(format!("/v1/chat/{a}")); + paths.push(format!("/v1/chat/{a}.jsonl")); + } + for p in &paths { + assert_eq!(handle(&Method::Get, p, None, tok).0, 401, "{p}"); + assert_eq!(handle(&Method::Get, &format!("{p}?lines=5"), Some("Bearer wrong"), tok).0, 401, "{p}"); + assert_eq!(handle(&Method::Post, p, None, tok).0, 405, "{p}"); + } + } + #[test] + fn chat_agent_list() { + let (code, body, ctype) = handle(&Method::Get, "/v1/chat", Some("Bearer s3cret"), "s3cret"); + assert_eq!((code, ctype), (200, "application/json")); + assert_eq!(body, r#"{"agents":["claude","codex","opencode","gemini","muse"]}"#); + let v: Value = serde_json::from_str(&body).unwrap(); + assert_eq!(v["agents"].as_array().unwrap().len(), AGENTS.len()); + } + #[test] fn feed_defaults() { assert_eq!(feed_body("/nonexistent/x", validate_status, STATUS_DEFAULT), STATUS_DEFAULT); assert_eq!(feed_body("/nonexistent/x", validate_results, RESULTS_DEFAULT), RESULTS_DEFAULT);