From 4b260a84449970d17bbdeb416f7ade2bb95256d4 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:01:26 +0900 Subject: [PATCH 01/67] test(context): pin fail-closed context database guarantees Characterize the safety contract of the context store before moving any code. Read APIs on a missing database return the migration-required error without creating the file or its directory, a symlinked database is refused, and the read-only connection rejects writes. Opening refuses a symlinked WAL sidecar without touching its target. A CSV reindex truncates the WAL and scrubs replaced text even with a concurrent connection open. The reply-queue loader never follows a symlink or creates a missing queue. Co-Authored-By: Claude Opus 5.5 --- src/context/mod.rs | 202 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) diff --git a/src/context/mod.rs b/src/context/mod.rs index cb92ef2a..5d61ada1 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -6436,6 +6436,208 @@ mod tests { Ok(()) } + #[test] + fn context_reads_fail_closed_on_a_missing_database_without_creating_it() { + let dir = tempdir().unwrap(); + let parent = dir.path().join("absent"); + let db = parent.join("context.sqlite3"); + let assert_migration_required = |label: &str, result: Result<()>| { + assert_eq!( + result.unwrap_err().to_string(), + CONTEXT_RETRIEVAL_MIGRATION_REQUIRED, + "{label}" + ); + }; + + assert_migration_required( + "search", + search(&db, Some("방"), None, "probe", "hybrid", 5).map(|_| ()), + ); + assert_migration_required( + "style_search", + style_search(&db, Some("방"), "probe", 5).map(|_| ()), + ); + assert_migration_required( + "style_profile", + style_profile(&db, "방", STYLE_USER, None).map(|_| ()), + ); + assert_migration_required( + "recipient_style_profile", + recipient_style_profile(&db, "방", "민수", None).map(|_| ()), + ); + assert_migration_required( + "response_time_stats", + response_time_stats(&db, "방", STYLE_USER, None).map(|_| ()), + ); + assert_migration_required( + "reply_decision_search", + reply_decision_search(&db, "방", "probe", 5).map(|_| ()), + ); + assert_migration_required( + "get_reply_decision", + get_reply_decision(&db, "db:7:42").map(|_| ()), + ); + assert_migration_required( + "live_context_sync_state", + live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, 7).map(|_| ()), + ); + assert_migration_required( + "context_reply_bundle", + context_reply_bundle(&db, "방", "probe", None).map(|_| ()), + ); + assert_migration_required( + "context_reply_bundle_for_recipient_excluding_live_events", + context_reply_bundle_for_recipient_excluding_live_events( + &db, + "방", + "probe", + None, + "민수", + 7, + &[42], + ) + .map(|_| ()), + ); + + assert!( + !parent.exists(), + "read paths must not create the context database or its directory" + ); + } + + #[cfg(unix)] + #[test] + fn context_reads_refuse_a_symlinked_database() { + let dir = tempdir().unwrap(); + let real = dir.path().join("real.sqlite3"); + let csv = fixture(dir.path(), "chat.csv", "symlink probe"); + index_csv(&real, "방", &csv).unwrap(); + let link = dir.path().join("link.sqlite3"); + std::os::unix::fs::symlink(&real, &link).unwrap(); + + assert_eq!( + search(&real, Some("방"), None, "symlink", "keyword", 5) + .unwrap() + .len(), + 1 + ); + assert_eq!( + search(&link, Some("방"), None, "symlink", "keyword", 5) + .unwrap_err() + .to_string(), + CONTEXT_RETRIEVAL_MIGRATION_REQUIRED + ); + assert_eq!( + context_reply_bundle(&link, "방", "symlink", None) + .unwrap_err() + .to_string(), + CONTEXT_RETRIEVAL_MIGRATION_REQUIRED + ); + } + + #[test] + fn read_only_context_connection_rejects_writes() -> Result<()> { + let dir = tempdir()?; + let db = dir.path().join("readonly.sqlite3"); + let csv = fixture(dir.path(), "chat.csv", "read only probe"); + index_csv(&db, "방", &csv)?; + + let reader = open_db_readonly(&db)?; + let write = reader.execute( + "INSERT INTO context_messages(source, chat, date, user_name, message, vector) + VALUES ('source', '방', '2026-01-01', '민수', 'write probe', x'')", + [], + ); + assert!( + matches!( + write, + Err(rusqlite::Error::SqliteFailure(ref error, _)) + if error.code == rusqlite::ErrorCode::ReadOnly + ), + "{write:?}" + ); + let count: i64 = + reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| row.get(0))?; + assert_eq!(count, 1); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn context_database_open_refuses_a_symlinked_wal_sidecar() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let dir = tempdir()?; + let outside = dir.path().join("outside.bin"); + fs::write(&outside, b"not a wal")?; + fs::set_permissions(&outside, fs::Permissions::from_mode(0o644))?; + let db_dir = dir.path().join("ctx"); + fs::create_dir(&db_dir)?; + let db = db_dir.join("context.sqlite3"); + std::os::unix::fs::symlink(&outside, db_dir.join("context.sqlite3-wal"))?; + let csv = fixture(dir.path(), "chat.csv", "sidecar probe"); + + assert!(index_csv(&db, "방", &csv).is_err()); + assert!(!db.exists()); + assert_eq!(fs::metadata(&outside)?.permissions().mode() & 0o777, 0o644); + assert_eq!(fs::read(&outside)?, b"not a wal"); + Ok(()) + } + + #[test] + fn csv_reindex_truncates_the_wal_and_scrubs_replaced_text() -> Result<()> { + let dir = tempdir()?; + let db = dir.path().join("context.sqlite3"); + let retired = "retiredphrase7d28b"; + let csv = fixture(dir.path(), "chat.csv", retired); + index_csv(&db, "방", &csv)?; + + let concurrent_reader = open_db_readonly(&db)?; + let rows: i64 = + concurrent_reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { + row.get(0) + })?; + assert_eq!(rows, 1); + fs::write(&csv, "Date,User,Message\n2026-01-01,민수,replacement\n")?; + index_csv(&db, "방", &csv)?; + + let wal = fs::read(dir.path().join("context.sqlite3-wal")).unwrap_or_default(); + assert_eq!(wal.len(), 0); + let main_db = fs::read(&db)?; + assert!(!main_db + .windows(retired.len()) + .any(|window| window == retired.as_bytes())); + drop(concurrent_reader); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn queue_loader_never_follows_a_symlink_or_creates_a_missing_queue() { + let dir = tempdir().unwrap(); + let queue = dir.path().join("reply-queue.sqlite3"); + { + let conn = Connection::open(&queue).unwrap(); + conn.execute_batch( + "CREATE TABLE reply_jobs(event_id TEXT, status TEXT, reply TEXT, updated_at REAL); + INSERT INTO reply_jobs VALUES('a','sent','보낸 문장',1000.0);", + ) + .unwrap(); + } + let before = fs::read(&queue).unwrap(); + assert_eq!(confirmed_self_sends_from_queue(&queue).unwrap().len(), 1); + assert_eq!(fs::read(&queue).unwrap(), before); + + let link = dir.path().join("linked-queue.sqlite3"); + std::os::unix::fs::symlink(&queue, &link).unwrap(); + assert!(confirmed_self_sends_from_queue(&link).unwrap().is_empty()); + + let missing_parent = dir.path().join("missing"); + let absent = missing_parent.join("reply-queue.sqlite3"); + assert!(confirmed_self_sends_from_queue(&absent).unwrap().is_empty()); + assert!(!missing_parent.exists()); + } + fn style_profile_with_stats( common_endings: &[(&str, usize)], emoji_count: usize, From 4c9ebbca948e34c0bdefec31024af408719f3bc5 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:06:27 +0900 Subject: [PATCH 02/67] style(context): rustfmt src/context Format-only. Stripping whitespace, the diff is nine removed and three added trailing commas plus braces around closure bodies, so the module no longer contributes to cargo fmt --check failures before the split. Co-Authored-By: Claude Opus 5.5 --- src/context/mod.rs | 195 ++++++++++++++++++++++----------------------- 1 file changed, 97 insertions(+), 98 deletions(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index 5d61ada1..001bd441 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1919,10 +1919,7 @@ fn mark_bot_sent_features(features_json: &str, event_id: &str, offset_seconds: i "content_kind".to_string(), serde_json::json!("bot_sent_reply"), ); - object.insert( - "bot_sent_event_id".to_string(), - serde_json::json!(event_id), - ); + object.insert("bot_sent_event_id".to_string(), serde_json::json!(event_id)); object.insert( "bot_sent_offset_seconds".to_string(), serde_json::json!(offset_seconds), @@ -2696,10 +2693,7 @@ pub fn index_csv_with_queue( } let tx = conn.unchecked_transaction()?; tx.execute("DELETE FROM context_messages WHERE source = ?1", [&source])?; - tx.execute( - "DELETE FROM owner_style WHERE source = ?1", - [&source], - )?; + tx.execute("DELETE FROM owner_style WHERE source = ?1", [&source])?; tx.execute( "DELETE FROM owner_style_profile WHERE source = ?1", [&source], @@ -2725,7 +2719,8 @@ pub fn index_csv_with_queue( // not produce a response-time sample either. let bot_send = if user == STYLE_USER { row_sent_at.and_then(|sent_at| { - let (matched, ambiguous) = match_confirmed_send(&confirmed_sends, &message, sent_at); + let (matched, ambiguous) = + match_confirmed_send(&confirmed_sends, &message, sent_at); matched .filter(|_| !ambiguous) .map(|send| (send, sent_at - send.sent_at)) @@ -2932,21 +2927,19 @@ fn response_time_distribution_second_splits( } let first_midpoint = first_start + (first_end - first_start) / 2; let first_split = first_candidates[first_midpoint]; - let lower = - second_candidates.partition_point(|boundary| *boundary < first_split + minimum); + let lower = second_candidates.partition_point(|boundary| *boundary < first_split + minimum); let scan_start = second_start.max(lower); let mut best: Option<(f64, usize)> = None; for position in scan_start..=second_end { let second_split = second_candidates[position]; - let sse = - response_time_segment_sse(prefix_sum, prefix_square_sum, 0, first_split) - + response_time_segment_sse( - prefix_sum, - prefix_square_sum, - first_split, - second_split, - ) - + response_time_segment_sse(prefix_sum, prefix_square_sum, second_split, total); + let sse = response_time_segment_sse(prefix_sum, prefix_square_sum, 0, first_split) + + response_time_segment_sse( + prefix_sum, + prefix_square_sum, + first_split, + second_split, + ) + + response_time_segment_sse(prefix_sum, prefix_square_sum, second_split, total); if best.is_none_or(|(best_sse, _)| sse < best_sse) { best = Some((sse, position)); } @@ -3674,8 +3667,14 @@ pub fn record_reply_decision(db_path: &Path, record_json: &str) -> Result } } let mut evidence_map = serde_json::Map::new(); - evidence_map.insert("evidence_ids".into(), serde_json::to_value(&input.evidence_ids)?); - evidence_map.insert("style_policy_version".into(), serde_json::to_value(&input.style_policy_version)?); + evidence_map.insert( + "evidence_ids".into(), + serde_json::to_value(&input.evidence_ids)?, + ); + evidence_map.insert( + "style_policy_version".into(), + serde_json::to_value(&input.style_policy_version)?, + ); if let Some(prov) = &input.provenance { evidence_map.insert("provenance".into(), prov.clone()); } @@ -4299,7 +4298,15 @@ pub fn search( ) -> Result> { // 기본 경로는 결정적 로컬 해시 임베더를 주입해 리팩터링 이전과 동일한 // 검색 결과·정렬을 그대로 유지한다. - search_with_embedder(db_path, chat, source, query, mode, limit, &LocalHashEmbedder) + search_with_embedder( + db_path, + chat, + source, + query, + mode, + limit, + &LocalHashEmbedder, + ) } /// 착탈식 임베더를 주입받는 검색 진입점. 기본 `search`는 `LocalHashEmbedder`를 @@ -5258,8 +5265,9 @@ fn set_context_db_permissions(path: &Path) -> Result<()> { use std::os::unix::fs::PermissionsExt; if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) { - fs::set_permissions(parent, fs::Permissions::from_mode(0o700)) - .with_context(|| format!("secure context database directory {}", parent.display()))?; + fs::set_permissions(parent, fs::Permissions::from_mode(0o700)).with_context(|| { + format!("secure context database directory {}", parent.display()) + })?; } for file in [ @@ -5296,11 +5304,10 @@ fn sqlite_sidecar_path(path: &Path, suffix: &str) -> PathBuf { } fn checkpoint_context_wal_truncate(conn: &Connection) -> Result<()> { - let (busy, log_frames, checkpointed_frames): (i64, i64, i64) = conn.query_row( - "PRAGMA wal_checkpoint(TRUNCATE)", - [], - |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)), - )?; + let (busy, log_frames, checkpointed_frames): (i64, i64, i64) = + conn.query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |row| { + Ok((row.get(0)?, row.get(1)?, row.get(2)?)) + })?; if busy != 0 { anyhow::bail!( "context WAL truncate checkpoint remained busy ({checkpointed_frames}/{log_frames} frames checkpointed)" @@ -6368,11 +6375,10 @@ mod tests { writer.execute_batch("BEGIN EXCLUSIVE")?; let reader = open_db_readonly(&db)?; - let messages: i64 = reader.query_row( - "SELECT COUNT(*) FROM context_messages", - [], - |row| row.get(0), - )?; + let messages: i64 = + reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { + row.get(0) + })?; writer.execute_batch("ROLLBACK")?; assert_eq!(messages, 0); Ok(()) @@ -6387,7 +6393,10 @@ mod tests { conn.execute( "INSERT INTO context_messages(source, chat, date, user_name, message, vector) VALUES ('source', 'chat', '2026-09-24', 'user', ?1, ?2)", - params![retired_message, vector_to_bytes(&encode_vector(retired_message))], + params![ + retired_message, + vector_to_bytes(&encode_vector(retired_message)) + ], )?; checkpoint_context_wal_truncate(&conn)?; @@ -6400,7 +6409,9 @@ mod tests { let wal = fs::read(sqlite_sidecar_path(&db, "-wal")).unwrap_or_default(); let main_db = fs::read(&db)?; assert_eq!(wal.len(), 0); - assert!(!wal.windows(retired_message.len()).any(|w| w == retired_message.as_bytes())); + assert!(!wal + .windows(retired_message.len()) + .any(|w| w == retired_message.as_bytes())); assert!(!main_db .windows(retired_message.len()) .any(|w| w == retired_message.as_bytes())); @@ -6422,15 +6433,15 @@ mod tests { )?; set_context_db_permissions(&db)?; - assert_eq!(fs::metadata(dir.path())?.permissions().mode() & 0o777, 0o700); + assert_eq!( + fs::metadata(dir.path())?.permissions().mode() & 0o777, + 0o700 + ); assert_eq!(fs::metadata(&db)?.permissions().mode() & 0o777, 0o600); for suffix in ["-wal", "-shm"] { let sidecar = sqlite_sidecar_path(&db, suffix); if sidecar.exists() { - assert_eq!( - fs::metadata(sidecar)?.permissions().mode() & 0o777, - 0o600 - ); + assert_eq!(fs::metadata(sidecar)?.permissions().mode() & 0o777, 0o600); } } Ok(()) @@ -6556,8 +6567,9 @@ mod tests { ), "{write:?}" ); - let count: i64 = - reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| row.get(0))?; + let count: i64 = reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { + row.get(0) + })?; assert_eq!(count, 1); Ok(()) } @@ -6674,12 +6686,8 @@ mod tests { #[test] fn derive_deferential_endings_are_formal_honorific() { - let profile = style_profile_with_stats( - &[("습니다", 8), ("합니다", 4), ("요", 2)], - 0, - 5, - &[], - ); + let profile = + style_profile_with_stats(&[("습니다", 8), ("합니다", 4), ("요", 2)], 0, 5, &[]); let (honorific, formality) = derive_style_profile_honorific_formality(&profile); assert_eq!(honorific, Honorific::Honorific); assert_eq!(formality, Formality::Formal); @@ -6688,12 +6696,7 @@ mod tests { #[test] fn derive_polite_with_emoji_is_informal_honorific() { // 해요체 위주 + 이모지/축약이 섞이면 존댓말이지만 비격식체. - let profile = style_profile_with_stats( - &[("요", 10), ("죠", 3)], - 6, - 2, - &[("ㅋㅋ", 4)], - ); + let profile = style_profile_with_stats(&[("요", 10), ("죠", 3)], 6, 2, &[("ㅋㅋ", 4)]); let (honorific, formality) = derive_style_profile_honorific_formality(&profile); assert_eq!(honorific, Honorific::Honorific); assert_eq!(formality, Formality::Informal); @@ -6723,12 +6726,8 @@ mod tests { #[test] fn derive_deferential_overwhelmed_by_emoji_is_informal() { // 합쇼체가 있어도 이모지·축약 신호가 우세하면 비격식체로 본다. - let profile = style_profile_with_stats( - &[("습니다", 2), ("요", 6)], - 20, - 1, - &[("ㅋㅋㅋ", 10)], - ); + let profile = + style_profile_with_stats(&[("습니다", 2), ("요", 6)], 20, 1, &[("ㅋㅋㅋ", 10)]); let (honorific, formality) = derive_style_profile_honorific_formality(&profile); assert_eq!(honorific, Honorific::Honorific); assert_eq!(formality, Formality::Informal); @@ -7082,9 +7081,7 @@ mod tests { let conn = open_db(&db).unwrap(); let count: i64 = conn - .query_row("SELECT COUNT(*) FROM owner_style", [], |row| { - row.get(0) - }) + .query_row("SELECT COUNT(*) FROM owner_style", [], |row| row.get(0)) .unwrap(); assert_eq!(count, 1); } @@ -7173,9 +7170,7 @@ mod tests { /// Exhaustive reference for the shipped exact split search. Kept in the /// test module so every randomized case can assert the optimized path /// returns the identical distribution. - fn exhaustive_response_time_distribution( - delays: &[f64], - ) -> Option { + fn exhaustive_response_time_distribution(delays: &[f64]) -> Option { if delays.len() < RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES || delays.iter().any(|delay| { !delay.is_finite() || !(0.0..=MAX_RESPONSE_DELAY_SECONDS as f64).contains(delay) @@ -7219,8 +7214,7 @@ mod tests { let immediate_upper_seconds = delays[first_split - 1].min(global_upper_seconds); let short_lower_seconds = delays[first_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); let short_upper_seconds = delays[second_split - 1].min(global_upper_seconds); - let delayed_lower_seconds = - delays[second_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); + let delayed_lower_seconds = delays[second_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); if immediate_upper_seconds < MIN_SCHEDULED_RESPONSE_DELAY_SECONDS || short_lower_seconds > short_upper_seconds || delayed_lower_seconds > global_upper_seconds @@ -7290,7 +7284,9 @@ mod tests { } fn deterministic_delays(seed: u64, count: usize) -> Vec { - let mut state = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); + let mut state = seed + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); let mut next = move || { state = state .wrapping_mul(6364136223846793005) @@ -7336,16 +7332,16 @@ mod tests { "split diverged at count={count} seed={seed}" ); assert_eq!(fast.global_upper_seconds, reference.global_upper_seconds); - assert_eq!( - fast.tail_winsorized_count, - reference.tail_winsorized_count - ); + assert_eq!(fast.tail_winsorized_count, reference.tail_winsorized_count); assert_eq!(fast.sample_count, reference.sample_count); for (optimized, naive) in fast.components.iter().zip(reference.components.iter()) { assert_eq!(optimized.name, naive.name); assert_eq!(optimized.sample_count, naive.sample_count); assert_eq!(optimized.weight, naive.weight); - assert_eq!(optimized.normal_location_seconds, naive.normal_location_seconds); + assert_eq!( + optimized.normal_location_seconds, + naive.normal_location_seconds + ); assert_eq!(optimized.normal_scale_seconds, naive.normal_scale_seconds); assert_eq!(optimized.lower_seconds, naive.lower_seconds); assert_eq!(optimized.upper_seconds, naive.upper_seconds); @@ -7382,7 +7378,9 @@ mod tests { } fn duplicate_heavy_delays(seed: u64, count: usize) -> Vec { - let mut state = seed.wrapping_mul(2862933555777941757).wrapping_add(3037000493); + let mut state = seed + .wrapping_mul(2862933555777941757) + .wrapping_add(3037000493); let mut next = move || { state = state .wrapping_mul(2862933555777941757) @@ -7448,7 +7446,9 @@ mod tests { ); assert_eq!(fast.global_upper_seconds, reference.global_upper_seconds); assert_eq!(fast.tail_winsorized_count, reference.tail_winsorized_count); - for (optimized, naive) in fast.components.iter().zip(reference.components.iter()) { + for (optimized, naive) in + fast.components.iter().zip(reference.components.iter()) + { assert_eq!(optimized.name, naive.name); assert_eq!(optimized.sample_count, naive.sample_count); assert_eq!(optimized.weight, naive.weight); @@ -7615,10 +7615,10 @@ mod tests { let db = dir.path().join("relative.sqlite3"); let path = fixture(dir.path(), "chat.csv", "hello"); index_csv(&db, "방", &path).unwrap(); - // Token-less queries (`!!!`) no longer fail the whole vector lookup: - // they return an empty hit list so style/timing bundles keep flowing. - let hits = search(&db, Some("방"), None, "!!!", "vector", 5).unwrap(); - assert!(hits.is_empty()); + // Token-less queries (`!!!`) no longer fail the whole vector lookup: + // they return an empty hit list so style/timing bundles keep flowing. + let hits = search(&db, Some("방"), None, "!!!", "vector", 5).unwrap(); + assert!(hits.is_empty()); } #[test] fn records_and_searches_reply_decisions() { @@ -9590,10 +9590,11 @@ mod tests { .unwrap() .to_rfc3339() }; - let sent_at = chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:00", "%Y-%m-%d %H:%M:%S") - .unwrap() - .and_utc() - .timestamp(); + let sent_at = + chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:00", "%Y-%m-%d %H:%M:%S") + .unwrap() + .and_utc() + .timestamp(); record_sent_reply( &db, "bot-1", @@ -9647,10 +9648,11 @@ mod tests { let db = dir.path().join("csv-queue-send.sqlite3"); ensure_live_context_schema(&db).unwrap(); let reply = "역시 세긴 하네요"; - let sent_at = chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:04", "%Y-%m-%d %H:%M:%S") - .unwrap() - .and_utc() - .timestamp(); + let sent_at = + chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:04", "%Y-%m-%d %H:%M:%S") + .unwrap() + .and_utc() + .timestamp(); let queue = dir.path().join("reply-queue.sqlite3"); { let conn = Connection::open(&queue).unwrap(); @@ -9705,13 +9707,11 @@ mod tests { let db = dir.path().join("csv-bot-pacing.sqlite3"); ensure_live_context_schema(&db).unwrap(); let bot_reply = "역시 세긴 하네요"; - let bot_sent_at = chrono::NaiveDateTime::parse_from_str( - "2026-01-01 10:00:04", - "%Y-%m-%d %H:%M:%S", - ) - .unwrap() - .and_utc() - .timestamp(); + let bot_sent_at = + chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:04", "%Y-%m-%d %H:%M:%S") + .unwrap() + .and_utc() + .timestamp(); record_sent_reply( &db, "bot-pacing", @@ -9926,5 +9926,4 @@ mod tests { assert_eq!(kind, "bot_sent_reply"); assert_eq!(eligible, 0); } - } From 243c3c1ac6771f5b8bdeae369f5db87a56dd94d8 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:10:03 +0900 Subject: [PATCH 03/67] refactor(context): delete dead bundle paths and one-caller wrappers Remove code nothing in the repo reaches: - The non-recipient "excluding live events" bundle family (context_reply_bundle_excluding_live_event[s][_json]). The CLI bails on that argument shape, so only the recipient variant can exclude rows. context_reply_bundle now carries the body of its former context_reply_bundle_internal, and context_ids_for_excluded_live_events takes the exclusions directly instead of an always-Some Option. - confirmed_self_sends_from_decisions and REPLY_DECISION_STATUSES, which had zero references. - Test-only one-line wrappers context_reply_bundle_for_recipient[_json] and rebuild_context_index. Their tests now call the wrapped function with the same arguments. - stable_provenance_id, a private alias of provenance_id. - search_with_connection and reply_decision_search_with_connection, each with one caller. - Two empty if-blocks that held only a comment, and the empty index, live and reply_bundle stub modules. Co-Authored-By: Claude Opus 5.5 --- src/context/index.rs | 2 - src/context/live.rs | 2 - src/context/mod.rs | 268 +++++------------------------------- src/context/reply_bundle.rs | 2 - 4 files changed, 34 insertions(+), 240 deletions(-) delete mode 100644 src/context/index.rs delete mode 100644 src/context/live.rs delete mode 100644 src/context/reply_bundle.rs diff --git a/src/context/index.rs b/src/context/index.rs deleted file mode 100644 index f7fa6514..00000000 --- a/src/context/index.rs +++ /dev/null @@ -1,2 +0,0 @@ -//! Offline CSV index / keyword+vector search surface. -//! Bodies stay in `mod.rs` to avoid a schema-touching move. diff --git a/src/context/live.rs b/src/context/live.rs deleted file mode 100644 index d2acf8d6..00000000 --- a/src/context/live.rs +++ /dev/null @@ -1,2 +0,0 @@ -//! Live local-db sync surface. -//! Bodies stay in `mod.rs` to avoid a schema-touching move. diff --git a/src/context/mod.rs b/src/context/mod.rs index 001bd441..4c0c9ce5 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -11,9 +11,6 @@ use std::path::{Path, PathBuf}; use std::time::Duration; mod common; -mod index; -mod live; -mod reply_bundle; pub use common::{CONTEXT_REPLY_BUNDLE_MAX_EXCLUDED_LOG_IDS, STYLE_POLICY_VERSION}; use common::{ CONTEXT_RETRIEVAL_MIGRATION_REQUIRED, RETRIEVAL_INDEX_SCHEMA_VERSION, STYLE_USER, VECTOR_DIM, @@ -482,13 +479,13 @@ impl ContextReplyBundle { } fn redact_provenance(&mut self) { for result in self.context.iter_mut().chain(self.styles.iter_mut()) { - result.source = stable_provenance_id(&result.source); + result.source = provenance_id(&result.source); } if let Some(profile) = self.style_profile.as_mut() { - profile.source = stable_provenance_id(&profile.source); + profile.source = provenance_id(&profile.source); } if let Some(response_time) = self.response_time.as_mut() { - response_time.source = stable_provenance_id(&response_time.source); + response_time.source = provenance_id(&response_time.source); } for prior in &mut self.prior_decisions { if let Ok(mut evidence) = @@ -552,29 +549,29 @@ impl RecipientContextReplyBundle { fn redact_provenance(&mut self) { for result in self.context.iter_mut().chain(self.styles.iter_mut()) { - result.source = stable_provenance_id(&result.source); + result.source = provenance_id(&result.source); } if let Some(profile) = self.style_profile.as_mut() { - profile.source = stable_provenance_id(&profile.source); + profile.source = provenance_id(&profile.source); } if let Some(profile) = self.recipient_style_profile.as_mut() { - profile.profile.source = stable_provenance_id(&profile.profile.source); + profile.profile.source = provenance_id(&profile.profile.source); } if let Some(response_time) = self.response_time.as_mut() { - response_time.source = stable_provenance_id(&response_time.source); + response_time.source = provenance_id(&response_time.source); } redact_reply_decisions(&mut self.prior_decisions); } } -fn stable_provenance_id(source: &str) -> String { +pub fn provenance_id(source: &str) -> String { format!("source:{}", hex::encode(Sha256::digest(source.as_bytes()))) } pub fn redact_absolute_paths(value: &mut serde_json::Value) { match value { serde_json::Value::String(text) if Path::new(text).is_absolute() => { - *text = stable_provenance_id(text); + *text = provenance_id(text); } serde_json::Value::Array(values) => { for value in values { @@ -589,19 +586,16 @@ pub fn redact_absolute_paths(value: &mut serde_json::Value) { _ => {} } } -pub fn provenance_id(value: &str) -> String { - stable_provenance_id(value) -} pub fn redact_context_results(results: &mut [ContextResult]) { for result in results { - result.source = stable_provenance_id(&result.source); + result.source = provenance_id(&result.source); } } pub fn redact_response_time_stats(stats: &mut Option) { if let Some(stats) = stats { - stats.source = stable_provenance_id(&stats.source); + stats.source = provenance_id(&stats.source); } } @@ -613,7 +607,7 @@ pub fn redact_reply_decisions(results: &mut [ReplyDecisionMatch]) { serde_json::to_string(&evidence).unwrap_or_else(|_| "{}".to_string()); } else { result.evidence_json = serde_json::json!({ - "redacted_evidence": stable_provenance_id(&result.evidence_json) + "redacted_evidence": provenance_id(&result.evidence_json) }) .to_string(); } @@ -1837,14 +1831,6 @@ pub struct SelfSendRepairReport { /// ledger. Used where no room queue is addressable (CSV import), so a bot reply /// that was already confirmed in the context database cannot be learned as an /// owner style sample. -pub fn confirmed_self_sends_from_decisions( - db_path: &Path, - chat: &str, -) -> Result> { - let conn = open_db_readonly(db_path)?; - confirmed_self_sends_with_connection(&conn, chat) -} - fn confirmed_self_sends_with_connection( conn: &Connection, chat: &str, @@ -3548,7 +3534,7 @@ pub fn recipient_style_profile_json( ) -> Result> { match recipient_style_profile(db_path, chat, recipient, source)? { Some(mut profile) => { - profile.profile.source = stable_provenance_id(&profile.profile.source); + profile.profile.source = provenance_id(&profile.profile.source); Ok(Some(serde_json::to_string(&profile)?)) } None => Ok(None), @@ -3563,7 +3549,7 @@ pub fn style_profile_json( ) -> Result> { match style_profile(db_path, chat, user, source)? { Some(mut profile) => { - profile.source = stable_provenance_id(&profile.source); + profile.source = provenance_id(&profile.source); Ok(Some(serde_json::to_string(&profile)?)) } None => Ok(None), @@ -4157,16 +4143,7 @@ pub fn reply_decision_search( } let conn = open_db_readonly(db_path)?; ensure_retrieval_index_current(&conn)?; - reply_decision_search_with_connection(&conn, chat, query, limit) -} - -fn reply_decision_search_with_connection( - conn: &Connection, - chat: &str, - query: &str, - limit: usize, -) -> Result> { - reply_decision_search_with_connection_excluding(conn, chat, query, limit, &BTreeSet::new()) + reply_decision_search_with_connection_excluding(&conn, chat, query, limit, &BTreeSet::new()) } fn reply_decision_search_with_connection_excluding( @@ -4328,20 +4305,8 @@ pub fn search_with_embedder( } let conn = open_db_readonly(db_path)?; ensure_retrieval_index_current(&conn)?; - search_with_connection(&conn, chat, source, query, mode, limit, embedder) -} - -fn search_with_connection( - conn: &Connection, - chat: Option<&str>, - source: Option<&str>, - query: &str, - mode: &str, - limit: usize, - embedder: &dyn Embedder, -) -> Result> { search_with_connection_excluding( - conn, + &conn, chat, source, query, @@ -4600,15 +4565,6 @@ fn recipient_style_search_with_connection( .collect()) } -pub fn context_reply_bundle( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, -) -> Result { - context_reply_bundle_internal(db_path, chat, query, source, None) -} - #[derive(Debug)] struct LiveEventExclusions { chat_id: i64, @@ -4646,50 +4602,12 @@ fn live_event_exclusions(chat_id: i64, log_ids: &[i64]) -> Result, - chat_id: i64, - current_log_id: i64, -) -> Result { - context_reply_bundle_excluding_live_events( - db_path, - chat, - query, - source, - chat_id, - &[current_log_id], - ) -} - -/// Build a reply bundle while excluding every row in one bounded incoming -/// burst. Both live context rows and their canonical reply-decision IDs are -/// excluded in the same read transaction. -pub fn context_reply_bundle_excluding_live_events( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, - chat_id: i64, - excluded_log_ids: &[i64], -) -> Result { - let exclusions = live_event_exclusions(chat_id, excluded_log_ids)?; - context_reply_bundle_internal(db_path, chat, query, source, Some(&exclusions)) -} - fn context_ids_for_excluded_live_events( conn: &Connection, chat: &str, preferred_source: Option<&str>, - exclusions: Option<&LiveEventExclusions>, + exclusions: &LiveEventExclusions, ) -> Result> { - let Some(exclusions) = exclusions else { - return Ok(BTreeSet::new()); - }; let has_live_events = conn.query_row( "SELECT EXISTS( SELECT 1 FROM sqlite_master @@ -4732,31 +4650,22 @@ fn context_ids_for_excluded_live_events( Ok(context_ids) } -fn context_reply_bundle_internal( +pub fn context_reply_bundle( db_path: &Path, chat: &str, query: &str, source: Option<&str>, - exclusions: Option<&LiveEventExclusions>, ) -> Result { if chat.trim().is_empty() { anyhow::bail!("chat name must not be empty"); } - if query.trim().is_empty() { - // Punctuation-only or blank inbound still needs style/timing evidence. - } + // Punctuation-only or blank inbound still needs style/timing evidence. let conn = open_db_readonly(db_path)?; conn.execute_batch("BEGIN DEFERRED TRANSACTION")?; let result = (|| { ensure_retrieval_index_current(&conn)?; let preferred_source = preferred_context_source(&conn, Some(chat), source)?; - let excluded_context_ids = context_ids_for_excluded_live_events( - &conn, - chat, - preferred_source.as_deref(), - exclusions, - )?; let context = search_with_connection_excluding( &conn, Some(chat), @@ -4764,7 +4673,7 @@ fn context_reply_bundle_internal( query, "hybrid", CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT, - &excluded_context_ids, + &BTreeSet::new(), &LocalHashEmbedder, )?; let styles = style_search_with_connection( @@ -4775,16 +4684,12 @@ fn context_reply_bundle_internal( CONTEXT_REPLY_BUNDLE_STYLE_LIMIT, true, )?; - let empty_decision_event_ids = BTreeSet::new(); - let excluded_decision_event_ids = exclusions - .map(|value| &value.decision_event_ids) - .unwrap_or(&empty_decision_event_ids); let prior_decisions = reply_decision_search_with_connection_excluding( &conn, chat, query, CONTEXT_REPLY_BUNDLE_DECISION_LIMIT, - excluded_decision_event_ids, + &BTreeSet::new(), )?; let style_profile = style_profile_with_connection( &conn, @@ -4825,30 +4730,11 @@ fn context_reply_bundle_internal( } /// Build the version-2 reply bundle for a specific participant in one read -/// snapshot. The current live row is excluded from context retrieval. Direct +/// snapshot. Every live row of the incoming burst (`excluded_log_ids`) is +/// excluded from context retrieval and prior decisions. Direct /// recipient-linked style evidence is used only when its profile clears the /// sample/confidence floor; otherwise both profile and style examples fall /// back to the authoritative room-wide register. -pub fn context_reply_bundle_for_recipient( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, - recipient: &str, - chat_id: i64, - current_log_id: i64, -) -> Result { - context_reply_bundle_for_recipient_excluding_live_events( - db_path, - chat, - query, - source, - recipient, - chat_id, - &[current_log_id], - ) -} - pub fn context_reply_bundle_for_recipient_excluding_live_events( db_path: &Path, chat: &str, @@ -4861,9 +4747,7 @@ pub fn context_reply_bundle_for_recipient_excluding_live_events( if chat.trim().is_empty() || recipient.trim().is_empty() { anyhow::bail!("recipient context reply bundle chat and recipient must not be empty"); } - if query.trim().is_empty() { - // Recipient bundles keep style/timing when the inbound has no tokens. - } + // Recipient bundles keep style/timing when the inbound has no tokens. let exclusions = live_event_exclusions(chat_id, excluded_log_ids)?; let conn = open_db_readonly(db_path)?; @@ -4875,7 +4759,7 @@ pub fn context_reply_bundle_for_recipient_excluding_live_events( &conn, chat, preferred_source.as_deref(), - Some(&exclusions), + &exclusions, )?; let context = search_with_connection_excluding( &conn, @@ -4962,26 +4846,6 @@ pub fn context_reply_bundle_for_recipient_excluding_live_events( } } -pub fn context_reply_bundle_for_recipient_json( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, - recipient: &str, - chat_id: i64, - current_log_id: i64, -) -> Result { - context_reply_bundle_for_recipient_excluding_live_events_json( - db_path, - chat, - query, - source, - recipient, - chat_id, - &[current_log_id], - ) -} - pub fn context_reply_bundle_for_recipient_excluding_live_events_json( db_path: &Path, chat: &str, @@ -5010,50 +4874,6 @@ pub fn context_reply_bundle_for_recipient_excluding_live_events_json( Ok(output) } -pub fn context_reply_bundle_excluding_live_event_json( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, - chat_id: i64, - current_log_id: i64, -) -> Result { - context_reply_bundle_excluding_live_events_json( - db_path, - chat, - query, - source, - chat_id, - &[current_log_id], - ) -} - -pub fn context_reply_bundle_excluding_live_events_json( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, - chat_id: i64, - excluded_log_ids: &[i64], -) -> Result { - let bundle = context_reply_bundle_excluding_live_events( - db_path, - chat, - query, - source, - chat_id, - excluded_log_ids, - )?; - let output = serde_json::to_string(&bundle)?; - if output.len() > CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES { - anyhow::bail!( - "context reply bundle exceeds {} bytes", - CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES - ); - } - Ok(output) -} - pub fn context_reply_bundle_json( db_path: &Path, chat: &str, @@ -5071,21 +4891,6 @@ pub fn context_reply_bundle_json( Ok(output) } -pub const REPLY_DECISION_STATUSES: &[&str] = &[ - "pending", - "processing", - "projection_pending", - "scheduled", - "sending", - "accepted_unconfirmed", - "sent", - "skipped", - "failed", - "delivery_unknown", - "reconcile_required", - "poison", -]; - #[derive(Debug, Clone, Serialize)] pub struct ReplyDecision { pub event_id: String, @@ -6003,11 +5808,6 @@ fn rebuild_retrieval_index(conn: &Connection) -> Result<()> { Ok(()) } -pub fn rebuild_context_index(db_path: &Path) -> Result<()> { - let conn = open_db(db_path)?; - rebuild_retrieval_index(&conn) -} - fn keyword_search_excluding( conn: &Connection, chat: Option<&str>, @@ -8194,14 +7994,14 @@ mod tests { assert!(record_reply_decision(&db, &record.to_string()).unwrap()); } - let unfiltered = context_reply_bundle_for_recipient( + let unfiltered = context_reply_bundle_for_recipient_excluding_live_events( &db, chat, "burstmarker", Some(&ingested.source), "민수", chat_id, - 999, + &[999], ) .unwrap(); assert!(unfiltered @@ -8341,7 +8141,7 @@ mod tests { let error = search(&db, Some("chat-a"), None, "rebuild", "keyword", 5).unwrap_err(); assert_eq!(error.to_string(), CONTEXT_RETRIEVAL_MIGRATION_REQUIRED); - rebuild_context_index(&db).unwrap(); + rebuild_retrieval_index(&open_db(&db).unwrap()).unwrap(); let results = search(&db, Some("chat-a"), None, "rebuild", "keyword", 5).unwrap(); assert_eq!(results.len(), 1); assert_eq!(results[0].message, "rebuild marker"); @@ -8989,14 +8789,14 @@ mod tests { assert!(!plan.contains("SCAN styles")); assert!(!plan.contains("USE TEMP B-TREE")); - let direct_bundle = context_reply_bundle_for_recipient( + let direct_bundle = context_reply_bundle_for_recipient_excluding_live_events( &db, "부자멘토멘티", "현재질문표식", None, "민수", chat_id, - 9, + &[9], ) .unwrap(); assert_eq!( @@ -9022,14 +8822,14 @@ mod tests { .iter() .all(|style| style.source.starts_with("source:"))); - let fallback_bundle = context_reply_bundle_for_recipient( + let fallback_bundle = context_reply_bundle_for_recipient_excluding_live_events( &db, "부자멘토멘티", "현재질문표식", None, "영희", chat_id, - 9, + &[9], ) .unwrap(); assert!(fallback_bundle @@ -9045,14 +8845,14 @@ mod tests { .iter() .all(|style| style.mode == "vector_style")); - let json = context_reply_bundle_for_recipient_json( + let json = context_reply_bundle_for_recipient_excluding_live_events_json( &db, "부자멘토멘티", "현재질문표식", None, "민수", chat_id, - 9, + &[9], ) .unwrap(); let value: serde_json::Value = serde_json::from_str(&json).unwrap(); diff --git a/src/context/reply_bundle.rs b/src/context/reply_bundle.rs deleted file mode 100644 index 14876cbe..00000000 --- a/src/context/reply_bundle.rs +++ /dev/null @@ -1,2 +0,0 @@ -//! Reply-bundle / recipient-style / decision surface. -//! Bodies stay in `mod.rs` to avoid a schema-touching move. From ba11071e374b95a4db42155d28dc8a0b71748097 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:10:51 +0900 Subject: [PATCH 04/67] fix: make every bridge child reachable by global abort Children started without a caller token (mlx-server-launch, model-set, model-prepare, room-upsert, knowledge actions) were never registered, so a global abort could not reach them during their long timeouts. Register every child under an internal key that caller tokens cannot name or cancel. Adds fixture-backed bridge tests that run the real spawn path with a synthetic python3 child and assert the abort returns and the child is reaped. Co-Authored-By: GPT-6 Astra via opencodex --- desktop/src-tauri/src/python_bridge.rs | 187 +++++++++++++++++++++---- 1 file changed, 160 insertions(+), 27 deletions(-) diff --git a/desktop/src-tauri/src/python_bridge.rs b/desktop/src-tauri/src/python_bridge.rs index 4b245697..2893209a 100644 --- a/desktop/src-tauri/src/python_bridge.rs +++ b/desktop/src-tauri/src/python_bridge.rs @@ -51,6 +51,10 @@ const MODEL_SWAP_OPT_IN: &str = "qwen38-27b-explicit-v1"; const MODEL_SWAP_CANCEL_DIR: &str = "model-swap-cancel"; const VOICE_TTS_OUT_NAME: &str = "jarvis-voice-out.wav"; static VOICE_SESSION_START_LOCK: Mutex<()> = Mutex::new(()); +// Registry keys for children started without a caller token. The prefix is +// refused for caller tokens, so the UI cannot cancel these entries by name. +const INTERNAL_CANCEL_PREFIX: &str = "internal:"; +static INTERNAL_CANCEL_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); #[derive(Debug, Error)] pub enum BridgeError { @@ -591,6 +595,9 @@ impl PythonBridge { } pub fn cancel(&self, token_id: &str) -> bool { + if token_id.starts_with(INTERNAL_CANCEL_PREFIX) { + return false; + } let Ok(map) = self.cancellations.lock() else { return false; }; @@ -737,31 +744,38 @@ impl PythonBridge { } else { None }; - if let Some(token) = token_id { - { - let mut map = self - .cancellations - .lock() - .map_err(|_| BridgeError::StateIo)?; - if map.contains_key(token) { - return Err(BridgeError::ActionNotAllowed); - } - if let Some(flag) = global_abort_flag.as_ref() { - if global_abort_is_latched(&self.config.state_root) { - // The child still starts so Python can return its fixed - // aborted envelope; Rust enforces the hard deadline. - flag.store(true, Ordering::SeqCst); - } + // Every child is registered so global abort and shutdown can reach it. + // Callers without a token get an internal key the UI cannot cancel. + let registry_key = match token_id { + Some(token) if token.starts_with(INTERNAL_CANCEL_PREFIX) => { + return Err(BridgeError::ActionNotAllowed); + } + Some(token) => token.to_string(), + None => internal_cancel_key(), + }; + { + let mut map = self + .cancellations + .lock() + .map_err(|_| BridgeError::StateIo)?; + if map.contains_key(®istry_key) { + return Err(BridgeError::ActionNotAllowed); + } + if let Some(flag) = global_abort_flag.as_ref() { + if global_abort_is_latched(&self.config.state_root) { + // The child still starts so Python can return its fixed + // aborted envelope; Rust enforces the hard deadline. + flag.store(true, Ordering::SeqCst); } - map.insert( - token.to_string(), - CancellationHandle { - flag: cancel_flag.clone(), - cooperative_marker: cooperative_marker.clone(), - global_abort_flag: global_abort_flag.clone(), - }, - ); } + map.insert( + registry_key.clone(), + CancellationHandle { + flag: cancel_flag.clone(), + cooperative_marker: cooperative_marker.clone(), + global_abort_flag: global_abort_flag.clone(), + }, + ); } let mut args = vec![ @@ -791,10 +805,8 @@ impl PythonBridge { recovery_timeout: MODEL_SWAP_RECOVERY_TIMEOUT, }, ); - if let Some(token) = token_id { - if let Ok(mut map) = self.cancellations.lock() { - map.remove(token); - } + if let Ok(mut map) = self.cancellations.lock() { + map.remove(®istry_key); } if let Some(marker) = cooperative_marker { let _ = fs::remove_file(marker); @@ -1399,6 +1411,11 @@ fn settings_action_args( Ok(args) } +fn internal_cancel_key() -> String { + let seq = INTERNAL_CANCEL_SEQ.fetch_add(1, Ordering::Relaxed); + format!("{INTERNAL_CANCEL_PREFIX}{seq}") +} + fn valid_model_swap_token(value: &str) -> bool { let bytes = value.as_bytes(); if bytes.len() != 36 { @@ -4949,4 +4966,120 @@ mod tests { ); let _ = fs::remove_dir_all(&temp); } + + // Fixture bridge: every bundled payload path exists with synthetic bytes, + // so resource validation passes and tests reach the real spawn path. The + // dev layout with `python3` runs the synthetic script found at the menubar + // path. Nothing from the installed app, a LaunchAgent, or the real state + // directories is touched. + fn write_fixture_file(path: &Path, bytes: &[u8], executable: bool) { + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, bytes).unwrap(); + fs::set_permissions( + path, + std::os::unix::fs::PermissionsExt::from_mode(if executable { 0o755 } else { 0o644 }), + ) + .unwrap(); + } + + fn fixture_bridge(name: &str, script: &str) -> (PythonBridge, PathBuf) { + let root = std::env::temp_dir() + .canonicalize() + .unwrap() + .join(format!("openkakao-bridge-{name}-{}", std::process::id())); + let _ = fs::remove_dir_all(&root); + for relative in resource_layout::DATA_FILES { + write_fixture_file(&root.join(relative), b"fixture only", false); + } + write_fixture_file(&root.join(resource_layout::CLI), b"#!/bin/sh\nexit 0\n", true); + write_fixture_file(&root.join(resource_layout::MENUBAR_SCRIPT), script.as_bytes(), false); + let mut bridge = PythonBridge::new(); + let config = Arc::get_mut(&mut bridge.config).unwrap(); + config.python = PathBuf::from("python3"); + config.resources = Ok(ResourceLayout { + root: root.clone(), + installed: false, + script: root.join(resource_layout::MENUBAR_SCRIPT), + bin: root.join(resource_layout::CLI), + }); + config.state_root = root.join("state"); + config.logs_dir = root.join("logs"); + (bridge, root) + } + + /// Synthetic child: records its own PID under the state root, then sleeps. + const SLEEPING_CHILD: &str = "import os, sys, time\nfrom pathlib import Path\nstate = Path(sys.argv[sys.argv.index('--state-root') + 1])\nstate.mkdir(parents=True, exist_ok=True)\n(state / 'child.pid').write_text(str(os.getpid()))\ntime.sleep(30)\n"; + + fn wait_for_pid_file(path: &Path) -> i32 { + let deadline = Instant::now() + Duration::from_secs(10); + loop { + if let Some(pid) = fs::read_to_string(path) + .ok() + .and_then(|text| text.trim().parse::().ok()) + { + return pid; + } + assert!(Instant::now() < deadline, "child never published its pid"); + thread::sleep(Duration::from_millis(10)); + } + } + + /// Signal 0 probes existence only for a PID this test spawned. A reaped + /// process reports ESRCH; a zombie still answers, so the probe also proves + /// the bridge reaped it. + fn pid_is_gone(pid: i32) -> bool { + let probe = unsafe { libc::kill(pid, 0) }; + probe == -1 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH) + } + + fn wait_until_gone(pid: i32, limit: Duration) -> bool { + let deadline = Instant::now() + limit; + while Instant::now() < deadline { + if pid_is_gone(pid) { + return true; + } + thread::sleep(Duration::from_millis(20)); + } + pid_is_gone(pid) + } + + /// Cleanup only: kills a PID that a test's own synthetic child published. + fn kill_own_child(pid: i32) { + if pid > 0 && !pid_is_gone(pid) { + unsafe { + libc::kill(pid, libc::SIGKILL); + } + } + } + + #[test] + fn global_abort_reaches_untokened_child() { + // mlx-server-launch, model-set, model-prepare, room-upsert and the + // knowledge actions run without a token. Global abort must still reach + // them during their long timeouts. + let (bridge, root) = fixture_bridge("untokened-abort", SLEEPING_CHILD); + let worker = bridge.clone(); + let (outcome_tx, outcome_rx) = std::sync::mpsc::channel(); + thread::spawn(move || { + let result = worker.run_python_with_output_limit(PythonRun { + timeout: Duration::from_secs(60), + ..PythonRun::default() + }); + let _ = outcome_tx.send(result.map(|_| ())); + }); + let pid = wait_for_pid_file(&bridge.config.state_root.join("child.pid")); + let started = Instant::now(); + bridge.global_abort().unwrap(); + let outcome = outcome_rx.recv_timeout(Duration::from_secs(5)); + let returned_in = started.elapsed(); + let gone = wait_until_gone(pid, Duration::from_secs(2)); + kill_own_child(pid); + fs::remove_dir_all(root).unwrap(); + assert!( + matches!(outcome, Ok(Err(BridgeError::Cancelled))), + "untokened child was not aborted: {outcome:?}" + ); + assert!(returned_in < Duration::from_secs(2)); + assert!(gone, "aborted child survived"); + } } From d753084977f28f3f53dd86bd9855d5e5e31cea30 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:11:51 +0900 Subject: [PATCH 05/67] refactor(auto-reply): remove unreferenced constants, methods and helpers Delete symbols with no caller anywhere in the repo (scripts, tests, desktop, macOS, manifests): ALIAS_DICTIONARY_VERSION, VECTOR_CANDIDATE_MIN, QWEN38_27B_MIN_MEMORY_GB, ModelResidencyManager.cancel and reset_after_human_resume, detect_available_engines, is_audited_gold. Drop the unused aliases binding in the graph context builder and keep its json.loads, which still rejects a corrupt row. Remove an f-string with no placeholders. No behavior change. Co-Authored-By: GPT-6.1 Sol via opencodex --- scripts/auto_reply_golden_dataset.py | 4 ---- scripts/auto_reply_knowledge_graph.py | 7 +++---- scripts/auto_reply_ondevice.py | 14 -------------- 3 files changed, 3 insertions(+), 22 deletions(-) diff --git a/scripts/auto_reply_golden_dataset.py b/scripts/auto_reply_golden_dataset.py index 900ec43c..2e9faf08 100644 --- a/scripts/auto_reply_golden_dataset.py +++ b/scripts/auto_reply_golden_dataset.py @@ -371,10 +371,6 @@ def audit_golden_record(record: dict[str, Any], approvals: QualityApprovals | No } -def is_audited_gold(record: dict[str, Any], approvals: QualityApprovals | None = None) -> bool: - return bool(audit_golden_record(record, approvals).get("accepted")) - - def audit_golden_records( records: Sequence[dict[str, Any]], approvals: QualityApprovals | None = None, diff --git a/scripts/auto_reply_knowledge_graph.py b/scripts/auto_reply_knowledge_graph.py index b7eaaa8e..37734e1a 100644 --- a/scripts/auto_reply_knowledge_graph.py +++ b/scripts/auto_reply_knowledge_graph.py @@ -78,7 +78,6 @@ # 호환된다(2026-09-22). _ANN_SIGN_MATRIX_CACHE: dict[tuple[int, int], tuple[bytes, ...]] = {} _ANN_SIGN_MATRIX_CACHE_MAX_SHAPES = 8 -ALIAS_DICTIONARY_VERSION = "2026-09-20.1" KST = ZoneInfo("Asia/Seoul") # Provenance kinds. "seed" is a hand-written starting node that no message @@ -2397,14 +2396,15 @@ def collect_knowledge_graph_list( ) except (TypeError, ValueError): evidence = _seed_evidence() - aliases = json.loads(aliases_str) + # Parsed only to reject a corrupt row; the aliases are not shown. + json.loads(aliases_str) facts = json.loads(facts_str) connected = rel_map.get(eid, []) body_parts = [ f"【{name}】 ({cat})", f"설명: {desc}", - f"주요 사실/맥락:", + "주요 사실/맥락:", ] for fact in facts: body_parts.append(f" • {fact}") @@ -2701,7 +2701,6 @@ def _alias_matches( KNOWLEDGE_EMBEDDING_DIM = 128 -VECTOR_CANDIDATE_MIN = 0.0 def _deterministic_text_embedding( diff --git a/scripts/auto_reply_ondevice.py b/scripts/auto_reply_ondevice.py index 4b47bf52..85c818f2 100644 --- a/scripts/auto_reply_ondevice.py +++ b/scripts/auto_reply_ondevice.py @@ -40,7 +40,6 @@ MLX_GATEWAY_CANDIDATES = (MLX_GATEWAY_BASE_URL,) FLASH_NEXT_MODEL_ID = "mlx/ddalcu/Qwen3.8-Flash-Next-MLX-Serve-mixed-4-8bit" QWEN38_27B_MODEL_ID = "mlx/ddalcu/Qwen3.8-27B-MLX-Serve-4bit" -QWEN38_27B_MIN_MEMORY_GB = 32.0 QWEN38_27B_DEFAULT_LOADED = False PROBE_TIMEOUT_SECONDS = 45.0 PROBE_HARD_TIMEOUT_SECONDS = 90.0 @@ -329,15 +328,6 @@ def request_lease(self) -> Iterator[None]: self._in_flight = max(0, self._in_flight - 1) self._condition.notify_all() - def cancel(self) -> None: - with self._condition: - self._cancelled = True - self._condition.notify_all() - - def reset_after_human_resume(self) -> None: - with self._condition: - self._cancelled = False - def _drain(self, timeout: float) -> bool: deadline = time.monotonic() + max(0.0, timeout) with self._condition: @@ -1076,10 +1066,6 @@ def detect_engine_paths( return paths -def detect_available_engines() -> list[str]: - return list(detect_engine_paths().keys()) - - def detect_local_qwen_models(home: Path | None = None) -> dict[str, str]: """Find already present Qwen3.8 directories without touching model hubs.""" root = (home or Path.home()) / "Models" From 12469abd21502e35190c27fe570af96ed15e5f27 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:13:07 +0900 Subject: [PATCH 06/67] fix: stop bridge children by process group with a SIGTERM grace Timeouts and cancels killed only the direct child, so a descendant that inherited stdout kept the pipe open and the reader join stretched the timeout until it exited. Spawn each child in its own process group, SIGTERM the group, allow a short grace, then SIGKILL the group. Bound every pipe drain and detach a thread that a surviving descendant still holds instead of joining it. Co-Authored-By: GPT-6 Astra via opencodex --- desktop/src-tauri/src/python_bridge.rs | 246 +++++++++++++++++++++++-- 1 file changed, 226 insertions(+), 20 deletions(-) diff --git a/desktop/src-tauri/src/python_bridge.rs b/desktop/src-tauri/src/python_bridge.rs index 2893209a..4638c346 100644 --- a/desktop/src-tauri/src/python_bridge.rs +++ b/desktop/src-tauri/src/python_bridge.rs @@ -9,8 +9,9 @@ use std::fs::{self, OpenOptions}; use std::io::{Read, Write}; use std::os::unix::ffi::{OsStrExt, OsStringExt}; use std::os::unix::fs::OpenOptionsExt; +use std::os::unix::process::CommandExt; use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; +use std::process::{Child, Command, Stdio}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use std::thread; @@ -25,6 +26,10 @@ const MODEL_SWAP_TIMEOUT: Duration = Duration::from_secs(120); const MODEL_SWAP_RECOVERY_TIMEOUT: Duration = Duration::from_secs(120); const MLX_LAUNCH_TIMEOUT: Duration = Duration::from_secs(150); const OUTPUT_LIMIT_BYTES: usize = 4 * 1024 * 1024; +// SIGTERM grace before SIGKILL on a stopped process group, and the bound on +// pipe drains after the leader is gone. +const PROCESS_TERM_GRACE: Duration = Duration::from_millis(250); +const PIPE_DRAIN_GRACE: Duration = Duration::from_millis(500); const BROWSER_TOOL_OUTPUT_LIMIT_BYTES: usize = 96 * 1024; const BROWSER_TOOL_RESULT_LIMIT_BYTES: usize = 64 * 1024; const BROWSER_TOOL_TASK_LIMIT_BYTES: usize = 16 * 1024; @@ -970,8 +975,12 @@ fn run_process_with_recovery( Stdio::null() }) .stdout(Stdio::piped()) - .stderr(Stdio::null()); + .stderr(Stdio::null()) + // A private group lets stop signals reach descendants, not just the + // direct child. The group id is the child's pid. + .process_group(0); let mut child = command.spawn().map_err(|_| BridgeError::Spawn)?; + let group = i32::try_from(child.id()).map_err(|_| BridgeError::Spawn)?; let mut stdin_writer = if let Some(payload) = stdin_payload { let mut stdin = child.stdin.take().ok_or(BridgeError::Spawn)?; let payload = payload.to_vec(); @@ -1010,12 +1019,7 @@ fn run_process_with_recovery( let mut global_abort_started = None; let status = loop { if hard_cancel_flag.load(Ordering::SeqCst) { - let _ = child.kill(); - let _ = child.wait(); - if let Some(writer) = stdin_writer.take() { - let _ = writer.join(); - } - let _ = reader.join(); + stop_process_group(&mut child, group, stdin_writer, reader); return Err(BridgeError::Cancelled); } if global_abort @@ -1033,12 +1037,7 @@ fn run_process_with_recovery( } } if recovery_started.is_none_or(|since| since.elapsed() >= recovery_timeout) { - let _ = child.kill(); - let _ = child.wait(); - if let Some(writer) = stdin_writer.take() { - let _ = writer.join(); - } - let _ = reader.join(); + stop_process_group(&mut child, group, stdin_writer, reader); return Err(BridgeError::Timeout); } } @@ -1050,12 +1049,7 @@ fn run_process_with_recovery( .as_ref() .is_some_and(|(_, grace)| since.elapsed() >= *grace) }) { - let _ = child.kill(); - let _ = child.wait(); - if let Some(writer) = stdin_writer.take() { - let _ = writer.join(); - } - let _ = reader.join(); + stop_process_group(&mut child, group, stdin_writer, reader); return Err(BridgeError::Cancelled); } thread::sleep(Duration::from_millis(10)); @@ -1063,9 +1057,23 @@ fn run_process_with_recovery( } }; + // The leader has exited, but a descendant may still hold the pipes open. + // Every wait below is bounded; a stuck pipe is stopped, then abandoned. if let Some(writer) = stdin_writer.take() { + if !wait_for_thread(&writer, PIPE_DRAIN_GRACE) { + signal_process_group(group, libc::SIGKILL); + if !wait_for_thread(&writer, PIPE_DRAIN_GRACE) { + return Err(BridgeError::Wait); + } + } writer.join().map_err(|_| BridgeError::Wait)??; } + if !wait_for_thread(&reader, PIPE_DRAIN_GRACE) { + signal_process_group(group, libc::SIGKILL); + if !wait_for_thread(&reader, PIPE_DRAIN_GRACE) { + return Err(BridgeError::Wait); + } + } let (output, oversized) = reader.join().map_err(|_| BridgeError::Wait)??; if oversized { return Err(BridgeError::OutputTooLarge); @@ -1079,6 +1087,72 @@ fn run_process_with_recovery( Ok(output) } +/// Stop a child and everything it spawned, then release its pipe threads. +fn stop_process_group( + child: &mut Child, + group: i32, + stdin_writer: Option>>, + reader: thread::JoinHandle, bool), BridgeError>>, +) { + terminate_process_group(child, group); + if let Some(writer) = stdin_writer { + let _ = join_when_finished(writer, PIPE_DRAIN_GRACE); + } + let _ = join_when_finished(reader, PIPE_DRAIN_GRACE); +} + +/// SIGTERM the group, allow a short grace for handlers, then SIGKILL whatever +/// remains. The leader is reaped here so the group id stays reserved until the +/// group is gone. +fn terminate_process_group(child: &mut Child, group: i32) { + signal_process_group(group, libc::SIGTERM); + let deadline = Instant::now() + PROCESS_TERM_GRACE; + loop { + let _ = child.try_wait(); + if !process_group_alive(group) || Instant::now() >= deadline { + break; + } + thread::sleep(Duration::from_millis(10)); + } + if process_group_alive(group) { + signal_process_group(group, libc::SIGKILL); + } + let _ = child.wait(); +} + +fn signal_process_group(group: i32, signal: libc::c_int) { + // A negative pid addresses the process group. Errors mean the group is + // already gone, which is the state we want. + unsafe { + libc::killpg(group, signal); + } +} + +fn process_group_alive(group: i32) -> bool { + unsafe { libc::killpg(group, 0) == 0 } +} + +fn wait_for_thread(handle: &thread::JoinHandle, limit: Duration) -> bool { + let deadline = Instant::now() + limit; + while !handle.is_finished() { + if Instant::now() >= deadline { + return false; + } + thread::sleep(Duration::from_millis(5)); + } + true +} + +/// Join only a finished thread. A thread still blocked on a pipe is detached +/// rather than joined, so the caller's deadline holds. +fn join_when_finished(handle: thread::JoinHandle, limit: Duration) -> Option { + if wait_for_thread(&handle, limit) { + handle.join().ok() + } else { + None + } +} + fn parse_json_output(bytes: &[u8]) -> Result { serde_json::from_slice(bytes).map_err(|_| BridgeError::Json) } @@ -5052,6 +5126,138 @@ mod tests { } } + #[test] + fn timeout_stops_grandchild_that_holds_stdout() { + // A shell spawns a sleeping grandchild that inherits stdout. Killing only + // the shell used to leave the pipe open, so the timeout waited 30s. + let temp = std::env::temp_dir() + .canonicalize() + .unwrap() + .join(format!("openkakao-group-timeout-{}", std::process::id())); + let _ = fs::remove_dir_all(&temp); + fs::create_dir_all(&temp).unwrap(); + let pid_file = temp.join("grandchild.pid"); + let script = format!("sleep 30 & echo $! > '{}'; sleep 30", pid_file.display()); + let started = Instant::now(); + let result = run_process_with_recovery( + "sh", + &["-c".to_string(), script], + Duration::from_millis(200), + 1024, + ProcessControl { + stdin_payload: None, + hard_cancel_flag: Arc::new(AtomicBool::new(false)), + global_abort: None, + cooperative_marker: None, + recovery_timeout: Duration::ZERO, + }, + ); + let elapsed = started.elapsed(); + let grandchild = wait_for_pid_file(&pid_file); + let gone = wait_until_gone(grandchild, Duration::from_secs(2)); + kill_own_child(grandchild); + fs::remove_dir_all(&temp).unwrap(); + assert!(matches!(result, Err(BridgeError::Timeout))); + assert!( + elapsed < Duration::from_secs(3), + "timeout stretched to {elapsed:?} by a grandchild holding stdout" + ); + assert!(gone, "grandchild survived the timeout"); + } + + #[test] + fn cancel_gives_child_a_sigterm_grace_before_kill() { + // The child installs a SIGTERM handler that records the signal. A + // SIGKILL-only stop never runs it, so the marker would be missing. + let temp = std::env::temp_dir() + .canonicalize() + .unwrap() + .join(format!("openkakao-term-grace-{}", std::process::id())); + let _ = fs::remove_dir_all(&temp); + fs::create_dir_all(&temp).unwrap(); + let term_marker = temp.join("term.txt"); + let ready = temp.join("ready.pid"); + let script = "import os, pathlib, signal, sys, time\n\ + term, ready = pathlib.Path(sys.argv[1]), pathlib.Path(sys.argv[2])\n\ + def on_term(*_):\n term.write_text('term')\n sys.exit(0)\n\ + signal.signal(signal.SIGTERM, on_term)\n\ + ready.write_text(str(os.getpid()))\n\ + time.sleep(30)\n"; + let cancel = Arc::new(AtomicBool::new(false)); + let trigger = cancel.clone(); + let ready_for_trigger = ready.clone(); + let signal = thread::spawn(move || { + let child_pid = wait_for_pid_file(&ready_for_trigger); + trigger.store(true, Ordering::SeqCst); + child_pid + }); + let result = run_process_with_recovery( + "python3", + &[ + "-c".to_string(), + script.to_string(), + term_marker.to_string_lossy().into_owned(), + ready.to_string_lossy().into_owned(), + ], + Duration::from_secs(20), + 1024, + ProcessControl { + stdin_payload: None, + hard_cancel_flag: cancel, + global_abort: None, + cooperative_marker: None, + recovery_timeout: Duration::ZERO, + }, + ); + let child_pid = signal.join().unwrap(); + let gone = wait_until_gone(child_pid, Duration::from_secs(2)); + let marker = fs::read_to_string(&term_marker).ok(); + kill_own_child(child_pid); + fs::remove_dir_all(&temp).unwrap(); + assert!(matches!(result, Err(BridgeError::Cancelled))); + assert_eq!(marker.as_deref(), Some("term"), "SIGTERM was not delivered"); + assert!(gone, "child survived cancellation"); + } + + #[test] + fn normal_exit_does_not_wait_on_a_lingering_grandchild() { + // The leader succeeds, but a background grandchild keeps stdout open. + // The pipe drain used to be unbounded, so this blocked for 30s. + let temp = std::env::temp_dir() + .canonicalize() + .unwrap() + .join(format!("openkakao-group-exit-{}", std::process::id())); + let _ = fs::remove_dir_all(&temp); + fs::create_dir_all(&temp).unwrap(); + let pid_file = temp.join("grandchild.pid"); + let script = format!("sleep 30 & echo $! > '{}'; echo done", pid_file.display()); + let started = Instant::now(); + let result = run_process_with_recovery( + "sh", + &["-c".to_string(), script], + Duration::from_secs(5), + 1024, + ProcessControl { + stdin_payload: None, + hard_cancel_flag: Arc::new(AtomicBool::new(false)), + global_abort: None, + cooperative_marker: None, + recovery_timeout: Duration::ZERO, + }, + ); + let elapsed = started.elapsed(); + let grandchild = wait_for_pid_file(&pid_file); + let gone = wait_until_gone(grandchild, Duration::from_secs(2)); + kill_own_child(grandchild); + fs::remove_dir_all(&temp).unwrap(); + assert_eq!(result.unwrap(), b"done\n"); + assert!( + elapsed < Duration::from_secs(5), + "normal exit waited {elapsed:?} on a lingering grandchild" + ); + assert!(gone, "lingering grandchild was not stopped"); + } + #[test] fn global_abort_reaches_untokened_child() { // mlx-server-launch, model-set, model-prepare, room-upsert and the From 0b9df2f0a97143873bc06bf4e083f7cd8b17ee29 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:13:42 +0900 Subject: [PATCH 07/67] refactor(context): extract schema migration from open_db open_db now reads as the file lifecycle (directory, permissions, open, busy timeout, WAL mode) around one migrate(conn, was_missing) call. The legacy rename, pragmas, DDL, additive columns, retrieval marker and the three data migrations run in the same order as before. This is the seam the store/schema split needs. Co-Authored-By: Claude Opus 5.5 --- src/context/mod.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index 4c0c9ce5..39c83e8b 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -5186,12 +5186,18 @@ fn open_db(path: &Path) -> Result { } } set_context_db_permissions(path)?; + migrate(&conn, was_missing)?; + set_context_db_permissions(path)?; + Ok(conn) +} + +fn migrate(conn: &Connection, was_missing: bool) -> Result<()> { // Owner-scope rename (R10.6): before the `CREATE TABLE IF NOT EXISTS owner_*` // statements run, rename any legacy `choi_yeonwoo_*` tables so an upgrading // database keeps its style/context data instead of orphaning it under the // old names. `profile::migrate_owner_scope` later adds the `owner_key` // column and records the migration marker once the owner is confirmed. - rename_legacy_owner_tables(&conn)?; + rename_legacy_owner_tables(conn)?; conn.execute_batch("PRAGMA secure_delete = ON; PRAGMA journal_size_limit = 0; PRAGMA wal_autocheckpoint = 1000; @@ -5328,11 +5334,10 @@ fn open_db(path: &Path) -> Result { [RETRIEVAL_INDEX_SCHEMA_VERSION], )?; } - migrate_live_context_schema(&conn)?; - migrate_style_policy(&conn)?; - migrate_response_time_distribution_schema(&conn)?; - set_context_db_permissions(path)?; - Ok(conn) + migrate_live_context_schema(conn)?; + migrate_style_policy(conn)?; + migrate_response_time_distribution_schema(conn)?; + Ok(()) } fn migrate_style_policy(conn: &Connection) -> Result<()> { From c8a34c372dc7e59d951a54f0f3ae4329fcfec75c Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:14:22 +0900 Subject: [PATCH 08/67] fix: keep a running job when its duplicate token is refused run_browser_tool and fetch_settings_action began their job before the child registered its token. A duplicate token was refused by the registry, but the caller still finished the job, removing the entry of the call that was still running. Begin the job only after registration succeeds, so refused calls never touch the registry. Co-Authored-By: GPT-6 Astra via opencodex --- desktop/src-tauri/src/python_bridge.rs | 102 ++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 10 deletions(-) diff --git a/desktop/src-tauri/src/python_bridge.rs b/desktop/src-tauri/src/python_bridge.rs index 4638c346..780f912e 100644 --- a/desktop/src-tauri/src/python_bridge.rs +++ b/desktop/src-tauri/src/python_bridge.rs @@ -3,6 +3,7 @@ use crate::resource_layout::{ }; use serde::Serialize; use serde_json::{json, Value}; +use std::cell::Cell; use std::collections::{HashMap, HashSet}; use std::ffi::{OsStr, OsString}; use std::fs::{self, OpenOptions}; @@ -134,6 +135,10 @@ struct PythonRun<'a> { output_limit: usize, stdin_payload: Option<&'a [u8]>, global_abort_grace: Option, + /// Runs once the child is registered and about to spawn. A refused call + /// (duplicate token, bad resources) never reaches it, so it cannot begin + /// or finish a job that another call owns. + on_registered: Option<&'a dyn Fn()>, } impl Default for PythonRun<'_> { @@ -146,6 +151,7 @@ impl Default for PythonRun<'_> { output_limit: OUTPUT_LIMIT_BYTES, stdin_payload: None, global_abort_grace: None, + on_registered: None, } } } @@ -434,22 +440,26 @@ impl PythonBridge { let is_swap = action == "model-swap"; let is_mlx_launch = action == MLX_SERVER_LAUNCH_ACTION; let swap_job_id = token_id.unwrap_or("model-swap"); - if is_swap { + let begun = Cell::new(false); + let begin = || { self.begin_job(swap_job_id, "model_swap", "swap", 0.9); - } + begun.set(true); + }; let result: Result = (|| { - let bytes = self.run_python( - &args, - if is_swap { + let bytes = self.run_python_with_output_limit(PythonRun { + extra: &args, + timeout: if is_swap { MODEL_SWAP_TIMEOUT } else if is_mlx_launch { MLX_LAUNCH_TIMEOUT } else { DEFAULT_TIMEOUT }, - if is_swap { token_id } else { None }, - is_swap, - )?; + token_id: if is_swap { token_id } else { None }, + cooperative_cancel: is_swap, + on_registered: if is_swap { Some(&begin) } else { None }, + ..PythonRun::default() + })?; let value = parse_json_output(&bytes)?; Ok(match action { "models" => sanitize_models(&value), @@ -468,7 +478,7 @@ impl PythonBridge { _ => return Err(BridgeError::ActionNotAllowed), }) })(); - if is_swap { + if is_swap && begun.get() { self.finish_job(swap_job_id); } result @@ -486,7 +496,11 @@ impl PythonBridge { let args = browser_tool_args(job_id, task)?; let internal_token = format!("browser-tool-{job_id}"); let cancellation_token = token_id.unwrap_or(&internal_token); - self.begin_job(job_id, "browser", "running", 0.7); + let begun = Cell::new(false); + let begin = || { + self.begin_job(job_id, "browser", "running", 0.7); + begun.set(true); + }; let result: Result = (|| { let bytes = self.run_python_with_output_limit(PythonRun { extra: &args, @@ -495,11 +509,17 @@ impl PythonBridge { output_limit: BROWSER_TOOL_OUTPUT_LIMIT_BYTES, stdin_payload: Some(task.as_bytes()), global_abort_grace: Some(BROWSER_TOOL_ABORT_GRACE), + on_registered: Some(&begin), ..PythonRun::default() })?; let value = parse_json_output(&bytes)?; Ok(sanitize_browser_tool_result(&value)) })(); + if !begun.get() { + // Refused before the child started. The job entry, if any, belongs + // to the call that holds the token, so leave it alone. + return result; + } match &result { Ok(value) if !value.error_code.is_empty() => { self.set_job_error(job_id, Some(&value.error_code)); @@ -717,6 +737,7 @@ impl PythonBridge { output_limit, stdin_payload, global_abort_grace, + on_registered, } = run; let resources = self.config.resources()?; resources.validate().map_err(BridgeError::from)?; @@ -782,6 +803,9 @@ impl PythonBridge { }, ); } + if let Some(on_registered) = on_registered { + on_registered(); + } let mut args = vec![ "-E".to_string(), @@ -5258,6 +5282,64 @@ mod tests { assert!(gone, "lingering grandchild was not stopped"); } + fn register_token_in_use(bridge: &PythonBridge, token: &str) { + bridge.cancellations.lock().unwrap().insert( + token.to_string(), + CancellationHandle { + flag: Arc::new(AtomicBool::new(false)), + cooperative_marker: None, + global_abort_flag: None, + }, + ); + } + + fn job_error_code(bridge: &PythonBridge, job_id: &str) -> Option> { + bridge + .jobs_snapshot() + .into_iter() + .find(|event| event.job_id == job_id) + .map(|event| event.error_code) + } + + #[test] + fn rejected_duplicate_browser_token_keeps_the_running_job() { + // A duplicate token is refused before any child starts. The refusal must + // not finish or re-label the job that still owns the token. + let (bridge, root) = fixture_bridge("dup-browser", SLEEPING_CHILD); + register_token_in_use(&bridge, "browser-dup"); + bridge.begin_job("job-running", "browser", "running", 0.7); + + let result = bridge.run_browser_tool("job-running", "open page", Some("browser-dup")); + let still_listed = job_error_code(&bridge, "job-running"); + fs::remove_dir_all(root).unwrap(); + + assert!(matches!(result, Err(BridgeError::ActionNotAllowed))); + assert_eq!(still_listed, Some(None), "running browser job was removed or relabelled"); + } + + #[test] + fn rejected_duplicate_model_swap_token_keeps_the_running_job() { + let (bridge, root) = fixture_bridge("dup-swap", SLEEPING_CHILD); + let token = "123e4567-e89b-42d3-a456-426614174099"; + register_token_in_use(&bridge, token); + bridge.begin_job(token, "model_swap", "swap", 0.9); + + let result = bridge.fetch_settings_action( + "model-swap", + None, + None, + None, + Some(SWAP_MODEL_ID), + Some(true), + Some(token), + ); + let still_listed = job_error_code(&bridge, token); + fs::remove_dir_all(root).unwrap(); + + assert!(matches!(result, Err(BridgeError::ActionNotAllowed))); + assert_eq!(still_listed, Some(None), "running swap job was removed or relabelled"); + } + #[test] fn global_abort_reaches_untokened_child() { // mlx-server-launch, model-set, model-prepare, room-upsert and the From f093ce5cf29dad79e622b0c2406d4cb27da548ff Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:15:23 +0900 Subject: [PATCH 09/67] fix: reap detached voice session children start_voice_session spawned the interpreter and dropped the Child without waiting, so each session left a zombie once it exited. Hand every detached child to one background wait that collects its exit status. Co-Authored-By: GPT-6 Astra via opencodex --- desktop/src-tauri/src/python_bridge.rs | 35 +++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/desktop/src-tauri/src/python_bridge.rs b/desktop/src-tauri/src/python_bridge.rs index 780f912e..874af22b 100644 --- a/desktop/src-tauri/src/python_bridge.rs +++ b/desktop/src-tauri/src/python_bridge.rs @@ -707,7 +707,7 @@ impl PythonBridge { )?; voice_session_command(&plan, &self.config.state_root)? .spawn() - .map(|_| ()) + .map(reap_in_background) .map_err(|_| BridgeError::Spawn) }) } @@ -1779,6 +1779,15 @@ fn plan_voice_session( }) } +/// The voice session outlives the call that starts it, so the bridge does not +/// keep the handle. One background wait collects the exit status; dropping the +/// Child unwaited would leave a zombie once the session ends. +fn reap_in_background(mut child: Child) { + thread::spawn(move || { + let _ = child.wait(); + }); +} + fn voice_session_command( plan: &VoiceSessionPlan, state_root: &Path, @@ -5340,6 +5349,30 @@ mod tests { assert_eq!(still_listed, Some(None), "running swap job was removed or relabelled"); } + #[test] + fn voice_session_child_is_reaped_after_it_exits() { + // The voice child is detached on purpose, but dropping the Child without + // waiting leaves a zombie once it exits. The fixture interpreter records + // its PID under the state root and exits straight away. + let (mut bridge, root) = fixture_bridge("voice-reap", SLEEPING_CHILD); + let state = root.join("state"); + fs::create_dir_all(&state).unwrap(); + let voice_python = root.join("bin/voice-python"); + write_fixture_file( + &voice_python, + b"#!/bin/sh\nwhile [ \"$#\" -gt 0 ]; do\n if [ \"$1\" = --state-root ]; then echo $$ > \"$2/voice.pid\"; fi\n shift\ndone\nexit 0\n", + true, + ); + Arc::get_mut(&mut bridge.config).unwrap().voice_python = voice_python; + + bridge.start_voice_session().unwrap(); + let pid = wait_for_pid_file(&state.join("voice.pid")); + let gone = wait_until_gone(pid, Duration::from_secs(2)); + kill_own_child(pid); + fs::remove_dir_all(root).unwrap(); + assert!(gone, "exited voice session child was left as a zombie"); + } + #[test] fn global_abort_reaches_untokened_child() { // mlx-server-launch, model-set, model-prepare, room-upsert and the From 55076a88681d2f1e73fb2460e596ab2a6d2cef28 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:16:13 +0900 Subject: [PATCH 10/67] fix: give each cancel-marker write a unique temp name The marker temp name was derived from the pid alone, so two writers in one process raced on create_new and the second failed with a state error. Add a process-wide sequence number to the temp name. Co-Authored-By: GPT-6 Astra via opencodex --- desktop/src-tauri/src/python_bridge.rs | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/desktop/src-tauri/src/python_bridge.rs b/desktop/src-tauri/src/python_bridge.rs index 874af22b..f3bb0bd5 100644 --- a/desktop/src-tauri/src/python_bridge.rs +++ b/desktop/src-tauri/src/python_bridge.rs @@ -57,6 +57,7 @@ const MODEL_SWAP_OPT_IN: &str = "qwen38-27b-explicit-v1"; const MODEL_SWAP_CANCEL_DIR: &str = "model-swap-cancel"; const VOICE_TTS_OUT_NAME: &str = "jarvis-voice-out.wav"; static VOICE_SESSION_START_LOCK: Mutex<()> = Mutex::new(()); +static MARKER_TEMP_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); // Registry keys for children started without a caller token. The prefix is // refused for caller tokens, so the UI cannot cancel these entries by name. const INTERNAL_CANCEL_PREFIX: &str = "internal:"; @@ -2023,7 +2024,10 @@ fn write_model_swap_cancel_marker(path: &Path) -> Result<(), BridgeError> { .file_name() .and_then(|value| value.to_str()) .ok_or(BridgeError::StateIo)?; - let temp = parent.join(format!(".{file_name}.{}.tmp", std::process::id())); + // Same-process writers share the pid, so each write also takes a sequence + // number; create_new then never collides with a writer in flight. + let seq = MARKER_TEMP_SEQ.fetch_add(1, Ordering::Relaxed); + let temp = parent.join(format!(".{file_name}.{}.{seq}.tmp", std::process::id())); let mut file = OpenOptions::new() .write(true) .create_new(true) @@ -5349,6 +5353,31 @@ mod tests { assert_eq!(still_listed, Some(None), "running swap job was removed or relabelled"); } + #[test] + fn cancel_marker_writes_do_not_collide_on_temp_names() { + // Writers in one process share a pid. A writer that is already in flight + // holds the pid-based temp name, and create_new used to make the second + // writer fail. Each write needs its own temp name. + let temp = std::env::temp_dir() + .canonicalize() + .unwrap() + .join(format!("openkakao-marker-collide-{}", std::process::id())); + let _ = fs::remove_dir_all(&temp); + fs::create_dir_all(&temp).unwrap(); + let marker = temp.join("marker.json"); + let in_flight = temp.join(format!(".marker.json.{}.tmp", std::process::id())); + fs::write(&in_flight, b"other writer").unwrap(); + + let result = write_model_swap_cancel_marker(&marker); + let written = safe_small_json(&marker).and_then(|value| value.get("cancelled").cloned()); + let other_untouched = fs::read(&in_flight).unwrap(); + fs::remove_dir_all(&temp).unwrap(); + + assert!(result.is_ok(), "a second writer collided with an in-flight temp file"); + assert_eq!(written, Some(json!(true))); + assert_eq!(other_untouched, b"other writer"); + } + #[test] fn voice_session_child_is_reaped_after_it_exits() { // The voice child is detached on purpose, but dropping the Child without From 549e3c661faf6ddb1e22fec7ce90cc0d2fa89659 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:16:40 +0900 Subject: [PATCH 11/67] fix: truncate Korean previews on char boundaries Three diagnostic previews sliced strings by byte index and panicked when the cut landed inside a multi-byte character: the login response preview (200 bytes), the renew_token field dump (60 bytes) and the CHATINFO field dump (100 bytes). A 100-character Korean value was enough to crash login error reporting. All three now go through util::truncate, which counts characters. Co-Authored-By: Claude Opus 5.5 --- src/commands/probe.rs | 33 +++++++++++++++++++++++++-------- src/loco_helpers.rs | 36 ++++++++++++++++++++++++++---------- src/rest.rs | 25 ++++++++++++++++++------- 3 files changed, 69 insertions(+), 25 deletions(-) diff --git a/src/commands/probe.rs b/src/commands/probe.rs index 102a0e29..a012cdf6 100644 --- a/src/commands/probe.rs +++ b/src/commands/probe.rs @@ -7,7 +7,7 @@ use crate::loco; use crate::loco_helpers::{ loco_connect_with_auto_refresh, reconnect_loco_probe_client, should_retry_loco_probe_error, }; -use crate::util::{get_creds, print_section_title}; +use crate::util::{get_creds, print_section_title, truncate}; #[derive(Debug, Clone, Serialize)] pub struct MethodProbeResult { @@ -208,13 +208,8 @@ pub fn cmd_loco_chatinfo(chat_id: i64, json: bool) -> Result<()> { println!("{}", serde_json::to_string_pretty(&json_val)?); } else { print_section_title(&format!("Chat info: {}", chat_id)); - for (k, v) in response.body.iter() { - let v_str = format!("{:?}", v); - if v_str.len() > 100 { - println!(" {}: {}...", k, &v_str[..100]); - } else { - println!(" {}: {}", k, v_str); - } + for line in chat_info_lines(&response.body) { + println!("{line}"); } } @@ -222,6 +217,12 @@ pub fn cmd_loco_chatinfo(chat_id: i64, json: bool) -> Result<()> { }) } +fn chat_info_lines(body: &bson::Document) -> Vec { + body.iter() + .map(|(k, v)| format!(" {k}: {}", truncate(&format!("{v:?}"), 100))) + .collect() +} + pub async fn probe_method_variants( method: &str, variants: &[serde_json::Value], @@ -278,3 +279,19 @@ pub async fn probe_method_variants( Ok(results) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn chat_info_preview_keeps_korean_values_whole() { + let lines = chat_info_lines(&bson::doc! { "name": "가".repeat(100) }); + assert_eq!(lines.len(), 1); + let preview = lines[0] + .strip_prefix(" name: ") + .and_then(|rest| rest.strip_suffix("...")) + .expect("long value is shown as a truncated preview"); + assert_eq!(preview.chars().count(), 100); + } +} diff --git a/src/loco_helpers.rs b/src/loco_helpers.rs index 923dc14d..a178313f 100644 --- a/src/loco_helpers.rs +++ b/src/loco_helpers.rs @@ -5,7 +5,7 @@ use crate::error::OpenKakaoError; use crate::loco; use crate::model::KakaoCredentials; use crate::rest::KakaoRestClient; -use crate::util::{parse_loco_status_from_error, print_loco_error_hint}; +use crate::util::{parse_loco_status_from_error, print_loco_error_hint, truncate}; /// Connect LOCO client and login, auto-refreshing token on -950. pub async fn loco_connect_with_auto_refresh( @@ -51,15 +51,8 @@ pub fn try_renew_token(creds: &KakaoCredentials, refresh_token: &str) -> Result< .unwrap_or(-1); eprintln!("[renew] renew_token.json status: {}", status); - if let Some(obj) = response.as_object() { - for (k, v) in obj { - let v_str = format!("{}", v); - if v_str.len() > 60 { - eprintln!(" {}: {}...", k, &v_str[..60]); - } else { - eprintln!(" {}: {}", k, v); - } - } + for line in renew_response_lines(&response) { + eprintln!("{line}"); } if status != 0 { @@ -72,6 +65,15 @@ pub fn try_renew_token(creds: &KakaoCredentials, refresh_token: &str) -> Result< .map(String::from)) } +fn renew_response_lines(response: &serde_json::Value) -> Vec { + let Some(obj) = response.as_object() else { + return Vec::new(); + }; + obj.iter() + .map(|(k, v)| format!(" {k}: {}", truncate(&v.to_string(), 60))) + .collect() +} + /// Whether a LOCO probe error should be retried (transient socket failures). pub fn should_retry_loco_probe_error(error: &anyhow::Error) -> bool { if let Some(oke) = error.downcast_ref::() { @@ -111,3 +113,17 @@ pub fn check_loco_status(command: &str, response: &crate::loco::packet::LocoPack Err(OpenKakaoError::loco_with_body(command, status, response.body.clone()).into()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn renew_response_preview_keeps_korean_values_whole() { + let response = serde_json::json!({ "message": "가".repeat(100) }); + assert_eq!( + renew_response_lines(&response), + vec![format!(" message: \"{}...", "가".repeat(59))] + ); + } +} diff --git a/src/rest.rs b/src/rest.rs index d1505764..c8d9daab 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -13,6 +13,7 @@ use crate::error::OpenKakaoError; use crate::model::{ json_i64, json_string, ChatMember, ChatMessage, ChatRoom, Friend, KakaoCredentials, MyProfile, }; +use crate::util::truncate; const BASE_URL: &str = "https://katalk.kakao.com"; const PILSNER_URL: &str = "https://talk-pilsner.kakao.com"; @@ -375,13 +376,7 @@ impl KakaoRestClient { .with_context(|| format!("{endpoint} request failed"))?; let text = response.text().context("Failed to read response")?; - serde_json::from_str(&text).with_context(|| { - format!( - "Failed to parse {} response: {}", - endpoint, - &text[..200.min(text.len())] - ) - }) + parse_account_response(endpoint, &text) } // NOTE: a passcode/register_device flow (request_passcode.json, @@ -532,10 +527,26 @@ impl KakaoRestClient { } } +fn parse_account_response(endpoint: &str, text: &str) -> Result { + serde_json::from_str(text).with_context(|| { + format!( + "Failed to parse {endpoint} response: {}", + truncate(text, 200) + ) + }) +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn unparseable_korean_account_response_is_an_error_not_a_panic() { + let body = "가".repeat(100); + let error = parse_account_response("login.json", &body).unwrap_err(); + assert!(error.to_string().contains(&body)); + } + #[test] fn xvc_is_deterministic_sha512_prefix() { // X-VC = SHA512("YLLAS|{login_id}|{uuid}|GRAEB|{user_agent}")[..16] From bf48fb32cc12176b69121a178419e0ce1ceb6e59 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:16:50 +0900 Subject: [PATCH 12/67] docs(auto-reply): drop dated provenance tags from comments Remove the parenthesised (YYYY-MM-DD ...) review dates that narrate history git already records, keeping each sentence's why. No code or string literal changed. Co-Authored-By: GPT-6.1 Sol via opencodex --- scripts/auto_reply_finetune.py | 19 +++--- scripts/auto_reply_golden_dataset.py | 22 +++---- scripts/auto_reply_knowledge_graph.py | 92 +++++++++++---------------- scripts/auto_reply_reference_store.py | 8 +-- 4 files changed, 60 insertions(+), 81 deletions(-) diff --git a/scripts/auto_reply_finetune.py b/scripts/auto_reply_finetune.py index 4b07c91d..05f95372 100644 --- a/scripts/auto_reply_finetune.py +++ b/scripts/auto_reply_finetune.py @@ -10,7 +10,6 @@ Nothing here downloads weights or starts training on its own; every entry point takes explicit paths and the caller decides when to spend the compute -(2026-09-17). """ from __future__ import annotations @@ -43,7 +42,6 @@ # The golden extractor owns the transcript clock format, so its parser is # reused instead of re-implemented: a session bucket that disagrees with # the row's own timestamp would put one burst on both sides of the split - # (2026-09-19). from scripts.auto_reply_golden_dataset import parse_timestamp except ImportError: # `scripts` is not importable from inside its own directory from auto_reply_golden_dataset import parse_timestamp @@ -54,7 +52,7 @@ DEFAULT_SEED = 42 # Golden rows are cut from the same room bursts, so neighbours share most of # their window text. Two rows further apart than this gap come from separate -# conversations and may land on opposite sides of the split (2026-09-19). +# conversations and may land on opposite sides of the split. DEFAULT_SESSION_GAP = 1800.0 # mlx_lm.lora masks the prompt when told to, so the model is scored on the # answer only. The instruction line frames the task the same way every time. @@ -122,7 +120,7 @@ def _to_mlx_row(record: dict[str, Any], instruction: str) -> dict[str, str]: mlx_lm reads a prompt/completion pair for completions-style data. The room window is folded into the prompt so the model sees the same context the - live worker would give it (2026-09-17). + live worker would give it. """ prompt = str(record.get("prompt") or "").strip() window = record.get("window") @@ -156,7 +154,7 @@ def _session_key(record: dict[str, Any], session_gap: float) -> tuple[str, str] A row without a room cannot be held together with anything else, so it is its own group. A room row whose clock cannot be read joins that room's no_clock group: an unreadable timestamp must not become a licence to split - a burst apart (2026-09-19). + a burst apart. """ room = str(record.get("room") or "").strip() if not room: @@ -202,7 +200,7 @@ def build_splits( row-by-row shuffle puts one conversation on both sides of the split and the held-out loss stops measuring anything. The rows are therefore de-duplicated on the whitespace-folded prompt and then grouped by room and - session, and a whole group is dealt to exactly one split (2026-09-19). + session, and a whole group is dealt to exactly one split. """ stats = SplitStats() if not pairs: @@ -259,7 +257,6 @@ def build_splits( # One burst can swallow the whole test and valid budget and leave # nothing to train on. Demote the smallest group that is not already # training, which is how a single-group dataset ends up in train - # (2026-09-19). smallest = min(range(len(entries)), key=lambda i: (len(entries[i][0]), i)) entries[smallest] = (entries[smallest][0], "train") @@ -311,7 +308,7 @@ def plan_training( The iteration count is derived from the dataset when the caller does not pin it: two epochs over the training split is enough to move a small set - without overfitting it into memorised replies (2026-09-17). + without overfitting it into memorised replies. """ effective_iters = iters if effective_iters <= 0: @@ -410,7 +407,7 @@ def _parse_test_loss(output: str) -> float | None: continue # The line reads "Test loss 3.412, Test ppl 30.3". Take the # number directly after the word loss, not the last number on - # the line, which is the perplexity (2026-09-17). + # the line, which is the perplexity. parts = stripped.replace(",", " ").split() for index, token in enumerate(parts): if token.lower() == "loss" and index + 1 < len(parts): @@ -448,7 +445,7 @@ def evaluate_adapter( The same call is used before and after training, so the two numbers are comparable. A missing mlx_lm is reported rather than raised, because the - evaluation is diagnostic and must not kill a scheduled run (2026-09-17). + evaluation is diagnostic and must not kill a scheduled run. """ executable = _resolve_lora_executable() if not executable: @@ -725,7 +722,7 @@ def evaluate_preference_pairs( # --------------------------------------------------------------------------- -# DPO 응답 토큰 로그확률 수집 (2026-09-22) +# DPO 응답 토큰 로그확률 수집 # # 로컬 MLX 게이트웨이는 요청에 logprobs=true를 주면 모델이 직접 생성한 토큰의 # 로그확률을 돌려준다. 다만 echo 모드를 구현하지 않고 assistant prefill도 diff --git a/scripts/auto_reply_golden_dataset.py b/scripts/auto_reply_golden_dataset.py index 2e9faf08..787ee2ba 100644 --- a/scripts/auto_reply_golden_dataset.py +++ b/scripts/auto_reply_golden_dataset.py @@ -7,7 +7,7 @@ on. A sent row proves delivery, not quality, so it only becomes gold when the approval ledger (golden/quality-approvals.jsonl) accepts it or the row itself carries a quality flag. Without a verdict the row is dropped, or kept and - labelled model_generated under --include-unreviewed-evidence (2026-09-19). + labelled model_generated under --include-unreviewed-evidence. 2. context.sqlite3 holds the room transcript. The self author's own messages are the human gold answers: they are what the operator really typed, with no model in the loop. @@ -16,7 +16,7 @@ event_id, reviewer), which room it belongs to, the conversation window that precedes it, and a deterministic content hash used for de-duplication. Nothing here touches the network, and the context database is opened read-only so a -running worker is never blocked (2026-09-17). +running worker is never blocked. """ from __future__ import annotations @@ -56,7 +56,7 @@ _REPEAT_PATTERN = re.compile(r"(.)\1{4,}") # The room importer rewrites some payloads into a fixed summary block. Those # blocks are machine prose, not the operator's own phrasing, so they must never -# be learned as a gold answer (2026-09-17). +# be learned as a gold answer. _SUMMARY_BLOCK_PATTERN = re.compile(r"^\[설명자료\]|무엇을:|어떻게:|핵심:주장:") # Automated feeds post under the operator's nickname in a few rooms. _BOT_PREFIXES = ("GeekNews TOP5", "GeekNews TOP", "[GeekNews]") @@ -64,13 +64,13 @@ DEFAULT_SELF_AUTHORS: tuple[str, ...] = ("최연우",) # KakaoTalk users split one thought across several sends. Merging consecutive # self messages inside this gap produces the answer the operator actually meant -# instead of a nine-character fragment (2026-09-17). +# instead of a nine-character fragment. DEFAULT_MERGE_GAP_SECONDS = 180 # Where the golden set records that a person looked at a model-written reply and # accepted it. Delivery success is not a language judgement - a reply can be # sent and still be wrong - so a sent row only becomes gold once something -# explicit says so (2026-09-19). +# explicit says so. QUALITY_HUMAN_AUTHORED = "human_authored" QUALITY_APPROVED = "approved" QUALITY_UNREVIEWED = "unreviewed" @@ -196,7 +196,7 @@ class QualityApprovals: The ledger is append-only JSONL and the last verdict for a key wins, so a row can be approved, rejected, and approved again without rewriting what a - reviewer already said (2026-09-19). + reviewer already said. """ by_log_id: dict[str, dict[str, str]] = field(default_factory=dict) @@ -501,7 +501,6 @@ def iter_self_pairs( # Every participant is read, not only the self author: the prompt window is # made of what other people said right before the gold answer. Filtering the # self author in SQL left the window empty and produced no pairs at all - # (2026-09-17). query = ( "SELECT id, chat, date, user_name, message FROM context_messages" f"{room_clause} ORDER BY chat, id" @@ -538,7 +537,7 @@ def _flush() -> GoldenPair | None: ) # Positional access keeps this working for a plain tuple connection as well - # as the Row-factory connection the CLI opens (2026-09-17). + # as the Row-factory connection the CLI opens. for row in connection.execute(query, params): row_id, row_chat, row_date, row_user, row_message = row[:5] room = str(row_chat or "") @@ -629,7 +628,7 @@ def resolve_inbound_messages( The evidence ledger stores the outgoing reply but not always the message that triggered it. context_live_events maps the KakaoTalk log id onto the - transcript row, which is where the inbound text still lives (2026-09-17). + transcript row, which is where the inbound text still lives. """ lookup: dict[int, dict[str, str]] = {} unique = sorted({int(x) for x in log_ids if int(x) > 0}) @@ -682,7 +681,7 @@ def iter_evidence_pairs( rows carrying an approval verdict (or an inline quality flag) become gold. require_approval=False keeps the rest and labels them model_generated so an exploratory set is still possible, but a rejected row is always dropped and - the two cases are never mixed silently (2026-09-19). + the two cases are never mixed silently. """ if not evidence_path.exists(): return @@ -943,7 +942,6 @@ def _db_has_transcript(path: Path) -> bool: The state root keeps a small context.sqlite3 that only holds operator prompts, while the real transcript lives in the openkakao application support root. The name alone is not enough, so the table is probed - (2026-09-17). """ if not path.is_file(): return False @@ -1046,7 +1044,7 @@ def _print_candidates( Delivery success is the only thing the ledger proves, so the review list is the whole set of sent rows that carry no verdict. Each line is JSON with the - log_id the approve/reject flags take (2026-09-19). + log_id the approve/reject flags take. """ inbound_lookup: dict[int, dict[str, str]] = {} paths = _evidence_paths(state_root) diff --git a/scripts/auto_reply_knowledge_graph.py b/scripts/auto_reply_knowledge_graph.py index 37734e1a..b93ca955 100644 --- a/scripts/auto_reply_knowledge_graph.py +++ b/scripts/auto_reply_knowledge_graph.py @@ -75,7 +75,7 @@ # 71.139ms였다. 모양별 행렬을 한 번 만들면(콜드 63.41ms, 웜 2.4us) 같은 # 질의가 6.032ms이고, 50개 벡터는 3,560.9ms에서 320.9ms가 된다. 부호가 # 그대로라 버킷 키도 그대로이고, 이미 만들어진 ann_buckets 테이블과도 -# 호환된다(2026-09-22). +# 호환된다. _ANN_SIGN_MATRIX_CACHE: dict[tuple[int, int], tuple[bytes, ...]] = {} _ANN_SIGN_MATRIX_CACHE_MAX_SHAPES = 8 KST = ZoneInfo("Asia/Seoul") @@ -84,7 +84,7 @@ # backs yet; "ledger" means a real room message was found to mention the node. # The 6 Pro review rejected nodes that carried no source message id, room id, # confirmation time or retraction state, so every node and edge now reports -# which of the two it is and what backs it (2026-09-16). +# which of the two it is and what backs it. PROVENANCE_SEED = "seed" PROVENANCE_LEDGER = "ledger" MAX_EVIDENCE_PER_NODE = 8 @@ -165,7 +165,6 @@ def stable_node_id(entity_id: str) -> int: fresh interpreter on every refresh. Python's ``hash()`` is salted per process, so ids changed on every poll and a selected row was lost between reads. CRC32 of the entity id is stable across processes and machines - (2026-09-16). """ return zlib.crc32(entity_id.encode("utf-8")) % 10000000 @@ -337,7 +336,7 @@ def _connect_kg(db_path: Path) -> sqlite3.Connection: ) """) # Existing graphs predate the evidence columns; add them in place so an - # installed graph keeps its rows instead of being rebuilt (2026-09-16). + # installed graph keeps its rows instead of being rebuilt. for table in ("kg_entities", "kg_relations"): try: columns = {row[1] for row in conn.execute(f"PRAGMA table_info({table})")} @@ -584,7 +583,7 @@ def _room_ledgers(state_root: Path) -> list[Path]: # The graph used to be five hand-written nodes, so it could not answer anything # the author had not thought of in advance: the room discussed Alibaba Cloud # for an hour and the graph still had nothing to say about it. These rows turn -# the index into neurons and synapses (2026-09-16). +# the index into neurons and synapses. INDEX_TOPIC_LABELS = { "coins": "코인", "stocks": "주식", @@ -613,7 +612,7 @@ def _room_ledgers(state_root: Path) -> list[Path]: # 방에서 누구와 나눈 이야기인지는 몰랐다. 답변에 필요한 것은 주제만이 # 아니라 그 주제가 누구의 어떤 맥락이었는가다. 방과 사람을 따로 세우고 # 주제와 이어 두면, 같은 "주식"이라도 성린이형 방의 주식과 부자멘토멘티의 -# 주식이 다른 자리로 남는다 (2026-09-16, 사용자 지시). +# 주식이 다른 자리로 남는다. INDEX_CHAT_MIN_MESSAGES = 40 INDEX_PERSON_MIN_MESSAGES = 25 # 한 방에서 이 사람의 메시지가 이 비율을 넘으면 그 방의 중심 인물로 본다. @@ -629,7 +628,7 @@ def _room_ledgers(state_root: Path) -> list[Path]: # A neuron's facts should quote the people in the room, not the bots that post # into it. Without this every 인프라 fact was a GeekNews digest line and the -# node said nothing about what the members actually discussed (2026-09-16). +# node said nothing about what the members actually discussed. INDEX_SAMPLE_SKIP_AUTHORS = ( "드리고", "드리고봇", @@ -726,7 +725,7 @@ def _copy_consistent_sqlite_replica(db_path: Path, tmpdir: Path) -> Path: @contextmanager def _open_isolated_ro_conn(db_path: Path): """DB Lock 방지: 카카오톡 DB 동기화 시 실행 중 파일 잠금을 방지하기 위해 - 임시 디렉터리로 복제한 뒤 Read Only 모드로 안전하게 연결한다 (2026-09-17). + 임시 디렉터리로 복제한 뒤 Read Only 모드로 안전하게 연결한다. """ if not db_path.exists(): raise FileNotFoundError(f"Database not found: {db_path}") @@ -952,7 +951,7 @@ def _index_topic_samples( # B-tree over every message the topic ever had, which cost 6.7s for 13 # topics. The topic index is keyed (topic, message_id), so walking it # backwards yields the newest ids directly and any sorting happens in - # Python on at most `want` rows (2026-09-16). + # Python on at most `want` rows. want = max(int(limit), 1) try: row = conn.execute( @@ -1011,7 +1010,7 @@ def _synapse_weight(count: int) -> int: ``30 + count`` pushed every busy pair to the ceiling: 코인-주식 and 연락-주식 both drew at 99, so the picture showed no difference between the - strongest and the weakest link. A log curve keeps them apart (2026-09-16). + strongest and the weakest link. A log curve keeps them apart. """ count = max(int(count), 1) return max(30, min(99, 30 + int(round(20 * math.log10(count + 1))))) @@ -1046,7 +1045,7 @@ def _room_titles(state_root: Path) -> dict[str, str]: 색인에는 방이 ``그룹:325472527151234``처럼 번호로만 남는 경우가 많다. 메뉴바가 이미 방 목록을 카탈로그에 적어 두므로, 그래프도 같은 이름을 써야 표와 그림이 같은 방을 다른 이름으로 부르지 않는다. 읽지 못하면 - 빈 표를 돌려주고 번호 이름을 그대로 쓴다 (2026-09-16). + 빈 표를 돌려주고 번호 이름을 그대로 쓴다. """ path = state_root / "menubar-room-catalog.json" try: @@ -1076,7 +1075,7 @@ def _room_key(state_root: Path, chat: str) -> str: ``그룹:325472527151234``로 적고, 어떤 경로는 사람이 읽는 이름을 안다. 그래서 같은 방에 뉴런이 둘 서고, 방 안의 사람도 두 벌로 갈렸다. 카탈로그가 번호와 이름을 모두 알고 있으므로 번호를 이름으로 바꿔 - 하나로 모은다. 카탈로그에 없으면 원래 키를 그대로 쓴다 (2026-09-16). + 하나로 모은다. 카탈로그에 없으면 원래 키를 그대로 쓴다. """ raw = (chat or "").strip() if not raw: @@ -1092,7 +1091,7 @@ def _merge_seed_rooms(conn: sqlite3.Connection) -> int: ``chat:부자멘토멘티``로 만든다. 둘 다 남으면 같은 방이 그림에 두 번 서고, 손으로 적은 쪽의 시냅스(알쫀쿠 추천 등)는 색인 쪽과 이어지지 않아 그래프가 끊긴다. 색인 뉴런으로 옮겨 붙이고 옛 뉴런은 지운다. - 옮길 곳이 없으면 그대로 둔다 (2026-09-16). + 옮길 곳이 없으면 그대로 둔다. """ moved = 0 for entity_id, name in list( @@ -1135,7 +1134,7 @@ def index_chat_entities( 방 뉴런이 있어야 "어디서 나눈 이야기인가"가 그래프에 남는다. 주제 뉴런만 있으면 같은 주제를 여러 방에서 나눠도 한 덩어리로 뭉쳐, 답변이 - 엉뚱한 방의 맥락을 끌어온다 (2026-09-16). + 엉뚱한 방의 맥락을 끌어온다. """ stats = {"chats": 0, "written": 0} index_path = _index_db_path(state_root) @@ -1160,7 +1159,7 @@ def index_chat_entities( finally: # with 블록이 실패하면 index_conn 이 할당되지 않아, 여기서 바로 # 닫으면 UnboundLocalError 가 원래 예외를 덮어쓴다. 자식 프로세스가 - # exit 1 로만 죽어 원인을 알 수 없던 이유다 (2026-09-17). + # exit 1 로만 죽어 원인을 알 수 없던 이유다. if index_conn is not None: try: index_conn.close() @@ -1168,7 +1167,7 @@ def index_chat_entities( pass now = int(time.time()) # 같은 방이 두 키로 들어오면 여기서 하나로 합친다. 합치지 않으면 - # 방 뉴런이 둘 서고 방 안의 사람도 두 벌로 갈린다 (2026-09-16). + # 방 뉴런이 둘 서고 방 안의 사람도 두 벌로 갈린다. merged: dict[str, list[Any]] = {} for room, count, first, last in rows: key = _room_key(state_root, str(room)) @@ -1228,7 +1227,6 @@ def index_person_entities( 사람 뉴런은 방 뉴런에 매인다. 같은 이름이 여러 방에 있어도 방마다 다른 뉴런이 되어, "성린이형 방의 성린이"와 다른 방의 동명이인이 섞이지 않는다 - (2026-09-16, 사용자 지시). """ stats = {"persons": 0, "written": 0, "with_lines": 0} index_path = _index_db_path(state_root) @@ -1263,7 +1261,6 @@ def index_person_entities( totals[str(room)] = int(count or 0) # 방 키를 하나로 모은 뒤에 사람을 센다. 같은 방이 두 키로 들어오면 # 한 사람이 두 뉴런으로 갈리고, 방마다 세는 상한도 두 번 적용된다 - # (2026-09-16). merged: dict[tuple[str, str], int] = {} raw_keys: dict[str, list[str]] = {} for room, user, count in rows: @@ -1291,7 +1288,7 @@ def index_person_entities( # 방 이름과 같다. 그래서 "커리어톡 (커리어톡)" 같은 뉴런이 방 # 뉴런 바로 옆에 하나 더 서서, 같은 것을 가리키는 두 이름이 그림에 # 겹쳐 보였다. 방 이름과 같은 화자는 방 뉴런이 이미 말하고 있으므로 - # 사람 뉴런으로 세우지 않는다 (2026-09-16, 6 Pro 지적). + # 사람 뉴런으로 세우지 않는다. room_name = _chat_label(room_key) if name.casefold() == room_name.casefold(): continue @@ -1360,7 +1357,7 @@ def _index_person_lines( """이 사람이 실제로 남긴 최근 대화 몇 줄. 요약이 아니라 발언 원문이다. 답변이 그 사람의 말투와 관심사를 그대로 - 참고할 수 있어야 한다 (2026-09-16). + 참고할 수 있어야 한다. """ keys = [chats] if isinstance(chats, str) else list(chats) if not keys: @@ -1472,7 +1469,7 @@ def index_topic_relations( Two topics mentioned in the same message are related, and the number of such messages is the weight. That is what makes the picture a network - rather than a list of separate circles (2026-09-16). + rather than a list of separate circles. """ stats = {"pairs": 0, "written": 0} index_path = _index_db_path(state_root) @@ -1538,7 +1535,7 @@ def index_membership_relations( - 사람 → 주제 (TALKS_ABOUT): 이 사람이 이 주제를 자주 꺼낸다 세 번째가 답변에 가장 쓸모 있다. 같은 "주식"이라도 누가 꺼낸 - 이야기인지에 따라 답이 달라지기 때문이다 (2026-09-16, 사용자 지시). + 이야기인지에 따라 답이 달라지기 때문이다. """ stats = {"person_chat": 0, "chat_topic": 0, "person_topic": 0} index_path = _index_db_path(state_root) @@ -1669,17 +1666,16 @@ def prune_indexed_entities( 색인에서 빠진 대화가 그래프에 그대로 남았다. 그래서 같은 방을 가리키는 두 뉴런("커리어톡"과 "커리어톡 (커리어톡)")이 나란히 서고, 지워진 주제도 계속 그려졌다. 색인으로 만든 뉴런 중 이번 주기에 다시 쓰이지 않은 것을 - 지우고, 그 뉴런에 매달린 시냅스도 함께 지운다 (2026-09-16, 사용자 지시). + 지우고, 그 뉴런에 매달린 시냅스도 함께 지운다. 손으로 적은 기본 뉴런(ent:*)과 원장에서 온 근거는 건드리지 않는다. 기준 시각은 이번 주기가 시작된 때다. "일주일보다 오래된 것"을 지우면 이번에 방 이름이 바뀌어 다시 쓰이지 않은 옛 뉴런이 그대로 남는다 - (2026-09-16). 색인을 읽지 못한 채 이 함수가 돌면 그래프가 통째로 비어 버린다. 지우는 수가 절반을 넘으면 색인 쪽이 고장 난 것으로 보고 아무것도 - 지우지 않는다 (2026-09-16). + 지우지 않는다. """ cutoff = int(cycle_started_at) stale = [ @@ -1840,7 +1836,7 @@ def wait_for_background_reindex(timeout: float = 30.0) -> bool: The menu bar must never wait, but a caller that needs a settled graph (a test, or a one-shot export) can. Returns True when nothing is left - running (2026-09-17). + running. """ deadline = time.time() + max(0.0, timeout) while time.time() < deadline: @@ -1865,7 +1861,6 @@ def _start_background_reindex( Returns a small status dict the caller can report. A second call while one is already running returns {"started": False, "reason": "in_flight"} so the caller keeps serving the stored graph instead of stacking work - (2026-09-17, 6 Pro 지적). """ global _reindex_inflight, _reindex_last_started_at started_at = int(cycle_started_at or time.time()) @@ -1873,7 +1868,7 @@ def _start_background_reindex( # 프로세스 모드에서는 자식이 잠금 파일을 들고 있다. 부모의 메모리 # 플래그는 쓰지 않는다. 부모는 자식이 언제 끝나는지 볼 수 없으므로, # 플래그를 잡으면 부모가 사는 동안 영원히 in_flight 로 남아 재색인이 - # 다시는 걸리지 않는다 (2026-09-17). + # 다시는 걸리지 않는다. if _reindex_process_running(state_root): return {"started": False, "reason": "in_flight"} if _spawn_reindex_process(state_root, chat=chat, cycle_started_at=started_at): @@ -1915,8 +1910,8 @@ def _run() -> None: # 메뉴바는 이 모듈을 짧게 사는 프로세스로 부른다. 데몬 스레드는 그 # 프로세스가 끝나면 함께 죽어 재색인이 반쯤 돌다 만 채로 남는다. 그래서 - # 기본값(mode="process")은 스스로 살아남는 자식 프로세스다 (2026-09-17, - # 사용자 지시). 테스트는 "thread", 일회성 내보내기는 "inline" 으로 돈다. + # 기본값(mode="process")은 스스로 살아남는 자식 프로세스다. + # 테스트는 "thread", 일회성 내보내기는 "inline" 으로 돈다. thread = threading.Thread(target=_run, name="kg-reindex", daemon=True) thread.start() return {"started": True, "chat": chat, "cycle_started_at": started_at, "mode": mode} @@ -1926,7 +1921,7 @@ def _reindex_process_running(state_root: Path) -> bool: """True when a detached child already holds the reindex lock. The lock file is the only thing the parent and the child share, so it is - also the only honest answer to whether a walk is still going (2026-09-17). + also the only honest answer to whether a walk is still going. """ lock_path = state_root / "knowledge-graph-reindex.lock" try: @@ -1956,7 +1951,6 @@ def _spawn_reindex_process( Returns True when a child was started. The child holds the same lock file the in-process guard uses, so a burst of menu polls still starts one walk - (2026-09-17). """ lock_path = state_root / "knowledge-graph-reindex.lock" try: @@ -2033,7 +2027,6 @@ def _reindex_once_entry(argv: list[str]) -> int: except Exception as error: # 자식은 어떤 경우에도 조용히 끝난다. 메뉴가 읽는 것은 상태 파일이지 # 이 프로세스의 종료 코드가 아니다. 다만 왜 죽었는지는 남긴다 - # (2026-09-17). write_meta(conn, "last_index_error", f"detached: {type(error).__name__}: {error}"[:400]) return 1 finally: @@ -2056,12 +2049,10 @@ def _reindex_all( This is the body that used to run inline in collect_knowledge_graph. Each step is isolated: a failure in one indexer must not stop the others, because a graph with topics but no rooms is still better than no refresh - (2026-09-16, 2026-09-17). A step that dies is recorded in kg_meta instead of being swallowed. The menu showed a freshly indexed, empty graph while every indexer had been failing on a NameError that only the real database path reached - (2026-09-17, 사용자 지시). """ failures: list[str] = [] @@ -2088,7 +2079,6 @@ def _note(step: str, error: BaseException) -> None: if failures: # 실패를 남기고 "색인했다"는 도장은 찍지 않는다. 찍으면 다음 폴링이 # 같은 일을 다시 하지 않아 그래프가 영영 낡은 채로 남는다 - # (2026-09-17). write_meta(conn, "last_index_error", " | ".join(failures)) if any("isolated read-only snapshot unavailable" in failure for failure in failures): # 카카오 원본 DB로 폴백하지 않았음을 상태 화면에서도 확인할 수 @@ -2098,7 +2088,7 @@ def _note(step: str, error: BaseException) -> None: write_meta(conn, "last_index_error", "") write_meta(conn, "last_snapshot_status", "copy_ok") # 이번 색인이 언제 끝났는지 남긴다. 이 값이 없으면 다음 폴링이 방금 - # 끝난 색인을 모르고 같은 일을 다시 시작한다 (2026-09-17). + # 끝난 색인을 모르고 같은 일을 다시 시작한다. # Dense는 optional이므로 이 도장은 dense endpoint 상태와 독립적이다. write_meta(conn, "last_indexed_at", str(int(time.time()))) @@ -2139,12 +2129,11 @@ def collect_knowledge_graph( A refresh normally runs in the background so the menu bar never blocks on the 1.4GB context DB. Set ``wait_for_reindex`` when the caller needs a settled graph before reading rows: a one-shot export, or a test asserting on - freshly indexed evidence (2026-09-17, 6 Pro 지적). + freshly indexed evidence. reindex_mode picks who walks the index when wait_for_reindex is false: "process" detaches a child that outlives the short-lived menu call, "thread" keeps the walk in this process so a test can wait for it - (2026-09-17). """ kg_path = db_path.parent / KNOWLEDGE_GRAPH_DB_NAME try: @@ -2152,7 +2141,7 @@ def collect_knowledge_graph( conn = _connect_kg(kg_path) except (OSError, sqlite3.Error) as error: # A window that cannot reach its store must draw an empty graph and say - # why, not take the menu down with it (2026-09-16). + # why, not take the menu down with it. return { "ok": False, "nodes": [], @@ -2169,13 +2158,13 @@ def collect_knowledge_graph( # 이미 색인된 데이터가 있고 최근(기본 5분)에 갱신되었다면 매번 # 1.4GB DB 전체를 다시 훑지 않고 저장된 그래프를 즉시 반환한다. # 메뉴바 폴링(2초 주기)과 감사에서 프로세스가 25초 타임아웃에 - # 걸려 빈 그래프로 떨어지는 병목을 해결한다 (2026-09-17). + # 걸려 빈 그래프로 떨어지는 병목을 해결한다. now = int(time.time()) # 색인 시각은 메타에 남는다. 예전에는 뉴런의 updated_at 최댓값을 # 썼는데, 손으로 적은 시드 뉴런만 있는 그래프에서는 그 값이 방금 # 갱신된 것으로 보여 재색인이 영원히 걸리지 않았다. 반대로 색인할 # 대화가 없으면 개수가 0이라 매 폴링마다 재색인이 걸렸다. 이제 - # "언제 색인을 끝냈는가"를 직접 기록해 둘 다 피한다 (2026-09-17). + # "언제 색인을 끝냈는가"를 직접 기록해 둘 다 피한다. try: indexed_count = int( conn.execute( @@ -2194,7 +2183,6 @@ def collect_knowledge_graph( # 메타가 없으면(=예전 그래프) 한 번은 다시 색인한다. 행의 updated_at # 으로 대신하면, 방금 심은 시드 뉴런의 시각이 "최신"으로 읽혀 색인이 # 영영 돌지 않는다. 그게 6 Pro가 지적한 빈 그래프의 원인이었다 - # (2026-09-17). needs_reindex = force_reindex or ( last_updated == 0 or now - last_updated >= reindex_interval_seconds ) @@ -2211,7 +2199,6 @@ def collect_knowledge_graph( # 동기로 돌려, 캐시가 만료된 첫 조회가 25초 타임아웃에 걸려 빈 # 그래프로 떨어졌다. 지금은 저장된 그래프를 즉시 돌려주고, # 재색인은 백그라운드에서 돌린 뒤 다음 폴링이 새 그림을 읽는다 - # (2026-09-17, 6 Pro 지적). if wait_for_reindex: # 동기 경로: 재색인이 끝난 뒤의 그래프를 이 응답에 담는다. _reindex_all(conn, state_root, chat=chat, cycle_started_at=now) @@ -2413,7 +2400,7 @@ def collect_knowledge_graph_list( for rel in connected: body_parts.append(f" ↔ {rel}") # Say where the node came from, so a reader can check it instead of - # trusting the description (2026-09-16). + # trusting the description. if evidence["kind"] == PROVENANCE_LEDGER: body_parts.append( "근거: 원문 메시지 " @@ -2465,7 +2452,7 @@ def collect_knowledge_graph_list( # 목록 봉투는 다른 보기(messages·style·topics…)와 같은 모양이어야 한다. # 창은 ok·rows·count·truncated를 읽는다. 예전에는 items·has_more로 # 돌려주어 디코딩이 통째로 실패했고, 지식 그래프를 고르면 69개 뉴런이 - # 있는데도 "기록 없음"만 떴다 (2026-09-16). + # 있는데도 "기록 없음"만 떴다. return { "ok": True, "action": "vector-list", @@ -2501,7 +2488,6 @@ def collect_knowledge_graph_list( "가상화폐": ["코인", "비트코인"], # 별칭이 영어 정식 이름이고 질문은 줄임말일 때도 만나야 한다. 사전이 # 한글 키만 있으면 "Anthropic Claude" 같은 별칭은 "클로드"를 못 찾았다 - # (2026-09-19). "claude": ["클로드", "앤트로픽 클로드", "anthropic claude"], "chatgpt": ["지피티", "챗지피티", "gpt", "openai"], "qwen": ["큐웬", "큐웬3.8", "qwen3.8"], @@ -2531,7 +2517,6 @@ def _build_synonym_index( (낱말, 바늘더미) 한 쌍당 실측 4.34us가 그 비용이었고, 조회표를 쓰면 1.10us다. 접힌 항목은 원래 키를 그대로 들고 있어서 문맥 게이트 (`CONTEXT_GATED_SYNONYM_KEYS`)는 예전과 같은 원문 키로 비교한다 - (2026-09-22). """ index: dict[str, list[tuple[str, str, tuple[str, ...]]]] = {} @@ -2619,7 +2604,7 @@ def normalize_text_query(text: str, *, message_timestamp: Any = None) -> str: 답변 생성은 이 함수를 거친 질의로 그래프를 찾는다. 사용자가 "러닝박에"나 "오늘 아침"처럼 써도 그래프의 "러닝"·"아침(8시)" 노드와 같은 말로 만나게 - 하는 것이 목적이다. 여러 번 불러도 같은 결과가 나온다(2026-09-19). + 하는 것이 목적이다. 여러 번 불러도 같은 결과가 나온다. 예전 본문의 시간 규칙에는 단어 경계를 뜻하는 역슬래시-b 대신 진짜 백스페이스 문자가 들어가 있었다. 그 규칙은 아무것도 바꾸지 못했고, @@ -2648,7 +2633,7 @@ def query_haystacks( """질의와 곁말을 원문과 표준형으로 함께 모은다. 원문을 지우지 않는다. 오타 사전이 모르는 표기를 표준형이 지워 버릴 수 - 있어서 둘 다 바늘더미에 남긴다. 원문이 먼저다(2026-09-19). + 있어서 둘 다 바늘더미에 남긴다. 원문이 먼저다. """ haystacks: list[str] = [] @@ -3790,7 +3775,7 @@ def retrieve_knowledge_bundle( """GraphRAG + BM25/Dense RRF 검색 번들 반환. 엔티티와 관계(트리플)가 한쪽에 편중되지 않도록 균형 예산을 적용하며, - 방 ID와 방 이름을 상호 확인하여 방 간 데이터 격리를 보장한다 (2026-09-17). + 방 ID와 방 이름을 상호 확인하여 방 간 데이터 격리를 보장한다. Dense 실패 시 모드는 bm25_only이며 hybrid/rrf로 표시하지 않는다. """ ranked = _query_knowledge_ranked( @@ -3901,7 +3886,7 @@ def _query_knowledge_ranked( Cloud pricing and then sent a bare photo. Matching only the incoming "사진" retrieved nothing, so the answer that followed had no idea the conversation was about cloud subscriptions, even though the graph held - that fact (2026-09-16). The caller passes the recent conversation here. + that fact. The caller passes the recent conversation here. Every additional source is matched at lower precedence than the incoming message, and the returned lines keep their category and name so the model @@ -3923,7 +3908,7 @@ def _query_knowledge_ranked( } if not haystacks: # 빈 질의로도 불린다. 여기서 리스트 하나를 돌려주면 호출자의 세 값 - # 언패킹이 깨져 조회 전체가 실패한다(2026-09-19). + # 언패킹이 깨져 조회 전체가 실패한다. return empty root = state_root or Path.home() / "Library/Application Support/openkakao/bujamentor" kg_path = root / KNOWLEDGE_GRAPH_DB_NAME @@ -3939,7 +3924,6 @@ def _query_knowledge_ranked( # 예전에는 부분 문자열로 비교했다. 그러면 방 이름이 다른 방 이름의 # 일부일 때 정상 노드가 걸러지고, 반대로 짧은 키는 남의 방 노드를 # 통과시킨다. 허용 집합을 만든 뒤 완전 일치만 통과시킨다 - # (2026-09-17, 6 Pro 지적). allowed_rooms: set[str] = set() if chat_str: allowed_rooms.add(chat_str) diff --git a/scripts/auto_reply_reference_store.py b/scripts/auto_reply_reference_store.py index 716867d5..ff3fce23 100644 --- a/scripts/auto_reply_reference_store.py +++ b/scripts/auto_reply_reference_store.py @@ -61,7 +61,7 @@ # A group whose `last_updated_at` has not moved since the last pass cannot hold # a message we have not already read. Remembering that stamp lets a harvest # skip the per-group `local-read` instead of re-reading 2000 rows for every one -# of ~90 groups on every pass, which cost 12-20 seconds each time (2026-09-17). +# of ~90 groups on every pass, which cost 12-20 seconds each time. LOCAL_GROUP_SEEN_KEY_PREFIX = "reference_pack_group_seen:" BOT_SENDERS = frozenset( @@ -1076,7 +1076,7 @@ def _harvest_event_stream( # write transaction across it kept a RESERVED lock on the shared # context database for that whole window, so every room's # context-sync-local failed with "database is locked" and the - # auto-reply group restarted in a loop (2026-09-18). Flush first. + # auto-reply group restarted in a loop. Flush first. connection.commit() _enrich_pack_analysis( connection, @@ -1230,7 +1230,7 @@ def _harvest_local_groups( # Remember the stamp only once this group's events reached the # checkpoint. Recording it before the harvest would skip a group whose # processing ran out of budget, and its messages would never be read - # again until the group changed (2026-09-17). + # again until the group changed. if updated is not None and updated > 0 and not _timed_out(deadline_at): _remember_local_group_stamp(connection, chat_id, updated) seen_stamps[chat_id] = updated @@ -1356,7 +1356,7 @@ def _rebuild_stale_v1_packs( # A partial batch must not leak its write transaction into the caller's # long read phase. This database is shared with every room's # context-sync-local, and an open write transaction holds a RESERVED lock - # on it for as long as the caller keeps working (2026-09-18). + # on it for as long as the caller keeps working. connection.commit() return rebuilt From af56b11263605ade6c5d259c0d6361aefe6e8be9 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:17:06 +0900 Subject: [PATCH 13/67] fix: stop owned children on app exit without latching the abort Quitting during a model swap or other bridge job left the Python child running. Add an exit hook that gives model swaps their cooperative marker and a short grace, then hard-stops whatever is still registered. It deliberately does not call global_abort: that writes the emergency latch, which refuses every later job until a human resumes. Co-Authored-By: GPT-6 Astra via opencodex --- desktop/src-tauri/src/main.rs | 14 +++++- desktop/src-tauri/src/python_bridge.rs | 67 ++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/desktop/src-tauri/src/main.rs b/desktop/src-tauri/src/main.rs index e7f423c8..9336e3de 100644 --- a/desktop/src-tauri/src/main.rs +++ b/desktop/src-tauri/src/main.rs @@ -10,6 +10,8 @@ use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, const PANEL_WIDTH: f64 = 276.0; const VISIBILITY_EVENT: &str = "jarvis://visibility"; +// Time a quitting app gives a model swap to roll back on its own. +const APP_EXIT_COOPERATIVE_GRACE: std::time::Duration = std::time::Duration::from_secs(3); /// Payload for the window visibility event: one boolean, never content. fn visibility_payload(visible: bool) -> Value { @@ -279,8 +281,16 @@ fn main() { } _ => {} }) - .run(tauri::generate_context!()) - .expect("error while running OpenKakao Jarvis"); + .build(tauri::generate_context!()) + .expect("error while building OpenKakao Jarvis") + .run(|app, event| { + // Quitting must not orphan a Python child (for example a model + // swap in progress). Stop owned children without latching the abort. + if let tauri::RunEvent::Exit = event { + app.state::() + .terminate_children(APP_EXIT_COOPERATIVE_GRACE); + } + }); } #[cfg(test)] diff --git a/desktop/src-tauri/src/python_bridge.rs b/desktop/src-tauri/src/python_bridge.rs index f3bb0bd5..c3d84f63 100644 --- a/desktop/src-tauri/src/python_bridge.rs +++ b/desktop/src-tauri/src/python_bridge.rs @@ -31,6 +31,8 @@ const OUTPUT_LIMIT_BYTES: usize = 4 * 1024 * 1024; // pipe drains after the leader is gone. const PROCESS_TERM_GRACE: Duration = Duration::from_millis(250); const PIPE_DRAIN_GRACE: Duration = Duration::from_millis(500); +// Hard-stop window on quit, after the cooperative grace has run out. +const TERMINATE_HARD_GRACE: Duration = Duration::from_secs(2); const BROWSER_TOOL_OUTPUT_LIMIT_BYTES: usize = 96 * 1024; const BROWSER_TOOL_RESULT_LIMIT_BYTES: usize = 64 * 1024; const BROWSER_TOOL_TASK_LIMIT_BYTES: usize = 16 * 1024; @@ -697,6 +699,42 @@ impl PythonBridge { Ok(()) } + /// Quit path: stop every owned child. Model swaps get their cooperative + /// marker first and the grace to roll back. Everything still registered is + /// then hard-stopped. The abort latch is deliberately not written, because + /// a latch refuses every later job until a human resume. + pub fn terminate_children(&self, cooperative_grace: Duration) { + if let Ok(map) = self.cancellations.lock() { + for handle in map.values() { + if let Some(marker) = handle.cooperative_marker.as_deref() { + let _ = write_model_swap_cancel_marker(marker); + } + } + } + self.wait_for_registry_drain(cooperative_grace); + if let Ok(map) = self.cancellations.lock() { + for handle in map.values() { + handle.flag.store(true, Ordering::SeqCst); + } + } + self.wait_for_registry_drain(TERMINATE_HARD_GRACE); + } + + fn wait_for_registry_drain(&self, limit: Duration) { + let deadline = Instant::now() + limit; + while Instant::now() < deadline { + let empty = self + .cancellations + .lock() + .map(|map| map.is_empty()) + .unwrap_or(true); + if empty { + return; + } + thread::sleep(Duration::from_millis(10)); + } + } + pub fn start_voice_session(&self) -> Result<(), BridgeError> { start_voice_session_if_absent(&self.config.state_root, voice_session_is_running, || { let resources = self.config.resources()?; @@ -5353,6 +5391,35 @@ mod tests { assert_eq!(still_listed, Some(None), "running swap job was removed or relabelled"); } + #[test] + fn app_exit_stops_children_without_latching_future_jobs() { + // Quitting mid-job must not orphan the child, and must not write the + // emergency latch: a latch blocks every later job until a human resume. + let (bridge, root) = fixture_bridge("exit-stop", SLEEPING_CHILD); + let worker = bridge.clone(); + let (outcome_tx, outcome_rx) = std::sync::mpsc::channel(); + thread::spawn(move || { + let result = worker.run_python_with_output_limit(PythonRun { + timeout: Duration::from_secs(60), + ..PythonRun::default() + }); + let _ = outcome_tx.send(result.map(|_| ())); + }); + let pid = wait_for_pid_file(&bridge.config.state_root.join("child.pid")); + bridge.terminate_children(Duration::from_millis(100)); + let outcome = outcome_rx.recv_timeout(Duration::from_secs(5)); + let gone = wait_until_gone(pid, Duration::from_secs(2)); + kill_own_child(pid); + let latched = bridge.config.state_root.join(ABORT_STATE_NAME).exists(); + fs::remove_dir_all(root).unwrap(); + assert!( + matches!(outcome, Ok(Err(BridgeError::Cancelled))), + "exit did not stop the child: {outcome:?}" + ); + assert!(gone, "child survived app exit"); + assert!(!latched, "app exit latched the abort"); + } + #[test] fn cancel_marker_writes_do_not_collide_on_temp_names() { // Writers in one process share a pid. A writer that is already in flight From 1b3f9081b55d3f37de43ce82c520c3c236c2899f Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:18:14 +0900 Subject: [PATCH 14/67] ci: run eight more Python test modules and check desktop formatting Eight test modules passed on a fresh checkout with Python 3.11, no user data, no network, and no host services, but CI never ran them. They add 171 tests and about 40 seconds to the desktop job. tests.test_auto_reply_menubar stays out because it needs a frozen .pyc. The full tests.test_auto_reply_cli_runtime module stays out because 73 of its 348 tests fail on a fresh checkout. The desktop crate is rustfmt-clean, so its format is now checked. The CLI crate is not. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 672dca70..2da2aa2e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -149,6 +149,11 @@ jobs: with: python-version: "3.11" + # Only 3 of the 348 tests in tests.test_auto_reply_cli_runtime run here. + # The others fail on a fresh checkout: they need + # ~/Library/Application Support/openkakao/context.sqlite or a model + # server on 127.0.0.1:11234, or they hit "transition queue room path + # mismatch" (2026-11-16). - name: Run focused local AI tests run: >- python3 -m unittest @@ -183,6 +188,14 @@ jobs: tests.test_jarvis_wake_evidence tests.test_jarvis_browser_runtime tests.test_jarvis_desktop_render_check + tests.test_ahp_measure_live + tests.test_auto_reply_golden_dataset + tests.test_auto_reply_transition_journal + tests.test_auto_reply_tui + tests.test_jarvis_tool_runtime + tests.test_jarvis_unit3 + tests.test_jarvis_voice_runtime + tests.test_local_mlx_gateway - name: Set up Node.js uses: actions/setup-node@v4 @@ -231,6 +244,11 @@ jobs: OPENKAKAO_TAURI_SOURCE_CHECK: "1" run: cargo check --manifest-path desktop/src-tauri/Cargo.toml --locked --offline + # Only the desktop crate is rustfmt-clean; the CLI crate is not yet + # (2026-11-16). + - name: Check desktop Rust bridge formatting + run: cargo fmt --manifest-path desktop/src-tauri/Cargo.toml --check + # Every desktop-crate lint is either fixed or carries an in-code reason, # so this step keeps the bridge free of dead code and accidental # signatures instead of relying on a local clippy run (2026-09-22). From 6719a9946e2061bc9b57f4a5920d616b4bc084ed Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:18:14 +0900 Subject: [PATCH 15/67] ci: gate releases on clippy --all-targets The release build ran only cargo test, so a tag could ship code that main rejects. It now runs the same clippy step as the macOS CI job. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/release.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 82c51eec..11f88013 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -76,6 +76,9 @@ jobs: - name: Cargo test run: cargo test --manifest-path $MANIFEST + - name: Lint CLI crate (clippy) + run: cargo clippy --manifest-path $MANIFEST --all-targets -- -D warnings + - name: Cargo release build run: cargo build --release --manifest-path $MANIFEST From 2ff0fca9b49b3d89ae13b04a9735cfb46e0e8080 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:18:14 +0900 Subject: [PATCH 16/67] docs: match the push checklist to CI The checklist listed a cargo fmt gate that CI does not run. It also used clippy without --all-targets, and it left out the launchd harness, the Python tests, and the desktop steps. It now lists the same commands as ci.yml and reads the Python module list from ci.yml. Release step 5 now names the macos_build job instead of a verify job that does not exist. Co-Authored-By: Claude Opus 5.5 --- CONTRIBUTING.md | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b77431f0..32bf4852 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -63,21 +63,43 @@ docs: update API endpoint documentation ## Push 전 체크리스트 -CI와 동일한 게이트를 로컬에서 먼저 돌려 주세요. `main`과 릴리스 워크플로우 모두 이 세 명령이 통과해야 진행됩니다. +`.github/workflows/ci.yml`과 같은 게이트를 로컬에서 먼저 돌려 주세요. 릴리스 워크플로우는 이 중 CLI 크레이트의 test와 clippy를 다시 돌립니다. + +CLI 크레이트: ```bash -cargo fmt --manifest-path Cargo.toml --check -cargo clippy --manifest-path Cargo.toml -- -D warnings cargo test --manifest-path Cargo.toml +cargo clippy --manifest-path Cargo.toml --all-targets -- -D warnings +sh scripts/test-auto-reply-launchd-artifacts.sh +``` + +Python 테스트(CI는 Python 3.11). 모듈 목록은 `ci.yml`의 `Run focused local AI tests` 단계가 기준이므로 거기서 그대로 읽어 실행합니다: + +```bash +python3 -m unittest $(sed -n '/Run focused local AI tests/,/^$/p' .github/workflows/ci.yml | grep -oE 'tests\.test_[A-Za-z0-9_.]+') ``` +Desktop(Tauri) 프런트엔드와 Rust 브리지: + +```bash +(cd desktop && npm ci && npm test -- --run && npm run build) +cargo fmt --manifest-path desktop/src-tauri/Cargo.toml --check +cargo fetch --manifest-path desktop/src-tauri/Cargo.toml --locked +OPENKAKAO_TAURI_SOURCE_CHECK=1 cargo test --manifest-path desktop/src-tauri/Cargo.toml --locked --offline +OPENKAKAO_TAURI_SOURCE_CHECK=1 cargo clippy --manifest-path desktop/src-tauri/Cargo.toml --all-targets --locked --offline -- -D warnings +``` + +CLI 크레이트는 아직 `cargo fmt` 기준으로 정리되지 않아 CI가 `cargo fmt --check`를 돌리지 않습니다. 고친 파일만 포매팅하고, 무관한 파일을 한꺼번에 재포매팅하지 마세요. + +`tests.test_auto_reply_menubar`는 저장소에 없는 frozen `.pyc`가 필요해 CI에서 돌지 않습니다. 메뉴 막대 코드를 고쳤다면 baked runtime이 있는 머신에서 `python3 -m unittest tests.test_auto_reply_menubar`를 따로 돌려 주세요. + ## 릴리스 절차 1. `CHANGELOG.md`의 `[Unreleased]` 섹션을 새 버전 섹션으로 정리 2. `Cargo.toml`의 `version` 필드 bump (+ `cargo update -p openkakao-cli`로 `Cargo.lock` 반영) 3. Push 전 체크리스트 통과 확인 4. `main`에 커밋·푸시 후 `git tag vX.Y.Z && git push origin vX.Y.Z` -5. 릴리스 워크플로우의 `verify` job이 통과해야 빌드·Homebrew tap 업데이트가 진행됨 — `verify`가 빨갛게 나면 태그만 남고 release는 만들어지지 않으므로, fix 후 버전을 한 단계 올려 재태그할 것 (태그 force-move 금지: v1.1.0 인시던트의 원인) +5. 릴리스 워크플로우의 `macos_build` job(`cargo test`, clippy, release 빌드)이 통과해야 GitHub Release가 게시됨. `macos_build`가 빨갛게 나면 태그만 남고 release는 만들어지지 않으므로, fix 후 버전을 한 단계 올려 재태그할 것 (태그 force-move 금지: v1.1.0 인시던트의 원인) ## 주의사항 From f579e1e68ce45c53f01d821dfaf07eee6449c52e Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:18:17 +0900 Subject: [PATCH 17/67] refactor(auto-reply): keep user-directive provenance and drop the dead cancel flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment cleanup removed six (사용자 지시) markers. They record that a behavior was the owner's product decision, which the code cannot show, so they come back without the dates. Removing cancel() left _cancelled always False; _is_cancelled still reads cancel_check, so the field goes too. Co-Authored-By: Claude Opus 5.5 --- scripts/auto_reply_knowledge_graph.py | 10 ++++++---- scripts/auto_reply_ondevice.py | 3 --- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/scripts/auto_reply_knowledge_graph.py b/scripts/auto_reply_knowledge_graph.py index b93ca955..d0f1fd79 100644 --- a/scripts/auto_reply_knowledge_graph.py +++ b/scripts/auto_reply_knowledge_graph.py @@ -612,7 +612,7 @@ def _room_ledgers(state_root: Path) -> list[Path]: # 방에서 누구와 나눈 이야기인지는 몰랐다. 답변에 필요한 것은 주제만이 # 아니라 그 주제가 누구의 어떤 맥락이었는가다. 방과 사람을 따로 세우고 # 주제와 이어 두면, 같은 "주식"이라도 성린이형 방의 주식과 부자멘토멘티의 -# 주식이 다른 자리로 남는다. +# 주식이 다른 자리로 남는다 (사용자 지시). INDEX_CHAT_MIN_MESSAGES = 40 INDEX_PERSON_MIN_MESSAGES = 25 # 한 방에서 이 사람의 메시지가 이 비율을 넘으면 그 방의 중심 인물로 본다. @@ -1227,6 +1227,7 @@ def index_person_entities( 사람 뉴런은 방 뉴런에 매인다. 같은 이름이 여러 방에 있어도 방마다 다른 뉴런이 되어, "성린이형 방의 성린이"와 다른 방의 동명이인이 섞이지 않는다 + (사용자 지시). """ stats = {"persons": 0, "written": 0, "with_lines": 0} index_path = _index_db_path(state_root) @@ -1535,7 +1536,7 @@ def index_membership_relations( - 사람 → 주제 (TALKS_ABOUT): 이 사람이 이 주제를 자주 꺼낸다 세 번째가 답변에 가장 쓸모 있다. 같은 "주식"이라도 누가 꺼낸 - 이야기인지에 따라 답이 달라지기 때문이다. + 이야기인지에 따라 답이 달라지기 때문이다 (사용자 지시). """ stats = {"person_chat": 0, "chat_topic": 0, "person_topic": 0} index_path = _index_db_path(state_root) @@ -1666,7 +1667,7 @@ def prune_indexed_entities( 색인에서 빠진 대화가 그래프에 그대로 남았다. 그래서 같은 방을 가리키는 두 뉴런("커리어톡"과 "커리어톡 (커리어톡)")이 나란히 서고, 지워진 주제도 계속 그려졌다. 색인으로 만든 뉴런 중 이번 주기에 다시 쓰이지 않은 것을 - 지우고, 그 뉴런에 매달린 시냅스도 함께 지운다. + 지우고, 그 뉴런에 매달린 시냅스도 함께 지운다 (사용자 지시). 손으로 적은 기본 뉴런(ent:*)과 원장에서 온 근거는 건드리지 않는다. @@ -1910,7 +1911,7 @@ def _run() -> None: # 메뉴바는 이 모듈을 짧게 사는 프로세스로 부른다. 데몬 스레드는 그 # 프로세스가 끝나면 함께 죽어 재색인이 반쯤 돌다 만 채로 남는다. 그래서 - # 기본값(mode="process")은 스스로 살아남는 자식 프로세스다. + # 기본값(mode="process")은 스스로 살아남는 자식 프로세스다 (사용자 지시). # 테스트는 "thread", 일회성 내보내기는 "inline" 으로 돈다. thread = threading.Thread(target=_run, name="kg-reindex", daemon=True) thread.start() @@ -2053,6 +2054,7 @@ def _reindex_all( A step that dies is recorded in kg_meta instead of being swallowed. The menu showed a freshly indexed, empty graph while every indexer had been failing on a NameError that only the real database path reached + (사용자 지시). """ failures: list[str] = [] diff --git a/scripts/auto_reply_ondevice.py b/scripts/auto_reply_ondevice.py index 85c818f2..56a786de 100644 --- a/scripts/auto_reply_ondevice.py +++ b/scripts/auto_reply_ondevice.py @@ -299,12 +299,9 @@ def __init__( self.cancel_check = cancel_check self._condition = threading.Condition() self._in_flight = 0 - self._cancelled = False self._swap_in_progress = False def _is_cancelled(self) -> bool: - if self._cancelled: - return True if self.cancel_check is None: return False try: From 320631e44a2d69c55ff3bfb9445fee43f24ef996 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:20:28 +0900 Subject: [PATCH 18/67] fix: stop chat pagination on a repeated or endless cursor get_all_chats looped for as long as the server returned a non-None cursor, so a server that repeated a cursor made the CLI hammer talk-pilsner forever. Pagination now errors when a cursor repeats and after 100 pages, the same bound get_all_messages uses. The loop takes a fetch closure so it can be tested without the network. Co-Authored-By: Claude Opus 5.5 --- src/rest.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 71 insertions(+), 13 deletions(-) diff --git a/src/rest.rs b/src/rest.rs index c8d9daab..f7b100ef 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; use std::time::Duration; use anyhow::{anyhow, Context, Result}; @@ -17,6 +18,7 @@ use crate::util::truncate; const BASE_URL: &str = "https://katalk.kakao.com"; const PILSNER_URL: &str = "https://talk-pilsner.kakao.com"; +const MAX_CHAT_PAGES: usize = 100; pub struct KakaoRestClient { creds: KakaoCredentials, @@ -177,19 +179,7 @@ impl KakaoRestClient { } pub fn get_all_chats(&self) -> Result> { - let mut all = Vec::new(); - let mut cursor: Option = None; - - loop { - let (rooms, next_cursor) = self.get_chats(cursor)?; - all.extend(rooms); - if next_cursor.is_none() { - break; - } - cursor = next_cursor; - } - - Ok(all) + collect_chat_pages(|cursor| self.get_chats(cursor)) } pub fn get_chat_members(&self, chat_id: i64) -> Result> { @@ -527,6 +517,30 @@ impl KakaoRestClient { } } +fn collect_chat_pages( + mut fetch: impl FnMut(Option) -> Result<(Vec, Option)>, +) -> Result> { + let mut all = Vec::new(); + let mut seen = HashSet::new(); + let mut cursor: Option = None; + + for _ in 0..MAX_CHAT_PAGES { + let (rooms, next_cursor) = fetch(cursor)?; + all.extend(rooms); + let Some(next) = next_cursor else { + return Ok(all); + }; + if !seen.insert(next) { + return Err(anyhow!("chat list cursor {next} repeated; stopped paging")); + } + cursor = Some(next); + } + + Err(anyhow!( + "chat list did not end after {MAX_CHAT_PAGES} pages" + )) +} + fn parse_account_response(endpoint: &str, text: &str) -> Result { serde_json::from_str(text).with_context(|| { format!( @@ -540,6 +554,50 @@ fn parse_account_response(endpoint: &str, text: &str) -> Result { mod tests { use super::*; + fn room(chat_id: i64) -> ChatRoom { + ChatRoom::from_json(&serde_json::json!({ "chatId": chat_id })) + } + + fn chat_ids(rooms: &[ChatRoom]) -> Vec { + rooms.iter().map(|room| room.chat_id).collect() + } + + #[test] + fn chat_pages_follow_cursors_until_the_last_page() { + let pages = collect_chat_pages(|cursor| match cursor { + None => Ok((vec![room(1), room(2)], Some(10))), + Some(10) => Ok((vec![room(3)], Some(20))), + Some(20) => Ok((vec![room(4)], None)), + other => panic!("unexpected cursor {other:?}"), + }) + .unwrap(); + assert_eq!(chat_ids(&pages), vec![1, 2, 3, 4]); + } + + #[test] + fn repeated_chat_cursor_stops_with_an_error() { + let mut requests = 0; + let result = collect_chat_pages(|_| { + requests += 1; + assert!(requests <= 50, "kept requesting a repeated cursor"); + Ok((vec![room(requests)], Some(7))) + }); + assert!(result.is_err()); + assert_eq!(requests, 2, "no request after the cursor repeats"); + } + + #[test] + fn endless_chat_cursors_stop_at_the_page_cap() { + let mut requests = 0; + let result = collect_chat_pages(|_| { + requests += 1; + assert!(requests <= 2 * MAX_CHAT_PAGES, "never stopped paginating"); + Ok((vec![room(requests as i64)], Some(requests as i64))) + }); + assert!(result.is_err()); + assert_eq!(requests, MAX_CHAT_PAGES); + } + #[test] fn unparseable_korean_account_response_is_an_error_not_a_panic() { let body = "가".repeat(100); From c56af9074ed821ce57f79bbc6ec1891612b78bac Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:25:22 +0900 Subject: [PATCH 19/67] fix: reject out-of-range LOCO ports from booking and checkin full_connect cast server-supplied i32 ports with 'as u16', so 70000 became 4464 and -1 became 65535, and it connected to the wrong port. checkin already range-checked the port for its log line but full_connect re-parsed it unchecked, so the log and the real connection could disagree. One parser now rejects ports outside 1..=65535: booking drops them, and checkin falls back to the default port its log already reported. Co-Authored-By: Claude Opus 5.5 --- src/loco/client.rs | 75 +++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/src/loco/client.rs b/src/loco/client.rs index 357a907b..9599a645 100644 --- a/src/loco/client.rs +++ b/src/loco/client.rs @@ -300,13 +300,7 @@ impl LocoClient { { Ok(Ok(response)) => { if let Ok(host) = response.body.get_str("host") { - let loco_port = response - .body - .get_i32("port") - .ok() - .filter(|&p| p > 0 && p <= 65535) - .map(|p| p as u16) - .unwrap_or(DEFAULT_LOCO_PORT); + let loco_port = checkin_loco_port(&response.body); eprintln!( "[checkin] Server: {}:{} (TLS={}, port={})", host, loco_port, use_tls, port @@ -566,20 +560,11 @@ impl LocoClient { return Err(anyhow!("No checkin hosts in booking response")); } - // Get ports from wifi config - let ports: Vec = config - .get_document("wifi") - .ok() - .and_then(|w| w.get_array("ports").ok()) - .map(|arr| { - arr.iter() - .filter_map(|v| v.as_i32().map(|p| p as u16)) - .collect() - }) - .unwrap_or_default(); - let checkin_host = &checkin_hosts[0]; - let checkin_port = ports.first().copied().unwrap_or(DEFAULT_LOCO_PORT); + let checkin_port = booking_ports(&config) + .first() + .copied() + .unwrap_or(DEFAULT_LOCO_PORT); // Phase 2: Checkin let (checkin_data, use_tls) = self.checkin(checkin_host, checkin_port).await?; @@ -588,10 +573,7 @@ impl LocoClient { .get_str("host") .map_err(|_| anyhow!("No LOCO host from checkin"))? .to_string(); - let loco_port = checkin_data - .get_i32("port") - .map(|p| p as u16) - .unwrap_or(DEFAULT_LOCO_PORT); + let loco_port = checkin_loco_port(&checkin_data); // Phase 3: Connect and login self.connect(&loco_host, loco_port, use_tls).await?; @@ -667,6 +649,31 @@ impl LocoClient { } } +fn booking_ports(config: &Document) -> Vec { + config + .get_document("wifi") + .ok() + .and_then(|w| w.get_array("ports").ok()) + .map(|arr| { + arr.iter() + .filter_map(|v| v.as_i32().and_then(port)) + .collect() + }) + .unwrap_or_default() +} + +fn checkin_loco_port(checkin: &Document) -> u16 { + checkin + .get_i32("port") + .ok() + .and_then(port) + .unwrap_or(DEFAULT_LOCO_PORT) +} + +fn port(raw: i32) -> Option { + u16::try_from(raw).ok().filter(|&p| p != 0) +} + #[allow(clippy::too_many_arguments)] /// Upload a file to the LOCO media server. /// @@ -765,3 +772,23 @@ pub async fn loco_upload( eprintln!("[upload] Complete"); Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn booking_ports_skip_values_outside_the_port_range() { + let config = doc! { "wifi": { "ports": [70000_i32, -1_i32, 0_i32, 9282_i32, 443_i32] } }; + assert_eq!(booking_ports(&config), vec![9282, 443]); + } + + #[test] + fn checkin_port_outside_the_port_range_falls_back_to_the_default() { + for bad in [70000_i32, -1, 0] { + assert_eq!(checkin_loco_port(&doc! { "port": bad }), DEFAULT_LOCO_PORT); + } + assert_eq!(checkin_loco_port(&doc! { "port": 9282_i32 }), 9282); + assert_eq!(checkin_loco_port(&doc! {}), DEFAULT_LOCO_PORT); + } +} From 958b5d5727f58eb37372bd3cde0d12aa344c9009 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:27:39 +0900 Subject: [PATCH 20/67] fix(metrics): let shutdown exceptions pass through the metrics side channel Every catch in auto_reply_metrics.py was `except BaseException`, so a SystemExit or KeyboardInterrupt raised while a metric was being written (for example from a SIGTERM handler that calls sys.exit) was swallowed and the decorated service function returned normally. record() and Measurement.__exit__ run on every @perf.timed call, so a shutdown that lands during the metrics file write was silently lost. Narrow all seven catches to `except Exception`. Ordinary metrics I/O errors still never reach the caller; process-control exceptions do. Before: test_shutdown_raised_during_metric_write_reaches_the_caller failed with "SystemExit not raised". After: both metrics tests pass. Co-Authored-By: Claude Opus 5.5 --- scripts/auto_reply_metrics.py | 14 +++---- tests/test_auto_reply_metrics.py | 72 ++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 tests/test_auto_reply_metrics.py diff --git a/scripts/auto_reply_metrics.py b/scripts/auto_reply_metrics.py index 44e9a37c..e2d9b50d 100644 --- a/scripts/auto_reply_metrics.py +++ b/scripts/auto_reply_metrics.py @@ -49,7 +49,7 @@ def _number(value: object, *, integer: bool = False) -> int | float | None: return None try: number = float(value) - except BaseException: + except Exception: return None if not math.isfinite(number) or number < 0: return None @@ -94,7 +94,7 @@ def coarse_error_class(error: BaseException | object) -> str: return "runtime" if "subprocess" in name: return "subprocess" - except BaseException: + except Exception: pass return "unknown" @@ -120,7 +120,7 @@ def __enter__(self) -> "Measurement": try: if enabled(): self._started = time.monotonic() - except BaseException: + except Exception: self._started = None return self @@ -147,7 +147,7 @@ def __exit__(self, exc_type: object, exc: BaseException | None, _tb: object) -> lateness_ms=self.lateness_ms, error_class=self.error_class, ) - except BaseException: + except Exception: pass return False @@ -232,7 +232,7 @@ def record( else: sys.stderr.write(line) sys.stderr.flush() - except BaseException: + except Exception: return False return True @@ -274,7 +274,7 @@ def wrapped(*args: Any, **kwargs: Any) -> T: ) elif isinstance(result, bool) and not result: sample.outcome = "unavailable" - except BaseException: + except Exception: pass return result return wrapped @@ -287,5 +287,5 @@ def byte_length(value: object) -> int: return 0 try: return min(len(value.encode("utf-8", "ignore")), _MAX_TOTAL) - except BaseException: + except Exception: return 0 diff --git a/tests/test_auto_reply_metrics.py b/tests/test_auto_reply_metrics.py new file mode 100644 index 00000000..b151f1b6 --- /dev/null +++ b/tests/test_auto_reply_metrics.py @@ -0,0 +1,72 @@ +import importlib.util +import os +import signal +import sys +import tempfile +import unittest +from pathlib import Path +from unittest import mock + + +ROOT = Path(__file__).resolve().parents[1] +SCRIPTS = ROOT / "scripts" + + +def load_metrics(name: str): + spec = importlib.util.spec_from_file_location(name, SCRIPTS / "auto_reply_metrics.py") + module = importlib.util.module_from_spec(spec) + assert spec.loader is not None + sys.modules[name] = module + spec.loader.exec_module(module) + return module + + +class MetricsSideChannelTests(unittest.TestCase): + def test_metric_write_failure_never_reaches_the_caller(self): + perf = load_metrics("auto_reply_metrics_io_failure_test") + + @perf.timed("supervisor.status_write") + def service_step(): + return {"ok": True} + + with tempfile.TemporaryDirectory() as raw: + with mock.patch.dict( + os.environ, + {"OPENKAKAO_PERF_METRICS": "1", "OPENKAKAO_PERF_METRICS_FILE": raw}, + ): + self.assertEqual(service_step(), {"ok": True}) + + @unittest.skipUnless(hasattr(signal, "setitimer"), "needs setitimer") + def test_shutdown_raised_during_metric_write_reaches_the_caller(self): + perf = load_metrics("auto_reply_metrics_shutdown_test") + + @perf.timed("supervisor.status_write") + def service_step(): + return {"ok": True} + + def request_shutdown(_signum, _frame): + raise SystemExit(143) + + with tempfile.TemporaryDirectory() as raw: + sink = Path(raw) / "metrics.fifo" + os.mkfifo(sink) + previous = signal.signal(signal.SIGALRM, request_shutdown) + try: + with mock.patch.dict( + os.environ, + { + "OPENKAKAO_PERF_METRICS": "1", + "OPENKAKAO_PERF_METRICS_FILE": str(sink), + }, + ): + with self.assertRaises(SystemExit) as raised: + signal.setitimer(signal.ITIMER_REAL, 0.05) + service_step() + finally: + signal.setitimer(signal.ITIMER_REAL, 0) + signal.signal(signal.SIGALRM, previous) + self.assertEqual(raised.exception.code, 143) + + +if __name__ == "__main__": + unittest.main() From 7ebed123da42c13fa21313f2ea5f7e00cf51eadc Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:32:11 +0900 Subject: [PATCH 21/67] fix(supervisor): decide GeekNews-only once and keep db-watch off for it A GeekNews-only room must run the reply worker without the per-account db-watch. main() only applied that rule when db_ready() was true at startup, and then reset database_started to False. The main loop treats database_started=False as "DB not ready yet": on the first tick db_ready() answered true, so the loop started db-watch and set OPENKAKAO_DB_READY=1 and OPENKAKAO_AUTO_REPLY_ENABLED=1 anyway. A room whose DB was unready at startup skipped the rule entirely and was promoted the same way later. Decide geeknews_only once, before the DB probe, publish it as the database reason, and make the promotion branch respect it. Before: both GeekNews-only main() tests failed with db-watch in the spawned scripts. After: they pass, and the reply-room control still starts db-watch once the DB becomes ready. Co-Authored-By: Claude Opus 5.5 --- scripts/auto-reply-supervisor.py | 29 +-- tests/test_auto_reply_supervisor_lifecycle.py | 194 ++++++++++++++++++ 2 files changed, 211 insertions(+), 12 deletions(-) create mode 100644 tests/test_auto_reply_supervisor_lifecycle.py diff --git a/scripts/auto-reply-supervisor.py b/scripts/auto-reply-supervisor.py index 803c68df..0a2688f8 100644 --- a/scripts/auto-reply-supervisor.py +++ b/scripts/auto-reply-supervisor.py @@ -1533,8 +1533,18 @@ def main() -> int: signal.signal(signal.SIGINT, _handle_shutdown_signal) acquire_owner() - database_started = db_ready() - database_reason = "ready" if database_started else "local_db_unavailable" + # 긱뉴스 전용 방은 DB 감시자 없이 워커만 돌린다. DB 감시자는 계정당 하나라 + # 두 방이 동시에 요구하면 서로를 밀어낸다(2026-09-12 실측). 답장 전송도 함께 + # 꺼지지만 워커는 계속 살아 긱뉴스 슬롯을 처리한다. + geeknews_only = ( + str(os.environ.get("OPENKAKAO_GEEKNEWS_ONLY") or "").strip() == "1" + or _geeknews_only_room() + ) + database_started = not geeknews_only and db_ready() + if geeknews_only: + database_reason = "geeknews_only" + else: + database_reason = "ready" if database_started else "local_db_unavailable" auto_reply_enabled, auto_reply_reason = auto_reply_config(parsed_config) auto_reply_section = _toml_section(parsed_config, "auto_reply", "auto-reply", "bujamentor") allow_link_fetch = ( @@ -1561,15 +1571,6 @@ def main() -> int: os.environ["OPENKAKAO_DB_SOURCE_EPOCH"] = source_epoch os.environ["OPENKAKAO_WATCH_OWNER"] = owner_id os.environ["OPENKAKAO_WATCH_EPOCH"] = source_epoch - # 긱뉴스 전용 방은 DB 감시자 없이 워커만 돌린다. DB 감시자는 계정당 하나라 - # 두 방이 동시에 요구하면 서로를 밀어낸다(2026-09-12 실측). 답장 전송도 함께 - # 꺼지지만 워커는 계속 살아 긱뉴스 슬롯을 처리한다. - if database_started and ( - str(os.environ.get("OPENKAKAO_GEEKNEWS_ONLY") or "").strip() == "1" - or _geeknews_only_room() - ): - database_started = False - database_reason = "geeknews_only" os.environ["OPENKAKAO_DB_READY"] = "1" if database_started else "0" os.environ["OPENKAKAO_AUTO_REPLY_ENABLED"] = "1" if database_started and auto_reply_enabled else "0" os.environ[PRIVACY_ATTESTATION_ENV] = privacy_digest @@ -1654,7 +1655,11 @@ def main() -> int: if exit_reason: write_status(**_status_context, state_name="fenced", fence_reason=exit_reason, force=True) stop(reason=exit_reason, exit_code=1) - if not _status_context["database_started"] and db_ready(): + if ( + not geeknews_only + and not _status_context["database_started"] + and db_ready() + ): _status_context["database_started"] = True _status_context["database_reason"] = "ready" _status_context["auto_reply_enabled"] = bool(auto_reply_enabled) diff --git a/tests/test_auto_reply_supervisor_lifecycle.py b/tests/test_auto_reply_supervisor_lifecycle.py new file mode 100644 index 00000000..df1dc7aa --- /dev/null +++ b/tests/test_auto_reply_supervisor_lifecycle.py @@ -0,0 +1,194 @@ +import importlib.util +import itertools +import json +import os +import signal +import sys +import tempfile +import unittest +from pathlib import Path +from unittest import mock + + +ROOT = Path(__file__).resolve().parents[1] +SCRIPTS = ROOT / "scripts" +if str(SCRIPTS) not in sys.path: + sys.path.insert(0, str(SCRIPTS)) + +_counter = itertools.count() + + +def load_supervisor(): + name = f"auto_reply_supervisor_lifecycle_{next(_counter)}" + spec = importlib.util.spec_from_file_location(name, SCRIPTS / "auto-reply-supervisor.py") + module = importlib.util.module_from_spec(spec) + assert spec.loader is not None + spec.loader.exec_module(module) + return module + + +class FakeChild: + _pids = itertools.count(41000) + + def __init__(self, argv, env): + self.argv = list(argv) + self.env = dict(env or {}) + self.pid = next(self._pids) + self.returncode = None + self.stdout = None + self.terminated = False + + def poll(self): + return self.returncode + + def terminate(self): + self.terminated = True + self.returncode = -signal.SIGTERM + + def kill(self): + self.terminated = True + self.returncode = -signal.SIGKILL + + def wait(self, timeout=None): + return self.returncode + + +class SupervisorMainHarness: + """Run the real supervisor main() against fake children and a fake DB probe.""" + + def __init__(self, test, root, *, db_ready, env=None, ticks=3, sleep_hook=None): + self.test = test + self.state_root = root / "home" / "state" / "rooms" / "42" + self.module = load_supervisor() + self.spawned = [] + self.db_ready = db_ready + self.env = env or {} + self.ticks = ticks + self.sleep_hook = sleep_hook + self.sleeps = 0 + + def _popen(self, argv, **kwargs): + child = FakeChild(argv, kwargs.get("env")) + self.spawned.append(child) + return child + + def _sleep(self, _seconds): + self.sleeps += 1 + if self.sleep_hook is not None: + self.sleep_hook(self) + if self.sleeps >= self.ticks: + self.module._signal_requested = signal.SIGTERM + + def scripts_started(self): + return [ + Path(arg).name + for child in self.spawned + for arg in child.argv + if str(arg).endswith(".py") + ] + + def status(self): + return json.loads((self.state_root / "supervisor-status.json").read_text("utf-8")) + + def run(self): + module = self.module + config = { + "safety": {"allow_auto_reply": True}, + "model": {"privacy_mode": "local"}, + } + argv = [ + "auto-reply-supervisor.py", + "--state-root", + str(self.state_root), + "--target-chat-id", + "42", + "--self-nickname", + "me", + "--interval", + "0.2", + ] + environment = { + "OPENKAKAO_REPLY_AUTHORS": "friend", + "OPENKAKAO_GEEKNEWS_ONLY": "", + **self.env, + } + with mock.patch.dict(os.environ, environment), mock.patch.object( + sys, "argv", argv + ), mock.patch.object( + module, "read_attested_config", return_value=("digest", config) + ), mock.patch.object( + module, "db_ready", side_effect=self.db_ready + ), mock.patch.object( + module.subprocess, "Popen", side_effect=self._popen + ), mock.patch.object( + module.signal, "signal" + ), mock.patch.object( + module.time, "sleep", side_effect=self._sleep + ): + with self.test.assertRaises(SystemExit) as raised: + module.main() + return raised.exception.code + + +class GeekNewsOnlyRoomTests(unittest.TestCase): + def assert_never_started_db_watch(self, harness): + self.assertNotIn("auto-reply-db-watch.py", harness.scripts_started()) + for child in harness.spawned: + self.assertEqual(child.env.get("OPENKAKAO_DB_READY"), "0", child.argv) + self.assertEqual( + child.env.get("OPENKAKAO_AUTO_REPLY_ENABLED"), "0", child.argv + ) + status = harness.status() + self.assertFalse(status["database_started"]) + self.assertEqual(status["database_reason"], "geeknews_only") + self.assertFalse(status["auto_reply_enabled"]) + + def test_geeknews_only_room_never_starts_db_watch_while_db_is_ready(self): + with tempfile.TemporaryDirectory() as raw: + harness = SupervisorMainHarness( + self, + Path(raw), + db_ready=lambda: True, + env={"OPENKAKAO_GEEKNEWS_ONLY": "1"}, + ) + self.assertEqual(harness.run(), 0) + self.assertEqual( + harness.scripts_started(), + ["auto-reply-apple-watch.py", "auto-reply-worker.py"], + ) + self.assert_never_started_db_watch(harness) + + def test_geeknews_only_room_never_starts_db_watch_when_db_becomes_ready(self): + answers = iter([False]) + with tempfile.TemporaryDirectory() as raw: + harness = SupervisorMainHarness( + self, + Path(raw), + db_ready=lambda: next(answers, True), + env={"OPENKAKAO_GEEKNEWS_ONLY": "1"}, + ) + self.assertEqual(harness.run(), 0) + self.assert_never_started_db_watch(harness) + + def test_reply_room_starts_db_watch_once_the_db_becomes_ready(self): + answers = iter([False]) + with tempfile.TemporaryDirectory() as raw: + harness = SupervisorMainHarness( + self, + Path(raw), + db_ready=lambda: next(answers, True), + ) + self.assertEqual(harness.run(), 0) + self.assertEqual( + harness.scripts_started(), + [ + "auto-reply-apple-watch.py", + "auto-reply-worker.py", + "auto-reply-db-watch.py", + ], + ) + self.assertTrue(harness.status()["database_started"]) + + +if __name__ == "__main__": + unittest.main() From a3293811186f4962b349948e45cb70e8a9425844 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:32:54 +0900 Subject: [PATCH 22/67] fix: bound LOCO connects and replies with timeouts booking, connect, send_command and loco_upload waited without a deadline, so a server that accepted the socket and never answered hung the CLI forever. Only checkin (10 s) and Legacy frame reassembly (30 s) were bounded. Every one-shot exchange (booking and checkin) and every connection setup now has a 10 s deadline, and every reply wait on an open connection has a 30 s deadline. A timed-out command marks the connection dirty so the next call reconnects instead of reading a half-consumed stream. The public recv_packet used by watch loops stays unbounded on purpose. Co-Authored-By: Claude Opus 5.5 --- src/loco/client.rs | 173 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 144 insertions(+), 29 deletions(-) diff --git a/src/loco/client.rs b/src/loco/client.rs index 9599a645..915ccc0f 100644 --- a/src/loco/client.rs +++ b/src/loco/client.rs @@ -23,6 +23,30 @@ const BOOKING_HOST: &str = "booking-loco.kakao.com"; const BOOKING_PORT: u16 = 443; const DEFAULT_LOCO_PORT: u16 = 5223; +// Tests shorten both deadlines so silent-server cases settle in milliseconds. +const CONNECT_TIMEOUT: Duration = if cfg!(test) { + Duration::from_millis(200) +} else { + Duration::from_secs(10) +}; +const RESPONSE_TIMEOUT: Duration = if cfg!(test) { + Duration::from_millis(200) +} else { + Duration::from_secs(30) +}; + +async fn within( + limit: Duration, + waiting_for: &str, + work: impl std::future::Future>, +) -> Result { + timeout(limit, work).await.unwrap_or_else(|_| { + Err(anyhow!( + "timed out after {limit:?} waiting for {waiting_for}" + )) + }) +} + enum LocoStream { Tls(Box>), Legacy { @@ -100,7 +124,7 @@ impl LocoStream { // Read additional frames if the first frame doesn't contain the full packet while decrypted.len() < total_needed { - let fragment_result = timeout(Duration::from_secs(30), async { + let frame2 = within(RESPONSE_TIMEOUT, "the next LOCO frame", async { let mut size_buf2 = [0u8; 4]; stream.read_exact(&mut size_buf2).await?; let size2 = ReadBytesExt::read_u32::(&mut Cursor::new( @@ -111,18 +135,11 @@ impl LocoStream { } let mut frame2 = vec![0u8; size2]; stream.read_exact(&mut frame2).await?; - Ok::, anyhow::Error>(frame2) + Ok(frame2) }) - .await; - - match fragment_result { - Ok(Ok(frame2)) => { - let decrypted2 = encryptor.decrypt(&frame2)?; - decrypted.extend_from_slice(&decrypted2); - } - Ok(Err(e)) => return Err(e), - Err(_) => return Err(anyhow!("Frame reassembly timed out after 30s")), - } + .await?; + let decrypted2 = encryptor.decrypt(&frame2)?; + decrypted.extend_from_slice(&decrypted2); } } @@ -156,6 +173,20 @@ async fn loco_oneshot( port: u16, packet: &LocoPacket, use_tls: bool, +) -> Result { + within( + CONNECT_TIMEOUT, + &format!("a LOCO reply from {host}:{port}"), + loco_exchange(host, port, packet, use_tls), + ) + .await +} + +async fn loco_exchange( + host: &str, + port: u16, + packet: &LocoPacket, + use_tls: bool, ) -> Result { if use_tls { let mut tls = tls_connect(host, port).await?; @@ -292,13 +323,8 @@ impl LocoClient { "[checkin] Trying {}:{} (TLS={})...", checkin_host, port, use_tls ); - match tokio::time::timeout( - std::time::Duration::from_secs(10), - loco_oneshot(checkin_host, *port, &pkt, *use_tls), - ) - .await - { - Ok(Ok(response)) => { + match loco_oneshot(checkin_host, *port, &pkt, *use_tls).await { + Ok(response) => { if let Ok(host) = response.body.get_str("host") { let loco_port = checkin_loco_port(&response.body); eprintln!( @@ -309,12 +335,9 @@ impl LocoClient { } eprintln!("[checkin] No host in response, trying next..."); } - Ok(Err(e)) => { + Err(e) => { eprintln!("[checkin] TLS={} port={} failed: {}", use_tls, port, e); } - Err(_) => { - eprintln!("[checkin] TLS={} port={} timed out", use_tls, port); - } } } @@ -328,11 +351,15 @@ impl LocoClient { host, port, use_tls ); + let waiting_for = format!("a LOCO connection to {host}:{port}"); if use_tls { - let tls = tls_connect(host, port).await?; + let tls = within(CONNECT_TIMEOUT, &waiting_for, tls_connect(host, port)).await?; self.stream = Some(LocoStream::Tls(Box::new(tls))); } else { - let mut tcp = TcpStream::connect((host, port)).await?; + let mut tcp = within(CONNECT_TIMEOUT, &waiting_for, async { + Ok(TcpStream::connect((host, port)).await?) + }) + .await?; let enc = LocoEncryptor::new(); let handshake = enc.build_handshake_packet()?; if (std::env::var("OPENKAKAO_CLI_DEBUG").is_ok() @@ -389,7 +416,7 @@ impl LocoClient { // Read packets until we find the response matching our packet_id let mut skip_count = 0usize; loop { - let response = match stream.recv_packet().await { + let response = match within(RESPONSE_TIMEOUT, method, stream.recv_packet()).await { Ok(r) => r, Err(e) => { self.is_dirty = true; @@ -693,7 +720,12 @@ pub async fn loco_upload( eprintln!("[upload] Connecting to {}:{}...", vhost, port); // Upload server uses legacy encrypted connection (same as main LOCO) - let mut tcp = TcpStream::connect((vhost, port)).await?; + let mut tcp = within( + CONNECT_TIMEOUT, + &format!("an upload connection to {vhost}:{port}"), + async { Ok(TcpStream::connect((vhost, port)).await?) }, + ) + .await?; let enc = LocoEncryptor::new(); let handshake = enc.build_handshake_packet()?; tcp.write_all(&handshake).await?; @@ -731,7 +763,12 @@ pub async fn loco_upload( stream.send_packet(&post_pkt).await?; // Read POST response - let post_resp = stream.recv_packet().await?; + let post_resp = within( + RESPONSE_TIMEOUT, + "the upload POST reply", + stream.recv_packet(), + ) + .await?; let post_status = post_resp.status(); if post_status != 0 { return Err(anyhow!( @@ -759,7 +796,12 @@ pub async fn loco_upload( } // Wait for upload completion response - let upload_resp = stream.recv_packet().await?; + let upload_resp = within( + RESPONSE_TIMEOUT, + "the upload completion reply", + stream.recv_packet(), + ) + .await?; let upload_status = upload_resp.status(); if upload_status != 0 { return Err(anyhow!( @@ -777,6 +819,79 @@ pub async fn loco_upload( mod tests { use super::*; + async fn silent_server() -> u16 { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + tokio::spawn(async move { + let mut held = Vec::new(); + while let Ok((socket, _)) = listener.accept().await { + held.push(socket); + } + }); + port + } + + async fn settles(work: impl std::future::Future) -> T { + timeout(Duration::from_secs(5), work) + .await + .expect("hung on a server that never answers") + } + + fn credentials() -> KakaoCredentials { + KakaoCredentials::new( + "token".into(), + 1, + "uuid".into(), + "3.7.0".into(), + "ua".into(), + "a".into(), + ) + } + + #[tokio::test] + async fn oneshot_to_a_silent_server_times_out() { + let port = silent_server().await; + let packet = PacketBuilder::new().build("GETCONF", doc! {}); + assert!(settles(loco_oneshot("127.0.0.1", port, &packet, false)) + .await + .is_err()); + } + + #[tokio::test] + async fn tls_oneshot_to_a_silent_server_times_out() { + let port = silent_server().await; + let packet = PacketBuilder::new().build("GETCONF", doc! {}); + assert!(settles(loco_oneshot("127.0.0.1", port, &packet, true)) + .await + .is_err()); + } + + #[tokio::test] + async fn tls_connect_to_a_silent_server_times_out() { + let port = silent_server().await; + let mut client = LocoClient::new(credentials()); + assert!(settles(client.connect("127.0.0.1", port, true)) + .await + .is_err()); + } + + #[tokio::test] + async fn command_to_a_silent_server_times_out() { + let port = silent_server().await; + let mut client = LocoClient::new(credentials()); + settles(client.connect("127.0.0.1", port, false)) + .await + .unwrap(); + assert!(settles(client.send_command("PING", doc! {})).await.is_err()); + } + + #[tokio::test] + async fn upload_to_a_silent_server_times_out() { + let port = silent_server().await; + let upload = loco_upload("127.0.0.1", port, 1, "key", 2, b"data", 2, 1, 1, "3.7.0"); + assert!(settles(upload).await.is_err()); + } + #[test] fn booking_ports_skip_values_outside_the_port_range() { let config = doc! { "wifi": { "ports": [70000_i32, -1_i32, 0_i32, 9282_i32, 443_i32] } }; From 705592bff138ecc7436dab8cfc2442e98f210060 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:33:04 +0900 Subject: [PATCH 23/67] refactor(context): move the test module to tests.rs The inline test module becomes src/context/tests.rs at the same module path, context::tests, so test names are unchanged. The body is identical once whitespace is ignored. Lines are dedented one level except inside string literals, and rustfmt rejoined lines that now fit. Co-Authored-By: Claude Opus 5.5 --- src/context/mod.rs | 3572 +----------------------------------------- src/context/tests.rs | 3552 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 3553 insertions(+), 3571 deletions(-) create mode 100644 src/context/tests.rs diff --git a/src/context/mod.rs b/src/context/mod.rs index 39c83e8b..a2125624 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -6161,3574 +6161,4 @@ fn bytes_to_vector(bytes: &[u8]) -> Vec { } #[cfg(test)] -mod tests { - use super::*; - use std::io::Write; - use tempfile::tempdir; - - const TEST_ACCOUNT_FINGERPRINT: &str = - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - - #[test] - fn context_database_wal_allows_readers_during_a_writer_transaction() -> Result<()> { - let dir = tempdir()?; - let db = dir.path().join("context-wal.sqlite3"); - let writer = open_db(&db)?; - let journal_mode: String = - writer.pragma_query_value(None, "journal_mode", |row| row.get(0))?; - assert_eq!(journal_mode.to_ascii_lowercase(), "wal"); - - writer.execute_batch("BEGIN EXCLUSIVE")?; - let reader = open_db_readonly(&db)?; - let messages: i64 = - reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { - row.get(0) - })?; - writer.execute_batch("ROLLBACK")?; - assert_eq!(messages, 0); - Ok(()) - } - - #[test] - fn context_wal_truncate_checkpoint_clears_deleted_message_pages() -> Result<()> { - let dir = tempdir()?; - let db = dir.path().join("context-wal-private.sqlite3"); - let conn = open_db(&db)?; - let retired_message = "retired-message-marker-7d28b"; - conn.execute( - "INSERT INTO context_messages(source, chat, date, user_name, message, vector) - VALUES ('source', 'chat', '2026-09-24', 'user', ?1, ?2)", - params![ - retired_message, - vector_to_bytes(&encode_vector(retired_message)) - ], - )?; - checkpoint_context_wal_truncate(&conn)?; - - conn.execute( - "DELETE FROM context_messages WHERE message = ?1", - [retired_message], - )?; - checkpoint_context_wal_truncate(&conn)?; - - let wal = fs::read(sqlite_sidecar_path(&db, "-wal")).unwrap_or_default(); - let main_db = fs::read(&db)?; - assert_eq!(wal.len(), 0); - assert!(!wal - .windows(retired_message.len()) - .any(|w| w == retired_message.as_bytes())); - assert!(!main_db - .windows(retired_message.len()) - .any(|w| w == retired_message.as_bytes())); - Ok(()) - } - - #[cfg(unix)] - #[test] - fn context_database_and_wal_sidecars_use_private_permissions() -> Result<()> { - use std::os::unix::fs::PermissionsExt; - - let dir = tempdir()?; - let db = dir.path().join("permissions.sqlite3"); - let conn = open_db(&db)?; - conn.execute( - "INSERT INTO context_messages(source, chat, date, user_name, message, vector) - VALUES ('source', 'chat', '2026-09-24', 'user', 'permission probe', ?1)", - [vector_to_bytes(&encode_vector("permission probe"))], - )?; - set_context_db_permissions(&db)?; - - assert_eq!( - fs::metadata(dir.path())?.permissions().mode() & 0o777, - 0o700 - ); - assert_eq!(fs::metadata(&db)?.permissions().mode() & 0o777, 0o600); - for suffix in ["-wal", "-shm"] { - let sidecar = sqlite_sidecar_path(&db, suffix); - if sidecar.exists() { - assert_eq!(fs::metadata(sidecar)?.permissions().mode() & 0o777, 0o600); - } - } - Ok(()) - } - - #[test] - fn context_reads_fail_closed_on_a_missing_database_without_creating_it() { - let dir = tempdir().unwrap(); - let parent = dir.path().join("absent"); - let db = parent.join("context.sqlite3"); - let assert_migration_required = |label: &str, result: Result<()>| { - assert_eq!( - result.unwrap_err().to_string(), - CONTEXT_RETRIEVAL_MIGRATION_REQUIRED, - "{label}" - ); - }; - - assert_migration_required( - "search", - search(&db, Some("방"), None, "probe", "hybrid", 5).map(|_| ()), - ); - assert_migration_required( - "style_search", - style_search(&db, Some("방"), "probe", 5).map(|_| ()), - ); - assert_migration_required( - "style_profile", - style_profile(&db, "방", STYLE_USER, None).map(|_| ()), - ); - assert_migration_required( - "recipient_style_profile", - recipient_style_profile(&db, "방", "민수", None).map(|_| ()), - ); - assert_migration_required( - "response_time_stats", - response_time_stats(&db, "방", STYLE_USER, None).map(|_| ()), - ); - assert_migration_required( - "reply_decision_search", - reply_decision_search(&db, "방", "probe", 5).map(|_| ()), - ); - assert_migration_required( - "get_reply_decision", - get_reply_decision(&db, "db:7:42").map(|_| ()), - ); - assert_migration_required( - "live_context_sync_state", - live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, 7).map(|_| ()), - ); - assert_migration_required( - "context_reply_bundle", - context_reply_bundle(&db, "방", "probe", None).map(|_| ()), - ); - assert_migration_required( - "context_reply_bundle_for_recipient_excluding_live_events", - context_reply_bundle_for_recipient_excluding_live_events( - &db, - "방", - "probe", - None, - "민수", - 7, - &[42], - ) - .map(|_| ()), - ); - - assert!( - !parent.exists(), - "read paths must not create the context database or its directory" - ); - } - - #[cfg(unix)] - #[test] - fn context_reads_refuse_a_symlinked_database() { - let dir = tempdir().unwrap(); - let real = dir.path().join("real.sqlite3"); - let csv = fixture(dir.path(), "chat.csv", "symlink probe"); - index_csv(&real, "방", &csv).unwrap(); - let link = dir.path().join("link.sqlite3"); - std::os::unix::fs::symlink(&real, &link).unwrap(); - - assert_eq!( - search(&real, Some("방"), None, "symlink", "keyword", 5) - .unwrap() - .len(), - 1 - ); - assert_eq!( - search(&link, Some("방"), None, "symlink", "keyword", 5) - .unwrap_err() - .to_string(), - CONTEXT_RETRIEVAL_MIGRATION_REQUIRED - ); - assert_eq!( - context_reply_bundle(&link, "방", "symlink", None) - .unwrap_err() - .to_string(), - CONTEXT_RETRIEVAL_MIGRATION_REQUIRED - ); - } - - #[test] - fn read_only_context_connection_rejects_writes() -> Result<()> { - let dir = tempdir()?; - let db = dir.path().join("readonly.sqlite3"); - let csv = fixture(dir.path(), "chat.csv", "read only probe"); - index_csv(&db, "방", &csv)?; - - let reader = open_db_readonly(&db)?; - let write = reader.execute( - "INSERT INTO context_messages(source, chat, date, user_name, message, vector) - VALUES ('source', '방', '2026-01-01', '민수', 'write probe', x'')", - [], - ); - assert!( - matches!( - write, - Err(rusqlite::Error::SqliteFailure(ref error, _)) - if error.code == rusqlite::ErrorCode::ReadOnly - ), - "{write:?}" - ); - let count: i64 = reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { - row.get(0) - })?; - assert_eq!(count, 1); - Ok(()) - } - - #[cfg(unix)] - #[test] - fn context_database_open_refuses_a_symlinked_wal_sidecar() -> Result<()> { - use std::os::unix::fs::PermissionsExt; - - let dir = tempdir()?; - let outside = dir.path().join("outside.bin"); - fs::write(&outside, b"not a wal")?; - fs::set_permissions(&outside, fs::Permissions::from_mode(0o644))?; - let db_dir = dir.path().join("ctx"); - fs::create_dir(&db_dir)?; - let db = db_dir.join("context.sqlite3"); - std::os::unix::fs::symlink(&outside, db_dir.join("context.sqlite3-wal"))?; - let csv = fixture(dir.path(), "chat.csv", "sidecar probe"); - - assert!(index_csv(&db, "방", &csv).is_err()); - assert!(!db.exists()); - assert_eq!(fs::metadata(&outside)?.permissions().mode() & 0o777, 0o644); - assert_eq!(fs::read(&outside)?, b"not a wal"); - Ok(()) - } - - #[test] - fn csv_reindex_truncates_the_wal_and_scrubs_replaced_text() -> Result<()> { - let dir = tempdir()?; - let db = dir.path().join("context.sqlite3"); - let retired = "retiredphrase7d28b"; - let csv = fixture(dir.path(), "chat.csv", retired); - index_csv(&db, "방", &csv)?; - - let concurrent_reader = open_db_readonly(&db)?; - let rows: i64 = - concurrent_reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { - row.get(0) - })?; - assert_eq!(rows, 1); - fs::write(&csv, "Date,User,Message\n2026-01-01,민수,replacement\n")?; - index_csv(&db, "방", &csv)?; - - let wal = fs::read(dir.path().join("context.sqlite3-wal")).unwrap_or_default(); - assert_eq!(wal.len(), 0); - let main_db = fs::read(&db)?; - assert!(!main_db - .windows(retired.len()) - .any(|window| window == retired.as_bytes())); - drop(concurrent_reader); - Ok(()) - } - - #[cfg(unix)] - #[test] - fn queue_loader_never_follows_a_symlink_or_creates_a_missing_queue() { - let dir = tempdir().unwrap(); - let queue = dir.path().join("reply-queue.sqlite3"); - { - let conn = Connection::open(&queue).unwrap(); - conn.execute_batch( - "CREATE TABLE reply_jobs(event_id TEXT, status TEXT, reply TEXT, updated_at REAL); - INSERT INTO reply_jobs VALUES('a','sent','보낸 문장',1000.0);", - ) - .unwrap(); - } - let before = fs::read(&queue).unwrap(); - assert_eq!(confirmed_self_sends_from_queue(&queue).unwrap().len(), 1); - assert_eq!(fs::read(&queue).unwrap(), before); - - let link = dir.path().join("linked-queue.sqlite3"); - std::os::unix::fs::symlink(&queue, &link).unwrap(); - assert!(confirmed_self_sends_from_queue(&link).unwrap().is_empty()); - - let missing_parent = dir.path().join("missing"); - let absent = missing_parent.join("reply-queue.sqlite3"); - assert!(confirmed_self_sends_from_queue(&absent).unwrap().is_empty()); - assert!(!missing_parent.exists()); - } - - fn style_profile_with_stats( - common_endings: &[(&str, usize)], - emoji_count: usize, - punctuation_count: usize, - common_tokens: &[(&str, usize)], - ) -> StyleProfile { - let endings: BTreeMap = common_endings - .iter() - .map(|(k, v)| (k.to_string(), *v)) - .collect(); - let tokens: BTreeMap = common_tokens - .iter() - .map(|(k, v)| (k.to_string(), *v)) - .collect(); - let sample_count = common_endings.iter().map(|(_, v)| *v).sum::().max(1); - StyleProfile { - chat: "부자멘토멘티".to_string(), - source: "local-db".to_string(), - user: STYLE_USER.to_string(), - sample_count, - average_character_length: 10.0, - median_character_length: 10.0, - p90_character_length: 12.0, - casual_ending_count: 0, - casual_ending_counts_json: "{}".to_string(), - question_count: 0, - emoji_count, - punctuation_count, - common_endings_json: serde_json::to_string(&endings).unwrap(), - common_tokens_json: serde_json::to_string(&tokens).unwrap(), - policy_version: STYLE_POLICY_VERSION.to_string(), - } - } - - #[test] - fn derive_deferential_endings_are_formal_honorific() { - let profile = - style_profile_with_stats(&[("습니다", 8), ("합니다", 4), ("요", 2)], 0, 5, &[]); - let (honorific, formality) = derive_style_profile_honorific_formality(&profile); - assert_eq!(honorific, Honorific::Honorific); - assert_eq!(formality, Formality::Formal); - } - - #[test] - fn derive_polite_with_emoji_is_informal_honorific() { - // 해요체 위주 + 이모지/축약이 섞이면 존댓말이지만 비격식체. - let profile = style_profile_with_stats(&[("요", 10), ("죠", 3)], 6, 2, &[("ㅋㅋ", 4)]); - let (honorific, formality) = derive_style_profile_honorific_formality(&profile); - assert_eq!(honorific, Honorific::Honorific); - assert_eq!(formality, Formality::Informal); - } - - #[test] - fn derive_casual_endings_are_informal_casual() { - let profile = style_profile_with_stats( - &[("어", 9), ("야", 4), ("지", 3), ("요", 1)], - 1, - 0, - &[("ㅋㅋㅋ", 5)], - ); - let (honorific, formality) = derive_style_profile_honorific_formality(&profile); - assert_eq!(honorific, Honorific::Casual); - assert_eq!(formality, Formality::Informal); - } - - #[test] - fn derive_no_ending_evidence_defaults_to_informal_casual() { - let profile = style_profile_with_stats(&[], 0, 0, &[]); - let (honorific, formality) = derive_style_profile_honorific_formality(&profile); - assert_eq!(honorific, Honorific::Casual); - assert_eq!(formality, Formality::Informal); - } - - #[test] - fn derive_deferential_overwhelmed_by_emoji_is_informal() { - // 합쇼체가 있어도 이모지·축약 신호가 우세하면 비격식체로 본다. - let profile = - style_profile_with_stats(&[("습니다", 2), ("요", 6)], 20, 1, &[("ㅋㅋㅋ", 10)]); - let (honorific, formality) = derive_style_profile_honorific_formality(&profile); - assert_eq!(honorific, Honorific::Honorific); - assert_eq!(formality, Formality::Informal); - } - - #[test] - fn contraction_token_detection() { - assert!(is_contraction_token("ㅋㅋ")); - assert!(is_contraction_token("ㅠㅠㅠ")); - assert!(!is_contraction_token("회의")); - assert!(!is_contraction_token("")); - } - - fn fixture(dir: &Path, name: &str, message: &str) -> PathBuf { - let path = dir.join(name); - let mut file = fs::File::create(&path).unwrap(); - writeln!(file, "Date,User,Message\n2026-01-01,민수,{}", message).unwrap(); - path - } - fn insert_context_row(conn: &Connection, source: &str, chat: &str, date: &str, message: &str) { - conn.execute( - "INSERT INTO context_messages( - source, chat, date, user_name, message, vector - ) VALUES (?1, ?2, ?3, '민수', ?4, ?5)", - params![ - source, - chat, - date, - message, - vector_to_bytes(&encode_vector(message)), - ], - ) - .unwrap(); - } - - fn live_event( - chat_id: i64, - log_id: i64, - sender_name: &str, - message: &str, - sent_at: i64, - ) -> LiveContextEvent { - LiveContextEvent { - chat_id, - log_id, - sender_name: sender_name.to_string(), - message: message.to_string(), - sent_at, - is_self: sender_name == STYLE_USER, - exclude_from_learning: false, - auto_generated: false, - attachment: String::new(), - message_type: 1, - interest_only: false, - } - } - - fn record_sent_reply(db: &Path, event_id: &str, chat: &str, reply: &str, sent_at: &str) { - let record = serde_json::json!({ - "event_id": event_id, - "chat": chat, - "author": "민수", - "received_at": "2026-01-01T00:00:00Z", - "message": format!("{event_id} incoming"), - "decision": "reply", - "reason": "direct_question", - "category": "question", - "context_match_count": 1, - "style_match_count": 1, - "best_context_score": 0.5, - "best_style_score": 0.5, - "prior_similarity": 0.0, - "scheduled_delay_seconds": 1.0, - "status": "scheduled", - "reply": reply, - "evidence_ids": ["context:test"], - "style_policy_version": STYLE_POLICY_VERSION, - }); - assert!(record_reply_decision(db, &record.to_string()).unwrap()); - assert!(update_reply_decision(db, event_id, "sent", Some(reply), Some(sent_at)).unwrap()); - } - - #[test] - fn conversational_style_filter_excludes_pasted_information() { - assert!(is_conversational_style_message("ㅋㅋㅋ 이건 좀 세긴 하네")); - assert!(!is_conversational_style_message("ㅋㅋ 이건 좀 세긴 하네")); - assert!(!is_conversational_style_message("ㅎㅎㅎ 이건 좀 세긴 하네")); - assert!(!is_conversational_style_message( - "1) 핵심 내용\n2) 세부 내용\n3) 참고 자료" - )); - assert!(!is_conversational_style_message( - "긴 정보 전달문 https://example.com 자세한 내용이 이어집니다" - )); - assert!(!is_conversational_style_message( - "이 문장은 실제 대화보다 정보 전달에 가까운 긴 문장이라서 말투 학습에서 제외해야 합니다." - )); - assert!(!is_conversational_style_message("1차 실무 면접")); - assert!(!is_conversational_style_message("사진")); - assert!(!is_conversational_style_message("@뉴스봇 환율")); - assert!(!is_conversational_style_message( - "문승현님이 부방장이 되었습니다." - )); - assert!(is_conversational_style_message("ㅇㅇ 저장해둘게")); - assert!(is_conversational_style_message( - "사용량 많으면 플러스가 낫긴 하지" - )); - assert!(!is_conversational_style_message("ㅇㅇ")); - assert!(!is_conversational_style_message("ㅇㅇㅋㅋㅋㅋ")); - assert!(!is_conversational_style_message( - "응, 사진 메시지도 확인해서 맥락에 맞게 답할 수 있어!" - )); - assert!(!is_conversational_style_message("대만 여행 알아보나 보네")); - assert!(!is_conversational_style_message( - "재정 관리 빡세게 하나보네" - )); - assert!(is_conversational_style_message( - "사용량 많으면 플러스가 낫긴 하지" - )); - } - #[test] - fn topic_lexicon_ascii_needles_are_lowercase() { - // `classify_message_topics` matches ASCII needles against the already - // lowercased haystack without re-lowercasing them (saves one String per - // needle per message). That shortcut is only correct while every ASCII - // needle is stored lowercase; this guards future lexicon edits. - for (topic, needles) in TOPIC_LEXICON { - for needle in *needles { - if needle.bytes().all(|byte| byte.is_ascii()) { - assert_eq!( - *needle, - needle.to_ascii_lowercase(), - "topic `{topic}` has a non-lowercase ASCII needle `{needle}`" - ); - } - } - } - } - - #[test] - fn assistant_tell_style_matches_case_insensitively() { - assert!(is_assistant_tell_style("I CAN HELP with that")); - assert!(is_assistant_tell_style("도와드릴 수 있어요")); - assert!(!is_assistant_tell_style("오늘 저녁에 뭐 해?")); - } - - #[test] - fn classify_message_topics_assigns_contact_and_ax() { - assert_eq!( - classify_message_topics("연락처 저장함 010-1234-5678"), - vec!["contact"] - ); - assert!(classify_message_topics("어제 번호 이야기해줫던거 머더라").contains(&"contact")); - assert!( - classify_message_topics("AX API가 손쉬운 사용으로 카톡 조작함").contains(&"ax_macos") - ); - assert!(classify_message_topics("토큰 부자ㄷㄷ").contains(&"llm_tools")); - assert!(classify_message_topics("ㅋㅋㅋ").is_empty()); - assert!(classify_message_topics("코스피 배당주 투자").contains(&"stocks")); - assert!(classify_message_topics("업비트 비트코인").contains(&"coins")); - assert!(classify_message_topics("전세 부동산 경매").contains(&"real_estate")); - assert!(classify_message_topics("전세 부동산 경매").contains(&"auction")); - assert!(classify_message_topics("그록 인공지능").contains(&"ai")); - assert!(message_has_interest_topic("공모주 넣었어요")); - assert!(!message_has_interest_topic("ㅋㅋㅋ")); - let indexed = live_index_text( - "사진", - r#"{"urls":["https://grok.com/supergrok"],"src_message":"67% 슈퍼그록"}"#, - 2, - ); - assert!(indexed.contains("https://grok.com/supergrok")); - assert!(indexed.contains("슈퍼그록")); - assert!(message_has_interest_topic(&indexed)); - } - - #[test] - fn interest_only_indexes_investing_and_skips_chatter() { - let dir = tempdir().unwrap(); - let db = dir.path().join("interest-only.sqlite3"); - let chat_id = 415878504092105; - let chat = "변우중"; - let mut keep = live_event(chat_id, 11, "변우중", "비트코인 지금 들어가도 됨?", 100); - keep.interest_only = true; - let mut skip = live_event(chat_id, 12, "변우중", "ㅋㅋㅋ", 110); - skip.interest_only = true; - let mut photo = live_event(chat_id, 13, "변우중", "사진", 120); - photo.interest_only = true; - photo.message_type = 2; - photo.attachment = r#"{"src_message":"강남 아파트 경매 보셈"}"#.into(); - ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &[keep, skip, photo], - true, - false, - ) - .unwrap(); - let conn = open_db(&db).unwrap(); - let messages: Vec = conn - .prepare("SELECT message FROM context_messages ORDER BY id") - .unwrap() - .query_map([], |row| row.get(0)) - .unwrap() - .collect::>() - .unwrap(); - assert_eq!(messages.len(), 2); - assert!(messages.iter().any(|row| row.contains("비트코인"))); - assert!(messages.iter().any(|row| row.contains("경매"))); - assert!(!messages.iter().any(|row| row == "ㅋㅋㅋ")); - let topics: Vec = conn - .prepare("SELECT DISTINCT topic FROM context_message_topics ORDER BY topic") - .unwrap() - .query_map([], |row| row.get(0)) - .unwrap() - .collect::>() - .unwrap(); - assert!(topics.iter().any(|topic| topic == "coins")); - } - - #[test] - fn topic_overlay_recalls_contact_thread_without_shared_keyword() { - let dir = tempdir().unwrap(); - let db = dir.path().join("topics.sqlite3"); - let path = dir.path().join("chat.csv"); - fs::write( - &path, - "Date,User,Message\n\ -2026-01-01 10:00:00,문승현,연락처 저장함 010-1234-5678\n\ -2026-01-01 10:00:10,최연우,ㅇㅇ 저장해둘게\n\ -2026-01-01 12:00:00,문승현,AX API가 손쉬운 사용으로 카톡 조작함\n\ -2026-01-01 12:00:05,최연우,컴퓨터 유즈보다 AX가 나음\n\ -2026-01-01 13:00:00,문승현,토큰 부자ㄷㄷ\n", - ) - .unwrap(); - assert_eq!(index_csv(&db, "부자멘토멘티", &path).unwrap(), 5); - - let recalled = search( - &db, - Some("부자멘토멘티"), - None, - "어제 번호 이야기해줫던거 머더라", - "hybrid", - 5, - ) - .unwrap(); - assert!( - recalled - .iter() - .any(|row| row.message.contains("010-") || row.message.contains("연락처")), - "{recalled:?}" - ); - - let ax = search( - &db, - Some("부자멘토멘티"), - None, - "AX API 손쉬운 사용", - "hybrid", - 5, - ) - .unwrap(); - assert!( - ax.iter() - .any(|row| row.message.contains("손쉬운 사용") || row.message.contains("AX")), - "{ax:?}" - ); - assert!(!ax.iter().any(|row| row.message.contains("010-")), "{ax:?}"); - - let conn = open_db(&db).unwrap(); - conn.execute("DELETE FROM context_message_topics", []) - .unwrap(); - conn.execute("DELETE FROM context_topic_stats", []).unwrap(); - conn.execute( - "UPDATE context_retrieval_meta SET value = '1' WHERE key = 'live_context_schema'", - [], - ) - .unwrap(); - drop(conn); - let conn = open_db(&db).unwrap(); - let version: String = conn - .query_row( - "SELECT value FROM context_retrieval_meta WHERE key = 'live_context_schema'", - [], - |row| row.get(0), - ) - .unwrap(); - assert_eq!(version, LIVE_CONTEXT_SCHEMA_VERSION); - let n: i64 = conn - .query_row("SELECT COUNT(*) FROM context_message_topics", [], |row| { - row.get(0) - }) - .unwrap(); - assert!(n >= 3, "backfill topics {n}"); - let contact: i64 = conn - .query_row( - "SELECT message_count FROM context_topic_stats - WHERE chat = '부자멘토멘티' AND topic = 'contact'", - [], - |row| row.get(0), - ) - .unwrap(); - assert!(contact >= 1, "contact stats {contact}"); - } - - #[test] - fn indexes_and_scopes_by_chat_and_source() { - let dir = tempdir().unwrap(); - let db = dir.path().join("index.sqlite3"); - let first = fixture(dir.path(), "first.csv", "세금 신고 일정"); - let second = fixture(dir.path(), "second.csv", "점심 김치찌개"); - assert_eq!(index_csv(&db, "같은 이름", &first).unwrap(), 1); - assert_eq!(index_csv(&db, "같은 이름", &second).unwrap(), 1); - assert_eq!( - search(&db, Some("같은 이름"), None, "세금", "keyword", 5) - .unwrap() - .len(), - 1 - ); - assert_eq!( - search( - &db, - Some("같은 이름"), - Some(&second.canonicalize().unwrap().display().to_string()), - "세금", - "keyword", - 5 - ) - .unwrap() - .len(), - 0 - ); - } - #[test] - fn indexes_choi_style_rows_in_separate_vector_table() { - let dir = tempdir().unwrap(); - let db = dir.path().join("index.sqlite3"); - let path = dir.path().join("chat.csv"); - fs::write( - &path, - "Date,User,Message\n2026-01-01,최연우,ㅋㅋㅋ 이건 좀 세긴 하네\n2026-01-01,문승현,다른 사람 메시지\n", - ) - .unwrap(); - - assert_eq!(index_csv(&db, "부자멘토멘티", &path).unwrap(), 2); - let results = style_search(&db, Some("부자멘토멘티"), "세긴 하네", 5).unwrap(); - assert_eq!(results.len(), 1); - assert_eq!(results[0].user, STYLE_USER); - assert_eq!(results[0].message, "ㅋㅋㅋ 이건 좀 세긴 하네"); - - let conn = open_db(&db).unwrap(); - let count: i64 = conn - .query_row("SELECT COUNT(*) FROM owner_style", [], |row| row.get(0)) - .unwrap(); - assert_eq!(count, 1); - } - - #[test] - fn indexes_and_reads_response_time_from_vector_db() { - let dir = tempdir().unwrap(); - let db = dir.path().join("index.sqlite3"); - let path = dir.path().join("chat.csv"); - fs::write( - &path, - "Date,User,Message\n\ - 2026-01-01 00:00:00,문승현,질문 하나\n\ - 2026-01-01 00:00:10,최연우,답변 하나\n\ - 2026-01-01 00:01:00,민수,질문 둘\n\ - 2026-01-01 00:03:00,최연우,답변 둘\n", - ) - .unwrap(); - - index_csv(&db, "부자멘토멘티", &path).unwrap(); - let stats = response_time_stats(&db, "부자멘토멘티", STYLE_USER, None) - .unwrap() - .unwrap(); - assert_eq!(stats.sample_count, 2); - assert!((stats.average_seconds - 65.0).abs() < f64::EPSILON); - assert!((stats.median_seconds - 65.0).abs() < f64::EPSILON); - assert_eq!(stats.p90_seconds, 109.0); - assert_eq!(stats.min_seconds, 10.0); - assert_eq!(stats.max_seconds, 120.0); - assert!(stats.stddev_seconds > 0.0); - assert!(stats.distribution.is_none()); - } - - #[test] - fn response_time_distribution_finds_three_empirical_modes() { - let mut delays = Vec::new(); - delays.extend((0..16).map(|index| index as f64)); - delays.extend((0..8).map(|index| 40.0 + index as f64 * 10.0)); - delays.extend((0..8).map(|index| 500.0 + index as f64 * 100.0)); - let stats = summarize_response_delays("방", "source", STYLE_USER, delays).unwrap(); - let distribution = stats.distribution.unwrap(); - assert_eq!(distribution.schema_version, 2); - assert_eq!(distribution.model_kind, "bounded-normal-mixture"); - assert_eq!(distribution.sample_count, 32); - assert_eq!(distribution.retained_sample_count, 32); - assert_eq!(distribution.components.len(), 3); - assert_eq!(distribution.components[0].name, "immediate"); - assert_eq!(distribution.components[1].name, "short"); - assert_eq!(distribution.components[2].name, "delayed"); - assert_eq!( - distribution.components[0].sample_count - + distribution.components[1].sample_count - + distribution.components[2].sample_count, - 32 - ); - assert_eq!( - distribution.components[0].upper_seconds, - distribution.split_seconds[0] - ); - assert_eq!( - distribution.components[1].upper_seconds, - distribution.split_seconds[1] - ); - assert_eq!( - distribution.components[2].upper_seconds, - distribution.global_upper_seconds - ); - assert_eq!(distribution.split_seconds, vec![15.0, 110.0]); - assert!((distribution.global_upper_seconds - 890.0).abs() < 1e-9); - assert_eq!(distribution.tail_winsorized_count, 4); - assert_eq!(distribution.components[0].weight, 0.5); - assert_eq!(distribution.components[1].weight, 0.25); - assert_eq!(distribution.components[2].weight, 0.25); - assert!(response_time_distribution_is_valid(&distribution)); - } - - #[test] - fn response_time_distribution_fails_closed_for_weak_or_degenerate_samples() { - assert!(fit_response_time_distribution(&vec![1.0; 31]).is_none()); - assert!(fit_response_time_distribution(&vec![1.0; 32]).is_none()); - let mut only_two_modes = vec![1.0; 16]; - only_two_modes.extend(vec![100.0; 16]); - assert!(fit_response_time_distribution(&only_two_modes).is_none()); - } - - /// Exhaustive reference for the shipped exact split search. Kept in the - /// test module so every randomized case can assert the optimized path - /// returns the identical distribution. - fn exhaustive_response_time_distribution(delays: &[f64]) -> Option { - if delays.len() < RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES - || delays.iter().any(|delay| { - !delay.is_finite() || !(0.0..=MAX_RESPONSE_DELAY_SECONDS as f64).contains(delay) - }) - { - return None; - } - let transformed = delays.iter().map(|delay| delay.ln_1p()).collect::>(); - let mut prefix_sum = vec![0.0; transformed.len() + 1]; - let mut prefix_square_sum = vec![0.0; transformed.len() + 1]; - for (index, value) in transformed.iter().enumerate() { - prefix_sum[index + 1] = prefix_sum[index] + value; - prefix_square_sum[index + 1] = prefix_square_sum[index] + value * value; - } - let segment_sse = |start: usize, end: usize| { - let count = (end - start) as f64; - let sum = prefix_sum[end] - prefix_sum[start]; - let square_sum = prefix_square_sum[end] - prefix_square_sum[start]; - (square_sum - sum * sum / count).max(0.0) - }; - let mut best: Option<(f64, usize, usize)> = None; - let minimum = RESPONSE_TIME_DISTRIBUTION_MIN_COMPONENT_SAMPLES; - for first_split in minimum..=delays.len() - 2 * minimum { - if delays[first_split - 1] >= delays[first_split] { - continue; - } - for second_split in first_split + minimum..=delays.len() - minimum { - if delays[second_split - 1] >= delays[second_split] { - continue; - } - let sse = segment_sse(0, first_split) - + segment_sse(first_split, second_split) - + segment_sse(second_split, delays.len()); - if best.as_ref().is_none_or(|(best_sse, _, _)| sse < *best_sse) { - best = Some((sse, first_split, second_split)); - } - } - } - let (_, first_split, second_split) = best?; - let global_upper_seconds = response_delay_percentile(delays, 0.9); - let immediate_upper_seconds = delays[first_split - 1].min(global_upper_seconds); - let short_lower_seconds = delays[first_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); - let short_upper_seconds = delays[second_split - 1].min(global_upper_seconds); - let delayed_lower_seconds = delays[second_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); - if immediate_upper_seconds < MIN_SCHEDULED_RESPONSE_DELAY_SECONDS - || short_lower_seconds > short_upper_seconds - || delayed_lower_seconds > global_upper_seconds - || immediate_upper_seconds >= short_lower_seconds - || short_upper_seconds >= delayed_lower_seconds - { - return None; - } - let bounds = [ - ( - MIN_SCHEDULED_RESPONSE_DELAY_SECONDS, - immediate_upper_seconds, - ), - (short_lower_seconds, short_upper_seconds), - (delayed_lower_seconds, global_upper_seconds), - ]; - let ranges = [ - (0, first_split), - (first_split, second_split), - (second_split, delays.len()), - ]; - let mut components = Vec::with_capacity(RESPONSE_TIME_DISTRIBUTION_COMPONENTS); - for ((start, end), ((lower_seconds, upper_seconds), name)) in ranges - .into_iter() - .zip(bounds.into_iter().zip(["immediate", "short", "delayed"])) - { - let values = delays[start..end] - .iter() - .map(|delay| delay.clamp(lower_seconds, upper_seconds)) - .collect::>(); - let mean = values.iter().sum::() / values.len() as f64; - let stddev = (values - .iter() - .map(|value| (value - mean).powi(2)) - .sum::() - / values.len() as f64) - .sqrt(); - if !stddev.is_finite() || stddev <= 0.0 { - return None; - } - components.push(ResponseTimeComponent { - name: name.to_string(), - sample_count: values.len(), - weight: values.len() as f64 / delays.len() as f64, - normal_location_seconds: mean, - normal_scale_seconds: stddev, - lower_seconds, - upper_seconds, - }); - } - let distribution = ResponseTimeDistribution { - schema_version: RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION, - policy_version: RESPONSE_TIME_DISTRIBUTION_POLICY_VERSION.to_string(), - model_kind: RESPONSE_TIME_DISTRIBUTION_MODEL_KIND.to_string(), - fit_transform: RESPONSE_TIME_DISTRIBUTION_FIT_TRANSFORM.to_string(), - sample_count: delays.len(), - retained_sample_count: delays.len(), - tail_winsorized_count: delays - .iter() - .filter(|delay| **delay > global_upper_seconds) - .count(), - split_seconds: vec![immediate_upper_seconds, short_upper_seconds], - global_upper_seconds, - components, - }; - response_time_distribution_is_valid(&distribution).then_some(distribution) - } - - fn deterministic_delays(seed: u64, count: usize) -> Vec { - let mut state = seed - .wrapping_mul(6364136223846793005) - .wrapping_add(1442695040888963407); - let mut next = move || { - state = state - .wrapping_mul(6364136223846793005) - .wrapping_add(1442695040888963407); - (state >> 11) as f64 / (1u64 << 53) as f64 - }; - let mut values = (0..count) - .map(|index| { - // Three latent modes with heavy duplicates so the strict-increase - // boundary rule and the tie-break both get exercised. - let mode = index % 3; - let base = match mode { - 0 => 12.0, - 1 => 90.0, - _ => 900.0, - }; - let jitter = (next() * 40.0).floor(); - let duplicate = if next() < 0.35 { 0.0 } else { 1.0 }; - base + jitter * duplicate - }) - .collect::>(); - values.sort_by(|left, right| left.partial_cmp(right).unwrap()); - values - } - - #[test] - fn response_time_distribution_split_search_matches_exhaustive() { - for count in [32usize, 33, 40, 100, 257, 1000] { - for seed in 1u64..=8 { - let delays = deterministic_delays(seed, count); - let fast = fit_response_time_distribution(&delays); - let reference = exhaustive_response_time_distribution(&delays); - assert_eq!( - fast.is_some(), - reference.is_some(), - "presence diverged at count={count} seed={seed}" - ); - let (Some(fast), Some(reference)) = (fast, reference) else { - continue; - }; - assert_eq!( - fast.split_seconds, reference.split_seconds, - "split diverged at count={count} seed={seed}" - ); - assert_eq!(fast.global_upper_seconds, reference.global_upper_seconds); - assert_eq!(fast.tail_winsorized_count, reference.tail_winsorized_count); - assert_eq!(fast.sample_count, reference.sample_count); - for (optimized, naive) in fast.components.iter().zip(reference.components.iter()) { - assert_eq!(optimized.name, naive.name); - assert_eq!(optimized.sample_count, naive.sample_count); - assert_eq!(optimized.weight, naive.weight); - assert_eq!( - optimized.normal_location_seconds, - naive.normal_location_seconds - ); - assert_eq!(optimized.normal_scale_seconds, naive.normal_scale_seconds); - assert_eq!(optimized.lower_seconds, naive.lower_seconds); - assert_eq!(optimized.upper_seconds, naive.upper_seconds); - } - } - } - } - - #[test] - fn response_time_distribution_split_search_handles_plateaus() { - // A fully flat sample set has no strict-increase boundary, so the fit - // must fail closed exactly like the exhaustive scan. - assert!(fit_response_time_distribution(&vec![7.0; 64]).is_none()); - // Duplicate runs at the boundaries leave only a narrow strict-increase - // window; the optimized search must still find the same pair. - let mut delays = vec![3.0; 12]; - delays.extend((0..12).map(|index| 20.0 + index as f64)); - delays.extend(vec![400.0; 12]); - delays.extend((0..12).map(|index| 800.0 + index as f64)); - assert_eq!( - fit_response_time_distribution(&delays).map(|item| item.split_seconds), - exhaustive_response_time_distribution(&delays).map(|item| item.split_seconds), - ); - // Heavy duplication ties many candidate pairs on SSE, so the tie-break - // (lowest first then lowest second) is what keeps the two paths equal. - for seed in 1u64..=16 { - let delays = duplicate_heavy_delays(seed, 200); - assert_eq!( - fit_response_time_distribution(&delays).map(|item| item.split_seconds), - exhaustive_response_time_distribution(&delays).map(|item| item.split_seconds), - "tie-break diverged at seed={seed}" - ); - } - } - - fn duplicate_heavy_delays(seed: u64, count: usize) -> Vec { - let mut state = seed - .wrapping_mul(2862933555777941757) - .wrapping_add(3037000493); - let mut next = move || { - state = state - .wrapping_mul(2862933555777941757) - .wrapping_add(3037000493); - (state >> 33) as f64 / (1u64 << 31) as f64 - }; - // Only six distinct levels, so most segment SSEs tie and the boundary - // rule plus tie-break decide the winner. - let levels = [10.0, 30.0, 30.0, 120.0, 600.0, 600.0]; - let mut values = (0..count) - .map(|_| levels[(next() * levels.len() as f64) as usize % levels.len()]) - .collect::>(); - values.sort_by(|left, right| left.partial_cmp(right).unwrap()); - values - } - - #[test] - fn response_time_distribution_split_search_scales_to_live_sample_counts() { - // The live chats carry up to ~20k response samples, where the previous - // O(N^2) pair scan was the slow path. A dense continuous shape is the - // worst case for it, because no duplicate run removes candidate - // boundaries: the exhaustive scan takes ~660 ms at 20,174 samples on - // this machine, and the O(N log N) search takes ~2.4 ms. The bound - // below is 100x the measured search while still failing an O(N^2) - // regression. - let delays = shaped_delays(99, 20_000, 2); - let started = std::time::Instant::now(); - let fitted = fit_response_time_distribution(&delays).expect("live shape must fit"); - let elapsed = started.elapsed(); - assert_eq!(fitted.sample_count, 20_000); - assert!( - elapsed < std::time::Duration::from_millis(250), - "exact split search took {elapsed:?}" - ); - } - - #[test] - fn response_time_distribution_split_search_matches_exhaustive_across_shapes() { - // The divide-and-conquer split search rests on the quadrangle - // inequality of the segment SSE, a property of sorted shapes rather - // than of arbitrary inputs, so the equivalence is pinned against the - // exhaustive scan over a wide battery instead of assumed: every - // strict-increase window, every plateau, and every duplicate tie is - // compared on both paths, at counts around the minimum window (32, 33, - // 34) as well as at live scale. - for seed in 1u64..=24 { - for count in [32usize, 33, 34, 40, 47, 55, 64, 96, 150, 311, 640, 1000] { - for shape in 0..6u64 { - let delays = shaped_delays(seed * 7 + shape, count, shape); - let fast = fit_response_time_distribution(&delays); - let reference = exhaustive_response_time_distribution(&delays); - assert_eq!( - fast.is_some(), - reference.is_some(), - "presence diverged at count={count} seed={seed} shape={shape}" - ); - let (Some(fast), Some(reference)) = (fast, reference) else { - continue; - }; - assert_eq!( - fast.split_seconds, reference.split_seconds, - "split diverged at count={count} seed={seed} shape={shape}" - ); - assert_eq!(fast.global_upper_seconds, reference.global_upper_seconds); - assert_eq!(fast.tail_winsorized_count, reference.tail_winsorized_count); - for (optimized, naive) in - fast.components.iter().zip(reference.components.iter()) - { - assert_eq!(optimized.name, naive.name); - assert_eq!(optimized.sample_count, naive.sample_count); - assert_eq!(optimized.weight, naive.weight); - assert_eq!( - optimized.normal_location_seconds, - naive.normal_location_seconds - ); - assert_eq!(optimized.normal_scale_seconds, naive.normal_scale_seconds); - assert_eq!(optimized.lower_seconds, naive.lower_seconds); - assert_eq!(optimized.upper_seconds, naive.upper_seconds); - } - } - } - } - } - - /// Six sorted shapes: three equal modes, a coarse staircase, a dense - /// uniform spread, a heavy-tail sweep, a six-level duplicate ladder, and a - /// stair of long duplicate runs. The last two collapse most candidate - /// boundaries, so they exercise the fail-closed paths and the tie-break. - fn shaped_delays(seed: u64, count: usize, shape: u64) -> Vec { - let mut state = seed - .wrapping_mul(6364136223846793005) - .wrapping_add(1442695040888963407); - let mut next = move || { - state = state - .wrapping_mul(6364136223846793005) - .wrapping_add(1442695040888963407); - (state >> 11) as f64 / (1u64 << 53) as f64 - }; - let mut values = (0..count) - .map(|index| match shape { - 0 => { - let base = match index % 3 { - 0 => 8.0, - 1 => 90.0, - _ => 700.0, - }; - base + (next() * 30.0).floor() - } - 1 => (index / 4) as f64 * 25.0 + (next() * 6.0).floor(), - 2 => next() * 900.0, - 3 => (next().powf(2.5)) * 900.0, - 4 => { - const LEVELS: [f64; 6] = [10.0, 30.0, 30.0, 120.0, 600.0, 600.0]; - LEVELS[(next() * LEVELS.len() as f64) as usize % LEVELS.len()] - } - _ => (index / 6) as f64 * 120.0, - }) - .collect::>(); - values.sort_by(|left, right| left.partial_cmp(right).unwrap()); - values - } - - #[test] - fn response_time_distribution_migration_is_additive_backfills_and_repairs() { - let dir = tempdir().unwrap(); - let db = dir.path().join("migration.sqlite3"); - let legacy = Connection::open(&db).unwrap(); - legacy - .execute_batch( - "CREATE TABLE response_time_stats( - chat TEXT NOT NULL, source TEXT NOT NULL, user_name TEXT NOT NULL, - sample_count INTEGER NOT NULL, average_seconds REAL NOT NULL, - median_seconds REAL NOT NULL, p90_seconds REAL NOT NULL, - min_seconds REAL NOT NULL, max_seconds REAL NOT NULL, - max_window_seconds INTEGER NOT NULL, - stddev_seconds REAL NOT NULL DEFAULT 0.0, - PRIMARY KEY(chat, source, user_name) - );", - ) - .unwrap(); - drop(legacy); - let conn = open_db(&db).unwrap(); - let columns = conn - .prepare("PRAGMA table_info(response_time_stats)") - .unwrap() - .query_map([], |row| row.get::<_, String>(1)) - .unwrap() - .collect::>>() - .unwrap(); - assert!(columns.contains(&"distribution_schema_version".to_string())); - assert!(columns.contains(&"distribution_json".to_string())); - let source = "local-db:migration"; - conn.execute( - "INSERT INTO context_sources( - source,kind,account_fingerprint,chat_id,chat,authoritative, - checkpoint_log_id,pending_burst_json,summary_dirty,sync_status,updated_at - ) VALUES(?1,'local_db',?2,42,'방',1,64,'[]',0,'ready',?3)", - params![source, TEST_ACCOUNT_FINGERPRINT, Utc::now().to_rfc3339()], - ) - .unwrap(); - let mut delays = Vec::new(); - delays.extend((0..16).map(|index| index as f64)); - delays.extend((0..8).map(|index| 40.0 + index as f64 * 10.0)); - delays.extend((0..8).map(|index| 500.0 + index as f64 * 100.0)); - for (index, delay) in delays.iter().enumerate() { - conn.execute( - "INSERT INTO response_time_samples( - source,chat_id,reply_log_id,prompt_log_id,recipient,delay_seconds - ) VALUES(?1,42,?2,?3,'민수',?4)", - params![source, 100 + index as i64, 10 + index as i64, delay], - ) - .unwrap(); - } - conn.execute( - "INSERT INTO response_time_stats( - chat,source,user_name,sample_count,average_seconds,median_seconds, - p90_seconds,min_seconds,max_seconds,max_window_seconds,stddev_seconds, - distribution_schema_version,distribution_json - ) VALUES('방',?1,?2,32,0,0,890,0,1200,86400,1,1,'{}')", - params![source, STYLE_USER], - ) - .unwrap(); - conn.execute( - "INSERT INTO context_retrieval_meta(key,value) - VALUES('response_time_distribution_schema',?1) - ON CONFLICT(key) DO UPDATE SET value=excluded.value", - [RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION.to_string()], - ) - .unwrap(); - drop(conn); - - let repaired = open_db(&db).unwrap(); - let (version, json): (i64, String) = repaired - .query_row( - "SELECT distribution_schema_version,distribution_json - FROM response_time_stats WHERE source=?1", - [source], - |row| Ok((row.get(0)?, row.get(1)?)), - ) - .unwrap(); - assert_eq!(version, RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION as i64); - let distribution: ResponseTimeDistribution = serde_json::from_str(&json).unwrap(); - assert!(response_time_distribution_is_valid(&distribution)); - drop(repaired); - let reopened = open_db(&db).unwrap(); - let unchanged: String = reopened - .query_row( - "SELECT distribution_json FROM response_time_stats WHERE source=?1", - [source], - |row| row.get(0), - ) - .unwrap(); - assert_eq!(unchanged, json); - } - #[test] - fn reindex_removes_old_rows_and_vector_is_deterministic() { - let dir = tempdir().unwrap(); - let db = dir.path().join("index.sqlite3"); - let path = fixture(dir.path(), "chat.csv", "old phrase"); - index_csv(&db, "방", &path).unwrap(); - fs::write(&path, "Date,User,Message\n2026-01-01,민수,new phrase\n").unwrap(); - index_csv(&db, "방", &path).unwrap(); - assert!(search(&db, Some("방"), None, "old", "keyword", 5) - .unwrap() - .is_empty()); - assert_eq!(encode_vector("same"), encode_vector("same")); - } - - #[test] - fn relative_db_and_invalid_vector_query_are_safe() { - let dir = tempdir().unwrap(); - let db = dir.path().join("relative.sqlite3"); - let path = fixture(dir.path(), "chat.csv", "hello"); - index_csv(&db, "방", &path).unwrap(); - // Token-less queries (`!!!`) no longer fail the whole vector lookup: - // they return an empty hit list so style/timing bundles keep flowing. - let hits = search(&db, Some("방"), None, "!!!", "vector", 5).unwrap(); - assert!(hits.is_empty()); - } - #[test] - fn records_and_searches_reply_decisions() { - let dir = tempdir().unwrap(); - let db = dir.path().join("index.sqlite3"); - let record = serde_json::json!({ - "event_id": "event-1", - "chat": "부자멘토멘티", - "author": "민수", - "received_at": "2026-01-01T00:00:00Z", - "message": "세금 신고 일정이 궁금해", - "decision": "reply", - "reason": "direct_question", - "category": "question", - "context_match_count": 2, - "style_match_count": 3, - "best_context_score": 0.8, - "best_style_score": 0.9, - "prior_similarity": 0.0, - "scheduled_delay_seconds": 15.0, - "status": "scheduled", - "reply": "지난번처럼 일정 확인해보면 돼", - "evidence_ids": [ - "context:abc", - "style:def", - "timing:2:empirical-log1p-three-means-p90-v1:immediate:w0.5:lo5:hi17" - ], - "style_policy_version": "ordinary-conversation-v3", - "provenance": { - "rerank": {"fallback": "none", "policy_rejections": []}, - "response_timing": {"component": "immediate", "component_weight": 0.5} - } - }); - record_reply_decision(&db, &record.to_string()).unwrap(); - let matches = reply_decision_search(&db, "부자멘토멘티", "세금 신고", 5).unwrap(); - assert_eq!(matches.len(), 1); - assert_eq!(matches[0].decision, "reply"); - assert_eq!(matches[0].status, "scheduled"); - assert!(update_reply_decision( - &db, - "event-1", - "sent", - Some("답변 완료"), - Some("2026-01-01T00:00:15Z") - ) - .unwrap()); - let updated = reply_decision_search(&db, "부자멘토멘티", "세금 신고", 5).unwrap(); - assert_eq!(updated[0].status, "sent"); - assert!(updated[0] - .evidence_json - .contains("ordinary-conversation-v3")); - assert!(updated[0] - .evidence_json - .contains("timing:2:empirical-log1p-three-means-p90-v1:immediate:w0.5:lo5:hi17")); - assert!(updated[0].evidence_json.contains("response_timing")); - assert!(updated[0].evidence_json.contains("immediate")); - } - #[test] - fn reply_decision_boundary_validation_and_stale_noop() { - let dir = tempdir().unwrap(); - let db = dir.path().join("reply-validation.sqlite3"); - let base = serde_json::json!({ - "event_id": "event-1", - "chat": "chat-a", - "author": "민수", - "received_at": "2026-01-01T00:00:00Z", - "message": "질문", - "decision": "reply", - "reason": "question", - "category": "question", - "context_match_count": 1, - "style_match_count": 1, - "best_context_score": 0.5, - "best_style_score": 0.5, - "prior_similarity": 0.5, - "scheduled_delay_seconds": 1.0, - "status": "scheduled", - "reply": "답변", - "evidence_ids": ["context:1"], - "style_policy_version": STYLE_POLICY_VERSION, - }); - let mut invalid = base.clone(); - invalid["evidence_ids"] = serde_json::json!([]); - assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); - let mut invalid = base.clone(); - invalid["style_policy_version"] = serde_json::json!("legacy-v1"); - assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); - let mut invalid = base.clone(); - invalid["best_context_score"] = serde_json::json!(-0.1); - assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); - let mut invalid = base.clone(); - invalid["scheduled_delay_seconds"] = serde_json::json!(-1.0); - assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); - for reply in ["ㅋ", "ㅋㅋ", "ㅎ", "ㅎㅎ", "ㅎㅎㅎ"] { - let mut invalid = base.clone(); - invalid["reply"] = serde_json::json!(reply); - assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); - } - - assert!(record_reply_decision(&db, &base.to_string()).unwrap()); - assert!(update_reply_decision(&db, "event-1", "sent", Some("ㅎㅎㅎ"), None).is_err()); - let mut stale = base; - stale["status"] = serde_json::json!("pending"); - assert!(!record_reply_decision(&db, &stale.to_string()).unwrap()); - - let mut skip = serde_json::json!({ - "event_id": "skip-1", - "chat": "chat-a", - "author": "민수", - "received_at": "2026-01-01T00:00:00Z", - "message": "공지", - "decision": "skip", - "reason": "not_actionable", - "category": "other", - "context_match_count": 0, - "style_match_count": 0, - "best_context_score": 0.0, - "best_style_score": 0.0, - "prior_similarity": 0.0, - "scheduled_delay_seconds": 0.0, - "status": "skipped", - "reply": null, - "evidence_ids": [], - "style_policy_version": "", - }); - assert!(record_reply_decision(&db, &skip.to_string()).unwrap()); - skip["evidence_ids"] = serde_json::json!([""]); - assert!(record_reply_decision(&db, &skip.to_string()).is_err()); - } - - #[test] - fn terminal_skip_clears_a_previously_scheduled_reply_and_sent_marker() { - let dir = tempdir().unwrap(); - let db = dir.path().join("reply-terminal-skip.sqlite3"); - let mut record = serde_json::json!({ - "event_id": "event-skip", - "chat": "chat-a", - "author": "민수", - "received_at": "2026-01-01T00:00:00Z", - "message": "질문", - "decision": "reply", - "reason": "question", - "category": "question", - "context_match_count": 1, - "style_match_count": 1, - "best_context_score": 0.5, - "best_style_score": 0.5, - "prior_similarity": 0.5, - "scheduled_delay_seconds": 1.0, - "status": "scheduled", - "reply": "답변", - "evidence_ids": ["context:1"], - "style_policy_version": STYLE_POLICY_VERSION, - }); - assert!(record_reply_decision(&db, &record.to_string()).unwrap()); - let conn = Connection::open(&db).unwrap(); - conn.execute( - "UPDATE reply_decisions SET sent_at='2026-01-01T00:00:01Z' WHERE event_id='event-skip'", - [], - ) - .unwrap(); - drop(conn); - - record["decision"] = serde_json::json!("skip"); - record["reason"] = serde_json::json!("stale_backlog"); - record["category"] = serde_json::json!("policy"); - record["scheduled_delay_seconds"] = serde_json::json!(0.0); - record["status"] = serde_json::json!("skipped"); - record["reply"] = serde_json::Value::Null; - record["evidence_ids"] = serde_json::json!([]); - record["style_policy_version"] = serde_json::json!(""); - assert!(record_reply_decision(&db, &record.to_string()).unwrap()); - - let conn = Connection::open(&db).unwrap(); - let row = conn - .query_row( - "SELECT decision,status,reason,category,reply,sent_at FROM reply_decisions WHERE event_id='event-skip'", - [], - |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, String>(1)?, - row.get::<_, String>(2)?, - row.get::<_, String>(3)?, - row.get::<_, Option>(4)?, - row.get::<_, Option>(5)?, - )) - }, - ) - .unwrap(); - assert_eq!( - row, - ( - "skip".to_string(), - "skipped".to_string(), - "stale_backlog".to_string(), - "policy".to_string(), - None, - None, - ) - ); - } - - #[test] - fn exact_reply_decisions_precede_bounded_similarity_candidates() { - let dir = tempdir().unwrap(); - let db = dir.path().join("reply-exact.sqlite3"); - let make_record = |event_id: String, message: String| { - serde_json::json!({ - "event_id": event_id, - "chat": "chat-a", - "author": "민수", - "received_at": "2026-01-01T00:00:00Z", - "message": message, - "decision": "skip", - "reason": "not_actionable", - "category": "other", - "context_match_count": 0, - "style_match_count": 0, - "best_context_score": 0.0, - "best_style_score": 0.0, - "prior_similarity": 0.0, - "scheduled_delay_seconds": 0.0, - "status": "skipped", - "reply": null, - "evidence_ids": [], - "style_policy_version": "", - }) - }; - record_reply_decision( - &db, - &make_record("exact-old".into(), "target duplicate".into()).to_string(), - ) - .unwrap(); - for index in 0..(REPLY_DECISION_CANDIDATE_CAP + 16) { - record_reply_decision( - &db, - &make_record( - format!("event-{index:03}"), - format!("other message {index}"), - ) - .to_string(), - ) - .unwrap(); - } - let results = reply_decision_search(&db, "chat-a", "target duplicate", 5).unwrap(); - assert_eq!( - results.first().map(|result| result.event_id.as_str()), - Some("exact-old") - ); - assert_eq!(results.first().map(|result| result.score), Some(1.0)); - } - - #[test] - fn reply_projection_reports_updates_and_rejects_regression() { - let dir = tempdir().unwrap(); - let db = dir.path().join("reply.sqlite3"); - let first = - project_reply_decision(&db, "db:7:42", "scheduled", r#"{"source":"db"}"#).unwrap(); - assert!(first.applied); - assert!(first.updated); - let sent = project_reply_decision(&db, "db:7:42", "sent", r#"{"confirmed":true}"#).unwrap(); - assert!(sent.updated); - let stale = project_reply_decision(&db, "db:7:42", "pending", "{}").unwrap(); - assert!(!stale.updated); - assert_eq!( - get_reply_decision(&db, "db:7:42").unwrap().unwrap().status, - "sent" - ); - } - #[test] - fn delivery_unknown_and_reconcile_required_are_terminal() { - let dir = tempdir().unwrap(); - let db = dir.path().join("reply-terminal.sqlite3"); - - for (index, terminal) in ["delivery_unknown", "reconcile_required"] - .into_iter() - .enumerate() - { - let event_id = format!("terminal-{index}"); - assert!( - project_reply_decision(&db, &event_id, "scheduled", "{}") - .unwrap() - .updated - ); - let terminal_update = - project_reply_decision(&db, &event_id, terminal, r#"{"unknown":true}"#).unwrap(); - assert!(terminal_update.applied); - assert!(terminal_update.updated); - - let idempotent = - project_reply_decision(&db, &event_id, terminal, r#"{"unknown":true}"#).unwrap(); - assert!(idempotent.applied); - assert!(!idempotent.updated); - - for next_status in [ - "sent", - "skipped", - "poison", - "delivery_unknown", - "reconcile_required", - ] { - if next_status == terminal { - continue; - } - let blocked = project_reply_decision(&db, &event_id, next_status, "{}").unwrap(); - assert!(!blocked.applied); - assert!(!blocked.updated); - assert_eq!( - blocked.reason.as_deref(), - Some("terminal_or_newer_status_cannot_regress") - ); - assert!(!update_reply_decision(&db, &event_id, next_status, None, None).unwrap()); - } - } - } - - #[test] - fn retrieval_families_require_current_migration_marker() { - let dir = tempdir().unwrap(); - let db = dir.path().join("retrieval-marker.sqlite3"); - let path = fixture(dir.path(), "chat.csv", "marker query"); - index_csv(&db, "chat-a", &path).unwrap(); - - let conn = open_db(&db).unwrap(); - conn.execute( - "UPDATE context_retrieval_meta SET value = 'stale' WHERE key = 'fts_schema'", - [], - ) - .unwrap(); - drop(conn); - - let assert_migration_required = |result: Result<()>| { - assert_eq!( - result.unwrap_err().to_string(), - CONTEXT_RETRIEVAL_MIGRATION_REQUIRED - ); - }; - assert_migration_required( - search(&db, Some("chat-a"), None, "marker", "keyword", 5).map(|_| ()), - ); - assert_migration_required(style_search(&db, Some("chat-a"), "marker", 5).map(|_| ())); - assert_migration_required(style_profile(&db, "chat-a", STYLE_USER, None).map(|_| ())); - assert_migration_required(response_time_stats(&db, "chat-a", STYLE_USER, None).map(|_| ())); - assert_migration_required(reply_decision_search(&db, "chat-a", "marker", 5).map(|_| ())); - assert_migration_required(context_reply_bundle(&db, "chat-a", "marker", None).map(|_| ())); - } - - #[test] - fn builds_rebuilds_and_serializes_style_profile_from_eligible_rows() { - let dir = tempdir().unwrap(); - let db = dir.path().join("profile.sqlite3"); - let path = dir.path().join("chat.csv"); - fs::write( - &path, - "Date,User,Message\n\ - 2026-01-01,최연우,ㅋㅋㅋ 오늘 좋다\n\ - 2026-01-01,최연우,뭐해요? 😊\n\ - 2026-01-01,최연우,1) 복사한 정보\n\ - 2026-01-01,최연우,https://example.com 안내\n\ - 2026-01-01,최연우,공지드립니다 합니다\n\ - 2026-01-01,민수,일반 대화\n", - ) - .unwrap(); - - let source = path.canonicalize().unwrap().display().to_string(); - assert_eq!(index_csv(&db, "프로필방", &path).unwrap(), 6); - - let profile = style_profile(&db, "프로필방", STYLE_USER, Some(&source)) - .unwrap() - .unwrap(); - assert_eq!(profile.sample_count, 2); - assert_eq!(profile.question_count, 1); - assert_eq!(profile.emoji_count, 1); - assert_eq!(profile.punctuation_count, 1); - assert_eq!(profile.casual_ending_count, 1); - assert!(profile.median_character_length > 0.0); - assert!(profile.p90_character_length >= profile.median_character_length); - assert_eq!(profile.policy_version, STYLE_POLICY_VERSION); - let endings: serde_json::Value = - serde_json::from_str(&profile.common_endings_json).unwrap(); - assert_eq!(endings["요"], 1); - let serialized = style_profile_json(&db, "프로필방", STYLE_USER, Some(&source)) - .unwrap() - .unwrap(); - let serialized: serde_json::Value = serde_json::from_str(&serialized).unwrap(); - assert_eq!(serialized["sample_count"], 2); - - let conn = open_db(&db).unwrap(); - let rows = conn - .prepare( - "SELECT source_row, content_kind, style_eligible - FROM owner_style WHERE source = ?1 ORDER BY source_row", - ) - .unwrap() - .query_map([&source], |row| { - Ok(( - row.get::<_, i64>(0)?, - row.get::<_, String>(1)?, - row.get::<_, i64>(2)?, - )) - }) - .unwrap() - .collect::>>() - .unwrap(); - assert_eq!(rows.len(), 5); - assert_eq!(rows[0], (1, "ordinary_conversation".into(), 1)); - assert_eq!(rows[2], (3, "list_or_numbered".into(), 0)); - assert_eq!(rows[3], (4, "url".into(), 0)); - assert_eq!(rows[4], (5, "long_or_formal".into(), 0)); - - fs::write(&path, "Date,User,Message\n2026-01-01,최연우,새 말투네\n").unwrap(); - index_csv(&db, "프로필방", &path).unwrap(); - let rebuilt = style_profile(&db, "프로필방", STYLE_USER, Some(&source)) - .unwrap() - .unwrap(); - assert_eq!(rebuilt.sample_count, 1); - assert!(rebuilt.common_tokens_json.contains("새")); - assert!(!rebuilt.common_tokens_json.contains("오늘")); - - fs::write( - &path, - "Date,User,Message\n2026-01-01,최연우,https://example.com\n", - ) - .unwrap(); - index_csv(&db, "프로필방", &path).unwrap(); - assert!(style_profile(&db, "프로필방", STYLE_USER, Some(&source)) - .unwrap() - .is_none()); - assert_eq!( - open_db(&db) - .unwrap() - .query_row( - "SELECT COUNT(*) FROM owner_style_profile - WHERE chat = ?1 AND source = ?2", - params!["프로필방", source], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - } - #[test] - fn context_reply_bundle_is_versioned_and_shape_stable() { - let dir = tempdir().unwrap(); - let db = dir.path().join("bundle.sqlite3"); - let path = dir.path().join("chat.csv"); - fs::write( - &path, - "Date,User,Message\n\ - 2026-01-01 00:00:00,민수,세금 일정 알려줘\n\ - 2026-01-01 00:00:10,최연우,ㅋㅋㅋ 일정 확인해요\n", - ) - .unwrap(); - index_csv(&db, "부자멘토멘티", &path).unwrap(); - - let bundle = context_reply_bundle(&db, "부자멘토멘티", "세금 일정", None).unwrap(); - assert_eq!(bundle.schema_version, CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION); - assert!(bundle.context.len() <= CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT); - assert!(bundle.styles.len() <= CONTEXT_REPLY_BUNDLE_STYLE_LIMIT); - assert!(bundle.prior_decisions.len() <= CONTEXT_REPLY_BUNDLE_DECISION_LIMIT); - assert_eq!( - bundle.style_profile.as_ref().unwrap().policy_version, - STYLE_POLICY_VERSION - ); - let value: serde_json::Value = serde_json::from_str( - &context_reply_bundle_json(&db, "부자멘토멘티", "세금 일정", None).unwrap(), - ) - .unwrap(); - let serialized = serde_json::to_string(&value).unwrap(); - let home = dirs::home_dir().unwrap().display().to_string(); - assert!(!serialized.contains(&home)); - assert!(!serialized.contains(&path.display().to_string())); - assert_eq!( - value - .as_object() - .unwrap() - .keys() - .cloned() - .collect::>(), - vec![ - "schema_version", - "context", - "styles", - "prior_decisions", - "style_profile", - "response_time", - ] - ); - } - - #[test] - fn punctuation_only_query_keeps_style_and_timing() { - let dir = tempdir().unwrap(); - let db = dir.path().join("tokenless.sqlite3"); - let path = dir.path().join("chat.csv"); - fs::write( - &path, - "Date,User,Message\n\ - 2026-01-01 00:00:00,민수,세금 일정 알려줘\n\ - 2026-01-01 00:00:10,최연우,ㅋㅋㅋ 일정 확인해요\n", - ) - .unwrap(); - index_csv(&db, "부자멘토멘티", &path).unwrap(); - let bundle = context_reply_bundle(&db, "부자멘토멘티", "???", None).unwrap(); - assert!(bundle.context.is_empty()); - assert!(!bundle.styles.is_empty()); - assert!(bundle.styles.iter().all(|row| row.mode == "recency_style")); - assert!(bundle.style_profile.is_some()); - assert!(bundle.response_time.is_some()); - } - - #[test] - fn recipient_bundle_excludes_every_burst_row_and_decision() { - let dir = tempdir().unwrap(); - let db = dir.path().join("burst-exclusion.sqlite3"); - let chat_id = 77; - let chat = "부자멘토멘티"; - let events = (101..=107) - .map(|log_id| { - let disposition = if log_id == 107 { - "retained" - } else { - "excluded" - }; - live_event( - chat_id, - log_id, - "민수", - &format!("burstmarker {disposition} context {log_id}"), - 1_000 + log_id, - ) - }) - .collect::>(); - let ingested = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &events, - true, - false, - ) - .unwrap(); - - for log_id in 101..=107 { - let disposition = if log_id == 107 { - "retained" - } else { - "excluded" - }; - let record = serde_json::json!({ - "event_id": format!("db:{chat_id}:{log_id}"), - "chat": chat, - "author": "민수", - "received_at": "2026-01-01T00:00:00Z", - "message": format!("burstmarker {disposition} decision {log_id}"), - "decision": "skip", - "reason": "duplicate", - "category": "duplicate", - "context_match_count": 0, - "style_match_count": 0, - "best_context_score": 0.0, - "best_style_score": 0.0, - "prior_similarity": 0.0, - "scheduled_delay_seconds": 0.0, - "status": "skipped", - "reply": null, - "evidence_ids": [], - "style_policy_version": "", - }); - assert!(record_reply_decision(&db, &record.to_string()).unwrap()); - } - - let unfiltered = context_reply_bundle_for_recipient_excluding_live_events( - &db, - chat, - "burstmarker", - Some(&ingested.source), - "민수", - chat_id, - &[999], - ) - .unwrap(); - assert!(unfiltered - .context - .iter() - .any(|row| row.message.contains("excluded context 101"))); - assert!(unfiltered - .prior_decisions - .iter() - .any(|row| row.event_id == format!("db:{chat_id}:101"))); - - let bundle = context_reply_bundle_for_recipient_excluding_live_events( - &db, - chat, - "burstmarker", - Some(&ingested.source), - "민수", - chat_id, - &[101, 102, 103, 104, 105, 106], - ) - .unwrap(); - assert!(!bundle - .context - .iter() - .any(|row| row.message.contains("excluded context"))); - assert!(bundle - .context - .iter() - .any(|row| row.message.contains("retained context 107"))); - assert!(!bundle - .prior_decisions - .iter() - .any(|row| (101..=106).any(|log_id| row.event_id == format!("db:{chat_id}:{log_id}")))); - assert!(bundle - .prior_decisions - .iter() - .any(|row| row.event_id == format!("db:{chat_id}:107"))); - } - - #[test] - fn burst_exclusion_ids_are_positive_unique_and_bounded() { - assert!(live_event_exclusions(7, &[1]).is_ok()); - assert!(live_event_exclusions(0, &[1]).is_err()); - assert!(live_event_exclusions(7, &[]).is_err()); - assert!(live_event_exclusions(7, &[0]).is_err()); - assert!(live_event_exclusions(7, &[-1]).is_err()); - assert!(live_event_exclusions(7, &[1, 1]).is_err()); - assert!(live_event_exclusions(7, &[1, 2, 3, 4, 5, 6]).is_ok()); - assert!(live_event_exclusions(7, &[1, 2, 3, 4, 5, 6, 7]).is_err()); - assert!(live_event_exclusions(7, &[i64::MAX]).is_ok()); - } - - #[test] - fn retrieval_candidate_pools_are_bounded_and_deterministic() { - let dir = tempdir().unwrap(); - let db = dir.path().join("bounded.sqlite3"); - let conn = open_db(&db).unwrap(); - for index in 0..(CONTEXT_VECTOR_CANDIDATE_CAP + 32) { - insert_context_row( - &conn, - "source-a", - "chat-a", - "2026-01-01", - &format!("bounded candidate {index}"), - ); - } - for index in 0..(STYLE_VECTOR_CANDIDATE_CAP + 32) { - conn.execute( - "INSERT INTO owner_style( - source, chat, date, user_name, message, vector, source_row, - content_kind, style_eligible, policy_version, features_json - ) VALUES (?1, 'chat-a', '2026-01-01', ?2, ?3, ?4, ?5, - 'ordinary_conversation', 1, ?6, '{}')", - params![ - "source-a", - STYLE_USER, - format!("style candidate {index}"), - vector_to_bytes(&encode_vector("style candidate")), - index as i64, - STYLE_POLICY_VERSION, - ], - ) - .unwrap(); - } - drop(conn); - - let first = search( - &db, - Some("chat-a"), - Some("source-a"), - "bounded", - "vector", - CONTEXT_VECTOR_CANDIDATE_CAP * 2, - ) - .unwrap(); - let second = search( - &db, - Some("chat-a"), - Some("source-a"), - "bounded", - "vector", - CONTEXT_VECTOR_CANDIDATE_CAP * 2, - ) - .unwrap(); - assert_eq!(first.len(), CONTEXT_VECTOR_CANDIDATE_CAP); - assert_eq!( - serde_json::to_string(&first).unwrap(), - serde_json::to_string(&second).unwrap() - ); - - let conn = open_db(&db).unwrap(); - let styles = style_search_with_connection( - &conn, - Some("chat-a"), - Some("source-a"), - "style", - STYLE_VECTOR_CANDIDATE_CAP * 2, - true, - ) - .unwrap(); - assert_eq!(styles.len(), STYLE_VECTOR_CANDIDATE_CAP); - } - - #[test] - fn fts_rebuild_recovers_deleted_index_rows() { - let dir = tempdir().unwrap(); - let db = dir.path().join("fts-rebuild.sqlite3"); - let path = fixture(dir.path(), "chat.csv", "rebuild marker"); - index_csv(&db, "chat-a", &path).unwrap(); - - let conn = open_db(&db).unwrap(); - conn.execute("DELETE FROM context_messages_fts", []) - .unwrap(); - conn.execute("DELETE FROM context_retrieval_meta", []) - .unwrap(); - drop(conn); - - let error = search(&db, Some("chat-a"), None, "rebuild", "keyword", 5).unwrap_err(); - assert_eq!(error.to_string(), CONTEXT_RETRIEVAL_MIGRATION_REQUIRED); - rebuild_retrieval_index(&open_db(&db).unwrap()).unwrap(); - let results = search(&db, Some("chat-a"), None, "rebuild", "keyword", 5).unwrap(); - assert_eq!(results.len(), 1); - assert_eq!(results[0].message, "rebuild marker"); - } - - #[test] - fn style_policy_and_source_filters_are_applied_before_scoring() { - let dir = tempdir().unwrap(); - let db = dir.path().join("style-policy.sqlite3"); - let first = dir.path().join("first.csv"); - let second = dir.path().join("second.csv"); - fs::write( - &first, - "Date,User,Message\n2026-01-01,최연우,current style\n", - ) - .unwrap(); - fs::write( - &second, - "Date,User,Message\n2026-01-01,최연우,other source style\n", - ) - .unwrap(); - index_csv(&db, "chat-a", &first).unwrap(); - index_csv(&db, "chat-a", &second).unwrap(); - let source = first.canonicalize().unwrap().display().to_string(); - - let conn = open_db(&db).unwrap(); - conn.execute( - "INSERT INTO owner_style( - source, chat, date, user_name, message, vector, source_row, - content_kind, style_eligible, policy_version, features_json - ) VALUES (?1, 'chat-a', '2027-01-01', ?2, 'legacy style', - ?3, 999, 'ordinary_conversation', 1, 'legacy-v1', '{}')", - params![ - source, - STYLE_USER, - vector_to_bytes(&encode_vector("legacy style")), - ], - ) - .unwrap(); - - let results = - style_search_with_connection(&conn, Some("chat-a"), Some(&source), "legacy", 10, true) - .unwrap(); - assert_eq!(results.len(), 1); - assert_eq!(results[0].message, "current style"); - assert_eq!(results[0].source, source); - } - - #[test] - fn retrieval_without_bounded_candidates_fails_closed() { - let dir = tempdir().unwrap(); - let db = dir.path().join("empty.sqlite3"); - let path = fixture(dir.path(), "chat.csv", "known phrase"); - index_csv(&db, "chat-a", &path).unwrap(); - assert!( - search(&db, Some("chat-a"), None, "completely missing", "vector", 5) - .unwrap() - .is_empty() - ); - assert!( - search(&db, Some("missing-chat"), None, "query", "vector", 5) - .unwrap() - .is_empty() - ); - assert!(style_search(&db, Some("missing-chat"), "query", 5) - .unwrap() - .is_empty()); - assert!(context_reply_bundle(&db, "missing-chat", "query", None) - .unwrap() - .context - .is_empty()); - } - - #[test] - fn live_schema_bootstrap_is_additive_and_idempotent() { - let dir = tempdir().unwrap(); - let db = dir.path().join("live-schema.sqlite3"); - - ensure_live_context_schema(&db).unwrap(); - ensure_live_context_schema(&db).unwrap(); - assert!(live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, 77) - .unwrap() - .is_none()); - - let conn = open_db_readonly(&db).unwrap(); - assert_eq!( - conn.query_row( - "SELECT value FROM context_retrieval_meta WHERE key='live_context_schema'", - [], - |row| row.get::<_, String>(0), - ) - .unwrap(), - LIVE_CONTEXT_SCHEMA_VERSION - ); - for table in [ - "context_sources", - "context_live_events", - "response_time_samples", - "owner_recipient_style_samples", - "owner_recipient_style_profile", - "context_message_topics", - "context_topic_stats", - "context_reference_packs", - ] { - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?1", - [table], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 1 - ); - } - assert_eq!( - conn.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { - row.get::<_, i64>(0) - }) - .unwrap(), - 0 - ); - drop(conn); - - let csv = fixture(dir.path(), "legacy.csv", "legacy fts marker"); - assert_eq!(index_csv(&db, "legacy-chat", &csv).unwrap(), 1); - assert_eq!( - search(&db, Some("legacy-chat"), None, "legacy", "keyword", 5,) - .unwrap() - .len(), - 1 - ); - } - - #[test] - fn auto_reply_startup_allows_dirty_partial_authoritative_identity() { - let dirty_ok = LiveContextSyncState { - source: "local-db:test:42".to_string(), - chat_id: 42, - chat: "부자멘토멘티".to_string(), - checkpoint_log_id: 7, - authoritative: true, - summary_dirty: true, - sync_status: "partial".to_string(), - }; - assert!(dirty_ok.allows_auto_reply_startup(42, "부자멘토멘티")); - - let mut not_auth = dirty_ok.clone(); - not_auth.authoritative = false; - assert!(!not_auth.allows_auto_reply_startup(42, "부자멘토멘티")); - - let mut bad_sync = dirty_ok.clone(); - bad_sync.sync_status = "stale".to_string(); - assert!(!bad_sync.allows_auto_reply_startup(42, "부자멘토멘티")); - - assert!(!dirty_ok.allows_auto_reply_startup(43, "부자멘토멘티")); - assert!(!dirty_ok.allows_auto_reply_startup(42, "다른방")); - - let mut clean_ready = dirty_ok.clone(); - clean_ready.summary_dirty = false; - clean_ready.sync_status = "ready".to_string(); - assert!(clean_ready.allows_auto_reply_startup(42, "부자멘토멘티")); - } - - #[test] - fn live_ingest_is_idempotent_and_rolls_back_a_late_conflict() { - let dir = tempdir().unwrap(); - let db = dir.path().join("live-idempotent.sqlite3"); - let chat_id = 77; - let chat = "부자멘토멘티"; - let page = vec![ - live_event(chat_id, 1, "민수", "첫 질문", 100), - live_event(chat_id, 2, STYLE_USER, "좋아요", 110), - ]; - - let first = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &page, - true, - false, - ) - .unwrap(); - assert_eq!(first.inserted_events, 2); - assert_eq!(first.duplicate_events, 0); - assert_eq!(first.checkpoint_log_id, 2); - assert!(first.summary_refreshed); - - let replay = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &page, - true, - false, - ) - .unwrap(); - assert_eq!(replay.inserted_events, 0); - assert_eq!(replay.duplicate_events, 2); - assert_eq!(replay.checkpoint_log_id, 2); - - let mut changed = page.clone(); - changed[0].message = "바뀐 질문".to_string(); - assert!(ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &changed, - true, - false, - ) - .unwrap_err() - .to_string() - .contains("digest mismatch")); - - let source = live_context_source_id(TEST_ACCOUNT_FINGERPRINT, chat_id).unwrap(); - let future = live_event(chat_id, 4, "민수", "원본", 130); - let conn = open_db(&db).unwrap(); - conn.execute( - "INSERT INTO context_live_events( - source, chat_id, log_id, sent_at, sender_name, message_digest, - disposition, auto_generated, context_message_id, style_message_id, created_at - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, 'context', 0, NULL, NULL, ?7)", - params![ - source, - chat_id, - future.log_id, - future.sent_at, - future.sender_name, - live_event_digest(&future).unwrap(), - Utc::now().to_rfc3339(), - ], - ) - .unwrap(); - drop(conn); - - let conflict_page = vec![ - live_event(chat_id, 3, "민수", "롤백 표식", 120), - live_event(chat_id, 4, "민수", "변경된 원본", 130), - ]; - assert!(ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 2, - &conflict_page, - false, - false, - ) - .unwrap_err() - .to_string() - .contains("digest mismatch")); - assert_eq!( - live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) - .unwrap() - .unwrap() - .checkpoint_log_id, - 2 - ); - let conn = open_db_readonly(&db).unwrap(); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM context_live_events WHERE source=?1 AND log_id=3", - [&source], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM context_messages WHERE source=?1 AND message='롤백 표식'", - [&source], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - } - - #[test] - fn live_ingest_prefix_checkpoint_cannot_skip_a_deferred_self_row() { - let dir = tempdir().unwrap(); - let db = dir.path().join("live-deferred-prefix.sqlite3"); - let chat_id = 78; - let chat = "부자멘토멘티"; - - let prefix = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &[live_event(chat_id, 10, "민수", "직전 질문", 1_000)], - false, - false, - ) - .unwrap(); - assert_eq!(prefix.checkpoint_log_id, 10); - - let skipped = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 20, - &[live_event(chat_id, 30, "민수", "다음 질문", 1_020)], - true, - false, - ) - .unwrap_err(); - assert!(skipped - .to_string() - .contains("checkpoint is behind the caller checkpoint")); - assert_eq!( - live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) - .unwrap() - .unwrap() - .checkpoint_log_id, - 10 - ); - - let mut deferred_self = live_event(chat_id, 20, STYLE_USER, "확정된 직접 답변", 1_010); - deferred_self.is_self = true; - let resumed = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 10, - &[deferred_self], - false, - false, - ) - .unwrap(); - assert_eq!(resumed.checkpoint_log_id, 20); - } - - #[test] - fn live_ingest_carries_bursts_across_pages_and_excludes_automatic_self_rows() { - let dir = tempdir().unwrap(); - let db = dir.path().join("live-pages.sqlite3"); - let chat_id = 88; - let chat = "부자멘토멘티"; - - let partial = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &[live_event(chat_id, 10, "민수", "페이지 경계 질문", 1_000)], - false, - false, - ) - .unwrap(); - assert_eq!(partial.checkpoint_log_id, 10); - let partial_state = live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) - .unwrap() - .unwrap(); - assert_eq!(partial_state.sync_status, "partial"); - - let complete = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 10, - &[live_event(chat_id, 20, STYLE_USER, "경계도 좋아요", 1_012)], - true, - false, - ) - .unwrap(); - assert_eq!(complete.response_samples, 1); - assert_eq!(complete.recipient_style_samples, 1); - assert!(complete.summary_refreshed); - let stats = response_time_stats(&db, chat, STYLE_USER, Some(&complete.source)) - .unwrap() - .unwrap(); - assert_eq!(stats.sample_count, 1); - assert_eq!(stats.average_seconds, 12.0); - let recipient = recipient_style_profile(&db, chat, "민수", Some(&complete.source)) - .unwrap() - .unwrap(); - assert_eq!(recipient.direct_sample_count, 1); - assert!(recipient.used_fallback); - - let mut automatic = live_event(chat_id, 40, STYLE_USER, "자동출력표식", 2_010); - automatic.auto_generated = true; - let excluded = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 20, - &[ - live_event(chat_id, 30, "민수", "자동 직전 질문", 2_000), - automatic, - ], - true, - false, - ) - .unwrap(); - assert_eq!(excluded.indexed_messages, 1); - assert_eq!(excluded.style_messages, 0); - assert_eq!(excluded.response_samples, 0); - assert_eq!(excluded.recipient_style_samples, 0); - assert!(search( - &db, - Some(chat), - Some(&excluded.source), - "자동출력표식", - "keyword", - 5, - ) - .unwrap() - .is_empty()); - assert_eq!( - response_time_stats(&db, chat, STYLE_USER, Some(&excluded.source)) - .unwrap() - .unwrap() - .sample_count, - 1 - ); - let conn = open_db_readonly(&db).unwrap(); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM context_live_events - WHERE source=?1 AND log_id=40 AND auto_generated=1 - AND context_message_id IS NULL AND style_message_id IS NULL", - [&excluded.source], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 1 - ); - } - - #[test] - fn live_ingest_does_not_treat_a_nickname_collision_as_self() { - let dir = tempdir().unwrap(); - let db = dir.path().join("live-nickname-collision.sqlite3"); - let chat_id = 89; - let mut collision = live_event(chat_id, 1, STYLE_USER, "동명이인 메시지", 1_000); - collision.is_self = false; - let result = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - "부자멘토멘티", - 0, - &[collision], - true, - false, - ) - .unwrap(); - assert_eq!(result.indexed_messages, 1); - assert_eq!(result.style_messages, 0); - let conn = open_db_readonly(&db).unwrap(); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM owner_style WHERE source=?1", - [&result.source], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - - let mut unresolved = live_event(chat_id, 2, STYLE_USER, "확정 전 출력", 1_010); - unresolved.is_self = true; - unresolved.exclude_from_learning = true; - let excluded = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - "부자멘토멘티", - 1, - &[unresolved], - true, - false, - ) - .unwrap(); - assert_eq!(excluded.indexed_messages, 0); - assert_eq!(excluded.style_messages, 0); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM context_live_events - WHERE source=?1 AND log_id=2 AND disposition='ambiguous_auto_candidate' - AND auto_generated=0 AND context_message_id IS NULL - AND style_message_id IS NULL", - [&result.source], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 1 - ); - } - - #[test] - fn promotion_requires_complete_summaries_and_is_transactional() { - let dir = tempdir().unwrap(); - let db = dir.path().join("live-promotion-rollback.sqlite3"); - let chat_id = 99; - let page = vec![ - live_event(chat_id, 1, "민수", "질문", 100), - live_event(chat_id, 2, STYLE_USER, "좋아요", 110), - ]; - assert!(ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - "방", - 0, - &page, - true, - true, - ) - .unwrap_err() - .to_string() - .contains("lacks required")); - assert!( - live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) - .unwrap() - .is_none() - ); - let conn = open_db_readonly(&db).unwrap(); - assert_eq!( - conn.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { - row.get::<_, i64>(0) - }) - .unwrap(), - 0 - ); - } - - #[test] - fn authoritative_live_source_and_recipient_bundle_use_the_right_register() { - let dir = tempdir().unwrap(); - let db = dir.path().join("recipient-bundle.sqlite3"); - let legacy = dir.path().join("legacy.csv"); - fs::write( - &legacy, - "Date,User,Message\n2026-01-01,민수,legacytoken\n2026-01-01,최연우,예전 말투예요\n", - ) - .unwrap(); - index_csv(&db, "부자멘토멘티", &legacy).unwrap(); - - let chat_id = 123; - let events = vec![ - live_event(chat_id, 1, "민수", "시장 질문 알파", 100), - live_event(chat_id, 2, STYLE_USER, "알파는 좋아요", 110), - live_event(chat_id, 3, "민수", "시장 질문 베타", 200), - live_event(chat_id, 4, STYLE_USER, "베타도 봐요", 210), - live_event(chat_id, 5, "민수", "시장 질문 감마", 300), - live_event(chat_id, 6, STYLE_USER, "감마도 좋아요", 310), - live_event(chat_id, 7, "영희", "시장 질문 델타", 400), - live_event(chat_id, 8, STYLE_USER, "델타는 천천히요", 410), - live_event(chat_id, 9, "민수", "현재질문표식", 500), - ]; - let ingested = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - "부자멘토멘티", - 0, - &events, - true, - true, - ) - .unwrap(); - assert!(ingested.authoritative); - assert!( - search(&db, Some("부자멘토멘티"), None, "legacytoken", "keyword", 5,) - .unwrap() - .is_empty() - ); - let live = search( - &db, - Some("부자멘토멘티"), - None, - "현재질문표식", - "keyword", - 5, - ) - .unwrap(); - assert_eq!(live.len(), 1); - assert_eq!(live[0].source, ingested.source); - assert_eq!( - style_profile(&db, "부자멘토멘티", STYLE_USER, None) - .unwrap() - .unwrap() - .source, - ingested.source - ); - assert_eq!( - response_time_stats(&db, "부자멘토멘티", STYLE_USER, None) - .unwrap() - .unwrap() - .sample_count, - 4 - ); - - let direct = recipient_style_profile(&db, "부자멘토멘티", "민수", None) - .unwrap() - .unwrap(); - assert_eq!(direct.direct_sample_count, 3); - assert_eq!(direct.confidence_sum, 3.0); - assert!(!direct.used_fallback); - let fallback = recipient_style_profile(&db, "부자멘토멘티", "영희", None) - .unwrap() - .unwrap(); - assert_eq!(fallback.direct_sample_count, 1); - assert!(fallback.used_fallback); - assert_eq!(fallback.profile.sample_count, 4); - - let conn = open_db_readonly(&db).unwrap(); - let mut plan_stmt = conn - .prepare(&format!("EXPLAIN QUERY PLAN {RECIPIENT_STYLE_SEARCH_SQL}")) - .unwrap(); - let plan = plan_stmt - .query_map( - params![ - &ingested.source, - "민수", - "부자멘토멘티", - STYLE_USER, - STYLE_POLICY_VERSION, - STYLE_VECTOR_CANDIDATE_CAP as i64, - ], - |row| row.get::<_, String>(3), - ) - .unwrap() - .collect::>>() - .unwrap() - .join("\n"); - assert!(plan.contains("idx_recipient_style_samples_lookup")); - assert!(!plan.contains("SCAN styles")); - assert!(!plan.contains("USE TEMP B-TREE")); - - let direct_bundle = context_reply_bundle_for_recipient_excluding_live_events( - &db, - "부자멘토멘티", - "현재질문표식", - None, - "민수", - chat_id, - &[9], - ) - .unwrap(); - assert_eq!( - direct_bundle.schema_version, - RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION - ); - assert!(direct_bundle.context.is_empty()); - assert!(direct_bundle - .recipient_style_profile - .as_ref() - .is_some_and(|profile| !profile.used_fallback)); - assert_eq!(direct_bundle.styles.len(), 3); - assert!(direct_bundle - .styles - .iter() - .all(|style| style.mode == "vector_style_recipient")); - assert!(!direct_bundle - .styles - .iter() - .any(|style| style.message.contains("델타"))); - assert!(direct_bundle - .styles - .iter() - .all(|style| style.source.starts_with("source:"))); - - let fallback_bundle = context_reply_bundle_for_recipient_excluding_live_events( - &db, - "부자멘토멘티", - "현재질문표식", - None, - "영희", - chat_id, - &[9], - ) - .unwrap(); - assert!(fallback_bundle - .recipient_style_profile - .as_ref() - .is_some_and(|profile| profile.used_fallback)); - assert!(fallback_bundle - .styles - .iter() - .any(|style| style.message.contains("델타"))); - assert!(fallback_bundle - .styles - .iter() - .all(|style| style.mode == "vector_style")); - - let json = context_reply_bundle_for_recipient_excluding_live_events_json( - &db, - "부자멘토멘티", - "현재질문표식", - None, - "민수", - chat_id, - &[9], - ) - .unwrap(); - let value: serde_json::Value = serde_json::from_str(&json).unwrap(); - assert_eq!( - value["schema_version"], - RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION - ); - assert_eq!(value["recipient"], "민수"); - assert!(value.get("recipient_style_profile").is_some()); - assert_eq!(value.as_object().unwrap().len(), 8); - let state = live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) - .unwrap() - .unwrap(); - assert_eq!(state.checkpoint_log_id, 9); - assert!(state.authoritative); - assert_eq!(state.sync_status, "ready"); - } - - #[test] - fn automatic_self_event_matching_is_exact_bounded_and_fail_closed() { - let dir = tempdir().unwrap(); - let db = dir.path().join("self-classification.sqlite3"); - ensure_live_context_schema(&db).unwrap(); - let timestamp = |seconds| { - chrono::DateTime::::from_timestamp(seconds, 0) - .unwrap() - .to_rfc3339() - }; - record_sent_reply(&db, "unique", "방", "유일 답변", ×tamp(1_000)); - record_sent_reply(&db, "reused", "방", "재사용 답변", ×tamp(2_000)); - record_sent_reply(&db, "many-a", "방", "중복 답변", ×tamp(3_000)); - record_sent_reply(&db, "many-b", "방", "중복 답변", ×tamp(3_010)); - record_sent_reply(&db, "malformed", "방", "시간 오류", "not-a-time"); - - let outgoing = vec![ - OutgoingSelfEvent { - chat_id: 7, - log_id: 1, - message: "유일 답변".into(), - sent_at: 1_020, - }, - OutgoingSelfEvent { - chat_id: 7, - log_id: 2, - message: "재사용 답변".into(), - sent_at: 1_990, - }, - OutgoingSelfEvent { - chat_id: 7, - log_id: 3, - message: "재사용 답변".into(), - sent_at: 2_010, - }, - OutgoingSelfEvent { - chat_id: 7, - log_id: 4, - message: "중복 답변".into(), - sent_at: 3_005, - }, - OutgoingSelfEvent { - chat_id: 7, - log_id: 5, - message: "시간 오류".into(), - sent_at: 4_000, - }, - OutgoingSelfEvent { - chat_id: 7, - log_id: 6, - message: "사람이 직접 쓴 말".into(), - sent_at: 5_000, - }, - ]; - let classified = classify_auto_generated_self_events(&db, "방", &outgoing, &[]).unwrap(); - assert_eq!(classified.len(), outgoing.len()); - assert!(classified[0].auto_generated); - assert_eq!(classified[0].matched_event_id.as_deref(), Some("unique")); - assert_eq!(classified[0].reason, "unique_exact_sent_match"); - assert!(!classified[1].auto_generated); - assert!(!classified[2].auto_generated); - assert_eq!(classified[1].reason, "sent_match_reused_in_batch"); - assert_eq!(classified[2].reason, "sent_match_reused_in_batch"); - assert!(!classified[3].auto_generated); - assert_eq!(classified[3].reason, "multiple_sent_matches"); - assert!(!classified[4].auto_generated); - assert_eq!(classified[4].reason, "unparseable_candidate_time"); - assert!(!classified[5].auto_generated); - assert_eq!(classified[5].reason, "no_exact_sent_match"); - - let outside_window = [OutgoingSelfEvent { - chat_id: 7, - log_id: 7, - message: "유일 답변".into(), - sent_at: 1_121, - }]; - assert!( - !classify_auto_generated_self_events(&db, "방", &outside_window, &[]).unwrap()[0] - .auto_generated - ); - } - - #[test] - fn ambiguous_automatic_candidates_never_feed_context_style_or_timing() { - let dir = tempdir().unwrap(); - let db = dir.path().join("ambiguous-self-classification.sqlite3"); - let chat = "부자멘토멘티"; - let chat_id = 91; - let now = Utc::now().timestamp(); - ensure_live_context_schema(&db).unwrap(); - - let statuses = [ - "scheduled", - "sending", - "accepted_unconfirmed", - "delivery_unknown", - "reconcile_required", - ]; - let outgoing = statuses - .iter() - .enumerate() - .map(|(index, status)| OutgoingSelfEvent { - chat_id, - log_id: index as i64 + 2, - message: format!("자동 후보 {status}"), - sent_at: now, - }) - .collect::>(); - - let before_projection = - classify_auto_generated_self_events(&db, chat, &outgoing, &[]).unwrap(); - assert!(before_projection.iter().all(|item| { - !item.auto_generated - && item.matched_event_id.is_none() - && item.reason == "no_exact_sent_match" - })); - - for (index, status) in statuses.iter().enumerate() { - let event_id = format!("ambiguous-{index}"); - let record = serde_json::json!({ - "event_id": event_id, - "chat": chat, - "author": "민수", - "received_at": chrono::DateTime::::from_timestamp(now, 0) - .unwrap() - .to_rfc3339(), - "message": format!("incoming {index}"), - "decision": "reply", - "reason": "direct_question", - "category": "question", - "context_match_count": 1, - "style_match_count": 1, - "best_context_score": 0.5, - "best_style_score": 0.5, - "prior_similarity": 0.0, - "scheduled_delay_seconds": 0.0, - "status": status, - "reply": outgoing[index].message, - "evidence_ids": ["context:test"], - "style_policy_version": STYLE_POLICY_VERSION, - }); - assert!(record_reply_decision(&db, &record.to_string()).unwrap()); - } - assert!( - update_reply_decision(&db, "ambiguous-0", "delivery_unknown", None, None,).unwrap() - ); - - let classified = classify_auto_generated_self_events(&db, chat, &outgoing, &[]).unwrap(); - assert_eq!(classified.len(), statuses.len()); - for (index, item) in classified.iter().enumerate() { - assert!(!item.auto_generated); - assert_eq!(item.reason, "ambiguous_auto_candidate"); - assert_eq!( - item.matched_event_id.as_deref(), - Some(format!("ambiguous-{index}").as_str()) - ); - } - let mut outside_window = outgoing[1].clone(); - outside_window.log_id = 100; - outside_window.sent_at = now + AUTO_GENERATED_MATCH_WINDOW_SECONDS + 10; - let outside = - classify_auto_generated_self_events(&db, chat, &[outside_window], &[]).unwrap(); - assert!(!outside[0].auto_generated); - assert_eq!(outside[0].reason, "no_exact_sent_match"); - - let mut live_events = vec![live_event(chat_id, 1, "민수", "직전 질문", now - 10)]; - live_events.extend(outgoing.iter().zip(&classified).map(|(event, item)| { - let mut live = live_event( - event.chat_id, - event.log_id, - STYLE_USER, - &event.message, - event.sent_at, - ); - live.exclude_from_learning = - !item.auto_generated && item.reason != "no_exact_sent_match"; - live.auto_generated = item.auto_generated; - live - })); - let result = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &live_events, - true, - false, - ) - .unwrap(); - assert_eq!(result.indexed_messages, 1); - assert_eq!(result.style_messages, 0); - assert_eq!(result.response_samples, 0); - assert_eq!(result.recipient_style_samples, 0); - - let conn = open_db_readonly(&db).unwrap(); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM context_live_events - WHERE source=?1 AND disposition='ambiguous_auto_candidate' - AND auto_generated=0 AND context_message_id IS NULL - AND style_message_id IS NULL", - [&result.source], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - statuses.len() as i64 - ); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM response_time_samples WHERE source=?1", - [&result.source], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - } - #[test] - fn queue_confirmed_send_excludes_a_loopback_self_row() { - let dir = tempdir().unwrap(); - let db = dir.path().join("queue-send.sqlite3"); - ensure_live_context_schema(&db).unwrap(); - let chat_id = 7; - let chat = "방"; - let text = "제목은 폭탄인데 내용은 지원이네요"; - assert!(classify_style_message(text).style_eligible); - let sent = ConfirmedSelfSend { - event_id: "queue:417780809780519:1".into(), - text: text.into(), - sent_at: 1_000, - }; - let outgoing = vec![OutgoingSelfEvent { - chat_id, - log_id: 11, - message: text.into(), - sent_at: 1_004, - }]; - let classified = - classify_auto_generated_self_events(&db, chat, &outgoing, std::slice::from_ref(&sent)) - .unwrap(); - assert!(classified[0].auto_generated); - assert_eq!(classified[0].reason, "unique_exact_sent_match"); - assert_eq!( - classified[0].matched_event_id.as_deref(), - Some(sent.event_id.as_str()) - ); - - let mut events = vec![live_event( - chat_id, - 10, - "민수", - "지원은 어디까지 해봤어", - 990, - )]; - let mut loopback = live_event(chat_id, 11, STYLE_USER, text, 1_004); - loopback.auto_generated = classified[0].auto_generated; - events.push(loopback); - let result = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &events, - true, - false, - ) - .unwrap(); - assert_eq!(result.style_messages, 0); - assert_eq!(result.response_samples, 0); - let conn = open_db_readonly(&db).unwrap(); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM owner_style WHERE chat=?1 AND message=?2", - params![chat, text], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM context_live_events WHERE disposition='auto_generated'", - [], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 1 - ); - drop(conn); - - // The same text without a delivered send stays an owner sample. - let unmatched = classify_auto_generated_self_events(&db, chat, &outgoing, &[]).unwrap(); - assert!(!unmatched[0].auto_generated); - assert_eq!(unmatched[0].reason, "no_exact_sent_match"); - } - - #[test] - fn queue_and_decision_sends_must_agree_on_one_identity() { - let dir = tempdir().unwrap(); - let db = dir.path().join("self-classification-conflict.sqlite3"); - ensure_live_context_schema(&db).unwrap(); - let timestamp = |seconds| { - chrono::DateTime::::from_timestamp(seconds, 0) - .unwrap() - .to_rfc3339() - }; - record_sent_reply(&db, "decided", "방", "같은 문장", ×tamp(1_000)); - let outgoing = vec![OutgoingSelfEvent { - chat_id: 7, - log_id: 21, - message: "같은 문장".into(), - sent_at: 1_001, - }]; - let queue = ConfirmedSelfSend { - event_id: "queue:9".into(), - text: "같은 문장".into(), - sent_at: 1_002, - }; - let classified = - classify_auto_generated_self_events(&db, "방", &outgoing, std::slice::from_ref(&queue)) - .unwrap(); - assert!(!classified[0].auto_generated); - assert_eq!(classified[0].reason, "multiple_sent_matches"); - assert!(classified[0].matched_event_id.is_none()); - } - - #[test] - fn queue_loader_reads_only_terminal_sent_jobs() { - let dir = tempdir().unwrap(); - let queue = dir.path().join("reply-queue.sqlite3"); - { - let conn = Connection::open(&queue).unwrap(); - conn.execute_batch( - "CREATE TABLE reply_jobs(event_id TEXT, status TEXT, reply TEXT, updated_at REAL);", - ) - .unwrap(); - conn.execute( - "INSERT INTO reply_jobs VALUES('a','sent','보낸 문장',1000.4)", - [], - ) - .unwrap(); - conn.execute( - "INSERT INTO reply_jobs VALUES('b','skipped','버려진 문장',1001.0)", - [], - ) - .unwrap(); - conn.execute("INSERT INTO reply_jobs VALUES('c','sent',' ',1002.0)", []) - .unwrap(); - conn.execute( - "INSERT INTO reply_jobs VALUES('d','sent','시간 없음',NULL)", - [], - ) - .unwrap(); - conn.execute( - "INSERT INTO reply_jobs VALUES('e','delivery_unknown','불확실',1003.0)", - [], - ) - .unwrap(); - } - let sends = confirmed_self_sends_from_queue(&queue).unwrap(); - assert_eq!(sends.len(), 1); - assert_eq!(sends[0].event_id, "a"); - assert_eq!(sends[0].text, "보낸 문장"); - assert_eq!(sends[0].sent_at, 1_000); - - let without_jobs = dir.path().join("other-queue.sqlite3"); - Connection::open(&without_jobs) - .unwrap() - .execute_batch("CREATE TABLE unrelated(x)") - .unwrap(); - assert!(confirmed_self_sends_from_queue(&without_jobs) - .unwrap() - .is_empty()); - assert!( - confirmed_self_sends_from_queue(&dir.path().join("absent.sqlite3")) - .unwrap() - .is_empty() - ); - } - - #[test] - fn repair_reclassifies_bot_sent_style_rows_and_rebuilds_aggregates() { - let dir = tempdir().unwrap(); - let db = dir.path().join("repair.sqlite3"); - ensure_live_context_schema(&db).unwrap(); - let chat_id = 7; - let chat = "방"; - let bot_text = "제목은 폭탄인데 내용은 지원이네요"; - let events = vec![ - live_event(chat_id, 1, "민수", "오늘 뭐 해", 1_000), - live_event(chat_id, 2, STYLE_USER, "그냥 집에 있었어", 1_030), - live_event(chat_id, 3, "민수", "지원은 어디까지 해봤어", 2_000), - live_event(chat_id, 4, STYLE_USER, bot_text, 2_050), - ]; - let ingested = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &events, - true, - false, - ) - .unwrap(); - assert_eq!(ingested.style_messages, 2); - assert_eq!(ingested.response_samples, 2); - let eligible_before = { - let conn = open_db_readonly(&db).unwrap(); - conn.query_row( - "SELECT COUNT(*) FROM owner_style WHERE style_eligible=1", - [], - |row| row.get::<_, i64>(0), - ) - .unwrap() - }; - assert_eq!(eligible_before, 2); - - let bot_style_id: i64 = { - let conn = open_db_readonly(&db).unwrap(); - conn.query_row( - "SELECT id FROM owner_style WHERE message=?1", - params![bot_text], - |row| row.get::<_, i64>(0), - ) - .unwrap() - }; - let sends = vec![ConfirmedSelfSend { - event_id: "queue:2".into(), - text: bot_text.into(), - sent_at: 2_051, - }]; - - let dry = repair_bot_sent_style_samples(&db, chat, chat_id, &sends, true).unwrap(); - assert_eq!(dry.matched_rows, 1); - assert_eq!(dry.already_ineligible, 0); - assert!(dry.dry_run); - { - let conn = open_db_readonly(&db).unwrap(); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM owner_style WHERE content_kind='bot_sent_reply'", - [], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - assert_eq!( - conn.query_row("SELECT sample_count FROM owner_style_profile", [], |row| { - row.get::<_, i64>(0) - },) - .unwrap(), - eligible_before - ); - } - - let report = repair_bot_sent_style_samples(&db, chat, chat_id, &sends, false).unwrap(); - assert_eq!(report.matched_rows, 1); - assert_eq!(report.already_ineligible, 0); - assert_eq!(report.ambiguous_rows, 0); - assert_eq!(report.deleted_response_samples, 1); - assert_eq!(report.rebuilt_sources.len(), 1); - assert_eq!(report.matches[0].offset_seconds, -1); - { - let conn = open_db_readonly(&db).unwrap(); - let (kind, eligible) = conn - .query_row( - "SELECT content_kind, style_eligible FROM owner_style WHERE message=?1", - params![bot_text], - |row| Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?)), - ) - .unwrap(); - assert_eq!(kind, "bot_sent_reply"); - assert_eq!(eligible, 0); - assert_eq!( - conn.query_row("SELECT sample_count FROM owner_style_profile", [], |row| { - row.get::<_, i64>(0) - },) - .unwrap(), - eligible_before - 1 - ); - assert_eq!( - conn.query_row("SELECT COUNT(*) FROM response_time_samples", [], |row| row - .get::<_, i64>( - 0 - ),) - .unwrap(), - 1 - ); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM owner_recipient_style_samples WHERE style_message_id = ?1", - params![bot_style_id], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - } - - // Idempotent: the second pass only sees an already ineligible row. - let again = repair_bot_sent_style_samples(&db, chat, chat_id, &sends, false).unwrap(); - assert_eq!(again.matched_rows, 1); - assert_eq!(again.already_ineligible, 1); - assert!(again.rebuilt_sources.is_empty()); - } - #[test] - fn csv_import_does_not_learn_a_confirmed_bot_reply() { - let dir = tempdir().unwrap(); - let db = dir.path().join("csv-bot-send.sqlite3"); - ensure_live_context_schema(&db).unwrap(); - let timestamp = |seconds: i64| { - chrono::DateTime::::from_timestamp(seconds, 0) - .unwrap() - .to_rfc3339() - }; - let sent_at = - chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:00", "%Y-%m-%d %H:%M:%S") - .unwrap() - .and_utc() - .timestamp(); - record_sent_reply( - &db, - "bot-1", - "부자멘토멘티", - "제목은 폭탄인데 내용은 지원이네요", - ×tamp(sent_at), - ); - let path = dir.path().join("chat.csv"); - fs::write( - &path, - "Date,User,Message\n\ -2026-01-01 09:59:30,문승현,기사 봤어\n\ -2026-01-01 10:00:04,최연우,제목은 폭탄인데 내용은 지원이네요\n\ -2026-01-01 11:00:00,최연우,그건 좀 세긴 하네\n", - ) - .unwrap(); - assert_eq!(index_csv(&db, "부자멘토멘티", &path).unwrap(), 3); - - let conn = open_db_readonly(&db).unwrap(); - let (kind, eligible): (String, i64) = conn - .query_row( - "SELECT content_kind, style_eligible FROM owner_style WHERE message=?1", - params!["제목은 폭탄인데 내용은 지원이네요"], - |row| Ok((row.get(0)?, row.get(1)?)), - ) - .unwrap(); - assert_eq!(kind, "bot_sent_reply"); - assert_eq!(eligible, 0); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM owner_style WHERE style_eligible=1", - [], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 1 - ); - let sample_count: i64 = conn - .query_row( - "SELECT sample_count FROM owner_style_profile WHERE chat=?1", - params!["부자멘토멘티"], - |row| row.get(0), - ) - .unwrap(); - assert_eq!(sample_count, 1); - } - - #[test] - fn csv_import_excludes_a_queue_only_confirmed_send() { - let dir = tempdir().unwrap(); - let db = dir.path().join("csv-queue-send.sqlite3"); - ensure_live_context_schema(&db).unwrap(); - let reply = "역시 세긴 하네요"; - let sent_at = - chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:04", "%Y-%m-%d %H:%M:%S") - .unwrap() - .and_utc() - .timestamp(); - let queue = dir.path().join("reply-queue.sqlite3"); - { - let conn = Connection::open(&queue).unwrap(); - conn.execute_batch( - "CREATE TABLE reply_jobs(event_id TEXT, status TEXT, reply TEXT, updated_at REAL);", - ) - .unwrap(); - conn.execute( - "INSERT INTO reply_jobs VALUES('queue-only','sent',?1,?2)", - params![reply, sent_at as f64], - ) - .unwrap(); - } - let path = dir.path().join("chat.csv"); - fs::write( - &path, - "Date,User,Message\n\ -2026-01-01 10:00:00,문승현,그건 좀 세네\n\ -2026-01-01 10:00:04,최연우,역시 세긴 하네요\n", - ) - .unwrap(); - // No decision-ledger row exists for this reply: only the room queue - // proves the assistant wrote it. - assert_eq!( - index_csv_with_queue(&db, "부자멘토멘티", &path, Some(&queue)).unwrap(), - 2 - ); - let conn = open_db_readonly(&db).unwrap(); - let (kind, eligible): (String, i64) = conn - .query_row( - "SELECT content_kind, style_eligible FROM owner_style WHERE message=?1", - params![reply], - |row| Ok((row.get(0)?, row.get(1)?)), - ) - .unwrap(); - assert_eq!(kind, "bot_sent_reply"); - assert_eq!(eligible, 0); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM owner_style WHERE style_eligible=1", - [], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - } - - #[test] - fn csv_import_does_not_learn_pacing_from_a_bot_reply() { - let dir = tempdir().unwrap(); - let db = dir.path().join("csv-bot-pacing.sqlite3"); - ensure_live_context_schema(&db).unwrap(); - let bot_reply = "역시 세긴 하네요"; - let bot_sent_at = - chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:04", "%Y-%m-%d %H:%M:%S") - .unwrap() - .and_utc() - .timestamp(); - record_sent_reply( - &db, - "bot-pacing", - "부자멘토멘티", - bot_reply, - &chrono::DateTime::::from_timestamp(bot_sent_at, 0) - .unwrap() - .to_rfc3339(), - ); - let path = dir.path().join("chat.csv"); - fs::write( - &path, - "Date,User,Message\n\ -2026-01-01 09:59:30,문승현,그건 좀 세네\n\ -2026-01-01 10:00:04,최연우,역시 세긴 하네요\n\ -2026-01-01 10:01:00,문승현,다음 얘기 하자\n\ -2026-01-01 10:01:30,최연우,그건 좀 웃기네\n", - ) - .unwrap(); - index_csv(&db, "부자멘토멘티", &path).unwrap(); - let conn = open_db_readonly(&db).unwrap(); - // Only the genuine human pair (10:01:00 -> 10:01:30) may be measured. - let sample_count: i64 = conn - .query_row( - "SELECT sample_count FROM response_time_stats WHERE chat=?1", - params!["부자멘토멘티"], - |row| row.get(0), - ) - .unwrap(); - assert_eq!(sample_count, 1); - } - - #[test] - fn repair_rebuilds_a_source_whose_matched_rows_were_already_ineligible() { - let dir = tempdir().unwrap(); - let db = dir.path().join("repair-ineligible.sqlite3"); - ensure_live_context_schema(&db).unwrap(); - let chat_id = 7; - let chat = "방"; - let link = "https://example.test/notice"; - let events = vec![ - live_event(chat_id, 1, "민수", "이거 봐", 1_000), - live_event(chat_id, 2, STYLE_USER, link, 1_030), - ]; - let ingested = ingest_live_context_events( - &db, - TEST_ACCOUNT_FINGERPRINT, - chat_id, - chat, - 0, - &events, - true, - false, - ) - .unwrap(); - assert_eq!(ingested.style_messages, 1); - assert_eq!(ingested.response_samples, 1); - { - let conn = open_db_readonly(&db).unwrap(); - let eligible: i64 = conn - .query_row( - "SELECT style_eligible FROM owner_style WHERE message=?1", - params![link], - |row| row.get(0), - ) - .unwrap(); - // A URL row is style-ineligible but still produced a pacing sample. - assert_eq!(eligible, 0); - } - let sends = vec![ConfirmedSelfSend { - event_id: "queue:ineligible".into(), - text: link.into(), - sent_at: 1_031, - }]; - let report = repair_bot_sent_style_samples(&db, chat, chat_id, &sends, false).unwrap(); - assert_eq!(report.matched_rows, 1); - assert_eq!(report.already_ineligible, 1); - assert_eq!(report.deleted_response_samples, 1); - assert_eq!(report.rebuilt_sources.len(), 1); - let conn = open_db_readonly(&db).unwrap(); - assert_eq!( - conn.query_row( - "SELECT COUNT(*) FROM response_time_samples WHERE chat_id=?1", - params![chat_id], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 0 - ); - } - - #[test] - fn repair_recomputes_an_imported_source_pacing_aggregate() { - let dir = tempdir().unwrap(); - let db = dir.path().join("repair-imported.sqlite3"); - ensure_live_context_schema(&db).unwrap(); - let source = "/tmp/legacy-chat.csv"; - let chat = "부자멘토멘티"; - let bot_reply = "역시 세긴 하네요"; - let human_reply = "그건 좀 웃기네"; - let bot_sent_at = - chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:04", "%Y-%m-%d %H:%M:%S") - .unwrap() - .and_utc() - .timestamp(); - { - let conn = open_db(&db).unwrap(); - for (date, user, message) in [ - ("2026-01-01 09:59:30", "문승현", "그건 좀 세네"), - ("2026-01-01 10:00:04", STYLE_USER, bot_reply), - ("2026-01-01 10:01:00", "문승현", "다음 얘기 하자"), - ("2026-01-01 10:01:30", STYLE_USER, human_reply), - ] { - conn.execute( - "INSERT INTO context_messages(source, chat, date, user_name, message, vector) - VALUES (?1, ?2, ?3, ?4, ?5, ?6)", - params![ - source, - chat, - date, - user, - message, - vector_to_bytes(&encode_vector(message)) - ], - ) - .unwrap(); - } - // Both owner rows were eligible when the legacy importer ran, so the - // style profile starts at two samples even though one is the bot. - for (index, (date, message)) in [ - ("2026-01-01 10:00:04", bot_reply), - ("2026-01-01 10:01:30", human_reply), - ] - .iter() - .enumerate() - { - conn.execute( - "INSERT INTO owner_style(source, chat, date, user_name, message, vector, - source_row, content_kind, style_eligible, policy_version, features_json) - VALUES (?1, ?2, ?3, '최연우', ?4, ?5, ?6, 'ordinary_conversation', 1, ?7, '{}')", - params![ - source, - chat, - date, - message, - vector_to_bytes(&encode_vector(message)), - index as i64 + 1, - STYLE_POLICY_VERSION - ], - ) - .unwrap(); - } - conn.execute( - "INSERT INTO owner_style_profile( - chat, source, user_name, sample_count, average_character_length, - median_character_length, p90_character_length, casual_ending_count, - casual_ending_counts_json, question_count, emoji_count, punctuation_count, - common_endings_json, common_tokens_json, policy_version, created_at - ) VALUES (?1, ?2, '최연우', 2, 8.0, 8.0, 8.0, 0, '{}', 0, 0, 0, '{}', '{}', ?3, '2026-01-01')", - params![chat, source, STYLE_POLICY_VERSION], - ) - .unwrap(); - // The importer wrote the pacing aggregate itself and kept no sample - // rows: the 34s entry is really the bot, the 30s entry is human. - conn.execute( - "INSERT INTO response_time_stats( - chat, source, user_name, sample_count, average_seconds, median_seconds, - p90_seconds, min_seconds, max_seconds, max_window_seconds, stddev_seconds, - distribution_schema_version, distribution_json - ) VALUES (?1, ?2, '최연우', 2, 32.0, 32.0, 34.0, 30.0, 34.0, 34.0, 2.0, 0, '{}')", - params![chat, source], - ) - .unwrap(); - } - let sends = vec![ConfirmedSelfSend { - event_id: "legacy".into(), - text: bot_reply.into(), - sent_at: bot_sent_at, - }]; - let report = repair_bot_sent_style_samples(&db, chat, 7, &sends, false).unwrap(); - assert_eq!(report.matched_rows, 1); - assert_eq!(report.deleted_response_samples, 0); - assert_eq!(report.rebuilt_sources, vec![source.to_string()]); - let conn = open_db_readonly(&db).unwrap(); - // The bot sentence leaves the style profile: two samples become one. - assert_eq!( - conn.query_row( - "SELECT sample_count FROM owner_style_profile WHERE source = ?1", - params![source], - |row| row.get::<_, i64>(0), - ) - .unwrap(), - 1 - ); - // The pacing aggregate keeps only the human pair (30s). - let (samples, average): (i64, f64) = conn - .query_row( - "SELECT sample_count, average_seconds FROM response_time_stats WHERE source = ?1", - params![source], - |row| Ok((row.get(0)?, row.get(1)?)), - ) - .unwrap(); - assert_eq!(samples, 1); - assert_eq!(average, 30.0); - let (kind, eligible): (String, i64) = conn - .query_row( - "SELECT content_kind, style_eligible FROM owner_style WHERE message = ?1", - params![bot_reply], - |row| Ok((row.get(0)?, row.get(1)?)), - ) - .unwrap(); - assert_eq!(kind, "bot_sent_reply"); - assert_eq!(eligible, 0); - } -} +mod tests; diff --git a/src/context/tests.rs b/src/context/tests.rs new file mode 100644 index 00000000..fed4ef60 --- /dev/null +++ b/src/context/tests.rs @@ -0,0 +1,3552 @@ +use super::*; +use std::io::Write; +use tempfile::tempdir; + +const TEST_ACCOUNT_FINGERPRINT: &str = + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + +#[test] +fn context_database_wal_allows_readers_during_a_writer_transaction() -> Result<()> { + let dir = tempdir()?; + let db = dir.path().join("context-wal.sqlite3"); + let writer = open_db(&db)?; + let journal_mode: String = writer.pragma_query_value(None, "journal_mode", |row| row.get(0))?; + assert_eq!(journal_mode.to_ascii_lowercase(), "wal"); + + writer.execute_batch("BEGIN EXCLUSIVE")?; + let reader = open_db_readonly(&db)?; + let messages: i64 = reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { + row.get(0) + })?; + writer.execute_batch("ROLLBACK")?; + assert_eq!(messages, 0); + Ok(()) +} + +#[test] +fn context_wal_truncate_checkpoint_clears_deleted_message_pages() -> Result<()> { + let dir = tempdir()?; + let db = dir.path().join("context-wal-private.sqlite3"); + let conn = open_db(&db)?; + let retired_message = "retired-message-marker-7d28b"; + conn.execute( + "INSERT INTO context_messages(source, chat, date, user_name, message, vector) + VALUES ('source', 'chat', '2026-09-24', 'user', ?1, ?2)", + params![ + retired_message, + vector_to_bytes(&encode_vector(retired_message)) + ], + )?; + checkpoint_context_wal_truncate(&conn)?; + + conn.execute( + "DELETE FROM context_messages WHERE message = ?1", + [retired_message], + )?; + checkpoint_context_wal_truncate(&conn)?; + + let wal = fs::read(sqlite_sidecar_path(&db, "-wal")).unwrap_or_default(); + let main_db = fs::read(&db)?; + assert_eq!(wal.len(), 0); + assert!(!wal + .windows(retired_message.len()) + .any(|w| w == retired_message.as_bytes())); + assert!(!main_db + .windows(retired_message.len()) + .any(|w| w == retired_message.as_bytes())); + Ok(()) +} + +#[cfg(unix)] +#[test] +fn context_database_and_wal_sidecars_use_private_permissions() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let dir = tempdir()?; + let db = dir.path().join("permissions.sqlite3"); + let conn = open_db(&db)?; + conn.execute( + "INSERT INTO context_messages(source, chat, date, user_name, message, vector) + VALUES ('source', 'chat', '2026-09-24', 'user', 'permission probe', ?1)", + [vector_to_bytes(&encode_vector("permission probe"))], + )?; + set_context_db_permissions(&db)?; + + assert_eq!( + fs::metadata(dir.path())?.permissions().mode() & 0o777, + 0o700 + ); + assert_eq!(fs::metadata(&db)?.permissions().mode() & 0o777, 0o600); + for suffix in ["-wal", "-shm"] { + let sidecar = sqlite_sidecar_path(&db, suffix); + if sidecar.exists() { + assert_eq!(fs::metadata(sidecar)?.permissions().mode() & 0o777, 0o600); + } + } + Ok(()) +} + +#[test] +fn context_reads_fail_closed_on_a_missing_database_without_creating_it() { + let dir = tempdir().unwrap(); + let parent = dir.path().join("absent"); + let db = parent.join("context.sqlite3"); + let assert_migration_required = |label: &str, result: Result<()>| { + assert_eq!( + result.unwrap_err().to_string(), + CONTEXT_RETRIEVAL_MIGRATION_REQUIRED, + "{label}" + ); + }; + + assert_migration_required( + "search", + search(&db, Some("방"), None, "probe", "hybrid", 5).map(|_| ()), + ); + assert_migration_required( + "style_search", + style_search(&db, Some("방"), "probe", 5).map(|_| ()), + ); + assert_migration_required( + "style_profile", + style_profile(&db, "방", STYLE_USER, None).map(|_| ()), + ); + assert_migration_required( + "recipient_style_profile", + recipient_style_profile(&db, "방", "민수", None).map(|_| ()), + ); + assert_migration_required( + "response_time_stats", + response_time_stats(&db, "방", STYLE_USER, None).map(|_| ()), + ); + assert_migration_required( + "reply_decision_search", + reply_decision_search(&db, "방", "probe", 5).map(|_| ()), + ); + assert_migration_required( + "get_reply_decision", + get_reply_decision(&db, "db:7:42").map(|_| ()), + ); + assert_migration_required( + "live_context_sync_state", + live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, 7).map(|_| ()), + ); + assert_migration_required( + "context_reply_bundle", + context_reply_bundle(&db, "방", "probe", None).map(|_| ()), + ); + assert_migration_required( + "context_reply_bundle_for_recipient_excluding_live_events", + context_reply_bundle_for_recipient_excluding_live_events( + &db, + "방", + "probe", + None, + "민수", + 7, + &[42], + ) + .map(|_| ()), + ); + + assert!( + !parent.exists(), + "read paths must not create the context database or its directory" + ); +} + +#[cfg(unix)] +#[test] +fn context_reads_refuse_a_symlinked_database() { + let dir = tempdir().unwrap(); + let real = dir.path().join("real.sqlite3"); + let csv = fixture(dir.path(), "chat.csv", "symlink probe"); + index_csv(&real, "방", &csv).unwrap(); + let link = dir.path().join("link.sqlite3"); + std::os::unix::fs::symlink(&real, &link).unwrap(); + + assert_eq!( + search(&real, Some("방"), None, "symlink", "keyword", 5) + .unwrap() + .len(), + 1 + ); + assert_eq!( + search(&link, Some("방"), None, "symlink", "keyword", 5) + .unwrap_err() + .to_string(), + CONTEXT_RETRIEVAL_MIGRATION_REQUIRED + ); + assert_eq!( + context_reply_bundle(&link, "방", "symlink", None) + .unwrap_err() + .to_string(), + CONTEXT_RETRIEVAL_MIGRATION_REQUIRED + ); +} + +#[test] +fn read_only_context_connection_rejects_writes() -> Result<()> { + let dir = tempdir()?; + let db = dir.path().join("readonly.sqlite3"); + let csv = fixture(dir.path(), "chat.csv", "read only probe"); + index_csv(&db, "방", &csv)?; + + let reader = open_db_readonly(&db)?; + let write = reader.execute( + "INSERT INTO context_messages(source, chat, date, user_name, message, vector) + VALUES ('source', '방', '2026-01-01', '민수', 'write probe', x'')", + [], + ); + assert!( + matches!( + write, + Err(rusqlite::Error::SqliteFailure(ref error, _)) + if error.code == rusqlite::ErrorCode::ReadOnly + ), + "{write:?}" + ); + let count: i64 = reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { + row.get(0) + })?; + assert_eq!(count, 1); + Ok(()) +} + +#[cfg(unix)] +#[test] +fn context_database_open_refuses_a_symlinked_wal_sidecar() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let dir = tempdir()?; + let outside = dir.path().join("outside.bin"); + fs::write(&outside, b"not a wal")?; + fs::set_permissions(&outside, fs::Permissions::from_mode(0o644))?; + let db_dir = dir.path().join("ctx"); + fs::create_dir(&db_dir)?; + let db = db_dir.join("context.sqlite3"); + std::os::unix::fs::symlink(&outside, db_dir.join("context.sqlite3-wal"))?; + let csv = fixture(dir.path(), "chat.csv", "sidecar probe"); + + assert!(index_csv(&db, "방", &csv).is_err()); + assert!(!db.exists()); + assert_eq!(fs::metadata(&outside)?.permissions().mode() & 0o777, 0o644); + assert_eq!(fs::read(&outside)?, b"not a wal"); + Ok(()) +} + +#[test] +fn csv_reindex_truncates_the_wal_and_scrubs_replaced_text() -> Result<()> { + let dir = tempdir()?; + let db = dir.path().join("context.sqlite3"); + let retired = "retiredphrase7d28b"; + let csv = fixture(dir.path(), "chat.csv", retired); + index_csv(&db, "방", &csv)?; + + let concurrent_reader = open_db_readonly(&db)?; + let rows: i64 = + concurrent_reader.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { + row.get(0) + })?; + assert_eq!(rows, 1); + fs::write(&csv, "Date,User,Message\n2026-01-01,민수,replacement\n")?; + index_csv(&db, "방", &csv)?; + + let wal = fs::read(dir.path().join("context.sqlite3-wal")).unwrap_or_default(); + assert_eq!(wal.len(), 0); + let main_db = fs::read(&db)?; + assert!(!main_db + .windows(retired.len()) + .any(|window| window == retired.as_bytes())); + drop(concurrent_reader); + Ok(()) +} + +#[cfg(unix)] +#[test] +fn queue_loader_never_follows_a_symlink_or_creates_a_missing_queue() { + let dir = tempdir().unwrap(); + let queue = dir.path().join("reply-queue.sqlite3"); + { + let conn = Connection::open(&queue).unwrap(); + conn.execute_batch( + "CREATE TABLE reply_jobs(event_id TEXT, status TEXT, reply TEXT, updated_at REAL); + INSERT INTO reply_jobs VALUES('a','sent','보낸 문장',1000.0);", + ) + .unwrap(); + } + let before = fs::read(&queue).unwrap(); + assert_eq!(confirmed_self_sends_from_queue(&queue).unwrap().len(), 1); + assert_eq!(fs::read(&queue).unwrap(), before); + + let link = dir.path().join("linked-queue.sqlite3"); + std::os::unix::fs::symlink(&queue, &link).unwrap(); + assert!(confirmed_self_sends_from_queue(&link).unwrap().is_empty()); + + let missing_parent = dir.path().join("missing"); + let absent = missing_parent.join("reply-queue.sqlite3"); + assert!(confirmed_self_sends_from_queue(&absent).unwrap().is_empty()); + assert!(!missing_parent.exists()); +} + +fn style_profile_with_stats( + common_endings: &[(&str, usize)], + emoji_count: usize, + punctuation_count: usize, + common_tokens: &[(&str, usize)], +) -> StyleProfile { + let endings: BTreeMap = common_endings + .iter() + .map(|(k, v)| (k.to_string(), *v)) + .collect(); + let tokens: BTreeMap = common_tokens + .iter() + .map(|(k, v)| (k.to_string(), *v)) + .collect(); + let sample_count = common_endings.iter().map(|(_, v)| *v).sum::().max(1); + StyleProfile { + chat: "부자멘토멘티".to_string(), + source: "local-db".to_string(), + user: STYLE_USER.to_string(), + sample_count, + average_character_length: 10.0, + median_character_length: 10.0, + p90_character_length: 12.0, + casual_ending_count: 0, + casual_ending_counts_json: "{}".to_string(), + question_count: 0, + emoji_count, + punctuation_count, + common_endings_json: serde_json::to_string(&endings).unwrap(), + common_tokens_json: serde_json::to_string(&tokens).unwrap(), + policy_version: STYLE_POLICY_VERSION.to_string(), + } +} + +#[test] +fn derive_deferential_endings_are_formal_honorific() { + let profile = style_profile_with_stats(&[("습니다", 8), ("합니다", 4), ("요", 2)], 0, 5, &[]); + let (honorific, formality) = derive_style_profile_honorific_formality(&profile); + assert_eq!(honorific, Honorific::Honorific); + assert_eq!(formality, Formality::Formal); +} + +#[test] +fn derive_polite_with_emoji_is_informal_honorific() { + // 해요체 위주 + 이모지/축약이 섞이면 존댓말이지만 비격식체. + let profile = style_profile_with_stats(&[("요", 10), ("죠", 3)], 6, 2, &[("ㅋㅋ", 4)]); + let (honorific, formality) = derive_style_profile_honorific_formality(&profile); + assert_eq!(honorific, Honorific::Honorific); + assert_eq!(formality, Formality::Informal); +} + +#[test] +fn derive_casual_endings_are_informal_casual() { + let profile = style_profile_with_stats( + &[("어", 9), ("야", 4), ("지", 3), ("요", 1)], + 1, + 0, + &[("ㅋㅋㅋ", 5)], + ); + let (honorific, formality) = derive_style_profile_honorific_formality(&profile); + assert_eq!(honorific, Honorific::Casual); + assert_eq!(formality, Formality::Informal); +} + +#[test] +fn derive_no_ending_evidence_defaults_to_informal_casual() { + let profile = style_profile_with_stats(&[], 0, 0, &[]); + let (honorific, formality) = derive_style_profile_honorific_formality(&profile); + assert_eq!(honorific, Honorific::Casual); + assert_eq!(formality, Formality::Informal); +} + +#[test] +fn derive_deferential_overwhelmed_by_emoji_is_informal() { + // 합쇼체가 있어도 이모지·축약 신호가 우세하면 비격식체로 본다. + let profile = style_profile_with_stats(&[("습니다", 2), ("요", 6)], 20, 1, &[("ㅋㅋㅋ", 10)]); + let (honorific, formality) = derive_style_profile_honorific_formality(&profile); + assert_eq!(honorific, Honorific::Honorific); + assert_eq!(formality, Formality::Informal); +} + +#[test] +fn contraction_token_detection() { + assert!(is_contraction_token("ㅋㅋ")); + assert!(is_contraction_token("ㅠㅠㅠ")); + assert!(!is_contraction_token("회의")); + assert!(!is_contraction_token("")); +} + +fn fixture(dir: &Path, name: &str, message: &str) -> PathBuf { + let path = dir.join(name); + let mut file = fs::File::create(&path).unwrap(); + writeln!(file, "Date,User,Message\n2026-01-01,민수,{}", message).unwrap(); + path +} +fn insert_context_row(conn: &Connection, source: &str, chat: &str, date: &str, message: &str) { + conn.execute( + "INSERT INTO context_messages( + source, chat, date, user_name, message, vector + ) VALUES (?1, ?2, ?3, '민수', ?4, ?5)", + params![ + source, + chat, + date, + message, + vector_to_bytes(&encode_vector(message)), + ], + ) + .unwrap(); +} + +fn live_event( + chat_id: i64, + log_id: i64, + sender_name: &str, + message: &str, + sent_at: i64, +) -> LiveContextEvent { + LiveContextEvent { + chat_id, + log_id, + sender_name: sender_name.to_string(), + message: message.to_string(), + sent_at, + is_self: sender_name == STYLE_USER, + exclude_from_learning: false, + auto_generated: false, + attachment: String::new(), + message_type: 1, + interest_only: false, + } +} + +fn record_sent_reply(db: &Path, event_id: &str, chat: &str, reply: &str, sent_at: &str) { + let record = serde_json::json!({ + "event_id": event_id, + "chat": chat, + "author": "민수", + "received_at": "2026-01-01T00:00:00Z", + "message": format!("{event_id} incoming"), + "decision": "reply", + "reason": "direct_question", + "category": "question", + "context_match_count": 1, + "style_match_count": 1, + "best_context_score": 0.5, + "best_style_score": 0.5, + "prior_similarity": 0.0, + "scheduled_delay_seconds": 1.0, + "status": "scheduled", + "reply": reply, + "evidence_ids": ["context:test"], + "style_policy_version": STYLE_POLICY_VERSION, + }); + assert!(record_reply_decision(db, &record.to_string()).unwrap()); + assert!(update_reply_decision(db, event_id, "sent", Some(reply), Some(sent_at)).unwrap()); +} + +#[test] +fn conversational_style_filter_excludes_pasted_information() { + assert!(is_conversational_style_message("ㅋㅋㅋ 이건 좀 세긴 하네")); + assert!(!is_conversational_style_message("ㅋㅋ 이건 좀 세긴 하네")); + assert!(!is_conversational_style_message("ㅎㅎㅎ 이건 좀 세긴 하네")); + assert!(!is_conversational_style_message( + "1) 핵심 내용\n2) 세부 내용\n3) 참고 자료" + )); + assert!(!is_conversational_style_message( + "긴 정보 전달문 https://example.com 자세한 내용이 이어집니다" + )); + assert!(!is_conversational_style_message( + "이 문장은 실제 대화보다 정보 전달에 가까운 긴 문장이라서 말투 학습에서 제외해야 합니다." + )); + assert!(!is_conversational_style_message("1차 실무 면접")); + assert!(!is_conversational_style_message("사진")); + assert!(!is_conversational_style_message("@뉴스봇 환율")); + assert!(!is_conversational_style_message( + "문승현님이 부방장이 되었습니다." + )); + assert!(is_conversational_style_message("ㅇㅇ 저장해둘게")); + assert!(is_conversational_style_message( + "사용량 많으면 플러스가 낫긴 하지" + )); + assert!(!is_conversational_style_message("ㅇㅇ")); + assert!(!is_conversational_style_message("ㅇㅇㅋㅋㅋㅋ")); + assert!(!is_conversational_style_message( + "응, 사진 메시지도 확인해서 맥락에 맞게 답할 수 있어!" + )); + assert!(!is_conversational_style_message("대만 여행 알아보나 보네")); + assert!(!is_conversational_style_message( + "재정 관리 빡세게 하나보네" + )); + assert!(is_conversational_style_message( + "사용량 많으면 플러스가 낫긴 하지" + )); +} +#[test] +fn topic_lexicon_ascii_needles_are_lowercase() { + // `classify_message_topics` matches ASCII needles against the already + // lowercased haystack without re-lowercasing them (saves one String per + // needle per message). That shortcut is only correct while every ASCII + // needle is stored lowercase; this guards future lexicon edits. + for (topic, needles) in TOPIC_LEXICON { + for needle in *needles { + if needle.bytes().all(|byte| byte.is_ascii()) { + assert_eq!( + *needle, + needle.to_ascii_lowercase(), + "topic `{topic}` has a non-lowercase ASCII needle `{needle}`" + ); + } + } + } +} + +#[test] +fn assistant_tell_style_matches_case_insensitively() { + assert!(is_assistant_tell_style("I CAN HELP with that")); + assert!(is_assistant_tell_style("도와드릴 수 있어요")); + assert!(!is_assistant_tell_style("오늘 저녁에 뭐 해?")); +} + +#[test] +fn classify_message_topics_assigns_contact_and_ax() { + assert_eq!( + classify_message_topics("연락처 저장함 010-1234-5678"), + vec!["contact"] + ); + assert!(classify_message_topics("어제 번호 이야기해줫던거 머더라").contains(&"contact")); + assert!(classify_message_topics("AX API가 손쉬운 사용으로 카톡 조작함").contains(&"ax_macos")); + assert!(classify_message_topics("토큰 부자ㄷㄷ").contains(&"llm_tools")); + assert!(classify_message_topics("ㅋㅋㅋ").is_empty()); + assert!(classify_message_topics("코스피 배당주 투자").contains(&"stocks")); + assert!(classify_message_topics("업비트 비트코인").contains(&"coins")); + assert!(classify_message_topics("전세 부동산 경매").contains(&"real_estate")); + assert!(classify_message_topics("전세 부동산 경매").contains(&"auction")); + assert!(classify_message_topics("그록 인공지능").contains(&"ai")); + assert!(message_has_interest_topic("공모주 넣었어요")); + assert!(!message_has_interest_topic("ㅋㅋㅋ")); + let indexed = live_index_text( + "사진", + r#"{"urls":["https://grok.com/supergrok"],"src_message":"67% 슈퍼그록"}"#, + 2, + ); + assert!(indexed.contains("https://grok.com/supergrok")); + assert!(indexed.contains("슈퍼그록")); + assert!(message_has_interest_topic(&indexed)); +} + +#[test] +fn interest_only_indexes_investing_and_skips_chatter() { + let dir = tempdir().unwrap(); + let db = dir.path().join("interest-only.sqlite3"); + let chat_id = 415878504092105; + let chat = "변우중"; + let mut keep = live_event(chat_id, 11, "변우중", "비트코인 지금 들어가도 됨?", 100); + keep.interest_only = true; + let mut skip = live_event(chat_id, 12, "변우중", "ㅋㅋㅋ", 110); + skip.interest_only = true; + let mut photo = live_event(chat_id, 13, "변우중", "사진", 120); + photo.interest_only = true; + photo.message_type = 2; + photo.attachment = r#"{"src_message":"강남 아파트 경매 보셈"}"#.into(); + ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &[keep, skip, photo], + true, + false, + ) + .unwrap(); + let conn = open_db(&db).unwrap(); + let messages: Vec = conn + .prepare("SELECT message FROM context_messages ORDER BY id") + .unwrap() + .query_map([], |row| row.get(0)) + .unwrap() + .collect::>() + .unwrap(); + assert_eq!(messages.len(), 2); + assert!(messages.iter().any(|row| row.contains("비트코인"))); + assert!(messages.iter().any(|row| row.contains("경매"))); + assert!(!messages.iter().any(|row| row == "ㅋㅋㅋ")); + let topics: Vec = conn + .prepare("SELECT DISTINCT topic FROM context_message_topics ORDER BY topic") + .unwrap() + .query_map([], |row| row.get(0)) + .unwrap() + .collect::>() + .unwrap(); + assert!(topics.iter().any(|topic| topic == "coins")); +} + +#[test] +fn topic_overlay_recalls_contact_thread_without_shared_keyword() { + let dir = tempdir().unwrap(); + let db = dir.path().join("topics.sqlite3"); + let path = dir.path().join("chat.csv"); + fs::write( + &path, + "Date,User,Message\n\ +2026-01-01 10:00:00,문승현,연락처 저장함 010-1234-5678\n\ +2026-01-01 10:00:10,최연우,ㅇㅇ 저장해둘게\n\ +2026-01-01 12:00:00,문승현,AX API가 손쉬운 사용으로 카톡 조작함\n\ +2026-01-01 12:00:05,최연우,컴퓨터 유즈보다 AX가 나음\n\ +2026-01-01 13:00:00,문승현,토큰 부자ㄷㄷ\n", + ) + .unwrap(); + assert_eq!(index_csv(&db, "부자멘토멘티", &path).unwrap(), 5); + + let recalled = search( + &db, + Some("부자멘토멘티"), + None, + "어제 번호 이야기해줫던거 머더라", + "hybrid", + 5, + ) + .unwrap(); + assert!( + recalled + .iter() + .any(|row| row.message.contains("010-") || row.message.contains("연락처")), + "{recalled:?}" + ); + + let ax = search( + &db, + Some("부자멘토멘티"), + None, + "AX API 손쉬운 사용", + "hybrid", + 5, + ) + .unwrap(); + assert!( + ax.iter() + .any(|row| row.message.contains("손쉬운 사용") || row.message.contains("AX")), + "{ax:?}" + ); + assert!(!ax.iter().any(|row| row.message.contains("010-")), "{ax:?}"); + + let conn = open_db(&db).unwrap(); + conn.execute("DELETE FROM context_message_topics", []) + .unwrap(); + conn.execute("DELETE FROM context_topic_stats", []).unwrap(); + conn.execute( + "UPDATE context_retrieval_meta SET value = '1' WHERE key = 'live_context_schema'", + [], + ) + .unwrap(); + drop(conn); + let conn = open_db(&db).unwrap(); + let version: String = conn + .query_row( + "SELECT value FROM context_retrieval_meta WHERE key = 'live_context_schema'", + [], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(version, LIVE_CONTEXT_SCHEMA_VERSION); + let n: i64 = conn + .query_row("SELECT COUNT(*) FROM context_message_topics", [], |row| { + row.get(0) + }) + .unwrap(); + assert!(n >= 3, "backfill topics {n}"); + let contact: i64 = conn + .query_row( + "SELECT message_count FROM context_topic_stats + WHERE chat = '부자멘토멘티' AND topic = 'contact'", + [], + |row| row.get(0), + ) + .unwrap(); + assert!(contact >= 1, "contact stats {contact}"); +} + +#[test] +fn indexes_and_scopes_by_chat_and_source() { + let dir = tempdir().unwrap(); + let db = dir.path().join("index.sqlite3"); + let first = fixture(dir.path(), "first.csv", "세금 신고 일정"); + let second = fixture(dir.path(), "second.csv", "점심 김치찌개"); + assert_eq!(index_csv(&db, "같은 이름", &first).unwrap(), 1); + assert_eq!(index_csv(&db, "같은 이름", &second).unwrap(), 1); + assert_eq!( + search(&db, Some("같은 이름"), None, "세금", "keyword", 5) + .unwrap() + .len(), + 1 + ); + assert_eq!( + search( + &db, + Some("같은 이름"), + Some(&second.canonicalize().unwrap().display().to_string()), + "세금", + "keyword", + 5 + ) + .unwrap() + .len(), + 0 + ); +} +#[test] +fn indexes_choi_style_rows_in_separate_vector_table() { + let dir = tempdir().unwrap(); + let db = dir.path().join("index.sqlite3"); + let path = dir.path().join("chat.csv"); + fs::write( + &path, + "Date,User,Message\n2026-01-01,최연우,ㅋㅋㅋ 이건 좀 세긴 하네\n2026-01-01,문승현,다른 사람 메시지\n", + ) + .unwrap(); + + assert_eq!(index_csv(&db, "부자멘토멘티", &path).unwrap(), 2); + let results = style_search(&db, Some("부자멘토멘티"), "세긴 하네", 5).unwrap(); + assert_eq!(results.len(), 1); + assert_eq!(results[0].user, STYLE_USER); + assert_eq!(results[0].message, "ㅋㅋㅋ 이건 좀 세긴 하네"); + + let conn = open_db(&db).unwrap(); + let count: i64 = conn + .query_row("SELECT COUNT(*) FROM owner_style", [], |row| row.get(0)) + .unwrap(); + assert_eq!(count, 1); +} + +#[test] +fn indexes_and_reads_response_time_from_vector_db() { + let dir = tempdir().unwrap(); + let db = dir.path().join("index.sqlite3"); + let path = dir.path().join("chat.csv"); + fs::write( + &path, + "Date,User,Message\n\ + 2026-01-01 00:00:00,문승현,질문 하나\n\ + 2026-01-01 00:00:10,최연우,답변 하나\n\ + 2026-01-01 00:01:00,민수,질문 둘\n\ + 2026-01-01 00:03:00,최연우,답변 둘\n", + ) + .unwrap(); + + index_csv(&db, "부자멘토멘티", &path).unwrap(); + let stats = response_time_stats(&db, "부자멘토멘티", STYLE_USER, None) + .unwrap() + .unwrap(); + assert_eq!(stats.sample_count, 2); + assert!((stats.average_seconds - 65.0).abs() < f64::EPSILON); + assert!((stats.median_seconds - 65.0).abs() < f64::EPSILON); + assert_eq!(stats.p90_seconds, 109.0); + assert_eq!(stats.min_seconds, 10.0); + assert_eq!(stats.max_seconds, 120.0); + assert!(stats.stddev_seconds > 0.0); + assert!(stats.distribution.is_none()); +} + +#[test] +fn response_time_distribution_finds_three_empirical_modes() { + let mut delays = Vec::new(); + delays.extend((0..16).map(|index| index as f64)); + delays.extend((0..8).map(|index| 40.0 + index as f64 * 10.0)); + delays.extend((0..8).map(|index| 500.0 + index as f64 * 100.0)); + let stats = summarize_response_delays("방", "source", STYLE_USER, delays).unwrap(); + let distribution = stats.distribution.unwrap(); + assert_eq!(distribution.schema_version, 2); + assert_eq!(distribution.model_kind, "bounded-normal-mixture"); + assert_eq!(distribution.sample_count, 32); + assert_eq!(distribution.retained_sample_count, 32); + assert_eq!(distribution.components.len(), 3); + assert_eq!(distribution.components[0].name, "immediate"); + assert_eq!(distribution.components[1].name, "short"); + assert_eq!(distribution.components[2].name, "delayed"); + assert_eq!( + distribution.components[0].sample_count + + distribution.components[1].sample_count + + distribution.components[2].sample_count, + 32 + ); + assert_eq!( + distribution.components[0].upper_seconds, + distribution.split_seconds[0] + ); + assert_eq!( + distribution.components[1].upper_seconds, + distribution.split_seconds[1] + ); + assert_eq!( + distribution.components[2].upper_seconds, + distribution.global_upper_seconds + ); + assert_eq!(distribution.split_seconds, vec![15.0, 110.0]); + assert!((distribution.global_upper_seconds - 890.0).abs() < 1e-9); + assert_eq!(distribution.tail_winsorized_count, 4); + assert_eq!(distribution.components[0].weight, 0.5); + assert_eq!(distribution.components[1].weight, 0.25); + assert_eq!(distribution.components[2].weight, 0.25); + assert!(response_time_distribution_is_valid(&distribution)); +} + +#[test] +fn response_time_distribution_fails_closed_for_weak_or_degenerate_samples() { + assert!(fit_response_time_distribution(&vec![1.0; 31]).is_none()); + assert!(fit_response_time_distribution(&vec![1.0; 32]).is_none()); + let mut only_two_modes = vec![1.0; 16]; + only_two_modes.extend(vec![100.0; 16]); + assert!(fit_response_time_distribution(&only_two_modes).is_none()); +} + +/// Exhaustive reference for the shipped exact split search. Kept in the +/// test module so every randomized case can assert the optimized path +/// returns the identical distribution. +fn exhaustive_response_time_distribution(delays: &[f64]) -> Option { + if delays.len() < RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES + || delays.iter().any(|delay| { + !delay.is_finite() || !(0.0..=MAX_RESPONSE_DELAY_SECONDS as f64).contains(delay) + }) + { + return None; + } + let transformed = delays.iter().map(|delay| delay.ln_1p()).collect::>(); + let mut prefix_sum = vec![0.0; transformed.len() + 1]; + let mut prefix_square_sum = vec![0.0; transformed.len() + 1]; + for (index, value) in transformed.iter().enumerate() { + prefix_sum[index + 1] = prefix_sum[index] + value; + prefix_square_sum[index + 1] = prefix_square_sum[index] + value * value; + } + let segment_sse = |start: usize, end: usize| { + let count = (end - start) as f64; + let sum = prefix_sum[end] - prefix_sum[start]; + let square_sum = prefix_square_sum[end] - prefix_square_sum[start]; + (square_sum - sum * sum / count).max(0.0) + }; + let mut best: Option<(f64, usize, usize)> = None; + let minimum = RESPONSE_TIME_DISTRIBUTION_MIN_COMPONENT_SAMPLES; + for first_split in minimum..=delays.len() - 2 * minimum { + if delays[first_split - 1] >= delays[first_split] { + continue; + } + for second_split in first_split + minimum..=delays.len() - minimum { + if delays[second_split - 1] >= delays[second_split] { + continue; + } + let sse = segment_sse(0, first_split) + + segment_sse(first_split, second_split) + + segment_sse(second_split, delays.len()); + if best.as_ref().is_none_or(|(best_sse, _, _)| sse < *best_sse) { + best = Some((sse, first_split, second_split)); + } + } + } + let (_, first_split, second_split) = best?; + let global_upper_seconds = response_delay_percentile(delays, 0.9); + let immediate_upper_seconds = delays[first_split - 1].min(global_upper_seconds); + let short_lower_seconds = delays[first_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); + let short_upper_seconds = delays[second_split - 1].min(global_upper_seconds); + let delayed_lower_seconds = delays[second_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); + if immediate_upper_seconds < MIN_SCHEDULED_RESPONSE_DELAY_SECONDS + || short_lower_seconds > short_upper_seconds + || delayed_lower_seconds > global_upper_seconds + || immediate_upper_seconds >= short_lower_seconds + || short_upper_seconds >= delayed_lower_seconds + { + return None; + } + let bounds = [ + ( + MIN_SCHEDULED_RESPONSE_DELAY_SECONDS, + immediate_upper_seconds, + ), + (short_lower_seconds, short_upper_seconds), + (delayed_lower_seconds, global_upper_seconds), + ]; + let ranges = [ + (0, first_split), + (first_split, second_split), + (second_split, delays.len()), + ]; + let mut components = Vec::with_capacity(RESPONSE_TIME_DISTRIBUTION_COMPONENTS); + for ((start, end), ((lower_seconds, upper_seconds), name)) in ranges + .into_iter() + .zip(bounds.into_iter().zip(["immediate", "short", "delayed"])) + { + let values = delays[start..end] + .iter() + .map(|delay| delay.clamp(lower_seconds, upper_seconds)) + .collect::>(); + let mean = values.iter().sum::() / values.len() as f64; + let stddev = (values + .iter() + .map(|value| (value - mean).powi(2)) + .sum::() + / values.len() as f64) + .sqrt(); + if !stddev.is_finite() || stddev <= 0.0 { + return None; + } + components.push(ResponseTimeComponent { + name: name.to_string(), + sample_count: values.len(), + weight: values.len() as f64 / delays.len() as f64, + normal_location_seconds: mean, + normal_scale_seconds: stddev, + lower_seconds, + upper_seconds, + }); + } + let distribution = ResponseTimeDistribution { + schema_version: RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION, + policy_version: RESPONSE_TIME_DISTRIBUTION_POLICY_VERSION.to_string(), + model_kind: RESPONSE_TIME_DISTRIBUTION_MODEL_KIND.to_string(), + fit_transform: RESPONSE_TIME_DISTRIBUTION_FIT_TRANSFORM.to_string(), + sample_count: delays.len(), + retained_sample_count: delays.len(), + tail_winsorized_count: delays + .iter() + .filter(|delay| **delay > global_upper_seconds) + .count(), + split_seconds: vec![immediate_upper_seconds, short_upper_seconds], + global_upper_seconds, + components, + }; + response_time_distribution_is_valid(&distribution).then_some(distribution) +} + +fn deterministic_delays(seed: u64, count: usize) -> Vec { + let mut state = seed + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + let mut next = move || { + state = state + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + (state >> 11) as f64 / (1u64 << 53) as f64 + }; + let mut values = (0..count) + .map(|index| { + // Three latent modes with heavy duplicates so the strict-increase + // boundary rule and the tie-break both get exercised. + let mode = index % 3; + let base = match mode { + 0 => 12.0, + 1 => 90.0, + _ => 900.0, + }; + let jitter = (next() * 40.0).floor(); + let duplicate = if next() < 0.35 { 0.0 } else { 1.0 }; + base + jitter * duplicate + }) + .collect::>(); + values.sort_by(|left, right| left.partial_cmp(right).unwrap()); + values +} + +#[test] +fn response_time_distribution_split_search_matches_exhaustive() { + for count in [32usize, 33, 40, 100, 257, 1000] { + for seed in 1u64..=8 { + let delays = deterministic_delays(seed, count); + let fast = fit_response_time_distribution(&delays); + let reference = exhaustive_response_time_distribution(&delays); + assert_eq!( + fast.is_some(), + reference.is_some(), + "presence diverged at count={count} seed={seed}" + ); + let (Some(fast), Some(reference)) = (fast, reference) else { + continue; + }; + assert_eq!( + fast.split_seconds, reference.split_seconds, + "split diverged at count={count} seed={seed}" + ); + assert_eq!(fast.global_upper_seconds, reference.global_upper_seconds); + assert_eq!(fast.tail_winsorized_count, reference.tail_winsorized_count); + assert_eq!(fast.sample_count, reference.sample_count); + for (optimized, naive) in fast.components.iter().zip(reference.components.iter()) { + assert_eq!(optimized.name, naive.name); + assert_eq!(optimized.sample_count, naive.sample_count); + assert_eq!(optimized.weight, naive.weight); + assert_eq!( + optimized.normal_location_seconds, + naive.normal_location_seconds + ); + assert_eq!(optimized.normal_scale_seconds, naive.normal_scale_seconds); + assert_eq!(optimized.lower_seconds, naive.lower_seconds); + assert_eq!(optimized.upper_seconds, naive.upper_seconds); + } + } + } +} + +#[test] +fn response_time_distribution_split_search_handles_plateaus() { + // A fully flat sample set has no strict-increase boundary, so the fit + // must fail closed exactly like the exhaustive scan. + assert!(fit_response_time_distribution(&vec![7.0; 64]).is_none()); + // Duplicate runs at the boundaries leave only a narrow strict-increase + // window; the optimized search must still find the same pair. + let mut delays = vec![3.0; 12]; + delays.extend((0..12).map(|index| 20.0 + index as f64)); + delays.extend(vec![400.0; 12]); + delays.extend((0..12).map(|index| 800.0 + index as f64)); + assert_eq!( + fit_response_time_distribution(&delays).map(|item| item.split_seconds), + exhaustive_response_time_distribution(&delays).map(|item| item.split_seconds), + ); + // Heavy duplication ties many candidate pairs on SSE, so the tie-break + // (lowest first then lowest second) is what keeps the two paths equal. + for seed in 1u64..=16 { + let delays = duplicate_heavy_delays(seed, 200); + assert_eq!( + fit_response_time_distribution(&delays).map(|item| item.split_seconds), + exhaustive_response_time_distribution(&delays).map(|item| item.split_seconds), + "tie-break diverged at seed={seed}" + ); + } +} + +fn duplicate_heavy_delays(seed: u64, count: usize) -> Vec { + let mut state = seed + .wrapping_mul(2862933555777941757) + .wrapping_add(3037000493); + let mut next = move || { + state = state + .wrapping_mul(2862933555777941757) + .wrapping_add(3037000493); + (state >> 33) as f64 / (1u64 << 31) as f64 + }; + // Only six distinct levels, so most segment SSEs tie and the boundary + // rule plus tie-break decide the winner. + let levels = [10.0, 30.0, 30.0, 120.0, 600.0, 600.0]; + let mut values = (0..count) + .map(|_| levels[(next() * levels.len() as f64) as usize % levels.len()]) + .collect::>(); + values.sort_by(|left, right| left.partial_cmp(right).unwrap()); + values +} + +#[test] +fn response_time_distribution_split_search_scales_to_live_sample_counts() { + // The live chats carry up to ~20k response samples, where the previous + // O(N^2) pair scan was the slow path. A dense continuous shape is the + // worst case for it, because no duplicate run removes candidate + // boundaries: the exhaustive scan takes ~660 ms at 20,174 samples on + // this machine, and the O(N log N) search takes ~2.4 ms. The bound + // below is 100x the measured search while still failing an O(N^2) + // regression. + let delays = shaped_delays(99, 20_000, 2); + let started = std::time::Instant::now(); + let fitted = fit_response_time_distribution(&delays).expect("live shape must fit"); + let elapsed = started.elapsed(); + assert_eq!(fitted.sample_count, 20_000); + assert!( + elapsed < std::time::Duration::from_millis(250), + "exact split search took {elapsed:?}" + ); +} + +#[test] +fn response_time_distribution_split_search_matches_exhaustive_across_shapes() { + // The divide-and-conquer split search rests on the quadrangle + // inequality of the segment SSE, a property of sorted shapes rather + // than of arbitrary inputs, so the equivalence is pinned against the + // exhaustive scan over a wide battery instead of assumed: every + // strict-increase window, every plateau, and every duplicate tie is + // compared on both paths, at counts around the minimum window (32, 33, + // 34) as well as at live scale. + for seed in 1u64..=24 { + for count in [32usize, 33, 34, 40, 47, 55, 64, 96, 150, 311, 640, 1000] { + for shape in 0..6u64 { + let delays = shaped_delays(seed * 7 + shape, count, shape); + let fast = fit_response_time_distribution(&delays); + let reference = exhaustive_response_time_distribution(&delays); + assert_eq!( + fast.is_some(), + reference.is_some(), + "presence diverged at count={count} seed={seed} shape={shape}" + ); + let (Some(fast), Some(reference)) = (fast, reference) else { + continue; + }; + assert_eq!( + fast.split_seconds, reference.split_seconds, + "split diverged at count={count} seed={seed} shape={shape}" + ); + assert_eq!(fast.global_upper_seconds, reference.global_upper_seconds); + assert_eq!(fast.tail_winsorized_count, reference.tail_winsorized_count); + for (optimized, naive) in fast.components.iter().zip(reference.components.iter()) { + assert_eq!(optimized.name, naive.name); + assert_eq!(optimized.sample_count, naive.sample_count); + assert_eq!(optimized.weight, naive.weight); + assert_eq!( + optimized.normal_location_seconds, + naive.normal_location_seconds + ); + assert_eq!(optimized.normal_scale_seconds, naive.normal_scale_seconds); + assert_eq!(optimized.lower_seconds, naive.lower_seconds); + assert_eq!(optimized.upper_seconds, naive.upper_seconds); + } + } + } + } +} + +/// Six sorted shapes: three equal modes, a coarse staircase, a dense +/// uniform spread, a heavy-tail sweep, a six-level duplicate ladder, and a +/// stair of long duplicate runs. The last two collapse most candidate +/// boundaries, so they exercise the fail-closed paths and the tie-break. +fn shaped_delays(seed: u64, count: usize, shape: u64) -> Vec { + let mut state = seed + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + let mut next = move || { + state = state + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + (state >> 11) as f64 / (1u64 << 53) as f64 + }; + let mut values = (0..count) + .map(|index| match shape { + 0 => { + let base = match index % 3 { + 0 => 8.0, + 1 => 90.0, + _ => 700.0, + }; + base + (next() * 30.0).floor() + } + 1 => (index / 4) as f64 * 25.0 + (next() * 6.0).floor(), + 2 => next() * 900.0, + 3 => (next().powf(2.5)) * 900.0, + 4 => { + const LEVELS: [f64; 6] = [10.0, 30.0, 30.0, 120.0, 600.0, 600.0]; + LEVELS[(next() * LEVELS.len() as f64) as usize % LEVELS.len()] + } + _ => (index / 6) as f64 * 120.0, + }) + .collect::>(); + values.sort_by(|left, right| left.partial_cmp(right).unwrap()); + values +} + +#[test] +fn response_time_distribution_migration_is_additive_backfills_and_repairs() { + let dir = tempdir().unwrap(); + let db = dir.path().join("migration.sqlite3"); + let legacy = Connection::open(&db).unwrap(); + legacy + .execute_batch( + "CREATE TABLE response_time_stats( + chat TEXT NOT NULL, source TEXT NOT NULL, user_name TEXT NOT NULL, + sample_count INTEGER NOT NULL, average_seconds REAL NOT NULL, + median_seconds REAL NOT NULL, p90_seconds REAL NOT NULL, + min_seconds REAL NOT NULL, max_seconds REAL NOT NULL, + max_window_seconds INTEGER NOT NULL, + stddev_seconds REAL NOT NULL DEFAULT 0.0, + PRIMARY KEY(chat, source, user_name) + );", + ) + .unwrap(); + drop(legacy); + let conn = open_db(&db).unwrap(); + let columns = conn + .prepare("PRAGMA table_info(response_time_stats)") + .unwrap() + .query_map([], |row| row.get::<_, String>(1)) + .unwrap() + .collect::>>() + .unwrap(); + assert!(columns.contains(&"distribution_schema_version".to_string())); + assert!(columns.contains(&"distribution_json".to_string())); + let source = "local-db:migration"; + conn.execute( + "INSERT INTO context_sources( + source,kind,account_fingerprint,chat_id,chat,authoritative, + checkpoint_log_id,pending_burst_json,summary_dirty,sync_status,updated_at + ) VALUES(?1,'local_db',?2,42,'방',1,64,'[]',0,'ready',?3)", + params![source, TEST_ACCOUNT_FINGERPRINT, Utc::now().to_rfc3339()], + ) + .unwrap(); + let mut delays = Vec::new(); + delays.extend((0..16).map(|index| index as f64)); + delays.extend((0..8).map(|index| 40.0 + index as f64 * 10.0)); + delays.extend((0..8).map(|index| 500.0 + index as f64 * 100.0)); + for (index, delay) in delays.iter().enumerate() { + conn.execute( + "INSERT INTO response_time_samples( + source,chat_id,reply_log_id,prompt_log_id,recipient,delay_seconds + ) VALUES(?1,42,?2,?3,'민수',?4)", + params![source, 100 + index as i64, 10 + index as i64, delay], + ) + .unwrap(); + } + conn.execute( + "INSERT INTO response_time_stats( + chat,source,user_name,sample_count,average_seconds,median_seconds, + p90_seconds,min_seconds,max_seconds,max_window_seconds,stddev_seconds, + distribution_schema_version,distribution_json + ) VALUES('방',?1,?2,32,0,0,890,0,1200,86400,1,1,'{}')", + params![source, STYLE_USER], + ) + .unwrap(); + conn.execute( + "INSERT INTO context_retrieval_meta(key,value) + VALUES('response_time_distribution_schema',?1) + ON CONFLICT(key) DO UPDATE SET value=excluded.value", + [RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION.to_string()], + ) + .unwrap(); + drop(conn); + + let repaired = open_db(&db).unwrap(); + let (version, json): (i64, String) = repaired + .query_row( + "SELECT distribution_schema_version,distribution_json + FROM response_time_stats WHERE source=?1", + [source], + |row| Ok((row.get(0)?, row.get(1)?)), + ) + .unwrap(); + assert_eq!(version, RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION as i64); + let distribution: ResponseTimeDistribution = serde_json::from_str(&json).unwrap(); + assert!(response_time_distribution_is_valid(&distribution)); + drop(repaired); + let reopened = open_db(&db).unwrap(); + let unchanged: String = reopened + .query_row( + "SELECT distribution_json FROM response_time_stats WHERE source=?1", + [source], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(unchanged, json); +} +#[test] +fn reindex_removes_old_rows_and_vector_is_deterministic() { + let dir = tempdir().unwrap(); + let db = dir.path().join("index.sqlite3"); + let path = fixture(dir.path(), "chat.csv", "old phrase"); + index_csv(&db, "방", &path).unwrap(); + fs::write(&path, "Date,User,Message\n2026-01-01,민수,new phrase\n").unwrap(); + index_csv(&db, "방", &path).unwrap(); + assert!(search(&db, Some("방"), None, "old", "keyword", 5) + .unwrap() + .is_empty()); + assert_eq!(encode_vector("same"), encode_vector("same")); +} + +#[test] +fn relative_db_and_invalid_vector_query_are_safe() { + let dir = tempdir().unwrap(); + let db = dir.path().join("relative.sqlite3"); + let path = fixture(dir.path(), "chat.csv", "hello"); + index_csv(&db, "방", &path).unwrap(); + // Token-less queries (`!!!`) no longer fail the whole vector lookup: + // they return an empty hit list so style/timing bundles keep flowing. + let hits = search(&db, Some("방"), None, "!!!", "vector", 5).unwrap(); + assert!(hits.is_empty()); +} +#[test] +fn records_and_searches_reply_decisions() { + let dir = tempdir().unwrap(); + let db = dir.path().join("index.sqlite3"); + let record = serde_json::json!({ + "event_id": "event-1", + "chat": "부자멘토멘티", + "author": "민수", + "received_at": "2026-01-01T00:00:00Z", + "message": "세금 신고 일정이 궁금해", + "decision": "reply", + "reason": "direct_question", + "category": "question", + "context_match_count": 2, + "style_match_count": 3, + "best_context_score": 0.8, + "best_style_score": 0.9, + "prior_similarity": 0.0, + "scheduled_delay_seconds": 15.0, + "status": "scheduled", + "reply": "지난번처럼 일정 확인해보면 돼", + "evidence_ids": [ + "context:abc", + "style:def", + "timing:2:empirical-log1p-three-means-p90-v1:immediate:w0.5:lo5:hi17" + ], + "style_policy_version": "ordinary-conversation-v3", + "provenance": { + "rerank": {"fallback": "none", "policy_rejections": []}, + "response_timing": {"component": "immediate", "component_weight": 0.5} + } + }); + record_reply_decision(&db, &record.to_string()).unwrap(); + let matches = reply_decision_search(&db, "부자멘토멘티", "세금 신고", 5).unwrap(); + assert_eq!(matches.len(), 1); + assert_eq!(matches[0].decision, "reply"); + assert_eq!(matches[0].status, "scheduled"); + assert!(update_reply_decision( + &db, + "event-1", + "sent", + Some("답변 완료"), + Some("2026-01-01T00:00:15Z") + ) + .unwrap()); + let updated = reply_decision_search(&db, "부자멘토멘티", "세금 신고", 5).unwrap(); + assert_eq!(updated[0].status, "sent"); + assert!(updated[0] + .evidence_json + .contains("ordinary-conversation-v3")); + assert!(updated[0] + .evidence_json + .contains("timing:2:empirical-log1p-three-means-p90-v1:immediate:w0.5:lo5:hi17")); + assert!(updated[0].evidence_json.contains("response_timing")); + assert!(updated[0].evidence_json.contains("immediate")); +} +#[test] +fn reply_decision_boundary_validation_and_stale_noop() { + let dir = tempdir().unwrap(); + let db = dir.path().join("reply-validation.sqlite3"); + let base = serde_json::json!({ + "event_id": "event-1", + "chat": "chat-a", + "author": "민수", + "received_at": "2026-01-01T00:00:00Z", + "message": "질문", + "decision": "reply", + "reason": "question", + "category": "question", + "context_match_count": 1, + "style_match_count": 1, + "best_context_score": 0.5, + "best_style_score": 0.5, + "prior_similarity": 0.5, + "scheduled_delay_seconds": 1.0, + "status": "scheduled", + "reply": "답변", + "evidence_ids": ["context:1"], + "style_policy_version": STYLE_POLICY_VERSION, + }); + let mut invalid = base.clone(); + invalid["evidence_ids"] = serde_json::json!([]); + assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); + let mut invalid = base.clone(); + invalid["style_policy_version"] = serde_json::json!("legacy-v1"); + assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); + let mut invalid = base.clone(); + invalid["best_context_score"] = serde_json::json!(-0.1); + assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); + let mut invalid = base.clone(); + invalid["scheduled_delay_seconds"] = serde_json::json!(-1.0); + assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); + for reply in ["ㅋ", "ㅋㅋ", "ㅎ", "ㅎㅎ", "ㅎㅎㅎ"] { + let mut invalid = base.clone(); + invalid["reply"] = serde_json::json!(reply); + assert!(record_reply_decision(&db, &invalid.to_string()).is_err()); + } + + assert!(record_reply_decision(&db, &base.to_string()).unwrap()); + assert!(update_reply_decision(&db, "event-1", "sent", Some("ㅎㅎㅎ"), None).is_err()); + let mut stale = base; + stale["status"] = serde_json::json!("pending"); + assert!(!record_reply_decision(&db, &stale.to_string()).unwrap()); + + let mut skip = serde_json::json!({ + "event_id": "skip-1", + "chat": "chat-a", + "author": "민수", + "received_at": "2026-01-01T00:00:00Z", + "message": "공지", + "decision": "skip", + "reason": "not_actionable", + "category": "other", + "context_match_count": 0, + "style_match_count": 0, + "best_context_score": 0.0, + "best_style_score": 0.0, + "prior_similarity": 0.0, + "scheduled_delay_seconds": 0.0, + "status": "skipped", + "reply": null, + "evidence_ids": [], + "style_policy_version": "", + }); + assert!(record_reply_decision(&db, &skip.to_string()).unwrap()); + skip["evidence_ids"] = serde_json::json!([""]); + assert!(record_reply_decision(&db, &skip.to_string()).is_err()); +} + +#[test] +fn terminal_skip_clears_a_previously_scheduled_reply_and_sent_marker() { + let dir = tempdir().unwrap(); + let db = dir.path().join("reply-terminal-skip.sqlite3"); + let mut record = serde_json::json!({ + "event_id": "event-skip", + "chat": "chat-a", + "author": "민수", + "received_at": "2026-01-01T00:00:00Z", + "message": "질문", + "decision": "reply", + "reason": "question", + "category": "question", + "context_match_count": 1, + "style_match_count": 1, + "best_context_score": 0.5, + "best_style_score": 0.5, + "prior_similarity": 0.5, + "scheduled_delay_seconds": 1.0, + "status": "scheduled", + "reply": "답변", + "evidence_ids": ["context:1"], + "style_policy_version": STYLE_POLICY_VERSION, + }); + assert!(record_reply_decision(&db, &record.to_string()).unwrap()); + let conn = Connection::open(&db).unwrap(); + conn.execute( + "UPDATE reply_decisions SET sent_at='2026-01-01T00:00:01Z' WHERE event_id='event-skip'", + [], + ) + .unwrap(); + drop(conn); + + record["decision"] = serde_json::json!("skip"); + record["reason"] = serde_json::json!("stale_backlog"); + record["category"] = serde_json::json!("policy"); + record["scheduled_delay_seconds"] = serde_json::json!(0.0); + record["status"] = serde_json::json!("skipped"); + record["reply"] = serde_json::Value::Null; + record["evidence_ids"] = serde_json::json!([]); + record["style_policy_version"] = serde_json::json!(""); + assert!(record_reply_decision(&db, &record.to_string()).unwrap()); + + let conn = Connection::open(&db).unwrap(); + let row = conn + .query_row( + "SELECT decision,status,reason,category,reply,sent_at FROM reply_decisions WHERE event_id='event-skip'", + [], + |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, String>(2)?, + row.get::<_, String>(3)?, + row.get::<_, Option>(4)?, + row.get::<_, Option>(5)?, + )) + }, + ) + .unwrap(); + assert_eq!( + row, + ( + "skip".to_string(), + "skipped".to_string(), + "stale_backlog".to_string(), + "policy".to_string(), + None, + None, + ) + ); +} + +#[test] +fn exact_reply_decisions_precede_bounded_similarity_candidates() { + let dir = tempdir().unwrap(); + let db = dir.path().join("reply-exact.sqlite3"); + let make_record = |event_id: String, message: String| { + serde_json::json!({ + "event_id": event_id, + "chat": "chat-a", + "author": "민수", + "received_at": "2026-01-01T00:00:00Z", + "message": message, + "decision": "skip", + "reason": "not_actionable", + "category": "other", + "context_match_count": 0, + "style_match_count": 0, + "best_context_score": 0.0, + "best_style_score": 0.0, + "prior_similarity": 0.0, + "scheduled_delay_seconds": 0.0, + "status": "skipped", + "reply": null, + "evidence_ids": [], + "style_policy_version": "", + }) + }; + record_reply_decision( + &db, + &make_record("exact-old".into(), "target duplicate".into()).to_string(), + ) + .unwrap(); + for index in 0..(REPLY_DECISION_CANDIDATE_CAP + 16) { + record_reply_decision( + &db, + &make_record( + format!("event-{index:03}"), + format!("other message {index}"), + ) + .to_string(), + ) + .unwrap(); + } + let results = reply_decision_search(&db, "chat-a", "target duplicate", 5).unwrap(); + assert_eq!( + results.first().map(|result| result.event_id.as_str()), + Some("exact-old") + ); + assert_eq!(results.first().map(|result| result.score), Some(1.0)); +} + +#[test] +fn reply_projection_reports_updates_and_rejects_regression() { + let dir = tempdir().unwrap(); + let db = dir.path().join("reply.sqlite3"); + let first = project_reply_decision(&db, "db:7:42", "scheduled", r#"{"source":"db"}"#).unwrap(); + assert!(first.applied); + assert!(first.updated); + let sent = project_reply_decision(&db, "db:7:42", "sent", r#"{"confirmed":true}"#).unwrap(); + assert!(sent.updated); + let stale = project_reply_decision(&db, "db:7:42", "pending", "{}").unwrap(); + assert!(!stale.updated); + assert_eq!( + get_reply_decision(&db, "db:7:42").unwrap().unwrap().status, + "sent" + ); +} +#[test] +fn delivery_unknown_and_reconcile_required_are_terminal() { + let dir = tempdir().unwrap(); + let db = dir.path().join("reply-terminal.sqlite3"); + + for (index, terminal) in ["delivery_unknown", "reconcile_required"] + .into_iter() + .enumerate() + { + let event_id = format!("terminal-{index}"); + assert!( + project_reply_decision(&db, &event_id, "scheduled", "{}") + .unwrap() + .updated + ); + let terminal_update = + project_reply_decision(&db, &event_id, terminal, r#"{"unknown":true}"#).unwrap(); + assert!(terminal_update.applied); + assert!(terminal_update.updated); + + let idempotent = + project_reply_decision(&db, &event_id, terminal, r#"{"unknown":true}"#).unwrap(); + assert!(idempotent.applied); + assert!(!idempotent.updated); + + for next_status in [ + "sent", + "skipped", + "poison", + "delivery_unknown", + "reconcile_required", + ] { + if next_status == terminal { + continue; + } + let blocked = project_reply_decision(&db, &event_id, next_status, "{}").unwrap(); + assert!(!blocked.applied); + assert!(!blocked.updated); + assert_eq!( + blocked.reason.as_deref(), + Some("terminal_or_newer_status_cannot_regress") + ); + assert!(!update_reply_decision(&db, &event_id, next_status, None, None).unwrap()); + } + } +} + +#[test] +fn retrieval_families_require_current_migration_marker() { + let dir = tempdir().unwrap(); + let db = dir.path().join("retrieval-marker.sqlite3"); + let path = fixture(dir.path(), "chat.csv", "marker query"); + index_csv(&db, "chat-a", &path).unwrap(); + + let conn = open_db(&db).unwrap(); + conn.execute( + "UPDATE context_retrieval_meta SET value = 'stale' WHERE key = 'fts_schema'", + [], + ) + .unwrap(); + drop(conn); + + let assert_migration_required = |result: Result<()>| { + assert_eq!( + result.unwrap_err().to_string(), + CONTEXT_RETRIEVAL_MIGRATION_REQUIRED + ); + }; + assert_migration_required( + search(&db, Some("chat-a"), None, "marker", "keyword", 5).map(|_| ()), + ); + assert_migration_required(style_search(&db, Some("chat-a"), "marker", 5).map(|_| ())); + assert_migration_required(style_profile(&db, "chat-a", STYLE_USER, None).map(|_| ())); + assert_migration_required(response_time_stats(&db, "chat-a", STYLE_USER, None).map(|_| ())); + assert_migration_required(reply_decision_search(&db, "chat-a", "marker", 5).map(|_| ())); + assert_migration_required(context_reply_bundle(&db, "chat-a", "marker", None).map(|_| ())); +} + +#[test] +fn builds_rebuilds_and_serializes_style_profile_from_eligible_rows() { + let dir = tempdir().unwrap(); + let db = dir.path().join("profile.sqlite3"); + let path = dir.path().join("chat.csv"); + fs::write( + &path, + "Date,User,Message\n\ + 2026-01-01,최연우,ㅋㅋㅋ 오늘 좋다\n\ + 2026-01-01,최연우,뭐해요? 😊\n\ + 2026-01-01,최연우,1) 복사한 정보\n\ + 2026-01-01,최연우,https://example.com 안내\n\ + 2026-01-01,최연우,공지드립니다 합니다\n\ + 2026-01-01,민수,일반 대화\n", + ) + .unwrap(); + + let source = path.canonicalize().unwrap().display().to_string(); + assert_eq!(index_csv(&db, "프로필방", &path).unwrap(), 6); + + let profile = style_profile(&db, "프로필방", STYLE_USER, Some(&source)) + .unwrap() + .unwrap(); + assert_eq!(profile.sample_count, 2); + assert_eq!(profile.question_count, 1); + assert_eq!(profile.emoji_count, 1); + assert_eq!(profile.punctuation_count, 1); + assert_eq!(profile.casual_ending_count, 1); + assert!(profile.median_character_length > 0.0); + assert!(profile.p90_character_length >= profile.median_character_length); + assert_eq!(profile.policy_version, STYLE_POLICY_VERSION); + let endings: serde_json::Value = serde_json::from_str(&profile.common_endings_json).unwrap(); + assert_eq!(endings["요"], 1); + let serialized = style_profile_json(&db, "프로필방", STYLE_USER, Some(&source)) + .unwrap() + .unwrap(); + let serialized: serde_json::Value = serde_json::from_str(&serialized).unwrap(); + assert_eq!(serialized["sample_count"], 2); + + let conn = open_db(&db).unwrap(); + let rows = conn + .prepare( + "SELECT source_row, content_kind, style_eligible + FROM owner_style WHERE source = ?1 ORDER BY source_row", + ) + .unwrap() + .query_map([&source], |row| { + Ok(( + row.get::<_, i64>(0)?, + row.get::<_, String>(1)?, + row.get::<_, i64>(2)?, + )) + }) + .unwrap() + .collect::>>() + .unwrap(); + assert_eq!(rows.len(), 5); + assert_eq!(rows[0], (1, "ordinary_conversation".into(), 1)); + assert_eq!(rows[2], (3, "list_or_numbered".into(), 0)); + assert_eq!(rows[3], (4, "url".into(), 0)); + assert_eq!(rows[4], (5, "long_or_formal".into(), 0)); + + fs::write(&path, "Date,User,Message\n2026-01-01,최연우,새 말투네\n").unwrap(); + index_csv(&db, "프로필방", &path).unwrap(); + let rebuilt = style_profile(&db, "프로필방", STYLE_USER, Some(&source)) + .unwrap() + .unwrap(); + assert_eq!(rebuilt.sample_count, 1); + assert!(rebuilt.common_tokens_json.contains("새")); + assert!(!rebuilt.common_tokens_json.contains("오늘")); + + fs::write( + &path, + "Date,User,Message\n2026-01-01,최연우,https://example.com\n", + ) + .unwrap(); + index_csv(&db, "프로필방", &path).unwrap(); + assert!(style_profile(&db, "프로필방", STYLE_USER, Some(&source)) + .unwrap() + .is_none()); + assert_eq!( + open_db(&db) + .unwrap() + .query_row( + "SELECT COUNT(*) FROM owner_style_profile + WHERE chat = ?1 AND source = ?2", + params!["프로필방", source], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); +} +#[test] +fn context_reply_bundle_is_versioned_and_shape_stable() { + let dir = tempdir().unwrap(); + let db = dir.path().join("bundle.sqlite3"); + let path = dir.path().join("chat.csv"); + fs::write( + &path, + "Date,User,Message\n\ + 2026-01-01 00:00:00,민수,세금 일정 알려줘\n\ + 2026-01-01 00:00:10,최연우,ㅋㅋㅋ 일정 확인해요\n", + ) + .unwrap(); + index_csv(&db, "부자멘토멘티", &path).unwrap(); + + let bundle = context_reply_bundle(&db, "부자멘토멘티", "세금 일정", None).unwrap(); + assert_eq!(bundle.schema_version, CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION); + assert!(bundle.context.len() <= CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT); + assert!(bundle.styles.len() <= CONTEXT_REPLY_BUNDLE_STYLE_LIMIT); + assert!(bundle.prior_decisions.len() <= CONTEXT_REPLY_BUNDLE_DECISION_LIMIT); + assert_eq!( + bundle.style_profile.as_ref().unwrap().policy_version, + STYLE_POLICY_VERSION + ); + let value: serde_json::Value = serde_json::from_str( + &context_reply_bundle_json(&db, "부자멘토멘티", "세금 일정", None).unwrap(), + ) + .unwrap(); + let serialized = serde_json::to_string(&value).unwrap(); + let home = dirs::home_dir().unwrap().display().to_string(); + assert!(!serialized.contains(&home)); + assert!(!serialized.contains(&path.display().to_string())); + assert_eq!( + value + .as_object() + .unwrap() + .keys() + .cloned() + .collect::>(), + vec![ + "schema_version", + "context", + "styles", + "prior_decisions", + "style_profile", + "response_time", + ] + ); +} + +#[test] +fn punctuation_only_query_keeps_style_and_timing() { + let dir = tempdir().unwrap(); + let db = dir.path().join("tokenless.sqlite3"); + let path = dir.path().join("chat.csv"); + fs::write( + &path, + "Date,User,Message\n\ + 2026-01-01 00:00:00,민수,세금 일정 알려줘\n\ + 2026-01-01 00:00:10,최연우,ㅋㅋㅋ 일정 확인해요\n", + ) + .unwrap(); + index_csv(&db, "부자멘토멘티", &path).unwrap(); + let bundle = context_reply_bundle(&db, "부자멘토멘티", "???", None).unwrap(); + assert!(bundle.context.is_empty()); + assert!(!bundle.styles.is_empty()); + assert!(bundle.styles.iter().all(|row| row.mode == "recency_style")); + assert!(bundle.style_profile.is_some()); + assert!(bundle.response_time.is_some()); +} + +#[test] +fn recipient_bundle_excludes_every_burst_row_and_decision() { + let dir = tempdir().unwrap(); + let db = dir.path().join("burst-exclusion.sqlite3"); + let chat_id = 77; + let chat = "부자멘토멘티"; + let events = (101..=107) + .map(|log_id| { + let disposition = if log_id == 107 { + "retained" + } else { + "excluded" + }; + live_event( + chat_id, + log_id, + "민수", + &format!("burstmarker {disposition} context {log_id}"), + 1_000 + log_id, + ) + }) + .collect::>(); + let ingested = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &events, + true, + false, + ) + .unwrap(); + + for log_id in 101..=107 { + let disposition = if log_id == 107 { + "retained" + } else { + "excluded" + }; + let record = serde_json::json!({ + "event_id": format!("db:{chat_id}:{log_id}"), + "chat": chat, + "author": "민수", + "received_at": "2026-01-01T00:00:00Z", + "message": format!("burstmarker {disposition} decision {log_id}"), + "decision": "skip", + "reason": "duplicate", + "category": "duplicate", + "context_match_count": 0, + "style_match_count": 0, + "best_context_score": 0.0, + "best_style_score": 0.0, + "prior_similarity": 0.0, + "scheduled_delay_seconds": 0.0, + "status": "skipped", + "reply": null, + "evidence_ids": [], + "style_policy_version": "", + }); + assert!(record_reply_decision(&db, &record.to_string()).unwrap()); + } + + let unfiltered = context_reply_bundle_for_recipient_excluding_live_events( + &db, + chat, + "burstmarker", + Some(&ingested.source), + "민수", + chat_id, + &[999], + ) + .unwrap(); + assert!(unfiltered + .context + .iter() + .any(|row| row.message.contains("excluded context 101"))); + assert!(unfiltered + .prior_decisions + .iter() + .any(|row| row.event_id == format!("db:{chat_id}:101"))); + + let bundle = context_reply_bundle_for_recipient_excluding_live_events( + &db, + chat, + "burstmarker", + Some(&ingested.source), + "민수", + chat_id, + &[101, 102, 103, 104, 105, 106], + ) + .unwrap(); + assert!(!bundle + .context + .iter() + .any(|row| row.message.contains("excluded context"))); + assert!(bundle + .context + .iter() + .any(|row| row.message.contains("retained context 107"))); + assert!(!bundle + .prior_decisions + .iter() + .any(|row| (101..=106).any(|log_id| row.event_id == format!("db:{chat_id}:{log_id}")))); + assert!(bundle + .prior_decisions + .iter() + .any(|row| row.event_id == format!("db:{chat_id}:107"))); +} + +#[test] +fn burst_exclusion_ids_are_positive_unique_and_bounded() { + assert!(live_event_exclusions(7, &[1]).is_ok()); + assert!(live_event_exclusions(0, &[1]).is_err()); + assert!(live_event_exclusions(7, &[]).is_err()); + assert!(live_event_exclusions(7, &[0]).is_err()); + assert!(live_event_exclusions(7, &[-1]).is_err()); + assert!(live_event_exclusions(7, &[1, 1]).is_err()); + assert!(live_event_exclusions(7, &[1, 2, 3, 4, 5, 6]).is_ok()); + assert!(live_event_exclusions(7, &[1, 2, 3, 4, 5, 6, 7]).is_err()); + assert!(live_event_exclusions(7, &[i64::MAX]).is_ok()); +} + +#[test] +fn retrieval_candidate_pools_are_bounded_and_deterministic() { + let dir = tempdir().unwrap(); + let db = dir.path().join("bounded.sqlite3"); + let conn = open_db(&db).unwrap(); + for index in 0..(CONTEXT_VECTOR_CANDIDATE_CAP + 32) { + insert_context_row( + &conn, + "source-a", + "chat-a", + "2026-01-01", + &format!("bounded candidate {index}"), + ); + } + for index in 0..(STYLE_VECTOR_CANDIDATE_CAP + 32) { + conn.execute( + "INSERT INTO owner_style( + source, chat, date, user_name, message, vector, source_row, + content_kind, style_eligible, policy_version, features_json + ) VALUES (?1, 'chat-a', '2026-01-01', ?2, ?3, ?4, ?5, + 'ordinary_conversation', 1, ?6, '{}')", + params![ + "source-a", + STYLE_USER, + format!("style candidate {index}"), + vector_to_bytes(&encode_vector("style candidate")), + index as i64, + STYLE_POLICY_VERSION, + ], + ) + .unwrap(); + } + drop(conn); + + let first = search( + &db, + Some("chat-a"), + Some("source-a"), + "bounded", + "vector", + CONTEXT_VECTOR_CANDIDATE_CAP * 2, + ) + .unwrap(); + let second = search( + &db, + Some("chat-a"), + Some("source-a"), + "bounded", + "vector", + CONTEXT_VECTOR_CANDIDATE_CAP * 2, + ) + .unwrap(); + assert_eq!(first.len(), CONTEXT_VECTOR_CANDIDATE_CAP); + assert_eq!( + serde_json::to_string(&first).unwrap(), + serde_json::to_string(&second).unwrap() + ); + + let conn = open_db(&db).unwrap(); + let styles = style_search_with_connection( + &conn, + Some("chat-a"), + Some("source-a"), + "style", + STYLE_VECTOR_CANDIDATE_CAP * 2, + true, + ) + .unwrap(); + assert_eq!(styles.len(), STYLE_VECTOR_CANDIDATE_CAP); +} + +#[test] +fn fts_rebuild_recovers_deleted_index_rows() { + let dir = tempdir().unwrap(); + let db = dir.path().join("fts-rebuild.sqlite3"); + let path = fixture(dir.path(), "chat.csv", "rebuild marker"); + index_csv(&db, "chat-a", &path).unwrap(); + + let conn = open_db(&db).unwrap(); + conn.execute("DELETE FROM context_messages_fts", []) + .unwrap(); + conn.execute("DELETE FROM context_retrieval_meta", []) + .unwrap(); + drop(conn); + + let error = search(&db, Some("chat-a"), None, "rebuild", "keyword", 5).unwrap_err(); + assert_eq!(error.to_string(), CONTEXT_RETRIEVAL_MIGRATION_REQUIRED); + rebuild_retrieval_index(&open_db(&db).unwrap()).unwrap(); + let results = search(&db, Some("chat-a"), None, "rebuild", "keyword", 5).unwrap(); + assert_eq!(results.len(), 1); + assert_eq!(results[0].message, "rebuild marker"); +} + +#[test] +fn style_policy_and_source_filters_are_applied_before_scoring() { + let dir = tempdir().unwrap(); + let db = dir.path().join("style-policy.sqlite3"); + let first = dir.path().join("first.csv"); + let second = dir.path().join("second.csv"); + fs::write( + &first, + "Date,User,Message\n2026-01-01,최연우,current style\n", + ) + .unwrap(); + fs::write( + &second, + "Date,User,Message\n2026-01-01,최연우,other source style\n", + ) + .unwrap(); + index_csv(&db, "chat-a", &first).unwrap(); + index_csv(&db, "chat-a", &second).unwrap(); + let source = first.canonicalize().unwrap().display().to_string(); + + let conn = open_db(&db).unwrap(); + conn.execute( + "INSERT INTO owner_style( + source, chat, date, user_name, message, vector, source_row, + content_kind, style_eligible, policy_version, features_json + ) VALUES (?1, 'chat-a', '2027-01-01', ?2, 'legacy style', + ?3, 999, 'ordinary_conversation', 1, 'legacy-v1', '{}')", + params![ + source, + STYLE_USER, + vector_to_bytes(&encode_vector("legacy style")), + ], + ) + .unwrap(); + + let results = + style_search_with_connection(&conn, Some("chat-a"), Some(&source), "legacy", 10, true) + .unwrap(); + assert_eq!(results.len(), 1); + assert_eq!(results[0].message, "current style"); + assert_eq!(results[0].source, source); +} + +#[test] +fn retrieval_without_bounded_candidates_fails_closed() { + let dir = tempdir().unwrap(); + let db = dir.path().join("empty.sqlite3"); + let path = fixture(dir.path(), "chat.csv", "known phrase"); + index_csv(&db, "chat-a", &path).unwrap(); + assert!( + search(&db, Some("chat-a"), None, "completely missing", "vector", 5) + .unwrap() + .is_empty() + ); + assert!( + search(&db, Some("missing-chat"), None, "query", "vector", 5) + .unwrap() + .is_empty() + ); + assert!(style_search(&db, Some("missing-chat"), "query", 5) + .unwrap() + .is_empty()); + assert!(context_reply_bundle(&db, "missing-chat", "query", None) + .unwrap() + .context + .is_empty()); +} + +#[test] +fn live_schema_bootstrap_is_additive_and_idempotent() { + let dir = tempdir().unwrap(); + let db = dir.path().join("live-schema.sqlite3"); + + ensure_live_context_schema(&db).unwrap(); + ensure_live_context_schema(&db).unwrap(); + assert!(live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, 77) + .unwrap() + .is_none()); + + let conn = open_db_readonly(&db).unwrap(); + assert_eq!( + conn.query_row( + "SELECT value FROM context_retrieval_meta WHERE key='live_context_schema'", + [], + |row| row.get::<_, String>(0), + ) + .unwrap(), + LIVE_CONTEXT_SCHEMA_VERSION + ); + for table in [ + "context_sources", + "context_live_events", + "response_time_samples", + "owner_recipient_style_samples", + "owner_recipient_style_profile", + "context_message_topics", + "context_topic_stats", + "context_reference_packs", + ] { + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?1", + [table], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 1 + ); + } + assert_eq!( + conn.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { + row.get::<_, i64>(0) + }) + .unwrap(), + 0 + ); + drop(conn); + + let csv = fixture(dir.path(), "legacy.csv", "legacy fts marker"); + assert_eq!(index_csv(&db, "legacy-chat", &csv).unwrap(), 1); + assert_eq!( + search(&db, Some("legacy-chat"), None, "legacy", "keyword", 5,) + .unwrap() + .len(), + 1 + ); +} + +#[test] +fn auto_reply_startup_allows_dirty_partial_authoritative_identity() { + let dirty_ok = LiveContextSyncState { + source: "local-db:test:42".to_string(), + chat_id: 42, + chat: "부자멘토멘티".to_string(), + checkpoint_log_id: 7, + authoritative: true, + summary_dirty: true, + sync_status: "partial".to_string(), + }; + assert!(dirty_ok.allows_auto_reply_startup(42, "부자멘토멘티")); + + let mut not_auth = dirty_ok.clone(); + not_auth.authoritative = false; + assert!(!not_auth.allows_auto_reply_startup(42, "부자멘토멘티")); + + let mut bad_sync = dirty_ok.clone(); + bad_sync.sync_status = "stale".to_string(); + assert!(!bad_sync.allows_auto_reply_startup(42, "부자멘토멘티")); + + assert!(!dirty_ok.allows_auto_reply_startup(43, "부자멘토멘티")); + assert!(!dirty_ok.allows_auto_reply_startup(42, "다른방")); + + let mut clean_ready = dirty_ok.clone(); + clean_ready.summary_dirty = false; + clean_ready.sync_status = "ready".to_string(); + assert!(clean_ready.allows_auto_reply_startup(42, "부자멘토멘티")); +} + +#[test] +fn live_ingest_is_idempotent_and_rolls_back_a_late_conflict() { + let dir = tempdir().unwrap(); + let db = dir.path().join("live-idempotent.sqlite3"); + let chat_id = 77; + let chat = "부자멘토멘티"; + let page = vec![ + live_event(chat_id, 1, "민수", "첫 질문", 100), + live_event(chat_id, 2, STYLE_USER, "좋아요", 110), + ]; + + let first = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &page, + true, + false, + ) + .unwrap(); + assert_eq!(first.inserted_events, 2); + assert_eq!(first.duplicate_events, 0); + assert_eq!(first.checkpoint_log_id, 2); + assert!(first.summary_refreshed); + + let replay = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &page, + true, + false, + ) + .unwrap(); + assert_eq!(replay.inserted_events, 0); + assert_eq!(replay.duplicate_events, 2); + assert_eq!(replay.checkpoint_log_id, 2); + + let mut changed = page.clone(); + changed[0].message = "바뀐 질문".to_string(); + assert!(ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &changed, + true, + false, + ) + .unwrap_err() + .to_string() + .contains("digest mismatch")); + + let source = live_context_source_id(TEST_ACCOUNT_FINGERPRINT, chat_id).unwrap(); + let future = live_event(chat_id, 4, "민수", "원본", 130); + let conn = open_db(&db).unwrap(); + conn.execute( + "INSERT INTO context_live_events( + source, chat_id, log_id, sent_at, sender_name, message_digest, + disposition, auto_generated, context_message_id, style_message_id, created_at + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, 'context', 0, NULL, NULL, ?7)", + params![ + source, + chat_id, + future.log_id, + future.sent_at, + future.sender_name, + live_event_digest(&future).unwrap(), + Utc::now().to_rfc3339(), + ], + ) + .unwrap(); + drop(conn); + + let conflict_page = vec![ + live_event(chat_id, 3, "민수", "롤백 표식", 120), + live_event(chat_id, 4, "민수", "변경된 원본", 130), + ]; + assert!(ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 2, + &conflict_page, + false, + false, + ) + .unwrap_err() + .to_string() + .contains("digest mismatch")); + assert_eq!( + live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) + .unwrap() + .unwrap() + .checkpoint_log_id, + 2 + ); + let conn = open_db_readonly(&db).unwrap(); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM context_live_events WHERE source=?1 AND log_id=3", + [&source], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM context_messages WHERE source=?1 AND message='롤백 표식'", + [&source], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); +} + +#[test] +fn live_ingest_prefix_checkpoint_cannot_skip_a_deferred_self_row() { + let dir = tempdir().unwrap(); + let db = dir.path().join("live-deferred-prefix.sqlite3"); + let chat_id = 78; + let chat = "부자멘토멘티"; + + let prefix = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &[live_event(chat_id, 10, "민수", "직전 질문", 1_000)], + false, + false, + ) + .unwrap(); + assert_eq!(prefix.checkpoint_log_id, 10); + + let skipped = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 20, + &[live_event(chat_id, 30, "민수", "다음 질문", 1_020)], + true, + false, + ) + .unwrap_err(); + assert!(skipped + .to_string() + .contains("checkpoint is behind the caller checkpoint")); + assert_eq!( + live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) + .unwrap() + .unwrap() + .checkpoint_log_id, + 10 + ); + + let mut deferred_self = live_event(chat_id, 20, STYLE_USER, "확정된 직접 답변", 1_010); + deferred_self.is_self = true; + let resumed = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 10, + &[deferred_self], + false, + false, + ) + .unwrap(); + assert_eq!(resumed.checkpoint_log_id, 20); +} + +#[test] +fn live_ingest_carries_bursts_across_pages_and_excludes_automatic_self_rows() { + let dir = tempdir().unwrap(); + let db = dir.path().join("live-pages.sqlite3"); + let chat_id = 88; + let chat = "부자멘토멘티"; + + let partial = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &[live_event(chat_id, 10, "민수", "페이지 경계 질문", 1_000)], + false, + false, + ) + .unwrap(); + assert_eq!(partial.checkpoint_log_id, 10); + let partial_state = live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) + .unwrap() + .unwrap(); + assert_eq!(partial_state.sync_status, "partial"); + + let complete = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 10, + &[live_event(chat_id, 20, STYLE_USER, "경계도 좋아요", 1_012)], + true, + false, + ) + .unwrap(); + assert_eq!(complete.response_samples, 1); + assert_eq!(complete.recipient_style_samples, 1); + assert!(complete.summary_refreshed); + let stats = response_time_stats(&db, chat, STYLE_USER, Some(&complete.source)) + .unwrap() + .unwrap(); + assert_eq!(stats.sample_count, 1); + assert_eq!(stats.average_seconds, 12.0); + let recipient = recipient_style_profile(&db, chat, "민수", Some(&complete.source)) + .unwrap() + .unwrap(); + assert_eq!(recipient.direct_sample_count, 1); + assert!(recipient.used_fallback); + + let mut automatic = live_event(chat_id, 40, STYLE_USER, "자동출력표식", 2_010); + automatic.auto_generated = true; + let excluded = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 20, + &[ + live_event(chat_id, 30, "민수", "자동 직전 질문", 2_000), + automatic, + ], + true, + false, + ) + .unwrap(); + assert_eq!(excluded.indexed_messages, 1); + assert_eq!(excluded.style_messages, 0); + assert_eq!(excluded.response_samples, 0); + assert_eq!(excluded.recipient_style_samples, 0); + assert!(search( + &db, + Some(chat), + Some(&excluded.source), + "자동출력표식", + "keyword", + 5, + ) + .unwrap() + .is_empty()); + assert_eq!( + response_time_stats(&db, chat, STYLE_USER, Some(&excluded.source)) + .unwrap() + .unwrap() + .sample_count, + 1 + ); + let conn = open_db_readonly(&db).unwrap(); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM context_live_events + WHERE source=?1 AND log_id=40 AND auto_generated=1 + AND context_message_id IS NULL AND style_message_id IS NULL", + [&excluded.source], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 1 + ); +} + +#[test] +fn live_ingest_does_not_treat_a_nickname_collision_as_self() { + let dir = tempdir().unwrap(); + let db = dir.path().join("live-nickname-collision.sqlite3"); + let chat_id = 89; + let mut collision = live_event(chat_id, 1, STYLE_USER, "동명이인 메시지", 1_000); + collision.is_self = false; + let result = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + "부자멘토멘티", + 0, + &[collision], + true, + false, + ) + .unwrap(); + assert_eq!(result.indexed_messages, 1); + assert_eq!(result.style_messages, 0); + let conn = open_db_readonly(&db).unwrap(); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM owner_style WHERE source=?1", + [&result.source], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); + + let mut unresolved = live_event(chat_id, 2, STYLE_USER, "확정 전 출력", 1_010); + unresolved.is_self = true; + unresolved.exclude_from_learning = true; + let excluded = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + "부자멘토멘티", + 1, + &[unresolved], + true, + false, + ) + .unwrap(); + assert_eq!(excluded.indexed_messages, 0); + assert_eq!(excluded.style_messages, 0); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM context_live_events + WHERE source=?1 AND log_id=2 AND disposition='ambiguous_auto_candidate' + AND auto_generated=0 AND context_message_id IS NULL + AND style_message_id IS NULL", + [&result.source], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 1 + ); +} + +#[test] +fn promotion_requires_complete_summaries_and_is_transactional() { + let dir = tempdir().unwrap(); + let db = dir.path().join("live-promotion-rollback.sqlite3"); + let chat_id = 99; + let page = vec![ + live_event(chat_id, 1, "민수", "질문", 100), + live_event(chat_id, 2, STYLE_USER, "좋아요", 110), + ]; + assert!(ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + "방", + 0, + &page, + true, + true, + ) + .unwrap_err() + .to_string() + .contains("lacks required")); + assert!( + live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) + .unwrap() + .is_none() + ); + let conn = open_db_readonly(&db).unwrap(); + assert_eq!( + conn.query_row("SELECT COUNT(*) FROM context_messages", [], |row| { + row.get::<_, i64>(0) + }) + .unwrap(), + 0 + ); +} + +#[test] +fn authoritative_live_source_and_recipient_bundle_use_the_right_register() { + let dir = tempdir().unwrap(); + let db = dir.path().join("recipient-bundle.sqlite3"); + let legacy = dir.path().join("legacy.csv"); + fs::write( + &legacy, + "Date,User,Message\n2026-01-01,민수,legacytoken\n2026-01-01,최연우,예전 말투예요\n", + ) + .unwrap(); + index_csv(&db, "부자멘토멘티", &legacy).unwrap(); + + let chat_id = 123; + let events = vec![ + live_event(chat_id, 1, "민수", "시장 질문 알파", 100), + live_event(chat_id, 2, STYLE_USER, "알파는 좋아요", 110), + live_event(chat_id, 3, "민수", "시장 질문 베타", 200), + live_event(chat_id, 4, STYLE_USER, "베타도 봐요", 210), + live_event(chat_id, 5, "민수", "시장 질문 감마", 300), + live_event(chat_id, 6, STYLE_USER, "감마도 좋아요", 310), + live_event(chat_id, 7, "영희", "시장 질문 델타", 400), + live_event(chat_id, 8, STYLE_USER, "델타는 천천히요", 410), + live_event(chat_id, 9, "민수", "현재질문표식", 500), + ]; + let ingested = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + "부자멘토멘티", + 0, + &events, + true, + true, + ) + .unwrap(); + assert!(ingested.authoritative); + assert!( + search(&db, Some("부자멘토멘티"), None, "legacytoken", "keyword", 5,) + .unwrap() + .is_empty() + ); + let live = search( + &db, + Some("부자멘토멘티"), + None, + "현재질문표식", + "keyword", + 5, + ) + .unwrap(); + assert_eq!(live.len(), 1); + assert_eq!(live[0].source, ingested.source); + assert_eq!( + style_profile(&db, "부자멘토멘티", STYLE_USER, None) + .unwrap() + .unwrap() + .source, + ingested.source + ); + assert_eq!( + response_time_stats(&db, "부자멘토멘티", STYLE_USER, None) + .unwrap() + .unwrap() + .sample_count, + 4 + ); + + let direct = recipient_style_profile(&db, "부자멘토멘티", "민수", None) + .unwrap() + .unwrap(); + assert_eq!(direct.direct_sample_count, 3); + assert_eq!(direct.confidence_sum, 3.0); + assert!(!direct.used_fallback); + let fallback = recipient_style_profile(&db, "부자멘토멘티", "영희", None) + .unwrap() + .unwrap(); + assert_eq!(fallback.direct_sample_count, 1); + assert!(fallback.used_fallback); + assert_eq!(fallback.profile.sample_count, 4); + + let conn = open_db_readonly(&db).unwrap(); + let mut plan_stmt = conn + .prepare(&format!("EXPLAIN QUERY PLAN {RECIPIENT_STYLE_SEARCH_SQL}")) + .unwrap(); + let plan = plan_stmt + .query_map( + params![ + &ingested.source, + "민수", + "부자멘토멘티", + STYLE_USER, + STYLE_POLICY_VERSION, + STYLE_VECTOR_CANDIDATE_CAP as i64, + ], + |row| row.get::<_, String>(3), + ) + .unwrap() + .collect::>>() + .unwrap() + .join("\n"); + assert!(plan.contains("idx_recipient_style_samples_lookup")); + assert!(!plan.contains("SCAN styles")); + assert!(!plan.contains("USE TEMP B-TREE")); + + let direct_bundle = context_reply_bundle_for_recipient_excluding_live_events( + &db, + "부자멘토멘티", + "현재질문표식", + None, + "민수", + chat_id, + &[9], + ) + .unwrap(); + assert_eq!( + direct_bundle.schema_version, + RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION + ); + assert!(direct_bundle.context.is_empty()); + assert!(direct_bundle + .recipient_style_profile + .as_ref() + .is_some_and(|profile| !profile.used_fallback)); + assert_eq!(direct_bundle.styles.len(), 3); + assert!(direct_bundle + .styles + .iter() + .all(|style| style.mode == "vector_style_recipient")); + assert!(!direct_bundle + .styles + .iter() + .any(|style| style.message.contains("델타"))); + assert!(direct_bundle + .styles + .iter() + .all(|style| style.source.starts_with("source:"))); + + let fallback_bundle = context_reply_bundle_for_recipient_excluding_live_events( + &db, + "부자멘토멘티", + "현재질문표식", + None, + "영희", + chat_id, + &[9], + ) + .unwrap(); + assert!(fallback_bundle + .recipient_style_profile + .as_ref() + .is_some_and(|profile| profile.used_fallback)); + assert!(fallback_bundle + .styles + .iter() + .any(|style| style.message.contains("델타"))); + assert!(fallback_bundle + .styles + .iter() + .all(|style| style.mode == "vector_style")); + + let json = context_reply_bundle_for_recipient_excluding_live_events_json( + &db, + "부자멘토멘티", + "현재질문표식", + None, + "민수", + chat_id, + &[9], + ) + .unwrap(); + let value: serde_json::Value = serde_json::from_str(&json).unwrap(); + assert_eq!( + value["schema_version"], + RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION + ); + assert_eq!(value["recipient"], "민수"); + assert!(value.get("recipient_style_profile").is_some()); + assert_eq!(value.as_object().unwrap().len(), 8); + let state = live_context_sync_state(&db, TEST_ACCOUNT_FINGERPRINT, chat_id) + .unwrap() + .unwrap(); + assert_eq!(state.checkpoint_log_id, 9); + assert!(state.authoritative); + assert_eq!(state.sync_status, "ready"); +} + +#[test] +fn automatic_self_event_matching_is_exact_bounded_and_fail_closed() { + let dir = tempdir().unwrap(); + let db = dir.path().join("self-classification.sqlite3"); + ensure_live_context_schema(&db).unwrap(); + let timestamp = |seconds| { + chrono::DateTime::::from_timestamp(seconds, 0) + .unwrap() + .to_rfc3339() + }; + record_sent_reply(&db, "unique", "방", "유일 답변", ×tamp(1_000)); + record_sent_reply(&db, "reused", "방", "재사용 답변", ×tamp(2_000)); + record_sent_reply(&db, "many-a", "방", "중복 답변", ×tamp(3_000)); + record_sent_reply(&db, "many-b", "방", "중복 답변", ×tamp(3_010)); + record_sent_reply(&db, "malformed", "방", "시간 오류", "not-a-time"); + + let outgoing = vec![ + OutgoingSelfEvent { + chat_id: 7, + log_id: 1, + message: "유일 답변".into(), + sent_at: 1_020, + }, + OutgoingSelfEvent { + chat_id: 7, + log_id: 2, + message: "재사용 답변".into(), + sent_at: 1_990, + }, + OutgoingSelfEvent { + chat_id: 7, + log_id: 3, + message: "재사용 답변".into(), + sent_at: 2_010, + }, + OutgoingSelfEvent { + chat_id: 7, + log_id: 4, + message: "중복 답변".into(), + sent_at: 3_005, + }, + OutgoingSelfEvent { + chat_id: 7, + log_id: 5, + message: "시간 오류".into(), + sent_at: 4_000, + }, + OutgoingSelfEvent { + chat_id: 7, + log_id: 6, + message: "사람이 직접 쓴 말".into(), + sent_at: 5_000, + }, + ]; + let classified = classify_auto_generated_self_events(&db, "방", &outgoing, &[]).unwrap(); + assert_eq!(classified.len(), outgoing.len()); + assert!(classified[0].auto_generated); + assert_eq!(classified[0].matched_event_id.as_deref(), Some("unique")); + assert_eq!(classified[0].reason, "unique_exact_sent_match"); + assert!(!classified[1].auto_generated); + assert!(!classified[2].auto_generated); + assert_eq!(classified[1].reason, "sent_match_reused_in_batch"); + assert_eq!(classified[2].reason, "sent_match_reused_in_batch"); + assert!(!classified[3].auto_generated); + assert_eq!(classified[3].reason, "multiple_sent_matches"); + assert!(!classified[4].auto_generated); + assert_eq!(classified[4].reason, "unparseable_candidate_time"); + assert!(!classified[5].auto_generated); + assert_eq!(classified[5].reason, "no_exact_sent_match"); + + let outside_window = [OutgoingSelfEvent { + chat_id: 7, + log_id: 7, + message: "유일 답변".into(), + sent_at: 1_121, + }]; + assert!( + !classify_auto_generated_self_events(&db, "방", &outside_window, &[]).unwrap()[0] + .auto_generated + ); +} + +#[test] +fn ambiguous_automatic_candidates_never_feed_context_style_or_timing() { + let dir = tempdir().unwrap(); + let db = dir.path().join("ambiguous-self-classification.sqlite3"); + let chat = "부자멘토멘티"; + let chat_id = 91; + let now = Utc::now().timestamp(); + ensure_live_context_schema(&db).unwrap(); + + let statuses = [ + "scheduled", + "sending", + "accepted_unconfirmed", + "delivery_unknown", + "reconcile_required", + ]; + let outgoing = statuses + .iter() + .enumerate() + .map(|(index, status)| OutgoingSelfEvent { + chat_id, + log_id: index as i64 + 2, + message: format!("자동 후보 {status}"), + sent_at: now, + }) + .collect::>(); + + let before_projection = classify_auto_generated_self_events(&db, chat, &outgoing, &[]).unwrap(); + assert!(before_projection.iter().all(|item| { + !item.auto_generated + && item.matched_event_id.is_none() + && item.reason == "no_exact_sent_match" + })); + + for (index, status) in statuses.iter().enumerate() { + let event_id = format!("ambiguous-{index}"); + let record = serde_json::json!({ + "event_id": event_id, + "chat": chat, + "author": "민수", + "received_at": chrono::DateTime::::from_timestamp(now, 0) + .unwrap() + .to_rfc3339(), + "message": format!("incoming {index}"), + "decision": "reply", + "reason": "direct_question", + "category": "question", + "context_match_count": 1, + "style_match_count": 1, + "best_context_score": 0.5, + "best_style_score": 0.5, + "prior_similarity": 0.0, + "scheduled_delay_seconds": 0.0, + "status": status, + "reply": outgoing[index].message, + "evidence_ids": ["context:test"], + "style_policy_version": STYLE_POLICY_VERSION, + }); + assert!(record_reply_decision(&db, &record.to_string()).unwrap()); + } + assert!(update_reply_decision(&db, "ambiguous-0", "delivery_unknown", None, None,).unwrap()); + + let classified = classify_auto_generated_self_events(&db, chat, &outgoing, &[]).unwrap(); + assert_eq!(classified.len(), statuses.len()); + for (index, item) in classified.iter().enumerate() { + assert!(!item.auto_generated); + assert_eq!(item.reason, "ambiguous_auto_candidate"); + assert_eq!( + item.matched_event_id.as_deref(), + Some(format!("ambiguous-{index}").as_str()) + ); + } + let mut outside_window = outgoing[1].clone(); + outside_window.log_id = 100; + outside_window.sent_at = now + AUTO_GENERATED_MATCH_WINDOW_SECONDS + 10; + let outside = classify_auto_generated_self_events(&db, chat, &[outside_window], &[]).unwrap(); + assert!(!outside[0].auto_generated); + assert_eq!(outside[0].reason, "no_exact_sent_match"); + + let mut live_events = vec![live_event(chat_id, 1, "민수", "직전 질문", now - 10)]; + live_events.extend(outgoing.iter().zip(&classified).map(|(event, item)| { + let mut live = live_event( + event.chat_id, + event.log_id, + STYLE_USER, + &event.message, + event.sent_at, + ); + live.exclude_from_learning = !item.auto_generated && item.reason != "no_exact_sent_match"; + live.auto_generated = item.auto_generated; + live + })); + let result = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &live_events, + true, + false, + ) + .unwrap(); + assert_eq!(result.indexed_messages, 1); + assert_eq!(result.style_messages, 0); + assert_eq!(result.response_samples, 0); + assert_eq!(result.recipient_style_samples, 0); + + let conn = open_db_readonly(&db).unwrap(); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM context_live_events + WHERE source=?1 AND disposition='ambiguous_auto_candidate' + AND auto_generated=0 AND context_message_id IS NULL + AND style_message_id IS NULL", + [&result.source], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + statuses.len() as i64 + ); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM response_time_samples WHERE source=?1", + [&result.source], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); +} +#[test] +fn queue_confirmed_send_excludes_a_loopback_self_row() { + let dir = tempdir().unwrap(); + let db = dir.path().join("queue-send.sqlite3"); + ensure_live_context_schema(&db).unwrap(); + let chat_id = 7; + let chat = "방"; + let text = "제목은 폭탄인데 내용은 지원이네요"; + assert!(classify_style_message(text).style_eligible); + let sent = ConfirmedSelfSend { + event_id: "queue:417780809780519:1".into(), + text: text.into(), + sent_at: 1_000, + }; + let outgoing = vec![OutgoingSelfEvent { + chat_id, + log_id: 11, + message: text.into(), + sent_at: 1_004, + }]; + let classified = + classify_auto_generated_self_events(&db, chat, &outgoing, std::slice::from_ref(&sent)) + .unwrap(); + assert!(classified[0].auto_generated); + assert_eq!(classified[0].reason, "unique_exact_sent_match"); + assert_eq!( + classified[0].matched_event_id.as_deref(), + Some(sent.event_id.as_str()) + ); + + let mut events = vec![live_event( + chat_id, + 10, + "민수", + "지원은 어디까지 해봤어", + 990, + )]; + let mut loopback = live_event(chat_id, 11, STYLE_USER, text, 1_004); + loopback.auto_generated = classified[0].auto_generated; + events.push(loopback); + let result = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &events, + true, + false, + ) + .unwrap(); + assert_eq!(result.style_messages, 0); + assert_eq!(result.response_samples, 0); + let conn = open_db_readonly(&db).unwrap(); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM owner_style WHERE chat=?1 AND message=?2", + params![chat, text], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM context_live_events WHERE disposition='auto_generated'", + [], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 1 + ); + drop(conn); + + // The same text without a delivered send stays an owner sample. + let unmatched = classify_auto_generated_self_events(&db, chat, &outgoing, &[]).unwrap(); + assert!(!unmatched[0].auto_generated); + assert_eq!(unmatched[0].reason, "no_exact_sent_match"); +} + +#[test] +fn queue_and_decision_sends_must_agree_on_one_identity() { + let dir = tempdir().unwrap(); + let db = dir.path().join("self-classification-conflict.sqlite3"); + ensure_live_context_schema(&db).unwrap(); + let timestamp = |seconds| { + chrono::DateTime::::from_timestamp(seconds, 0) + .unwrap() + .to_rfc3339() + }; + record_sent_reply(&db, "decided", "방", "같은 문장", ×tamp(1_000)); + let outgoing = vec![OutgoingSelfEvent { + chat_id: 7, + log_id: 21, + message: "같은 문장".into(), + sent_at: 1_001, + }]; + let queue = ConfirmedSelfSend { + event_id: "queue:9".into(), + text: "같은 문장".into(), + sent_at: 1_002, + }; + let classified = + classify_auto_generated_self_events(&db, "방", &outgoing, std::slice::from_ref(&queue)) + .unwrap(); + assert!(!classified[0].auto_generated); + assert_eq!(classified[0].reason, "multiple_sent_matches"); + assert!(classified[0].matched_event_id.is_none()); +} + +#[test] +fn queue_loader_reads_only_terminal_sent_jobs() { + let dir = tempdir().unwrap(); + let queue = dir.path().join("reply-queue.sqlite3"); + { + let conn = Connection::open(&queue).unwrap(); + conn.execute_batch( + "CREATE TABLE reply_jobs(event_id TEXT, status TEXT, reply TEXT, updated_at REAL);", + ) + .unwrap(); + conn.execute( + "INSERT INTO reply_jobs VALUES('a','sent','보낸 문장',1000.4)", + [], + ) + .unwrap(); + conn.execute( + "INSERT INTO reply_jobs VALUES('b','skipped','버려진 문장',1001.0)", + [], + ) + .unwrap(); + conn.execute("INSERT INTO reply_jobs VALUES('c','sent',' ',1002.0)", []) + .unwrap(); + conn.execute( + "INSERT INTO reply_jobs VALUES('d','sent','시간 없음',NULL)", + [], + ) + .unwrap(); + conn.execute( + "INSERT INTO reply_jobs VALUES('e','delivery_unknown','불확실',1003.0)", + [], + ) + .unwrap(); + } + let sends = confirmed_self_sends_from_queue(&queue).unwrap(); + assert_eq!(sends.len(), 1); + assert_eq!(sends[0].event_id, "a"); + assert_eq!(sends[0].text, "보낸 문장"); + assert_eq!(sends[0].sent_at, 1_000); + + let without_jobs = dir.path().join("other-queue.sqlite3"); + Connection::open(&without_jobs) + .unwrap() + .execute_batch("CREATE TABLE unrelated(x)") + .unwrap(); + assert!(confirmed_self_sends_from_queue(&without_jobs) + .unwrap() + .is_empty()); + assert!( + confirmed_self_sends_from_queue(&dir.path().join("absent.sqlite3")) + .unwrap() + .is_empty() + ); +} + +#[test] +fn repair_reclassifies_bot_sent_style_rows_and_rebuilds_aggregates() { + let dir = tempdir().unwrap(); + let db = dir.path().join("repair.sqlite3"); + ensure_live_context_schema(&db).unwrap(); + let chat_id = 7; + let chat = "방"; + let bot_text = "제목은 폭탄인데 내용은 지원이네요"; + let events = vec![ + live_event(chat_id, 1, "민수", "오늘 뭐 해", 1_000), + live_event(chat_id, 2, STYLE_USER, "그냥 집에 있었어", 1_030), + live_event(chat_id, 3, "민수", "지원은 어디까지 해봤어", 2_000), + live_event(chat_id, 4, STYLE_USER, bot_text, 2_050), + ]; + let ingested = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &events, + true, + false, + ) + .unwrap(); + assert_eq!(ingested.style_messages, 2); + assert_eq!(ingested.response_samples, 2); + let eligible_before = { + let conn = open_db_readonly(&db).unwrap(); + conn.query_row( + "SELECT COUNT(*) FROM owner_style WHERE style_eligible=1", + [], + |row| row.get::<_, i64>(0), + ) + .unwrap() + }; + assert_eq!(eligible_before, 2); + + let bot_style_id: i64 = { + let conn = open_db_readonly(&db).unwrap(); + conn.query_row( + "SELECT id FROM owner_style WHERE message=?1", + params![bot_text], + |row| row.get::<_, i64>(0), + ) + .unwrap() + }; + let sends = vec![ConfirmedSelfSend { + event_id: "queue:2".into(), + text: bot_text.into(), + sent_at: 2_051, + }]; + + let dry = repair_bot_sent_style_samples(&db, chat, chat_id, &sends, true).unwrap(); + assert_eq!(dry.matched_rows, 1); + assert_eq!(dry.already_ineligible, 0); + assert!(dry.dry_run); + { + let conn = open_db_readonly(&db).unwrap(); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM owner_style WHERE content_kind='bot_sent_reply'", + [], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); + assert_eq!( + conn.query_row("SELECT sample_count FROM owner_style_profile", [], |row| { + row.get::<_, i64>(0) + },) + .unwrap(), + eligible_before + ); + } + + let report = repair_bot_sent_style_samples(&db, chat, chat_id, &sends, false).unwrap(); + assert_eq!(report.matched_rows, 1); + assert_eq!(report.already_ineligible, 0); + assert_eq!(report.ambiguous_rows, 0); + assert_eq!(report.deleted_response_samples, 1); + assert_eq!(report.rebuilt_sources.len(), 1); + assert_eq!(report.matches[0].offset_seconds, -1); + { + let conn = open_db_readonly(&db).unwrap(); + let (kind, eligible) = conn + .query_row( + "SELECT content_kind, style_eligible FROM owner_style WHERE message=?1", + params![bot_text], + |row| Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?)), + ) + .unwrap(); + assert_eq!(kind, "bot_sent_reply"); + assert_eq!(eligible, 0); + assert_eq!( + conn.query_row("SELECT sample_count FROM owner_style_profile", [], |row| { + row.get::<_, i64>(0) + },) + .unwrap(), + eligible_before - 1 + ); + assert_eq!( + conn.query_row("SELECT COUNT(*) FROM response_time_samples", [], |row| row + .get::<_, i64>( + 0 + ),) + .unwrap(), + 1 + ); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM owner_recipient_style_samples WHERE style_message_id = ?1", + params![bot_style_id], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); + } + + // Idempotent: the second pass only sees an already ineligible row. + let again = repair_bot_sent_style_samples(&db, chat, chat_id, &sends, false).unwrap(); + assert_eq!(again.matched_rows, 1); + assert_eq!(again.already_ineligible, 1); + assert!(again.rebuilt_sources.is_empty()); +} +#[test] +fn csv_import_does_not_learn_a_confirmed_bot_reply() { + let dir = tempdir().unwrap(); + let db = dir.path().join("csv-bot-send.sqlite3"); + ensure_live_context_schema(&db).unwrap(); + let timestamp = |seconds: i64| { + chrono::DateTime::::from_timestamp(seconds, 0) + .unwrap() + .to_rfc3339() + }; + let sent_at = chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:00", "%Y-%m-%d %H:%M:%S") + .unwrap() + .and_utc() + .timestamp(); + record_sent_reply( + &db, + "bot-1", + "부자멘토멘티", + "제목은 폭탄인데 내용은 지원이네요", + ×tamp(sent_at), + ); + let path = dir.path().join("chat.csv"); + fs::write( + &path, + "Date,User,Message\n\ +2026-01-01 09:59:30,문승현,기사 봤어\n\ +2026-01-01 10:00:04,최연우,제목은 폭탄인데 내용은 지원이네요\n\ +2026-01-01 11:00:00,최연우,그건 좀 세긴 하네\n", + ) + .unwrap(); + assert_eq!(index_csv(&db, "부자멘토멘티", &path).unwrap(), 3); + + let conn = open_db_readonly(&db).unwrap(); + let (kind, eligible): (String, i64) = conn + .query_row( + "SELECT content_kind, style_eligible FROM owner_style WHERE message=?1", + params!["제목은 폭탄인데 내용은 지원이네요"], + |row| Ok((row.get(0)?, row.get(1)?)), + ) + .unwrap(); + assert_eq!(kind, "bot_sent_reply"); + assert_eq!(eligible, 0); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM owner_style WHERE style_eligible=1", + [], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 1 + ); + let sample_count: i64 = conn + .query_row( + "SELECT sample_count FROM owner_style_profile WHERE chat=?1", + params!["부자멘토멘티"], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(sample_count, 1); +} + +#[test] +fn csv_import_excludes_a_queue_only_confirmed_send() { + let dir = tempdir().unwrap(); + let db = dir.path().join("csv-queue-send.sqlite3"); + ensure_live_context_schema(&db).unwrap(); + let reply = "역시 세긴 하네요"; + let sent_at = chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:04", "%Y-%m-%d %H:%M:%S") + .unwrap() + .and_utc() + .timestamp(); + let queue = dir.path().join("reply-queue.sqlite3"); + { + let conn = Connection::open(&queue).unwrap(); + conn.execute_batch( + "CREATE TABLE reply_jobs(event_id TEXT, status TEXT, reply TEXT, updated_at REAL);", + ) + .unwrap(); + conn.execute( + "INSERT INTO reply_jobs VALUES('queue-only','sent',?1,?2)", + params![reply, sent_at as f64], + ) + .unwrap(); + } + let path = dir.path().join("chat.csv"); + fs::write( + &path, + "Date,User,Message\n\ +2026-01-01 10:00:00,문승현,그건 좀 세네\n\ +2026-01-01 10:00:04,최연우,역시 세긴 하네요\n", + ) + .unwrap(); + // No decision-ledger row exists for this reply: only the room queue + // proves the assistant wrote it. + assert_eq!( + index_csv_with_queue(&db, "부자멘토멘티", &path, Some(&queue)).unwrap(), + 2 + ); + let conn = open_db_readonly(&db).unwrap(); + let (kind, eligible): (String, i64) = conn + .query_row( + "SELECT content_kind, style_eligible FROM owner_style WHERE message=?1", + params![reply], + |row| Ok((row.get(0)?, row.get(1)?)), + ) + .unwrap(); + assert_eq!(kind, "bot_sent_reply"); + assert_eq!(eligible, 0); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM owner_style WHERE style_eligible=1", + [], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); +} + +#[test] +fn csv_import_does_not_learn_pacing_from_a_bot_reply() { + let dir = tempdir().unwrap(); + let db = dir.path().join("csv-bot-pacing.sqlite3"); + ensure_live_context_schema(&db).unwrap(); + let bot_reply = "역시 세긴 하네요"; + let bot_sent_at = + chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:04", "%Y-%m-%d %H:%M:%S") + .unwrap() + .and_utc() + .timestamp(); + record_sent_reply( + &db, + "bot-pacing", + "부자멘토멘티", + bot_reply, + &chrono::DateTime::::from_timestamp(bot_sent_at, 0) + .unwrap() + .to_rfc3339(), + ); + let path = dir.path().join("chat.csv"); + fs::write( + &path, + "Date,User,Message\n\ +2026-01-01 09:59:30,문승현,그건 좀 세네\n\ +2026-01-01 10:00:04,최연우,역시 세긴 하네요\n\ +2026-01-01 10:01:00,문승현,다음 얘기 하자\n\ +2026-01-01 10:01:30,최연우,그건 좀 웃기네\n", + ) + .unwrap(); + index_csv(&db, "부자멘토멘티", &path).unwrap(); + let conn = open_db_readonly(&db).unwrap(); + // Only the genuine human pair (10:01:00 -> 10:01:30) may be measured. + let sample_count: i64 = conn + .query_row( + "SELECT sample_count FROM response_time_stats WHERE chat=?1", + params!["부자멘토멘티"], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(sample_count, 1); +} + +#[test] +fn repair_rebuilds_a_source_whose_matched_rows_were_already_ineligible() { + let dir = tempdir().unwrap(); + let db = dir.path().join("repair-ineligible.sqlite3"); + ensure_live_context_schema(&db).unwrap(); + let chat_id = 7; + let chat = "방"; + let link = "https://example.test/notice"; + let events = vec![ + live_event(chat_id, 1, "민수", "이거 봐", 1_000), + live_event(chat_id, 2, STYLE_USER, link, 1_030), + ]; + let ingested = ingest_live_context_events( + &db, + TEST_ACCOUNT_FINGERPRINT, + chat_id, + chat, + 0, + &events, + true, + false, + ) + .unwrap(); + assert_eq!(ingested.style_messages, 1); + assert_eq!(ingested.response_samples, 1); + { + let conn = open_db_readonly(&db).unwrap(); + let eligible: i64 = conn + .query_row( + "SELECT style_eligible FROM owner_style WHERE message=?1", + params![link], + |row| row.get(0), + ) + .unwrap(); + // A URL row is style-ineligible but still produced a pacing sample. + assert_eq!(eligible, 0); + } + let sends = vec![ConfirmedSelfSend { + event_id: "queue:ineligible".into(), + text: link.into(), + sent_at: 1_031, + }]; + let report = repair_bot_sent_style_samples(&db, chat, chat_id, &sends, false).unwrap(); + assert_eq!(report.matched_rows, 1); + assert_eq!(report.already_ineligible, 1); + assert_eq!(report.deleted_response_samples, 1); + assert_eq!(report.rebuilt_sources.len(), 1); + let conn = open_db_readonly(&db).unwrap(); + assert_eq!( + conn.query_row( + "SELECT COUNT(*) FROM response_time_samples WHERE chat_id=?1", + params![chat_id], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 0 + ); +} + +#[test] +fn repair_recomputes_an_imported_source_pacing_aggregate() { + let dir = tempdir().unwrap(); + let db = dir.path().join("repair-imported.sqlite3"); + ensure_live_context_schema(&db).unwrap(); + let source = "/tmp/legacy-chat.csv"; + let chat = "부자멘토멘티"; + let bot_reply = "역시 세긴 하네요"; + let human_reply = "그건 좀 웃기네"; + let bot_sent_at = + chrono::NaiveDateTime::parse_from_str("2026-01-01 10:00:04", "%Y-%m-%d %H:%M:%S") + .unwrap() + .and_utc() + .timestamp(); + { + let conn = open_db(&db).unwrap(); + for (date, user, message) in [ + ("2026-01-01 09:59:30", "문승현", "그건 좀 세네"), + ("2026-01-01 10:00:04", STYLE_USER, bot_reply), + ("2026-01-01 10:01:00", "문승현", "다음 얘기 하자"), + ("2026-01-01 10:01:30", STYLE_USER, human_reply), + ] { + conn.execute( + "INSERT INTO context_messages(source, chat, date, user_name, message, vector) + VALUES (?1, ?2, ?3, ?4, ?5, ?6)", + params![ + source, + chat, + date, + user, + message, + vector_to_bytes(&encode_vector(message)) + ], + ) + .unwrap(); + } + // Both owner rows were eligible when the legacy importer ran, so the + // style profile starts at two samples even though one is the bot. + for (index, (date, message)) in [ + ("2026-01-01 10:00:04", bot_reply), + ("2026-01-01 10:01:30", human_reply), + ] + .iter() + .enumerate() + { + conn.execute( + "INSERT INTO owner_style(source, chat, date, user_name, message, vector, + source_row, content_kind, style_eligible, policy_version, features_json) + VALUES (?1, ?2, ?3, '최연우', ?4, ?5, ?6, 'ordinary_conversation', 1, ?7, '{}')", + params![ + source, + chat, + date, + message, + vector_to_bytes(&encode_vector(message)), + index as i64 + 1, + STYLE_POLICY_VERSION + ], + ) + .unwrap(); + } + conn.execute( + "INSERT INTO owner_style_profile( + chat, source, user_name, sample_count, average_character_length, + median_character_length, p90_character_length, casual_ending_count, + casual_ending_counts_json, question_count, emoji_count, punctuation_count, + common_endings_json, common_tokens_json, policy_version, created_at + ) VALUES (?1, ?2, '최연우', 2, 8.0, 8.0, 8.0, 0, '{}', 0, 0, 0, '{}', '{}', ?3, '2026-01-01')", + params![chat, source, STYLE_POLICY_VERSION], + ) + .unwrap(); + // The importer wrote the pacing aggregate itself and kept no sample + // rows: the 34s entry is really the bot, the 30s entry is human. + conn.execute( + "INSERT INTO response_time_stats( + chat, source, user_name, sample_count, average_seconds, median_seconds, + p90_seconds, min_seconds, max_seconds, max_window_seconds, stddev_seconds, + distribution_schema_version, distribution_json + ) VALUES (?1, ?2, '최연우', 2, 32.0, 32.0, 34.0, 30.0, 34.0, 34.0, 2.0, 0, '{}')", + params![chat, source], + ) + .unwrap(); + } + let sends = vec![ConfirmedSelfSend { + event_id: "legacy".into(), + text: bot_reply.into(), + sent_at: bot_sent_at, + }]; + let report = repair_bot_sent_style_samples(&db, chat, 7, &sends, false).unwrap(); + assert_eq!(report.matched_rows, 1); + assert_eq!(report.deleted_response_samples, 0); + assert_eq!(report.rebuilt_sources, vec![source.to_string()]); + let conn = open_db_readonly(&db).unwrap(); + // The bot sentence leaves the style profile: two samples become one. + assert_eq!( + conn.query_row( + "SELECT sample_count FROM owner_style_profile WHERE source = ?1", + params![source], + |row| row.get::<_, i64>(0), + ) + .unwrap(), + 1 + ); + // The pacing aggregate keeps only the human pair (30s). + let (samples, average): (i64, f64) = conn + .query_row( + "SELECT sample_count, average_seconds FROM response_time_stats WHERE source = ?1", + params![source], + |row| Ok((row.get(0)?, row.get(1)?)), + ) + .unwrap(); + assert_eq!(samples, 1); + assert_eq!(average, 30.0); + let (kind, eligible): (String, i64) = conn + .query_row( + "SELECT content_kind, style_eligible FROM owner_style WHERE message = ?1", + params![bot_reply], + |row| Ok((row.get(0)?, row.get(1)?)), + ) + .unwrap(); + assert_eq!(kind, "bot_sent_reply"); + assert_eq!(eligible, 0); +} From 0d7d6874cc8ca24fb2717bacc3e2d9f8ce6cc3c9 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:34:28 +0900 Subject: [PATCH 24/67] test(desktop): pin settings wiring, panel boot and snapshot parsing main.ts had one test for its 600 lines, and the snapshot parser was only checked field by field. These characterization tests record what the settings window, the panel boot and the bridge parser do today, so the clean-code pass that follows can be checked against them. Co-Authored-By: Claude Fable 5.2 --- desktop/src/__tests__/settings-wiring.test.ts | 663 ++++++++++++++++++ .../src/__tests__/snapshot-contract.test.ts | 269 +++++++ 2 files changed, 932 insertions(+) create mode 100644 desktop/src/__tests__/settings-wiring.test.ts create mode 100644 desktop/src/__tests__/snapshot-contract.test.ts diff --git a/desktop/src/__tests__/settings-wiring.test.ts b/desktop/src/__tests__/settings-wiring.test.ts new file mode 100644 index 00000000..0e10e34a --- /dev/null +++ b/desktop/src/__tests__/settings-wiring.test.ts @@ -0,0 +1,663 @@ +// @vitest-environment happy-dom +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { parseRuntimeSnapshot, unavailableSnapshot, type CancellationToken, type RuntimeSnapshot } from "../contracts"; +import type { KnowledgeDrilldown, KnowledgeGraph, KnowledgeNode, KnowledgeView } from "../knowledge/graph-model"; +import type { PollTimerScheduler } from "../runtime-poller"; +import { RESIDENT_MODEL_ID, SWAP_MODEL_ID } from "../tokens"; +import { mainPanelMarkup } from "../ui"; + +type InvokeArgs = Record | undefined; +type FocusHandler = (event: { node: KnowledgeNode; view: KnowledgeView }) => void; + +interface FakeHologram { + graph: KnowledgeGraph; + renderCount: number; + starts: number; + stops: number; + disposals: number; + clickNode(nodeId: string): KnowledgeView; +} + +const invoke = vi.hoisted(() => vi.fn<(command: string, args?: InvokeArgs) => Promise>()); +const stage = vi.hoisted(() => ({ holograms: [] as FakeHologram[], failConstruction: false })); + +vi.mock("@tauri-apps/api/core", () => ({ invoke })); +vi.mock("../knowledge/hologram", async () => { + const model = await import("../knowledge/graph-model"); + return { + KnowledgeHologram: class implements FakeHologram { + renderCount = 7; + starts = 0; + stops = 0; + disposals = 0; + private readonly drilldown: KnowledgeDrilldown; + + constructor( + readonly canvas: HTMLCanvasElement, + readonly graph: KnowledgeGraph, + private readonly onFocus: FocusHandler, + ) { + if (stage.failConstruction) throw new Error("webgl_unavailable"); + this.drilldown = new model.KnowledgeDrilldown(graph); + stage.holograms.push(this); + } + + start(): void { this.starts += 1; } + stop(): void { this.stops += 1; } + dispose(): void { this.disposals += 1; } + + clickNode(nodeId: string): KnowledgeView { + const node = this.graph.nodes.find((candidate) => candidate.id === nodeId); + const view = this.drilldown.clickNode(nodeId); + if (node) this.onFocus({ node, view }); + return view; + } + + expandOneHop(): KnowledgeView { + return this.drilldown.expandOneHop(); + } + }, + }; +}); + +let main: typeof import("../main"); + +beforeAll(async () => { + document.body.innerHTML = '
'; + window.history.replaceState({}, "", "/?view=settings"); + invoke.mockResolvedValue(null); + main = await import("../main"); + await vi.waitFor(() => expect(app().dataset.state).toBe("unavailable")); +}); + +beforeEach(() => { + invoke.mockReset(); + invoke.mockResolvedValue(null); + stage.holograms.length = 0; + stage.failConstruction = false; +}); + +const CONTEXT = { mode: "async", waited: false }; +const settle = (): Promise => new Promise((resolve) => { setTimeout(resolve, 0); }); +const app = (): HTMLElement => document.querySelector("#app")!; +const text = (id: string): string => document.getElementById(id)?.textContent ?? ""; +const choice = (name: "fast" | "deep"): HTMLButtonElement => + document.querySelector(`button[data-model-choice="${name}"]`)!; +const selection = (): Array<[string | null, boolean]> => (["fast", "deep"] as const).map((name) => [ + choice(name).getAttribute("aria-pressed"), + choice(name).classList.contains("selection"), +]); + +function deferred(): { promise: Promise; resolve: (value: T) => void } { + let resolve: (value: T) => void = () => undefined; + const promise = new Promise((accept) => { resolve = accept; }); + return { promise, resolve }; +} + +function snapshotOf(overrides: Record = {}): RuntimeSnapshot { + return parseRuntimeSnapshot({ + available: true, + rooms: [{ chat_id: 417, title: "임원방", live: true }], + available_chats: [{ chat_id: 417, title: "임원방" }, { chat_id: 998, title: "새 방" }], + jobs: [], + context_sync: CONTEXT, + ...overrides, + }); +} + +const GRAPH = { + nodes: [ + { id: "person:a", label: "연우", importance: 90 }, + { id: "term:b", label: "알쿠", importance: 70 }, + { id: "org:c", label: "클라우드", importance: 60 }, + { id: "doc:d", label: "정책", importance: 50 }, + { id: "lone:e", label: "외톨이", importance: 10, evidence: { chat_id: "555" } }, + ], + edges: [ + { source: "person:a", relation: "MENTIONS", target: "term:b", weight: 3, room_id: "417" }, + { source: "term:b", relation: "member-of", target: "org:c", weight: 2 }, + { source: "org:c", relation: "related to", target: "doc:d", weight: 1, room_id: "000" }, + { source: "doc:d", relation: "HAS_POLICY", target: "person:a", weight: 0.5, evidence: { chat_id: "417" } }, + ], +}; + +interface BootOptions { + snapshot?: RuntimeSnapshot | Error; + status?: Record | null | Error; + graph?: Record | null | Error; + roomUpsert?: () => Promise | null>; + wireVoice?: (root: ParentNode, invokeFn: unknown) => void; +} + +async function boot(options: BootOptions = {}) { + const answer = (value: T | Error): Promise => + value instanceof Error ? Promise.reject(value) : Promise.resolve(value); + const loadSnapshot = vi.fn((_token: CancellationToken) => answer(options.snapshot ?? snapshotOf())); + const loadAction = vi.fn((action: string, _input?: Record) => { + if (action === "knowledge-graph-status") return answer(options.status === undefined ? { stale: false } : options.status); + if (action === "knowledge-graph") return answer(options.graph === undefined ? null : options.graph); + if (action === "room-upsert" && options.roomUpsert) return options.roomUpsert(); + return Promise.resolve(null); + }); + const wireVoice = vi.fn(options.wireVoice ?? (() => undefined)); + const invokeCommand = vi.fn(); + await main.bootSettings({ + loadSnapshot, + loadAction: loadAction as never, + wireVoice, + invokeCommand: invokeCommand as never, + subscribeVisibility: null, + readVisibility: null, + }); + return { loadSnapshot, loadAction, wireVoice, invokeCommand }; +} + +describe("answer model choice", () => { + it.each([ + [null, [["true", true], ["false", false]], "빠른 대화가 선택되어 있습니다."], + [`mlx/${SWAP_MODEL_ID}`, [["false", false], ["true", true]], "깊은 분석이 선택되어 있습니다."], + ["someone/else", [["false", false], ["false", false]], "AI 답변 설정을 확인할 수 없습니다."], + ])("shows the model the runtime reports (%s)", async (replyModelId, expected, status) => { + await boot({ snapshot: snapshotOf({ reply_model_id: replyModelId }) }); + + expect(selection()).toEqual(expected); + expect(text("model-status")).toBe(status); + expect([choice("fast").disabled, choice("deep").disabled]).toEqual([false, false]); + }); + + it("locks both choices while the fast model is being saved, then selects it", async () => { + await boot({ snapshot: snapshotOf({ reply_model_id: SWAP_MODEL_ID }) }); + const saved = deferred(); + invoke.mockReturnValueOnce(saved.promise); + + choice("fast").click(); + + expect(invoke.mock.calls).toEqual([["fetch_settings_action", { action: "model-set", model: RESIDENT_MODEL_ID }]]); + expect([choice("fast").disabled, choice("deep").disabled]).toEqual([true, true]); + expect(choice("fast").getAttribute("aria-busy")).toBe("true"); + expect(text("model-status")).toBe("빠른 대화를 선택하고 있습니다…"); + + saved.resolve({ ok: true, action: "model-set", model: RESIDENT_MODEL_ID, stored: true }); + await settle(); + + expect([choice("fast").disabled, choice("deep").disabled]).toEqual([false, false]); + expect(choice("fast").hasAttribute("aria-busy")).toBe(false); + expect(selection()).toEqual([["true", true], ["false", false]]); + expect(text("model-status")).toBe("빠른 대화가 선택되어 있습니다."); + }); + + it("keeps the previous choice and marks the row when the fast model cannot be saved", async () => { + await boot({ snapshot: snapshotOf({ reply_model_id: SWAP_MODEL_ID }) }); + invoke.mockResolvedValueOnce({ ok: false }); + + choice("fast").click(); + await settle(); + + expect(selection()).toEqual([["false", false], ["true", true]]); + expect(choice("fast").classList.contains("model-failed")).toBe(true); + expect(text("model-status")).toBe("빠른 대화를 선택하지 못했습니다. 기존 설정을 유지했습니다."); + + invoke.mockResolvedValueOnce({ ok: true, action: "model-set", model: RESIDENT_MODEL_ID, stored: true }); + choice("fast").click(); + await settle(); + expect(choice("fast").classList.contains("model-failed")).toBe(false); + }); + + it("asks for the deep model with an explicit opt-in and selects it once ready", async () => { + await boot(); + const swapped = deferred(); + invoke.mockReturnValueOnce(swapped.promise); + + choice("deep").click(); + + const [command, args] = invoke.mock.calls[0]; + expect(command).toBe("fetch_settings_action"); + expect(args).toEqual({ action: "model-swap", model: SWAP_MODEL_ID, explicitOptIn: true, tokenId: expect.any(String) }); + expect([choice("fast").disabled, choice("deep").disabled]).toEqual([true, true]); + expect(choice("deep").getAttribute("aria-busy")).toBe("true"); + expect(text("model-status")).toBe("깊은 분석을 준비하고 있습니다…"); + + swapped.resolve({ + ok: true, action: "model-swap", model: SWAP_MODEL_ID, stage: "ready", reason: "ready", stored: true, prepared: true, + }); + await settle(); + + expect([choice("fast").disabled, choice("deep").disabled]).toEqual([false, false]); + expect(choice("deep").hasAttribute("aria-busy")).toBe(false); + expect(selection()).toEqual([["false", false], ["true", true]]); + expect(text("model-status")).toBe("깊은 분석을 사용할 준비가 되었습니다."); + }); + + it("explains a refused deep-model change and leaves the choice alone", async () => { + await boot(); + invoke.mockResolvedValueOnce({ + ok: false, action: "model-swap", model: SWAP_MODEL_ID, stage: "failed", reason: "model_swap_busy", + }); + + choice("deep").click(); + await settle(); + + expect(selection()).toEqual([["true", true], ["false", false]]); + expect(choice("deep").classList.contains("model-failed")).toBe(true); + expect(text("model-status")).toBe("다른 설정 변경이 끝난 뒤 다시 시도해 주세요."); + }); + + it("cancels an unfinished deep-model change when the window closes", async () => { + await boot(); + const swapped = deferred(); + invoke.mockReturnValueOnce(swapped.promise); + choice("deep").click(); + const tokenId = invoke.mock.calls[0][1]?.tokenId; + + window.dispatchEvent(new Event("pagehide")); + await settle(); + + expect(invoke.mock.calls.filter(([command]) => command === "cancel_model_swap")).toEqual([ + ["cancel_model_swap", { tokenId }], + ]); + swapped.resolve(null); + await settle(); + }); + + it.each([ + ["model_residency_uncertain", "AI 설정 변경을 확인하지 못했습니다. 현재 설정을 유지했습니다."], + ["model_swap_busy", "다른 설정 변경이 끝난 뒤 다시 시도해 주세요."], + ["cancelled", "요청을 취소했습니다. 기존 설정을 유지했습니다."], + ["memory_budget_unavailable", "안전하게 사용할 수 있는 메모리가 부족해 설정을 바꾸지 않았습니다."], + ["model_owner_state_invalid", "AI 실행 상태를 확인하지 못해 기존 설정을 유지했습니다."], + ["model_owner_state_stale", "AI 실행 상태를 확인하지 못해 기존 설정을 유지했습니다."], + ["load_failed_rollback_failed", "AI 설정을 바꾸지 못했습니다. 현재 상태를 확인한 뒤 다시 시도해 주세요."], + ["insufficient_free_memory_rollback_failed", "AI 설정을 바꾸지 못했습니다. 현재 상태를 확인한 뒤 다시 시도해 주세요."], + ["load_failed", "깊은 분석을 준비하지 못했습니다. 잠시 후 다시 시도해 주세요."], + ["probe_failed", "깊은 분석을 준비하지 못했습니다. 잠시 후 다시 시도해 주세요."], + ["unload_failed", "깊은 분석을 준비하지 못했습니다. 잠시 후 다시 시도해 주세요."], + ["drain_timeout", "깊은 분석을 준비하지 못했습니다. 기존 설정을 유지했습니다."], + ])("words the %s refusal for the user", (reason, message) => { + expect(main.modelSwapFailureText(reason)).toBe(message); + }); +}); + +describe("voice status line", () => { + it.each([ + ["idle", "마이크가 대기 중입니다."], + ["wake_listen", "호출어를 기다리고 있습니다."], + ["user_listen", "말씀을 듣고 있습니다."], + ["transcribing", "말씀을 정리하고 있습니다."], + ["generating", "답변을 준비하고 있습니다."], + ["speaking", "답변을 말하고 있습니다."], + ["ended", "음성 대화가 끝났습니다."], + ["aborted", "음성 대화가 끝났습니다."], + ["error", "음성 기능을 사용할 수 없습니다."], + ["listening", "음성 상태를 확인하고 있습니다."], + ])("describes the %s state", async (state, message) => { + await boot({ snapshot: snapshotOf({ voice: { available: true, state } }) }); + expect(text("voice-status")).toBe(message); + }); + + it("prefers the error explanation and admits when voice is unreachable", async () => { + await boot({ snapshot: snapshotOf({ voice: { available: true, state: "speaking", error_code: "tts_error" } }) }); + expect(text("voice-status")).toBe("답변을 소리로 들려주지 못했습니다."); + + await boot({ snapshot: snapshotOf({ voice: { available: false, state: "speaking" } }) }); + expect(text("voice-status")).toBe("음성 상태를 확인할 수 없습니다."); + }); +}); + +describe("adding a chat room", () => { + const addButton = (): HTMLButtonElement => document.querySelector("#settings-add-room-button")!; + const pick = (value: string): void => { + document.querySelector("#settings-add-room-select")!.value = value; + }; + + it("does nothing until a room is picked", async () => { + const { loadAction } = await boot(); + addButton().click(); + await settle(); + expect(loadAction.mock.calls.map(([action]) => action)).toEqual(["knowledge-graph-status", "knowledge-graph"]); + expect(text("room-summary")).toBe("등록된 채팅방 1개 · 연결됨 1개"); + }); + + it("registers the picked room and redraws the list from a fresh snapshot", async () => { + const upsert = deferred | null>(); + const { loadAction, loadSnapshot } = await boot({ roomUpsert: () => upsert.promise }); + loadSnapshot.mockResolvedValueOnce(snapshotOf({ + rooms: [{ chat_id: 417, title: "임원방", live: true }, { chat_id: 998, title: "새 방" }], + })); + pick("998"); + + addButton().click(); + + expect(loadAction.mock.calls.at(-1)).toEqual(["room-upsert", { chatId: "998", query: "새 방" }]); + expect(addButton().disabled).toBe(true); + expect(text("room-summary")).toBe("채팅방을 등록하는 중..."); + + upsert.resolve({ ok: true }); + await settle(); + + expect(loadSnapshot).toHaveBeenCalledTimes(2); + expect(addButton().disabled).toBe(false); + expect(text("room-summary")).toBe("등록된 채팅방 2개 · 연결됨 1개"); + expect([...document.querySelectorAll("#settings-add-room-select option")].map((option) => option.value)) + .toEqual([""]); + }); + + it("reports a refused or failed registration without reloading", async () => { + const refused = await boot({ roomUpsert: async () => ({ ok: false }) }); + pick("998"); + addButton().click(); + await settle(); + expect(text("room-summary")).toBe("채팅방을 등록하지 못했습니다. 확인 후 다시 시도해 주세요."); + expect(refused.loadSnapshot).toHaveBeenCalledTimes(1); + expect(addButton().disabled).toBe(false); + + await boot({ roomUpsert: () => Promise.reject(new Error("bridge")) }); + pick("998"); + addButton().click(); + await settle(); + expect(text("room-summary")).toBe("채팅방 등록 중 오류가 발생했습니다."); + expect(addButton().disabled).toBe(false); + }); +}); + +describe("conversation graph", () => { + const relations = (): string[][] => [...document.querySelectorAll("#knowledge-relations .knowledge-relation-row")] + .map((row) => [row.querySelector("strong")?.textContent ?? "", row.querySelector("span")?.textContent ?? ""]); + const nodeButtons = (): HTMLButtonElement[] => + [...document.querySelectorAll("#knowledge-accessible-nodes .knowledge-a11y-node")]; + const expand = (): HTMLButtonElement => document.querySelector("#knowledge-expand-hop")!; + const focusCalls = (): unknown[] => invoke.mock.calls.filter(([, args]) => args?.action === "knowledge-graph-focus"); + + it("summarises the graph and lists its nodes for assistive technology", async () => { + await boot({ graph: GRAPH }); + + expect(app().dataset.state).toBe("ready"); + expect(text("knowledge-summary")).toBe("대화에서 찾은 연결 항목 5개"); + expect(text("knowledge-mode")).toBe("연결된 주제"); + expect(text("settings-sync-source")).toBe("대화 자료가 준비되었습니다."); + expect(nodeButtons().map((button) => [button.type, button.dataset.nodeId, button.textContent, button.getAttribute("aria-label")])) + .toEqual(GRAPH.nodes.map((node) => ["button", node.id, node.label, `${node.label} 선택`])); + expect(stage.holograms).toHaveLength(1); + expect(stage.holograms[0].starts).toBe(1); + expect((window as unknown as { __knowledgeRenderCount: number }).__knowledgeRenderCount).toBe(7); + expect(expand().disabled).toBe(true); + }); + + it("flags stale material and an unreadable status separately", async () => { + await boot({ graph: { ...GRAPH, stale: true }, status: { stale: true } }); + expect(text("knowledge-mode")).toBe("자료 확인 필요"); + expect(text("settings-sync-source")).toBe("대화 자료를 새로 확인해야 합니다."); + expect(app().dataset.state).toBe("ready"); + + await boot({ graph: GRAPH, status: null }); + expect(text("settings-sync-source")).toBe("대화 준비 상태를 확인하지 못했습니다."); + expect(app().dataset.state).toBe("ready"); + + await boot({ graph: GRAPH, status: new Error("bridge") }); + expect(text("settings-sync-source")).toBe("대화 준비 상태를 확인하지 못했습니다."); + expect(app().dataset.state).toBe("unavailable"); + }); + + it("says so when there is nothing to draw", async () => { + await boot({ graph: { nodes: [], edges: [] } }); + expect(text("knowledge-summary")).toBe("아직 연결된 대화가 없습니다."); + expect(text("knowledge-mode")).toBe("준비 중"); + expect(stage.holograms).toHaveLength(0); + expect(nodeButtons()).toHaveLength(0); + expect(app().dataset.state).toBe("ready"); + }); + + it("focuses a node, lists its strongest relations and looks up related talk", async () => { + await boot({ graph: GRAPH }); + const lookup = deferred(); + invoke.mockReturnValueOnce(lookup.promise); + + nodeButtons()[0].click(); + + expect(text("knowledge-focus-title")).toBe("연우"); + expect(expand().disabled).toBe(false); + expect(relations()).toEqual([ + ["연우 —언급→ 알쿠", "대화방 · 임원방"], + ["정책 —연결→ 연우", "대화방 · 임원방"], + ]); + expect(text("knowledge-retrieve")).toBe("관련 대화를 찾고 있습니다…"); + expect(invoke.mock.calls).toEqual([["fetch_settings_action", { + action: "knowledge-graph-focus", + query: "연우", + nodeId: "person:a", + chatId: "417", + model: undefined, + explicitOptIn: undefined, + }]]); + + lookup.resolve({ ok: true, facts: ["첫 사실".padEnd(200, "!"), 7, "둘째"] }); + await settle(); + expect(text("knowledge-retrieve")).toBe(`관련 정보 2건을 찾았습니다. ${"첫 사실".padEnd(180, "!")}`); + }); + + it("names relation kinds and falls back when the room is not enrolled", async () => { + await boot({ graph: GRAPH }); + + nodeButtons()[2].click(); + expect(relations()).toEqual([ + ["알쿠 —소속→ 클라우드", "대화에서 확인한 관계"], + ["클라우드 —관련→ 정책", "대화에서 확인한 관계"], + ]); + + nodeButtons()[4].click(); + expect(text("knowledge-focus-title")).toBe("외톨이"); + expect(document.querySelector("#knowledge-relations .knowledge-empty")?.textContent).toBe("아직 연결된 항목이 없습니다."); + expect(focusCalls().map((call) => (call as [string, { chatId: string }])[1].chatId)).toEqual(["417", "555"]); + }); + + it.each([ + ["PARTICIPATES", "참여"], + ["participates in", "참여"], + ["MEMBER_OF", "소속"], + ["occurred-in", "발생"], + ["ABOUT", "관련"], + ["", "연결"], + ])("labels the %s relation", async (relation, label) => { + await boot({ + graph: { + nodes: [{ id: "a", label: "가" }, { id: "b", label: "나" }], + edges: [{ source: "a", relation, target: "b", weight: 1 }], + }, + }); + nodeButtons()[0].click(); + expect(relations()).toEqual([[`가 —${label}→ 나`, "대화에서 확인한 관계"]]); + }); + + it("reports an empty or failed lookup and ignores an answer for a node no longer in focus", async () => { + await boot({ graph: GRAPH }); + const first = deferred(); + invoke.mockReturnValueOnce(first.promise).mockResolvedValueOnce({ ok: true, facts: [] }); + + nodeButtons()[0].click(); + nodeButtons()[1].click(); + await settle(); + expect(text("knowledge-retrieve")).toBe("관련 대화를 찾지 못했습니다."); + + first.resolve({ ok: true, facts: ["늦은 답"] }); + await settle(); + expect(text("knowledge-focus-title")).toBe("알쿠"); + expect(text("knowledge-retrieve")).toBe("관련 대화를 찾지 못했습니다."); + + invoke.mockResolvedValueOnce({ ok: false, facts: ["숨김"] }); + nodeButtons()[3].click(); + await settle(); + expect(text("knowledge-retrieve")).toBe("관련 대화를 찾지 못했습니다."); + + invoke.mockRejectedValueOnce(new Error("bridge")); + nodeButtons()[0].click(); + await settle(); + expect(text("knowledge-retrieve")).toBe("관련 대화를 찾지 못했습니다."); + }); + + it("widens the focus one step and stops offering more at the limit", async () => { + const chain = { + nodes: ["a", "b", "c", "d", "e"].map((id) => ({ id, label: id.toUpperCase() })), + edges: [["a", "b"], ["b", "c"], ["c", "d"], ["d", "e"]] + .map(([source, target], index) => ({ source, target, relation: "ABOUT", weight: 4 - index })), + }; + await boot({ graph: chain }); + + expand().click(); + expect(relations()).toEqual([]); + + nodeButtons()[1].click(); + expect(relations()).toEqual([ + ["A —관련→ B", "대화에서 확인한 관계"], + ["B —관련→ C", "대화에서 확인한 관계"], + ]); + expect(expand().disabled).toBe(false); + + document.getElementById("knowledge-relations")?.replaceChildren(); + expand().click(); + expect(expand().disabled).toBe(true); + expect(relations()).toHaveLength(2); + expect(focusCalls()).toHaveLength(1); + }); + + it("stops and releases the graph when the window closes", async () => { + await boot({ graph: GRAPH }); + const hologram = stage.holograms[0]; + + window.dispatchEvent(new Event("pagehide")); + + expect([hologram.stops, hologram.disposals]).toEqual([1, 1]); + window.dispatchEvent(new Event("focus")); + expect(hologram.starts).toBe(1); + }); +}); + +describe("settings window that cannot start", () => { + const unavailableTexts = (): Record => Object.fromEntries([ + "room-summary", "model-status", "voice-status", "settings-sync-source", "settings-activity-source", + "knowledge-summary", "knowledge-mode", "history-summary", + ].map((id) => [id, text(id)])); + const FIXED = { + "room-summary": "채팅방 목록을 불러오지 못했습니다. 다시 확인해 주세요.", + "model-status": "AI 답변 설정을 확인할 수 없습니다.", + "voice-status": "음성 상태를 확인할 수 없습니다.", + "settings-sync-source": "대화 준비 상태를 확인할 수 없습니다.", + "settings-activity-source": "앱의 작업 상태를 불러오지 못했습니다.", + "knowledge-summary": "대화에서 찾은 연결 정보를 확인할 수 없습니다.", + "knowledge-mode": "확인 필요", + "history-summary": "최근 답변 기록을 불러오지 못했습니다.", + }; + const allDisabled = (): boolean[] => [...document.querySelectorAll("button")].map((button) => button.disabled); + + it("keeps working with what loaded when only the snapshot is missing", async () => { + await boot({ snapshot: new Error("bridge"), graph: GRAPH }); + + expect(app().dataset.state).toBe("unavailable"); + expect(text("room-summary")).toBe("채팅방 목록을 불러오지 못했습니다. 다시 확인해 주세요."); + expect(text("model-status")).toBe("빠른 대화가 선택되어 있습니다."); + expect(text("knowledge-summary")).toBe("대화에서 찾은 연결 항목 5개"); + expect(choice("fast").disabled).toBe(false); + }); + + it("shows the fixed unavailable screen when wiring fails", async () => { + await boot({ graph: GRAPH, wireVoice: () => { throw new Error("no voice bridge"); } }); + + expect(app().dataset.state).toBe("unavailable"); + expect(unavailableTexts()).toEqual(FIXED); + expect(allDisabled()).toEqual([true, true, true, true, true]); + expect(selection()).toEqual([["true", true], ["false", false]]); + }); + + it("shows the same screen when the graph renderer cannot be created", async () => { + stage.failConstruction = true; + const { wireVoice, invokeCommand } = await boot({ graph: GRAPH }); + + expect(wireVoice).toHaveBeenCalledWith(document, invokeCommand); + expect(app().dataset.state).toBe("unavailable"); + expect(unavailableTexts()).toEqual(FIXED); + expect(allDisabled()).toEqual([true, true, true, true, true]); + }); + + it("falls back to the unavailable screen of the view that failed to boot", async () => { + const failing = { panel: () => Promise.reject(new Error("panel")), settings: () => Promise.reject(new Error("settings")) }; + + await main.startDesktopApp(true, failing); + expect(app().dataset.state).toBe("unavailable"); + expect(unavailableTexts()).toEqual(FIXED); + + await main.startDesktopApp(false, failing); + expect(app().dataset.state).toBe("unavailable"); + expect(text("panel-status")).toBe("Jarvis 상태를 확인할 수 없습니다."); + expect(document.querySelector(".settings-shell")).toBeNull(); + }); +}); + +describe("menu-bar panel boot", () => { + class ManualTimers implements PollTimerScheduler { + readonly pending = new Map void>(); + private nextId = 1; + setTimeout(callback: () => void): number { + this.pending.set(this.nextId, callback); + return this.nextId++; + } + clearTimeout(timerId: number): void { this.pending.delete(timerId); } + } + + function fakeCore() { + return { renderCount: 3, start: vi.fn(), stop: vi.fn(), setSignals: vi.fn(), dispose: vi.fn() }; + } + + it("draws only the core, starts it and feeds it the polled load", async () => { + const core = fakeCore(); + const timers = new ManualTimers(); + const loadSnapshot = vi.fn(async () => snapshotOf({ job_load: 0.4, voice: { available: true, rms: 0.2 } })); + const cancelSnapshot = vi.fn(async () => undefined); + const canvases: HTMLCanvasElement[] = []; + + await main.bootPanel({ + createCore: (canvas) => { canvases.push(canvas); return core; }, + loadSnapshot, + cancelSnapshot, + makeToken: () => ({ id: "token-1", cancelled: false }), + pollScheduler: timers, + subscribeVisibility: null, + readVisibility: null, + }); + await settle(); + + expect(app().innerHTML).toBe(mainPanelMarkup()); + expect(app().dataset.state).toBe("ready"); + expect(canvases).toEqual([document.querySelector(".jarvis-core")]); + expect((window as unknown as { __jarvisRenderCount: number }).__jarvisRenderCount).toBe(3); + expect(core.start).toHaveBeenCalledTimes(1); + expect(loadSnapshot).toHaveBeenCalledTimes(1); + expect(core.setSignals.mock.calls).toEqual([[0.4, 0.2]]); + expect(timers.pending.size).toBe(1); + + window.dispatchEvent(new Event("pagehide")); + + expect(core.stop).toHaveBeenCalled(); + expect(core.dispose).toHaveBeenCalledTimes(1); + expect(timers.pending.size).toBe(0); + window.dispatchEvent(new Event("focus")); + expect(core.start).toHaveBeenCalledTimes(1); + expect(cancelSnapshot).not.toHaveBeenCalled(); + }); + + it("explains itself in place when the renderer cannot be created", async () => { + await main.bootPanel({ + createCore: () => Promise.reject(new Error("webgl_unavailable")), + loadSnapshot: async () => unavailableSnapshot(), + subscribeVisibility: null, + readVisibility: null, + }); + + expect(app().dataset.state).toBe("unavailable"); + expect(document.querySelector(".jarvis-core")?.getAttribute("aria-label")).toBe("Jarvis core 확인 불가"); + const status = document.getElementById("panel-status"); + expect(status?.textContent).toBe("Jarvis 상태를 확인할 수 없습니다."); + expect([status?.tagName, status?.getAttribute("role"), status?.getAttribute("aria-live")]).toEqual(["P", "status", "polite"]); + expect(status?.parentElement?.className).toBe("jarvis-panel"); + expect(status?.getAttribute("style")).toBe( + "position: absolute; left: 12px; right: 12px; bottom: 9px; margin: 0px; text-align: center; font-size: 10px; line-height: 1.35; color: var(--muted); pointer-events: none;", + ); + }); +}); diff --git a/desktop/src/__tests__/snapshot-contract.test.ts b/desktop/src/__tests__/snapshot-contract.test.ts new file mode 100644 index 00000000..5a9f96da --- /dev/null +++ b/desktop/src/__tests__/snapshot-contract.test.ts @@ -0,0 +1,269 @@ +// @vitest-environment happy-dom +import { describe, expect, it } from "vitest"; +import { parseRuntimeSnapshot, unavailableSnapshot, type RuntimeSnapshot } from "../contracts"; +import { renderBackground, renderHistory, renderRooms, settingsMarkup } from "../ui"; + +const CONTEXT = { mode: "async", waited: false }; +const UNSET: Pick = { + background: { + activity: 0, + caption: "", + replyLoad: 0, + geeknews: { state: "unknown", activity: 0, caption: "" }, + dbSync: { state: "unknown", activity: 0, caption: "" }, + }, + onDevice: unavailableSnapshot().onDevice, + pipeline: { active: false, stage: "none", stageIndex: 0, stageTotal: 0, outcome: "unknown" }, +}; + +describe("runtime snapshot boundary", () => { + it("parses the snake_case bridge payload field by field", () => { + const snapshot = parseRuntimeSnapshot({ + available: true, + rooms: [ + { chat_id: 11, title: "가", live: true, auto_reply: true, open_jobs: 2.9 }, + { chat_id: 12 }, + { chat_id: 0, title: "zero" }, + { chat_id: 1.5 }, + "room", + null, + ], + available_chats: [ + { chat_id: 21, title: "나", catalog: true, live: true }, + { chat_id: 22 }, + { chat_id: -1 }, + 7, + ], + jobs: [ + { jobId: "j1", kind: "reply", stage: "model", load: 4, time: 5, errorCode: null }, + { jobId: "incomplete" }, + ], + recent_receipts: [ + { + chatId: 31, title: "다", displayTime: "09-21 16:00", clock: "16:00", outcome: "deferred", + outcome_text: "나중에", reasonCode: "uncertain", reasonText: "보류", retrievalState: "empty", + }, + { + chatId: 32, title: "라", displayTime: "", clock: "", outcome: "sent", + reasonCode: "Bad Code", reasonText: "", retrievalState: "weird", + }, + { chatId: 33, title: "마", displayTime: "", clock: "", outcome: "failed", reasonCode: "x", reasonText: "" }, + { chatId: 0, title: "바", displayTime: "", clock: "", outcome: "sent", reasonCode: "x", reasonText: "" }, + { chatId: 34, title: "사", displayTime: "", clock: "", outcome: "sent", reasonCode: "x" }, + ], + job_load: 1.7, + terminal_counts: { sent: 3.9, skipped: -2, delivery_unknown: 4, burst_superseded: 5 }, + context_sync: CONTEXT, + reply_model_id: "mlx/model", + voice: { + available: true, state: "speaking", rms: 3, error_code: "tts_error", wake_source: "custom", + updated_at: 12.7, wake_phrase: "헤이 자비스", threshold: 0.99, custom_model_selected: true, + }, + error_code: "runtime_note", + }); + + expect(snapshot).toEqual({ + available: true, + rooms: [ + { chatId: 11, title: "가", live: true, autoReply: true, openJobs: 2 }, + { chatId: 12, title: "id:12", live: false, autoReply: false, openJobs: 0 }, + ], + availableChats: [ + { chatId: 21, title: "나", catalog: true, live: true }, + { chatId: 22, title: "id:22", catalog: false, live: false }, + ], + jobs: [{ jobId: "j1", kind: "reply", stage: "model", load: 1, time: 5, errorCode: null }], + recentReceipts: [ + { + chatId: 31, title: "다", displayTime: "09-21 16:00", clock: "16:00", outcome: "deferred", + outcomeText: "나중에", reasonCode: "uncertain", reasonText: "보류", retrievalState: "empty", + }, + { + chatId: 32, title: "라", displayTime: "", clock: "", outcome: "sent", + outcomeText: "", reasonCode: "unspecified", reasonText: "", retrievalState: "unrecorded", + }, + ], + jobLoad: 1, + ...UNSET, + terminal: { sent: 3, skipped: 0, deliveryUnknown: 4, burstSuperseded: 5 }, + contextSync: CONTEXT, + replyModelId: "mlx/model", + voice: { + available: true, state: "speaking", rms: 1, errorCode: "tts_error", wakeSource: "custom", + updatedAt: 12, wakePhrase: "헤이 자비스", threshold: 0.95, customModelSelected: true, + }, + errorCode: "runtime_note", + }); + }); + + it("accepts the camelCase spelling of the same fields", () => { + const snapshot = parseRuntimeSnapshot({ + rooms: [{ chatId: 11, title: "가", live: true, autoReply: true, openJobs: 1 }], + availableChats: [{ chatId: 21, title: "나", catalog: true, live: false }], + jobs: "none", + recentReceipts: [{ + chatId: 31, title: "다", displayTime: "t", clock: "c", outcome: "skipped", + outcomeText: "건너뜀", reasonCode: "low_information", reasonText: "r", retrievalState: "index_not_ready", + }], + jobLoad: -3, + terminal: { sent: 1, skipped: 2, deliveryUnknown: 3, burstSuperseded: 4 }, + contextSync: CONTEXT, + replyModelId: "model", + voice: { + available: true, state: "idle", rms: 0.25, errorCode: "mic_unavailable", wakeSource: "stock", + updatedAt: 9, wakePhrase: "자비스", threshold: 0.1, customModelSelected: true, + }, + errorCode: "ignored_spelling", + }); + + expect(snapshot).toEqual({ + available: true, + rooms: [{ chatId: 11, title: "가", live: true, autoReply: true, openJobs: 1 }], + availableChats: [{ chatId: 21, title: "나", catalog: true, live: false }], + jobs: [], + recentReceipts: [{ + chatId: 31, title: "다", displayTime: "t", clock: "c", outcome: "skipped", + outcomeText: "건너뜀", reasonCode: "low_information", reasonText: "r", retrievalState: "index_not_ready", + }], + jobLoad: 0, + ...UNSET, + terminal: { sent: 1, skipped: 2, deliveryUnknown: 3, burstSuperseded: 4 }, + contextSync: CONTEXT, + replyModelId: "model", + voice: { + available: true, state: "idle", rms: 0.25, errorCode: "mic_unavailable", wakeSource: "stock", + updatedAt: 9, wakePhrase: "자비스", threshold: 0.65, customModelSelected: true, + }, + errorCode: null, + }); + }); + + it("resolves a field given in both spellings the same way every time", () => { + const snapshot = parseRuntimeSnapshot({ + rooms: [{ chat_id: 1, chatId: 2, open_jobs: 3, openJobs: 4 }], + availableChats: "not-a-list", + available_chats: [{ chat_id: 5 }], + recent_receipts: "not-a-list", + recentReceipts: [{ + chatId: 6, title: "t", displayTime: "", clock: "", outcome: "sent", + outcomeText: 7, outcome_text: "snake", reasonCode: "ok", reasonText: "", + }], + job_load: 0.25, + jobLoad: 0.75, + terminal_counts: { delivery_unknown: 1, deliveryUnknown: 2 }, + terminal: { sent: 9 }, + context_sync: CONTEXT, + contextSync: { mode: "sync", waited: true }, + reply_model_id: 7, + replyModelId: "camel", + voice: { + error_code: 5, errorCode: "camel", wake_source: "custom", wakeSource: "stock", + updated_at: 1, updatedAt: 2, wake_phrase: "snake", wakePhrase: "camel", + }, + }); + + expect(snapshot.rooms).toEqual([{ chatId: 1, title: "id:1", live: false, autoReply: false, openJobs: 3 }]); + expect(snapshot.availableChats).toEqual([]); + expect(snapshot.recentReceipts.map((receipt) => receipt.outcomeText)).toEqual(["snake"]); + expect(snapshot.jobLoad).toBe(0.25); + expect(snapshot.terminal).toEqual({ sent: 0, skipped: 0, deliveryUnknown: 1, burstSuperseded: 0 }); + expect(snapshot.available).toBe(true); + expect(snapshot.replyModelId).toBe("camel"); + expect(snapshot.voice).toEqual({ + available: false, state: "unavailable", rms: 0, errorCode: "camel", wakeSource: "stock", + updatedAt: 1, wakePhrase: "snake", threshold: 0.65, customModelSelected: false, + }); + }); + + it("marks a snapshot that waited on context sync as unavailable", () => { + const snapshot = parseRuntimeSnapshot({ context_sync: { mode: "async", waited: true }, error_code: "x" }); + expect(snapshot.available).toBe(false); + expect(snapshot.errorCode).toBe("context_sync_invalid"); + expect(parseRuntimeSnapshot([])).toEqual(unavailableSnapshot("snapshot_invalid")); + }); +}); + +function withBackground(geeknews: string, dbSync: string): RuntimeSnapshot { + return parseRuntimeSnapshot({ + context_sync: CONTEXT, + background: { + activity: 0.5, + replyLoad: 0.6, + geeknews: { state: geeknews, activity: 0.5 }, + db_sync: { state: dbSync, activity: 0.5 }, + }, + }); +} + +describe("settings labels", () => { + it("names every background source state the bridge can report", () => { + document.body.innerHTML = settingsMarkup(); + const label = (geeknews: string, dbSync: string): string => { + renderBackground(withBackground(geeknews, dbSync)); + return document.getElementById("settings-activity-source")?.textContent ?? ""; + }; + + expect(label("sending", "retrying")).toBe("작업 현황 · 답변 대기 2건 · 긱뉴스 전송 중 · 대화 준비 확인 중"); + expect(label("confirmed", "stalled")).toBe("작업 현황 · 답변 대기 2건 · 긱뉴스 확인 중 · 대화 준비 확인 중"); + expect(label("idle", "syncing")).toBe("작업 현황 · 답변 대기 2건 · 긱뉴스 확인 중 · 대화 준비 확인 중"); + expect(label("unknown", "behind")).toBe("작업 현황 · 답변 대기 2건 · 긱뉴스 확인 중 · 대화 준비 새로 확인 필요"); + expect(label("sending", "ready")).toBe("작업 현황 · 답변 대기 2건 · 긱뉴스 전송 중 · 대화 준비 완료"); + expect(label("ready", "sending")).toBe("작업 현황 · 답변 대기 2건 · 긱뉴스 확인 중 · 대화 준비 확인 중"); + }); + + it("falls back to its own labels for each receipt outcome, reason and lookup state", () => { + document.body.innerHTML = settingsMarkup(); + const rows: Array<[string, string, string, string, string]> = [ + ["sent", "direct_question", "ok", "답변 완료", "답변 완료 · 질문에 답변 · 대화 찾기 자료 확인됨"], + ["deferred", "uncertain", "empty", "나중에 처리", "나중에 처리 · 판단을 보류한 대화 · 대화 찾기 관련 자료 없음"], + ["scheduled", "low_information", "skipped", "예약됨", "예약됨 · 답변할 정보가 부족한 대화 · 대화 찾기 확인하지 않음"], + ["skipped", "already_commented", "error", "건너뜀", "건너뜀 · 이미 답변한 대화 · 대화 찾기 자료를 확인하지 못함"], + ["sent", "social_reply", "index_not_ready", "답변 완료", "답변 완료 · 사유가 기록되지 않았습니다 · 대화 찾기 자료 준비 중"], + ["sent", "ok", "unrecorded", "답변 완료", "답변 완료 · 사유가 기록되지 않았습니다 · 대화 찾기 상태 확인 중"], + ]; + renderHistory(parseRuntimeSnapshot({ + context_sync: CONTEXT, + recent_receipts: rows.map(([outcome, reasonCode, retrievalState], index) => ({ + chatId: index + 1, title: "방", displayTime: index === 0 ? "09-21" : "", clock: index === 1 ? "16:00" : "", + outcome, reasonCode, reasonText: "", retrievalState, + })), + })); + + const cards = [...document.querySelectorAll("#history-list .history-receipt-card")]; + expect(cards.map((card) => card.getAttribute("role"))).toEqual(rows.map(() => "listitem")); + expect(cards.map((card) => card.querySelector(".history-receipt-badge")?.textContent)).toEqual(rows.map((row) => row[3])); + expect(cards.map((card) => card.querySelector(".history-receipt-detail")?.textContent)).toEqual(rows.map((row) => row[4])); + expect(cards.map((card) => card.querySelector(".history-receipt-title")?.textContent)).toEqual([ + "09-21 · 방", "16:00 · 방", "시간 미기록 · 방", "시간 미기록 · 방", "시간 미기록 · 방", "시간 미기록 · 방", + ]); + expect(document.getElementById("history-summary")?.textContent).toBe("최근 6건 · 메시지 내용은 표시하지 않습니다."); + }); + + it("explains an empty room list differently when the runtime is unreachable", () => { + document.body.innerHTML = settingsMarkup(); + const texts = (): string[] => [ + document.querySelector("#settings-room-popup option")?.textContent ?? "", + document.getElementById("room-summary")?.textContent ?? "", + document.querySelector("#settings-add-room-select option")?.textContent ?? "", + ]; + + renderRooms(parseRuntimeSnapshot({ context_sync: CONTEXT })); + expect(texts()).toEqual(["등록된 채팅방이 없습니다.", "등록된 채팅방이 없습니다.", "추가 가능한 새 채팅방 없음"]); + + renderRooms(unavailableSnapshot()); + expect(texts()).toEqual([ + "채팅방 목록을 불러오지 못했습니다.", + "채팅방 목록을 불러오지 못했습니다. 다시 확인해 주세요.", + "추가 가능한 새 채팅방 없음", + ]); + + renderRooms(parseRuntimeSnapshot({ + context_sync: CONTEXT, + rooms: [{ chat_id: 1, title: "가", live: true }, { chat_id: 2, title: "나" }], + available_chats: [{ chat_id: 2, title: "나" }, { chat_id: 3, title: "다" }], + })); + expect(texts()).toEqual(["가", "등록된 채팅방 2개 · 연결됨 1개", "추가할 채팅방 선택"]); + expect([...document.querySelectorAll("#settings-add-room-select option")].map((option) => option.value)) + .toEqual(["", "3"]); + }); +}); From 964f2d0a92afe7d27b1fe1a84293a3c29d9165dd Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:34:28 +0900 Subject: [PATCH 25/67] test(scripts): pin DREAM-RSI report envelopes and voice file-pipeline reports The provider tests asserted status and reason only. Pin the whole report for every failure path of both paper providers, the fixed shapes of the DPO and runtime-probe rejections, and the voice file pipeline's JSON report, exit codes and WAV fallback writer. Co-Authored-By: Claude Fable 5.2 --- tests/test_dream_rsi_alphaxiv.py | 471 +++++++++++++++++++++++++++++ tests/test_jarvis_voice_runtime.py | 327 ++++++++++++++++++++ 2 files changed, 798 insertions(+) diff --git a/tests/test_dream_rsi_alphaxiv.py b/tests/test_dream_rsi_alphaxiv.py index 46f580a8..64530851 100644 --- a/tests/test_dream_rsi_alphaxiv.py +++ b/tests/test_dream_rsi_alphaxiv.py @@ -4,6 +4,7 @@ import json import io +import os import subprocess import tempfile import unicodedata @@ -17,6 +18,7 @@ DPO_EVAL_UNAVAILABLE, DREAM_RSI_PAPER_ID, MAX_CLI_JSON_BYTES, + MAX_SUMMARY_CHARS, build_dpo_provenance, build_exploration_provenance, build_provenance_report, @@ -492,6 +494,475 @@ def test_absent_runtime_probe_never_implies_previous_success(self): self.assertFalse(probe["stale_success_reused"]) +def _unavailable( + reason: str, + *, + provider: str, + requested: str, + isolated: bool, + query: str = "DREAM-RSI", + resolved: str | None = None, + commands: tuple = (), + selected: dict | None = None, +) -> dict: + return { + "status": "unavailable", + "reason": reason, + "analysis_available": False, + "provider": provider, + "query": query, + "selection_method": "none", + "selected_paper": selected, + "evidence": None, + "string_similarity_used": False, + "cli": { + "requested": requested, + "resolved": resolved, + "isolated_context": isolated, + "commands": list(commands), + }, + } + + +def _command( + operation: str, + argv: list, + *, + returncode: int | None = 0, + reason: str = "", + stderr: str = "", +) -> dict: + record: dict = { + "operation": operation, + "argv": argv, + "status": "unavailable" if reason else "ok", + "returncode": returncode, + } + if reason: + record["reason"] = reason + if stderr: + record["stderr"] = stderr + return record + + +_PAPER = DREAM_RSI_PAPER_ID +_SEARCH = ["search", "papers", "DREAM-RSI", "--json"] +_USE = ["context", "use", "paper", _PAPER] +_SHOW = ["context", "show", "--json"] +_SUMMARY = ["paper", "summary", "--json"] +_ORX = ["paper", _PAPER, "--source", "alphaxiv", "--no-telemetry"] +_DIRECT = {"paper_id": _PAPER, "title": "", "url": ""} +_ORX_SELECTED = { + "paper_id": _PAPER, + "title": "", + "url": f"https://www.alphaxiv.org/abs/{_PAPER}", +} + + +def _json(payload: object) -> bytes: + return json.dumps(payload).encode() + + +class PaperReportEnvelopeTests(unittest.TestCase): + """Pin the whole report a caller serializes, not only status and reason.""" + + def test_alphaxiv_rejections_before_any_command(self): + cases = [ + ({"query": "-rf"}, "invalid_query:query_looks_like_option", {"query": "-rf"}), + ({"timeout": 0.05}, "invalid_timeout", {}), + ({"timeout": 61}, "invalid_timeout", {}), + ({"alphaxiv_cli": "../alphaxiv"}, "invalid_cli_path", {"requested": "../alphaxiv"}), + ({"paper_id": "not a paper"}, "invalid_paper_id", {"resolved": "/mock/alphaxiv"}), + ] + for arguments, reason, envelope in cases: + with self.subTest(reason=reason, arguments=arguments), mock.patch( + "scripts.dream_rsi_alphaxiv.shutil.which", return_value="/mock/alphaxiv" + ), mock.patch("scripts.dream_rsi_alphaxiv.subprocess.run") as run: + call = {"query": "DREAM-RSI", "paper_source": "alphaxiv", **arguments} + report = collect_paper_report(call.pop("query"), **call) + run.assert_not_called() + expected = { + "provider": "alphaxiv_cli", + "requested": "alphaxiv", + "isolated": True, + **envelope, + } + self.assertEqual(report, _unavailable(reason, **expected)) + + @mock.patch("scripts.dream_rsi_alphaxiv.shutil.which", return_value=None) + def test_unresolved_providers_report_the_provider_they_tried(self, _which): + long_query = "q" * 600 + self.assertEqual( + collect_paper_report("DREAM-RSI", paper_source="alphaxiv"), + _unavailable( + "cli_missing", provider="alphaxiv_cli", requested="alphaxiv", isolated=True + ), + ) + self.assertEqual( + collect_paper_report(long_query, paper_source="bogus", alphaxiv_cli="ax"), + _unavailable( + "invalid_paper_source", + provider="alphaxiv_cli", + requested="ax", + isolated=True, + query="q" * 512, + ), + ) + self.assertEqual( + collect_paper_report(long_query, paper_source="auto", orx_cli="orx-next"), + _unavailable( + "cli_missing", + provider="orx_cli", + requested="orx-next", + isolated=False, + query="q" * 512, + ), + ) + self.assertEqual( + collect_orx_paper_report("DREAM-RSI"), + _unavailable("cli_missing", provider="orx_cli", requested="orx", isolated=False), + ) + + def test_alphaxiv_failures_keep_commands_and_selected_paper(self): + def show_ok(): + return _completed(_json({"paper_id": _PAPER, "title": "Ctx"})) + + titled = {"paper_id": _PAPER, "title": "Ctx", "url": ""} + use_ok = _command("context_use_paper", _USE) + cases = [ + ( + "cli_nonzero", + {"paper_id": None}, + [_completed(returncode=7, stderr=b"boom")], + [_command("search_papers", _SEARCH, returncode=7, reason="cli_nonzero", stderr="boom")], + None, + ), + ( + "incomplete_search_response", + {"paper_id": None}, + [_completed(b"[]")], + [_command("search_papers", _SEARCH)], + None, + ), + ( + "cli_timeout", + {}, + [subprocess.TimeoutExpired(["alphaxiv"], 1)], + [_command("context_use_paper", _USE, returncode=None, reason="cli_timeout")], + _DIRECT, + ), + ( + "invalid_json", + {}, + [_completed(b"selected"), _completed(b"not-json")], + [use_ok, _command("context_show", _SHOW, reason="invalid_json")], + _DIRECT, + ), + ( + "paper_identity_mismatch", + {}, + [ + _completed(b"selected"), + _completed(_json({"paper_id": "2609.00001", "title": "Other"})), + ], + [use_ok, _command("context_show", _SHOW)], + _DIRECT, + ), + ( + "incomplete_paper_metadata", + {}, + [_completed(b"selected"), _completed(_json({"paper_id": _PAPER}))], + [use_ok, _command("context_show", _SHOW)], + _DIRECT, + ), + ( + "cli_nonzero", + {}, + [_completed(b"selected"), show_ok(), _completed(returncode=1)], + [ + use_ok, + _command("context_show", _SHOW), + _command("paper_summary", _SUMMARY, returncode=1, reason="cli_nonzero"), + ], + titled, + ), + ( + "incomplete_summary_response", + {}, + [_completed(b"selected"), show_ok(), _completed(_json({"summary": ""}))], + [use_ok, _command("context_show", _SHOW), _command("paper_summary", _SUMMARY)], + titled, + ), + ] + for reason, arguments, outputs, commands, selected in cases: + with self.subTest(reason=reason, commands=len(commands)), mock.patch( + "scripts.dream_rsi_alphaxiv.shutil.which", return_value="/mock/alphaxiv" + ), mock.patch("scripts.dream_rsi_alphaxiv.subprocess.run", side_effect=outputs): + call = {"paper_source": "alphaxiv", "paper_id": _PAPER, **arguments} + self.assertEqual( + collect_paper_report("DREAM-RSI", **call), + _unavailable( + reason, + provider="alphaxiv_cli", + requested="alphaxiv", + isolated=True, + resolved="/mock/alphaxiv", + commands=tuple(commands), + selected=selected, + ), + ) + + @mock.patch("scripts.dream_rsi_alphaxiv.subprocess.run") + @mock.patch("scripts.dream_rsi_alphaxiv.shutil.which", return_value="/mock/alphaxiv") + def test_alphaxiv_search_success_report_and_isolated_environment(self, _which, run): + found = { + "paper_id": _PAPER, + "title": "Found", + "url": f"https://arxiv.org/abs/{_PAPER}", + } + run.side_effect = [ + _completed(_json({"papers": [found, {"paper_id": "2609.00002", "title": "Second"}]})), + _completed(b"selected"), + _completed(_json({"paper_id": _PAPER, "title": "Ctx"})), + _completed(_json({"summary": " verified summary "})), + ] + + report = collect_paper_report("DREAM-RSI", paper_source="alphaxiv", timeout=3) + + self.assertEqual( + report, + { + "status": "ok", + "reason": "alphaxiv_summary_verified", + "analysis_available": True, + "provider": "alphaxiv_cli", + "query": "DREAM-RSI", + "selection_method": "alphaxiv_search_rank_1", + "selected_paper": found, + "evidence": { + "kind": "alphaxiv_paper_summary", + "summary": "verified summary", + "source_operation": "paper_summary", + }, + "string_similarity_used": False, + "cli": { + "requested": "alphaxiv", + "resolved": "/mock/alphaxiv", + "isolated_context": True, + "commands": [ + _command("search_papers", _SEARCH), + _command("context_use_paper", _USE), + _command("context_show", _SHOW), + _command("paper_summary", _SUMMARY), + ], + }, + }, + ) + homes = {call.kwargs["env"]["ALPHAXIV_HOME"] for call in run.call_args_list} + self.assertEqual(len(homes), 1) + self.assertTrue(Path(homes.pop()).name.startswith("openkakao-alphaxiv-")) + self.assertEqual({call.kwargs["timeout"] for call in run.call_args_list}, {3.0}) + + def test_orx_rejections_before_any_command(self): + cases = [ + ({"query": "-x"}, "invalid_query:query_looks_like_option", {"query": "-x"}), + ({"paper_id": "../etc"}, "invalid_paper_id", {}), + ({"timeout": "soon"}, "invalid_timeout", {}), + ({"orx_cli": "bin/orx"}, "invalid_cli_path", {"requested": "bin/orx"}), + ] + for arguments, reason, envelope in cases: + with self.subTest(reason=reason), mock.patch( + "scripts.dream_rsi_alphaxiv.shutil.which", return_value="/mock/orx" + ), mock.patch("scripts.dream_rsi_alphaxiv.subprocess.run") as run: + call = {"query": "DREAM-RSI", **arguments} + report = collect_orx_paper_report(call.pop("query"), **call) + run.assert_not_called() + expected = { + "provider": "orx_cli", + "requested": "orx", + "isolated": False, + **envelope, + } + self.assertEqual(report, _unavailable(reason, **expected)) + + def test_orx_failures_keep_command_and_selected_paper(self): + header = f"alphaXiv: https://www.alphaxiv.org/abs/{_PAPER}\n" + ran = _command("orx_paper", _ORX) + cases = [ + ( + "cli_nonzero", + _completed(returncode=3), + _command("orx_paper", _ORX, returncode=3, reason="cli_nonzero"), + ), + ("incomplete_summary_response", _completed(b"\n\n"), ran), + ( + "identity_mismatch", + _completed(b"alphaXiv: https://www.alphaxiv.org/abs/2609.00001\nbody"), + ran, + ), + ("incomplete_summary_response", _completed(header.encode()), ran), + ( + "text_too_long", + _completed((header + "x" * (MAX_SUMMARY_CHARS + 1)).encode()), + ran, + ), + ] + for reason, output, command in cases: + with self.subTest(reason=reason), mock.patch( + "scripts.dream_rsi_alphaxiv.shutil.which", return_value="/mock/orx" + ), mock.patch("scripts.dream_rsi_alphaxiv.subprocess.run", return_value=output): + self.assertEqual( + collect_orx_paper_report("DREAM-RSI"), + _unavailable( + reason, + provider="orx_cli", + requested="orx", + isolated=False, + resolved="/mock/orx", + commands=(command,), + selected=_ORX_SELECTED, + ), + ) + + @mock.patch("scripts.dream_rsi_alphaxiv.subprocess.run") + @mock.patch("scripts.dream_rsi_alphaxiv.shutil.which", return_value="/mock/orx") + def test_orx_success_report_environment_and_clamped_timeout(self, _which, run): + run.return_value = _completed( + f"\nalphaXiv: http://alphaxiv.org/abs/{_PAPER}v2\n# Report\nverified\n".encode() + ) + + report = collect_orx_paper_report("DREAM-RSI", timeout=1000) + + self.assertEqual( + report, + { + "status": "ok", + "reason": "orx_report_verified", + "analysis_available": True, + "provider": "orx_cli", + "query": "DREAM-RSI", + "selection_method": "direct_paper_id", + "selected_paper": _ORX_SELECTED, + "evidence": { + "kind": "orx_alphaxiv_paper_report", + "summary": "# Report\nverified", + "source_operation": "orx_paper", + }, + "string_similarity_used": False, + "cli": { + "requested": "orx", + "resolved": "/mock/orx", + "isolated_context": False, + "commands": [_command("orx_paper", _ORX)], + }, + }, + ) + self.assertEqual(run.call_args.kwargs["timeout"], 60.0) + self.assertEqual(run.call_args.kwargs["env"], dict(os.environ)) + + collect_orx_paper_report("DREAM-RSI", timeout=0.0001) + self.assertEqual(run.call_args.kwargs["timeout"], 0.1) + + +class InvalidProvenanceInputShapeTests(unittest.TestCase): + def test_dpo_rejections_return_fixed_shapes(self): + self.assertEqual( + build_dpo_provenance(None), + { + "status": DPO_EVAL_UNAVAILABLE, + "reason": "not_supplied", + "evaluated": 0, + "unavailable": 0, + "mean_loss": None, + "method": "response_token_logprob", + "string_similarity_used": False, + "pairs": [], + }, + ) + cases = [ + ({"pairs": "p1"}, "invalid_pairs"), + ({"pairs": [{}] * 129}, "invalid_pairs"), + ({"pairs": [], "beta": "steep"}, "invalid_dpo_metadata"), + ({"pairs": [], "tokenizer_id": "tok\x00"}, "invalid_dpo_metadata"), + ({"pairs": [], "base_model": "b" * 513}, "invalid_dpo_metadata"), + ({"pairs": [], "beta": 0}, "invalid_beta"), + ({"pairs": [], "beta": float("nan")}, "invalid_beta"), + ({"pairs": [{"pair_id": "p1"}, "p2"]}, "invalid_pair"), + ] + for dpo, reason in cases: + with self.subTest(reason=reason, dpo=str(dpo)[:40]): + self.assertEqual( + build_dpo_provenance(dpo), + { + "status": "invalid_input", + "reason": reason, + "method": "response_token_logprob", + "string_similarity_used": False, + "pairs": [], + }, + ) + + def test_runtime_probe_rejections_keep_the_no_reuse_flags(self): + base = { + "status": "not_supplied", + "source": "none", + "freshness": "unknown", + "stale_success_reused": False, + "probe_executed_by_report": False, + "model_switch_performed": False, + } + valid = { + "gateway": "http://127.0.0.1:11234/v1", + "model": "mlx/model", + "result": "OK", + "latency_ms": 10, + "timeout_ms": 100, + } + self.assertEqual(build_runtime_model_probe_provenance(None), base) + cases = [ + ("probe", "invalid_runtime_model_probe"), + ({**valid, "gateway": ""}, "invalid_runtime_model_probe"), + ({**valid, "latency_ms": "fast"}, "invalid_runtime_model_probe"), + ({**valid, "timeout_ms": None}, "invalid_runtime_model_probe"), + ({**valid, "latency_ms": -1}, "invalid_probe_timing"), + ({**valid, "latency_ms": 101}, "invalid_probe_timing"), + ({**valid, "timeout_ms": 0, "latency_ms": 0}, "invalid_probe_timing"), + ({**valid, "advertised_models": "all"}, "invalid_advertised_models"), + ({**valid, "advertised_models": [{}] * 17}, "invalid_advertised_models"), + ({**valid, "advertised_models": ["model"]}, "invalid_advertised_model"), + ( + {**valid, "advertised_models": [{"model": "m", "loaded": True}]}, + "invalid_advertised_model", + ), + ( + {**valid, "advertised_models": [{"model": "m", "state": "ready", "loaded": 1}]}, + "invalid_advertised_model", + ), + ] + for probe, reason in cases: + with self.subTest(reason=reason, probe=str(probe)[-60:]): + self.assertEqual( + build_runtime_model_probe_provenance(probe), + {**base, "status": "invalid_input", "reason": reason}, + ) + self.assertEqual( + build_runtime_model_probe_provenance( + {**valid, "advertised_models": [{"model": "m", "state": "ready", "loaded": True}]} + ), + { + "status": "observed", + "source": "caller_supplied_current_observation", + "freshness": "current_observation", + "observed_date": "", + **valid, + "advertised_models": [{"model": "m", "loaded": True, "state": "ready"}], + "stale_success_reused": False, + "probe_executed_by_report": False, + "model_switch_performed": False, + }, + ) + + class GoldenReplayRegressionTests(unittest.TestCase): def test_default_replay_cap_is_400_human_rows_without_model_gold(self): with tempfile.TemporaryDirectory() as tmp: diff --git a/tests/test_jarvis_voice_runtime.py b/tests/test_jarvis_voice_runtime.py index 66e5c92c..9ccc4782 100644 --- a/tests/test_jarvis_voice_runtime.py +++ b/tests/test_jarvis_voice_runtime.py @@ -15,12 +15,20 @@ if str(SCRIPTS) not in sys.path: sys.path.insert(0, str(SCRIPTS)) +import jarvis_voice # noqa: E402 +from jarvis_abort import AbortController # noqa: E402 from jarvis_voice import ( # noqa: E402 VOICE_MIC_FRAME_SAMPLES, + WHISPER_MODEL_ID, + AudioFrameAnalysis, + JarvisVoicePipeline, + Qwen3TtsAdapter, + VoiceResult, VoiceState, VoiceStatusStore, _MicrophoneDisconnected, _MicrophoneFramePoller, + run_file_pipeline, run_microphone_session, ) @@ -177,5 +185,324 @@ def unavailable(**_kwargs: object): self.assertEqual(disconnected_result.error_code, "mic_disconnected") +class FixedStt: + def transcribe(self, pcm16: bytes, sample_rate: int, token: object) -> str: + return " 회의 일정 알려줘 " + + +class FixedLlm: + def generate(self, text: str, token: object, *, history: object = ()) -> str: + return "세 시입니다." + + +class FailingTts: + def speak(self, text: str, token: object) -> None: + raise RuntimeError("speaker offline") + + +class FakeWakeFrontend: + def __init__(self, scores: list[tuple[float, float | None]]) -> None: + self.scores = list(scores) + self.frames: list[bytes] = [] + + def analyze(self, pcm16: bytes) -> AudioFrameAnalysis: + self.frames.append(pcm16) + stock, custom = self.scores.pop(0) if self.scores else (0.0, None) + return AudioFrameAnalysis( + rms=0.2, speech=False, stock_wake_score=stock, custom_wake_score=custom + ) + + +class FakeTtsEngine: + def __init__(self, error: Exception | None = None) -> None: + self.error = error + self.calls: list[tuple[str, Path]] = [] + + def write_wav(self, text: str, path: Path, token: object) -> dict[str, object]: + self.calls.append((text, path)) + if self.error is not None: + raise self.error + return {"path": str(path), "bytes": 44, "sample_rate": 24000, "nframes": 0} + + +class FailedTurnResultTests(unittest.TestCase): + def test_failed_turn_keeps_the_transcript_and_carries_no_reply(self) -> None: + with TemporaryDirectory() as temp_dir: + pipeline = JarvisVoicePipeline( + stt=FixedStt(), + llm=FixedLlm(), + tts=FailingTts(), + token=AbortController(Path(temp_dir)).token(), + ) + failed = pipeline.process_utterance(b"\x01\x00" * VOICE_MIC_FRAME_SAMPLES) + later = pipeline.mic_disconnected() + + self.assertEqual( + failed, VoiceResult(VoiceState.ERROR, "tts_error", "회의 일정 알려줘", "") + ) + self.assertEqual( + later, VoiceResult(VoiceState.ERROR, "mic_disconnected", "회의 일정 알려줘", "") + ) + + +class FilePipelineReportTests(unittest.TestCase): + FRAME = b"\x01\x00" * VOICE_MIC_FRAME_SAMPLES + + def _run( + self, + scores: list[tuple[float, float | None]], + *, + wake_pcm: bytes | None = None, + custom_model: Path | None = None, + tts_error: Exception | None = None, + abort_first: bool = False, + ) -> tuple[dict[str, object], FakeWakeFrontend, FakeTtsEngine, dict[str, mock.Mock], dict[str, object]]: + frontend = FakeWakeFrontend(scores) + engine = FakeTtsEngine(tts_error) + wake = self.FRAME * 3 if wake_pcm is None else wake_pcm + constructors = { + "frontend": mock.Mock(return_value=frontend), + "stt": mock.Mock(return_value=FixedStt()), + "llm": mock.Mock(return_value=FixedLlm()), + "tts": mock.Mock(return_value=engine), + } + with TemporaryDirectory() as temp_dir: + root = Path(temp_dir) + if abort_first: + AbortController(root).abort() + with mock.patch.dict(os.environ, {"OPENKAKAO_VOICE_ENV": "1"}), mock.patch( + "jarvis_voice.resolve_custom_wake_model", return_value=custom_model + ), mock.patch("jarvis_voice.OpenWakeVadFrontend", constructors["frontend"]), mock.patch( + "jarvis_voice.MlxWhisperAdapter", constructors["stt"] + ), mock.patch("jarvis_voice.LocalMlxLlm", constructors["llm"]), mock.patch( + "jarvis_voice.Qwen3TtsAdapter", constructors["tts"] + ), mock.patch( + "jarvis_voice._load_wav_pcm16", + side_effect=lambda path: wake if path.name == "wake.wav" else self.FRAME * 4, + ): + report = run_file_pipeline( + state_root=root, + wake_wav=root / "wake.wav", + utterance_wav=root / "utterance.wav", + tts_out=root / "reply.wav", + custom_wake_model=custom_model, + ) + status = json.loads((root / "jarvis-voice-status.json").read_text(encoding="utf-8")) + constructors["llm"].assert_called_once_with(state_root=root) + constructors["frontend"].assert_called_once_with(custom_wake_model=custom_model) + report = json.loads(json.dumps(report).replace(str(root), "")) + return report, frontend, engine, constructors, status + + def test_wake_miss_reports_the_strongest_scores(self) -> None: + report, frontend, engine, _constructors, status = self._run( + [(0.2, 0.3), (0.4, None)], wake_pcm=self.FRAME * 3 + b"\x00" * 100 + ) + + self.assertEqual( + list(report.items()), + [ + ("accepted", False), + ("state", "ended"), + ("error_code", "wake_miss"), + ("stock_max", 0.4), + ("custom_max", 0.3), + ("custom_model_selected", False), + ], + ) + self.assertEqual(frontend.frames, [self.FRAME] * 3) + self.assertEqual(engine.calls, []) + self.assertEqual((status["state"], status["error_code"]), ("ended", "wake_miss")) + + def test_abort_while_waiting_for_the_wake_word_ends_the_run(self) -> None: + report, frontend, _engine, _constructors, _status = self._run( + [(0.9, None)], abort_first=True + ) + + self.assertEqual( + list(report.items()), + [ + ("accepted", False), + ("state", "aborted"), + ("error_code", "global_abort"), + ("stock_max", 0.9), + ("custom_max", 0.0), + ("custom_model_selected", False), + ], + ) + self.assertEqual(len(frontend.frames), 1) + + def test_accepted_wake_runs_one_turn_and_reports_it(self) -> None: + custom_model = Path("/models/hey_jarvis_ko.onnx") + report, frontend, engine, constructors, status = self._run( + [(0.1, 0.2), (0.3, 0.8), (0.99, 0.99)], custom_model=custom_model + ) + + self.assertEqual( + list(report.items()), + [ + ("accepted", True), + ("state", "ended"), + ("error_code", ""), + ("transcript", "회의 일정 알려줘"), + ("reply", "세 시입니다."), + ("stock_max", 0.3), + ("custom_max", 0.8), + ("custom_model_selected", True), + ("threshold", 0.65), + ( + "tts", + {"path": "/reply.wav", "bytes": 44, "sample_rate": 24000, "nframes": 0}, + ), + # The turn re-arms the wake listener before the report is built. + ("wake_source", "none"), + ], + ) + self.assertEqual(len(frontend.frames), 2) + self.assertEqual([text for text, _path in engine.calls], ["세 시입니다."]) + constructors["stt"].assert_called_once_with(model=WHISPER_MODEL_ID) + self.assertEqual(status["state"], "wake_listen") + self.assertTrue(status["custom_model_selected"]) + + def test_failed_speech_reports_the_error_without_a_reply(self) -> None: + report, _frontend, _engine, _constructors, _status = self._run( + [(0.7, None)], tts_error=RuntimeError("disk full") + ) + + self.assertEqual( + list(report.items()), + [ + ("accepted", True), + ("state", "error"), + ("error_code", "tts_error"), + ("transcript", "회의 일정 알려줘"), + ("reply", ""), + ("stock_max", 0.7), + ("custom_max", 0.0), + ("custom_model_selected", False), + ("threshold", 0.65), + ("tts", {}), + ("wake_source", "stock"), + ], + ) + + def test_refuses_to_run_outside_the_voice_environment(self) -> None: + with TemporaryDirectory() as temp_dir, mock.patch.dict(os.environ, clear=False) as env: + env.pop("OPENKAKAO_VOICE_ENV", None) + with self.assertRaisesRegex(RuntimeError, "voice_environment_not_isolated"): + run_file_pipeline( + state_root=Path(temp_dir), + wake_wav=Path(temp_dir) / "wake.wav", + utterance_wav=Path(temp_dir) / "utterance.wav", + tts_out=Path(temp_dir) / "reply.wav", + ) + + +class VoiceCommandLineTests(unittest.TestCase): + def _main(self, argv: list[str], **patches: object) -> tuple[int, str]: + import io + from contextlib import redirect_stdout + + stdout = io.StringIO() + patches.setdefault("run_microphone_session", mock.Mock(side_effect=AssertionError)) + with mock.patch.multiple("jarvis_voice", **patches), redirect_stdout(stdout): + code = jarvis_voice.main(argv) + return code, stdout.getvalue() + + def test_microphone_session_exit_code_follows_the_final_state(self) -> None: + cases = [ + (VoiceResult(VoiceState.ENDED, "silence_timeout"), 0), + (VoiceResult(VoiceState.ABORTED, "global_abort"), 0), + (VoiceResult(VoiceState.ERROR, "mic_unavailable"), 1), + ] + for result, expected in cases: + with self.subTest(state=result.state): + session = mock.Mock(return_value=result) + code, printed = self._main( + ["--state-root", "/state", "--custom-wake-model", "/models/ko.onnx"], + run_microphone_session=session, + ) + self.assertEqual(code, expected) + self.assertEqual( + printed, + json.dumps({"state": result.state.value, "error_code": result.error_code}) + "\n", + ) + session.assert_called_once_with( + state_root=Path("/state"), custom_wake_model=Path("/models/ko.onnx") + ) + + def test_default_state_root_honours_the_override(self) -> None: + session = mock.Mock(return_value=VoiceResult(VoiceState.ENDED)) + with mock.patch.dict(os.environ, {"OPENKAKAO_STATE_ROOT": "/override/state"}): + self._main([], run_microphone_session=session) + session.assert_called_once_with( + state_root=Path("/override/state"), custom_wake_model=None + ) + + def test_file_pipeline_needs_all_three_paths(self) -> None: + for argv in (["--file-wake", "w.wav"], ["--tts-out", "o.wav", "--file-utterance", "u.wav"]): + with self.subTest(argv=argv): + with self.assertRaises(SystemExit) as raised: + self._main(["--state-root", "/state", *argv]) + self.assertEqual(raised.exception.code, "file_pipeline_args_incomplete") + + def test_file_pipeline_exit_code_needs_an_accepted_ended_turn(self) -> None: + argv = [ + "--state-root", "/state", "--file-wake", "w.wav", + "--file-utterance", "u.wav", "--tts-out", "o.wav", + ] + cases = [ + ({"accepted": True, "state": "ended", "reply": "예"}, 0), + ({"accepted": True, "state": "error"}, 1), + ({"accepted": False, "state": "ended"}, 1), + ] + for report, expected in cases: + with self.subTest(report=report): + pipeline = mock.Mock(return_value=report) + code, printed = self._main(argv, run_file_pipeline=pipeline) + self.assertEqual(code, expected) + self.assertEqual(printed, json.dumps(report, ensure_ascii=False) + "\n") + pipeline.assert_called_once_with( + state_root=Path("/state"), + wake_wav=Path("w.wav"), + utterance_wav=Path("u.wav"), + tts_out=Path("o.wav"), + custom_wake_model=None, + ) + + +class WavWriterWithoutNumpyTests(unittest.TestCase): + def test_peak_normalised_pcm16_is_written_when_numpy_is_absent(self) -> None: + import wave + from array import array + + engine = types.SimpleNamespace( + get_supported_speakers=lambda: [], + generate_custom_voice=mock.Mock(return_value=([[0.0, [0.5, -2.0]]], 8000)), + ) + adapter = Qwen3TtsAdapter() + adapter._engine = engine + with TemporaryDirectory() as temp_dir: + root = Path(temp_dir) + output = root / "nested" / "reply.wav" + with mock.patch.dict(sys.modules, {"numpy": None}): + meta = adapter.write_wav("안녕하세요", output, AbortController(root).token()) + with wave.open(str(output), "rb") as handle: + written = ( + handle.getnchannels(), + handle.getsampwidth(), + handle.getframerate(), + handle.readframes(handle.getnframes()), + ) + size = output.stat().st_size + + self.assertEqual(written, (1, 2, 8000, array("h", [0, 8192, -32767]).tobytes())) + self.assertEqual( + meta, {"path": str(output), "bytes": size, "sample_rate": 8000, "nframes": 3} + ) + engine.generate_custom_voice.assert_called_once_with( + text="안녕하세요", speaker="ryan", language="Korean" + ) + + if __name__ == "__main__": unittest.main() From 2e39619c3567d91f3aeac1f3e5aefc1ccf66c5c2 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:33:37 +0900 Subject: [PATCH 26/67] refactor(context): split mod.rs into submodules by domain mod.rs becomes the module root: a module map, declarations and re-exports. Every item moves unchanged into the submodule that owns its body of knowledge, and common.rs dissolves into the owners of its constants: - store: the database file, its permissions, WAL and read-only open - schema: tables, the retrieval-index marker and all migrations - embedding: the message vector and its byte encoding - csv_import: exported chat CSV import - live: live local-DB ingest, per-source sync state, authoritative source - self_send: confirmed self-sends and the bot-sent style repair - style: owner and recipient style classification and profiles - topics: the topic lexicon and message topic rows - response_time: the fitted reply-delay distribution - reply_decision: decision records and their status order - search: keyword, vector, topic and hybrid retrieval - reply_bundle: the versioned reply bundle - redact: provenance hashing and path redaction Every public item keeps its openkakao_cli::context path through a re-export. Items used across submodules become pub(super), so nothing outside context gains access. With whitespace, use lines, visibility qualifiers and trailing commas ignored, all 200 items are token-identical to the pre-split source, and tests.rs gains only imports plus its one test-only helper. Co-Authored-By: Claude Opus 5.5 --- src/context/common.rs | 10 - src/context/csv_import.rs | 237 ++ src/context/embedding.rs | 86 + src/context/live.rs | 965 +++++ src/context/mod.rs | 6235 +-------------------------------- src/context/redact.rs | 58 + src/context/reply_bundle.rs | 508 +++ src/context/reply_decision.rs | 588 ++++ src/context/response_time.rs | 697 ++++ src/context/schema.rs | 694 ++++ src/context/search.rs | 628 ++++ src/context/self_send.rs | 692 ++++ src/context/store.rs | 118 + src/context/style.rs | 832 +++++ src/context/tests.rs | 40 + src/context/topics.rs | 306 ++ 16 files changed, 6522 insertions(+), 6172 deletions(-) delete mode 100644 src/context/common.rs create mode 100644 src/context/csv_import.rs create mode 100644 src/context/embedding.rs create mode 100644 src/context/live.rs create mode 100644 src/context/redact.rs create mode 100644 src/context/reply_bundle.rs create mode 100644 src/context/reply_decision.rs create mode 100644 src/context/response_time.rs create mode 100644 src/context/schema.rs create mode 100644 src/context/search.rs create mode 100644 src/context/self_send.rs create mode 100644 src/context/store.rs create mode 100644 src/context/style.rs create mode 100644 src/context/topics.rs diff --git a/src/context/common.rs b/src/context/common.rs deleted file mode 100644 index a83d48b6..00000000 --- a/src/context/common.rs +++ /dev/null @@ -1,10 +0,0 @@ -//! Shared context constants and encoding helpers. -//! Schema version strings stay here as `&str` and must not be normalized. - -pub(super) const VECTOR_DIM: usize = 128; -pub(super) const STYLE_USER: &str = "최연우"; -pub const STYLE_POLICY_VERSION: &str = "ordinary-conversation-v3"; -pub const CONTEXT_REPLY_BUNDLE_MAX_EXCLUDED_LOG_IDS: usize = 6; -pub(super) const RETRIEVAL_INDEX_SCHEMA_VERSION: &str = "2"; -pub(super) const CONTEXT_RETRIEVAL_MIGRATION_REQUIRED: &str = - "context retrieval index migration required; run context-index"; diff --git a/src/context/csv_import.rs b/src/context/csv_import.rs new file mode 100644 index 00000000..a934e065 --- /dev/null +++ b/src/context/csv_import.rs @@ -0,0 +1,237 @@ +//! Exported chat CSV import into the context index. + +use super::embedding::{encode_vector, vector_to_bytes}; +use super::response_time::{ + is_response_participant, parse_chat_date, summarize_response_delays, MAX_RESPONSE_DELAY_SECONDS, +}; +use super::schema::rebuild_retrieval_index; +use super::self_send::{ + confirmed_self_sends_from_queue, confirmed_self_sends_with_connection, mark_bot_sent_features, + match_confirmed_send, +}; +use super::store::{checkpoint_context_wal_truncate, open_db, set_context_db_permissions}; +use super::style::{ + classify_style_message, StyleProfileAccumulator, STYLE_POLICY_VERSION, STYLE_USER, +}; +use super::topics::insert_message_topics; +use anyhow::{Context, Result}; +use chrono::{NaiveDateTime, Utc}; +use csv::ReaderBuilder; +use rusqlite::params; +use std::path::Path; + +pub fn index_csv(db_path: &Path, chat: &str, input: &Path) -> Result { + index_csv_with_queue(db_path, chat, input, None) +} + +/// Import one chat history CSV, excluding replies the assistant itself sent. +/// +/// A CSV row carries no room log id, so the send authorities are matched by +/// exact text inside [`SELF_SEND_REPAIR_WINDOW_SECONDS`](super::SELF_SEND_REPAIR_WINDOW_SECONDS): the decision ledger +/// plus, when the caller can name it, the room delivery queue. Both are needed +/// because a reply that only the queue recorded as `sent` would otherwise be +/// learned as an owner sample on the next import. +pub fn index_csv_with_queue( + db_path: &Path, + chat: &str, + input: &Path, + queue_path: Option<&Path>, +) -> Result { + if chat.trim().is_empty() { + anyhow::bail!("chat name must not be empty"); + } + let source = input + .canonicalize() + .with_context(|| format!("failed to resolve CSV {}", input.display()))? + .display() + .to_string(); + let conn = open_db(db_path)?; + let mut reader = ReaderBuilder::new() + .flexible(true) + .from_path(input) + .with_context(|| format!("failed to open CSV {}", input.display()))?; + let headers = reader.headers().context("CSV has no header row")?.clone(); + let date_idx = headers + .iter() + .position(|h| h.trim_start_matches('\u{feff}') == "Date"); + let user_idx = headers.iter().position(|h| h == "User"); + let message_idx = headers.iter().position(|h| h == "Message"); + let (Some(date_idx), Some(user_idx), Some(message_idx)) = (date_idx, user_idx, message_idx) + else { + anyhow::bail!("CSV must contain Date, User, and Message columns"); + }; + let required = date_idx.max(user_idx).max(message_idx); + // A bot reply that was already confirmed must not be re-learned as an owner + // style sample when the chat history is imported from CSV. + let mut confirmed_sends = confirmed_self_sends_with_connection(&conn, chat).unwrap_or_default(); + if let Some(queue) = queue_path { + for send in confirmed_self_sends_from_queue(queue)? { + if !confirmed_sends + .iter() + .any(|existing| existing.event_id == send.event_id) + { + confirmed_sends.push(send); + } + } + } + let tx = conn.unchecked_transaction()?; + tx.execute("DELETE FROM context_messages WHERE source = ?1", [&source])?; + tx.execute("DELETE FROM owner_style WHERE source = ?1", [&source])?; + tx.execute( + "DELETE FROM owner_style_profile WHERE source = ?1", + [&source], + )?; + let mut previous_human_at: Option = None; + let mut response_delays = Vec::new(); + let mut count = 0; + let mut profile_accumulator = StyleProfileAccumulator::default(); + for (source_row, row) in reader.records().enumerate() { + let row = row.context("invalid CSV record")?; + if row.len() <= required { + anyhow::bail!("CSV record has fewer fields than its header"); + } + let message = row.get(message_idx).unwrap_or_default().trim().to_string(); + if message.is_empty() { + continue; + } + let date = row.get(date_idx).unwrap_or_default().to_string(); + let user = row.get(user_idx).unwrap_or_default().to_string(); + let row_sent_at = parse_chat_date(&date).map(|value| value.and_utc().timestamp()); + // Identify a bot-sent row *before* it can become style, pacing or + // recipient evidence. A confirmed send is not a human reply, so it must + // not produce a response-time sample either. + let bot_send = if user == STYLE_USER { + row_sent_at.and_then(|sent_at| { + let (matched, ambiguous) = + match_confirmed_send(&confirmed_sends, &message, sent_at); + matched + .filter(|_| !ambiguous) + .map(|send| (send, sent_at - send.sent_at)) + }) + } else { + None + }; + if let Some(current_at) = parse_chat_date(&date) { + if user == STYLE_USER { + if bot_send.is_some() { + // Drop the pending prompt instead of pairing a later human + // reply with it: this turn was the assistant, not the owner. + previous_human_at = None; + } else if let Some(previous_at) = previous_human_at.take() { + let delay = (current_at - previous_at).num_seconds(); + if (0..=MAX_RESPONSE_DELAY_SECONDS).contains(&delay) { + response_delays.push(delay as f64); + } + } + } else if is_response_participant(&user) { + previous_human_at = Some(current_at); + } + } + let vector = encode_vector(&format!("{} {}", user, message)); + // `tx.execute` re-parses this INSERT on every CSV row; the prepared + // statement cache keeps the parse cost to one per connection. + tx.prepare_cached("INSERT INTO context_messages(source, chat, date, user_name, message, vector) VALUES (?1, ?2, ?3, ?4, ?5, ?6)") + .and_then(|mut stmt| { + stmt.execute(params![source, chat, date, user, message, vector_to_bytes(&vector)]) + })?; + insert_message_topics(&tx, tx.last_insert_rowid(), chat, &source, &date, &message)?; + if user == STYLE_USER { + let mut features = classify_style_message(&message); + if let Some((send, offset)) = bot_send { + features.style_eligible = false; + features.content_kind = "bot_sent_reply"; + features.features_json = + mark_bot_sent_features(&features.features_json, &send.event_id, offset); + } + tx.prepare_cached( + "INSERT INTO owner_style(source, chat, date, user_name, message, vector, source_row, content_kind, style_eligible, policy_version, features_json) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", + ) + .and_then(|mut stmt| { + stmt.execute(params![ + source, + chat, + date, + user, + message, + vector_to_bytes(&encode_vector(&message)), + source_row as i64 + 1, + features.content_kind, + features.style_eligible as i64, + STYLE_POLICY_VERSION, + features.features_json, + ]) + })?; + if features.style_eligible { + profile_accumulator.add(&message, &features); + } + } + count += 1; + } + tx.execute( + "DELETE FROM response_time_stats WHERE chat = ?1 AND source = ?2 AND user_name = ?3", + params![chat, source, STYLE_USER], + )?; + if let Some(profile) = profile_accumulator.finish(chat, &source, STYLE_USER) { + tx.execute( + "INSERT INTO owner_style_profile( + chat, source, user_name, sample_count, average_character_length, + median_character_length, p90_character_length, casual_ending_count, + casual_ending_counts_json, question_count, emoji_count, punctuation_count, + common_endings_json, common_tokens_json, policy_version, created_at + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16)", + params![ + profile.chat, + profile.source, + profile.user, + profile.sample_count as i64, + profile.average_character_length, + profile.median_character_length, + profile.p90_character_length, + profile.casual_ending_count as i64, + profile.casual_ending_counts_json, + profile.question_count as i64, + profile.emoji_count as i64, + profile.punctuation_count as i64, + profile.common_endings_json, + profile.common_tokens_json, + profile.policy_version, + Utc::now().to_rfc3339(), + ], + )?; + } + if let Some(stats) = summarize_response_delays(chat, &source, STYLE_USER, response_delays) { + tx.execute( + "INSERT INTO response_time_stats(chat, source, user_name, sample_count, average_seconds, median_seconds, p90_seconds, min_seconds, max_seconds, max_window_seconds, stddev_seconds, distribution_schema_version, distribution_json) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)", + params![ + stats.chat, + stats.source, + stats.user, + stats.sample_count as i64, + stats.average_seconds, + stats.median_seconds, + stats.p90_seconds, + stats.min_seconds, + stats.max_seconds, + stats.max_window_seconds, + stats.stddev_seconds, + stats + .distribution + .as_ref() + .map(|distribution| distribution.schema_version as i64) + .unwrap_or(0), + stats + .distribution + .as_ref() + .map(serde_json::to_string) + .transpose()? + .unwrap_or_else(|| "{}".to_string()), + ], + )?; + } + tx.commit()?; + rebuild_retrieval_index(&conn)?; + checkpoint_context_wal_truncate(&conn)?; + set_context_db_permissions(db_path)?; + Ok(count) +} diff --git a/src/context/embedding.rs b/src/context/embedding.rs new file mode 100644 index 00000000..3d6c1962 --- /dev/null +++ b/src/context/embedding.rs @@ -0,0 +1,86 @@ +//! The fixed-width message vector: the pluggable `Embedder`, the default +//! local hash embedder, and the vector's byte encoding in SQLite. + +pub(super) const VECTOR_DIM: usize = 128; + +/// 임베딩 공급원 추상화(착탈식 임베딩 인터페이스). 검색이 사용하는 벡터 +/// 표현을 이 트레이트 뒤로 감춰, 기본 로컬 구현 외에 로컬 임베딩 모델이나 +/// ANN 인덱스를 붙일 수 있게 한다. 기본 구현은 외부 네트워크 없이 동작한다. +pub trait Embedder { + /// 임베딩 벡터의 차원 수. + fn dim(&self) -> usize; + /// 입력 문자열을 벡터로 인코딩한다. + fn embed(&self, text: &str) -> Vec; + /// 외부 네트워크 사용 없이 로컬에서만 동작하는지 여부. + /// `Dataset_Builder`는 반드시 `true`인 임베더만 허용한다(R3-12). + fn is_local_only(&self) -> bool; +} + +/// 기본 임베더. 기존 결정적 해시 벡터(`encode_vector`)를 그대로 감싼다. +/// 외부 없이 동작하며 재현 가능한(결정적) 벡터를 만든다. 이 구현을 기본으로 +/// 주입하면 검색 동작이 리팩터링 이전과 완전히 동일하게 유지된다. +#[derive(Debug, Clone, Copy, Default)] +pub struct LocalHashEmbedder; + +impl LocalHashEmbedder { + pub fn new() -> Self { + LocalHashEmbedder + } +} + +impl Embedder for LocalHashEmbedder { + fn dim(&self) -> usize { + VECTOR_DIM + } + + fn embed(&self, text: &str) -> Vec { + encode_vector(text) + } + + fn is_local_only(&self) -> bool { + true + } +} + +pub(super) fn encode_vector(text: &str) -> Vec { + let mut vector = vec![0.0; VECTOR_DIM]; + for token in text + .split(|c: char| !c.is_alphanumeric()) + .filter(|t| !t.is_empty()) + .map(str::to_lowercase) + { + for window in token + .as_bytes() + .windows(3) + .chain(token.as_bytes().windows(2)) + { + let mut hash = 2166136261u32; + for byte in window { + hash = (hash ^ u32::from(*byte)).wrapping_mul(16777619); + } + vector[(hash as usize) % VECTOR_DIM] += if hash & 1 == 0 { 1.0 } else { -1.0 }; + } + } + let norm = vector.iter().map(|v| v * v).sum::().sqrt(); + if norm > 0.0 { + for value in &mut vector { + *value /= norm; + } + } + vector +} + +pub(super) fn cosine(a: &[f32], b: &[f32]) -> f32 { + a.iter().zip(b).map(|(l, r)| l * r).sum() +} + +pub(super) fn vector_to_bytes(vector: &[f32]) -> Vec { + vector.iter().flat_map(|v| v.to_le_bytes()).collect() +} + +pub(super) fn bytes_to_vector(bytes: &[u8]) -> Vec { + bytes + .chunks_exact(4) + .map(|c| f32::from_le_bytes(c.try_into().unwrap())) + .collect() +} diff --git a/src/context/live.rs b/src/context/live.rs new file mode 100644 index 00000000..d3cb5029 --- /dev/null +++ b/src/context/live.rs @@ -0,0 +1,965 @@ +//! Live local-DB sync: validated event batches, per-source checkpoint and +//! burst state (`LiveSourceRow`), the summaries refreshed after ingest, and +//! which source is authoritative for a chat. + +use super::embedding::{encode_vector, vector_to_bytes}; +use super::response_time::{ + insert_response_time_stats, is_response_participant, summarize_response_delays, + MAX_RESPONSE_DELAY_SECONDS, +}; +use super::schema::LIVE_CONTEXT_SCHEMA_VERSION; +use super::store::{open_db, open_db_readonly}; +use super::style::{ + classify_style_message, insert_style_profile, StyleProfileAccumulator, STYLE_POLICY_VERSION, + STYLE_USER, +}; +use super::topics::{insert_message_topics, message_has_interest_topic}; +use anyhow::{Context, Result}; +use chrono::Utc; +use rusqlite::{params, Connection, OptionalExtension}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::BTreeMap; +use std::path::Path; + +pub(super) const LIVE_CONTEXT_BATCH_MAX_ROWS: usize = 200; + +pub(super) const LIVE_CONTEXT_MAX_FIELD_BYTES: usize = 256 * 1024; + +const LIVE_CONTEXT_MAX_PENDING_RECIPIENTS: usize = 64; + +const LIVE_CONTEXT_SOURCE_PREFIX: &str = "local-db"; + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct LiveContextEvent { + pub chat_id: i64, + pub log_id: i64, + pub sender_name: String, + pub message: String, + pub sent_at: i64, + #[serde(default)] + pub is_self: bool, + #[serde(default)] + pub exclude_from_learning: bool, + #[serde(default)] + pub auto_generated: bool, + #[serde(default)] + pub attachment: String, + #[serde(default)] + pub message_type: i32, + #[serde(default)] + pub interest_only: bool, +} + +#[derive(Debug, Clone, Serialize, PartialEq, Eq)] +pub struct LiveContextSyncState { + pub source: String, + pub chat_id: i64, + pub chat: String, + pub checkpoint_log_id: i64, + pub authoritative: bool, + pub summary_dirty: bool, + pub sync_status: String, +} + +impl LiveContextSyncState { + /// Startup and `--check` may proceed while summaries are dirty. + /// + /// `summary_dirty` means a pending burst still needs a summary refresh. The + /// reply path already runs `context-sync-local` before grounded drafts, so + /// blocking the whole worker on that flag leaves authorized inbound stuck + /// behind a fence the worker itself is supposed to clear. + pub fn allows_auto_reply_startup(&self, chat_id: i64, chat_name: &str) -> bool { + // 로컬 DB에 이름이 없는 그룹방은 '그룹:' 자리표시자로 저장된다. 화면 + // 이름으로 시작을 요청해도 같은 방으로 보고 통과시킨다. + let placeholder = format!("그룹:{chat_id}"); + let name_matches = + self.chat == chat_name || self.chat == placeholder || chat_name.trim() == placeholder; + self.chat_id == chat_id + && name_matches + && self.authoritative + && matches!(self.sync_status.as_str(), "ready" | "partial") + } +} + +#[derive(Debug, Clone, Serialize, PartialEq, Eq)] +pub struct LiveContextIngestResult { + pub source: String, + pub chat_id: i64, + pub chat: String, + pub checkpoint_log_id: i64, + pub inserted_events: usize, + pub duplicate_events: usize, + pub indexed_messages: usize, + pub style_messages: usize, + pub response_samples: usize, + pub recipient_style_samples: usize, + pub summary_refreshed: bool, + pub authoritative: bool, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +struct PendingRecipient { + recipient: String, + log_id: i64, +} + +#[derive(Debug, Default)] +pub(super) struct RecipientStyleAccumulator { + pub(super) style: StyleProfileAccumulator, + pub(super) sample_count: usize, + pub(super) confidence_sum: f64, +} + +fn push_unique_fragment(parts: &mut Vec, value: &str) { + let trimmed = value.trim(); + if trimmed.is_empty() { + return; + } + if parts.iter().any(|part| part == trimmed) { + return; + } + parts.push(trimmed.to_string()); +} + +fn collect_attachment_fragments(value: &serde_json::Value, parts: &mut Vec) { + match value { + serde_json::Value::String(text) => { + let trimmed = text.trim(); + if trimmed.starts_with("http://") || trimmed.starts_with("https://") { + push_unique_fragment(parts, trimmed); + } + } + serde_json::Value::Array(items) => { + for item in items { + collect_attachment_fragments(item, parts); + } + } + serde_json::Value::Object(map) => { + for (key, child) in map { + let key_lower = key.to_ascii_lowercase(); + if matches!( + key_lower.as_str(), + "urls" + | "url" + | "src_message" + | "name" + | "filename" + | "title" + | "t" + | "d" + | "description" + | "text" + | "message" + | "content" + ) { + match child { + serde_json::Value::String(text) => push_unique_fragment(parts, text), + serde_json::Value::Array(items) => { + for item in items { + if let Some(text) = item.as_str() { + push_unique_fragment(parts, text); + } else { + collect_attachment_fragments(item, parts); + } + } + } + _ => collect_attachment_fragments(child, parts), + } + } else { + collect_attachment_fragments(child, parts); + } + } + } + _ => {} + } +} + +pub(super) fn live_index_text(message: &str, attachment: &str, message_type: i32) -> String { + let mut parts = Vec::new(); + push_unique_fragment(&mut parts, message); + match message_type { + 2 => push_unique_fragment(&mut parts, "사진"), + 3 => push_unique_fragment(&mut parts, "동영상"), + 18 | 16 => push_unique_fragment(&mut parts, "파일"), + 27 => push_unique_fragment(&mut parts, "이모티콘"), + 71 => push_unique_fragment(&mut parts, "샵검색"), + _ => {} + } + let trimmed_attachment = attachment.trim(); + if !trimmed_attachment.is_empty() { + if let Ok(value) = serde_json::from_str::(trimmed_attachment) { + collect_attachment_fragments(&value, &mut parts); + } + } + let mut text = parts.join("\n"); + if text.len() > LIVE_CONTEXT_MAX_FIELD_BYTES { + text.truncate(LIVE_CONTEXT_MAX_FIELD_BYTES); + } + text +} + +#[derive(Debug)] +struct LiveSourceRow { + account_fingerprint: String, + chat_id: i64, + chat: String, + authoritative: bool, + checkpoint_log_id: i64, + pending_human_log_id: Option, + pending_human_sent_at: Option, + pending_human_name: Option, + pending_burst: Vec, + summary_dirty: bool, +} + +pub fn live_context_source_id(account_fingerprint: &str, chat_id: i64) -> Result { + let fingerprint = account_fingerprint.trim().to_ascii_lowercase(); + if fingerprint.len() != 64 || !fingerprint.bytes().all(|byte| byte.is_ascii_hexdigit()) { + anyhow::bail!("live context account fingerprint must be 64 hexadecimal characters"); + } + if chat_id <= 0 { + anyhow::bail!("live context chat ID must be positive"); + } + Ok(format!( + "{LIVE_CONTEXT_SOURCE_PREFIX}:{fingerprint}:{chat_id}" + )) +} + +pub(super) fn live_event_digest(event: &LiveContextEvent) -> Result { + let payload = serde_json::to_vec(&serde_json::json!({ + "chat_id": event.chat_id, + "log_id": event.log_id, + "sender_name": event.sender_name, + "message": event.message, + "sent_at": event.sent_at, + "is_self": event.is_self, + "exclude_from_learning": event.exclude_from_learning, + "auto_generated": event.auto_generated, + }))?; + Ok(hex::encode(Sha256::digest(payload))) +} + +pub(super) fn live_event_date(sent_at: i64) -> Result { + let date = chrono::DateTime::::from_timestamp(sent_at, 0) + .context("live context sent_at is outside the supported timestamp range")?; + Ok(date.format("%Y-%m-%d %H:%M:%S").to_string()) +} + +fn validate_live_context_batch( + chat_id: i64, + chat: &str, + expected_checkpoint: i64, + events: &[LiveContextEvent], + batch_complete: bool, + promote_authoritative: bool, +) -> Result<()> { + if chat_id <= 0 || expected_checkpoint < 0 { + anyhow::bail!("live context chat ID and checkpoint are invalid"); + } + if chat.trim().is_empty() || chat.len() > LIVE_CONTEXT_MAX_FIELD_BYTES { + anyhow::bail!("live context chat name is invalid"); + } + if events.len() > LIVE_CONTEXT_BATCH_MAX_ROWS { + anyhow::bail!("live context batch exceeds {LIVE_CONTEXT_BATCH_MAX_ROWS} messages"); + } + if promote_authoritative && !batch_complete { + anyhow::bail!("live context source can only be promoted after a complete batch"); + } + let mut previous_log_id = expected_checkpoint; + for event in events { + if event.chat_id != chat_id + || event.log_id <= expected_checkpoint + || event.log_id <= previous_log_id + || ((event.auto_generated || event.exclude_from_learning) && !event.is_self) + || event.sender_name.len() > LIVE_CONTEXT_MAX_FIELD_BYTES + || event.message.len() > LIVE_CONTEXT_MAX_FIELD_BYTES + { + anyhow::bail!("live context batch contains an invalid or out-of-order event"); + } + live_event_date(event.sent_at)?; + previous_log_id = event.log_id; + } + Ok(()) +} + +fn load_live_source(conn: &Connection, source: &str) -> Result> { + conn.query_row( + "SELECT account_fingerprint, chat_id, chat, authoritative, + checkpoint_log_id, pending_human_log_id, pending_human_sent_at, + pending_human_name, pending_burst_json, summary_dirty + FROM context_sources WHERE source = ?1", + [source], + |row| { + let pending_burst_json: String = row.get(8)?; + Ok(( + row.get::<_, String>(0)?, + row.get::<_, i64>(1)?, + row.get::<_, String>(2)?, + row.get::<_, i64>(3)?, + row.get::<_, i64>(4)?, + row.get::<_, Option>(5)?, + row.get::<_, Option>(6)?, + row.get::<_, Option>(7)?, + pending_burst_json, + row.get::<_, i64>(9)?, + )) + }, + ) + .optional()? + .map( + |( + account_fingerprint, + chat_id, + chat, + authoritative, + checkpoint_log_id, + pending_human_log_id, + pending_human_sent_at, + pending_human_name, + pending_burst_json, + summary_dirty, + )| { + let pending_burst: Vec = serde_json::from_str(&pending_burst_json) + .context("live context pending recipient burst is malformed")?; + let burst_is_invalid = pending_burst.len() > LIVE_CONTEXT_MAX_PENDING_RECIPIENTS + || pending_burst.iter().any(|item| { + item.recipient.trim().is_empty() + || item.log_id <= 0 + || item.log_id > checkpoint_log_id + }) + || pending_burst + .windows(2) + .any(|items| items[0].log_id >= items[1].log_id); + let pending_is_invalid = match ( + pending_human_log_id, + pending_human_sent_at, + pending_human_name.as_deref(), + ) { + (None, None, None) => !pending_burst.is_empty(), + (Some(log_id), Some(sent_at), Some(name)) => { + log_id <= 0 + || log_id > checkpoint_log_id + || name.trim().is_empty() + || live_event_date(sent_at).is_err() + || pending_burst + .last() + .is_none_or(|item| item.log_id != log_id || item.recipient != name) + } + _ => true, + }; + if burst_is_invalid || pending_is_invalid { + anyhow::bail!("live context pending recipient burst is invalid"); + } + Ok(LiveSourceRow { + account_fingerprint, + chat_id, + chat, + authoritative: authoritative != 0, + checkpoint_log_id, + pending_human_log_id, + pending_human_sent_at, + pending_human_name, + pending_burst, + summary_dirty: summary_dirty != 0, + }) + }, + ) + .transpose() +} + +fn clear_pending_participant_burst(source: &mut LiveSourceRow) { + source.pending_human_log_id = None; + source.pending_human_sent_at = None; + source.pending_human_name = None; + source.pending_burst.clear(); +} + +fn recipient_confidences(burst: &[PendingRecipient]) -> Vec<(String, f64)> { + let mut latest_by_recipient = BTreeMap::new(); + for item in burst { + latest_by_recipient.insert(item.recipient.clone(), item.log_id); + } + let mut recipients = latest_by_recipient.into_iter().collect::>(); + recipients.sort_by(|(left_name, left_log), (right_name, right_log)| { + right_log + .cmp(left_log) + .then_with(|| left_name.cmp(right_name)) + }); + let total_weight = recipients + .iter() + .enumerate() + .map(|(rank, _)| 1.0 / (rank as f64 + 1.0)) + .sum::(); + recipients + .into_iter() + .enumerate() + .map(|(rank, (recipient, _))| (recipient, (1.0 / (rank as f64 + 1.0)) / total_weight)) + .collect() +} + +pub(super) fn refresh_live_context_summaries( + conn: &Connection, + source: &str, + chat_id: i64, + chat: &str, +) -> Result<()> { + let now = Utc::now().to_rfc3339(); + let style_messages = { + let mut stmt = conn.prepare( + "SELECT message FROM owner_style + WHERE source = ?1 AND chat = ?2 AND user_name = ?3 + AND style_eligible = 1 AND policy_version = ?4 + ORDER BY source_row ASC, id ASC", + )?; + let rows = stmt.query_map( + params![source, chat, STYLE_USER, STYLE_POLICY_VERSION], + |row| row.get::<_, String>(0), + )?; + rows.collect::>>()? + }; + let mut style_accumulator = StyleProfileAccumulator::default(); + for message in &style_messages { + let features = classify_style_message(message); + if features.style_eligible { + style_accumulator.add(message, &features); + } + } + conn.execute( + "DELETE FROM owner_style_profile + WHERE chat = ?1 AND source = ?2 AND user_name = ?3", + params![chat, source, STYLE_USER], + )?; + if let Some(profile) = style_accumulator.finish(chat, source, STYLE_USER) { + insert_style_profile(conn, &profile, &now)?; + } + + let response_delays = { + let mut stmt = conn.prepare( + "SELECT delay_seconds FROM response_time_samples + WHERE source = ?1 AND chat_id = ?2 ORDER BY reply_log_id ASC", + )?; + let rows = stmt.query_map(params![source, chat_id], |row| row.get::<_, f64>(0))?; + rows.collect::>>()? + }; + conn.execute( + "DELETE FROM response_time_stats + WHERE chat = ?1 AND source = ?2 AND user_name = ?3", + params![chat, source, STYLE_USER], + )?; + if let Some(stats) = summarize_response_delays(chat, source, STYLE_USER, response_delays) { + insert_response_time_stats(conn, &stats)?; + } + + let recipient_rows = { + let mut stmt = conn.prepare( + "SELECT samples.recipient, samples.confidence, styles.message + FROM owner_recipient_style_samples samples + JOIN owner_style styles ON styles.id = samples.style_message_id + WHERE samples.source = ?1 AND samples.chat_id = ?2 + AND styles.style_eligible = 1 AND styles.policy_version = ?3 + ORDER BY samples.recipient ASC, samples.reply_log_id ASC", + )?; + let rows = stmt.query_map(params![source, chat_id, STYLE_POLICY_VERSION], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, f64>(1)?, + row.get::<_, String>(2)?, + )) + })?; + rows.collect::>>()? + }; + let mut recipient_accumulators: BTreeMap = BTreeMap::new(); + for (recipient, confidence, message) in recipient_rows { + if !confidence.is_finite() || !(0.0..=1.0).contains(&confidence) || confidence == 0.0 { + anyhow::bail!("recipient style confidence is malformed"); + } + let features = classify_style_message(&message); + if !features.style_eligible { + continue; + } + let accumulator = recipient_accumulators.entry(recipient).or_default(); + accumulator.style.add(&message, &features); + accumulator.sample_count += 1; + accumulator.confidence_sum += confidence; + } + conn.execute( + "DELETE FROM owner_recipient_style_profile + WHERE source = ?1 AND chat_id = ?2", + params![source, chat_id], + )?; + for (recipient, accumulator) in recipient_accumulators { + let Some(profile) = accumulator.style.finish(chat, source, STYLE_USER) else { + continue; + }; + conn.execute( + "INSERT INTO owner_recipient_style_profile( + source, chat_id, chat, recipient, user_name, sample_count, + confidence_sum, average_character_length, median_character_length, + p90_character_length, casual_ending_count, casual_ending_counts_json, + question_count, emoji_count, punctuation_count, common_endings_json, + common_tokens_json, policy_version, updated_at + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, + ?13, ?14, ?15, ?16, ?17, ?18, ?19)", + params![ + source, + chat_id, + chat, + recipient, + STYLE_USER, + accumulator.sample_count as i64, + accumulator.confidence_sum, + profile.average_character_length, + profile.median_character_length, + profile.p90_character_length, + profile.casual_ending_count as i64, + profile.casual_ending_counts_json, + profile.question_count as i64, + profile.emoji_count as i64, + profile.punctuation_count as i64, + profile.common_endings_json, + profile.common_tokens_json, + STYLE_POLICY_VERSION, + now, + ], + )?; + } + Ok(()) +} + +fn live_source_has_required_summaries(conn: &Connection, source: &str, chat: &str) -> Result { + let style_samples = conn + .query_row( + "SELECT sample_count FROM owner_style_profile + WHERE chat = ?1 AND source = ?2 AND user_name = ?3 + AND policy_version = ?4", + params![chat, source, STYLE_USER, STYLE_POLICY_VERSION], + |row| row.get::<_, i64>(0), + ) + .optional()? + .unwrap_or(0); + let timing_samples = conn + .query_row( + "SELECT sample_count FROM response_time_stats + WHERE chat = ?1 AND source = ?2 AND user_name = ?3", + params![chat, source, STYLE_USER], + |row| row.get::<_, i64>(0), + ) + .optional()? + .unwrap_or(0); + Ok(style_samples > 0 && timing_samples >= 2) +} + +/// Atomically ingest one bounded `local-poll` page into the live context index. +/// +/// `expected_checkpoint` must be the `after_log_id` used for the source poll. +/// Replaying a committed page is idempotent when every event digest matches. +/// `batch_complete` should only be true for a snapshot-complete page; expensive +/// style/timing summaries are refreshed at that boundary. Promotion makes the +/// complete live source take precedence over legacy CSV sources for the chat. +#[allow(clippy::too_many_arguments)] +pub fn ingest_live_context_events( + db_path: &Path, + account_fingerprint: &str, + chat_id: i64, + chat: &str, + expected_checkpoint: i64, + events: &[LiveContextEvent], + batch_complete: bool, + promote_authoritative: bool, +) -> Result { + validate_live_context_batch( + chat_id, + chat, + expected_checkpoint, + events, + batch_complete, + promote_authoritative, + )?; + let fingerprint = account_fingerprint.trim().to_ascii_lowercase(); + let source_id = live_context_source_id(&fingerprint, chat_id)?; + let mut conn = open_db(db_path)?; + let tx = conn.transaction()?; + let now = Utc::now().to_rfc3339(); + let mut source = match load_live_source(&tx, &source_id)? { + Some(source) => source, + None => { + tx.execute( + "INSERT INTO context_sources( + source, kind, account_fingerprint, chat_id, chat, authoritative, + checkpoint_log_id, pending_burst_json, summary_dirty, sync_status, + updated_at + ) VALUES (?1, 'local_db', ?2, ?3, ?4, 0, ?5, '[]', 0, 'ready', ?6)", + params![ + source_id, + fingerprint, + chat_id, + chat, + expected_checkpoint, + now, + ], + )?; + LiveSourceRow { + account_fingerprint: fingerprint.clone(), + chat_id, + chat: chat.to_string(), + authoritative: false, + checkpoint_log_id: expected_checkpoint, + pending_human_log_id: None, + pending_human_sent_at: None, + pending_human_name: None, + pending_burst: Vec::new(), + summary_dirty: false, + } + } + }; + // 이름 없는 그룹방은 '그룹:' 자리표시자로 저장된다. 화면 이름으로 동기화해도 + // 같은 방이므로 통과시킨다. + let placeholder = format!("그룹:{chat_id}"); + let name_matches = source.chat == chat || source.chat == placeholder; + if source.account_fingerprint != fingerprint || source.chat_id != chat_id || !name_matches { + anyhow::bail!("live context source identity changed"); + } + if source.checkpoint_log_id < expected_checkpoint { + anyhow::bail!("live context checkpoint is behind the caller checkpoint"); + } + + let mut inserted_events = 0usize; + let mut duplicate_events = 0usize; + let mut indexed_messages = 0usize; + let mut style_messages = 0usize; + let mut response_samples = 0usize; + let mut recipient_style_samples = 0usize; + let mut summaries_changed = source.summary_dirty; + + for event in events { + let digest = live_event_digest(event)?; + let existing_digest = tx + .query_row( + "SELECT message_digest FROM context_live_events + WHERE source = ?1 AND chat_id = ?2 AND log_id = ?3", + params![source_id, chat_id, event.log_id], + |row| row.get::<_, String>(0), + ) + .optional()?; + if let Some(existing_digest) = existing_digest { + if existing_digest != digest { + anyhow::bail!("live context replay digest mismatch"); + } + if event.log_id > source.checkpoint_log_id { + anyhow::bail!("live context event exists beyond its checkpoint"); + } + duplicate_events += 1; + continue; + } + if event.log_id <= source.checkpoint_log_id { + anyhow::bail!("live context checkpoint skipped an unrecorded event"); + } + + let date = live_event_date(event.sent_at)?; + let sender = event.sender_name.trim(); + let message = event.message.trim(); + let mut context_message_id = None; + let mut style_message_id = None; + let disposition; + + if event.auto_generated || event.exclude_from_learning { + disposition = if event.auto_generated { + "auto_generated" + } else { + "ambiguous_auto_candidate" + }; + clear_pending_participant_burst(&mut source); + } else if sender.is_empty() { + disposition = "unknown_author"; + clear_pending_participant_burst(&mut source); + } else { + let index_text = live_index_text(message, &event.attachment, event.message_type); + let should_index = !index_text.is_empty() + && (!event.interest_only || message_has_interest_topic(&index_text)); + if should_index { + tx.execute( + "INSERT INTO context_messages( + source, chat, date, user_name, message, vector + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6)", + params![ + source_id, + chat, + date, + sender, + index_text, + vector_to_bytes(&encode_vector(&format!("{sender} {index_text}"))), + ], + )?; + context_message_id = Some(tx.last_insert_rowid()); + insert_message_topics( + &tx, + context_message_id.unwrap_or(0), + chat, + &source_id, + &date, + &index_text, + )?; + indexed_messages += 1; + } + + if event.is_self { + disposition = if !should_index { + "filtered_noise" + } else if message.is_empty() { + "empty" + } else { + "style" + }; + if should_index && !message.is_empty() { + let features = classify_style_message(message); + tx.execute( + "INSERT INTO owner_style( + source, chat, date, user_name, message, vector, source_row, + content_kind, style_eligible, policy_version, features_json + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", + params![ + source_id, + chat, + date, + STYLE_USER, + message, + vector_to_bytes(&encode_vector(message)), + event.log_id, + features.content_kind, + features.style_eligible as i64, + STYLE_POLICY_VERSION, + features.features_json, + ], + )?; + style_message_id = Some(tx.last_insert_rowid()); + style_messages += 1; + summaries_changed = true; + + if let (Some(prompt_log_id), Some(prompt_sent_at), Some(recipient)) = ( + source.pending_human_log_id, + source.pending_human_sent_at, + source.pending_human_name.as_deref(), + ) { + let delay = event.sent_at - prompt_sent_at; + if (0..=MAX_RESPONSE_DELAY_SECONDS).contains(&delay) { + response_samples += tx.execute( + "INSERT OR IGNORE INTO response_time_samples( + source, chat_id, reply_log_id, prompt_log_id, + recipient, delay_seconds + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6)", + params![ + source_id, + chat_id, + event.log_id, + prompt_log_id, + recipient, + delay as f64, + ], + )?; + } + } + if features.style_eligible { + let confidences = recipient_confidences(&source.pending_burst); + let burst_size = confidences.len(); + for (recipient, confidence) in confidences { + recipient_style_samples += tx.execute( + "INSERT OR IGNORE INTO owner_recipient_style_samples( + source, chat_id, reply_log_id, recipient, style_message_id, + burst_size, confidence + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", + params![ + source_id, + chat_id, + event.log_id, + recipient, + style_message_id, + burst_size as i64, + confidence, + ], + )?; + } + } + clear_pending_participant_burst(&mut source); + } + } else if is_response_participant(sender) { + disposition = if message.is_empty() { + "empty" + } else { + "context" + }; + if !message.is_empty() { + source.pending_human_log_id = Some(event.log_id); + source.pending_human_sent_at = Some(event.sent_at); + source.pending_human_name = Some(sender.to_string()); + source.pending_burst.push(PendingRecipient { + recipient: sender.to_string(), + log_id: event.log_id, + }); + if source.pending_burst.len() > LIVE_CONTEXT_MAX_PENDING_RECIPIENTS { + let remove = + source.pending_burst.len() - LIVE_CONTEXT_MAX_PENDING_RECIPIENTS; + source.pending_burst.drain(0..remove); + } + } + } else { + disposition = if message.is_empty() { + "empty" + } else { + "context" + }; + clear_pending_participant_burst(&mut source); + } + } + + tx.execute( + "INSERT INTO context_live_events( + source, chat_id, log_id, sent_at, sender_name, message_digest, + disposition, auto_generated, context_message_id, style_message_id, + created_at + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", + params![ + source_id, + chat_id, + event.log_id, + event.sent_at, + sender, + digest, + disposition, + event.auto_generated as i64, + context_message_id, + style_message_id, + now, + ], + )?; + source.checkpoint_log_id = event.log_id; + inserted_events += 1; + } + + let summary_refreshed = batch_complete && summaries_changed; + if summary_refreshed { + refresh_live_context_summaries(&tx, &source_id, chat_id, chat)?; + source.summary_dirty = false; + } else { + source.summary_dirty = summaries_changed; + } + if promote_authoritative { + if !live_source_has_required_summaries(&tx, &source_id, chat)? { + anyhow::bail!("live context source lacks required style or timing samples"); + } + tx.execute( + "UPDATE context_sources SET authoritative = 0 + WHERE chat = ?1 AND source <> ?2", + params![chat, source_id], + )?; + source.authoritative = true; + } + tx.execute( + "UPDATE context_sources + SET authoritative = ?2, checkpoint_log_id = ?3, + pending_human_log_id = ?4, pending_human_sent_at = ?5, + pending_human_name = ?6, pending_burst_json = ?7, + summary_dirty = ?8, sync_status = ?9, updated_at = ?10 + WHERE source = ?1", + params![ + source_id, + source.authoritative as i64, + source.checkpoint_log_id, + source.pending_human_log_id, + source.pending_human_sent_at, + source.pending_human_name, + serde_json::to_string(&source.pending_burst)?, + source.summary_dirty as i64, + if batch_complete { "ready" } else { "partial" }, + now, + ], + )?; + tx.commit()?; + Ok(LiveContextIngestResult { + source: source_id, + chat_id, + chat: chat.to_string(), + checkpoint_log_id: source.checkpoint_log_id, + inserted_events, + duplicate_events, + indexed_messages, + style_messages, + response_samples, + recipient_style_samples, + summary_refreshed, + authoritative: source.authoritative, + }) +} + +pub fn live_context_sync_state( + db_path: &Path, + account_fingerprint: &str, + chat_id: i64, +) -> Result> { + let source = live_context_source_id(account_fingerprint, chat_id)?; + let conn = open_db_readonly(db_path)?; + let has_schema = conn + .query_row( + "SELECT value FROM context_retrieval_meta WHERE key = 'live_context_schema'", + [], + |row| row.get::<_, String>(0), + ) + .optional()? + .as_deref() + == Some(LIVE_CONTEXT_SCHEMA_VERSION); + if !has_schema { + anyhow::bail!("live context index migration required"); + } + conn.query_row( + "SELECT source, chat_id, chat, checkpoint_log_id, authoritative, + summary_dirty, sync_status + FROM context_sources WHERE source = ?1", + [source], + |row| { + Ok(LiveContextSyncState { + source: row.get(0)?, + chat_id: row.get(1)?, + chat: row.get(2)?, + checkpoint_log_id: row.get(3)?, + authoritative: row.get::<_, i64>(4)? != 0, + summary_dirty: row.get::<_, i64>(5)? != 0, + sync_status: row.get(6)?, + }) + }, + ) + .optional() + .map_err(Into::into) +} + +pub(super) fn preferred_context_source( + conn: &Connection, + chat: Option<&str>, + source: Option<&str>, +) -> Result> { + if let Some(source) = source { + return Ok(Some(source.to_string())); + } + let Some(chat) = chat else { + return Ok(None); + }; + let has_sources_table = conn.query_row( + "SELECT EXISTS( + SELECT 1 FROM sqlite_master + WHERE type = 'table' AND name = 'context_sources' + )", + [], + |row| row.get::<_, i64>(0), + )? != 0; + if !has_sources_table { + return Ok(None); + } + conn.query_row( + "SELECT source FROM context_sources + WHERE chat = ?1 AND authoritative = 1 + ORDER BY updated_at DESC, source ASC LIMIT 1", + [chat], + |row| row.get::<_, String>(0), + ) + .optional() + .map_err(Into::into) +} diff --git a/src/context/mod.rs b/src/context/mod.rs index a2125624..2c648b9b 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1,6164 +1,75 @@ -use anyhow::{Context, Result}; -use chrono::{NaiveDateTime, Utc}; -use csv::ReaderBuilder; -use rusqlite::{params, Connection, OpenFlags, OptionalExtension}; -use serde::{Deserialize, Serialize}; -use sha2::{Digest, Sha256}; -use std::cmp::Ordering; -use std::collections::{BTreeMap, BTreeSet}; -use std::fs; -use std::path::{Path, PathBuf}; -use std::time::Duration; - -mod common; -pub use common::{CONTEXT_REPLY_BUNDLE_MAX_EXCLUDED_LOG_IDS, STYLE_POLICY_VERSION}; -use common::{ - CONTEXT_RETRIEVAL_MIGRATION_REQUIRED, RETRIEVAL_INDEX_SCHEMA_VERSION, STYLE_USER, VECTOR_DIM, -}; - -const MAX_RESPONSE_DELAY_SECONDS: i64 = 24 * 60 * 60; -const RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION: u32 = 2; -const RESPONSE_TIME_DISTRIBUTION_POLICY_VERSION: &str = "empirical-log1p-three-means-p90-v1"; -const RESPONSE_TIME_DISTRIBUTION_MODEL_KIND: &str = "bounded-normal-mixture"; -const RESPONSE_TIME_DISTRIBUTION_FIT_TRANSFORM: &str = "log1p"; -const RESPONSE_TIME_DISTRIBUTION_COMPONENTS: usize = 3; -const RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES: usize = 32; -const RESPONSE_TIME_DISTRIBUTION_MIN_COMPONENT_SAMPLES: usize = 8; -const MIN_SCHEDULED_RESPONSE_DELAY_SECONDS: f64 = 5.0; -const CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION: u32 = 2; -const RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION: u32 = 3; -const CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT: usize = 8; -const CONTEXT_REPLY_BUNDLE_STYLE_LIMIT: usize = 12; -const CONTEXT_REPLY_BUNDLE_DECISION_LIMIT: usize = 6; -const CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES: usize = 64 * 1024; -const CONTEXT_KEYWORD_CANDIDATE_CAP: usize = 256; -const CONTEXT_VECTOR_CANDIDATE_CAP: usize = 256; -const CONTEXT_TOPIC_CANDIDATE_CAP: usize = 64; -const STYLE_VECTOR_CANDIDATE_CAP: usize = 256; -const REPLY_DECISION_CANDIDATE_CAP: usize = 128; -const MAX_REPLY_EVIDENCE_IDS: usize = 64; -const MAX_REPLY_EVIDENCE_ID_BYTES: usize = 256; -const MAX_CONTEXT_RETRIEVAL_SCORE: f32 = 2.0; -const LIVE_CONTEXT_SCHEMA_VERSION: &str = "2"; -const LIVE_CONTEXT_BATCH_MAX_ROWS: usize = 200; -const LIVE_CONTEXT_MAX_FIELD_BYTES: usize = 256 * 1024; -const LIVE_CONTEXT_MAX_PENDING_RECIPIENTS: usize = 64; -const LIVE_CONTEXT_SOURCE_PREFIX: &str = "local-db"; -const RECIPIENT_STYLE_MIN_SAMPLES: usize = 3; -const RECIPIENT_STYLE_MIN_CONFIDENCE: f64 = 2.0; -const AUTO_GENERATED_MATCH_WINDOW_SECONDS: i64 = 120; -const AUTO_GENERATED_MATCH_MAX_CANDIDATES_PER_TEXT: usize = 16; -const RECIPIENT_STYLE_SEARCH_SQL: &str = - "SELECT styles.id, styles.chat, styles.source, styles.date, - styles.user_name, styles.message, styles.vector, samples.confidence - FROM owner_recipient_style_samples samples - JOIN owner_style styles ON styles.id = samples.style_message_id - WHERE samples.source = ?1 - AND samples.recipient = ?2 - AND styles.chat = ?3 - AND styles.source = samples.source - AND styles.user_name = ?4 - AND styles.style_eligible = 1 - AND styles.policy_version = ?5 - ORDER BY samples.reply_log_id DESC, samples.style_message_id DESC - LIMIT ?6"; - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct LiveContextEvent { - pub chat_id: i64, - pub log_id: i64, - pub sender_name: String, - pub message: String, - pub sent_at: i64, - #[serde(default)] - pub is_self: bool, - #[serde(default)] - pub exclude_from_learning: bool, - #[serde(default)] - pub auto_generated: bool, - #[serde(default)] - pub attachment: String, - #[serde(default)] - pub message_type: i32, - #[serde(default)] - pub interest_only: bool, -} - -#[derive(Debug, Clone, Serialize, PartialEq, Eq)] -pub struct LiveContextSyncState { - pub source: String, - pub chat_id: i64, - pub chat: String, - pub checkpoint_log_id: i64, - pub authoritative: bool, - pub summary_dirty: bool, - pub sync_status: String, -} - -impl LiveContextSyncState { - /// Startup and `--check` may proceed while summaries are dirty. - /// - /// `summary_dirty` means a pending burst still needs a summary refresh. The - /// reply path already runs `context-sync-local` before grounded drafts, so - /// blocking the whole worker on that flag leaves authorized inbound stuck - /// behind a fence the worker itself is supposed to clear. - pub fn allows_auto_reply_startup(&self, chat_id: i64, chat_name: &str) -> bool { - // 로컬 DB에 이름이 없는 그룹방은 '그룹:' 자리표시자로 저장된다. 화면 - // 이름으로 시작을 요청해도 같은 방으로 보고 통과시킨다. - let placeholder = format!("그룹:{chat_id}"); - let name_matches = - self.chat == chat_name || self.chat == placeholder || chat_name.trim() == placeholder; - self.chat_id == chat_id - && name_matches - && self.authoritative - && matches!(self.sync_status.as_str(), "ready" | "partial") - } -} - -#[derive(Debug, Clone, Serialize, PartialEq, Eq)] -pub struct LiveContextIngestResult { - pub source: String, - pub chat_id: i64, - pub chat: String, - pub checkpoint_log_id: i64, - pub inserted_events: usize, - pub duplicate_events: usize, - pub indexed_messages: usize, - pub style_messages: usize, - pub response_samples: usize, - pub recipient_style_samples: usize, - pub summary_refreshed: bool, - pub authoritative: bool, -} - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct OutgoingSelfEvent { - pub chat_id: i64, - pub log_id: i64, - pub message: String, - pub sent_at: i64, -} - -#[derive(Debug, Clone, Serialize, PartialEq, Eq)] -pub struct AutoGeneratedSelfEventClassification { - pub chat_id: i64, - pub log_id: i64, - pub auto_generated: bool, - pub matched_event_id: Option, - pub reason: String, -} - -#[derive(Debug, Clone, Serialize)] -pub struct RecipientStyleProfile { - pub recipient: String, - pub direct_sample_count: usize, - pub confidence_sum: f64, - pub used_fallback: bool, - /// 존댓말/반말 파생 필드. 종결어미 통계에서 규칙 기반으로 도출한다(R3.3). - pub honorific: Honorific, - /// 격식/비격식 파생 필드. 종결어미와 이모지·구두점·축약 토큰 통계를 - /// 결합해 규칙 기반으로 도출한다(R3.3). - pub formality: Formality, - pub profile: StyleProfile, -} - -/// 최연우가 특정 상대방에게 쓰는 말투가 존댓말인지 반말인지 구분한다(R3.3). -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] -#[serde(rename_all = "snake_case")] -pub enum Honorific { - /// 존댓말(예: `-요`, `-습니다`). - Honorific, - /// 반말(예: `-어`, `-야`). - Casual, -} - -/// 말투가 격식체인지 비격식체인지 구분한다(R3.3). -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] -#[serde(rename_all = "snake_case")] -pub enum Formality { - /// 격식체(합쇼체 위주, 이모지·축약이 적음). - Formal, - /// 비격식체(해요체·반말, 이모지·축약이 섞임). - Informal, -} - -/// 종결어미 하나의 분류. 존댓말/격식 파생에만 쓰는 내부 신호다. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum StyleEndingClass { - /// 합쇼체 종결어미(`습니다`/`합니다`/`됩니다`) — 격식·존댓말 신호. - Deferential, - /// 해요체 종결어미(`요`/`죠`) — 존댓말 신호. - Honorific, - /// 웃음·축약 종결(`ㅋㅋㅋ`/`ㅠㅠ` 등) — 비격식 신호. - Contraction, - /// 그 밖의 반말 종결(`어`/`아`/`야`/`지` 등). - Casual, -} - -/// 종결어미 문자열을 존댓말/격식 파생용 분류로 매핑한다. 기존 말투 통계에 -/// 등장하는 종결어미만 다룬다. -pub fn classify_style_ending_class(ending: &str) -> StyleEndingClass { - match ending { - "습니다" | "합니다" | "됩니다" => StyleEndingClass::Deferential, - "요" | "죠" => StyleEndingClass::Honorific, - "ㅋㅋㅋ" | "ㅠㅠ" | "ㅜㅜ" | "ㅠ" | "ㅜ" => StyleEndingClass::Contraction, - _ => StyleEndingClass::Casual, - } -} - -/// 토큰이 웃음·축약 토큰(`ㅋㅋ`, `ㅎㅎ`, `ㅠㅠ`, `ㅜㅜ` 계열)인지 판단한다. -/// 자음/모음 축약만으로 이루어진 토큰을 비격식 신호로 본다. -pub fn is_contraction_token(token: &str) -> bool { - let trimmed = token.trim(); - !trimmed.is_empty() - && trimmed - .chars() - .all(|ch| matches!(ch, 'ㅋ' | 'ㅎ' | 'ㅠ' | 'ㅜ' | 'ㅡ' | 'ㅗ' | 'ㄷ' | 'ㅇ')) -} - -/// 존댓말/격식 파생에 쓰는 규칙 기반 신호 묶음. 종결어미·이모지·구두점·축약 -/// 토큰 통계에서만 도출하며 원문은 담지 않는다(R3.3). -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] -pub struct StyleSignals { - pub sample_count: usize, - pub honorific_ending_count: usize, - pub deferential_ending_count: usize, - pub casual_ending_count: usize, - pub emoji_count: usize, - pub punctuation_count: usize, - pub contraction_count: usize, -} - -impl StyleSignals { - /// 이미 집계된 말투 프로필의 종결어미·이모지·구두점·축약 통계에서 신호를 - /// 뽑아낸다. `common_endings_json`으로 존댓말/반말/격식 종결을 분류하고, - /// `casual_ending_counts_json`·`common_tokens_json`에서 축약 신호를 더한다. - pub fn from_style_profile(profile: &StyleProfile) -> Self { - let mut signals = StyleSignals { - sample_count: profile.sample_count, - emoji_count: profile.emoji_count, - punctuation_count: profile.punctuation_count, - ..StyleSignals::default() - }; - if let Ok(map) = - serde_json::from_str::>(&profile.common_endings_json) - { - for (ending, count) in map { - match classify_style_ending_class(&ending) { - StyleEndingClass::Deferential => signals.deferential_ending_count += count, - StyleEndingClass::Honorific => signals.honorific_ending_count += count, - StyleEndingClass::Contraction => signals.contraction_count += count, - StyleEndingClass::Casual => signals.casual_ending_count += count, - } - } - } - // 축약/웃음 신호는 별도 통계에서만 추가로 집계한다(반말 종결과 중복 계산 방지). - if let Ok(map) = - serde_json::from_str::>(&profile.casual_ending_counts_json) - { - for (ending, count) in map { - if classify_style_ending_class(&ending) == StyleEndingClass::Contraction { - signals.contraction_count += count; - } - } - } - if let Ok(map) = - serde_json::from_str::>(&profile.common_tokens_json) - { - for (token, count) in map { - if is_contraction_token(&token) { - signals.contraction_count += count; - } - } - } - signals - } -} - -/// 규칙 기반으로 존댓말/반말과 격식/비격식을 도출한다(R3.3). -/// -/// - 존댓말/반말: 존댓말 종결(해요체+합쇼체) 신호가 반말 종결 신호보다 많으면 -/// 존댓말로 본다. -/// - 격식/비격식: 존댓말이면서 합쇼체 종결이 있고, 그 합쇼체 신호가 -/// 비격식 신호(이모지+축약+반말 종결)보다 우세할 때만 격식체로 본다. -/// 나머지는 모두 비격식체다(해요체·반말·이모지 섞인 존댓말 포함). -pub fn derive_honorific_formality(signals: &StyleSignals) -> (Honorific, Formality) { - let honorific_signal = signals.honorific_ending_count + signals.deferential_ending_count; - let casual_signal = signals.casual_ending_count; - let honorific = if honorific_signal > casual_signal { - Honorific::Honorific - } else { - Honorific::Casual - }; - - let informal_signal = - signals.emoji_count + signals.contraction_count + signals.casual_ending_count; - let formal_signal = signals.deferential_ending_count; - let formality = if honorific == Honorific::Honorific - && formal_signal > 0 - && formal_signal >= informal_signal - { - Formality::Formal - } else { - Formality::Informal - }; - - (honorific, formality) -} - -/// 말투 프로필에서 곧바로 존댓말/격식을 도출하는 편의 함수. -pub fn derive_style_profile_honorific_formality(profile: &StyleProfile) -> (Honorific, Formality) { - derive_honorific_formality(&StyleSignals::from_style_profile(profile)) -} - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -struct PendingRecipient { - recipient: String, - log_id: i64, -} - -#[derive(Debug, Default)] -struct RecipientStyleAccumulator { - style: StyleProfileAccumulator, - sample_count: usize, - confidence_sum: f64, -} - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -#[serde(deny_unknown_fields)] -pub struct ResponseTimeComponent { - pub name: String, - pub sample_count: usize, - pub weight: f64, - pub normal_location_seconds: f64, - pub normal_scale_seconds: f64, - pub lower_seconds: f64, - pub upper_seconds: f64, -} - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -#[serde(deny_unknown_fields)] -pub struct ResponseTimeDistribution { - pub schema_version: u32, - pub policy_version: String, - pub model_kind: String, - pub fit_transform: String, - pub sample_count: usize, - pub retained_sample_count: usize, - pub tail_winsorized_count: usize, - pub split_seconds: Vec, - pub global_upper_seconds: f64, - pub components: Vec, -} - -#[derive(Debug, Clone, Serialize)] -pub struct ResponseTimeStats { - pub chat: String, - pub source: String, - pub user: String, - pub sample_count: usize, - pub average_seconds: f64, - pub median_seconds: f64, - pub p90_seconds: f64, - pub min_seconds: f64, - pub max_seconds: f64, - pub max_window_seconds: i64, - pub stddev_seconds: f64, - pub distribution: Option, -} - -#[derive(Debug, Clone, Serialize)] -pub struct ContextResult { - pub chat: String, - pub source: String, - pub date: String, - pub user: String, - pub message: String, - pub score: f32, - pub mode: String, -} -#[derive(Debug, Clone)] -struct ContextCandidate { - id: i64, - result: ContextResult, -} -#[derive(Debug, Clone, Serialize)] -pub struct StyleProfile { - pub chat: String, - pub source: String, - pub user: String, - pub sample_count: usize, - pub average_character_length: f64, - pub median_character_length: f64, - pub p90_character_length: f64, - pub casual_ending_count: usize, - pub casual_ending_counts_json: String, - pub question_count: usize, - pub emoji_count: usize, - pub punctuation_count: usize, - pub common_endings_json: String, - pub common_tokens_json: String, - pub policy_version: String, -} -#[derive(Debug, Clone, Serialize)] -pub struct ContextReplyBundle { - pub schema_version: u32, - pub context: Vec, - pub styles: Vec, - pub prior_decisions: Vec, - pub style_profile: Option, - pub response_time: Option, -} - -#[derive(Debug, Clone, Serialize)] -pub struct RecipientContextReplyBundle { - pub schema_version: u32, - pub recipient: String, - pub context: Vec, - pub styles: Vec, - pub prior_decisions: Vec, - pub style_profile: Option, - pub recipient_style_profile: Option, - pub response_time: Option, -} - -fn validate_style_profile_laughter(profile: &StyleProfile, label: &str) -> Result<()> { - for encoded in [ - &profile.casual_ending_counts_json, - &profile.common_endings_json, - &profile.common_tokens_json, - ] { - let value: serde_json::Value = - serde_json::from_str(encoded).with_context(|| format!("{label} JSON is invalid"))?; - let values = value - .as_object() - .ok_or_else(|| anyhow::anyhow!("{label} JSON is not an object"))?; - if values - .keys() - .any(|key| crate::reply_policy::validate_auto_reply_laughter(key).is_err()) - { - anyhow::bail!("{label} contains disallowed laughter evidence"); - } - } - Ok(()) -} - -impl ContextReplyBundle { - fn validate_json_size(&self) -> Result<()> { - if self - .styles - .iter() - .any(|style| crate::reply_policy::validate_auto_reply_laughter(&style.message).is_err()) - { - anyhow::bail!("context reply bundle contains disallowed style evidence"); - } - if let Some(profile) = &self.style_profile { - if profile.sample_count == 0 || profile.user != STYLE_USER { - anyhow::bail!("context reply bundle style profile is invalid"); - } - if profile.policy_version != STYLE_POLICY_VERSION { - anyhow::bail!("context reply bundle style policy mismatch"); - } - validate_style_profile_laughter(profile, "context reply bundle style profile")?; - } - for prior in &self.prior_decisions { - let evidence: serde_json::Value = serde_json::from_str(&prior.evidence_json) - .context("context reply bundle evidence JSON is invalid")?; - if !evidence.is_object() { - anyhow::bail!("context reply bundle evidence JSON is not an object"); - } - } - let bytes = serde_json::to_vec(self)?; - if bytes.len() > CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES { - anyhow::bail!( - "context reply bundle exceeds {} bytes", - CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES - ); - } - Ok(()) - } - fn redact_provenance(&mut self) { - for result in self.context.iter_mut().chain(self.styles.iter_mut()) { - result.source = provenance_id(&result.source); - } - if let Some(profile) = self.style_profile.as_mut() { - profile.source = provenance_id(&profile.source); - } - if let Some(response_time) = self.response_time.as_mut() { - response_time.source = provenance_id(&response_time.source); - } - for prior in &mut self.prior_decisions { - if let Ok(mut evidence) = - serde_json::from_str::(&prior.evidence_json) - { - redact_absolute_paths(&mut evidence); - if let Ok(serialized) = serde_json::to_string(&evidence) { - prior.evidence_json = serialized; - } - } - } - } -} - -impl RecipientContextReplyBundle { - fn validate_json_size(&self) -> Result<()> { - if self.schema_version != RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION - || self.recipient.trim().is_empty() - { - anyhow::bail!("recipient context reply bundle identity is invalid"); - } - ContextReplyBundle { - schema_version: CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION, - context: self.context.clone(), - styles: self.styles.clone(), - prior_decisions: self.prior_decisions.clone(), - style_profile: self.style_profile.clone(), - response_time: self.response_time.clone(), - } - .validate_json_size()?; - if let Some(recipient_profile) = &self.recipient_style_profile { - if recipient_profile.recipient != self.recipient - || recipient_profile.profile.sample_count == 0 - || recipient_profile.profile.user != STYLE_USER - || recipient_profile.profile.policy_version != STYLE_POLICY_VERSION - || !recipient_profile.confidence_sum.is_finite() - || recipient_profile.confidence_sum < 0.0 - { - anyhow::bail!("recipient context reply bundle style profile is invalid"); - } - validate_style_profile_laughter( - &recipient_profile.profile, - "recipient context reply bundle style profile", - )?; - if !recipient_profile.used_fallback - && (recipient_profile.direct_sample_count < RECIPIENT_STYLE_MIN_SAMPLES - || recipient_profile.confidence_sum < RECIPIENT_STYLE_MIN_CONFIDENCE) - { - anyhow::bail!("recipient context reply bundle direct style evidence is weak"); - } - } - let bytes = serde_json::to_vec(self)?; - if bytes.len() > CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES { - anyhow::bail!( - "recipient context reply bundle exceeds {} bytes", - CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES - ); - } - Ok(()) - } - - fn redact_provenance(&mut self) { - for result in self.context.iter_mut().chain(self.styles.iter_mut()) { - result.source = provenance_id(&result.source); - } - if let Some(profile) = self.style_profile.as_mut() { - profile.source = provenance_id(&profile.source); - } - if let Some(profile) = self.recipient_style_profile.as_mut() { - profile.profile.source = provenance_id(&profile.profile.source); - } - if let Some(response_time) = self.response_time.as_mut() { - response_time.source = provenance_id(&response_time.source); - } - redact_reply_decisions(&mut self.prior_decisions); - } -} - -pub fn provenance_id(source: &str) -> String { - format!("source:{}", hex::encode(Sha256::digest(source.as_bytes()))) -} - -pub fn redact_absolute_paths(value: &mut serde_json::Value) { - match value { - serde_json::Value::String(text) if Path::new(text).is_absolute() => { - *text = provenance_id(text); - } - serde_json::Value::Array(values) => { - for value in values { - redact_absolute_paths(value); - } - } - serde_json::Value::Object(values) => { - for value in values.values_mut() { - redact_absolute_paths(value); - } - } - _ => {} - } -} - -pub fn redact_context_results(results: &mut [ContextResult]) { - for result in results { - result.source = provenance_id(&result.source); - } -} - -pub fn redact_response_time_stats(stats: &mut Option) { - if let Some(stats) = stats { - stats.source = provenance_id(&stats.source); - } -} - -pub fn redact_reply_decisions(results: &mut [ReplyDecisionMatch]) { - for result in results { - if let Ok(mut evidence) = serde_json::from_str::(&result.evidence_json) { - redact_absolute_paths(&mut evidence); - result.evidence_json = - serde_json::to_string(&evidence).unwrap_or_else(|_| "{}".to_string()); - } else { - result.evidence_json = serde_json::json!({ - "redacted_evidence": provenance_id(&result.evidence_json) - }) - .to_string(); - } - } -} - -#[derive(Debug, Clone)] -struct StyleMessageFeatures { - style_eligible: bool, - content_kind: &'static str, - character_length: usize, - casual_ending: Option<&'static str>, - question_count: usize, - emoji_count: usize, - punctuation_count: usize, - features_json: String, -} - -#[derive(Debug, Default)] -struct StyleProfileAccumulator { - lengths: Vec, - casual_endings: BTreeMap, - endings: BTreeMap, - question_count: usize, - emoji_count: usize, - punctuation_count: usize, - tokens: BTreeMap, -} - -impl StyleProfileAccumulator { - fn add(&mut self, message: &str, features: &StyleMessageFeatures) { - self.lengths.push(features.character_length); - if let Some(ending) = features.casual_ending { - *self.casual_endings.entry(ending.to_string()).or_default() += 1; - } - if let Some(ending) = common_style_ending(message) { - *self.endings.entry(ending.to_string()).or_default() += 1; - } - self.question_count += features.question_count; - self.emoji_count += features.emoji_count; - self.punctuation_count += features.punctuation_count; - for token in message.split_whitespace().filter_map(normalize_style_token) { - *self.tokens.entry(token).or_default() += 1; - } - } - - fn finish(self, chat: &str, source: &str, user: &str) -> Option { - if self.lengths.is_empty() { - return None; - } - let mut lengths = self.lengths; - lengths.sort_unstable(); - let percentile = |ratio: f64| { - let position = (lengths.len() - 1) as f64 * ratio; - let lower = position.floor() as usize; - let upper = position.ceil() as usize; - if lower == upper { - lengths[lower] as f64 - } else { - lengths[lower] as f64 - + (lengths[upper] - lengths[lower]) as f64 * (position - lower as f64) - } - }; - let casual_ending_count = self.casual_endings.values().sum(); - let casual_ending_counts_json = - serde_json::to_string(&top_counts(&self.casual_endings, 12)) - .unwrap_or_else(|_| "{}".into()); - let common_endings_json = - serde_json::to_string(&top_counts(&self.endings, 12)).unwrap_or_else(|_| "{}".into()); - let common_tokens_json = - serde_json::to_string(&top_counts(&self.tokens, 32)).unwrap_or_else(|_| "{}".into()); - Some(StyleProfile { - chat: chat.to_string(), - source: source.to_string(), - user: user.to_string(), - sample_count: lengths.len(), - average_character_length: lengths.iter().sum::() as f64 / lengths.len() as f64, - median_character_length: percentile(0.5), - p90_character_length: percentile(0.9), - casual_ending_count, - casual_ending_counts_json, - question_count: self.question_count, - emoji_count: self.emoji_count, - punctuation_count: self.punctuation_count, - common_endings_json, - common_tokens_json, - policy_version: STYLE_POLICY_VERSION.to_string(), - }) - } -} - -fn top_counts(counts: &BTreeMap, limit: usize) -> BTreeMap { - let mut values = counts.iter().collect::>(); - values.sort_by(|(left_name, left_count), (right_name, right_count)| { - right_count - .cmp(left_count) - .then_with(|| left_name.cmp(right_name)) - }); - values - .into_iter() - .take(limit) - .map(|(name, count)| (name.clone(), *count)) - .collect() -} - -fn normalize_style_token(token: &str) -> Option { - let normalized = token - .trim_matches(|ch: char| { - ch.is_ascii_punctuation() - || matches!( - ch, - '。' | ',' | '!' | '?' | '~' | '…' | '·' | '「' | '」' | '『' | '』' - ) - }) - .trim(); - (!normalized.is_empty()).then(|| normalized.to_string()) -} - -fn trim_style_tail(message: &str) -> &str { - message.trim_end_matches(|ch: char| { - ch.is_whitespace() - || is_emoji(ch) - || ch.is_ascii_punctuation() - || matches!(ch, '。' | '!' | '?' | '~' | '…') - }) -} - -fn style_ending(message: &str) -> Option<&'static str> { - let message = trim_style_tail(message); - [ - "잖아", - "거든", - "같아", - "ㅋㅋㅋ", - "ㅠㅠ", - "ㅜㅜ", - "요", - "죠", - "네", - "지", - "까", - "어", - "아", - "야", - "래", - "ㅠ", - "ㅜ", - ] - .into_iter() - .find(|ending| message.ends_with(ending)) -} -fn common_style_ending(message: &str) -> Option<&'static str> { - let message = trim_style_tail(message); - [ - "잖아", - "거든", - "같아", - "ㅋㅋㅋ", - "ㅠㅠ", - "ㅜㅜ", - "습니다", - "합니다", - "됩니다", - "요", - "죠", - "네", - "지", - "까", - "어", - "아", - "야", - "래", - "다", - "해", - "함", - "임", - ] - .into_iter() - .find(|ending| message.ends_with(ending)) -} - -fn is_emoji(ch: char) -> bool { - matches!( - ch as u32, - 0x1F000..=0x1FAFF | 0x2600..=0x27BF - ) -} - -fn is_generic_ack_style(message: &str) -> bool { - let remainder: String = message - .chars() - .filter(|ch| { - !ch.is_whitespace() - && !matches!( - *ch, - 'ㅋ' | 'ㅎ' | 'ㄷ' | ',' | '.' | '!' | '?' | '?' | '~' | '~' | ';' - ) - }) - .collect(); - matches!( - remainder.as_str(), - "" | "ㅇㅇ" | "응" | "네" | "맞아" | "ㅇ" | "음" | "어" | "웅" - ) -} - -fn is_assistant_tell_style(message: &str) -> bool { - let lower = message.to_ascii_lowercase(); - const TELLS: &[&str] = &[ - "답할 수 있어", - "답할 수 있습니다", - "도와드릴", - "무엇을 도와", - "맥락에 맞게 답", - "i can help", - "as an ai", - "i'm an ai", - "i am an ai", - ]; - // Every tell is either non-ASCII (Hangul, untouched by ASCII lowercasing) - // or already ASCII-lowercase, so `lower.contains(tell)` is exactly the - // original `message.contains(tell) || lower.contains(tell_lower)` test - // without allocating a throwaway String per tell. - TELLS.iter().any(|tell| lower.contains(*tell)) -} - -fn is_spectator_narration_style(message: &str) -> bool { - let compact: String = message.chars().filter(|ch| !ch.is_whitespace()).collect(); - compact.ends_with("알아보나보네") - || compact.ends_with("하나보네") - || compact.ends_with("인가보네") - || compact.ends_with("쪽인가보네") -} - -fn classify_style_message(message: &str) -> StyleMessageFeatures { - let normalized = message.trim(); - let char_count = normalized.chars().count(); - let lower = normalized.to_ascii_lowercase(); - let lines = normalized.lines().map(str::trim).collect::>(); - let starts_list = lines.first().is_some_and(|line| { - line.chars() - .next() - .is_some_and(|ch| matches!(ch, '*' | '-' | '•' | '⭐' | '📌' | '#')) - || line.starts_with("Q.") - || line.starts_with("A.") - || line.starts_with('①') - || line.starts_with('②') - || line.starts_with('③') - || (line.chars().next().is_some_and(|ch| ch.is_ascii_digit()) - && line - .chars() - .nth(1) - .is_some_and(|ch| matches!(ch, '.' | ')' | ':'))) - }); - let pasted_markers = [ - "복사", - "붙여넣", - "정보 전달", - "출처", - "요약", - "핵심 내용", - "web발신", - "공지사항", - "속보", - "공유드립니다", - "전달드립니다", - ]; - let formal_prefixes = ["공지", "안내", "알려드립니다", "필독", "[공지]", "[안내]"]; - let formal_suffixes = [ - "습니다", - "니다", - "합니다", - "됩니다", - "바랍니다", - "드립니다", - "하십시오", - ]; - let metadata = normalized.is_empty() - || normalized.starts_with('@') - || matches!(normalized, "사진" | "동영상" | "파일" | "이모티콘") - || (normalized.contains("님이") && normalized.contains("되었습니다")) - || normalized - .chars() - .all(|ch| ch.is_ascii_punctuation() || ch.is_whitespace()) - || normalized.chars().any(|ch| ch.is_ascii_digit()) - || normalized.contains(':'); - let formal_target = trim_style_tail(normalized); - let (style_eligible, content_kind) = - if crate::reply_policy::validate_auto_reply_laughter(normalized).is_err() { - (false, "reply_laughter_policy_excluded") - } else if char_count < 2 { - (false, "metadata_or_noise") - } else if lower.contains("http://") || lower.contains("https://") || lower.contains("www.") - { - (false, "url") - } else if lines.len() > 1 { - (false, "multiline") - } else if starts_list { - (false, "list_or_numbered") - } else if metadata { - (false, "metadata_or_noise") - } else if char_count > 60 - || formal_prefixes - .iter() - .any(|prefix| normalized.starts_with(prefix)) - || formal_suffixes - .iter() - .any(|suffix| formal_target.ends_with(suffix)) - || normalized.contains("하시기 바랍니다") - { - (false, "long_or_formal") - } else if pasted_markers - .iter() - .any(|marker| normalized.contains(marker)) - { - (false, "pasted_information") - } else if is_generic_ack_style(normalized) { - (false, "generic_ack") - } else if is_assistant_tell_style(normalized) { - (false, "assistant_tell") - } else if is_spectator_narration_style(normalized) { - (false, "spectator_narration") - } else { - (true, "ordinary_conversation") - }; - let question_count = normalized - .chars() - .filter(|ch| matches!(ch, '?' | '?')) - .count(); - let emoji_count = normalized.chars().filter(|ch| is_emoji(*ch)).count(); - let punctuation_count = normalized - .chars() - .filter(|ch| { - ch.is_ascii_punctuation() - || matches!( - ch, - '。' | ',' | '!' | '?' | '~' | '…' | '·' | '「' | '」' | '『' | '』' - ) - }) - .count(); - let casual_ending = style_ending(normalized); - let features_json = serde_json::json!({ - "character_length": char_count, - "casual_ending": casual_ending, - "question_count": question_count, - "emoji_count": emoji_count, - "punctuation_count": punctuation_count, - "content_kind": content_kind, - "style_eligible": style_eligible, - "policy_version": STYLE_POLICY_VERSION, - }) - .to_string(); - StyleMessageFeatures { - style_eligible, - content_kind, - character_length: char_count, - casual_ending, - question_count, - emoji_count, - punctuation_count, - features_json, - } -} - -const TOPIC_LEXICON: &[(&str, &[&str])] = &[ - ( - "contact", - &[ - "번호", - "연락처", - "전화번호", - "폰번호", - "핸드폰", - "휴대폰", - "전화", - ], - ), - ( - "ax_macos", - &[ - "ax api", - "axapi", - "손쉬운 사용", - "accessibility", - "axui", - "axtextarea", - "axshowmenu", - ], - ), - ( - "computer_use", - &[ - "컴퓨터 유즈", - "computer use", - "computer-use", - "스크린샷", - "픽셀", - "마우스", - ], - ), - ( - "llm_tools", - &[ - "토큰", - "cursor", - "grok", - "gemini", - "claude", - "chatgpt", - "가재코드", - "모델", - ], - ), - ( - "kakao_auto", - &["자동답", "local-send", "reply-to", "카톡 답", "워커"], - ), - ( - "business", - &[ - "사업자", - "지원", - "세금", - "신청서", - "사업", - "창업", - "매출", - "영업", - "법인", - "스타트업", - ], - ), - ("infra", &["리눅스", "linux", "가상머신"]), - ("news", &["긱뉴스", "geeknews", "hada.io"]), - ("identity", &["나임", "연우지", "사람이지"]), - ( - "stocks", - &[ - "주식", - "증권", - "코스피", - "코스닥", - "나스닥", - "다우", - "배당", - "상장", - "공모주", - "etf", - "양도세", - "매수", - "매도", - "주가", - ], - ), - ( - "coins", - &[ - "코인", - "비트코인", - "이더리움", - "알트", - "업비트", - "빗썸", - "바이낸스", - "김치프리미엄", - "김프", - "btc", - "eth", - "blockchain", - "블록체인", - ], - ), - ( - "investing", - &[ - "투자", - "수익률", - "포트폴리오", - "자산배분", - "적립", - "펀드", - "재테크", - "시드", - ], - ), - ( - "real_estate", - &[ - "부동산", - "아파트", - "전세", - "월세", - "매매", - "청약", - "분양", - "등기", - "전세사기", - "집값", - ], - ), - ("auction", &["경매", "공매", "낙찰", "입찰", "경매물건"]), - ( - "ai", - &[ - "인공지능", - "챗gpt", - "chatgpt", - "지피티", - "llm", - "그록", - "grok", - "클로드", - "claude", - "제미니", - "gemini", - "머신러닝", - "딥러닝", - "openai", - "anthropic", - ], - ), -]; - -const INTEREST_TOPICS: &[&str] = &[ - "stocks", - "coins", - "investing", - "real_estate", - "auction", - "business", - "ai", -]; -const TOPIC_LEXICON_VERSION: &str = "2"; - -fn classify_message_topics(message: &str) -> Vec<&'static str> { - let text = message.trim(); - if text.is_empty() { - return Vec::new(); - } - let lower = text.to_ascii_lowercase(); - let mut topics = Vec::new(); - for (topic, needles) in TOPIC_LEXICON { - let matched = needles.iter().any(|needle| { - if needle.bytes().all(|byte| byte.is_ascii()) { - // See `topic_lexicon_ascii_needles_are_lowercase`: every ASCII - // needle is already lowercase, so matching the precomputed - // lowercase haystack avoids one String per needle per message. - lower.contains(*needle) - } else { - text.contains(needle) - } - }); - if matched { - topics.push(*topic); - } - } - topics -} - -fn message_has_interest_topic(message: &str) -> bool { - classify_message_topics(message) - .iter() - .any(|topic| INTEREST_TOPICS.contains(topic)) -} - -fn push_unique_fragment(parts: &mut Vec, value: &str) { - let trimmed = value.trim(); - if trimmed.is_empty() { - return; - } - if parts.iter().any(|part| part == trimmed) { - return; - } - parts.push(trimmed.to_string()); -} - -fn collect_attachment_fragments(value: &serde_json::Value, parts: &mut Vec) { - match value { - serde_json::Value::String(text) => { - let trimmed = text.trim(); - if trimmed.starts_with("http://") || trimmed.starts_with("https://") { - push_unique_fragment(parts, trimmed); - } - } - serde_json::Value::Array(items) => { - for item in items { - collect_attachment_fragments(item, parts); - } - } - serde_json::Value::Object(map) => { - for (key, child) in map { - let key_lower = key.to_ascii_lowercase(); - if matches!( - key_lower.as_str(), - "urls" - | "url" - | "src_message" - | "name" - | "filename" - | "title" - | "t" - | "d" - | "description" - | "text" - | "message" - | "content" - ) { - match child { - serde_json::Value::String(text) => push_unique_fragment(parts, text), - serde_json::Value::Array(items) => { - for item in items { - if let Some(text) = item.as_str() { - push_unique_fragment(parts, text); - } else { - collect_attachment_fragments(item, parts); - } - } - } - _ => collect_attachment_fragments(child, parts), - } - } else { - collect_attachment_fragments(child, parts); - } - } - } - _ => {} - } -} - -fn live_index_text(message: &str, attachment: &str, message_type: i32) -> String { - let mut parts = Vec::new(); - push_unique_fragment(&mut parts, message); - match message_type { - 2 => push_unique_fragment(&mut parts, "사진"), - 3 => push_unique_fragment(&mut parts, "동영상"), - 18 | 16 => push_unique_fragment(&mut parts, "파일"), - 27 => push_unique_fragment(&mut parts, "이모티콘"), - 71 => push_unique_fragment(&mut parts, "샵검색"), - _ => {} - } - let trimmed_attachment = attachment.trim(); - if !trimmed_attachment.is_empty() { - if let Ok(value) = serde_json::from_str::(trimmed_attachment) { - collect_attachment_fragments(&value, &mut parts); - } - } - let mut text = parts.join("\n"); - if text.len() > LIVE_CONTEXT_MAX_FIELD_BYTES { - text.truncate(LIVE_CONTEXT_MAX_FIELD_BYTES); - } - text -} - -fn ensure_topic_lexicon(tx: &rusqlite::Transaction<'_>) -> Result<()> { - let current: Option = tx - .query_row( - "SELECT value FROM context_retrieval_meta WHERE key = 'topic_lexicon_version'", - [], - |row| row.get(0), - ) - .optional()?; - if current.as_deref() == Some(TOPIC_LEXICON_VERSION) { - return Ok(()); - } - backfill_message_topics(tx)?; - tx.execute( - "INSERT INTO context_retrieval_meta(key, value) - VALUES ('topic_lexicon_version', ?1) - ON CONFLICT(key) DO UPDATE SET value = excluded.value", - [TOPIC_LEXICON_VERSION], - )?; - Ok(()) -} - -fn insert_message_topics( - conn: &Connection, - message_id: i64, - chat: &str, - source: &str, - date: &str, - message: &str, -) -> Result<()> { - if message_id <= 0 { - return Ok(()); - } - for topic in classify_message_topics(message) { - let added = conn.execute( - "INSERT OR IGNORE INTO context_message_topics(message_id, topic) - VALUES (?1, ?2)", - params![message_id, topic], - )?; - if added == 0 { - continue; - } - conn.execute( - "INSERT INTO context_topic_stats( - chat, source, topic, message_count, last_date - ) VALUES (?1, ?2, ?3, 1, ?4) - ON CONFLICT(chat, source, topic) DO UPDATE SET - message_count = message_count + 1, - last_date = CASE - WHEN excluded.last_date > last_date THEN excluded.last_date - ELSE last_date - END", - params![chat, source, topic, date], - )?; - } - Ok(()) -} - -fn backfill_message_topics(conn: &Connection) -> Result<()> { - let mut last_id = 0_i64; - loop { - let mut stmt = conn.prepare( - "SELECT id, chat, source, date, message - FROM context_messages - WHERE id > ?1 - ORDER BY id ASC - LIMIT 1000", - )?; - let batch = stmt - .query_map([last_id], |row| { - Ok(( - row.get::<_, i64>(0)?, - row.get::<_, String>(1)?, - row.get::<_, String>(2)?, - row.get::<_, String>(3)?, - row.get::<_, String>(4)?, - )) - })? - .collect::>>()?; - drop(stmt); - if batch.is_empty() { - break; - } - for (id, chat, source, date, message) in batch { - last_id = id; - insert_message_topics(conn, id, &chat, &source, &date, &message)?; - } - } - Ok(()) -} - -fn context_topic_tables_ready(conn: &Connection) -> Result { - let count: i64 = conn.query_row( - "SELECT COUNT(*) FROM sqlite_master - WHERE type = 'table' AND name = 'context_message_topics'", - [], - |row| row.get(0), - )?; - Ok(count == 1) -} - -#[cfg(test)] -fn is_conversational_style_message(message: &str) -> bool { - classify_style_message(message).style_eligible -} -#[derive(Debug, Clone, Deserialize)] -struct ReplyDecisionInput { - event_id: String, - chat: String, - author: String, - received_at: String, - message: String, - decision: String, - reason: String, - category: String, - context_match_count: usize, - style_match_count: usize, - best_context_score: f32, - best_style_score: f32, - prior_similarity: f32, - scheduled_delay_seconds: f64, - status: String, - reply: Option, - #[serde(default)] - evidence_ids: Vec, - #[serde(default)] - style_policy_version: String, - #[serde(default)] - provenance: Option, -} - -#[derive(Debug, Clone, Serialize)] -pub struct ReplyDecisionMatch { - pub event_id: String, - pub chat: String, - pub author: String, - pub received_at: String, - pub message: String, - pub decision: String, - pub reason: String, - pub category: String, - pub status: String, - pub reply: Option, - pub evidence_json: String, - pub score: f32, -} - -pub fn default_db_path() -> PathBuf { - let base = dirs::data_local_dir() - .or_else(dirs::home_dir) - .unwrap_or_else(|| PathBuf::from("/tmp")); - base.join("openkakao").join("context.sqlite3") -} - -/// Apply the additive context/FTS/live-index migrations without ingesting or -/// deleting any context rows. Safe to call before a first sync state lookup. -pub fn ensure_live_context_schema(db_path: &Path) -> Result<()> { - let _ = open_db(db_path)?; - Ok(()) -} - -fn parse_chat_date(value: &str) -> Option { - NaiveDateTime::parse_from_str(value.trim(), "%Y-%m-%d %H:%M:%S") - .or_else(|_| NaiveDateTime::parse_from_str(value.trim(), "%Y-%m-%d %H:%M")) - .ok() -} - -fn is_response_participant(user: &str) -> bool { - let user = user.trim(); - !user.is_empty() - && user != STYLE_USER - && !user.ends_with('봇') - && !matches!( - user, - "드리고" | "뉴스봇" | "채팅봇" | "주식봇" | "날씨날씨" | "인아웃" | "채팅도구" - ) -} - -#[derive(Debug)] -struct LiveSourceRow { - account_fingerprint: String, - chat_id: i64, - chat: String, - authoritative: bool, - checkpoint_log_id: i64, - pending_human_log_id: Option, - pending_human_sent_at: Option, - pending_human_name: Option, - pending_burst: Vec, - summary_dirty: bool, -} - -pub fn live_context_source_id(account_fingerprint: &str, chat_id: i64) -> Result { - let fingerprint = account_fingerprint.trim().to_ascii_lowercase(); - if fingerprint.len() != 64 || !fingerprint.bytes().all(|byte| byte.is_ascii_hexdigit()) { - anyhow::bail!("live context account fingerprint must be 64 hexadecimal characters"); - } - if chat_id <= 0 { - anyhow::bail!("live context chat ID must be positive"); - } - Ok(format!( - "{LIVE_CONTEXT_SOURCE_PREFIX}:{fingerprint}:{chat_id}" - )) -} - -fn live_event_digest(event: &LiveContextEvent) -> Result { - let payload = serde_json::to_vec(&serde_json::json!({ - "chat_id": event.chat_id, - "log_id": event.log_id, - "sender_name": event.sender_name, - "message": event.message, - "sent_at": event.sent_at, - "is_self": event.is_self, - "exclude_from_learning": event.exclude_from_learning, - "auto_generated": event.auto_generated, - }))?; - Ok(hex::encode(Sha256::digest(payload))) -} - -fn live_event_date(sent_at: i64) -> Result { - let date = chrono::DateTime::::from_timestamp(sent_at, 0) - .context("live context sent_at is outside the supported timestamp range")?; - Ok(date.format("%Y-%m-%d %H:%M:%S").to_string()) -} - -fn validate_live_context_batch( - chat_id: i64, - chat: &str, - expected_checkpoint: i64, - events: &[LiveContextEvent], - batch_complete: bool, - promote_authoritative: bool, -) -> Result<()> { - if chat_id <= 0 || expected_checkpoint < 0 { - anyhow::bail!("live context chat ID and checkpoint are invalid"); - } - if chat.trim().is_empty() || chat.len() > LIVE_CONTEXT_MAX_FIELD_BYTES { - anyhow::bail!("live context chat name is invalid"); - } - if events.len() > LIVE_CONTEXT_BATCH_MAX_ROWS { - anyhow::bail!("live context batch exceeds {LIVE_CONTEXT_BATCH_MAX_ROWS} messages"); - } - if promote_authoritative && !batch_complete { - anyhow::bail!("live context source can only be promoted after a complete batch"); - } - let mut previous_log_id = expected_checkpoint; - for event in events { - if event.chat_id != chat_id - || event.log_id <= expected_checkpoint - || event.log_id <= previous_log_id - || ((event.auto_generated || event.exclude_from_learning) && !event.is_self) - || event.sender_name.len() > LIVE_CONTEXT_MAX_FIELD_BYTES - || event.message.len() > LIVE_CONTEXT_MAX_FIELD_BYTES - { - anyhow::bail!("live context batch contains an invalid or out-of-order event"); - } - live_event_date(event.sent_at)?; - previous_log_id = event.log_id; - } - Ok(()) -} - -fn load_live_source(conn: &Connection, source: &str) -> Result> { - conn.query_row( - "SELECT account_fingerprint, chat_id, chat, authoritative, - checkpoint_log_id, pending_human_log_id, pending_human_sent_at, - pending_human_name, pending_burst_json, summary_dirty - FROM context_sources WHERE source = ?1", - [source], - |row| { - let pending_burst_json: String = row.get(8)?; - Ok(( - row.get::<_, String>(0)?, - row.get::<_, i64>(1)?, - row.get::<_, String>(2)?, - row.get::<_, i64>(3)?, - row.get::<_, i64>(4)?, - row.get::<_, Option>(5)?, - row.get::<_, Option>(6)?, - row.get::<_, Option>(7)?, - pending_burst_json, - row.get::<_, i64>(9)?, - )) - }, - ) - .optional()? - .map( - |( - account_fingerprint, - chat_id, - chat, - authoritative, - checkpoint_log_id, - pending_human_log_id, - pending_human_sent_at, - pending_human_name, - pending_burst_json, - summary_dirty, - )| { - let pending_burst: Vec = serde_json::from_str(&pending_burst_json) - .context("live context pending recipient burst is malformed")?; - let burst_is_invalid = pending_burst.len() > LIVE_CONTEXT_MAX_PENDING_RECIPIENTS - || pending_burst.iter().any(|item| { - item.recipient.trim().is_empty() - || item.log_id <= 0 - || item.log_id > checkpoint_log_id - }) - || pending_burst - .windows(2) - .any(|items| items[0].log_id >= items[1].log_id); - let pending_is_invalid = match ( - pending_human_log_id, - pending_human_sent_at, - pending_human_name.as_deref(), - ) { - (None, None, None) => !pending_burst.is_empty(), - (Some(log_id), Some(sent_at), Some(name)) => { - log_id <= 0 - || log_id > checkpoint_log_id - || name.trim().is_empty() - || live_event_date(sent_at).is_err() - || pending_burst - .last() - .is_none_or(|item| item.log_id != log_id || item.recipient != name) - } - _ => true, - }; - if burst_is_invalid || pending_is_invalid { - anyhow::bail!("live context pending recipient burst is invalid"); - } - Ok(LiveSourceRow { - account_fingerprint, - chat_id, - chat, - authoritative: authoritative != 0, - checkpoint_log_id, - pending_human_log_id, - pending_human_sent_at, - pending_human_name, - pending_burst, - summary_dirty: summary_dirty != 0, - }) - }, - ) - .transpose() -} - -fn clear_pending_participant_burst(source: &mut LiveSourceRow) { - source.pending_human_log_id = None; - source.pending_human_sent_at = None; - source.pending_human_name = None; - source.pending_burst.clear(); -} - -fn recipient_confidences(burst: &[PendingRecipient]) -> Vec<(String, f64)> { - let mut latest_by_recipient = BTreeMap::new(); - for item in burst { - latest_by_recipient.insert(item.recipient.clone(), item.log_id); - } - let mut recipients = latest_by_recipient.into_iter().collect::>(); - recipients.sort_by(|(left_name, left_log), (right_name, right_log)| { - right_log - .cmp(left_log) - .then_with(|| left_name.cmp(right_name)) - }); - let total_weight = recipients - .iter() - .enumerate() - .map(|(rank, _)| 1.0 / (rank as f64 + 1.0)) - .sum::(); - recipients - .into_iter() - .enumerate() - .map(|(rank, (recipient, _))| (recipient, (1.0 / (rank as f64 + 1.0)) / total_weight)) - .collect() -} - -fn insert_style_profile(conn: &Connection, profile: &StyleProfile, created_at: &str) -> Result<()> { - conn.execute( - "INSERT INTO owner_style_profile( - chat, source, user_name, sample_count, average_character_length, - median_character_length, p90_character_length, casual_ending_count, - casual_ending_counts_json, question_count, emoji_count, punctuation_count, - common_endings_json, common_tokens_json, policy_version, created_at - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16)", - params![ - profile.chat, - profile.source, - profile.user, - profile.sample_count as i64, - profile.average_character_length, - profile.median_character_length, - profile.p90_character_length, - profile.casual_ending_count as i64, - profile.casual_ending_counts_json, - profile.question_count as i64, - profile.emoji_count as i64, - profile.punctuation_count as i64, - profile.common_endings_json, - profile.common_tokens_json, - profile.policy_version, - created_at, - ], - )?; - Ok(()) -} - -fn refresh_live_context_summaries( - conn: &Connection, - source: &str, - chat_id: i64, - chat: &str, -) -> Result<()> { - let now = Utc::now().to_rfc3339(); - let style_messages = { - let mut stmt = conn.prepare( - "SELECT message FROM owner_style - WHERE source = ?1 AND chat = ?2 AND user_name = ?3 - AND style_eligible = 1 AND policy_version = ?4 - ORDER BY source_row ASC, id ASC", - )?; - let rows = stmt.query_map( - params![source, chat, STYLE_USER, STYLE_POLICY_VERSION], - |row| row.get::<_, String>(0), - )?; - rows.collect::>>()? - }; - let mut style_accumulator = StyleProfileAccumulator::default(); - for message in &style_messages { - let features = classify_style_message(message); - if features.style_eligible { - style_accumulator.add(message, &features); - } - } - conn.execute( - "DELETE FROM owner_style_profile - WHERE chat = ?1 AND source = ?2 AND user_name = ?3", - params![chat, source, STYLE_USER], - )?; - if let Some(profile) = style_accumulator.finish(chat, source, STYLE_USER) { - insert_style_profile(conn, &profile, &now)?; - } - - let response_delays = { - let mut stmt = conn.prepare( - "SELECT delay_seconds FROM response_time_samples - WHERE source = ?1 AND chat_id = ?2 ORDER BY reply_log_id ASC", - )?; - let rows = stmt.query_map(params![source, chat_id], |row| row.get::<_, f64>(0))?; - rows.collect::>>()? - }; - conn.execute( - "DELETE FROM response_time_stats - WHERE chat = ?1 AND source = ?2 AND user_name = ?3", - params![chat, source, STYLE_USER], - )?; - if let Some(stats) = summarize_response_delays(chat, source, STYLE_USER, response_delays) { - insert_response_time_stats(conn, &stats)?; - } - - let recipient_rows = { - let mut stmt = conn.prepare( - "SELECT samples.recipient, samples.confidence, styles.message - FROM owner_recipient_style_samples samples - JOIN owner_style styles ON styles.id = samples.style_message_id - WHERE samples.source = ?1 AND samples.chat_id = ?2 - AND styles.style_eligible = 1 AND styles.policy_version = ?3 - ORDER BY samples.recipient ASC, samples.reply_log_id ASC", - )?; - let rows = stmt.query_map(params![source, chat_id, STYLE_POLICY_VERSION], |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, f64>(1)?, - row.get::<_, String>(2)?, - )) - })?; - rows.collect::>>()? - }; - let mut recipient_accumulators: BTreeMap = BTreeMap::new(); - for (recipient, confidence, message) in recipient_rows { - if !confidence.is_finite() || !(0.0..=1.0).contains(&confidence) || confidence == 0.0 { - anyhow::bail!("recipient style confidence is malformed"); - } - let features = classify_style_message(&message); - if !features.style_eligible { - continue; - } - let accumulator = recipient_accumulators.entry(recipient).or_default(); - accumulator.style.add(&message, &features); - accumulator.sample_count += 1; - accumulator.confidence_sum += confidence; - } - conn.execute( - "DELETE FROM owner_recipient_style_profile - WHERE source = ?1 AND chat_id = ?2", - params![source, chat_id], - )?; - for (recipient, accumulator) in recipient_accumulators { - let Some(profile) = accumulator.style.finish(chat, source, STYLE_USER) else { - continue; - }; - conn.execute( - "INSERT INTO owner_recipient_style_profile( - source, chat_id, chat, recipient, user_name, sample_count, - confidence_sum, average_character_length, median_character_length, - p90_character_length, casual_ending_count, casual_ending_counts_json, - question_count, emoji_count, punctuation_count, common_endings_json, - common_tokens_json, policy_version, updated_at - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, - ?13, ?14, ?15, ?16, ?17, ?18, ?19)", - params![ - source, - chat_id, - chat, - recipient, - STYLE_USER, - accumulator.sample_count as i64, - accumulator.confidence_sum, - profile.average_character_length, - profile.median_character_length, - profile.p90_character_length, - profile.casual_ending_count as i64, - profile.casual_ending_counts_json, - profile.question_count as i64, - profile.emoji_count as i64, - profile.punctuation_count as i64, - profile.common_endings_json, - profile.common_tokens_json, - STYLE_POLICY_VERSION, - now, - ], - )?; - } - Ok(()) -} - -/// Window used to attribute an owner style row to one confirmed send. -pub const SELF_SEND_REPAIR_WINDOW_SECONDS: i64 = 180; - -#[derive(Debug, Clone, Serialize, PartialEq, Eq)] -pub struct SelfSendRepairMatch { - pub source: String, - pub log_id: i64, - pub date: String, - pub message: String, - pub content_kind: String, - pub style_eligible_before: bool, - pub matched_event_id: String, - pub offset_seconds: i64, -} - -#[derive(Debug, Clone, Serialize, PartialEq, Eq)] -pub struct SelfSendRepairReport { - pub schema_version: u32, - pub chat: String, - pub chat_id: i64, - pub confirmed_sends: usize, - pub scanned_rows: usize, - pub matched_rows: usize, - pub ambiguous_rows: usize, - pub already_ineligible: usize, - pub deleted_response_samples: usize, - pub deleted_recipient_samples: usize, - pub rebuilt_sources: Vec, - pub dry_run: bool, - pub matches: Vec, -} - -/// Load the durable confirmed-send records of one chat from the decision -/// ledger. Used where no room queue is addressable (CSV import), so a bot reply -/// that was already confirmed in the context database cannot be learned as an -/// owner style sample. -fn confirmed_self_sends_with_connection( - conn: &Connection, - chat: &str, -) -> Result> { - if chat.trim().is_empty() { - anyhow::bail!("confirmed send chat is invalid"); - } - let mut stmt = conn.prepare( - "SELECT event_id, reply, sent_at, updated_at FROM reply_decisions - WHERE chat = ?1 AND decision = 'reply' AND status = 'sent' - AND reply IS NOT NULL AND length(trim(reply)) > 0", - )?; - let rows = stmt.query_map(params![chat], |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, String>(1)?, - row.get::<_, Option>(2)?, - row.get::<_, Option>(3)?, - )) - })?; - let mut sends = Vec::new(); - for row in rows { - let (event_id, text, sent_at, updated_at) = row?; - let text = text.trim().to_string(); - if text.is_empty() { - continue; - } - let Some(sent_at) = sent_at - .as_deref() - .and_then(owner_style_timestamp) - .or_else(|| updated_at.as_deref().and_then(owner_style_timestamp)) - else { - continue; - }; - sends.push(ConfirmedSelfSend { - event_id, - text, - sent_at, - }); - } - Ok(sends) -} - -/// Match one owner style row against confirmed sends. -/// -/// Returns the single matching send plus whether the match was ambiguous. A -/// many-to-one match must stay unclassified (fail closed), so callers have to -/// check the ambiguity flag before trusting the match. -fn match_confirmed_send<'a>( - sends: &'a [ConfirmedSelfSend], - text: &str, - sent_at: i64, -) -> (Option<&'a ConfirmedSelfSend>, bool) { - let mut unique: BTreeMap<&str, &ConfirmedSelfSend> = BTreeMap::new(); - for send in sends { - if send.text != text { - continue; - } - if sent_at.abs_diff(send.sent_at) <= SELF_SEND_REPAIR_WINDOW_SECONDS as u64 { - unique.entry(send.event_id.as_str()).or_insert(send); - } - } - (unique.values().next().copied(), unique.len() > 1) -} - -fn mark_bot_sent_features(features_json: &str, event_id: &str, offset_seconds: i64) -> String { - let mut features: serde_json::Value = - serde_json::from_str(features_json).unwrap_or_else(|_| serde_json::json!({})); - if let Some(object) = features.as_object_mut() { - object.insert("style_eligible".to_string(), serde_json::json!(false)); - object.insert( - "content_kind".to_string(), - serde_json::json!("bot_sent_reply"), - ); - object.insert("bot_sent_event_id".to_string(), serde_json::json!(event_id)); - object.insert( - "bot_sent_offset_seconds".to_string(), - serde_json::json!(offset_seconds), - ); - } - features.to_string() -} - -fn insert_response_time_stats(conn: &Connection, stats: &ResponseTimeStats) -> Result<()> { - conn.execute( - "INSERT INTO response_time_stats( - chat, source, user_name, sample_count, average_seconds, median_seconds, - p90_seconds, min_seconds, max_seconds, max_window_seconds, stddev_seconds, - distribution_schema_version, distribution_json - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)", - params![ - stats.chat, - stats.source, - stats.user, - stats.sample_count as i64, - stats.average_seconds, - stats.median_seconds, - stats.p90_seconds, - stats.min_seconds, - stats.max_seconds, - stats.max_window_seconds, - stats.stddev_seconds, - stats - .distribution - .as_ref() - .map(|distribution| distribution.schema_version as i64) - .unwrap_or(0), - stats - .distribution - .as_ref() - .map(serde_json::to_string) - .transpose()? - .unwrap_or_else(|| "{}".to_string()), - ], - )?; - Ok(()) -} - -/// Recompute the pacing aggregate of an imported (CSV / legacy) source. -/// -/// An imported source stores `response_time_stats` directly and keeps no -/// per-reply `response_time_samples` rows, so a bot reply that was measured -/// before the guard existed cannot be removed by deleting samples. The only way -/// to retire its contribution is to rebuild the aggregate from the source's own -/// messages with the same ordering rules the importer uses, skipping every row -/// that a confirmed send identifies as the assistant. -fn recompute_imported_response_time_stats( - conn: &Connection, - source: &str, - chat: &str, - confirmed_sends: &[ConfirmedSelfSend], -) -> Result { - let rows = { - let mut stmt = conn.prepare( - "SELECT date, user_name, message FROM context_messages - WHERE source = ?1 AND chat = ?2 ORDER BY date ASC, id ASC", - )?; - let mapped = stmt.query_map(params![source, chat], |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, String>(1)?, - row.get::<_, String>(2)?, - )) - })?; - mapped.collect::>>()? - }; - let mut delays = Vec::new(); - let mut previous_human_at: Option = None; - for (date, user, message) in rows { - let Some(current_at) = parse_chat_date(&date) else { - continue; - }; - if user == STYLE_USER { - let sent_at = current_at.and_utc().timestamp(); - let (matched, ambiguous) = - match_confirmed_send(confirmed_sends, message.trim(), sent_at); - if matched.is_some() && !ambiguous { - // A confirmed assistant reply is not a human response, and the - // pending prompt it consumed is gone with it. - previous_human_at = None; - continue; - } - if let Some(previous_at) = previous_human_at.take() { - let delay = (current_at - previous_at).num_seconds(); - if (0..=MAX_RESPONSE_DELAY_SECONDS).contains(&delay) { - delays.push(delay as f64); - } - } - } else if is_response_participant(&user) { - previous_human_at = Some(current_at); - } - } - conn.execute( - "DELETE FROM response_time_stats WHERE chat = ?1 AND source = ?2 AND user_name = ?3", - params![chat, source, STYLE_USER], - )?; - let sample_count = delays.len(); - if let Some(stats) = summarize_response_delays(chat, source, STYLE_USER, delays) { - insert_response_time_stats(conn, &stats)?; - } - // The source also owns a style profile. Rebuilding only the pacing numbers - // would leave the style aggregate describing rows the repair just changed. - let style_messages = { - let mut stmt = conn.prepare( - "SELECT message FROM owner_style - WHERE source = ?1 AND chat = ?2 AND user_name = ?3 - AND style_eligible = 1 AND policy_version = ?4 - ORDER BY source_row ASC, id ASC", - )?; - let mapped = stmt.query_map( - params![source, chat, STYLE_USER, STYLE_POLICY_VERSION], - |row| row.get::<_, String>(0), - )?; - mapped.collect::>>()? - }; - let mut accumulator = StyleProfileAccumulator::default(); - for message in &style_messages { - let features = classify_style_message(message); - if features.style_eligible { - accumulator.add(message, &features); - } - } - conn.execute( - "DELETE FROM owner_style_profile WHERE chat = ?1 AND source = ?2 AND user_name = ?3", - params![chat, source, STYLE_USER], - )?; - if let Some(profile) = accumulator.finish(chat, source, STYLE_USER) { - insert_style_profile(conn, &profile, &Utc::now().to_rfc3339())?; - } - Ok(sample_count) -} - -fn owner_style_timestamp(value: &str) -> Option { - chrono::NaiveDateTime::parse_from_str(value.trim(), "%Y-%m-%d %H:%M:%S") - .ok() - .map(|naive| naive.and_utc().timestamp()) - .or_else(|| { - chrono::DateTime::parse_from_rfc3339(value.trim()) - .ok() - .map(|value| value.timestamp()) - }) -} - -/// Mark owner style samples that the assistant itself sent as ineligible and -/// rebuild the aggregates they used to feed. -/// -/// The assistant writes into the room through the owner's own account, so each -/// confirmed outgoing reply also appears in the local database as a -/// self-authored row. Ingest blocks new ones, but rows recorded before that -/// guard are already mixed into `owner_style`, its profile, the response-time -/// samples and the recipient profile. A row is repaired only for a unique, -/// exact-text confirmed send inside [`SELF_SEND_REPAIR_WINDOW_SECONDS`]; a -/// many-to-one match stays untouched (fail closed). -pub fn repair_bot_sent_style_samples( - db_path: &Path, - chat: &str, - chat_id: i64, - confirmed_sends: &[ConfirmedSelfSend], - dry_run: bool, -) -> Result { - if chat.trim().is_empty() || chat.len() > LIVE_CONTEXT_MAX_FIELD_BYTES { - anyhow::bail!("style repair chat is invalid"); - } - if chat_id <= 0 { - anyhow::bail!("style repair chat id is invalid"); - } - let mut conn = open_db(db_path)?; - let rows = { - let mut stmt = conn.prepare( - "SELECT id, source, date, message, content_kind, style_eligible, source_row, - features_json - FROM owner_style WHERE chat = ?1 ORDER BY id ASC", - )?; - let rows = stmt.query_map(params![chat], |row| { - Ok(( - row.get::<_, i64>(0)?, - row.get::<_, String>(1)?, - row.get::<_, String>(2)?, - row.get::<_, String>(3)?, - row.get::<_, String>(4)?, - row.get::<_, i64>(5)?, - row.get::<_, i64>(6)?, - row.get::<_, String>(7)?, - )) - })?; - rows.collect::>>()? - }; - - let mut report = SelfSendRepairReport { - schema_version: 1, - chat: chat.to_string(), - chat_id, - confirmed_sends: confirmed_sends.len(), - scanned_rows: rows.len(), - matched_rows: 0, - ambiguous_rows: 0, - already_ineligible: 0, - deleted_response_samples: 0, - deleted_recipient_samples: 0, - rebuilt_sources: Vec::new(), - dry_run, - matches: Vec::new(), - }; - let mut affected_sources: BTreeMap = BTreeMap::new(); - let tx = conn.transaction()?; - for (style_id, source, date, message, content_kind, eligible, source_row, features_json) in rows - { - let Some(sent_at) = owner_style_timestamp(&date) else { - continue; - }; - let (matched, ambiguous) = match_confirmed_send(confirmed_sends, &message, sent_at); - if ambiguous { - report.ambiguous_rows += 1; - continue; - } - let Some(send) = matched else { - continue; - }; - let offset = sent_at - send.sent_at; - report.matched_rows += 1; - if eligible == 0 { - report.already_ineligible += 1; - } - report.matches.push(SelfSendRepairMatch { - source: source.clone(), - log_id: source_row, - date: date.clone(), - message: message.clone(), - content_kind: content_kind.clone(), - style_eligible_before: eligible != 0, - matched_event_id: send.event_id.clone(), - offset_seconds: offset, - }); - if dry_run { - continue; - } - tx.execute( - "UPDATE owner_style - SET style_eligible = 0, content_kind = 'bot_sent_reply', features_json = ?2 - WHERE id = ?1", - params![ - style_id, - mark_bot_sent_features(&features_json, &send.event_id, offset) - ], - )?; - let deleted_recipients = tx.execute( - "DELETE FROM owner_recipient_style_samples WHERE style_message_id = ?1", - params![style_id], - )?; - report.deleted_recipient_samples += deleted_recipients; - let deleted_samples = if source_row > 0 { - tx.execute( - "DELETE FROM response_time_samples - WHERE source = ?1 AND chat_id = ?2 AND reply_log_id = ?3", - params![source, chat_id, source_row], - )? - } else { - 0 - }; - report.deleted_response_samples += deleted_samples; - // A source is stale whenever anything actually changed, including a row - // that was already style-ineligible: its response-time samples were just - // deleted, so the aggregates derived from them must be regenerated too. - // An imported source is always re-derived: it keeps no sample rows, so a - // bot reply that was measured before the guard existed lives only inside - // its aggregate and nothing else can remove it. - let is_live_source = tx - .query_row( - "SELECT 1 FROM context_sources WHERE source = ?1 AND chat_id = ?2", - params![source, chat_id], - |_| Ok(true), - ) - .optional()? - .unwrap_or(false); - if !is_live_source || eligible != 0 || deleted_samples > 0 || deleted_recipients > 0 { - affected_sources.insert(source.clone(), true); - } - } - - if !dry_run { - for source in affected_sources.keys() { - let source_chat_id = tx - .query_row( - "SELECT chat_id FROM context_sources WHERE source = ?1 AND chat_id = ?2", - params![source, chat_id], - |row| row.get::<_, i64>(0), - ) - .optional()? - .or(tx - .query_row( - "SELECT chat_id FROM context_sources WHERE source = ?1 LIMIT 1", - params![source], - |row| row.get::<_, i64>(0), - ) - .optional()?) - .unwrap_or(0); - if source_chat_id > 0 { - refresh_live_context_summaries(&tx, source, source_chat_id, chat)?; - } else { - recompute_imported_response_time_stats(&tx, source, chat, confirmed_sends)?; - } - report.rebuilt_sources.push(source.clone()); - } - } - tx.commit()?; - Ok(report) -} - -fn live_source_has_required_summaries(conn: &Connection, source: &str, chat: &str) -> Result { - let style_samples = conn - .query_row( - "SELECT sample_count FROM owner_style_profile - WHERE chat = ?1 AND source = ?2 AND user_name = ?3 - AND policy_version = ?4", - params![chat, source, STYLE_USER, STYLE_POLICY_VERSION], - |row| row.get::<_, i64>(0), - ) - .optional()? - .unwrap_or(0); - let timing_samples = conn - .query_row( - "SELECT sample_count FROM response_time_stats - WHERE chat = ?1 AND source = ?2 AND user_name = ?3", - params![chat, source, STYLE_USER], - |row| row.get::<_, i64>(0), - ) - .optional()? - .unwrap_or(0); - Ok(style_samples > 0 && timing_samples >= 2) -} - -/// Atomically ingest one bounded `local-poll` page into the live context index. -/// -/// `expected_checkpoint` must be the `after_log_id` used for the source poll. -/// Replaying a committed page is idempotent when every event digest matches. -/// `batch_complete` should only be true for a snapshot-complete page; expensive -/// style/timing summaries are refreshed at that boundary. Promotion makes the -/// complete live source take precedence over legacy CSV sources for the chat. -#[allow(clippy::too_many_arguments)] -pub fn ingest_live_context_events( - db_path: &Path, - account_fingerprint: &str, - chat_id: i64, - chat: &str, - expected_checkpoint: i64, - events: &[LiveContextEvent], - batch_complete: bool, - promote_authoritative: bool, -) -> Result { - validate_live_context_batch( - chat_id, - chat, - expected_checkpoint, - events, - batch_complete, - promote_authoritative, - )?; - let fingerprint = account_fingerprint.trim().to_ascii_lowercase(); - let source_id = live_context_source_id(&fingerprint, chat_id)?; - let mut conn = open_db(db_path)?; - let tx = conn.transaction()?; - let now = Utc::now().to_rfc3339(); - let mut source = match load_live_source(&tx, &source_id)? { - Some(source) => source, - None => { - tx.execute( - "INSERT INTO context_sources( - source, kind, account_fingerprint, chat_id, chat, authoritative, - checkpoint_log_id, pending_burst_json, summary_dirty, sync_status, - updated_at - ) VALUES (?1, 'local_db', ?2, ?3, ?4, 0, ?5, '[]', 0, 'ready', ?6)", - params![ - source_id, - fingerprint, - chat_id, - chat, - expected_checkpoint, - now, - ], - )?; - LiveSourceRow { - account_fingerprint: fingerprint.clone(), - chat_id, - chat: chat.to_string(), - authoritative: false, - checkpoint_log_id: expected_checkpoint, - pending_human_log_id: None, - pending_human_sent_at: None, - pending_human_name: None, - pending_burst: Vec::new(), - summary_dirty: false, - } - } - }; - // 이름 없는 그룹방은 '그룹:' 자리표시자로 저장된다. 화면 이름으로 동기화해도 - // 같은 방이므로 통과시킨다. - let placeholder = format!("그룹:{chat_id}"); - let name_matches = source.chat == chat || source.chat == placeholder; - if source.account_fingerprint != fingerprint || source.chat_id != chat_id || !name_matches { - anyhow::bail!("live context source identity changed"); - } - if source.checkpoint_log_id < expected_checkpoint { - anyhow::bail!("live context checkpoint is behind the caller checkpoint"); - } - - let mut inserted_events = 0usize; - let mut duplicate_events = 0usize; - let mut indexed_messages = 0usize; - let mut style_messages = 0usize; - let mut response_samples = 0usize; - let mut recipient_style_samples = 0usize; - let mut summaries_changed = source.summary_dirty; - - for event in events { - let digest = live_event_digest(event)?; - let existing_digest = tx - .query_row( - "SELECT message_digest FROM context_live_events - WHERE source = ?1 AND chat_id = ?2 AND log_id = ?3", - params![source_id, chat_id, event.log_id], - |row| row.get::<_, String>(0), - ) - .optional()?; - if let Some(existing_digest) = existing_digest { - if existing_digest != digest { - anyhow::bail!("live context replay digest mismatch"); - } - if event.log_id > source.checkpoint_log_id { - anyhow::bail!("live context event exists beyond its checkpoint"); - } - duplicate_events += 1; - continue; - } - if event.log_id <= source.checkpoint_log_id { - anyhow::bail!("live context checkpoint skipped an unrecorded event"); - } - - let date = live_event_date(event.sent_at)?; - let sender = event.sender_name.trim(); - let message = event.message.trim(); - let mut context_message_id = None; - let mut style_message_id = None; - let disposition; - - if event.auto_generated || event.exclude_from_learning { - disposition = if event.auto_generated { - "auto_generated" - } else { - "ambiguous_auto_candidate" - }; - clear_pending_participant_burst(&mut source); - } else if sender.is_empty() { - disposition = "unknown_author"; - clear_pending_participant_burst(&mut source); - } else { - let index_text = live_index_text(message, &event.attachment, event.message_type); - let should_index = !index_text.is_empty() - && (!event.interest_only || message_has_interest_topic(&index_text)); - if should_index { - tx.execute( - "INSERT INTO context_messages( - source, chat, date, user_name, message, vector - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6)", - params![ - source_id, - chat, - date, - sender, - index_text, - vector_to_bytes(&encode_vector(&format!("{sender} {index_text}"))), - ], - )?; - context_message_id = Some(tx.last_insert_rowid()); - insert_message_topics( - &tx, - context_message_id.unwrap_or(0), - chat, - &source_id, - &date, - &index_text, - )?; - indexed_messages += 1; - } - - if event.is_self { - disposition = if !should_index { - "filtered_noise" - } else if message.is_empty() { - "empty" - } else { - "style" - }; - if should_index && !message.is_empty() { - let features = classify_style_message(message); - tx.execute( - "INSERT INTO owner_style( - source, chat, date, user_name, message, vector, source_row, - content_kind, style_eligible, policy_version, features_json - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", - params![ - source_id, - chat, - date, - STYLE_USER, - message, - vector_to_bytes(&encode_vector(message)), - event.log_id, - features.content_kind, - features.style_eligible as i64, - STYLE_POLICY_VERSION, - features.features_json, - ], - )?; - style_message_id = Some(tx.last_insert_rowid()); - style_messages += 1; - summaries_changed = true; - - if let (Some(prompt_log_id), Some(prompt_sent_at), Some(recipient)) = ( - source.pending_human_log_id, - source.pending_human_sent_at, - source.pending_human_name.as_deref(), - ) { - let delay = event.sent_at - prompt_sent_at; - if (0..=MAX_RESPONSE_DELAY_SECONDS).contains(&delay) { - response_samples += tx.execute( - "INSERT OR IGNORE INTO response_time_samples( - source, chat_id, reply_log_id, prompt_log_id, - recipient, delay_seconds - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6)", - params![ - source_id, - chat_id, - event.log_id, - prompt_log_id, - recipient, - delay as f64, - ], - )?; - } - } - if features.style_eligible { - let confidences = recipient_confidences(&source.pending_burst); - let burst_size = confidences.len(); - for (recipient, confidence) in confidences { - recipient_style_samples += tx.execute( - "INSERT OR IGNORE INTO owner_recipient_style_samples( - source, chat_id, reply_log_id, recipient, style_message_id, - burst_size, confidence - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", - params![ - source_id, - chat_id, - event.log_id, - recipient, - style_message_id, - burst_size as i64, - confidence, - ], - )?; - } - } - clear_pending_participant_burst(&mut source); - } - } else if is_response_participant(sender) { - disposition = if message.is_empty() { - "empty" - } else { - "context" - }; - if !message.is_empty() { - source.pending_human_log_id = Some(event.log_id); - source.pending_human_sent_at = Some(event.sent_at); - source.pending_human_name = Some(sender.to_string()); - source.pending_burst.push(PendingRecipient { - recipient: sender.to_string(), - log_id: event.log_id, - }); - if source.pending_burst.len() > LIVE_CONTEXT_MAX_PENDING_RECIPIENTS { - let remove = - source.pending_burst.len() - LIVE_CONTEXT_MAX_PENDING_RECIPIENTS; - source.pending_burst.drain(0..remove); - } - } - } else { - disposition = if message.is_empty() { - "empty" - } else { - "context" - }; - clear_pending_participant_burst(&mut source); - } - } - - tx.execute( - "INSERT INTO context_live_events( - source, chat_id, log_id, sent_at, sender_name, message_digest, - disposition, auto_generated, context_message_id, style_message_id, - created_at - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", - params![ - source_id, - chat_id, - event.log_id, - event.sent_at, - sender, - digest, - disposition, - event.auto_generated as i64, - context_message_id, - style_message_id, - now, - ], - )?; - source.checkpoint_log_id = event.log_id; - inserted_events += 1; - } - - let summary_refreshed = batch_complete && summaries_changed; - if summary_refreshed { - refresh_live_context_summaries(&tx, &source_id, chat_id, chat)?; - source.summary_dirty = false; - } else { - source.summary_dirty = summaries_changed; - } - if promote_authoritative { - if !live_source_has_required_summaries(&tx, &source_id, chat)? { - anyhow::bail!("live context source lacks required style or timing samples"); - } - tx.execute( - "UPDATE context_sources SET authoritative = 0 - WHERE chat = ?1 AND source <> ?2", - params![chat, source_id], - )?; - source.authoritative = true; - } - tx.execute( - "UPDATE context_sources - SET authoritative = ?2, checkpoint_log_id = ?3, - pending_human_log_id = ?4, pending_human_sent_at = ?5, - pending_human_name = ?6, pending_burst_json = ?7, - summary_dirty = ?8, sync_status = ?9, updated_at = ?10 - WHERE source = ?1", - params![ - source_id, - source.authoritative as i64, - source.checkpoint_log_id, - source.pending_human_log_id, - source.pending_human_sent_at, - source.pending_human_name, - serde_json::to_string(&source.pending_burst)?, - source.summary_dirty as i64, - if batch_complete { "ready" } else { "partial" }, - now, - ], - )?; - tx.commit()?; - Ok(LiveContextIngestResult { - source: source_id, - chat_id, - chat: chat.to_string(), - checkpoint_log_id: source.checkpoint_log_id, - inserted_events, - duplicate_events, - indexed_messages, - style_messages, - response_samples, - recipient_style_samples, - summary_refreshed, - authoritative: source.authoritative, - }) -} - -pub fn live_context_sync_state( - db_path: &Path, - account_fingerprint: &str, - chat_id: i64, -) -> Result> { - let source = live_context_source_id(account_fingerprint, chat_id)?; - let conn = open_db_readonly(db_path)?; - let has_schema = conn - .query_row( - "SELECT value FROM context_retrieval_meta WHERE key = 'live_context_schema'", - [], - |row| row.get::<_, String>(0), - ) - .optional()? - .as_deref() - == Some(LIVE_CONTEXT_SCHEMA_VERSION); - if !has_schema { - anyhow::bail!("live context index migration required"); - } - conn.query_row( - "SELECT source, chat_id, chat, checkpoint_log_id, authoritative, - summary_dirty, sync_status - FROM context_sources WHERE source = ?1", - [source], - |row| { - Ok(LiveContextSyncState { - source: row.get(0)?, - chat_id: row.get(1)?, - chat: row.get(2)?, - checkpoint_log_id: row.get(3)?, - authoritative: row.get::<_, i64>(4)? != 0, - summary_dirty: row.get::<_, i64>(5)? != 0, - sync_status: row.get(6)?, - }) - }, - ) - .optional() - .map_err(Into::into) -} - -pub fn index_csv(db_path: &Path, chat: &str, input: &Path) -> Result { - index_csv_with_queue(db_path, chat, input, None) -} - -/// Import one chat history CSV, excluding replies the assistant itself sent. -/// -/// A CSV row carries no room log id, so the send authorities are matched by -/// exact text inside [`SELF_SEND_REPAIR_WINDOW_SECONDS`]: the decision ledger -/// plus, when the caller can name it, the room delivery queue. Both are needed -/// because a reply that only the queue recorded as `sent` would otherwise be -/// learned as an owner sample on the next import. -pub fn index_csv_with_queue( - db_path: &Path, - chat: &str, - input: &Path, - queue_path: Option<&Path>, -) -> Result { - if chat.trim().is_empty() { - anyhow::bail!("chat name must not be empty"); - } - let source = input - .canonicalize() - .with_context(|| format!("failed to resolve CSV {}", input.display()))? - .display() - .to_string(); - let conn = open_db(db_path)?; - let mut reader = ReaderBuilder::new() - .flexible(true) - .from_path(input) - .with_context(|| format!("failed to open CSV {}", input.display()))?; - let headers = reader.headers().context("CSV has no header row")?.clone(); - let date_idx = headers - .iter() - .position(|h| h.trim_start_matches('\u{feff}') == "Date"); - let user_idx = headers.iter().position(|h| h == "User"); - let message_idx = headers.iter().position(|h| h == "Message"); - let (Some(date_idx), Some(user_idx), Some(message_idx)) = (date_idx, user_idx, message_idx) - else { - anyhow::bail!("CSV must contain Date, User, and Message columns"); - }; - let required = date_idx.max(user_idx).max(message_idx); - // A bot reply that was already confirmed must not be re-learned as an owner - // style sample when the chat history is imported from CSV. - let mut confirmed_sends = confirmed_self_sends_with_connection(&conn, chat).unwrap_or_default(); - if let Some(queue) = queue_path { - for send in confirmed_self_sends_from_queue(queue)? { - if !confirmed_sends - .iter() - .any(|existing| existing.event_id == send.event_id) - { - confirmed_sends.push(send); - } - } - } - let tx = conn.unchecked_transaction()?; - tx.execute("DELETE FROM context_messages WHERE source = ?1", [&source])?; - tx.execute("DELETE FROM owner_style WHERE source = ?1", [&source])?; - tx.execute( - "DELETE FROM owner_style_profile WHERE source = ?1", - [&source], - )?; - let mut previous_human_at: Option = None; - let mut response_delays = Vec::new(); - let mut count = 0; - let mut profile_accumulator = StyleProfileAccumulator::default(); - for (source_row, row) in reader.records().enumerate() { - let row = row.context("invalid CSV record")?; - if row.len() <= required { - anyhow::bail!("CSV record has fewer fields than its header"); - } - let message = row.get(message_idx).unwrap_or_default().trim().to_string(); - if message.is_empty() { - continue; - } - let date = row.get(date_idx).unwrap_or_default().to_string(); - let user = row.get(user_idx).unwrap_or_default().to_string(); - let row_sent_at = parse_chat_date(&date).map(|value| value.and_utc().timestamp()); - // Identify a bot-sent row *before* it can become style, pacing or - // recipient evidence. A confirmed send is not a human reply, so it must - // not produce a response-time sample either. - let bot_send = if user == STYLE_USER { - row_sent_at.and_then(|sent_at| { - let (matched, ambiguous) = - match_confirmed_send(&confirmed_sends, &message, sent_at); - matched - .filter(|_| !ambiguous) - .map(|send| (send, sent_at - send.sent_at)) - }) - } else { - None - }; - if let Some(current_at) = parse_chat_date(&date) { - if user == STYLE_USER { - if bot_send.is_some() { - // Drop the pending prompt instead of pairing a later human - // reply with it: this turn was the assistant, not the owner. - previous_human_at = None; - } else if let Some(previous_at) = previous_human_at.take() { - let delay = (current_at - previous_at).num_seconds(); - if (0..=MAX_RESPONSE_DELAY_SECONDS).contains(&delay) { - response_delays.push(delay as f64); - } - } - } else if is_response_participant(&user) { - previous_human_at = Some(current_at); - } - } - let vector = encode_vector(&format!("{} {}", user, message)); - // `tx.execute` re-parses this INSERT on every CSV row; the prepared - // statement cache keeps the parse cost to one per connection. - tx.prepare_cached("INSERT INTO context_messages(source, chat, date, user_name, message, vector) VALUES (?1, ?2, ?3, ?4, ?5, ?6)") - .and_then(|mut stmt| { - stmt.execute(params![source, chat, date, user, message, vector_to_bytes(&vector)]) - })?; - insert_message_topics(&tx, tx.last_insert_rowid(), chat, &source, &date, &message)?; - if user == STYLE_USER { - let mut features = classify_style_message(&message); - if let Some((send, offset)) = bot_send { - features.style_eligible = false; - features.content_kind = "bot_sent_reply"; - features.features_json = - mark_bot_sent_features(&features.features_json, &send.event_id, offset); - } - tx.prepare_cached( - "INSERT INTO owner_style(source, chat, date, user_name, message, vector, source_row, content_kind, style_eligible, policy_version, features_json) - VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", - ) - .and_then(|mut stmt| { - stmt.execute(params![ - source, - chat, - date, - user, - message, - vector_to_bytes(&encode_vector(&message)), - source_row as i64 + 1, - features.content_kind, - features.style_eligible as i64, - STYLE_POLICY_VERSION, - features.features_json, - ]) - })?; - if features.style_eligible { - profile_accumulator.add(&message, &features); - } - } - count += 1; - } - tx.execute( - "DELETE FROM response_time_stats WHERE chat = ?1 AND source = ?2 AND user_name = ?3", - params![chat, source, STYLE_USER], - )?; - if let Some(profile) = profile_accumulator.finish(chat, &source, STYLE_USER) { - tx.execute( - "INSERT INTO owner_style_profile( - chat, source, user_name, sample_count, average_character_length, - median_character_length, p90_character_length, casual_ending_count, - casual_ending_counts_json, question_count, emoji_count, punctuation_count, - common_endings_json, common_tokens_json, policy_version, created_at - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16)", - params![ - profile.chat, - profile.source, - profile.user, - profile.sample_count as i64, - profile.average_character_length, - profile.median_character_length, - profile.p90_character_length, - profile.casual_ending_count as i64, - profile.casual_ending_counts_json, - profile.question_count as i64, - profile.emoji_count as i64, - profile.punctuation_count as i64, - profile.common_endings_json, - profile.common_tokens_json, - profile.policy_version, - Utc::now().to_rfc3339(), - ], - )?; - } - if let Some(stats) = summarize_response_delays(chat, &source, STYLE_USER, response_delays) { - tx.execute( - "INSERT INTO response_time_stats(chat, source, user_name, sample_count, average_seconds, median_seconds, p90_seconds, min_seconds, max_seconds, max_window_seconds, stddev_seconds, distribution_schema_version, distribution_json) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)", - params![ - stats.chat, - stats.source, - stats.user, - stats.sample_count as i64, - stats.average_seconds, - stats.median_seconds, - stats.p90_seconds, - stats.min_seconds, - stats.max_seconds, - stats.max_window_seconds, - stats.stddev_seconds, - stats - .distribution - .as_ref() - .map(|distribution| distribution.schema_version as i64) - .unwrap_or(0), - stats - .distribution - .as_ref() - .map(serde_json::to_string) - .transpose()? - .unwrap_or_else(|| "{}".to_string()), - ], - )?; - } - tx.commit()?; - rebuild_retrieval_index(&conn)?; - checkpoint_context_wal_truncate(&conn)?; - set_context_db_permissions(db_path)?; - Ok(count) -} -fn response_delay_percentile(sorted: &[f64], ratio: f64) -> f64 { - let position = (sorted.len() - 1) as f64 * ratio; - let lower = position.floor() as usize; - let upper = position.ceil() as usize; - if lower == upper { - sorted[lower] - } else { - sorted[lower] + (sorted[upper] - sorted[lower]) * (position - lower as f64) - } -} - -fn response_time_segment_sse( - prefix_sum: &[f64], - prefix_square_sum: &[f64], - start: usize, - end: usize, -) -> f64 { - let count = (end - start) as f64; - let sum = prefix_sum[end] - prefix_sum[start]; - let square_sum = prefix_square_sum[end] - prefix_square_sum[start]; - (square_sum - sum * sum / count).max(0.0) -} - -/// Smallest admissible `second_split` for every `first_candidates` entry. -/// -/// The result is the index into `second_candidates` of the smallest minimiser -/// of `sse(first, s) + sse(s, total)`, or `None` when the strict-increase -/// window `[first + minimum, total - minimum]` holds no admissible boundary. -/// -/// The smallest constrained minimiser is monotone non-decreasing in -/// `first_split` because the segment SSE satisfies the quadrangle inequality -/// sse(a, c) + sse(b, d) <= sse(a, d) + sse(b, c) for a <= b <= c <= d, -/// so divide-and-conquer optimisation is exact here. A ternary search over the -/// middle boundary would not be: the objective is not unimodal, and a shape as -/// ordinary as a dense uniform spread already has a local minimum that is not -/// the global one. -fn response_time_distribution_second_splits( - prefix_sum: &[f64], - prefix_square_sum: &[f64], - first_candidates: &[usize], - second_candidates: &[usize], - total: usize, - minimum: usize, -) -> Vec> { - #[allow(clippy::too_many_arguments)] - // The loop variable is the answer, not an iteration counter: the winning - // position is stored in optima and reused as a boundary bound in the - // recursive calls, so enumerating the slice would only rename it. - #[allow(clippy::needless_range_loop)] - fn search( - prefix_sum: &[f64], - prefix_square_sum: &[f64], - first_candidates: &[usize], - second_candidates: &[usize], - total: usize, - minimum: usize, - first_start: usize, - first_end: usize, - second_start: usize, - second_end: usize, - optima: &mut [Option], - ) { - if first_start > first_end { - return; - } - if second_start > second_end { - // No admissible boundary is left for this first_split or any later - // one, because the lower bound only moves right with first_split. - for slot in &mut optima[first_start..=first_end] { - *slot = None; - } - return; - } - let first_midpoint = first_start + (first_end - first_start) / 2; - let first_split = first_candidates[first_midpoint]; - let lower = second_candidates.partition_point(|boundary| *boundary < first_split + minimum); - let scan_start = second_start.max(lower); - let mut best: Option<(f64, usize)> = None; - for position in scan_start..=second_end { - let second_split = second_candidates[position]; - let sse = response_time_segment_sse(prefix_sum, prefix_square_sum, 0, first_split) - + response_time_segment_sse( - prefix_sum, - prefix_square_sum, - first_split, - second_split, - ) - + response_time_segment_sse(prefix_sum, prefix_square_sum, second_split, total); - if best.is_none_or(|(best_sse, _)| sse < best_sse) { - best = Some((sse, position)); - } - } - optima[first_midpoint] = best.map(|(_, position)| position); - match best { - Some((_, best_position)) => { - if first_midpoint > first_start { - search( - prefix_sum, - prefix_square_sum, - first_candidates, - second_candidates, - total, - minimum, - first_start, - first_midpoint - 1, - second_start, - best_position, - optima, - ); - } - if first_midpoint < first_end { - search( - prefix_sum, - prefix_square_sum, - first_candidates, - second_candidates, - total, - minimum, - first_midpoint + 1, - first_end, - best_position, - second_end, - optima, - ); - } - } - None => { - if first_midpoint < first_end { - for slot in &mut optima[first_midpoint + 1..=first_end] { - *slot = None; - } - } - if first_midpoint > first_start { - search( - prefix_sum, - prefix_square_sum, - first_candidates, - second_candidates, - total, - minimum, - first_start, - first_midpoint - 1, - second_start, - second_end, - optima, - ); - } - } - } - } - let mut optima = vec![None; first_candidates.len()]; - if !first_candidates.is_empty() && !second_candidates.is_empty() { - search( - prefix_sum, - prefix_square_sum, - first_candidates, - second_candidates, - total, - minimum, - 0, - first_candidates.len() - 1, - 0, - second_candidates.len() - 1, - &mut optima, - ); - } - optima -} - -fn fit_response_time_distribution(delays: &[f64]) -> Option { - if delays.len() < RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES - || delays.iter().any(|delay| { - !delay.is_finite() || !(0.0..=MAX_RESPONSE_DELAY_SECONDS as f64).contains(delay) - }) - { - return None; - } - let transformed = delays.iter().map(|delay| delay.ln_1p()).collect::>(); - let mut prefix_sum = vec![0.0; transformed.len() + 1]; - let mut prefix_square_sum = vec![0.0; transformed.len() + 1]; - for (index, value) in transformed.iter().enumerate() { - prefix_sum[index + 1] = prefix_sum[index] + value; - prefix_square_sum[index + 1] = prefix_square_sum[index] + value * value; - } - let segment_sse = |start: usize, end: usize| { - response_time_segment_sse(&prefix_sum, &prefix_square_sum, start, end) - }; - // Exact three-way split search in O(N log N) instead of the O(N^2) pair - // scan, with an identical result. The per-first_split objective - // g(s) = sse(first, s) + sse(s, N) - // is not unimodal, so a ternary or gradient walk does not find its minimum; - // the quadrangle inequality of the segment SSE is what makes the smallest - // minimiser monotone in first_split and divide-and-conquer exact. Walking - // first_split ascending while replacing only on a strict improvement keeps - // the exhaustive scan's lexicographically smallest `(first, second)` tie: - // the earliest improving first_split wins with its own smallest second_split. - let minimum = RESPONSE_TIME_DISTRIBUTION_MIN_COMPONENT_SAMPLES; - let total = delays.len(); - let first_candidates = (minimum..=total - 2 * minimum) - .filter(|split| delays[split - 1] < delays[*split]) - .collect::>(); - let second_candidates = (minimum..=total - minimum) - .filter(|split| delays[split - 1] < delays[*split]) - .collect::>(); - let optima = response_time_distribution_second_splits( - &prefix_sum, - &prefix_square_sum, - &first_candidates, - &second_candidates, - total, - minimum, - ); - let mut best: Option<(f64, usize, usize)> = None; - for (position, first_split) in first_candidates.into_iter().enumerate() { - let Some(second_position) = optima[position] else { - continue; - }; - let second_split = second_candidates[second_position]; - let sse = segment_sse(0, first_split) - + segment_sse(first_split, second_split) - + segment_sse(second_split, total); - if best.as_ref().is_none_or(|(best_sse, _, _)| sse < *best_sse) { - best = Some((sse, first_split, second_split)); - } - } - let (_, first_split, second_split) = best?; - let global_upper_seconds = response_delay_percentile(delays, 0.9); - let immediate_upper_seconds = delays[first_split - 1].min(global_upper_seconds); - let short_lower_seconds = delays[first_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); - let short_upper_seconds = delays[second_split - 1].min(global_upper_seconds); - let delayed_lower_seconds = delays[second_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); - if immediate_upper_seconds < MIN_SCHEDULED_RESPONSE_DELAY_SECONDS - || short_lower_seconds > short_upper_seconds - || delayed_lower_seconds > global_upper_seconds - || immediate_upper_seconds >= short_lower_seconds - || short_upper_seconds >= delayed_lower_seconds - { - return None; - } - - let bounds = [ - ( - MIN_SCHEDULED_RESPONSE_DELAY_SECONDS, - immediate_upper_seconds, - ), - (short_lower_seconds, short_upper_seconds), - (delayed_lower_seconds, global_upper_seconds), - ]; - let ranges = [ - (0, first_split), - (first_split, second_split), - (second_split, delays.len()), - ]; - let mut components = Vec::with_capacity(RESPONSE_TIME_DISTRIBUTION_COMPONENTS); - for ((start, end), ((lower_seconds, upper_seconds), name)) in ranges - .into_iter() - .zip(bounds.into_iter().zip(["immediate", "short", "delayed"])) - { - let values = delays[start..end] - .iter() - .map(|delay| delay.clamp(lower_seconds, upper_seconds)) - .collect::>(); - let mean = values.iter().sum::() / values.len() as f64; - let stddev = (values - .iter() - .map(|value| (value - mean).powi(2)) - .sum::() - / values.len() as f64) - .sqrt(); - if !stddev.is_finite() || stddev <= 0.0 { - return None; - } - components.push(ResponseTimeComponent { - name: name.to_string(), - sample_count: values.len(), - weight: values.len() as f64 / delays.len() as f64, - normal_location_seconds: mean, - normal_scale_seconds: stddev, - lower_seconds, - upper_seconds, - }); - } - let distribution = ResponseTimeDistribution { - schema_version: RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION, - policy_version: RESPONSE_TIME_DISTRIBUTION_POLICY_VERSION.to_string(), - model_kind: RESPONSE_TIME_DISTRIBUTION_MODEL_KIND.to_string(), - fit_transform: RESPONSE_TIME_DISTRIBUTION_FIT_TRANSFORM.to_string(), - sample_count: delays.len(), - retained_sample_count: delays.len(), - tail_winsorized_count: delays - .iter() - .filter(|delay| **delay > global_upper_seconds) - .count(), - split_seconds: vec![immediate_upper_seconds, short_upper_seconds], - global_upper_seconds, - components, - }; - response_time_distribution_is_valid(&distribution).then_some(distribution) -} - -fn response_time_distribution_is_valid(distribution: &ResponseTimeDistribution) -> bool { - if distribution.schema_version != RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION - || distribution.policy_version != RESPONSE_TIME_DISTRIBUTION_POLICY_VERSION - || distribution.model_kind != RESPONSE_TIME_DISTRIBUTION_MODEL_KIND - || distribution.fit_transform != RESPONSE_TIME_DISTRIBUTION_FIT_TRANSFORM - || distribution.sample_count < RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES - || distribution.retained_sample_count != distribution.sample_count - || distribution.tail_winsorized_count >= distribution.sample_count - || distribution.components.len() != RESPONSE_TIME_DISTRIBUTION_COMPONENTS - || distribution.split_seconds.len() != RESPONSE_TIME_DISTRIBUTION_COMPONENTS - 1 - || distribution - .split_seconds - .iter() - .any(|split| !split.is_finite()) - || !distribution.global_upper_seconds.is_finite() - || distribution.split_seconds[0] < MIN_SCHEDULED_RESPONSE_DELAY_SECONDS - || distribution.global_upper_seconds > MAX_RESPONSE_DELAY_SECONDS as f64 - || distribution.split_seconds[0] >= distribution.split_seconds[1] - || distribution.split_seconds[1] >= distribution.global_upper_seconds - { - return false; - } - let expected_names = ["immediate", "short", "delayed"]; - let mut represented_samples = 0usize; - let mut total_weight = 0.0; - for (component, expected_name) in distribution.components.iter().zip(expected_names) { - if component.name != expected_name - || component.sample_count < RESPONSE_TIME_DISTRIBUTION_MIN_COMPONENT_SAMPLES - || !component.weight.is_finite() - || component.weight <= 0.0 - || !component.normal_location_seconds.is_finite() - || !component.normal_scale_seconds.is_finite() - || component.normal_scale_seconds <= 0.0 - || !component.lower_seconds.is_finite() - || !component.upper_seconds.is_finite() - || component.lower_seconds < MIN_SCHEDULED_RESPONSE_DELAY_SECONDS - || component.lower_seconds >= component.upper_seconds - || component.upper_seconds > distribution.global_upper_seconds - || !(component.lower_seconds..=component.upper_seconds) - .contains(&component.normal_location_seconds) - || (component.weight - component.sample_count as f64 / distribution.sample_count as f64) - .abs() - > 1e-12 - { - return false; - } - represented_samples = match represented_samples.checked_add(component.sample_count) { - Some(value) => value, - None => return false, - }; - total_weight += component.weight; - } - if represented_samples != distribution.sample_count - || (total_weight - 1.0).abs() > 1e-12 - || distribution.components[0].upper_seconds != distribution.split_seconds[0] - || distribution.components[1].upper_seconds != distribution.split_seconds[1] - || distribution.components[0].upper_seconds >= distribution.components[1].lower_seconds - || distribution.components[1].upper_seconds >= distribution.components[2].lower_seconds - { - return false; - } - distribution.global_upper_seconds - == distribution - .components - .iter() - .map(|component| component.upper_seconds) - .fold(0.0, f64::max) -} - -fn summarize_response_delays( - chat: &str, - source: &str, - user: &str, - mut delays: Vec, -) -> Option { - if delays.is_empty() - || delays.iter().any(|delay| { - !delay.is_finite() || !(0.0..=MAX_RESPONSE_DELAY_SECONDS as f64).contains(delay) - }) - { - return None; - } - delays.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal)); - let average_seconds = delays.iter().sum::() / delays.len() as f64; - let stddev_seconds = if delays.len() > 1 { - (delays - .iter() - .map(|delay| (delay - average_seconds).powi(2)) - .sum::() - / (delays.len() - 1) as f64) - .sqrt() - } else { - 0.0 - }; - let distribution = fit_response_time_distribution(&delays); - Some(ResponseTimeStats { - chat: chat.to_string(), - source: source.to_string(), - user: user.to_string(), - sample_count: delays.len(), - average_seconds, - median_seconds: response_delay_percentile(&delays, 0.5), - p90_seconds: response_delay_percentile(&delays, 0.9), - min_seconds: delays[0], - max_seconds: *delays.last().unwrap_or(&delays[0]), - max_window_seconds: MAX_RESPONSE_DELAY_SECONDS, - stddev_seconds, - distribution, - }) -} - -pub fn response_time_stats( - db_path: &Path, - chat: &str, - user: &str, - source: Option<&str>, -) -> Result> { - let conn = open_db_readonly(db_path)?; - ensure_retrieval_index_current(&conn)?; - response_time_stats_with_connection(&conn, chat, user, source) -} - -fn response_time_stats_with_connection( - conn: &Connection, - chat: &str, - user: &str, - source: Option<&str>, -) -> Result> { - ensure_retrieval_index_current(conn)?; - let preferred_source = preferred_context_source(conn, Some(chat), source)?; - let mut stmt = conn.prepare( - "SELECT chat, source, user_name, sample_count, average_seconds, median_seconds, p90_seconds, min_seconds, max_seconds, max_window_seconds, stddev_seconds, distribution_schema_version, distribution_json - FROM response_time_stats - WHERE chat = ?1 AND user_name = ?2 AND (?3 IS NULL OR source = ?3) - ORDER BY sample_count DESC - LIMIT 1", - )?; - match stmt.query_row(params![chat, user, preferred_source.as_deref()], |row| { - let distribution_schema_version = row.get::<_, i64>(11)?; - let distribution_json = row.get::<_, String>(12)?; - let distribution = - if distribution_schema_version == RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION as i64 { - serde_json::from_str::(&distribution_json) - .ok() - .filter(response_time_distribution_is_valid) - } else { - None - }; - Ok(ResponseTimeStats { - chat: row.get(0)?, - source: row.get(1)?, - user: row.get(2)?, - sample_count: row.get::<_, i64>(3)? as usize, - average_seconds: row.get(4)?, - median_seconds: row.get(5)?, - p90_seconds: row.get(6)?, - min_seconds: row.get(7)?, - max_seconds: row.get(8)?, - max_window_seconds: row.get(9)?, - stddev_seconds: row.get(10)?, - distribution, - }) - }) { - Ok(stats) => Ok(Some(stats)), - Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None), - Err(error) => Err(error.into()), - } -} -pub fn style_profile( - db_path: &Path, - chat: &str, - user: &str, - source: Option<&str>, -) -> Result> { - let conn = open_db_readonly(db_path)?; - ensure_retrieval_index_current(&conn)?; - style_profile_with_connection(&conn, chat, user, source, true) -} - -fn style_profile_with_connection( - conn: &Connection, - chat: &str, - user: &str, - source: Option<&str>, - require_policy: bool, -) -> Result> { - ensure_retrieval_index_current(conn)?; - let preferred_source = preferred_context_source(conn, Some(chat), source)?; - let mut stmt = conn.prepare( - "SELECT chat, source, user_name, sample_count, average_character_length, - median_character_length, p90_character_length, casual_ending_count, - casual_ending_counts_json, question_count, emoji_count, punctuation_count, - common_endings_json, common_tokens_json, policy_version - FROM owner_style_profile - WHERE chat = ?1 AND user_name = ?2 AND (?3 IS NULL OR source = ?3) - AND (?4 = 0 OR policy_version = ?5) - ORDER BY sample_count DESC, source ASC - LIMIT 1", - )?; - match stmt.query_row( - params![ - chat, - user, - preferred_source.as_deref(), - require_policy as i64, - STYLE_POLICY_VERSION - ], - |row| { - Ok(StyleProfile { - chat: row.get(0)?, - source: row.get(1)?, - user: row.get(2)?, - sample_count: row.get::<_, i64>(3)? as usize, - average_character_length: row.get(4)?, - median_character_length: row.get(5)?, - p90_character_length: row.get(6)?, - casual_ending_count: row.get::<_, i64>(7)? as usize, - casual_ending_counts_json: row.get(8)?, - question_count: row.get::<_, i64>(9)? as usize, - emoji_count: row.get::<_, i64>(10)? as usize, - punctuation_count: row.get::<_, i64>(11)? as usize, - common_endings_json: row.get(12)?, - common_tokens_json: row.get(13)?, - policy_version: row.get(14)?, - }) - }, - ) { - Ok(profile) => { - validate_style_profile_laughter(&profile, "style profile")?; - Ok(Some(profile)) - } - Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None), - Err(error) => Err(error.into()), - } -} - -fn preferred_context_source( - conn: &Connection, - chat: Option<&str>, - source: Option<&str>, -) -> Result> { - if let Some(source) = source { - return Ok(Some(source.to_string())); - } - let Some(chat) = chat else { - return Ok(None); - }; - let has_sources_table = conn.query_row( - "SELECT EXISTS( - SELECT 1 FROM sqlite_master - WHERE type = 'table' AND name = 'context_sources' - )", - [], - |row| row.get::<_, i64>(0), - )? != 0; - if !has_sources_table { - return Ok(None); - } - conn.query_row( - "SELECT source FROM context_sources - WHERE chat = ?1 AND authoritative = 1 - ORDER BY updated_at DESC, source ASC LIMIT 1", - [chat], - |row| row.get::<_, String>(0), - ) - .optional() - .map_err(Into::into) -} - -pub fn recipient_style_profile( - db_path: &Path, - chat: &str, - recipient: &str, - source: Option<&str>, -) -> Result> { - let conn = open_db_readonly(db_path)?; - recipient_style_profile_with_connection(&conn, chat, recipient, source) -} - -fn recipient_style_profile_with_connection( - conn: &Connection, - chat: &str, - recipient: &str, - source: Option<&str>, -) -> Result> { - if chat.trim().is_empty() || recipient.trim().is_empty() { - anyhow::bail!("recipient style chat and recipient must not be empty"); - } - ensure_retrieval_index_current(conn)?; - let preferred_source = preferred_context_source(conn, Some(chat), source)?; - let has_recipient_table = conn.query_row( - "SELECT EXISTS( - SELECT 1 FROM sqlite_master - WHERE type = 'table' AND name = 'owner_recipient_style_profile' - )", - [], - |row| row.get::<_, i64>(0), - )? != 0; - let direct = if has_recipient_table { - let mut stmt = conn.prepare( - "SELECT source, sample_count, confidence_sum, average_character_length, - median_character_length, p90_character_length, casual_ending_count, - casual_ending_counts_json, question_count, emoji_count, - punctuation_count, common_endings_json, common_tokens_json, - policy_version - FROM owner_recipient_style_profile - WHERE chat = ?1 AND recipient = ?2 - AND (?3 IS NULL OR source = ?3) - AND policy_version = ?4 - ORDER BY sample_count DESC, confidence_sum DESC, source ASC - LIMIT 1", - )?; - stmt.query_row( - params![ - chat, - recipient, - preferred_source.as_deref(), - STYLE_POLICY_VERSION - ], - |row| { - let source: String = row.get(0)?; - let sample_count = row.get::<_, i64>(1)? as usize; - let confidence_sum: f64 = row.get(2)?; - Ok(( - sample_count, - confidence_sum, - StyleProfile { - chat: chat.to_string(), - source, - user: STYLE_USER.to_string(), - sample_count, - average_character_length: row.get(3)?, - median_character_length: row.get(4)?, - p90_character_length: row.get(5)?, - casual_ending_count: row.get::<_, i64>(6)? as usize, - casual_ending_counts_json: row.get(7)?, - question_count: row.get::<_, i64>(8)? as usize, - emoji_count: row.get::<_, i64>(9)? as usize, - punctuation_count: row.get::<_, i64>(10)? as usize, - common_endings_json: row.get(11)?, - common_tokens_json: row.get(12)?, - policy_version: row.get(13)?, - }, - )) - }, - ) - .optional()? - } else { - None - }; - if let Some((_, _, profile)) = direct.as_ref() { - validate_style_profile_laughter(profile, "recipient style profile")?; - } - if let Some((sample_count, confidence_sum, profile)) = direct.as_ref() { - if *sample_count >= RECIPIENT_STYLE_MIN_SAMPLES - && confidence_sum.is_finite() - && *confidence_sum >= RECIPIENT_STYLE_MIN_CONFIDENCE - { - let (honorific, formality) = derive_style_profile_honorific_formality(profile); - return Ok(Some(RecipientStyleProfile { - recipient: recipient.to_string(), - direct_sample_count: *sample_count, - confidence_sum: *confidence_sum, - used_fallback: false, - honorific, - formality, - profile: profile.clone(), - })); - } - } - // 최소표본/최소신뢰도 미만이면 방 전체 폴백 프로필을 유지하고, 존댓말/격식은 - // 그 폴백 프로필의 통계에서 그대로 도출한다(R3.3). - let fallback = - style_profile_with_connection(conn, chat, STYLE_USER, preferred_source.as_deref(), true)?; - Ok(fallback.map(|profile| { - let (honorific, formality) = derive_style_profile_honorific_formality(&profile); - RecipientStyleProfile { - recipient: recipient.to_string(), - direct_sample_count: direct.as_ref().map(|value| value.0).unwrap_or(0), - confidence_sum: direct.as_ref().map(|value| value.1).unwrap_or(0.0), - used_fallback: true, - honorific, - formality, - profile, - } - })) -} - -pub fn recipient_style_profile_json( - db_path: &Path, - chat: &str, - recipient: &str, - source: Option<&str>, -) -> Result> { - match recipient_style_profile(db_path, chat, recipient, source)? { - Some(mut profile) => { - profile.profile.source = provenance_id(&profile.profile.source); - Ok(Some(serde_json::to_string(&profile)?)) - } - None => Ok(None), - } -} - -pub fn style_profile_json( - db_path: &Path, - chat: &str, - user: &str, - source: Option<&str>, -) -> Result> { - match style_profile(db_path, chat, user, source)? { - Some(mut profile) => { - profile.source = provenance_id(&profile.source); - Ok(Some(serde_json::to_string(&profile)?)) - } - None => Ok(None), - } -} -fn validate_reply_decision_input(input: &ReplyDecisionInput) -> Result<()> { - if input.event_id.trim().is_empty() - || input.chat.trim().is_empty() - || input.message.trim().is_empty() - { - anyhow::bail!("reply decision event_id, chat, and message must not be empty"); - } - if !matches!(input.decision.as_str(), "reply" | "skip") { - anyhow::bail!("reply decision must be 'reply' or 'skip'"); - } - if input.context_match_count > CONTEXT_KEYWORD_CANDIDATE_CAP - || input.style_match_count > STYLE_VECTOR_CANDIDATE_CAP - { - anyhow::bail!("reply decision match counts exceed retrieval caps"); - } - for (name, score, maximum) in [ - ( - "best_context_score", - input.best_context_score, - MAX_CONTEXT_RETRIEVAL_SCORE, - ), - ("best_style_score", input.best_style_score, 1.0), - ("prior_similarity", input.prior_similarity, 1.0), - ] { - if !score.is_finite() || !(0.0..=maximum).contains(&score) { - anyhow::bail!("reply decision {name} must be finite and in the range 0..={maximum}"); - } - } - if !input.scheduled_delay_seconds.is_finite() - || !(0.0..=(MAX_RESPONSE_DELAY_SECONDS as f64)).contains(&input.scheduled_delay_seconds) - { - anyhow::bail!( - "reply decision scheduled_delay_seconds must be finite and in the range 0..={MAX_RESPONSE_DELAY_SECONDS}" - ); - } - if input.evidence_ids.len() > MAX_REPLY_EVIDENCE_IDS { - anyhow::bail!("reply decision evidence_ids exceeds {MAX_REPLY_EVIDENCE_IDS} identifiers"); - } - if input.evidence_ids.iter().any(|id| { - id.trim().is_empty() - || id.len() > MAX_REPLY_EVIDENCE_ID_BYTES - || !id - .bytes() - .all(|byte| byte.is_ascii_alphanumeric() || b"._:-".contains(&byte)) - }) { - anyhow::bail!("reply decision evidence_ids must be bounded opaque identifiers"); - } - if input.decision == "reply" { - if input.evidence_ids.is_empty() { - anyhow::bail!("reply decision requires at least one evidence identifier"); - } - if input.style_policy_version != STYLE_POLICY_VERSION { - anyhow::bail!("reply decision style policy must be {STYLE_POLICY_VERSION}"); - } - if input - .reply - .as_deref() - .map(str::trim) - .unwrap_or("") - .is_empty() - { - anyhow::bail!("reply decision reply must not be empty"); - } - crate::reply_policy::validate_auto_reply_laughter( - input.reply.as_deref().unwrap_or_default(), - )?; - } - Ok(()) -} - -pub fn record_reply_decision(db_path: &Path, record_json: &str) -> Result { - let input: ReplyDecisionInput = - serde_json::from_str(record_json).context("invalid reply decision JSON")?; - validate_reply_decision_input(&input)?; - let mut conn = open_db(db_path)?; - let tx = conn.transaction()?; - let incoming_rank = reply_status_rank(&input.status) - .ok_or_else(|| anyhow::anyhow!("unknown reply decision status '{}'", input.status))?; - let now = Utc::now().to_rfc3339(); - let existing_status = tx - .query_row( - "SELECT status FROM reply_decisions WHERE event_id=?1", - [&input.event_id], - |row| row.get::<_, String>(0), - ) - .optional()?; - if let Some(previous_status) = existing_status { - let previous_rank = reply_status_rank(&previous_status).unwrap_or(6); - if !can_apply_reply_status( - &previous_status, - &input.status, - previous_rank, - incoming_rank, - ) { - return Ok(false); - } - } - let mut evidence_map = serde_json::Map::new(); - evidence_map.insert( - "evidence_ids".into(), - serde_json::to_value(&input.evidence_ids)?, - ); - evidence_map.insert( - "style_policy_version".into(), - serde_json::to_value(&input.style_policy_version)?, - ); - if let Some(prov) = &input.provenance { - evidence_map.insert("provenance".into(), prov.clone()); - } - let evidence_json = serde_json::Value::Object(evidence_map).to_string(); - if evidence_json.len() > 16 * 1024 { - anyhow::bail!("reply evidence exceeds 16 KiB"); - } - tx.execute( - "INSERT INTO reply_decisions( - event_id, chat, author, received_at, message, vector, decision, reason, - category, context_match_count, style_match_count, best_context_score, - best_style_score, prior_similarity, scheduled_delay_seconds, status, - reply, sent_at, created_at, updated_at, evidence_json - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, NULL, ?18, ?18, ?19) - ON CONFLICT(event_id) DO UPDATE SET - chat=excluded.chat, - author=excluded.author, - received_at=excluded.received_at, - message=excluded.message, - vector=excluded.vector, - decision=excluded.decision, - reason=excluded.reason, - category=excluded.category, - context_match_count=excluded.context_match_count, - style_match_count=excluded.style_match_count, - best_context_score=excluded.best_context_score, - best_style_score=excluded.best_style_score, - prior_similarity=excluded.prior_similarity, - scheduled_delay_seconds=excluded.scheduled_delay_seconds, - status=excluded.status, - reply=CASE - WHEN excluded.decision='skip' AND excluded.status='skipped' THEN NULL - ELSE COALESCE(excluded.reply, reply) - END, - sent_at=CASE - WHEN excluded.decision='skip' AND excluded.status='skipped' THEN NULL - ELSE sent_at - END, - evidence_json=excluded.evidence_json, - updated_at=excluded.updated_at", - params![ - input.event_id, - input.chat, - input.author, - input.received_at, - input.message, - vector_to_bytes(&encode_vector(&input.message)), - input.decision, - input.reason, - input.category, - input.context_match_count as i64, - input.style_match_count as i64, - input.best_context_score, - input.best_style_score, - input.prior_similarity, - input.scheduled_delay_seconds, - input.status, - input.reply, - now, - evidence_json, - ], - )?; - tx.commit()?; - Ok(true) -} - -pub fn update_reply_decision( - db_path: &Path, - event_id: &str, - status: &str, - reply: Option<&str>, - sent_at: Option<&str>, -) -> Result { - if event_id.trim().is_empty() || status.trim().is_empty() { - anyhow::bail!("reply decision event_id and status must not be empty"); - } - if let Some(reply) = reply { - crate::reply_policy::validate_auto_reply_laughter(reply)?; - } - let mut conn = open_db(db_path)?; - let tx = conn.transaction()?; - let incoming_rank = reply_status_rank(status) - .ok_or_else(|| anyhow::anyhow!("unknown reply decision status '{status}'"))?; - let previous_status = tx - .query_row( - "SELECT status FROM reply_decisions WHERE event_id=?1", - [event_id], - |row| row.get::<_, String>(0), - ) - .optional()?; - let Some(previous_status) = previous_status else { - return Ok(false); - }; - let previous_rank = reply_status_rank(&previous_status).unwrap_or(6); - if !can_apply_reply_status(&previous_status, status, previous_rank, incoming_rank) { - return Ok(false); - } - let changed = tx.execute( - "UPDATE reply_decisions - SET status = ?2, - reply = COALESCE(?3, reply), - sent_at = COALESCE(?4, sent_at), - updated_at = ?5 - WHERE event_id = ?1", - params![event_id, status, reply, sent_at, Utc::now().to_rfc3339()], - )?; - tx.commit()?; - Ok(changed > 0) -} - -fn parse_reply_sent_at_seconds(value: &str) -> Option { - chrono::DateTime::parse_from_rfc3339(value.trim()) - .ok() - .map(|value| value.timestamp()) - .or_else(|| parse_chat_date(value).map(|value| value.and_utc().timestamp())) -} - -#[derive(Debug, Clone)] -struct AutoGeneratedReplyCandidate { - event_id: String, - confirmed_sent: bool, -} - -/// A confirmed outgoing reply taken from the room delivery authority -/// (`reply-queue.sqlite3`). -/// -/// The queue stays the delivery authority, so a terminal `sent` job is direct -/// evidence that the assistant, not the owner, wrote that text into the room. -/// Style learning must not treat such a row as an owner sample: the worker's -/// own wording would otherwise be re-learned as the owner's voice (AHP: -/// style). -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct ConfirmedSelfSend { - pub event_id: String, - pub text: String, - pub sent_at: i64, -} - -/// Load the durable confirmed-send records of one room queue. -/// -/// Only terminal `sent` jobs with a non-empty reply are returned. `skipped` -/// jobs never reached the composer, so they must not be able to hide a genuine -/// owner message from style learning. A missing or unreadable queue is not an -/// error: it simply contributes no evidence. -pub fn confirmed_self_sends_from_queue(queue_path: &Path) -> Result> { - if queue_path.is_symlink() || !queue_path.is_file() { - return Ok(Vec::new()); - } - let conn = Connection::open_with_flags(queue_path, OpenFlags::SQLITE_OPEN_READ_ONLY) - .with_context(|| format!("open reply queue read-only: {}", queue_path.display()))?; - conn.busy_timeout(Duration::from_secs(CONTEXT_DB_BUSY_TIMEOUT_SECS))?; - let has_jobs: bool = conn - .query_row( - "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'reply_jobs'", - [], - |_| Ok(true), - ) - .optional()? - .unwrap_or(false); - if !has_jobs { - return Ok(Vec::new()); - } - let mut stmt = conn.prepare( - "SELECT event_id, reply, updated_at FROM reply_jobs - WHERE status = 'sent' AND reply IS NOT NULL AND length(trim(reply)) > 0", - )?; - let rows = stmt.query_map([], |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, String>(1)?, - row.get::<_, Option>(2)?, - )) - })?; - let mut sends = Vec::new(); - for row in rows { - let (event_id, text, updated_at) = row?; - let text = text.trim().to_string(); - let Some(sent_at) = updated_at.filter(|value| value.is_finite()) else { - continue; - }; - if text.is_empty() { - continue; - } - sends.push(ConfirmedSelfSend { - event_id, - text, - sent_at: sent_at.round() as i64, - }); - } - Ok(sends) -} - -fn ambiguous_auto_candidate_status(status: &str) -> bool { - matches!( - status, - "scheduled" - | "sending" - | "accepted_unconfirmed" - | "delivery_unknown" - | "reconcile_required" - ) -} - -/// Classify a bounded page of caller-identified outgoing self rows against -/// confirmed sends. A row is marked automatic only for a unique, exact-text -/// `sent` decision within +/- 120 seconds. Exact time-bounded candidates in an -/// uncertain delivery state remain non-automatic but are explicitly excluded -/// from learning. Any many-to-one, one-to-many, or unparseable-time match is -/// likewise fail-closed and remains non-automatic. -/// -/// `confirmed_sends` carries the delivery authority's own record (the room -/// queue's terminal `sent` jobs). The reply-decision ledger alone cannot prove a -/// send when the decision row was rewritten as `skip`, so both authorities are -/// unioned and the same uniqueness rule is applied across the union. -pub fn classify_auto_generated_self_events( - db_path: &Path, - chat: &str, - events: &[OutgoingSelfEvent], - confirmed_sends: &[ConfirmedSelfSend], -) -> Result> { - if chat.trim().is_empty() || chat.len() > LIVE_CONTEXT_MAX_FIELD_BYTES { - anyhow::bail!("automatic self-event classification chat is invalid"); - } - if events.len() > LIVE_CONTEXT_BATCH_MAX_ROWS { - anyhow::bail!("automatic self-event classification exceeds the live batch bound"); - } - let mut identities = BTreeMap::new(); - let mut chat_id = None; - let mut grouped: BTreeMap<&str, Vec> = BTreeMap::new(); - for (index, event) in events.iter().enumerate() { - if event.chat_id <= 0 - || event.log_id <= 0 - || event.message.len() > LIVE_CONTEXT_MAX_FIELD_BYTES - { - anyhow::bail!("automatic self-event classification row is invalid"); - } - live_event_date(event.sent_at)?; - if chat_id - .replace(event.chat_id) - .is_some_and(|id| id != event.chat_id) - { - anyhow::bail!("automatic self-event classification spans multiple chats"); - } - if identities - .insert((event.chat_id, event.log_id), ()) - .is_some() - { - anyhow::bail!("automatic self-event classification contains a duplicate row"); - } - grouped - .entry(event.message.as_str()) - .or_default() - .push(index); - } - if events.is_empty() { - return Ok(Vec::new()); - } - - let conn = open_db_readonly(db_path)?; - ensure_retrieval_index_current(&conn)?; - let mut candidates_by_event = vec![Vec::::new(); events.len()]; - let mut has_unparseable_candidate = vec![false; events.len()]; - let mut has_overflow_candidate = vec![false; events.len()]; - for (message, indexes) in grouped { - if message.is_empty() { - continue; - } - let minimum = indexes - .iter() - .map(|index| events[*index].sent_at) - .min() - .unwrap_or(0) - .saturating_sub(AUTO_GENERATED_MATCH_WINDOW_SECONDS); - let maximum = indexes - .iter() - .map(|index| events[*index].sent_at) - .max() - .unwrap_or(0) - .saturating_add(AUTO_GENERATED_MATCH_WINDOW_SECONDS); - let mut stmt = conn.prepare( - "SELECT event_id, status, sent_at, created_at, updated_at, - scheduled_delay_seconds - FROM reply_decisions - WHERE chat = ?1 AND decision = 'reply' AND reply = ?2 - AND ( - (status = 'sent' - AND (CAST(strftime('%s', sent_at) AS INTEGER) BETWEEN ?3 AND ?4 - OR strftime('%s', sent_at) IS NULL)) - OR - (status IN ( - 'scheduled', 'sending', 'accepted_unconfirmed', - 'delivery_unknown', 'reconcile_required' - ) - AND ( - CAST(strftime('%s', updated_at) AS INTEGER) BETWEEN ?3 AND ?4 - OR (CAST(strftime('%s', created_at) AS INTEGER) - + CAST(ROUND(scheduled_delay_seconds) AS INTEGER)) - BETWEEN ?3 AND ?4 - OR strftime('%s', updated_at) IS NULL - OR strftime('%s', created_at) IS NULL - )) - ) - ORDER BY event_id ASC - LIMIT ?5", - )?; - let rows = stmt.query_map( - params![ - chat, - message, - minimum, - maximum, - (AUTO_GENERATED_MATCH_MAX_CANDIDATES_PER_TEXT + 1) as i64, - ], - |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, String>(1)?, - row.get::<_, Option>(2)?, - row.get::<_, String>(3)?, - row.get::<_, String>(4)?, - row.get::<_, f64>(5)?, - )) - }, - )?; - let candidates = rows.collect::>>()?; - if candidates.len() > AUTO_GENERATED_MATCH_MAX_CANDIDATES_PER_TEXT { - anyhow::bail!("automatic self-event match candidate set exceeds its bound"); - } - for (event_id, status, sent_at, created_at, updated_at, scheduled_delay) in candidates { - let confirmed_sent = status == "sent"; - if !confirmed_sent && !ambiguous_auto_candidate_status(&status) { - anyhow::bail!("automatic self-event candidate has an unsupported status"); - } - let candidate_times = if confirmed_sent { - sent_at - .as_deref() - .and_then(parse_reply_sent_at_seconds) - .into_iter() - .collect::>() - } else { - let mut times = Vec::with_capacity(2); - if let Some(updated_at) = parse_reply_sent_at_seconds(&updated_at) { - times.push(updated_at); - } - if scheduled_delay.is_finite() - && (0.0..=(MAX_RESPONSE_DELAY_SECONDS as f64)).contains(&scheduled_delay) - { - if let Some(scheduled_at) = - parse_reply_sent_at_seconds(&created_at).and_then(|created_at| { - created_at.checked_add(scheduled_delay.round() as i64) - }) - { - times.push(scheduled_at); - } - } - times.sort_unstable(); - times.dedup(); - times - }; - if candidate_times.is_empty() { - for index in &indexes { - has_unparseable_candidate[*index] = true; - } - continue; - } - for index in &indexes { - if candidate_times.iter().any(|candidate_time| { - events[*index].sent_at.abs_diff(*candidate_time) - <= AUTO_GENERATED_MATCH_WINDOW_SECONDS as u64 - }) { - candidates_by_event[*index].push(AutoGeneratedReplyCandidate { - event_id: event_id.clone(), - confirmed_sent, - }); - } - } - } - } - - // Union in the delivery authority's confirmed sends. A queue `sent` job has - // no incoming row to attach to, so it is matched by exact text inside the - // same window and identified by its own job id. - let mut grouped_by_text: BTreeMap<&str, Vec> = BTreeMap::new(); - for (index, event) in events.iter().enumerate() { - if event.message.is_empty() { - continue; - } - grouped_by_text - .entry(event.message.as_str()) - .or_default() - .push(index); - } - for send in confirmed_sends { - let Some(indexes) = grouped_by_text.get(send.text.as_str()) else { - continue; - }; - for index in indexes { - if events[*index].sent_at.abs_diff(send.sent_at) - > AUTO_GENERATED_MATCH_WINDOW_SECONDS as u64 - { - continue; - } - if candidates_by_event[*index] - .iter() - .any(|candidate| candidate.event_id == send.event_id) - { - continue; - } - if candidates_by_event[*index].len() > AUTO_GENERATED_MATCH_MAX_CANDIDATES_PER_TEXT { - has_overflow_candidate[*index] = true; - continue; - } - candidates_by_event[*index].push(AutoGeneratedReplyCandidate { - event_id: send.event_id.clone(), - confirmed_sent: true, - }); - } - } - - let mut candidate_use_counts = BTreeMap::new(); - for candidates in &candidates_by_event { - for candidate in candidates { - *candidate_use_counts - .entry(candidate.event_id.as_str()) - .or_insert(0usize) += 1; - } - } - Ok(events - .iter() - .enumerate() - .map(|(index, event)| { - let candidates = &candidates_by_event[index]; - let (auto_generated, matched_event_id, reason) = if has_overflow_candidate[index] { - (false, None, "match_candidate_overflow") - } else if has_unparseable_candidate[index] { - (false, None, "unparseable_candidate_time") - } else if candidates.is_empty() { - (false, None, "no_exact_sent_match") - } else if candidates.len() != 1 { - (false, None, "multiple_sent_matches") - } else if candidate_use_counts - .get(candidates[0].event_id.as_str()) - .copied() - .unwrap_or(0) - != 1 - { - (false, None, "sent_match_reused_in_batch") - } else if !candidates[0].confirmed_sent { - ( - false, - Some(candidates[0].event_id.clone()), - "ambiguous_auto_candidate", - ) - } else { - ( - true, - Some(candidates[0].event_id.clone()), - "unique_exact_sent_match", - ) - }; - AutoGeneratedSelfEventClassification { - chat_id: event.chat_id, - log_id: event.log_id, - auto_generated, - matched_event_id, - reason: reason.to_string(), - } - }) - .collect()) -} - -pub fn reply_decision_search( - db_path: &Path, - chat: &str, - query: &str, - limit: usize, -) -> Result> { - if chat.trim().is_empty() || query.trim().is_empty() || limit == 0 { - return Ok(Vec::new()); - } - let conn = open_db_readonly(db_path)?; - ensure_retrieval_index_current(&conn)?; - reply_decision_search_with_connection_excluding(&conn, chat, query, limit, &BTreeSet::new()) -} - -fn reply_decision_search_with_connection_excluding( - conn: &Connection, - chat: &str, - query: &str, - limit: usize, - excluded_event_ids: &BTreeSet, -) -> Result> { - ensure_retrieval_index_current(conn)?; - let result_limit = limit.min(REPLY_DECISION_CANDIDATE_CAP); - if result_limit == 0 { - return Ok(Vec::new()); - } - let mut results = Vec::new(); - let mut exact_stmt = conn.prepare( - "SELECT event_id, chat, author, received_at, message, decision, reason, - category, status, reply, substr(evidence_json, 1, 16384) - FROM reply_decisions - WHERE chat = ?1 AND message = ?2 - ORDER BY created_at DESC, status ASC, event_id ASC - LIMIT ?3", - )?; - let exact_candidate_limit = result_limit.saturating_add(excluded_event_ids.len()); - let exact_rows = - exact_stmt.query_map(params![chat, query, exact_candidate_limit as i64], |row| { - Ok(ReplyDecisionMatch { - event_id: row.get(0)?, - chat: row.get(1)?, - author: row.get(2)?, - received_at: row.get(3)?, - message: row.get(4)?, - decision: row.get(5)?, - reason: row.get(6)?, - category: row.get(7)?, - status: row.get(8)?, - reply: row.get(9)?, - evidence_json: row.get(10)?, - score: 1.0, - }) - })?; - for row in exact_rows { - let row = row?; - if !excluded_event_ids.contains(&row.event_id) { - results.push(row); - } - if results.len() == result_limit { - break; - } - } - if results.len() == result_limit { - return Ok(results); - } - - let query_vector = encode_vector(query); - if query_vector.iter().all(|value| *value == 0.0) { - return Ok(results); - } - let mut stmt = conn.prepare( - "SELECT event_id, chat, author, received_at, message, decision, reason, - category, status, reply, substr(evidence_json, 1, 16384), vector - FROM reply_decisions - WHERE chat = ?1 - ORDER BY created_at DESC, event_id ASC - LIMIT ?2", - )?; - let similarity_candidate_limit = - REPLY_DECISION_CANDIDATE_CAP.saturating_add(excluded_event_ids.len()); - let rows = stmt.query_map(params![chat, similarity_candidate_limit as i64], |row| { - let evidence_json: String = row.get(10)?; - let bytes: Vec = row.get(11)?; - if bytes.len() != VECTOR_DIM * 4 { - return Err(rusqlite::Error::InvalidColumnType( - 11, - "vector".into(), - rusqlite::types::Type::Blob, - )); - } - Ok(( - ReplyDecisionMatch { - event_id: row.get(0)?, - chat: row.get(1)?, - author: row.get(2)?, - received_at: row.get(3)?, - message: row.get(4)?, - decision: row.get(5)?, - reason: row.get(6)?, - category: row.get(7)?, - status: row.get(8)?, - reply: row.get(9)?, - evidence_json, - score: 0.0, - }, - bytes_to_vector(&bytes), - )) - })?; - let exact_event_ids = results - .iter() - .map(|result| result.event_id.as_str()) - .collect::>(); - let mut similar = Vec::new(); - for row in rows { - let (mut result, vector) = row?; - if excluded_event_ids.contains(&result.event_id) - || exact_event_ids.contains(result.event_id.as_str()) - { - continue; - } - result.score = cosine(&query_vector, &vector); - similar.push(result); - } - similar.sort_by(|a, b| { - b.score - .partial_cmp(&a.score) - .unwrap_or(Ordering::Equal) - .then_with(|| a.event_id.cmp(&b.event_id)) - }); - results.extend(similar.into_iter().take(result_limit - results.len())); - Ok(results) -} - -pub fn search( - db_path: &Path, - chat: Option<&str>, - source: Option<&str>, - query: &str, - mode: &str, - limit: usize, -) -> Result> { - // 기본 경로는 결정적 로컬 해시 임베더를 주입해 리팩터링 이전과 동일한 - // 검색 결과·정렬을 그대로 유지한다. - search_with_embedder( - db_path, - chat, - source, - query, - mode, - limit, - &LocalHashEmbedder, - ) -} - -/// 착탈식 임베더를 주입받는 검색 진입점. 기본 `search`는 `LocalHashEmbedder`를 -/// 주입해 동작 동일성을 보장한다(R11.1, R11.2). -pub fn search_with_embedder( - db_path: &Path, - chat: Option<&str>, - source: Option<&str>, - query: &str, - mode: &str, - limit: usize, - embedder: &dyn Embedder, -) -> Result> { - if query.trim().is_empty() { - anyhow::bail!("query must not be empty"); - } - if limit == 0 { - return Ok(Vec::new()); - } - let conn = open_db_readonly(db_path)?; - ensure_retrieval_index_current(&conn)?; - search_with_connection_excluding( - &conn, - chat, - source, - query, - mode, - limit, - &BTreeSet::new(), - embedder, - ) -} - -// Mirrors the sibling search entry points' parameter shape (scope, query, -// mode, limit, exclusion, embedder); bundling them is an API change, not a fix. -#[allow( - clippy::too_many_arguments, - reason = "search entry points share one signature" -)] -fn search_with_connection_excluding( - conn: &Connection, - chat: Option<&str>, - source: Option<&str>, - query: &str, - mode: &str, - limit: usize, - excluded_context_ids: &BTreeSet, - embedder: &dyn Embedder, -) -> Result> { - ensure_retrieval_index_current(conn)?; - let preferred_source = preferred_context_source(conn, chat, source)?; - let candidates = match mode { - "keyword" => keyword_search_excluding( - conn, - chat, - preferred_source.as_deref(), - query, - excluded_context_ids, - )?, - "vector" => vector_search_excluding( - conn, - chat, - preferred_source.as_deref(), - query, - excluded_context_ids, - embedder, - )?, - "hybrid" => { - return hybrid_search_excluding( - conn, - chat, - preferred_source.as_deref(), - query, - limit, - excluded_context_ids, - embedder, - ) - } - other => { - anyhow::bail!("unknown context search mode '{other}' (use keyword, vector, or hybrid)") - } - }; - Ok(candidates - .into_iter() - .take(limit) - .map(|candidate| candidate.result) - .collect()) -} -pub fn style_search( - db_path: &Path, - chat: Option<&str>, - query: &str, - limit: usize, -) -> Result> { - if query.trim().is_empty() { - anyhow::bail!("query must not be empty"); - } - if limit == 0 { - return Ok(Vec::new()); - } - let conn = open_db_readonly(db_path)?; - ensure_retrieval_index_current(&conn)?; - style_search_with_connection(&conn, chat, None, query, limit, true) -} - -fn style_search_with_connection( - conn: &Connection, - chat: Option<&str>, - source: Option<&str>, - query: &str, - limit: usize, - require_policy: bool, -) -> Result> { - ensure_retrieval_index_current(conn)?; - let preferred_source = preferred_context_source(conn, chat, source)?; - let query_vector = encode_vector(query); - let tokenless = query_vector.iter().all(|value| *value == 0.0); - // Style retrieval intentionally uses a bounded eligible recency/diversity - // pool. This keeps policy filtering ahead of vector scoring without an - // unbounded fallback when no lexical style index is available. - let mut stmt = conn.prepare( - "SELECT id,chat,source,date,user_name,message,vector - FROM owner_style - WHERE user_name = ?5 - AND style_eligible = 1 - AND (?1 IS NULL OR chat = ?1) - AND (?2 IS NULL OR source = ?2) - AND (?3 = 0 OR policy_version = ?4) - ORDER BY date DESC, source_row DESC, id DESC - LIMIT ?6", - )?; - let rows = stmt.query_map( - params![ - chat, - preferred_source.as_deref(), - require_policy as i64, - STYLE_POLICY_VERSION, - STYLE_USER, - STYLE_VECTOR_CANDIDATE_CAP as i64, - ], - |row| { - let bytes: Vec = row.get(6)?; - if bytes.len() != VECTOR_DIM * 4 { - return Err(rusqlite::Error::InvalidColumnType( - 6, - "vector".into(), - rusqlite::types::Type::Blob, - )); - } - Ok(( - row.get::<_, i64>(0)?, - ContextResult { - chat: row.get(1)?, - source: row.get(2)?, - date: row.get(3)?, - user: row.get(4)?, - message: row.get(5)?, - score: 0.0, - mode: "vector_style".into(), - }, - bytes_to_vector(&bytes), - )) - }, - )?; - let mut results = Vec::new(); - for row in rows { - let (id, mut result, vector) = row?; - crate::reply_policy::validate_auto_reply_laughter(&result.message) - .context("style search contains disallowed laughter evidence")?; - result.score = if tokenless { - 0.0 - } else { - cosine(&query_vector, &vector) - }; - if tokenless { - result.mode = "recency_style".into(); - } - results.push(ContextCandidate { id, result }); - } - if !tokenless { - results.sort_by(|a, b| { - b.result - .score - .partial_cmp(&a.result.score) - .unwrap_or(Ordering::Equal) - .then_with(|| a.id.cmp(&b.id)) - }); - } - Ok(results - .into_iter() - .take(limit.min(STYLE_VECTOR_CANDIDATE_CAP)) - .map(|candidate| candidate.result) - .collect()) -} - -fn recipient_style_search_with_connection( - conn: &Connection, - chat: &str, - source: &str, - recipient: &str, - query: &str, - limit: usize, -) -> Result> { - ensure_retrieval_index_current(conn)?; - let query_vector = encode_vector(query); - let tokenless = query_vector.iter().all(|value| *value == 0.0); - let mut stmt = conn.prepare(RECIPIENT_STYLE_SEARCH_SQL)?; - let rows = stmt.query_map( - params![ - source, - recipient, - chat, - STYLE_USER, - STYLE_POLICY_VERSION, - STYLE_VECTOR_CANDIDATE_CAP as i64, - ], - |row| { - let bytes: Vec = row.get(6)?; - if bytes.len() != VECTOR_DIM * 4 { - return Err(rusqlite::Error::InvalidColumnType( - 6, - "vector".into(), - rusqlite::types::Type::Blob, - )); - } - let confidence: f32 = row.get(7)?; - if !confidence.is_finite() || !(0.0..=1.0).contains(&confidence) || confidence == 0.0 { - return Err(rusqlite::Error::InvalidColumnType( - 7, - "confidence".into(), - rusqlite::types::Type::Real, - )); - } - Ok(( - row.get::<_, i64>(0)?, - ContextResult { - chat: row.get(1)?, - source: row.get(2)?, - date: row.get(3)?, - user: row.get(4)?, - message: row.get(5)?, - score: 0.0, - mode: "vector_style_recipient".into(), - }, - bytes_to_vector(&bytes), - confidence, - )) - }, - )?; - let mut results = Vec::new(); - for row in rows { - let (id, mut result, vector, confidence) = row?; - crate::reply_policy::validate_auto_reply_laughter(&result.message) - .context("recipient style search contains disallowed laughter evidence")?; - result.score = if tokenless { - 0.0 - } else { - cosine(&query_vector, &vector) * confidence - }; - if tokenless { - result.mode = "recency_style_recipient".into(); - } - results.push(ContextCandidate { id, result }); - } - if !tokenless { - results.sort_by(|left, right| { - right - .result - .score - .partial_cmp(&left.result.score) - .unwrap_or(Ordering::Equal) - .then_with(|| left.id.cmp(&right.id)) - }); - } - Ok(results - .into_iter() - .take(limit.min(STYLE_VECTOR_CANDIDATE_CAP)) - .map(|candidate| candidate.result) - .collect()) -} - -#[derive(Debug)] -struct LiveEventExclusions { - chat_id: i64, - log_ids: BTreeSet, - decision_event_ids: BTreeSet, -} - -fn live_event_exclusions(chat_id: i64, log_ids: &[i64]) -> Result { - if chat_id <= 0 { - anyhow::bail!("live context exclusion chat ID must be positive"); - } - if log_ids.is_empty() || log_ids.len() > CONTEXT_REPLY_BUNDLE_MAX_EXCLUDED_LOG_IDS { - anyhow::bail!( - "live context exclusions must contain between 1 and {} log IDs", - CONTEXT_REPLY_BUNDLE_MAX_EXCLUDED_LOG_IDS - ); - } - let mut unique_log_ids = BTreeSet::new(); - for log_id in log_ids { - if *log_id <= 0 { - anyhow::bail!("live context exclusion log IDs must be positive"); - } - if !unique_log_ids.insert(*log_id) { - anyhow::bail!("live context exclusion log IDs must be unique"); - } - } - let decision_event_ids = unique_log_ids - .iter() - .map(|log_id| format!("db:{chat_id}:{log_id}")) - .collect(); - Ok(LiveEventExclusions { - chat_id, - log_ids: unique_log_ids, - decision_event_ids, - }) -} - -fn context_ids_for_excluded_live_events( - conn: &Connection, - chat: &str, - preferred_source: Option<&str>, - exclusions: &LiveEventExclusions, -) -> Result> { - let has_live_events = conn.query_row( - "SELECT EXISTS( - SELECT 1 FROM sqlite_master - WHERE type = 'table' AND name = 'context_live_events' - )", - [], - |row| row.get::<_, i64>(0), - )? != 0; - if !has_live_events { - return Ok(BTreeSet::new()); - } - let mut stmt = conn.prepare( - "SELECT events.context_message_id - FROM context_live_events events - JOIN context_messages messages - ON messages.id = events.context_message_id - AND messages.source = events.source - WHERE events.chat_id = ?1 AND events.log_id = ?2 - AND messages.chat = ?3 - AND (?4 IS NULL OR events.source = ?4) - ORDER BY events.source ASC, events.context_message_id ASC", - )?; - let mut context_ids = BTreeSet::new(); - for log_id in &exclusions.log_ids { - let rows = stmt.query_map( - params![exclusions.chat_id, log_id, chat, preferred_source], - |row| row.get::<_, i64>(0), - )?; - let mut mapped_context_id = None; - for row in rows { - let context_id = row?; - if mapped_context_id.replace(context_id).is_some() { - anyhow::bail!("live context exclusion mapping is ambiguous"); - } - } - if let Some(context_id) = mapped_context_id { - context_ids.insert(context_id); - } - } - Ok(context_ids) -} - -pub fn context_reply_bundle( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, -) -> Result { - if chat.trim().is_empty() { - anyhow::bail!("chat name must not be empty"); - } - // Punctuation-only or blank inbound still needs style/timing evidence. - - let conn = open_db_readonly(db_path)?; - conn.execute_batch("BEGIN DEFERRED TRANSACTION")?; - let result = (|| { - ensure_retrieval_index_current(&conn)?; - let preferred_source = preferred_context_source(&conn, Some(chat), source)?; - let context = search_with_connection_excluding( - &conn, - Some(chat), - preferred_source.as_deref(), - query, - "hybrid", - CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT, - &BTreeSet::new(), - &LocalHashEmbedder, - )?; - let styles = style_search_with_connection( - &conn, - Some(chat), - preferred_source.as_deref(), - query, - CONTEXT_REPLY_BUNDLE_STYLE_LIMIT, - true, - )?; - let prior_decisions = reply_decision_search_with_connection_excluding( - &conn, - chat, - query, - CONTEXT_REPLY_BUNDLE_DECISION_LIMIT, - &BTreeSet::new(), - )?; - let style_profile = style_profile_with_connection( - &conn, - chat, - STYLE_USER, - preferred_source.as_deref(), - true, - )?; - let response_time = response_time_stats_with_connection( - &conn, - chat, - STYLE_USER, - preferred_source.as_deref(), - )?; - let mut bundle = ContextReplyBundle { - schema_version: CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION, - context, - styles, - prior_decisions, - style_profile, - response_time, - }; - bundle.redact_provenance(); - bundle.validate_json_size()?; - Ok(bundle) - })(); - - match result { - Ok(bundle) => { - conn.execute_batch("COMMIT")?; - Ok(bundle) - } - Err(error) => { - let _ = conn.execute_batch("ROLLBACK"); - Err(error) - } - } -} - -/// Build the version-2 reply bundle for a specific participant in one read -/// snapshot. Every live row of the incoming burst (`excluded_log_ids`) is -/// excluded from context retrieval and prior decisions. Direct -/// recipient-linked style evidence is used only when its profile clears the -/// sample/confidence floor; otherwise both profile and style examples fall -/// back to the authoritative room-wide register. -pub fn context_reply_bundle_for_recipient_excluding_live_events( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, - recipient: &str, - chat_id: i64, - excluded_log_ids: &[i64], -) -> Result { - if chat.trim().is_empty() || recipient.trim().is_empty() { - anyhow::bail!("recipient context reply bundle chat and recipient must not be empty"); - } - // Recipient bundles keep style/timing when the inbound has no tokens. - let exclusions = live_event_exclusions(chat_id, excluded_log_ids)?; - - let conn = open_db_readonly(db_path)?; - conn.execute_batch("BEGIN DEFERRED TRANSACTION")?; - let result = (|| { - ensure_retrieval_index_current(&conn)?; - let preferred_source = preferred_context_source(&conn, Some(chat), source)?; - let excluded_context_ids = context_ids_for_excluded_live_events( - &conn, - chat, - preferred_source.as_deref(), - &exclusions, - )?; - let context = search_with_connection_excluding( - &conn, - Some(chat), - preferred_source.as_deref(), - query, - "hybrid", - CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT, - &excluded_context_ids, - &LocalHashEmbedder, - )?; - let recipient_style_profile = recipient_style_profile_with_connection( - &conn, - chat, - recipient, - preferred_source.as_deref(), - )?; - let styles = if let Some(profile) = recipient_style_profile - .as_ref() - .filter(|profile| !profile.used_fallback) - { - recipient_style_search_with_connection( - &conn, - chat, - &profile.profile.source, - recipient, - query, - CONTEXT_REPLY_BUNDLE_STYLE_LIMIT, - )? - } else { - style_search_with_connection( - &conn, - Some(chat), - preferred_source.as_deref(), - query, - CONTEXT_REPLY_BUNDLE_STYLE_LIMIT, - true, - )? - }; - let prior_decisions = reply_decision_search_with_connection_excluding( - &conn, - chat, - query, - CONTEXT_REPLY_BUNDLE_DECISION_LIMIT, - &exclusions.decision_event_ids, - )?; - let style_profile = style_profile_with_connection( - &conn, - chat, - STYLE_USER, - preferred_source.as_deref(), - true, - )?; - let response_time = response_time_stats_with_connection( - &conn, - chat, - STYLE_USER, - preferred_source.as_deref(), - )?; - let mut bundle = RecipientContextReplyBundle { - schema_version: RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION, - recipient: recipient.to_string(), - context, - styles, - prior_decisions, - style_profile, - recipient_style_profile, - response_time, - }; - bundle.redact_provenance(); - bundle.validate_json_size()?; - Ok(bundle) - })(); - - match result { - Ok(bundle) => { - conn.execute_batch("COMMIT")?; - Ok(bundle) - } - Err(error) => { - let _ = conn.execute_batch("ROLLBACK"); - Err(error) - } - } -} - -pub fn context_reply_bundle_for_recipient_excluding_live_events_json( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, - recipient: &str, - chat_id: i64, - excluded_log_ids: &[i64], -) -> Result { - let bundle = context_reply_bundle_for_recipient_excluding_live_events( - db_path, - chat, - query, - source, - recipient, - chat_id, - excluded_log_ids, - )?; - let output = serde_json::to_string(&bundle)?; - if output.len() > CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES { - anyhow::bail!( - "recipient context reply bundle exceeds {} bytes", - CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES - ); - } - Ok(output) -} - -pub fn context_reply_bundle_json( - db_path: &Path, - chat: &str, - query: &str, - source: Option<&str>, -) -> Result { - let bundle = context_reply_bundle(db_path, chat, query, source)?; - let output = serde_json::to_string(&bundle)?; - if output.len() > CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES { - anyhow::bail!( - "context reply bundle exceeds {} bytes", - CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES - ); - } - Ok(output) -} - -#[derive(Debug, Clone, Serialize)] -pub struct ReplyDecision { - pub event_id: String, - pub status: String, - pub evidence_json: String, - pub updated_at: String, -} - -#[derive(Debug, Clone, Serialize)] -pub struct ReplyDecisionUpdate { - pub applied: bool, - pub updated: bool, - pub decision: Option, - pub reason: Option, -} - -fn reply_status_rank(status: &str) -> Option { - Some(match status { - "pending" => 0, - "processing" => 1, - "projection_pending" => 2, - "scheduled" => 3, - "sending" => 4, - "accepted_unconfirmed" => 5, - "sent" | "skipped" | "poison" => 6, - "failed" => 4, - "delivery_unknown" | "reconcile_required" => 6, - _ => return None, - }) -} - -/// Apply a monotonic audit projection. The queue remains the delivery authority. -pub fn project_reply_decision( - db_path: &Path, - event_id: &str, - status: &str, - evidence_json: &str, -) -> Result { - if event_id.trim().is_empty() { - anyhow::bail!("reply event identity must not be empty"); - } - if evidence_json.len() > 16 * 1024 { - anyhow::bail!("reply evidence exceeds 16 KiB"); - } - let incoming_rank = reply_status_rank(status) - .ok_or_else(|| anyhow::anyhow!("unknown reply decision status '{status}'"))?; - let mut conn = open_db(db_path)?; - let tx = conn.transaction()?; - let now = chrono::Utc::now().to_rfc3339(); - let existing = tx - .query_row( - "SELECT status,evidence_json,updated_at FROM reply_decisions WHERE event_id=?1", - [event_id], - |row| { - Ok(ReplyDecision { - event_id: event_id.to_string(), - status: row.get(0)?, - evidence_json: row.get(1)?, - updated_at: row.get(2)?, - }) - }, - ) - .optional()?; - if let Some(previous) = existing { - let previous_rank = reply_status_rank(&previous.status).unwrap_or(6); - if !can_apply_reply_status(&previous.status, status, previous_rank, incoming_rank) { - return Ok(ReplyDecisionUpdate { - applied: false, - updated: false, - decision: Some(previous), - reason: Some("terminal_or_newer_status_cannot_regress".into()), - }); - } - if previous.status == status && previous.evidence_json == evidence_json { - return Ok(ReplyDecisionUpdate { - applied: true, - updated: false, - decision: Some(previous), - reason: Some("already_current".into()), - }); - } - tx.execute( - "UPDATE reply_decisions - SET status=?2,evidence_json=?3,updated_at=?4 - WHERE event_id=?1", - params![event_id, status, evidence_json, now], - )?; - } else { - tx.execute( - "INSERT INTO reply_decisions( - event_id, chat, author, received_at, message, vector, decision, - reason, category, context_match_count, style_match_count, - best_context_score, best_style_score, prior_similarity, - scheduled_delay_seconds, status, reply, sent_at, created_at, - updated_at, evidence_json - ) VALUES( - ?1, '__projection__', '', ?2, ?1, ?3, 'skip', 'projection', - 'uncertain', 0, 0, 0, 0, 0, 0, ?4, NULL, NULL, ?2, ?2, ?5 - )", - params![ - event_id, - now, - vector_to_bytes(&encode_vector(event_id)), - status, - evidence_json, - ], - )?; - } - tx.commit()?; - Ok(ReplyDecisionUpdate { - applied: true, - updated: true, - decision: Some(ReplyDecision { - event_id: event_id.to_string(), - status: status.to_string(), - evidence_json: evidence_json.to_string(), - updated_at: now, - }), - reason: None, - }) -} - -fn can_apply_reply_status( - previous: &str, - incoming: &str, - previous_rank: u8, - incoming_rank: u8, -) -> bool { - if incoming_rank < previous_rank { - return false; - } - if previous == incoming { - return true; - } - if is_terminal_reply_status(previous) { - return false; - } - true -} -fn is_terminal_reply_status(status: &str) -> bool { - matches!( - status, - "sent" | "skipped" | "poison" | "delivery_unknown" | "reconcile_required" - ) -} - -pub fn get_reply_decision(db_path: &Path, event_id: &str) -> Result> { - let conn = open_db_readonly(db_path)?; - ensure_retrieval_index_current(&conn)?; - conn.query_row( - "SELECT event_id,status,evidence_json,updated_at FROM reply_decisions WHERE event_id=?1", - [event_id], - |row| { - Ok(ReplyDecision { - event_id: row.get(0)?, - status: row.get(1)?, - evidence_json: row.get(2)?, - updated_at: row.get(3)?, - }) - }, - ) - .optional() - .map_err(Into::into) -} -/// How long a context-DB connection waits for a competing writer before -/// failing with SQLITE_BUSY. -/// -/// Every AutoReply room syncs the same context database, and on a multi-hundred -/// megabyte database a concurrent `context-sync-local` run can hold the write -/// lock for longer than five seconds. Timing out there fences the room and -/// exits its watcher, so wait the contention out instead. -const CONTEXT_DB_BUSY_TIMEOUT_SECS: u64 = 30; - -fn set_context_db_permissions(path: &Path) -> Result<()> { - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - - if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) { - fs::set_permissions(parent, fs::Permissions::from_mode(0o700)).with_context(|| { - format!("secure context database directory {}", parent.display()) - })?; - } - - for file in [ - path.to_path_buf(), - sqlite_sidecar_path(path, "-wal"), - sqlite_sidecar_path(path, "-shm"), - ] { - let metadata = match fs::symlink_metadata(&file) { - Ok(metadata) => metadata, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue, - Err(error) => { - return Err(error).with_context(|| { - format!("inspect context database file {}", file.display()) - }) - } - }; - if !metadata.file_type().is_file() { - anyhow::bail!( - "context database path is not a regular file: {}", - file.display() - ); - } - fs::set_permissions(&file, fs::Permissions::from_mode(0o600)) - .with_context(|| format!("secure context database file {}", file.display()))?; - } - } - Ok(()) -} - -fn sqlite_sidecar_path(path: &Path, suffix: &str) -> PathBuf { - let mut sidecar = path.as_os_str().to_os_string(); - sidecar.push(suffix); - PathBuf::from(sidecar) -} - -fn checkpoint_context_wal_truncate(conn: &Connection) -> Result<()> { - let (busy, log_frames, checkpointed_frames): (i64, i64, i64) = - conn.query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |row| { - Ok((row.get(0)?, row.get(1)?, row.get(2)?)) - })?; - if busy != 0 { - anyhow::bail!( - "context WAL truncate checkpoint remained busy ({checkpointed_frames}/{log_frames} frames checkpointed)" - ); - } - Ok(()) -} - -fn open_db_readonly(path: &Path) -> Result { - if path.is_symlink() || !path.is_file() { - anyhow::bail!("{CONTEXT_RETRIEVAL_MIGRATION_REQUIRED}"); - } - let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY) - .with_context(|| format!("open context database read-only: {}", path.display()))?; - conn.busy_timeout(Duration::from_secs(CONTEXT_DB_BUSY_TIMEOUT_SECS))?; - Ok(conn) -} - -/// Rename legacy `choi_yeonwoo_*` style tables to their `owner_*` names when an -/// older database is opened, so its data survives the owner-scope -/// generalization (R10.6). Only renames when the legacy table exists and the -/// renamed table does not, so it is safe to run on every open and on fresh -/// databases (where it does nothing). -fn rename_legacy_owner_tables(conn: &Connection) -> Result<()> { - const RENAMES: [(&str, &str); 4] = [ - ("choi_yeonwoo_style", "owner_style"), - ("choi_yeonwoo_style_profile", "owner_style_profile"), - ( - "choi_yeonwoo_recipient_style_profile", - "owner_recipient_style_profile", - ), - ( - "choi_yeonwoo_recipient_style_samples", - "owner_recipient_style_samples", - ), - ]; - let table_present = |name: &str| -> Result { - Ok(conn - .query_row( - "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?1", - params![name], - |_| Ok(()), - ) - .optional()? - .is_some()) - }; - for (legacy, renamed) in RENAMES { - if table_present(legacy)? && !table_present(renamed)? { - conn.execute_batch(&format!("ALTER TABLE {legacy} RENAME TO {renamed}"))?; - } - } - Ok(()) -} - -fn open_db(path: &Path) -> Result { - if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) { - fs::create_dir_all(parent)?; - } - set_context_db_permissions(path)?; - let was_missing = !path.exists(); - let conn = Connection::open(path)?; - conn.busy_timeout(Duration::from_secs(CONTEXT_DB_BUSY_TIMEOUT_SECS))?; - // Workers open read-only context snapshots while the watcher updates the - // shared index. WAL keeps those readers on their committed snapshot. - let journal_mode: String = conn.pragma_query_value(None, "journal_mode", |row| row.get(0))?; - if !journal_mode.eq_ignore_ascii_case("wal") { - let enabled_mode: String = - conn.query_row("PRAGMA journal_mode = WAL", [], |row| row.get(0))?; - if !enabled_mode.eq_ignore_ascii_case("wal") { - anyhow::bail!("context database could not enter WAL mode"); - } - } - set_context_db_permissions(path)?; - migrate(&conn, was_missing)?; - set_context_db_permissions(path)?; - Ok(conn) -} - -fn migrate(conn: &Connection, was_missing: bool) -> Result<()> { - // Owner-scope rename (R10.6): before the `CREATE TABLE IF NOT EXISTS owner_*` - // statements run, rename any legacy `choi_yeonwoo_*` tables so an upgrading - // database keeps its style/context data instead of orphaning it under the - // old names. `profile::migrate_owner_scope` later adds the `owner_key` - // column and records the migration marker once the owner is confirmed. - rename_legacy_owner_tables(conn)?; - conn.execute_batch("PRAGMA secure_delete = ON; - PRAGMA journal_size_limit = 0; - PRAGMA wal_autocheckpoint = 1000; - CREATE TABLE IF NOT EXISTS context_messages(id INTEGER PRIMARY KEY, source TEXT NOT NULL, chat TEXT NOT NULL, date TEXT NOT NULL, user_name TEXT NOT NULL, message TEXT NOT NULL, vector BLOB NOT NULL); - CREATE INDEX IF NOT EXISTS idx_context_messages_chat_source ON context_messages(chat, source); - CREATE TABLE IF NOT EXISTS context_retrieval_meta( - key TEXT PRIMARY KEY, - value TEXT NOT NULL - ); - CREATE VIRTUAL TABLE IF NOT EXISTS context_messages_fts USING fts5(message, user_name, chat, content='context_messages', content_rowid='id'); - CREATE TRIGGER IF NOT EXISTS context_messages_ai AFTER INSERT ON context_messages BEGIN INSERT INTO context_messages_fts(rowid,message,user_name,chat) VALUES(new.id,new.message,new.user_name,new.chat); END; - CREATE TRIGGER IF NOT EXISTS context_messages_ad AFTER DELETE ON context_messages BEGIN INSERT INTO context_messages_fts(context_messages_fts,rowid,message,user_name,chat) VALUES('delete',old.id,old.message,old.user_name,old.chat); END; - CREATE TRIGGER IF NOT EXISTS context_messages_au AFTER UPDATE ON context_messages BEGIN INSERT INTO context_messages_fts(context_messages_fts,rowid,message,user_name,chat) VALUES('delete',old.id,old.message,old.user_name,old.chat); INSERT INTO context_messages_fts(rowid,message,user_name,chat) VALUES(new.id,new.message,new.user_name,new.chat); END; - CREATE TABLE IF NOT EXISTS owner_style(id INTEGER PRIMARY KEY, source TEXT NOT NULL, chat TEXT NOT NULL, date TEXT NOT NULL, user_name TEXT NOT NULL CHECK(user_name = '최연우'), message TEXT NOT NULL, vector BLOB NOT NULL, source_row INTEGER NOT NULL DEFAULT 0, content_kind TEXT NOT NULL DEFAULT 'legacy', style_eligible INTEGER NOT NULL DEFAULT 1, policy_version TEXT NOT NULL DEFAULT 'legacy-v1', features_json TEXT NOT NULL DEFAULT '{}'); - CREATE INDEX IF NOT EXISTS idx_owner_style_chat_source ON owner_style(chat, source); - CREATE TABLE IF NOT EXISTS owner_style_profile( - chat TEXT NOT NULL, - source TEXT NOT NULL, - user_name TEXT NOT NULL CHECK(user_name = '최연우'), - sample_count INTEGER NOT NULL, - average_character_length REAL NOT NULL, - median_character_length REAL NOT NULL, - p90_character_length REAL NOT NULL, - casual_ending_count INTEGER NOT NULL, - casual_ending_counts_json TEXT NOT NULL DEFAULT '{}', - question_count INTEGER NOT NULL, - emoji_count INTEGER NOT NULL, - punctuation_count INTEGER NOT NULL, - common_endings_json TEXT NOT NULL DEFAULT '{}', - common_tokens_json TEXT NOT NULL DEFAULT '{}', - policy_version TEXT NOT NULL, - created_at TEXT NOT NULL, - PRIMARY KEY(chat, source, user_name) - ); - CREATE INDEX IF NOT EXISTS idx_owner_style_profile_chat_user ON owner_style_profile(chat, user_name); - CREATE TABLE IF NOT EXISTS response_time_stats(chat TEXT NOT NULL, source TEXT NOT NULL, user_name TEXT NOT NULL CHECK(user_name = '최연우'), sample_count INTEGER NOT NULL, average_seconds REAL NOT NULL, median_seconds REAL NOT NULL, p90_seconds REAL NOT NULL, min_seconds REAL NOT NULL, max_seconds REAL NOT NULL, max_window_seconds INTEGER NOT NULL, stddev_seconds REAL NOT NULL DEFAULT 0.0, distribution_schema_version INTEGER NOT NULL DEFAULT 0, distribution_json TEXT NOT NULL DEFAULT '{}', PRIMARY KEY(chat, source, user_name)); - CREATE INDEX IF NOT EXISTS idx_response_time_stats_chat_user ON response_time_stats(chat, user_name); - CREATE TABLE IF NOT EXISTS reply_decisions( - event_id TEXT PRIMARY KEY, - chat TEXT NOT NULL, - author TEXT NOT NULL, - received_at TEXT NOT NULL, - message TEXT NOT NULL, - vector BLOB NOT NULL, - decision TEXT NOT NULL CHECK(decision IN ('reply', 'skip')), - reason TEXT NOT NULL, - category TEXT NOT NULL, - context_match_count INTEGER NOT NULL, - style_match_count INTEGER NOT NULL, - best_context_score REAL NOT NULL, - best_style_score REAL NOT NULL, - prior_similarity REAL NOT NULL, - scheduled_delay_seconds REAL NOT NULL, - status TEXT NOT NULL, - reply TEXT, - sent_at TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - evidence_json TEXT NOT NULL DEFAULT '{}' - ); - CREATE INDEX IF NOT EXISTS idx_reply_decisions_chat_created ON reply_decisions(chat, created_at); - CREATE INDEX IF NOT EXISTS idx_reply_decisions_chat_status ON reply_decisions(chat, status);")?; - let style_columns = conn - .prepare("PRAGMA table_info(owner_style)")? - .query_map([], |row| row.get::<_, String>(1))? - .collect::>>()?; - for (name, declaration) in [ - ("source_row", "INTEGER NOT NULL DEFAULT 0"), - ("content_kind", "TEXT NOT NULL DEFAULT 'legacy'"), - ("style_eligible", "INTEGER NOT NULL DEFAULT 1"), - ("policy_version", "TEXT NOT NULL DEFAULT 'legacy-v1'"), - ("features_json", "TEXT NOT NULL DEFAULT '{}'"), - ] { - if !style_columns.iter().any(|column| column == name) { - conn.execute( - &format!("ALTER TABLE owner_style ADD COLUMN {name} {declaration}"), - [], - )?; - } - } - conn.execute( - "CREATE INDEX IF NOT EXISTS idx_owner_style_eligible - ON owner_style(chat, source, style_eligible)", - [], - )?; - let response_time_columns = conn - .prepare("PRAGMA table_info(response_time_stats)")? - .query_map([], |row| row.get::<_, String>(1))? - .collect::>>()?; - if !response_time_columns - .iter() - .any(|name| name == "stddev_seconds") - { - conn.execute( - "ALTER TABLE response_time_stats ADD COLUMN stddev_seconds REAL NOT NULL DEFAULT 0.0", - [], - )?; - } - if !response_time_columns - .iter() - .any(|name| name == "distribution_schema_version") - { - conn.execute( - "ALTER TABLE response_time_stats ADD COLUMN distribution_schema_version INTEGER NOT NULL DEFAULT 0", - [], - )?; - } - if !response_time_columns - .iter() - .any(|name| name == "distribution_json") - { - conn.execute( - "ALTER TABLE response_time_stats ADD COLUMN distribution_json TEXT NOT NULL DEFAULT '{}'", - [], - )?; - } - let reply_decision_columns = conn - .prepare("PRAGMA table_info(reply_decisions)")? - .query_map([], |row| row.get::<_, String>(1))? - .collect::>>()?; - if !reply_decision_columns - .iter() - .any(|name| name == "evidence_json") - { - conn.execute( - "ALTER TABLE reply_decisions ADD COLUMN evidence_json TEXT NOT NULL DEFAULT '{}'", - [], - )?; - } - if was_missing { - conn.execute( - "INSERT INTO context_retrieval_meta(key, value) VALUES ('fts_schema', ?1) - ON CONFLICT(key) DO UPDATE SET value = excluded.value", - [RETRIEVAL_INDEX_SCHEMA_VERSION], - )?; - } - migrate_live_context_schema(conn)?; - migrate_style_policy(conn)?; - migrate_response_time_distribution_schema(conn)?; - Ok(()) -} - -fn migrate_style_policy(conn: &Connection) -> Result<()> { - const MIGRATION_KEY: &str = "style_policy_version"; - let current: Option = conn - .query_row( - "SELECT value FROM context_retrieval_meta WHERE key = ?1", - [MIGRATION_KEY], - |row| row.get(0), - ) - .optional()?; - if current.as_deref() == Some(STYLE_POLICY_VERSION) { - return Ok(()); - } - - let tx = conn.unchecked_transaction()?; - let style_rows = { - let mut stmt = tx.prepare("SELECT id, message FROM owner_style ORDER BY id ASC")?; - let rows = stmt - .query_map([], |row| { - Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?)) - })? - .collect::>>()?; - rows - }; - for (id, message) in style_rows { - let features = classify_style_message(&message); - tx.execute( - "UPDATE owner_style - SET content_kind = ?2, style_eligible = ?3, - policy_version = ?4, features_json = ?5 - WHERE id = ?1", - params![ - id, - features.content_kind, - features.style_eligible as i64, - STYLE_POLICY_VERSION, - features.features_json, - ], - )?; - } - tx.execute( - "DELETE FROM owner_recipient_style_samples - WHERE style_message_id IN ( - SELECT id FROM owner_style - WHERE style_eligible <> 1 OR policy_version <> ?1 - )", - [STYLE_POLICY_VERSION], - )?; - - let now = Utc::now().to_rfc3339(); - tx.execute("DELETE FROM owner_style_profile", [])?; - let room_sources = { - let mut stmt = tx.prepare( - "SELECT DISTINCT source, chat FROM owner_style - WHERE user_name = ?1 AND style_eligible = 1 - AND policy_version = ?2 - ORDER BY source ASC, chat ASC", - )?; - let rows = stmt - .query_map(params![STYLE_USER, STYLE_POLICY_VERSION], |row| { - Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)) - })? - .collect::>>()?; - rows - }; - for (source, chat) in room_sources { - let messages = { - let mut stmt = tx.prepare( - "SELECT message FROM owner_style - WHERE source = ?1 AND chat = ?2 AND user_name = ?3 - AND style_eligible = 1 AND policy_version = ?4 - ORDER BY source_row ASC, id ASC", - )?; - let rows = stmt - .query_map( - params![source, chat, STYLE_USER, STYLE_POLICY_VERSION], - |row| row.get::<_, String>(0), - )? - .collect::>>()?; - rows - }; - let mut accumulator = StyleProfileAccumulator::default(); - for message in messages { - let features = classify_style_message(&message); - if features.style_eligible { - accumulator.add(&message, &features); - } - } - if let Some(profile) = accumulator.finish(&chat, &source, STYLE_USER) { - insert_style_profile(&tx, &profile, &now)?; - } - } - - tx.execute("DELETE FROM owner_recipient_style_profile", [])?; - let recipient_rows = { - let mut stmt = tx.prepare( - "SELECT samples.source, samples.chat_id, sources.chat, - samples.recipient, samples.confidence, styles.message - FROM owner_recipient_style_samples samples - JOIN owner_style styles ON styles.id = samples.style_message_id - JOIN context_sources sources ON sources.source = samples.source - WHERE styles.user_name = ?1 AND styles.style_eligible = 1 - AND styles.policy_version = ?2 - ORDER BY samples.source ASC, samples.chat_id ASC, - samples.recipient ASC, samples.reply_log_id ASC", - )?; - let rows = stmt - .query_map(params![STYLE_USER, STYLE_POLICY_VERSION], |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, i64>(1)?, - row.get::<_, String>(2)?, - row.get::<_, String>(3)?, - row.get::<_, f64>(4)?, - row.get::<_, String>(5)?, - )) - })? - .collect::>>()?; - rows - }; - let mut recipient_accumulators: BTreeMap< - (String, i64, String, String), - RecipientStyleAccumulator, - > = BTreeMap::new(); - for (source, chat_id, chat, recipient, confidence, message) in recipient_rows { - if !confidence.is_finite() || !(0.0..=1.0).contains(&confidence) || confidence == 0.0 { - anyhow::bail!("recipient style confidence is malformed"); - } - let features = classify_style_message(&message); - if !features.style_eligible { - continue; - } - let accumulator = recipient_accumulators - .entry((source, chat_id, chat, recipient)) - .or_default(); - accumulator.style.add(&message, &features); - accumulator.sample_count += 1; - accumulator.confidence_sum += confidence; - } - for ((source, chat_id, chat, recipient), accumulator) in recipient_accumulators { - let Some(profile) = accumulator.style.finish(&chat, &source, STYLE_USER) else { - continue; - }; - tx.execute( - "INSERT INTO owner_recipient_style_profile( - source, chat_id, chat, recipient, user_name, sample_count, - confidence_sum, average_character_length, median_character_length, - p90_character_length, casual_ending_count, casual_ending_counts_json, - question_count, emoji_count, punctuation_count, common_endings_json, - common_tokens_json, policy_version, updated_at - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, - ?13, ?14, ?15, ?16, ?17, ?18, ?19)", - params![ - source, - chat_id, - chat, - recipient, - STYLE_USER, - accumulator.sample_count as i64, - accumulator.confidence_sum, - profile.average_character_length, - profile.median_character_length, - profile.p90_character_length, - profile.casual_ending_count as i64, - profile.casual_ending_counts_json, - profile.question_count as i64, - profile.emoji_count as i64, - profile.punctuation_count as i64, - profile.common_endings_json, - profile.common_tokens_json, - STYLE_POLICY_VERSION, - now, - ], - )?; - } - - tx.execute( - "INSERT INTO context_retrieval_meta(key, value) VALUES (?1, ?2) - ON CONFLICT(key) DO UPDATE SET value = excluded.value", - params![MIGRATION_KEY, STYLE_POLICY_VERSION], - )?; - tx.commit()?; - Ok(()) -} - -fn migrate_response_time_distribution_schema(conn: &Connection) -> Result<()> { - let migration_key = "response_time_distribution_schema"; - let expected_version = RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION.to_string(); - let current: Option = conn - .query_row( - "SELECT value FROM context_retrieval_meta WHERE key = ?1", - [migration_key], - |row| row.get(0), - ) - .optional()?; - let mut live_rows = conn - .prepare( - "SELECT stats.chat, stats.source, sources.chat_id, - stats.distribution_schema_version, stats.distribution_json - FROM response_time_stats stats - JOIN context_sources sources ON sources.source = stats.source - WHERE stats.user_name = ?1 - ORDER BY stats.source ASC", - )? - .query_map([STYLE_USER], |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, String>(1)?, - row.get::<_, i64>(2)?, - row.get::<_, i64>(3)?, - row.get::<_, String>(4)?, - )) - })? - .collect::>>()?; - live_rows.sort(); - let valid = |schema_version: i64, distribution_json: &str| { - schema_version == RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION as i64 - && serde_json::from_str::(distribution_json) - .ok() - .as_ref() - .is_some_and(response_time_distribution_is_valid) - }; - if current.as_deref() == Some(expected_version.as_str()) - && live_rows - .iter() - .all(|(_, _, _, schema_version, distribution_json)| { - valid(*schema_version, distribution_json) - }) - { - return Ok(()); - } - let tx = conn.unchecked_transaction()?; - for (_chat, source, chat_id, schema_version, distribution_json) in live_rows { - if current.as_deref() == Some(expected_version.as_str()) - && valid(schema_version, &distribution_json) - { - continue; - } - let mut delays = tx - .prepare( - "SELECT delay_seconds FROM response_time_samples - WHERE source = ?1 AND chat_id = ?2 - ORDER BY delay_seconds ASC, reply_log_id ASC", - )? - .query_map(params![source, chat_id], |row| row.get::<_, f64>(0))? - .collect::>>()?; - delays.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal)); - let distribution = fit_response_time_distribution(&delays); - let (schema_version, distribution_json) = match distribution { - Some(distribution) => ( - distribution.schema_version as i64, - serde_json::to_string(&distribution)?, - ), - None => (0, "{}".to_string()), - }; - tx.execute( - "UPDATE response_time_stats - SET distribution_schema_version = ?3, distribution_json = ?4 - WHERE source = ?1 AND user_name = ?2", - params![source, STYLE_USER, schema_version, distribution_json], - )?; - } - tx.execute( - "INSERT INTO context_retrieval_meta(key, value) VALUES (?1, ?2) - ON CONFLICT(key) DO UPDATE SET value = excluded.value", - params![migration_key, expected_version], - )?; - tx.commit()?; - Ok(()) -} - -fn migrate_live_context_schema(conn: &Connection) -> Result<()> { - let tx = conn.unchecked_transaction()?; - tx.execute_batch( - "CREATE TABLE IF NOT EXISTS context_sources( - source TEXT PRIMARY KEY, - kind TEXT NOT NULL CHECK(kind = 'local_db'), - account_fingerprint TEXT NOT NULL, - chat_id INTEGER NOT NULL CHECK(chat_id > 0), - chat TEXT NOT NULL, - authoritative INTEGER NOT NULL DEFAULT 0 CHECK(authoritative IN (0, 1)), - checkpoint_log_id INTEGER NOT NULL DEFAULT 0 CHECK(checkpoint_log_id >= 0), - pending_human_log_id INTEGER, - pending_human_sent_at INTEGER, - pending_human_name TEXT, - pending_burst_json TEXT NOT NULL DEFAULT '[]', - summary_dirty INTEGER NOT NULL DEFAULT 0 CHECK(summary_dirty IN (0, 1)), - sync_status TEXT NOT NULL DEFAULT 'ready', - updated_at TEXT NOT NULL, - UNIQUE(account_fingerprint, chat_id) - ); - CREATE UNIQUE INDEX IF NOT EXISTS idx_context_sources_authoritative_chat - ON context_sources(chat) WHERE authoritative = 1; - CREATE INDEX IF NOT EXISTS idx_context_sources_chat - ON context_sources(chat, authoritative, updated_at); - CREATE TABLE IF NOT EXISTS context_live_events( - source TEXT NOT NULL, - chat_id INTEGER NOT NULL CHECK(chat_id > 0), - log_id INTEGER NOT NULL CHECK(log_id > 0), - sent_at INTEGER NOT NULL, - sender_name TEXT NOT NULL, - message_digest TEXT NOT NULL, - disposition TEXT NOT NULL, - auto_generated INTEGER NOT NULL CHECK(auto_generated IN (0, 1)), - context_message_id INTEGER, - style_message_id INTEGER, - created_at TEXT NOT NULL, - PRIMARY KEY(source, chat_id, log_id), - FOREIGN KEY(source) REFERENCES context_sources(source) - ); - CREATE INDEX IF NOT EXISTS idx_context_live_events_context_message - ON context_live_events(context_message_id); - CREATE INDEX IF NOT EXISTS idx_context_live_events_style_message - ON context_live_events(style_message_id); - CREATE TABLE IF NOT EXISTS response_time_samples( - source TEXT NOT NULL, - chat_id INTEGER NOT NULL CHECK(chat_id > 0), - reply_log_id INTEGER NOT NULL CHECK(reply_log_id > 0), - prompt_log_id INTEGER NOT NULL CHECK(prompt_log_id > 0), - recipient TEXT NOT NULL, - delay_seconds REAL NOT NULL, - PRIMARY KEY(source, chat_id, reply_log_id), - FOREIGN KEY(source) REFERENCES context_sources(source) - ); - CREATE INDEX IF NOT EXISTS idx_response_time_samples_source_chat - ON response_time_samples(source, chat_id, reply_log_id); - CREATE TABLE IF NOT EXISTS owner_recipient_style_samples( - source TEXT NOT NULL, - chat_id INTEGER NOT NULL CHECK(chat_id > 0), - reply_log_id INTEGER NOT NULL CHECK(reply_log_id > 0), - recipient TEXT NOT NULL, - style_message_id INTEGER NOT NULL, - burst_size INTEGER NOT NULL CHECK(burst_size > 0), - confidence REAL NOT NULL CHECK(confidence > 0.0 AND confidence <= 1.0), - PRIMARY KEY(source, chat_id, reply_log_id, recipient), - FOREIGN KEY(source) REFERENCES context_sources(source) - ); - CREATE INDEX IF NOT EXISTS idx_recipient_style_samples_recipient - ON owner_recipient_style_samples(source, chat_id, recipient); - CREATE INDEX IF NOT EXISTS idx_recipient_style_samples_lookup - ON owner_recipient_style_samples( - source, recipient, reply_log_id DESC, style_message_id DESC - ); - CREATE TABLE IF NOT EXISTS owner_recipient_style_profile( - source TEXT NOT NULL, - chat_id INTEGER NOT NULL CHECK(chat_id > 0), - chat TEXT NOT NULL, - recipient TEXT NOT NULL, - user_name TEXT NOT NULL CHECK(user_name = '최연우'), - sample_count INTEGER NOT NULL, - confidence_sum REAL NOT NULL, - average_character_length REAL NOT NULL, - median_character_length REAL NOT NULL, - p90_character_length REAL NOT NULL, - casual_ending_count INTEGER NOT NULL, - casual_ending_counts_json TEXT NOT NULL DEFAULT '{}', - question_count INTEGER NOT NULL, - emoji_count INTEGER NOT NULL, - punctuation_count INTEGER NOT NULL, - common_endings_json TEXT NOT NULL DEFAULT '{}', - common_tokens_json TEXT NOT NULL DEFAULT '{}', - policy_version TEXT NOT NULL, - updated_at TEXT NOT NULL, - PRIMARY KEY(source, chat_id, recipient) - ); - CREATE INDEX IF NOT EXISTS idx_recipient_style_profile_lookup - ON owner_recipient_style_profile(chat, recipient, sample_count); - CREATE TABLE IF NOT EXISTS context_message_topics( - message_id INTEGER NOT NULL, - topic TEXT NOT NULL CHECK(length(topic) > 0), - PRIMARY KEY(message_id, topic) - ); - CREATE INDEX IF NOT EXISTS idx_context_message_topics_topic - ON context_message_topics(topic, message_id); - CREATE TABLE IF NOT EXISTS context_topic_stats( - chat TEXT NOT NULL, - source TEXT NOT NULL, - topic TEXT NOT NULL, - message_count INTEGER NOT NULL CHECK(message_count >= 0), - last_date TEXT NOT NULL, - PRIMARY KEY(chat, source, topic) - ); - CREATE TABLE IF NOT EXISTS context_reference_packs( - id INTEGER PRIMARY KEY, - pack_key TEXT NOT NULL UNIQUE, - source TEXT NOT NULL, - chat TEXT NOT NULL, - chat_id INTEGER NOT NULL, - user_name TEXT NOT NULL, - started_at TEXT NOT NULL, - ended_at TEXT NOT NULL, - start_log_id INTEGER NOT NULL, - end_log_id INTEGER NOT NULL, - message_count INTEGER NOT NULL, - image_count INTEGER NOT NULL, - quality_score INTEGER NOT NULL, - topics TEXT NOT NULL DEFAULT '', - what_text TEXT NOT NULL, - how_text TEXT NOT NULL, - why_text TEXT NOT NULL, - body TEXT NOT NULL, - vector BLOB NOT NULL, - context_message_id INTEGER, - policy_version TEXT NOT NULL, - created_at TEXT NOT NULL - ); - CREATE INDEX IF NOT EXISTS idx_reference_packs_chat - ON context_reference_packs(chat, quality_score DESC, end_log_id DESC);", - )?; - tx.execute( - "INSERT OR IGNORE INTO context_retrieval_meta(key, value) - VALUES ('live_context_schema', ?1)", - [LIVE_CONTEXT_SCHEMA_VERSION], - )?; - let version: Option = tx - .query_row( - "SELECT value FROM context_retrieval_meta WHERE key = 'live_context_schema'", - [], - |row| row.get(0), - ) - .optional()?; - match version.as_deref() { - Some("1") => { - backfill_message_topics(&tx)?; - tx.execute( - "UPDATE context_retrieval_meta - SET value = ?1 - WHERE key = 'live_context_schema'", - [LIVE_CONTEXT_SCHEMA_VERSION], - )?; - ensure_topic_lexicon(&tx)?; - } - Some(value) if value == LIVE_CONTEXT_SCHEMA_VERSION => { - ensure_topic_lexicon(&tx)?; - } - _ => anyhow::bail!("live context index migration required"), - } - tx.commit()?; - Ok(()) -} -fn ensure_retrieval_index_current(conn: &Connection) -> Result<()> { - let version = match conn - .query_row( - "SELECT value FROM context_retrieval_meta WHERE key = 'fts_schema'", - [], - |row| row.get::<_, String>(0), - ) - .optional() - { - Ok(version) => version, - Err(error) if error.to_string().contains("no such table") => { - anyhow::bail!("{CONTEXT_RETRIEVAL_MIGRATION_REQUIRED}"); - } - Err(error) => return Err(error.into()), - }; - if version.as_deref() != Some(RETRIEVAL_INDEX_SCHEMA_VERSION) { - anyhow::bail!("{CONTEXT_RETRIEVAL_MIGRATION_REQUIRED}"); - } - Ok(()) -} - -fn rebuild_retrieval_index(conn: &Connection) -> Result<()> { - conn.execute( - "INSERT INTO context_messages_fts(context_messages_fts) VALUES ('rebuild')", - [], - )?; - conn.execute( - "INSERT INTO context_retrieval_meta(key, value) VALUES ('fts_schema', ?1) - ON CONFLICT(key) DO UPDATE SET value = excluded.value", - [RETRIEVAL_INDEX_SCHEMA_VERSION], - )?; - Ok(()) -} - -fn keyword_search_excluding( - conn: &Connection, - chat: Option<&str>, - source: Option<&str>, - query: &str, - excluded_context_ids: &BTreeSet, -) -> Result> { - let match_query = query - .split_whitespace() - .map(|term| format!("\"{}\"", term.replace('"', ""))) - .collect::>() - .join(" OR "); - if match_query.trim().is_empty() { - return Ok(Vec::new()); - } - let mut stmt = conn.prepare( - "SELECT m.id,m.chat,m.source,m.date,m.user_name,m.message, - bm25(context_messages_fts) - FROM context_messages_fts f - JOIN context_messages m ON m.id=f.rowid - WHERE context_messages_fts MATCH ?1 - AND (?2 IS NULL OR m.chat=?2) - AND (?3 IS NULL OR m.source=?3) - ORDER BY bm25(context_messages_fts), m.id ASC - LIMIT ?4", - )?; - let candidate_limit = CONTEXT_KEYWORD_CANDIDATE_CAP.saturating_add(excluded_context_ids.len()); - let rows = stmt.query_map( - params![match_query, chat, source, candidate_limit as i64], - |row| { - Ok(ContextCandidate { - id: row.get(0)?, - result: ContextResult { - chat: row.get(1)?, - source: row.get(2)?, - date: row.get(3)?, - user: row.get(4)?, - message: row.get(5)?, - score: -row.get::<_, f64>(6)? as f32, - mode: "keyword".into(), - }, - }) - }, - )?; - let mut results = Vec::new(); - for row in rows { - let row = row?; - if !excluded_context_ids.contains(&row.id) { - results.push(row); - } - if results.len() == CONTEXT_KEYWORD_CANDIDATE_CAP { - break; - } - } - Ok(results) -} - -fn vector_search_excluding( - conn: &Connection, - chat: Option<&str>, - source: Option<&str>, - query: &str, - excluded_context_ids: &BTreeSet, - embedder: &dyn Embedder, -) -> Result> { - let query_vector = embedder.embed(query); - if query_vector.iter().all(|value| *value == 0.0) { - return Ok(Vec::new()); - } - - // Restrict semantic scoring to the bounded lexical candidate set. A - // missing lexical match is an explicit no-match result. - let match_query = query - .split_whitespace() - .map(|term| format!("\"{}\"", term.replace('"', ""))) - .collect::>() - .join(" OR "); - let mut candidates = Vec::new(); - if !match_query.trim().is_empty() { - let mut stmt = conn.prepare( - "SELECT m.id,m.chat,m.source,m.date,m.user_name,m.message,m.vector - FROM context_messages_fts f - JOIN context_messages m ON m.id=f.rowid - WHERE context_messages_fts MATCH ?1 - AND (?2 IS NULL OR m.chat=?2) - AND (?3 IS NULL OR m.source=?3) - ORDER BY bm25(context_messages_fts), m.id ASC - LIMIT ?4", - )?; - let candidate_limit = - CONTEXT_VECTOR_CANDIDATE_CAP.saturating_add(excluded_context_ids.len()); - let rows = stmt.query_map( - params![match_query, chat, source, candidate_limit as i64], - |row| { - let bytes: Vec = row.get(6)?; - if bytes.len() != VECTOR_DIM * 4 { - return Err(rusqlite::Error::InvalidColumnType( - 6, - "vector".into(), - rusqlite::types::Type::Blob, - )); - } - Ok(( - row.get::<_, i64>(0)?, - ContextResult { - chat: row.get(1)?, - source: row.get(2)?, - date: row.get(3)?, - user: row.get(4)?, - message: row.get(5)?, - score: 0.0, - mode: "vector".into(), - }, - bytes_to_vector(&bytes), - )) - }, - )?; - for row in rows { - let (id, mut result, vector) = row?; - if excluded_context_ids.contains(&id) { - continue; - } - result.score = cosine(&query_vector, &vector); - candidates.push(ContextCandidate { id, result }); - if candidates.len() == CONTEXT_VECTOR_CANDIDATE_CAP { - break; - } - } - } - - candidates.sort_by(|a, b| { - b.result - .score - .partial_cmp(&a.result.score) - .unwrap_or(Ordering::Equal) - .then_with(|| a.id.cmp(&b.id)) - }); - Ok(candidates) -} - -fn topic_search_excluding( - conn: &Connection, - chat: Option<&str>, - source: Option<&str>, - query: &str, - excluded_context_ids: &BTreeSet, -) -> Result> { - if !context_topic_tables_ready(conn)? { - return Ok(Vec::new()); - } - let topics = classify_message_topics(query); - if topics.is_empty() { - return Ok(Vec::new()); - } - let mut stmt = conn.prepare( - "SELECT m.id, m.chat, m.source, m.date, m.user_name, m.message - FROM context_message_topics t - JOIN context_messages m ON m.id = t.message_id - WHERE t.topic = ?1 - AND (?2 IS NULL OR m.chat = ?2) - AND (?3 IS NULL OR m.source = ?3) - ORDER BY m.id DESC - LIMIT ?4", - )?; - let candidate_limit = CONTEXT_TOPIC_CANDIDATE_CAP.saturating_add(excluded_context_ids.len()); - let mut results = Vec::new(); - let mut seen = BTreeSet::new(); - for topic in topics { - let rows = stmt.query_map( - params![topic, chat, source, candidate_limit as i64], - |row| { - Ok(ContextCandidate { - id: row.get(0)?, - result: ContextResult { - chat: row.get(1)?, - source: row.get(2)?, - date: row.get(3)?, - user: row.get(4)?, - message: row.get(5)?, - score: 1.0, - mode: "topic".into(), - }, - }) - }, - )?; - for row in rows { - let row = row?; - if excluded_context_ids.contains(&row.id) || !seen.insert(row.id) { - continue; - } - results.push(row); - if results.len() == CONTEXT_TOPIC_CANDIDATE_CAP { - return Ok(results); - } - } - } - Ok(results) -} - -fn hybrid_search_excluding( - conn: &Connection, - chat: Option<&str>, - source: Option<&str>, - query: &str, - limit: usize, - excluded_context_ids: &BTreeSet, - embedder: &dyn Embedder, -) -> Result> { - let keyword = keyword_search_excluding(conn, chat, source, query, excluded_context_ids)?; - // An empty vector query is a fail-closed hybrid result: keyword evidence - // remains available, while no synthetic vector score is introduced. - let vector = if embedder.embed(query).iter().all(|value| *value == 0.0) { - Vec::new() - } else { - vector_search_excluding(conn, chat, source, query, excluded_context_ids, embedder)? - }; - let mut merged = keyword - .into_iter() - .enumerate() - .map(|(rank, mut item)| { - item.result.score = 1.0 / (rank as f32 + 1.0); - item.result.mode = "hybrid".into(); - item - }) - .collect::>(); - for (rank, item) in vector.into_iter().enumerate() { - let score = 1.0 / (rank as f32 + 1.0); - if let Some(existing) = merged.iter_mut().find(|existing| existing.id == item.id) { - existing.result.score += score; - } else { - let mut item = item; - item.result.score = score; - item.result.mode = "hybrid".into(); - merged.push(item); - } - } - let topics = topic_search_excluding(conn, chat, source, query, excluded_context_ids)?; - for (rank, item) in topics.into_iter().enumerate() { - let score = 1.0 / (rank as f32 + 1.0); - if let Some(existing) = merged.iter_mut().find(|existing| existing.id == item.id) { - existing.result.score += score; - } else { - let mut item = item; - item.result.score = score; - item.result.mode = "hybrid".into(); - merged.push(item); - } - } - const REFERENCE_PREFIX: &str = "[설명자료]"; - for item in &mut merged { - if item.result.message.starts_with(REFERENCE_PREFIX) { - item.result.score += 0.35; - } - } - merged.sort_by(|a, b| { - b.result - .score - .partial_cmp(&a.result.score) - .unwrap_or(Ordering::Equal) - .then_with(|| a.id.cmp(&b.id)) - }); - Ok(merged - .into_iter() - .take(limit.min(CONTEXT_KEYWORD_CANDIDATE_CAP)) - .map(|candidate| candidate.result) - .collect()) -} - -/// 임베딩 공급원 추상화(착탈식 임베딩 인터페이스). 검색이 사용하는 벡터 -/// 표현을 이 트레이트 뒤로 감춰, 기본 로컬 구현 외에 로컬 임베딩 모델이나 -/// ANN 인덱스를 붙일 수 있게 한다. 기본 구현은 외부 네트워크 없이 동작한다. -pub trait Embedder { - /// 임베딩 벡터의 차원 수. - fn dim(&self) -> usize; - /// 입력 문자열을 벡터로 인코딩한다. - fn embed(&self, text: &str) -> Vec; - /// 외부 네트워크 사용 없이 로컬에서만 동작하는지 여부. - /// `Dataset_Builder`는 반드시 `true`인 임베더만 허용한다(R3-12). - fn is_local_only(&self) -> bool; -} - -/// 기본 임베더. 기존 결정적 해시 벡터(`encode_vector`)를 그대로 감싼다. -/// 외부 없이 동작하며 재현 가능한(결정적) 벡터를 만든다. 이 구현을 기본으로 -/// 주입하면 검색 동작이 리팩터링 이전과 완전히 동일하게 유지된다. -#[derive(Debug, Clone, Copy, Default)] -pub struct LocalHashEmbedder; - -impl LocalHashEmbedder { - pub fn new() -> Self { - LocalHashEmbedder - } -} - -impl Embedder for LocalHashEmbedder { - fn dim(&self) -> usize { - VECTOR_DIM - } - - fn embed(&self, text: &str) -> Vec { - encode_vector(text) - } - - fn is_local_only(&self) -> bool { - true - } -} - -fn encode_vector(text: &str) -> Vec { - let mut vector = vec![0.0; VECTOR_DIM]; - for token in text - .split(|c: char| !c.is_alphanumeric()) - .filter(|t| !t.is_empty()) - .map(str::to_lowercase) - { - for window in token - .as_bytes() - .windows(3) - .chain(token.as_bytes().windows(2)) - { - let mut hash = 2166136261u32; - for byte in window { - hash = (hash ^ u32::from(*byte)).wrapping_mul(16777619); - } - vector[(hash as usize) % VECTOR_DIM] += if hash & 1 == 0 { 1.0 } else { -1.0 }; - } - } - let norm = vector.iter().map(|v| v * v).sum::().sqrt(); - if norm > 0.0 { - for value in &mut vector { - *value /= norm; - } - } - vector -} -fn cosine(a: &[f32], b: &[f32]) -> f32 { - a.iter().zip(b).map(|(l, r)| l * r).sum() -} -fn vector_to_bytes(vector: &[f32]) -> Vec { - vector.iter().flat_map(|v| v.to_le_bytes()).collect() -} -fn bytes_to_vector(bytes: &[u8]) -> Vec { - bytes - .chunks_exact(4) - .map(|c| f32::from_le_bytes(c.try_into().unwrap())) - .collect() -} - +//! Context store behind auto-reply: a private SQLite index of chat history, +//! owner style, response timing and reply decisions, read back as reply +//! bundles. +//! +//! Each submodule owns one body of that knowledge: +//! +//! - `store` opens the context database file and guards it: private +//! permissions, WAL, and read-only handles that fail closed. +//! - `schema` owns the tables, the retrieval-index marker and every migration. +//! - `embedding` owns the message vector and its byte encoding. +//! - `csv_import` imports an exported chat CSV into the index. +//! - `live` ingests local-DB events and owns each live source's sync state. +//! - `self_send` recognizes the assistant's own sends so they are never +//! learned as owner style or pacing. +//! - `style` classifies owner messages and builds the owner and recipient +//! style profiles. +//! - `topics` owns the topic lexicon and per-message topic rows. +//! - `response_time` fits the owner's reply-delay distribution. +//! - `reply_decision` records reply decisions and their status order. +//! - `search` ranks keyword, vector, topic and hybrid retrieval candidates. +//! - `reply_bundle` assembles the versioned bundle a reply worker reads. +//! - `redact` hashes provenance and strips absolute paths from output. + +mod csv_import; +mod embedding; +mod live; +mod redact; +mod reply_bundle; +mod reply_decision; +mod response_time; +mod schema; +mod search; +mod self_send; +mod store; +mod style; #[cfg(test)] mod tests; +mod topics; + +pub use csv_import::{index_csv, index_csv_with_queue}; +pub use embedding::{Embedder, LocalHashEmbedder}; +pub use live::{ + ingest_live_context_events, live_context_source_id, live_context_sync_state, LiveContextEvent, + LiveContextIngestResult, LiveContextSyncState, +}; +pub use redact::{ + provenance_id, redact_absolute_paths, redact_context_results, redact_reply_decisions, + redact_response_time_stats, +}; +pub use reply_bundle::{ + context_reply_bundle, context_reply_bundle_for_recipient_excluding_live_events, + context_reply_bundle_for_recipient_excluding_live_events_json, context_reply_bundle_json, + ContextReplyBundle, RecipientContextReplyBundle, CONTEXT_REPLY_BUNDLE_MAX_EXCLUDED_LOG_IDS, +}; +pub use reply_decision::{ + get_reply_decision, project_reply_decision, record_reply_decision, reply_decision_search, + update_reply_decision, ReplyDecision, ReplyDecisionMatch, ReplyDecisionUpdate, +}; +pub use response_time::{ + response_time_stats, ResponseTimeComponent, ResponseTimeDistribution, ResponseTimeStats, +}; +pub use schema::ensure_live_context_schema; +pub use search::{search, search_with_embedder, style_search, ContextResult}; +pub use self_send::{ + classify_auto_generated_self_events, confirmed_self_sends_from_queue, + repair_bot_sent_style_samples, AutoGeneratedSelfEventClassification, ConfirmedSelfSend, + OutgoingSelfEvent, SelfSendRepairMatch, SelfSendRepairReport, SELF_SEND_REPAIR_WINDOW_SECONDS, +}; +pub use store::default_db_path; +pub use style::{ + classify_style_ending_class, derive_honorific_formality, + derive_style_profile_honorific_formality, is_contraction_token, recipient_style_profile, + recipient_style_profile_json, style_profile, style_profile_json, Formality, Honorific, + RecipientStyleProfile, StyleEndingClass, StyleProfile, StyleSignals, STYLE_POLICY_VERSION, +}; diff --git a/src/context/redact.rs b/src/context/redact.rs new file mode 100644 index 00000000..5475bbe8 --- /dev/null +++ b/src/context/redact.rs @@ -0,0 +1,58 @@ +//! Output redaction: provenance hashes and absolute-path stripping applied +//! before context results leave the process. + +use super::reply_decision::ReplyDecisionMatch; +use super::response_time::ResponseTimeStats; +use super::search::ContextResult; +use sha2::{Digest, Sha256}; +use std::path::Path; + +pub fn provenance_id(source: &str) -> String { + format!("source:{}", hex::encode(Sha256::digest(source.as_bytes()))) +} + +pub fn redact_absolute_paths(value: &mut serde_json::Value) { + match value { + serde_json::Value::String(text) if Path::new(text).is_absolute() => { + *text = provenance_id(text); + } + serde_json::Value::Array(values) => { + for value in values { + redact_absolute_paths(value); + } + } + serde_json::Value::Object(values) => { + for value in values.values_mut() { + redact_absolute_paths(value); + } + } + _ => {} + } +} + +pub fn redact_context_results(results: &mut [ContextResult]) { + for result in results { + result.source = provenance_id(&result.source); + } +} + +pub fn redact_response_time_stats(stats: &mut Option) { + if let Some(stats) = stats { + stats.source = provenance_id(&stats.source); + } +} + +pub fn redact_reply_decisions(results: &mut [ReplyDecisionMatch]) { + for result in results { + if let Ok(mut evidence) = serde_json::from_str::(&result.evidence_json) { + redact_absolute_paths(&mut evidence); + result.evidence_json = + serde_json::to_string(&evidence).unwrap_or_else(|_| "{}".to_string()); + } else { + result.evidence_json = serde_json::json!({ + "redacted_evidence": provenance_id(&result.evidence_json) + }) + .to_string(); + } + } +} diff --git a/src/context/reply_bundle.rs b/src/context/reply_bundle.rs new file mode 100644 index 00000000..9875f6b1 --- /dev/null +++ b/src/context/reply_bundle.rs @@ -0,0 +1,508 @@ +//! The versioned reply bundle (`ContextReplyBundle`, +//! `RecipientContextReplyBundle`) read in one snapshot, with live-burst +//! exclusions and a JSON size bound. + +use super::embedding::LocalHashEmbedder; +use super::live::preferred_context_source; +use super::redact::{provenance_id, redact_absolute_paths, redact_reply_decisions}; +use super::reply_decision::{reply_decision_search_with_connection_excluding, ReplyDecisionMatch}; +use super::response_time::{response_time_stats_with_connection, ResponseTimeStats}; +use super::schema::ensure_retrieval_index_current; +use super::search::{ + recipient_style_search_with_connection, search_with_connection_excluding, + style_search_with_connection, ContextResult, +}; +use super::store::open_db_readonly; +use super::style::{ + recipient_style_profile_with_connection, style_profile_with_connection, + validate_style_profile_laughter, RecipientStyleProfile, StyleProfile, + RECIPIENT_STYLE_MIN_CONFIDENCE, RECIPIENT_STYLE_MIN_SAMPLES, STYLE_POLICY_VERSION, STYLE_USER, +}; +use anyhow::{Context, Result}; +use rusqlite::{params, Connection}; +use serde::Serialize; +use std::collections::BTreeSet; +use std::path::Path; + +pub const CONTEXT_REPLY_BUNDLE_MAX_EXCLUDED_LOG_IDS: usize = 6; + +pub(super) const CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION: u32 = 2; + +pub(super) const RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION: u32 = 3; + +pub(super) const CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT: usize = 8; + +pub(super) const CONTEXT_REPLY_BUNDLE_STYLE_LIMIT: usize = 12; + +pub(super) const CONTEXT_REPLY_BUNDLE_DECISION_LIMIT: usize = 6; + +const CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES: usize = 64 * 1024; + +#[derive(Debug, Clone, Serialize)] +pub struct ContextReplyBundle { + pub schema_version: u32, + pub context: Vec, + pub styles: Vec, + pub prior_decisions: Vec, + pub style_profile: Option, + pub response_time: Option, +} + +#[derive(Debug, Clone, Serialize)] +pub struct RecipientContextReplyBundle { + pub schema_version: u32, + pub recipient: String, + pub context: Vec, + pub styles: Vec, + pub prior_decisions: Vec, + pub style_profile: Option, + pub recipient_style_profile: Option, + pub response_time: Option, +} + +impl ContextReplyBundle { + fn validate_json_size(&self) -> Result<()> { + if self + .styles + .iter() + .any(|style| crate::reply_policy::validate_auto_reply_laughter(&style.message).is_err()) + { + anyhow::bail!("context reply bundle contains disallowed style evidence"); + } + if let Some(profile) = &self.style_profile { + if profile.sample_count == 0 || profile.user != STYLE_USER { + anyhow::bail!("context reply bundle style profile is invalid"); + } + if profile.policy_version != STYLE_POLICY_VERSION { + anyhow::bail!("context reply bundle style policy mismatch"); + } + validate_style_profile_laughter(profile, "context reply bundle style profile")?; + } + for prior in &self.prior_decisions { + let evidence: serde_json::Value = serde_json::from_str(&prior.evidence_json) + .context("context reply bundle evidence JSON is invalid")?; + if !evidence.is_object() { + anyhow::bail!("context reply bundle evidence JSON is not an object"); + } + } + let bytes = serde_json::to_vec(self)?; + if bytes.len() > CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES { + anyhow::bail!( + "context reply bundle exceeds {} bytes", + CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES + ); + } + Ok(()) + } + fn redact_provenance(&mut self) { + for result in self.context.iter_mut().chain(self.styles.iter_mut()) { + result.source = provenance_id(&result.source); + } + if let Some(profile) = self.style_profile.as_mut() { + profile.source = provenance_id(&profile.source); + } + if let Some(response_time) = self.response_time.as_mut() { + response_time.source = provenance_id(&response_time.source); + } + for prior in &mut self.prior_decisions { + if let Ok(mut evidence) = + serde_json::from_str::(&prior.evidence_json) + { + redact_absolute_paths(&mut evidence); + if let Ok(serialized) = serde_json::to_string(&evidence) { + prior.evidence_json = serialized; + } + } + } + } +} + +impl RecipientContextReplyBundle { + fn validate_json_size(&self) -> Result<()> { + if self.schema_version != RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION + || self.recipient.trim().is_empty() + { + anyhow::bail!("recipient context reply bundle identity is invalid"); + } + ContextReplyBundle { + schema_version: CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION, + context: self.context.clone(), + styles: self.styles.clone(), + prior_decisions: self.prior_decisions.clone(), + style_profile: self.style_profile.clone(), + response_time: self.response_time.clone(), + } + .validate_json_size()?; + if let Some(recipient_profile) = &self.recipient_style_profile { + if recipient_profile.recipient != self.recipient + || recipient_profile.profile.sample_count == 0 + || recipient_profile.profile.user != STYLE_USER + || recipient_profile.profile.policy_version != STYLE_POLICY_VERSION + || !recipient_profile.confidence_sum.is_finite() + || recipient_profile.confidence_sum < 0.0 + { + anyhow::bail!("recipient context reply bundle style profile is invalid"); + } + validate_style_profile_laughter( + &recipient_profile.profile, + "recipient context reply bundle style profile", + )?; + if !recipient_profile.used_fallback + && (recipient_profile.direct_sample_count < RECIPIENT_STYLE_MIN_SAMPLES + || recipient_profile.confidence_sum < RECIPIENT_STYLE_MIN_CONFIDENCE) + { + anyhow::bail!("recipient context reply bundle direct style evidence is weak"); + } + } + let bytes = serde_json::to_vec(self)?; + if bytes.len() > CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES { + anyhow::bail!( + "recipient context reply bundle exceeds {} bytes", + CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES + ); + } + Ok(()) + } + + fn redact_provenance(&mut self) { + for result in self.context.iter_mut().chain(self.styles.iter_mut()) { + result.source = provenance_id(&result.source); + } + if let Some(profile) = self.style_profile.as_mut() { + profile.source = provenance_id(&profile.source); + } + if let Some(profile) = self.recipient_style_profile.as_mut() { + profile.profile.source = provenance_id(&profile.profile.source); + } + if let Some(response_time) = self.response_time.as_mut() { + response_time.source = provenance_id(&response_time.source); + } + redact_reply_decisions(&mut self.prior_decisions); + } +} + +#[derive(Debug)] +pub(super) struct LiveEventExclusions { + chat_id: i64, + log_ids: BTreeSet, + decision_event_ids: BTreeSet, +} + +pub(super) fn live_event_exclusions(chat_id: i64, log_ids: &[i64]) -> Result { + if chat_id <= 0 { + anyhow::bail!("live context exclusion chat ID must be positive"); + } + if log_ids.is_empty() || log_ids.len() > CONTEXT_REPLY_BUNDLE_MAX_EXCLUDED_LOG_IDS { + anyhow::bail!( + "live context exclusions must contain between 1 and {} log IDs", + CONTEXT_REPLY_BUNDLE_MAX_EXCLUDED_LOG_IDS + ); + } + let mut unique_log_ids = BTreeSet::new(); + for log_id in log_ids { + if *log_id <= 0 { + anyhow::bail!("live context exclusion log IDs must be positive"); + } + if !unique_log_ids.insert(*log_id) { + anyhow::bail!("live context exclusion log IDs must be unique"); + } + } + let decision_event_ids = unique_log_ids + .iter() + .map(|log_id| format!("db:{chat_id}:{log_id}")) + .collect(); + Ok(LiveEventExclusions { + chat_id, + log_ids: unique_log_ids, + decision_event_ids, + }) +} + +fn context_ids_for_excluded_live_events( + conn: &Connection, + chat: &str, + preferred_source: Option<&str>, + exclusions: &LiveEventExclusions, +) -> Result> { + let has_live_events = conn.query_row( + "SELECT EXISTS( + SELECT 1 FROM sqlite_master + WHERE type = 'table' AND name = 'context_live_events' + )", + [], + |row| row.get::<_, i64>(0), + )? != 0; + if !has_live_events { + return Ok(BTreeSet::new()); + } + let mut stmt = conn.prepare( + "SELECT events.context_message_id + FROM context_live_events events + JOIN context_messages messages + ON messages.id = events.context_message_id + AND messages.source = events.source + WHERE events.chat_id = ?1 AND events.log_id = ?2 + AND messages.chat = ?3 + AND (?4 IS NULL OR events.source = ?4) + ORDER BY events.source ASC, events.context_message_id ASC", + )?; + let mut context_ids = BTreeSet::new(); + for log_id in &exclusions.log_ids { + let rows = stmt.query_map( + params![exclusions.chat_id, log_id, chat, preferred_source], + |row| row.get::<_, i64>(0), + )?; + let mut mapped_context_id = None; + for row in rows { + let context_id = row?; + if mapped_context_id.replace(context_id).is_some() { + anyhow::bail!("live context exclusion mapping is ambiguous"); + } + } + if let Some(context_id) = mapped_context_id { + context_ids.insert(context_id); + } + } + Ok(context_ids) +} + +pub fn context_reply_bundle( + db_path: &Path, + chat: &str, + query: &str, + source: Option<&str>, +) -> Result { + if chat.trim().is_empty() { + anyhow::bail!("chat name must not be empty"); + } + // Punctuation-only or blank inbound still needs style/timing evidence. + + let conn = open_db_readonly(db_path)?; + conn.execute_batch("BEGIN DEFERRED TRANSACTION")?; + let result = (|| { + ensure_retrieval_index_current(&conn)?; + let preferred_source = preferred_context_source(&conn, Some(chat), source)?; + let context = search_with_connection_excluding( + &conn, + Some(chat), + preferred_source.as_deref(), + query, + "hybrid", + CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT, + &BTreeSet::new(), + &LocalHashEmbedder, + )?; + let styles = style_search_with_connection( + &conn, + Some(chat), + preferred_source.as_deref(), + query, + CONTEXT_REPLY_BUNDLE_STYLE_LIMIT, + true, + )?; + let prior_decisions = reply_decision_search_with_connection_excluding( + &conn, + chat, + query, + CONTEXT_REPLY_BUNDLE_DECISION_LIMIT, + &BTreeSet::new(), + )?; + let style_profile = style_profile_with_connection( + &conn, + chat, + STYLE_USER, + preferred_source.as_deref(), + true, + )?; + let response_time = response_time_stats_with_connection( + &conn, + chat, + STYLE_USER, + preferred_source.as_deref(), + )?; + let mut bundle = ContextReplyBundle { + schema_version: CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION, + context, + styles, + prior_decisions, + style_profile, + response_time, + }; + bundle.redact_provenance(); + bundle.validate_json_size()?; + Ok(bundle) + })(); + + match result { + Ok(bundle) => { + conn.execute_batch("COMMIT")?; + Ok(bundle) + } + Err(error) => { + let _ = conn.execute_batch("ROLLBACK"); + Err(error) + } + } +} + +/// Build the version-2 reply bundle for a specific participant in one read +/// snapshot. Every live row of the incoming burst (`excluded_log_ids`) is +/// excluded from context retrieval and prior decisions. Direct +/// recipient-linked style evidence is used only when its profile clears the +/// sample/confidence floor; otherwise both profile and style examples fall +/// back to the authoritative room-wide register. +pub fn context_reply_bundle_for_recipient_excluding_live_events( + db_path: &Path, + chat: &str, + query: &str, + source: Option<&str>, + recipient: &str, + chat_id: i64, + excluded_log_ids: &[i64], +) -> Result { + if chat.trim().is_empty() || recipient.trim().is_empty() { + anyhow::bail!("recipient context reply bundle chat and recipient must not be empty"); + } + // Recipient bundles keep style/timing when the inbound has no tokens. + let exclusions = live_event_exclusions(chat_id, excluded_log_ids)?; + + let conn = open_db_readonly(db_path)?; + conn.execute_batch("BEGIN DEFERRED TRANSACTION")?; + let result = (|| { + ensure_retrieval_index_current(&conn)?; + let preferred_source = preferred_context_source(&conn, Some(chat), source)?; + let excluded_context_ids = context_ids_for_excluded_live_events( + &conn, + chat, + preferred_source.as_deref(), + &exclusions, + )?; + let context = search_with_connection_excluding( + &conn, + Some(chat), + preferred_source.as_deref(), + query, + "hybrid", + CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT, + &excluded_context_ids, + &LocalHashEmbedder, + )?; + let recipient_style_profile = recipient_style_profile_with_connection( + &conn, + chat, + recipient, + preferred_source.as_deref(), + )?; + let styles = if let Some(profile) = recipient_style_profile + .as_ref() + .filter(|profile| !profile.used_fallback) + { + recipient_style_search_with_connection( + &conn, + chat, + &profile.profile.source, + recipient, + query, + CONTEXT_REPLY_BUNDLE_STYLE_LIMIT, + )? + } else { + style_search_with_connection( + &conn, + Some(chat), + preferred_source.as_deref(), + query, + CONTEXT_REPLY_BUNDLE_STYLE_LIMIT, + true, + )? + }; + let prior_decisions = reply_decision_search_with_connection_excluding( + &conn, + chat, + query, + CONTEXT_REPLY_BUNDLE_DECISION_LIMIT, + &exclusions.decision_event_ids, + )?; + let style_profile = style_profile_with_connection( + &conn, + chat, + STYLE_USER, + preferred_source.as_deref(), + true, + )?; + let response_time = response_time_stats_with_connection( + &conn, + chat, + STYLE_USER, + preferred_source.as_deref(), + )?; + let mut bundle = RecipientContextReplyBundle { + schema_version: RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION, + recipient: recipient.to_string(), + context, + styles, + prior_decisions, + style_profile, + recipient_style_profile, + response_time, + }; + bundle.redact_provenance(); + bundle.validate_json_size()?; + Ok(bundle) + })(); + + match result { + Ok(bundle) => { + conn.execute_batch("COMMIT")?; + Ok(bundle) + } + Err(error) => { + let _ = conn.execute_batch("ROLLBACK"); + Err(error) + } + } +} + +pub fn context_reply_bundle_for_recipient_excluding_live_events_json( + db_path: &Path, + chat: &str, + query: &str, + source: Option<&str>, + recipient: &str, + chat_id: i64, + excluded_log_ids: &[i64], +) -> Result { + let bundle = context_reply_bundle_for_recipient_excluding_live_events( + db_path, + chat, + query, + source, + recipient, + chat_id, + excluded_log_ids, + )?; + let output = serde_json::to_string(&bundle)?; + if output.len() > CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES { + anyhow::bail!( + "recipient context reply bundle exceeds {} bytes", + CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES + ); + } + Ok(output) +} + +pub fn context_reply_bundle_json( + db_path: &Path, + chat: &str, + query: &str, + source: Option<&str>, +) -> Result { + let bundle = context_reply_bundle(db_path, chat, query, source)?; + let output = serde_json::to_string(&bundle)?; + if output.len() > CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES { + anyhow::bail!( + "context reply bundle exceeds {} bytes", + CONTEXT_REPLY_BUNDLE_MAX_JSON_BYTES + ); + } + Ok(output) +} diff --git a/src/context/reply_decision.rs b/src/context/reply_decision.rs new file mode 100644 index 00000000..c48e51d6 --- /dev/null +++ b/src/context/reply_decision.rs @@ -0,0 +1,588 @@ +//! Reply decision records (`reply_decisions` rows), their monotonic status +//! order, and similarity search over prior decisions. + +use super::embedding::{bytes_to_vector, cosine, encode_vector, vector_to_bytes, VECTOR_DIM}; +use super::response_time::{parse_chat_date, MAX_RESPONSE_DELAY_SECONDS}; +use super::schema::ensure_retrieval_index_current; +use super::search::{ + CONTEXT_KEYWORD_CANDIDATE_CAP, MAX_CONTEXT_RETRIEVAL_SCORE, STYLE_VECTOR_CANDIDATE_CAP, +}; +use super::store::{open_db, open_db_readonly}; +use super::style::STYLE_POLICY_VERSION; +use anyhow::{Context, Result}; +use chrono::Utc; +use rusqlite::{params, Connection, OptionalExtension}; +use serde::{Deserialize, Serialize}; +use std::cmp::Ordering; +use std::collections::BTreeSet; +use std::path::Path; + +pub(super) const REPLY_DECISION_CANDIDATE_CAP: usize = 128; + +const MAX_REPLY_EVIDENCE_IDS: usize = 64; + +const MAX_REPLY_EVIDENCE_ID_BYTES: usize = 256; + +#[derive(Debug, Clone, Deserialize)] +struct ReplyDecisionInput { + event_id: String, + chat: String, + author: String, + received_at: String, + message: String, + decision: String, + reason: String, + category: String, + context_match_count: usize, + style_match_count: usize, + best_context_score: f32, + best_style_score: f32, + prior_similarity: f32, + scheduled_delay_seconds: f64, + status: String, + reply: Option, + #[serde(default)] + evidence_ids: Vec, + #[serde(default)] + style_policy_version: String, + #[serde(default)] + provenance: Option, +} + +#[derive(Debug, Clone, Serialize)] +pub struct ReplyDecisionMatch { + pub event_id: String, + pub chat: String, + pub author: String, + pub received_at: String, + pub message: String, + pub decision: String, + pub reason: String, + pub category: String, + pub status: String, + pub reply: Option, + pub evidence_json: String, + pub score: f32, +} + +fn validate_reply_decision_input(input: &ReplyDecisionInput) -> Result<()> { + if input.event_id.trim().is_empty() + || input.chat.trim().is_empty() + || input.message.trim().is_empty() + { + anyhow::bail!("reply decision event_id, chat, and message must not be empty"); + } + if !matches!(input.decision.as_str(), "reply" | "skip") { + anyhow::bail!("reply decision must be 'reply' or 'skip'"); + } + if input.context_match_count > CONTEXT_KEYWORD_CANDIDATE_CAP + || input.style_match_count > STYLE_VECTOR_CANDIDATE_CAP + { + anyhow::bail!("reply decision match counts exceed retrieval caps"); + } + for (name, score, maximum) in [ + ( + "best_context_score", + input.best_context_score, + MAX_CONTEXT_RETRIEVAL_SCORE, + ), + ("best_style_score", input.best_style_score, 1.0), + ("prior_similarity", input.prior_similarity, 1.0), + ] { + if !score.is_finite() || !(0.0..=maximum).contains(&score) { + anyhow::bail!("reply decision {name} must be finite and in the range 0..={maximum}"); + } + } + if !input.scheduled_delay_seconds.is_finite() + || !(0.0..=(MAX_RESPONSE_DELAY_SECONDS as f64)).contains(&input.scheduled_delay_seconds) + { + anyhow::bail!( + "reply decision scheduled_delay_seconds must be finite and in the range 0..={MAX_RESPONSE_DELAY_SECONDS}" + ); + } + if input.evidence_ids.len() > MAX_REPLY_EVIDENCE_IDS { + anyhow::bail!("reply decision evidence_ids exceeds {MAX_REPLY_EVIDENCE_IDS} identifiers"); + } + if input.evidence_ids.iter().any(|id| { + id.trim().is_empty() + || id.len() > MAX_REPLY_EVIDENCE_ID_BYTES + || !id + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || b"._:-".contains(&byte)) + }) { + anyhow::bail!("reply decision evidence_ids must be bounded opaque identifiers"); + } + if input.decision == "reply" { + if input.evidence_ids.is_empty() { + anyhow::bail!("reply decision requires at least one evidence identifier"); + } + if input.style_policy_version != STYLE_POLICY_VERSION { + anyhow::bail!("reply decision style policy must be {STYLE_POLICY_VERSION}"); + } + if input + .reply + .as_deref() + .map(str::trim) + .unwrap_or("") + .is_empty() + { + anyhow::bail!("reply decision reply must not be empty"); + } + crate::reply_policy::validate_auto_reply_laughter( + input.reply.as_deref().unwrap_or_default(), + )?; + } + Ok(()) +} + +pub fn record_reply_decision(db_path: &Path, record_json: &str) -> Result { + let input: ReplyDecisionInput = + serde_json::from_str(record_json).context("invalid reply decision JSON")?; + validate_reply_decision_input(&input)?; + let mut conn = open_db(db_path)?; + let tx = conn.transaction()?; + let incoming_rank = reply_status_rank(&input.status) + .ok_or_else(|| anyhow::anyhow!("unknown reply decision status '{}'", input.status))?; + let now = Utc::now().to_rfc3339(); + let existing_status = tx + .query_row( + "SELECT status FROM reply_decisions WHERE event_id=?1", + [&input.event_id], + |row| row.get::<_, String>(0), + ) + .optional()?; + if let Some(previous_status) = existing_status { + let previous_rank = reply_status_rank(&previous_status).unwrap_or(6); + if !can_apply_reply_status( + &previous_status, + &input.status, + previous_rank, + incoming_rank, + ) { + return Ok(false); + } + } + let mut evidence_map = serde_json::Map::new(); + evidence_map.insert( + "evidence_ids".into(), + serde_json::to_value(&input.evidence_ids)?, + ); + evidence_map.insert( + "style_policy_version".into(), + serde_json::to_value(&input.style_policy_version)?, + ); + if let Some(prov) = &input.provenance { + evidence_map.insert("provenance".into(), prov.clone()); + } + let evidence_json = serde_json::Value::Object(evidence_map).to_string(); + if evidence_json.len() > 16 * 1024 { + anyhow::bail!("reply evidence exceeds 16 KiB"); + } + tx.execute( + "INSERT INTO reply_decisions( + event_id, chat, author, received_at, message, vector, decision, reason, + category, context_match_count, style_match_count, best_context_score, + best_style_score, prior_similarity, scheduled_delay_seconds, status, + reply, sent_at, created_at, updated_at, evidence_json + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, NULL, ?18, ?18, ?19) + ON CONFLICT(event_id) DO UPDATE SET + chat=excluded.chat, + author=excluded.author, + received_at=excluded.received_at, + message=excluded.message, + vector=excluded.vector, + decision=excluded.decision, + reason=excluded.reason, + category=excluded.category, + context_match_count=excluded.context_match_count, + style_match_count=excluded.style_match_count, + best_context_score=excluded.best_context_score, + best_style_score=excluded.best_style_score, + prior_similarity=excluded.prior_similarity, + scheduled_delay_seconds=excluded.scheduled_delay_seconds, + status=excluded.status, + reply=CASE + WHEN excluded.decision='skip' AND excluded.status='skipped' THEN NULL + ELSE COALESCE(excluded.reply, reply) + END, + sent_at=CASE + WHEN excluded.decision='skip' AND excluded.status='skipped' THEN NULL + ELSE sent_at + END, + evidence_json=excluded.evidence_json, + updated_at=excluded.updated_at", + params![ + input.event_id, + input.chat, + input.author, + input.received_at, + input.message, + vector_to_bytes(&encode_vector(&input.message)), + input.decision, + input.reason, + input.category, + input.context_match_count as i64, + input.style_match_count as i64, + input.best_context_score, + input.best_style_score, + input.prior_similarity, + input.scheduled_delay_seconds, + input.status, + input.reply, + now, + evidence_json, + ], + )?; + tx.commit()?; + Ok(true) +} + +pub fn update_reply_decision( + db_path: &Path, + event_id: &str, + status: &str, + reply: Option<&str>, + sent_at: Option<&str>, +) -> Result { + if event_id.trim().is_empty() || status.trim().is_empty() { + anyhow::bail!("reply decision event_id and status must not be empty"); + } + if let Some(reply) = reply { + crate::reply_policy::validate_auto_reply_laughter(reply)?; + } + let mut conn = open_db(db_path)?; + let tx = conn.transaction()?; + let incoming_rank = reply_status_rank(status) + .ok_or_else(|| anyhow::anyhow!("unknown reply decision status '{status}'"))?; + let previous_status = tx + .query_row( + "SELECT status FROM reply_decisions WHERE event_id=?1", + [event_id], + |row| row.get::<_, String>(0), + ) + .optional()?; + let Some(previous_status) = previous_status else { + return Ok(false); + }; + let previous_rank = reply_status_rank(&previous_status).unwrap_or(6); + if !can_apply_reply_status(&previous_status, status, previous_rank, incoming_rank) { + return Ok(false); + } + let changed = tx.execute( + "UPDATE reply_decisions + SET status = ?2, + reply = COALESCE(?3, reply), + sent_at = COALESCE(?4, sent_at), + updated_at = ?5 + WHERE event_id = ?1", + params![event_id, status, reply, sent_at, Utc::now().to_rfc3339()], + )?; + tx.commit()?; + Ok(changed > 0) +} + +pub(super) fn parse_reply_sent_at_seconds(value: &str) -> Option { + chrono::DateTime::parse_from_rfc3339(value.trim()) + .ok() + .map(|value| value.timestamp()) + .or_else(|| parse_chat_date(value).map(|value| value.and_utc().timestamp())) +} + +pub fn reply_decision_search( + db_path: &Path, + chat: &str, + query: &str, + limit: usize, +) -> Result> { + if chat.trim().is_empty() || query.trim().is_empty() || limit == 0 { + return Ok(Vec::new()); + } + let conn = open_db_readonly(db_path)?; + ensure_retrieval_index_current(&conn)?; + reply_decision_search_with_connection_excluding(&conn, chat, query, limit, &BTreeSet::new()) +} + +pub(super) fn reply_decision_search_with_connection_excluding( + conn: &Connection, + chat: &str, + query: &str, + limit: usize, + excluded_event_ids: &BTreeSet, +) -> Result> { + ensure_retrieval_index_current(conn)?; + let result_limit = limit.min(REPLY_DECISION_CANDIDATE_CAP); + if result_limit == 0 { + return Ok(Vec::new()); + } + let mut results = Vec::new(); + let mut exact_stmt = conn.prepare( + "SELECT event_id, chat, author, received_at, message, decision, reason, + category, status, reply, substr(evidence_json, 1, 16384) + FROM reply_decisions + WHERE chat = ?1 AND message = ?2 + ORDER BY created_at DESC, status ASC, event_id ASC + LIMIT ?3", + )?; + let exact_candidate_limit = result_limit.saturating_add(excluded_event_ids.len()); + let exact_rows = + exact_stmt.query_map(params![chat, query, exact_candidate_limit as i64], |row| { + Ok(ReplyDecisionMatch { + event_id: row.get(0)?, + chat: row.get(1)?, + author: row.get(2)?, + received_at: row.get(3)?, + message: row.get(4)?, + decision: row.get(5)?, + reason: row.get(6)?, + category: row.get(7)?, + status: row.get(8)?, + reply: row.get(9)?, + evidence_json: row.get(10)?, + score: 1.0, + }) + })?; + for row in exact_rows { + let row = row?; + if !excluded_event_ids.contains(&row.event_id) { + results.push(row); + } + if results.len() == result_limit { + break; + } + } + if results.len() == result_limit { + return Ok(results); + } + + let query_vector = encode_vector(query); + if query_vector.iter().all(|value| *value == 0.0) { + return Ok(results); + } + let mut stmt = conn.prepare( + "SELECT event_id, chat, author, received_at, message, decision, reason, + category, status, reply, substr(evidence_json, 1, 16384), vector + FROM reply_decisions + WHERE chat = ?1 + ORDER BY created_at DESC, event_id ASC + LIMIT ?2", + )?; + let similarity_candidate_limit = + REPLY_DECISION_CANDIDATE_CAP.saturating_add(excluded_event_ids.len()); + let rows = stmt.query_map(params![chat, similarity_candidate_limit as i64], |row| { + let evidence_json: String = row.get(10)?; + let bytes: Vec = row.get(11)?; + if bytes.len() != VECTOR_DIM * 4 { + return Err(rusqlite::Error::InvalidColumnType( + 11, + "vector".into(), + rusqlite::types::Type::Blob, + )); + } + Ok(( + ReplyDecisionMatch { + event_id: row.get(0)?, + chat: row.get(1)?, + author: row.get(2)?, + received_at: row.get(3)?, + message: row.get(4)?, + decision: row.get(5)?, + reason: row.get(6)?, + category: row.get(7)?, + status: row.get(8)?, + reply: row.get(9)?, + evidence_json, + score: 0.0, + }, + bytes_to_vector(&bytes), + )) + })?; + let exact_event_ids = results + .iter() + .map(|result| result.event_id.as_str()) + .collect::>(); + let mut similar = Vec::new(); + for row in rows { + let (mut result, vector) = row?; + if excluded_event_ids.contains(&result.event_id) + || exact_event_ids.contains(result.event_id.as_str()) + { + continue; + } + result.score = cosine(&query_vector, &vector); + similar.push(result); + } + similar.sort_by(|a, b| { + b.score + .partial_cmp(&a.score) + .unwrap_or(Ordering::Equal) + .then_with(|| a.event_id.cmp(&b.event_id)) + }); + results.extend(similar.into_iter().take(result_limit - results.len())); + Ok(results) +} + +#[derive(Debug, Clone, Serialize)] +pub struct ReplyDecision { + pub event_id: String, + pub status: String, + pub evidence_json: String, + pub updated_at: String, +} + +#[derive(Debug, Clone, Serialize)] +pub struct ReplyDecisionUpdate { + pub applied: bool, + pub updated: bool, + pub decision: Option, + pub reason: Option, +} + +fn reply_status_rank(status: &str) -> Option { + Some(match status { + "pending" => 0, + "processing" => 1, + "projection_pending" => 2, + "scheduled" => 3, + "sending" => 4, + "accepted_unconfirmed" => 5, + "sent" | "skipped" | "poison" => 6, + "failed" => 4, + "delivery_unknown" | "reconcile_required" => 6, + _ => return None, + }) +} + +/// Apply a monotonic audit projection. The queue remains the delivery authority. +pub fn project_reply_decision( + db_path: &Path, + event_id: &str, + status: &str, + evidence_json: &str, +) -> Result { + if event_id.trim().is_empty() { + anyhow::bail!("reply event identity must not be empty"); + } + if evidence_json.len() > 16 * 1024 { + anyhow::bail!("reply evidence exceeds 16 KiB"); + } + let incoming_rank = reply_status_rank(status) + .ok_or_else(|| anyhow::anyhow!("unknown reply decision status '{status}'"))?; + let mut conn = open_db(db_path)?; + let tx = conn.transaction()?; + let now = chrono::Utc::now().to_rfc3339(); + let existing = tx + .query_row( + "SELECT status,evidence_json,updated_at FROM reply_decisions WHERE event_id=?1", + [event_id], + |row| { + Ok(ReplyDecision { + event_id: event_id.to_string(), + status: row.get(0)?, + evidence_json: row.get(1)?, + updated_at: row.get(2)?, + }) + }, + ) + .optional()?; + if let Some(previous) = existing { + let previous_rank = reply_status_rank(&previous.status).unwrap_or(6); + if !can_apply_reply_status(&previous.status, status, previous_rank, incoming_rank) { + return Ok(ReplyDecisionUpdate { + applied: false, + updated: false, + decision: Some(previous), + reason: Some("terminal_or_newer_status_cannot_regress".into()), + }); + } + if previous.status == status && previous.evidence_json == evidence_json { + return Ok(ReplyDecisionUpdate { + applied: true, + updated: false, + decision: Some(previous), + reason: Some("already_current".into()), + }); + } + tx.execute( + "UPDATE reply_decisions + SET status=?2,evidence_json=?3,updated_at=?4 + WHERE event_id=?1", + params![event_id, status, evidence_json, now], + )?; + } else { + tx.execute( + "INSERT INTO reply_decisions( + event_id, chat, author, received_at, message, vector, decision, + reason, category, context_match_count, style_match_count, + best_context_score, best_style_score, prior_similarity, + scheduled_delay_seconds, status, reply, sent_at, created_at, + updated_at, evidence_json + ) VALUES( + ?1, '__projection__', '', ?2, ?1, ?3, 'skip', 'projection', + 'uncertain', 0, 0, 0, 0, 0, 0, ?4, NULL, NULL, ?2, ?2, ?5 + )", + params![ + event_id, + now, + vector_to_bytes(&encode_vector(event_id)), + status, + evidence_json, + ], + )?; + } + tx.commit()?; + Ok(ReplyDecisionUpdate { + applied: true, + updated: true, + decision: Some(ReplyDecision { + event_id: event_id.to_string(), + status: status.to_string(), + evidence_json: evidence_json.to_string(), + updated_at: now, + }), + reason: None, + }) +} + +fn can_apply_reply_status( + previous: &str, + incoming: &str, + previous_rank: u8, + incoming_rank: u8, +) -> bool { + if incoming_rank < previous_rank { + return false; + } + if previous == incoming { + return true; + } + if is_terminal_reply_status(previous) { + return false; + } + true +} + +fn is_terminal_reply_status(status: &str) -> bool { + matches!( + status, + "sent" | "skipped" | "poison" | "delivery_unknown" | "reconcile_required" + ) +} + +pub fn get_reply_decision(db_path: &Path, event_id: &str) -> Result> { + let conn = open_db_readonly(db_path)?; + ensure_retrieval_index_current(&conn)?; + conn.query_row( + "SELECT event_id,status,evidence_json,updated_at FROM reply_decisions WHERE event_id=?1", + [event_id], + |row| { + Ok(ReplyDecision { + event_id: row.get(0)?, + status: row.get(1)?, + evidence_json: row.get(2)?, + updated_at: row.get(3)?, + }) + }, + ) + .optional() + .map_err(Into::into) +} diff --git a/src/context/response_time.rs b/src/context/response_time.rs new file mode 100644 index 00000000..0b74dc36 --- /dev/null +++ b/src/context/response_time.rs @@ -0,0 +1,697 @@ +//! Owner reply timing: response-delay samples, the fitted delay +//! distribution, and the stored `response_time_stats` rows. + +use super::live::preferred_context_source; +use super::schema::ensure_retrieval_index_current; +use super::self_send::{match_confirmed_send, ConfirmedSelfSend}; +use super::store::open_db_readonly; +use super::style::{ + classify_style_message, insert_style_profile, StyleProfileAccumulator, STYLE_POLICY_VERSION, + STYLE_USER, +}; +use anyhow::Result; +use chrono::{NaiveDateTime, Utc}; +use rusqlite::{params, Connection}; +use serde::{Deserialize, Serialize}; +use std::cmp::Ordering; +use std::path::Path; + +pub(super) const MAX_RESPONSE_DELAY_SECONDS: i64 = 24 * 60 * 60; + +pub(super) const RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION: u32 = 2; + +pub(super) const RESPONSE_TIME_DISTRIBUTION_POLICY_VERSION: &str = + "empirical-log1p-three-means-p90-v1"; + +pub(super) const RESPONSE_TIME_DISTRIBUTION_MODEL_KIND: &str = "bounded-normal-mixture"; + +pub(super) const RESPONSE_TIME_DISTRIBUTION_FIT_TRANSFORM: &str = "log1p"; + +pub(super) const RESPONSE_TIME_DISTRIBUTION_COMPONENTS: usize = 3; + +pub(super) const RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES: usize = 32; + +pub(super) const RESPONSE_TIME_DISTRIBUTION_MIN_COMPONENT_SAMPLES: usize = 8; + +pub(super) const MIN_SCHEDULED_RESPONSE_DELAY_SECONDS: f64 = 5.0; + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct ResponseTimeComponent { + pub name: String, + pub sample_count: usize, + pub weight: f64, + pub normal_location_seconds: f64, + pub normal_scale_seconds: f64, + pub lower_seconds: f64, + pub upper_seconds: f64, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct ResponseTimeDistribution { + pub schema_version: u32, + pub policy_version: String, + pub model_kind: String, + pub fit_transform: String, + pub sample_count: usize, + pub retained_sample_count: usize, + pub tail_winsorized_count: usize, + pub split_seconds: Vec, + pub global_upper_seconds: f64, + pub components: Vec, +} + +#[derive(Debug, Clone, Serialize)] +pub struct ResponseTimeStats { + pub chat: String, + pub source: String, + pub user: String, + pub sample_count: usize, + pub average_seconds: f64, + pub median_seconds: f64, + pub p90_seconds: f64, + pub min_seconds: f64, + pub max_seconds: f64, + pub max_window_seconds: i64, + pub stddev_seconds: f64, + pub distribution: Option, +} + +pub(super) fn parse_chat_date(value: &str) -> Option { + NaiveDateTime::parse_from_str(value.trim(), "%Y-%m-%d %H:%M:%S") + .or_else(|_| NaiveDateTime::parse_from_str(value.trim(), "%Y-%m-%d %H:%M")) + .ok() +} + +pub(super) fn is_response_participant(user: &str) -> bool { + let user = user.trim(); + !user.is_empty() + && user != STYLE_USER + && !user.ends_with('봇') + && !matches!( + user, + "드리고" | "뉴스봇" | "채팅봇" | "주식봇" | "날씨날씨" | "인아웃" | "채팅도구" + ) +} + +pub(super) fn insert_response_time_stats( + conn: &Connection, + stats: &ResponseTimeStats, +) -> Result<()> { + conn.execute( + "INSERT INTO response_time_stats( + chat, source, user_name, sample_count, average_seconds, median_seconds, + p90_seconds, min_seconds, max_seconds, max_window_seconds, stddev_seconds, + distribution_schema_version, distribution_json + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)", + params![ + stats.chat, + stats.source, + stats.user, + stats.sample_count as i64, + stats.average_seconds, + stats.median_seconds, + stats.p90_seconds, + stats.min_seconds, + stats.max_seconds, + stats.max_window_seconds, + stats.stddev_seconds, + stats + .distribution + .as_ref() + .map(|distribution| distribution.schema_version as i64) + .unwrap_or(0), + stats + .distribution + .as_ref() + .map(serde_json::to_string) + .transpose()? + .unwrap_or_else(|| "{}".to_string()), + ], + )?; + Ok(()) +} + +/// Recompute the pacing aggregate of an imported (CSV / legacy) source. +/// +/// An imported source stores `response_time_stats` directly and keeps no +/// per-reply `response_time_samples` rows, so a bot reply that was measured +/// before the guard existed cannot be removed by deleting samples. The only way +/// to retire its contribution is to rebuild the aggregate from the source's own +/// messages with the same ordering rules the importer uses, skipping every row +/// that a confirmed send identifies as the assistant. +pub(super) fn recompute_imported_response_time_stats( + conn: &Connection, + source: &str, + chat: &str, + confirmed_sends: &[ConfirmedSelfSend], +) -> Result { + let rows = { + let mut stmt = conn.prepare( + "SELECT date, user_name, message FROM context_messages + WHERE source = ?1 AND chat = ?2 ORDER BY date ASC, id ASC", + )?; + let mapped = stmt.query_map(params![source, chat], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, String>(2)?, + )) + })?; + mapped.collect::>>()? + }; + let mut delays = Vec::new(); + let mut previous_human_at: Option = None; + for (date, user, message) in rows { + let Some(current_at) = parse_chat_date(&date) else { + continue; + }; + if user == STYLE_USER { + let sent_at = current_at.and_utc().timestamp(); + let (matched, ambiguous) = + match_confirmed_send(confirmed_sends, message.trim(), sent_at); + if matched.is_some() && !ambiguous { + // A confirmed assistant reply is not a human response, and the + // pending prompt it consumed is gone with it. + previous_human_at = None; + continue; + } + if let Some(previous_at) = previous_human_at.take() { + let delay = (current_at - previous_at).num_seconds(); + if (0..=MAX_RESPONSE_DELAY_SECONDS).contains(&delay) { + delays.push(delay as f64); + } + } + } else if is_response_participant(&user) { + previous_human_at = Some(current_at); + } + } + conn.execute( + "DELETE FROM response_time_stats WHERE chat = ?1 AND source = ?2 AND user_name = ?3", + params![chat, source, STYLE_USER], + )?; + let sample_count = delays.len(); + if let Some(stats) = summarize_response_delays(chat, source, STYLE_USER, delays) { + insert_response_time_stats(conn, &stats)?; + } + // The source also owns a style profile. Rebuilding only the pacing numbers + // would leave the style aggregate describing rows the repair just changed. + let style_messages = { + let mut stmt = conn.prepare( + "SELECT message FROM owner_style + WHERE source = ?1 AND chat = ?2 AND user_name = ?3 + AND style_eligible = 1 AND policy_version = ?4 + ORDER BY source_row ASC, id ASC", + )?; + let mapped = stmt.query_map( + params![source, chat, STYLE_USER, STYLE_POLICY_VERSION], + |row| row.get::<_, String>(0), + )?; + mapped.collect::>>()? + }; + let mut accumulator = StyleProfileAccumulator::default(); + for message in &style_messages { + let features = classify_style_message(message); + if features.style_eligible { + accumulator.add(message, &features); + } + } + conn.execute( + "DELETE FROM owner_style_profile WHERE chat = ?1 AND source = ?2 AND user_name = ?3", + params![chat, source, STYLE_USER], + )?; + if let Some(profile) = accumulator.finish(chat, source, STYLE_USER) { + insert_style_profile(conn, &profile, &Utc::now().to_rfc3339())?; + } + Ok(sample_count) +} + +pub(super) fn response_delay_percentile(sorted: &[f64], ratio: f64) -> f64 { + let position = (sorted.len() - 1) as f64 * ratio; + let lower = position.floor() as usize; + let upper = position.ceil() as usize; + if lower == upper { + sorted[lower] + } else { + sorted[lower] + (sorted[upper] - sorted[lower]) * (position - lower as f64) + } +} + +fn response_time_segment_sse( + prefix_sum: &[f64], + prefix_square_sum: &[f64], + start: usize, + end: usize, +) -> f64 { + let count = (end - start) as f64; + let sum = prefix_sum[end] - prefix_sum[start]; + let square_sum = prefix_square_sum[end] - prefix_square_sum[start]; + (square_sum - sum * sum / count).max(0.0) +} + +/// Smallest admissible `second_split` for every `first_candidates` entry. +/// +/// The result is the index into `second_candidates` of the smallest minimiser +/// of `sse(first, s) + sse(s, total)`, or `None` when the strict-increase +/// window `[first + minimum, total - minimum]` holds no admissible boundary. +/// +/// The smallest constrained minimiser is monotone non-decreasing in +/// `first_split` because the segment SSE satisfies the quadrangle inequality +/// sse(a, c) + sse(b, d) <= sse(a, d) + sse(b, c) for a <= b <= c <= d, +/// so divide-and-conquer optimisation is exact here. A ternary search over the +/// middle boundary would not be: the objective is not unimodal, and a shape as +/// ordinary as a dense uniform spread already has a local minimum that is not +/// the global one. +fn response_time_distribution_second_splits( + prefix_sum: &[f64], + prefix_square_sum: &[f64], + first_candidates: &[usize], + second_candidates: &[usize], + total: usize, + minimum: usize, +) -> Vec> { + #[allow(clippy::too_many_arguments)] + // The loop variable is the answer, not an iteration counter: the winning + // position is stored in optima and reused as a boundary bound in the + // recursive calls, so enumerating the slice would only rename it. + #[allow(clippy::needless_range_loop)] + fn search( + prefix_sum: &[f64], + prefix_square_sum: &[f64], + first_candidates: &[usize], + second_candidates: &[usize], + total: usize, + minimum: usize, + first_start: usize, + first_end: usize, + second_start: usize, + second_end: usize, + optima: &mut [Option], + ) { + if first_start > first_end { + return; + } + if second_start > second_end { + // No admissible boundary is left for this first_split or any later + // one, because the lower bound only moves right with first_split. + for slot in &mut optima[first_start..=first_end] { + *slot = None; + } + return; + } + let first_midpoint = first_start + (first_end - first_start) / 2; + let first_split = first_candidates[first_midpoint]; + let lower = second_candidates.partition_point(|boundary| *boundary < first_split + minimum); + let scan_start = second_start.max(lower); + let mut best: Option<(f64, usize)> = None; + for position in scan_start..=second_end { + let second_split = second_candidates[position]; + let sse = response_time_segment_sse(prefix_sum, prefix_square_sum, 0, first_split) + + response_time_segment_sse( + prefix_sum, + prefix_square_sum, + first_split, + second_split, + ) + + response_time_segment_sse(prefix_sum, prefix_square_sum, second_split, total); + if best.is_none_or(|(best_sse, _)| sse < best_sse) { + best = Some((sse, position)); + } + } + optima[first_midpoint] = best.map(|(_, position)| position); + match best { + Some((_, best_position)) => { + if first_midpoint > first_start { + search( + prefix_sum, + prefix_square_sum, + first_candidates, + second_candidates, + total, + minimum, + first_start, + first_midpoint - 1, + second_start, + best_position, + optima, + ); + } + if first_midpoint < first_end { + search( + prefix_sum, + prefix_square_sum, + first_candidates, + second_candidates, + total, + minimum, + first_midpoint + 1, + first_end, + best_position, + second_end, + optima, + ); + } + } + None => { + if first_midpoint < first_end { + for slot in &mut optima[first_midpoint + 1..=first_end] { + *slot = None; + } + } + if first_midpoint > first_start { + search( + prefix_sum, + prefix_square_sum, + first_candidates, + second_candidates, + total, + minimum, + first_start, + first_midpoint - 1, + second_start, + second_end, + optima, + ); + } + } + } + } + let mut optima = vec![None; first_candidates.len()]; + if !first_candidates.is_empty() && !second_candidates.is_empty() { + search( + prefix_sum, + prefix_square_sum, + first_candidates, + second_candidates, + total, + minimum, + 0, + first_candidates.len() - 1, + 0, + second_candidates.len() - 1, + &mut optima, + ); + } + optima +} + +pub(super) fn fit_response_time_distribution(delays: &[f64]) -> Option { + if delays.len() < RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES + || delays.iter().any(|delay| { + !delay.is_finite() || !(0.0..=MAX_RESPONSE_DELAY_SECONDS as f64).contains(delay) + }) + { + return None; + } + let transformed = delays.iter().map(|delay| delay.ln_1p()).collect::>(); + let mut prefix_sum = vec![0.0; transformed.len() + 1]; + let mut prefix_square_sum = vec![0.0; transformed.len() + 1]; + for (index, value) in transformed.iter().enumerate() { + prefix_sum[index + 1] = prefix_sum[index] + value; + prefix_square_sum[index + 1] = prefix_square_sum[index] + value * value; + } + let segment_sse = |start: usize, end: usize| { + response_time_segment_sse(&prefix_sum, &prefix_square_sum, start, end) + }; + // Exact three-way split search in O(N log N) instead of the O(N^2) pair + // scan, with an identical result. The per-first_split objective + // g(s) = sse(first, s) + sse(s, N) + // is not unimodal, so a ternary or gradient walk does not find its minimum; + // the quadrangle inequality of the segment SSE is what makes the smallest + // minimiser monotone in first_split and divide-and-conquer exact. Walking + // first_split ascending while replacing only on a strict improvement keeps + // the exhaustive scan's lexicographically smallest `(first, second)` tie: + // the earliest improving first_split wins with its own smallest second_split. + let minimum = RESPONSE_TIME_DISTRIBUTION_MIN_COMPONENT_SAMPLES; + let total = delays.len(); + let first_candidates = (minimum..=total - 2 * minimum) + .filter(|split| delays[split - 1] < delays[*split]) + .collect::>(); + let second_candidates = (minimum..=total - minimum) + .filter(|split| delays[split - 1] < delays[*split]) + .collect::>(); + let optima = response_time_distribution_second_splits( + &prefix_sum, + &prefix_square_sum, + &first_candidates, + &second_candidates, + total, + minimum, + ); + let mut best: Option<(f64, usize, usize)> = None; + for (position, first_split) in first_candidates.into_iter().enumerate() { + let Some(second_position) = optima[position] else { + continue; + }; + let second_split = second_candidates[second_position]; + let sse = segment_sse(0, first_split) + + segment_sse(first_split, second_split) + + segment_sse(second_split, total); + if best.as_ref().is_none_or(|(best_sse, _, _)| sse < *best_sse) { + best = Some((sse, first_split, second_split)); + } + } + let (_, first_split, second_split) = best?; + let global_upper_seconds = response_delay_percentile(delays, 0.9); + let immediate_upper_seconds = delays[first_split - 1].min(global_upper_seconds); + let short_lower_seconds = delays[first_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); + let short_upper_seconds = delays[second_split - 1].min(global_upper_seconds); + let delayed_lower_seconds = delays[second_split].max(MIN_SCHEDULED_RESPONSE_DELAY_SECONDS); + if immediate_upper_seconds < MIN_SCHEDULED_RESPONSE_DELAY_SECONDS + || short_lower_seconds > short_upper_seconds + || delayed_lower_seconds > global_upper_seconds + || immediate_upper_seconds >= short_lower_seconds + || short_upper_seconds >= delayed_lower_seconds + { + return None; + } + + let bounds = [ + ( + MIN_SCHEDULED_RESPONSE_DELAY_SECONDS, + immediate_upper_seconds, + ), + (short_lower_seconds, short_upper_seconds), + (delayed_lower_seconds, global_upper_seconds), + ]; + let ranges = [ + (0, first_split), + (first_split, second_split), + (second_split, delays.len()), + ]; + let mut components = Vec::with_capacity(RESPONSE_TIME_DISTRIBUTION_COMPONENTS); + for ((start, end), ((lower_seconds, upper_seconds), name)) in ranges + .into_iter() + .zip(bounds.into_iter().zip(["immediate", "short", "delayed"])) + { + let values = delays[start..end] + .iter() + .map(|delay| delay.clamp(lower_seconds, upper_seconds)) + .collect::>(); + let mean = values.iter().sum::() / values.len() as f64; + let stddev = (values + .iter() + .map(|value| (value - mean).powi(2)) + .sum::() + / values.len() as f64) + .sqrt(); + if !stddev.is_finite() || stddev <= 0.0 { + return None; + } + components.push(ResponseTimeComponent { + name: name.to_string(), + sample_count: values.len(), + weight: values.len() as f64 / delays.len() as f64, + normal_location_seconds: mean, + normal_scale_seconds: stddev, + lower_seconds, + upper_seconds, + }); + } + let distribution = ResponseTimeDistribution { + schema_version: RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION, + policy_version: RESPONSE_TIME_DISTRIBUTION_POLICY_VERSION.to_string(), + model_kind: RESPONSE_TIME_DISTRIBUTION_MODEL_KIND.to_string(), + fit_transform: RESPONSE_TIME_DISTRIBUTION_FIT_TRANSFORM.to_string(), + sample_count: delays.len(), + retained_sample_count: delays.len(), + tail_winsorized_count: delays + .iter() + .filter(|delay| **delay > global_upper_seconds) + .count(), + split_seconds: vec![immediate_upper_seconds, short_upper_seconds], + global_upper_seconds, + components, + }; + response_time_distribution_is_valid(&distribution).then_some(distribution) +} + +pub(super) fn response_time_distribution_is_valid(distribution: &ResponseTimeDistribution) -> bool { + if distribution.schema_version != RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION + || distribution.policy_version != RESPONSE_TIME_DISTRIBUTION_POLICY_VERSION + || distribution.model_kind != RESPONSE_TIME_DISTRIBUTION_MODEL_KIND + || distribution.fit_transform != RESPONSE_TIME_DISTRIBUTION_FIT_TRANSFORM + || distribution.sample_count < RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES + || distribution.retained_sample_count != distribution.sample_count + || distribution.tail_winsorized_count >= distribution.sample_count + || distribution.components.len() != RESPONSE_TIME_DISTRIBUTION_COMPONENTS + || distribution.split_seconds.len() != RESPONSE_TIME_DISTRIBUTION_COMPONENTS - 1 + || distribution + .split_seconds + .iter() + .any(|split| !split.is_finite()) + || !distribution.global_upper_seconds.is_finite() + || distribution.split_seconds[0] < MIN_SCHEDULED_RESPONSE_DELAY_SECONDS + || distribution.global_upper_seconds > MAX_RESPONSE_DELAY_SECONDS as f64 + || distribution.split_seconds[0] >= distribution.split_seconds[1] + || distribution.split_seconds[1] >= distribution.global_upper_seconds + { + return false; + } + let expected_names = ["immediate", "short", "delayed"]; + let mut represented_samples = 0usize; + let mut total_weight = 0.0; + for (component, expected_name) in distribution.components.iter().zip(expected_names) { + if component.name != expected_name + || component.sample_count < RESPONSE_TIME_DISTRIBUTION_MIN_COMPONENT_SAMPLES + || !component.weight.is_finite() + || component.weight <= 0.0 + || !component.normal_location_seconds.is_finite() + || !component.normal_scale_seconds.is_finite() + || component.normal_scale_seconds <= 0.0 + || !component.lower_seconds.is_finite() + || !component.upper_seconds.is_finite() + || component.lower_seconds < MIN_SCHEDULED_RESPONSE_DELAY_SECONDS + || component.lower_seconds >= component.upper_seconds + || component.upper_seconds > distribution.global_upper_seconds + || !(component.lower_seconds..=component.upper_seconds) + .contains(&component.normal_location_seconds) + || (component.weight - component.sample_count as f64 / distribution.sample_count as f64) + .abs() + > 1e-12 + { + return false; + } + represented_samples = match represented_samples.checked_add(component.sample_count) { + Some(value) => value, + None => return false, + }; + total_weight += component.weight; + } + if represented_samples != distribution.sample_count + || (total_weight - 1.0).abs() > 1e-12 + || distribution.components[0].upper_seconds != distribution.split_seconds[0] + || distribution.components[1].upper_seconds != distribution.split_seconds[1] + || distribution.components[0].upper_seconds >= distribution.components[1].lower_seconds + || distribution.components[1].upper_seconds >= distribution.components[2].lower_seconds + { + return false; + } + distribution.global_upper_seconds + == distribution + .components + .iter() + .map(|component| component.upper_seconds) + .fold(0.0, f64::max) +} + +pub(super) fn summarize_response_delays( + chat: &str, + source: &str, + user: &str, + mut delays: Vec, +) -> Option { + if delays.is_empty() + || delays.iter().any(|delay| { + !delay.is_finite() || !(0.0..=MAX_RESPONSE_DELAY_SECONDS as f64).contains(delay) + }) + { + return None; + } + delays.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal)); + let average_seconds = delays.iter().sum::() / delays.len() as f64; + let stddev_seconds = if delays.len() > 1 { + (delays + .iter() + .map(|delay| (delay - average_seconds).powi(2)) + .sum::() + / (delays.len() - 1) as f64) + .sqrt() + } else { + 0.0 + }; + let distribution = fit_response_time_distribution(&delays); + Some(ResponseTimeStats { + chat: chat.to_string(), + source: source.to_string(), + user: user.to_string(), + sample_count: delays.len(), + average_seconds, + median_seconds: response_delay_percentile(&delays, 0.5), + p90_seconds: response_delay_percentile(&delays, 0.9), + min_seconds: delays[0], + max_seconds: *delays.last().unwrap_or(&delays[0]), + max_window_seconds: MAX_RESPONSE_DELAY_SECONDS, + stddev_seconds, + distribution, + }) +} + +pub fn response_time_stats( + db_path: &Path, + chat: &str, + user: &str, + source: Option<&str>, +) -> Result> { + let conn = open_db_readonly(db_path)?; + ensure_retrieval_index_current(&conn)?; + response_time_stats_with_connection(&conn, chat, user, source) +} + +pub(super) fn response_time_stats_with_connection( + conn: &Connection, + chat: &str, + user: &str, + source: Option<&str>, +) -> Result> { + ensure_retrieval_index_current(conn)?; + let preferred_source = preferred_context_source(conn, Some(chat), source)?; + let mut stmt = conn.prepare( + "SELECT chat, source, user_name, sample_count, average_seconds, median_seconds, p90_seconds, min_seconds, max_seconds, max_window_seconds, stddev_seconds, distribution_schema_version, distribution_json + FROM response_time_stats + WHERE chat = ?1 AND user_name = ?2 AND (?3 IS NULL OR source = ?3) + ORDER BY sample_count DESC + LIMIT 1", + )?; + match stmt.query_row(params![chat, user, preferred_source.as_deref()], |row| { + let distribution_schema_version = row.get::<_, i64>(11)?; + let distribution_json = row.get::<_, String>(12)?; + let distribution = + if distribution_schema_version == RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION as i64 { + serde_json::from_str::(&distribution_json) + .ok() + .filter(response_time_distribution_is_valid) + } else { + None + }; + Ok(ResponseTimeStats { + chat: row.get(0)?, + source: row.get(1)?, + user: row.get(2)?, + sample_count: row.get::<_, i64>(3)? as usize, + average_seconds: row.get(4)?, + median_seconds: row.get(5)?, + p90_seconds: row.get(6)?, + min_seconds: row.get(7)?, + max_seconds: row.get(8)?, + max_window_seconds: row.get(9)?, + stddev_seconds: row.get(10)?, + distribution, + }) + }) { + Ok(stats) => Ok(Some(stats)), + Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None), + Err(error) => Err(error.into()), + } +} diff --git a/src/context/schema.rs b/src/context/schema.rs new file mode 100644 index 00000000..e4c86324 --- /dev/null +++ b/src/context/schema.rs @@ -0,0 +1,694 @@ +//! Context database tables, the retrieval-index version marker, and every +//! migration that brings an older database up to date. +//! Schema version strings stay `&str` and must not be normalized. + +use super::live::RecipientStyleAccumulator; +use super::response_time::{ + fit_response_time_distribution, response_time_distribution_is_valid, ResponseTimeDistribution, + RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION, +}; +use super::store::open_db; +use super::style::{ + classify_style_message, insert_style_profile, StyleProfileAccumulator, STYLE_POLICY_VERSION, + STYLE_USER, +}; +use super::topics::{backfill_message_topics, ensure_topic_lexicon}; +use anyhow::Result; +use chrono::Utc; +use rusqlite::{params, Connection, OptionalExtension}; +use std::cmp::Ordering; +use std::collections::BTreeMap; +use std::path::Path; + +pub(super) const RETRIEVAL_INDEX_SCHEMA_VERSION: &str = "2"; + +pub(super) const CONTEXT_RETRIEVAL_MIGRATION_REQUIRED: &str = + "context retrieval index migration required; run context-index"; + +pub(super) const LIVE_CONTEXT_SCHEMA_VERSION: &str = "2"; + +/// Apply the additive context/FTS/live-index migrations without ingesting or +/// deleting any context rows. Safe to call before a first sync state lookup. +pub fn ensure_live_context_schema(db_path: &Path) -> Result<()> { + let _ = open_db(db_path)?; + Ok(()) +} + +/// Rename legacy `choi_yeonwoo_*` style tables to their `owner_*` names when an +/// older database is opened, so its data survives the owner-scope +/// generalization (R10.6). Only renames when the legacy table exists and the +/// renamed table does not, so it is safe to run on every open and on fresh +/// databases (where it does nothing). +fn rename_legacy_owner_tables(conn: &Connection) -> Result<()> { + const RENAMES: [(&str, &str); 4] = [ + ("choi_yeonwoo_style", "owner_style"), + ("choi_yeonwoo_style_profile", "owner_style_profile"), + ( + "choi_yeonwoo_recipient_style_profile", + "owner_recipient_style_profile", + ), + ( + "choi_yeonwoo_recipient_style_samples", + "owner_recipient_style_samples", + ), + ]; + let table_present = |name: &str| -> Result { + Ok(conn + .query_row( + "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?1", + params![name], + |_| Ok(()), + ) + .optional()? + .is_some()) + }; + for (legacy, renamed) in RENAMES { + if table_present(legacy)? && !table_present(renamed)? { + conn.execute_batch(&format!("ALTER TABLE {legacy} RENAME TO {renamed}"))?; + } + } + Ok(()) +} + +pub(super) fn migrate(conn: &Connection, was_missing: bool) -> Result<()> { + // Owner-scope rename (R10.6): before the `CREATE TABLE IF NOT EXISTS owner_*` + // statements run, rename any legacy `choi_yeonwoo_*` tables so an upgrading + // database keeps its style/context data instead of orphaning it under the + // old names. `profile::migrate_owner_scope` later adds the `owner_key` + // column and records the migration marker once the owner is confirmed. + rename_legacy_owner_tables(conn)?; + conn.execute_batch("PRAGMA secure_delete = ON; + PRAGMA journal_size_limit = 0; + PRAGMA wal_autocheckpoint = 1000; + CREATE TABLE IF NOT EXISTS context_messages(id INTEGER PRIMARY KEY, source TEXT NOT NULL, chat TEXT NOT NULL, date TEXT NOT NULL, user_name TEXT NOT NULL, message TEXT NOT NULL, vector BLOB NOT NULL); + CREATE INDEX IF NOT EXISTS idx_context_messages_chat_source ON context_messages(chat, source); + CREATE TABLE IF NOT EXISTS context_retrieval_meta( + key TEXT PRIMARY KEY, + value TEXT NOT NULL + ); + CREATE VIRTUAL TABLE IF NOT EXISTS context_messages_fts USING fts5(message, user_name, chat, content='context_messages', content_rowid='id'); + CREATE TRIGGER IF NOT EXISTS context_messages_ai AFTER INSERT ON context_messages BEGIN INSERT INTO context_messages_fts(rowid,message,user_name,chat) VALUES(new.id,new.message,new.user_name,new.chat); END; + CREATE TRIGGER IF NOT EXISTS context_messages_ad AFTER DELETE ON context_messages BEGIN INSERT INTO context_messages_fts(context_messages_fts,rowid,message,user_name,chat) VALUES('delete',old.id,old.message,old.user_name,old.chat); END; + CREATE TRIGGER IF NOT EXISTS context_messages_au AFTER UPDATE ON context_messages BEGIN INSERT INTO context_messages_fts(context_messages_fts,rowid,message,user_name,chat) VALUES('delete',old.id,old.message,old.user_name,old.chat); INSERT INTO context_messages_fts(rowid,message,user_name,chat) VALUES(new.id,new.message,new.user_name,new.chat); END; + CREATE TABLE IF NOT EXISTS owner_style(id INTEGER PRIMARY KEY, source TEXT NOT NULL, chat TEXT NOT NULL, date TEXT NOT NULL, user_name TEXT NOT NULL CHECK(user_name = '최연우'), message TEXT NOT NULL, vector BLOB NOT NULL, source_row INTEGER NOT NULL DEFAULT 0, content_kind TEXT NOT NULL DEFAULT 'legacy', style_eligible INTEGER NOT NULL DEFAULT 1, policy_version TEXT NOT NULL DEFAULT 'legacy-v1', features_json TEXT NOT NULL DEFAULT '{}'); + CREATE INDEX IF NOT EXISTS idx_owner_style_chat_source ON owner_style(chat, source); + CREATE TABLE IF NOT EXISTS owner_style_profile( + chat TEXT NOT NULL, + source TEXT NOT NULL, + user_name TEXT NOT NULL CHECK(user_name = '최연우'), + sample_count INTEGER NOT NULL, + average_character_length REAL NOT NULL, + median_character_length REAL NOT NULL, + p90_character_length REAL NOT NULL, + casual_ending_count INTEGER NOT NULL, + casual_ending_counts_json TEXT NOT NULL DEFAULT '{}', + question_count INTEGER NOT NULL, + emoji_count INTEGER NOT NULL, + punctuation_count INTEGER NOT NULL, + common_endings_json TEXT NOT NULL DEFAULT '{}', + common_tokens_json TEXT NOT NULL DEFAULT '{}', + policy_version TEXT NOT NULL, + created_at TEXT NOT NULL, + PRIMARY KEY(chat, source, user_name) + ); + CREATE INDEX IF NOT EXISTS idx_owner_style_profile_chat_user ON owner_style_profile(chat, user_name); + CREATE TABLE IF NOT EXISTS response_time_stats(chat TEXT NOT NULL, source TEXT NOT NULL, user_name TEXT NOT NULL CHECK(user_name = '최연우'), sample_count INTEGER NOT NULL, average_seconds REAL NOT NULL, median_seconds REAL NOT NULL, p90_seconds REAL NOT NULL, min_seconds REAL NOT NULL, max_seconds REAL NOT NULL, max_window_seconds INTEGER NOT NULL, stddev_seconds REAL NOT NULL DEFAULT 0.0, distribution_schema_version INTEGER NOT NULL DEFAULT 0, distribution_json TEXT NOT NULL DEFAULT '{}', PRIMARY KEY(chat, source, user_name)); + CREATE INDEX IF NOT EXISTS idx_response_time_stats_chat_user ON response_time_stats(chat, user_name); + CREATE TABLE IF NOT EXISTS reply_decisions( + event_id TEXT PRIMARY KEY, + chat TEXT NOT NULL, + author TEXT NOT NULL, + received_at TEXT NOT NULL, + message TEXT NOT NULL, + vector BLOB NOT NULL, + decision TEXT NOT NULL CHECK(decision IN ('reply', 'skip')), + reason TEXT NOT NULL, + category TEXT NOT NULL, + context_match_count INTEGER NOT NULL, + style_match_count INTEGER NOT NULL, + best_context_score REAL NOT NULL, + best_style_score REAL NOT NULL, + prior_similarity REAL NOT NULL, + scheduled_delay_seconds REAL NOT NULL, + status TEXT NOT NULL, + reply TEXT, + sent_at TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + evidence_json TEXT NOT NULL DEFAULT '{}' + ); + CREATE INDEX IF NOT EXISTS idx_reply_decisions_chat_created ON reply_decisions(chat, created_at); + CREATE INDEX IF NOT EXISTS idx_reply_decisions_chat_status ON reply_decisions(chat, status);")?; + let style_columns = conn + .prepare("PRAGMA table_info(owner_style)")? + .query_map([], |row| row.get::<_, String>(1))? + .collect::>>()?; + for (name, declaration) in [ + ("source_row", "INTEGER NOT NULL DEFAULT 0"), + ("content_kind", "TEXT NOT NULL DEFAULT 'legacy'"), + ("style_eligible", "INTEGER NOT NULL DEFAULT 1"), + ("policy_version", "TEXT NOT NULL DEFAULT 'legacy-v1'"), + ("features_json", "TEXT NOT NULL DEFAULT '{}'"), + ] { + if !style_columns.iter().any(|column| column == name) { + conn.execute( + &format!("ALTER TABLE owner_style ADD COLUMN {name} {declaration}"), + [], + )?; + } + } + conn.execute( + "CREATE INDEX IF NOT EXISTS idx_owner_style_eligible + ON owner_style(chat, source, style_eligible)", + [], + )?; + let response_time_columns = conn + .prepare("PRAGMA table_info(response_time_stats)")? + .query_map([], |row| row.get::<_, String>(1))? + .collect::>>()?; + if !response_time_columns + .iter() + .any(|name| name == "stddev_seconds") + { + conn.execute( + "ALTER TABLE response_time_stats ADD COLUMN stddev_seconds REAL NOT NULL DEFAULT 0.0", + [], + )?; + } + if !response_time_columns + .iter() + .any(|name| name == "distribution_schema_version") + { + conn.execute( + "ALTER TABLE response_time_stats ADD COLUMN distribution_schema_version INTEGER NOT NULL DEFAULT 0", + [], + )?; + } + if !response_time_columns + .iter() + .any(|name| name == "distribution_json") + { + conn.execute( + "ALTER TABLE response_time_stats ADD COLUMN distribution_json TEXT NOT NULL DEFAULT '{}'", + [], + )?; + } + let reply_decision_columns = conn + .prepare("PRAGMA table_info(reply_decisions)")? + .query_map([], |row| row.get::<_, String>(1))? + .collect::>>()?; + if !reply_decision_columns + .iter() + .any(|name| name == "evidence_json") + { + conn.execute( + "ALTER TABLE reply_decisions ADD COLUMN evidence_json TEXT NOT NULL DEFAULT '{}'", + [], + )?; + } + if was_missing { + conn.execute( + "INSERT INTO context_retrieval_meta(key, value) VALUES ('fts_schema', ?1) + ON CONFLICT(key) DO UPDATE SET value = excluded.value", + [RETRIEVAL_INDEX_SCHEMA_VERSION], + )?; + } + migrate_live_context_schema(conn)?; + migrate_style_policy(conn)?; + migrate_response_time_distribution_schema(conn)?; + Ok(()) +} + +fn migrate_style_policy(conn: &Connection) -> Result<()> { + const MIGRATION_KEY: &str = "style_policy_version"; + let current: Option = conn + .query_row( + "SELECT value FROM context_retrieval_meta WHERE key = ?1", + [MIGRATION_KEY], + |row| row.get(0), + ) + .optional()?; + if current.as_deref() == Some(STYLE_POLICY_VERSION) { + return Ok(()); + } + + let tx = conn.unchecked_transaction()?; + let style_rows = { + let mut stmt = tx.prepare("SELECT id, message FROM owner_style ORDER BY id ASC")?; + let rows = stmt + .query_map([], |row| { + Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?)) + })? + .collect::>>()?; + rows + }; + for (id, message) in style_rows { + let features = classify_style_message(&message); + tx.execute( + "UPDATE owner_style + SET content_kind = ?2, style_eligible = ?3, + policy_version = ?4, features_json = ?5 + WHERE id = ?1", + params![ + id, + features.content_kind, + features.style_eligible as i64, + STYLE_POLICY_VERSION, + features.features_json, + ], + )?; + } + tx.execute( + "DELETE FROM owner_recipient_style_samples + WHERE style_message_id IN ( + SELECT id FROM owner_style + WHERE style_eligible <> 1 OR policy_version <> ?1 + )", + [STYLE_POLICY_VERSION], + )?; + + let now = Utc::now().to_rfc3339(); + tx.execute("DELETE FROM owner_style_profile", [])?; + let room_sources = { + let mut stmt = tx.prepare( + "SELECT DISTINCT source, chat FROM owner_style + WHERE user_name = ?1 AND style_eligible = 1 + AND policy_version = ?2 + ORDER BY source ASC, chat ASC", + )?; + let rows = stmt + .query_map(params![STYLE_USER, STYLE_POLICY_VERSION], |row| { + Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)) + })? + .collect::>>()?; + rows + }; + for (source, chat) in room_sources { + let messages = { + let mut stmt = tx.prepare( + "SELECT message FROM owner_style + WHERE source = ?1 AND chat = ?2 AND user_name = ?3 + AND style_eligible = 1 AND policy_version = ?4 + ORDER BY source_row ASC, id ASC", + )?; + let rows = stmt + .query_map( + params![source, chat, STYLE_USER, STYLE_POLICY_VERSION], + |row| row.get::<_, String>(0), + )? + .collect::>>()?; + rows + }; + let mut accumulator = StyleProfileAccumulator::default(); + for message in messages { + let features = classify_style_message(&message); + if features.style_eligible { + accumulator.add(&message, &features); + } + } + if let Some(profile) = accumulator.finish(&chat, &source, STYLE_USER) { + insert_style_profile(&tx, &profile, &now)?; + } + } + + tx.execute("DELETE FROM owner_recipient_style_profile", [])?; + let recipient_rows = { + let mut stmt = tx.prepare( + "SELECT samples.source, samples.chat_id, sources.chat, + samples.recipient, samples.confidence, styles.message + FROM owner_recipient_style_samples samples + JOIN owner_style styles ON styles.id = samples.style_message_id + JOIN context_sources sources ON sources.source = samples.source + WHERE styles.user_name = ?1 AND styles.style_eligible = 1 + AND styles.policy_version = ?2 + ORDER BY samples.source ASC, samples.chat_id ASC, + samples.recipient ASC, samples.reply_log_id ASC", + )?; + let rows = stmt + .query_map(params![STYLE_USER, STYLE_POLICY_VERSION], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, i64>(1)?, + row.get::<_, String>(2)?, + row.get::<_, String>(3)?, + row.get::<_, f64>(4)?, + row.get::<_, String>(5)?, + )) + })? + .collect::>>()?; + rows + }; + let mut recipient_accumulators: BTreeMap< + (String, i64, String, String), + RecipientStyleAccumulator, + > = BTreeMap::new(); + for (source, chat_id, chat, recipient, confidence, message) in recipient_rows { + if !confidence.is_finite() || !(0.0..=1.0).contains(&confidence) || confidence == 0.0 { + anyhow::bail!("recipient style confidence is malformed"); + } + let features = classify_style_message(&message); + if !features.style_eligible { + continue; + } + let accumulator = recipient_accumulators + .entry((source, chat_id, chat, recipient)) + .or_default(); + accumulator.style.add(&message, &features); + accumulator.sample_count += 1; + accumulator.confidence_sum += confidence; + } + for ((source, chat_id, chat, recipient), accumulator) in recipient_accumulators { + let Some(profile) = accumulator.style.finish(&chat, &source, STYLE_USER) else { + continue; + }; + tx.execute( + "INSERT INTO owner_recipient_style_profile( + source, chat_id, chat, recipient, user_name, sample_count, + confidence_sum, average_character_length, median_character_length, + p90_character_length, casual_ending_count, casual_ending_counts_json, + question_count, emoji_count, punctuation_count, common_endings_json, + common_tokens_json, policy_version, updated_at + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, + ?13, ?14, ?15, ?16, ?17, ?18, ?19)", + params![ + source, + chat_id, + chat, + recipient, + STYLE_USER, + accumulator.sample_count as i64, + accumulator.confidence_sum, + profile.average_character_length, + profile.median_character_length, + profile.p90_character_length, + profile.casual_ending_count as i64, + profile.casual_ending_counts_json, + profile.question_count as i64, + profile.emoji_count as i64, + profile.punctuation_count as i64, + profile.common_endings_json, + profile.common_tokens_json, + STYLE_POLICY_VERSION, + now, + ], + )?; + } + + tx.execute( + "INSERT INTO context_retrieval_meta(key, value) VALUES (?1, ?2) + ON CONFLICT(key) DO UPDATE SET value = excluded.value", + params![MIGRATION_KEY, STYLE_POLICY_VERSION], + )?; + tx.commit()?; + Ok(()) +} + +fn migrate_response_time_distribution_schema(conn: &Connection) -> Result<()> { + let migration_key = "response_time_distribution_schema"; + let expected_version = RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION.to_string(); + let current: Option = conn + .query_row( + "SELECT value FROM context_retrieval_meta WHERE key = ?1", + [migration_key], + |row| row.get(0), + ) + .optional()?; + let mut live_rows = conn + .prepare( + "SELECT stats.chat, stats.source, sources.chat_id, + stats.distribution_schema_version, stats.distribution_json + FROM response_time_stats stats + JOIN context_sources sources ON sources.source = stats.source + WHERE stats.user_name = ?1 + ORDER BY stats.source ASC", + )? + .query_map([STYLE_USER], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, i64>(2)?, + row.get::<_, i64>(3)?, + row.get::<_, String>(4)?, + )) + })? + .collect::>>()?; + live_rows.sort(); + let valid = |schema_version: i64, distribution_json: &str| { + schema_version == RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION as i64 + && serde_json::from_str::(distribution_json) + .ok() + .as_ref() + .is_some_and(response_time_distribution_is_valid) + }; + if current.as_deref() == Some(expected_version.as_str()) + && live_rows + .iter() + .all(|(_, _, _, schema_version, distribution_json)| { + valid(*schema_version, distribution_json) + }) + { + return Ok(()); + } + let tx = conn.unchecked_transaction()?; + for (_chat, source, chat_id, schema_version, distribution_json) in live_rows { + if current.as_deref() == Some(expected_version.as_str()) + && valid(schema_version, &distribution_json) + { + continue; + } + let mut delays = tx + .prepare( + "SELECT delay_seconds FROM response_time_samples + WHERE source = ?1 AND chat_id = ?2 + ORDER BY delay_seconds ASC, reply_log_id ASC", + )? + .query_map(params![source, chat_id], |row| row.get::<_, f64>(0))? + .collect::>>()?; + delays.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal)); + let distribution = fit_response_time_distribution(&delays); + let (schema_version, distribution_json) = match distribution { + Some(distribution) => ( + distribution.schema_version as i64, + serde_json::to_string(&distribution)?, + ), + None => (0, "{}".to_string()), + }; + tx.execute( + "UPDATE response_time_stats + SET distribution_schema_version = ?3, distribution_json = ?4 + WHERE source = ?1 AND user_name = ?2", + params![source, STYLE_USER, schema_version, distribution_json], + )?; + } + tx.execute( + "INSERT INTO context_retrieval_meta(key, value) VALUES (?1, ?2) + ON CONFLICT(key) DO UPDATE SET value = excluded.value", + params![migration_key, expected_version], + )?; + tx.commit()?; + Ok(()) +} + +fn migrate_live_context_schema(conn: &Connection) -> Result<()> { + let tx = conn.unchecked_transaction()?; + tx.execute_batch( + "CREATE TABLE IF NOT EXISTS context_sources( + source TEXT PRIMARY KEY, + kind TEXT NOT NULL CHECK(kind = 'local_db'), + account_fingerprint TEXT NOT NULL, + chat_id INTEGER NOT NULL CHECK(chat_id > 0), + chat TEXT NOT NULL, + authoritative INTEGER NOT NULL DEFAULT 0 CHECK(authoritative IN (0, 1)), + checkpoint_log_id INTEGER NOT NULL DEFAULT 0 CHECK(checkpoint_log_id >= 0), + pending_human_log_id INTEGER, + pending_human_sent_at INTEGER, + pending_human_name TEXT, + pending_burst_json TEXT NOT NULL DEFAULT '[]', + summary_dirty INTEGER NOT NULL DEFAULT 0 CHECK(summary_dirty IN (0, 1)), + sync_status TEXT NOT NULL DEFAULT 'ready', + updated_at TEXT NOT NULL, + UNIQUE(account_fingerprint, chat_id) + ); + CREATE UNIQUE INDEX IF NOT EXISTS idx_context_sources_authoritative_chat + ON context_sources(chat) WHERE authoritative = 1; + CREATE INDEX IF NOT EXISTS idx_context_sources_chat + ON context_sources(chat, authoritative, updated_at); + CREATE TABLE IF NOT EXISTS context_live_events( + source TEXT NOT NULL, + chat_id INTEGER NOT NULL CHECK(chat_id > 0), + log_id INTEGER NOT NULL CHECK(log_id > 0), + sent_at INTEGER NOT NULL, + sender_name TEXT NOT NULL, + message_digest TEXT NOT NULL, + disposition TEXT NOT NULL, + auto_generated INTEGER NOT NULL CHECK(auto_generated IN (0, 1)), + context_message_id INTEGER, + style_message_id INTEGER, + created_at TEXT NOT NULL, + PRIMARY KEY(source, chat_id, log_id), + FOREIGN KEY(source) REFERENCES context_sources(source) + ); + CREATE INDEX IF NOT EXISTS idx_context_live_events_context_message + ON context_live_events(context_message_id); + CREATE INDEX IF NOT EXISTS idx_context_live_events_style_message + ON context_live_events(style_message_id); + CREATE TABLE IF NOT EXISTS response_time_samples( + source TEXT NOT NULL, + chat_id INTEGER NOT NULL CHECK(chat_id > 0), + reply_log_id INTEGER NOT NULL CHECK(reply_log_id > 0), + prompt_log_id INTEGER NOT NULL CHECK(prompt_log_id > 0), + recipient TEXT NOT NULL, + delay_seconds REAL NOT NULL, + PRIMARY KEY(source, chat_id, reply_log_id), + FOREIGN KEY(source) REFERENCES context_sources(source) + ); + CREATE INDEX IF NOT EXISTS idx_response_time_samples_source_chat + ON response_time_samples(source, chat_id, reply_log_id); + CREATE TABLE IF NOT EXISTS owner_recipient_style_samples( + source TEXT NOT NULL, + chat_id INTEGER NOT NULL CHECK(chat_id > 0), + reply_log_id INTEGER NOT NULL CHECK(reply_log_id > 0), + recipient TEXT NOT NULL, + style_message_id INTEGER NOT NULL, + burst_size INTEGER NOT NULL CHECK(burst_size > 0), + confidence REAL NOT NULL CHECK(confidence > 0.0 AND confidence <= 1.0), + PRIMARY KEY(source, chat_id, reply_log_id, recipient), + FOREIGN KEY(source) REFERENCES context_sources(source) + ); + CREATE INDEX IF NOT EXISTS idx_recipient_style_samples_recipient + ON owner_recipient_style_samples(source, chat_id, recipient); + CREATE INDEX IF NOT EXISTS idx_recipient_style_samples_lookup + ON owner_recipient_style_samples( + source, recipient, reply_log_id DESC, style_message_id DESC + ); + CREATE TABLE IF NOT EXISTS owner_recipient_style_profile( + source TEXT NOT NULL, + chat_id INTEGER NOT NULL CHECK(chat_id > 0), + chat TEXT NOT NULL, + recipient TEXT NOT NULL, + user_name TEXT NOT NULL CHECK(user_name = '최연우'), + sample_count INTEGER NOT NULL, + confidence_sum REAL NOT NULL, + average_character_length REAL NOT NULL, + median_character_length REAL NOT NULL, + p90_character_length REAL NOT NULL, + casual_ending_count INTEGER NOT NULL, + casual_ending_counts_json TEXT NOT NULL DEFAULT '{}', + question_count INTEGER NOT NULL, + emoji_count INTEGER NOT NULL, + punctuation_count INTEGER NOT NULL, + common_endings_json TEXT NOT NULL DEFAULT '{}', + common_tokens_json TEXT NOT NULL DEFAULT '{}', + policy_version TEXT NOT NULL, + updated_at TEXT NOT NULL, + PRIMARY KEY(source, chat_id, recipient) + ); + CREATE INDEX IF NOT EXISTS idx_recipient_style_profile_lookup + ON owner_recipient_style_profile(chat, recipient, sample_count); + CREATE TABLE IF NOT EXISTS context_message_topics( + message_id INTEGER NOT NULL, + topic TEXT NOT NULL CHECK(length(topic) > 0), + PRIMARY KEY(message_id, topic) + ); + CREATE INDEX IF NOT EXISTS idx_context_message_topics_topic + ON context_message_topics(topic, message_id); + CREATE TABLE IF NOT EXISTS context_topic_stats( + chat TEXT NOT NULL, + source TEXT NOT NULL, + topic TEXT NOT NULL, + message_count INTEGER NOT NULL CHECK(message_count >= 0), + last_date TEXT NOT NULL, + PRIMARY KEY(chat, source, topic) + ); + CREATE TABLE IF NOT EXISTS context_reference_packs( + id INTEGER PRIMARY KEY, + pack_key TEXT NOT NULL UNIQUE, + source TEXT NOT NULL, + chat TEXT NOT NULL, + chat_id INTEGER NOT NULL, + user_name TEXT NOT NULL, + started_at TEXT NOT NULL, + ended_at TEXT NOT NULL, + start_log_id INTEGER NOT NULL, + end_log_id INTEGER NOT NULL, + message_count INTEGER NOT NULL, + image_count INTEGER NOT NULL, + quality_score INTEGER NOT NULL, + topics TEXT NOT NULL DEFAULT '', + what_text TEXT NOT NULL, + how_text TEXT NOT NULL, + why_text TEXT NOT NULL, + body TEXT NOT NULL, + vector BLOB NOT NULL, + context_message_id INTEGER, + policy_version TEXT NOT NULL, + created_at TEXT NOT NULL + ); + CREATE INDEX IF NOT EXISTS idx_reference_packs_chat + ON context_reference_packs(chat, quality_score DESC, end_log_id DESC);", + )?; + tx.execute( + "INSERT OR IGNORE INTO context_retrieval_meta(key, value) + VALUES ('live_context_schema', ?1)", + [LIVE_CONTEXT_SCHEMA_VERSION], + )?; + let version: Option = tx + .query_row( + "SELECT value FROM context_retrieval_meta WHERE key = 'live_context_schema'", + [], + |row| row.get(0), + ) + .optional()?; + match version.as_deref() { + Some("1") => { + backfill_message_topics(&tx)?; + tx.execute( + "UPDATE context_retrieval_meta + SET value = ?1 + WHERE key = 'live_context_schema'", + [LIVE_CONTEXT_SCHEMA_VERSION], + )?; + ensure_topic_lexicon(&tx)?; + } + Some(value) if value == LIVE_CONTEXT_SCHEMA_VERSION => { + ensure_topic_lexicon(&tx)?; + } + _ => anyhow::bail!("live context index migration required"), + } + tx.commit()?; + Ok(()) +} + +pub(super) fn ensure_retrieval_index_current(conn: &Connection) -> Result<()> { + let version = match conn + .query_row( + "SELECT value FROM context_retrieval_meta WHERE key = 'fts_schema'", + [], + |row| row.get::<_, String>(0), + ) + .optional() + { + Ok(version) => version, + Err(error) if error.to_string().contains("no such table") => { + anyhow::bail!("{CONTEXT_RETRIEVAL_MIGRATION_REQUIRED}"); + } + Err(error) => return Err(error.into()), + }; + if version.as_deref() != Some(RETRIEVAL_INDEX_SCHEMA_VERSION) { + anyhow::bail!("{CONTEXT_RETRIEVAL_MIGRATION_REQUIRED}"); + } + Ok(()) +} + +pub(super) fn rebuild_retrieval_index(conn: &Connection) -> Result<()> { + conn.execute( + "INSERT INTO context_messages_fts(context_messages_fts) VALUES ('rebuild')", + [], + )?; + conn.execute( + "INSERT INTO context_retrieval_meta(key, value) VALUES ('fts_schema', ?1) + ON CONFLICT(key) DO UPDATE SET value = excluded.value", + [RETRIEVAL_INDEX_SCHEMA_VERSION], + )?; + Ok(()) +} diff --git a/src/context/search.rs b/src/context/search.rs new file mode 100644 index 00000000..db940bd8 --- /dev/null +++ b/src/context/search.rs @@ -0,0 +1,628 @@ +//! Context retrieval: keyword, vector, topic and hybrid candidates +//! (`ContextCandidate`) over context rows and owner style rows. + +use super::embedding::{ + bytes_to_vector, cosine, encode_vector, Embedder, LocalHashEmbedder, VECTOR_DIM, +}; +use super::live::preferred_context_source; +use super::schema::ensure_retrieval_index_current; +use super::store::open_db_readonly; +use super::style::{STYLE_POLICY_VERSION, STYLE_USER}; +use super::topics::{classify_message_topics, context_topic_tables_ready}; +use anyhow::{Context, Result}; +use rusqlite::{params, Connection}; +use serde::Serialize; +use std::cmp::Ordering; +use std::collections::BTreeSet; +use std::path::Path; + +pub(super) const CONTEXT_KEYWORD_CANDIDATE_CAP: usize = 256; + +pub(super) const CONTEXT_VECTOR_CANDIDATE_CAP: usize = 256; + +const CONTEXT_TOPIC_CANDIDATE_CAP: usize = 64; + +pub(super) const STYLE_VECTOR_CANDIDATE_CAP: usize = 256; + +pub(super) const MAX_CONTEXT_RETRIEVAL_SCORE: f32 = 2.0; + +pub(super) const RECIPIENT_STYLE_SEARCH_SQL: &str = + "SELECT styles.id, styles.chat, styles.source, styles.date, + styles.user_name, styles.message, styles.vector, samples.confidence + FROM owner_recipient_style_samples samples + JOIN owner_style styles ON styles.id = samples.style_message_id + WHERE samples.source = ?1 + AND samples.recipient = ?2 + AND styles.chat = ?3 + AND styles.source = samples.source + AND styles.user_name = ?4 + AND styles.style_eligible = 1 + AND styles.policy_version = ?5 + ORDER BY samples.reply_log_id DESC, samples.style_message_id DESC + LIMIT ?6"; + +#[derive(Debug, Clone, Serialize)] +pub struct ContextResult { + pub chat: String, + pub source: String, + pub date: String, + pub user: String, + pub message: String, + pub score: f32, + pub mode: String, +} + +#[derive(Debug, Clone)] +struct ContextCandidate { + id: i64, + result: ContextResult, +} + +pub fn search( + db_path: &Path, + chat: Option<&str>, + source: Option<&str>, + query: &str, + mode: &str, + limit: usize, +) -> Result> { + // 기본 경로는 결정적 로컬 해시 임베더를 주입해 리팩터링 이전과 동일한 + // 검색 결과·정렬을 그대로 유지한다. + search_with_embedder( + db_path, + chat, + source, + query, + mode, + limit, + &LocalHashEmbedder, + ) +} + +/// 착탈식 임베더를 주입받는 검색 진입점. 기본 `search`는 `LocalHashEmbedder`를 +/// 주입해 동작 동일성을 보장한다(R11.1, R11.2). +pub fn search_with_embedder( + db_path: &Path, + chat: Option<&str>, + source: Option<&str>, + query: &str, + mode: &str, + limit: usize, + embedder: &dyn Embedder, +) -> Result> { + if query.trim().is_empty() { + anyhow::bail!("query must not be empty"); + } + if limit == 0 { + return Ok(Vec::new()); + } + let conn = open_db_readonly(db_path)?; + ensure_retrieval_index_current(&conn)?; + search_with_connection_excluding( + &conn, + chat, + source, + query, + mode, + limit, + &BTreeSet::new(), + embedder, + ) +} + +// Mirrors the sibling search entry points' parameter shape (scope, query, +// mode, limit, exclusion, embedder); bundling them is an API change, not a fix. +#[allow( + clippy::too_many_arguments, + reason = "search entry points share one signature" +)] +pub(super) fn search_with_connection_excluding( + conn: &Connection, + chat: Option<&str>, + source: Option<&str>, + query: &str, + mode: &str, + limit: usize, + excluded_context_ids: &BTreeSet, + embedder: &dyn Embedder, +) -> Result> { + ensure_retrieval_index_current(conn)?; + let preferred_source = preferred_context_source(conn, chat, source)?; + let candidates = match mode { + "keyword" => keyword_search_excluding( + conn, + chat, + preferred_source.as_deref(), + query, + excluded_context_ids, + )?, + "vector" => vector_search_excluding( + conn, + chat, + preferred_source.as_deref(), + query, + excluded_context_ids, + embedder, + )?, + "hybrid" => { + return hybrid_search_excluding( + conn, + chat, + preferred_source.as_deref(), + query, + limit, + excluded_context_ids, + embedder, + ) + } + other => { + anyhow::bail!("unknown context search mode '{other}' (use keyword, vector, or hybrid)") + } + }; + Ok(candidates + .into_iter() + .take(limit) + .map(|candidate| candidate.result) + .collect()) +} + +pub fn style_search( + db_path: &Path, + chat: Option<&str>, + query: &str, + limit: usize, +) -> Result> { + if query.trim().is_empty() { + anyhow::bail!("query must not be empty"); + } + if limit == 0 { + return Ok(Vec::new()); + } + let conn = open_db_readonly(db_path)?; + ensure_retrieval_index_current(&conn)?; + style_search_with_connection(&conn, chat, None, query, limit, true) +} + +pub(super) fn style_search_with_connection( + conn: &Connection, + chat: Option<&str>, + source: Option<&str>, + query: &str, + limit: usize, + require_policy: bool, +) -> Result> { + ensure_retrieval_index_current(conn)?; + let preferred_source = preferred_context_source(conn, chat, source)?; + let query_vector = encode_vector(query); + let tokenless = query_vector.iter().all(|value| *value == 0.0); + // Style retrieval intentionally uses a bounded eligible recency/diversity + // pool. This keeps policy filtering ahead of vector scoring without an + // unbounded fallback when no lexical style index is available. + let mut stmt = conn.prepare( + "SELECT id,chat,source,date,user_name,message,vector + FROM owner_style + WHERE user_name = ?5 + AND style_eligible = 1 + AND (?1 IS NULL OR chat = ?1) + AND (?2 IS NULL OR source = ?2) + AND (?3 = 0 OR policy_version = ?4) + ORDER BY date DESC, source_row DESC, id DESC + LIMIT ?6", + )?; + let rows = stmt.query_map( + params![ + chat, + preferred_source.as_deref(), + require_policy as i64, + STYLE_POLICY_VERSION, + STYLE_USER, + STYLE_VECTOR_CANDIDATE_CAP as i64, + ], + |row| { + let bytes: Vec = row.get(6)?; + if bytes.len() != VECTOR_DIM * 4 { + return Err(rusqlite::Error::InvalidColumnType( + 6, + "vector".into(), + rusqlite::types::Type::Blob, + )); + } + Ok(( + row.get::<_, i64>(0)?, + ContextResult { + chat: row.get(1)?, + source: row.get(2)?, + date: row.get(3)?, + user: row.get(4)?, + message: row.get(5)?, + score: 0.0, + mode: "vector_style".into(), + }, + bytes_to_vector(&bytes), + )) + }, + )?; + let mut results = Vec::new(); + for row in rows { + let (id, mut result, vector) = row?; + crate::reply_policy::validate_auto_reply_laughter(&result.message) + .context("style search contains disallowed laughter evidence")?; + result.score = if tokenless { + 0.0 + } else { + cosine(&query_vector, &vector) + }; + if tokenless { + result.mode = "recency_style".into(); + } + results.push(ContextCandidate { id, result }); + } + if !tokenless { + results.sort_by(|a, b| { + b.result + .score + .partial_cmp(&a.result.score) + .unwrap_or(Ordering::Equal) + .then_with(|| a.id.cmp(&b.id)) + }); + } + Ok(results + .into_iter() + .take(limit.min(STYLE_VECTOR_CANDIDATE_CAP)) + .map(|candidate| candidate.result) + .collect()) +} + +pub(super) fn recipient_style_search_with_connection( + conn: &Connection, + chat: &str, + source: &str, + recipient: &str, + query: &str, + limit: usize, +) -> Result> { + ensure_retrieval_index_current(conn)?; + let query_vector = encode_vector(query); + let tokenless = query_vector.iter().all(|value| *value == 0.0); + let mut stmt = conn.prepare(RECIPIENT_STYLE_SEARCH_SQL)?; + let rows = stmt.query_map( + params![ + source, + recipient, + chat, + STYLE_USER, + STYLE_POLICY_VERSION, + STYLE_VECTOR_CANDIDATE_CAP as i64, + ], + |row| { + let bytes: Vec = row.get(6)?; + if bytes.len() != VECTOR_DIM * 4 { + return Err(rusqlite::Error::InvalidColumnType( + 6, + "vector".into(), + rusqlite::types::Type::Blob, + )); + } + let confidence: f32 = row.get(7)?; + if !confidence.is_finite() || !(0.0..=1.0).contains(&confidence) || confidence == 0.0 { + return Err(rusqlite::Error::InvalidColumnType( + 7, + "confidence".into(), + rusqlite::types::Type::Real, + )); + } + Ok(( + row.get::<_, i64>(0)?, + ContextResult { + chat: row.get(1)?, + source: row.get(2)?, + date: row.get(3)?, + user: row.get(4)?, + message: row.get(5)?, + score: 0.0, + mode: "vector_style_recipient".into(), + }, + bytes_to_vector(&bytes), + confidence, + )) + }, + )?; + let mut results = Vec::new(); + for row in rows { + let (id, mut result, vector, confidence) = row?; + crate::reply_policy::validate_auto_reply_laughter(&result.message) + .context("recipient style search contains disallowed laughter evidence")?; + result.score = if tokenless { + 0.0 + } else { + cosine(&query_vector, &vector) * confidence + }; + if tokenless { + result.mode = "recency_style_recipient".into(); + } + results.push(ContextCandidate { id, result }); + } + if !tokenless { + results.sort_by(|left, right| { + right + .result + .score + .partial_cmp(&left.result.score) + .unwrap_or(Ordering::Equal) + .then_with(|| left.id.cmp(&right.id)) + }); + } + Ok(results + .into_iter() + .take(limit.min(STYLE_VECTOR_CANDIDATE_CAP)) + .map(|candidate| candidate.result) + .collect()) +} + +fn keyword_search_excluding( + conn: &Connection, + chat: Option<&str>, + source: Option<&str>, + query: &str, + excluded_context_ids: &BTreeSet, +) -> Result> { + let match_query = query + .split_whitespace() + .map(|term| format!("\"{}\"", term.replace('"', ""))) + .collect::>() + .join(" OR "); + if match_query.trim().is_empty() { + return Ok(Vec::new()); + } + let mut stmt = conn.prepare( + "SELECT m.id,m.chat,m.source,m.date,m.user_name,m.message, + bm25(context_messages_fts) + FROM context_messages_fts f + JOIN context_messages m ON m.id=f.rowid + WHERE context_messages_fts MATCH ?1 + AND (?2 IS NULL OR m.chat=?2) + AND (?3 IS NULL OR m.source=?3) + ORDER BY bm25(context_messages_fts), m.id ASC + LIMIT ?4", + )?; + let candidate_limit = CONTEXT_KEYWORD_CANDIDATE_CAP.saturating_add(excluded_context_ids.len()); + let rows = stmt.query_map( + params![match_query, chat, source, candidate_limit as i64], + |row| { + Ok(ContextCandidate { + id: row.get(0)?, + result: ContextResult { + chat: row.get(1)?, + source: row.get(2)?, + date: row.get(3)?, + user: row.get(4)?, + message: row.get(5)?, + score: -row.get::<_, f64>(6)? as f32, + mode: "keyword".into(), + }, + }) + }, + )?; + let mut results = Vec::new(); + for row in rows { + let row = row?; + if !excluded_context_ids.contains(&row.id) { + results.push(row); + } + if results.len() == CONTEXT_KEYWORD_CANDIDATE_CAP { + break; + } + } + Ok(results) +} + +fn vector_search_excluding( + conn: &Connection, + chat: Option<&str>, + source: Option<&str>, + query: &str, + excluded_context_ids: &BTreeSet, + embedder: &dyn Embedder, +) -> Result> { + let query_vector = embedder.embed(query); + if query_vector.iter().all(|value| *value == 0.0) { + return Ok(Vec::new()); + } + + // Restrict semantic scoring to the bounded lexical candidate set. A + // missing lexical match is an explicit no-match result. + let match_query = query + .split_whitespace() + .map(|term| format!("\"{}\"", term.replace('"', ""))) + .collect::>() + .join(" OR "); + let mut candidates = Vec::new(); + if !match_query.trim().is_empty() { + let mut stmt = conn.prepare( + "SELECT m.id,m.chat,m.source,m.date,m.user_name,m.message,m.vector + FROM context_messages_fts f + JOIN context_messages m ON m.id=f.rowid + WHERE context_messages_fts MATCH ?1 + AND (?2 IS NULL OR m.chat=?2) + AND (?3 IS NULL OR m.source=?3) + ORDER BY bm25(context_messages_fts), m.id ASC + LIMIT ?4", + )?; + let candidate_limit = + CONTEXT_VECTOR_CANDIDATE_CAP.saturating_add(excluded_context_ids.len()); + let rows = stmt.query_map( + params![match_query, chat, source, candidate_limit as i64], + |row| { + let bytes: Vec = row.get(6)?; + if bytes.len() != VECTOR_DIM * 4 { + return Err(rusqlite::Error::InvalidColumnType( + 6, + "vector".into(), + rusqlite::types::Type::Blob, + )); + } + Ok(( + row.get::<_, i64>(0)?, + ContextResult { + chat: row.get(1)?, + source: row.get(2)?, + date: row.get(3)?, + user: row.get(4)?, + message: row.get(5)?, + score: 0.0, + mode: "vector".into(), + }, + bytes_to_vector(&bytes), + )) + }, + )?; + for row in rows { + let (id, mut result, vector) = row?; + if excluded_context_ids.contains(&id) { + continue; + } + result.score = cosine(&query_vector, &vector); + candidates.push(ContextCandidate { id, result }); + if candidates.len() == CONTEXT_VECTOR_CANDIDATE_CAP { + break; + } + } + } + + candidates.sort_by(|a, b| { + b.result + .score + .partial_cmp(&a.result.score) + .unwrap_or(Ordering::Equal) + .then_with(|| a.id.cmp(&b.id)) + }); + Ok(candidates) +} + +fn topic_search_excluding( + conn: &Connection, + chat: Option<&str>, + source: Option<&str>, + query: &str, + excluded_context_ids: &BTreeSet, +) -> Result> { + if !context_topic_tables_ready(conn)? { + return Ok(Vec::new()); + } + let topics = classify_message_topics(query); + if topics.is_empty() { + return Ok(Vec::new()); + } + let mut stmt = conn.prepare( + "SELECT m.id, m.chat, m.source, m.date, m.user_name, m.message + FROM context_message_topics t + JOIN context_messages m ON m.id = t.message_id + WHERE t.topic = ?1 + AND (?2 IS NULL OR m.chat = ?2) + AND (?3 IS NULL OR m.source = ?3) + ORDER BY m.id DESC + LIMIT ?4", + )?; + let candidate_limit = CONTEXT_TOPIC_CANDIDATE_CAP.saturating_add(excluded_context_ids.len()); + let mut results = Vec::new(); + let mut seen = BTreeSet::new(); + for topic in topics { + let rows = stmt.query_map( + params![topic, chat, source, candidate_limit as i64], + |row| { + Ok(ContextCandidate { + id: row.get(0)?, + result: ContextResult { + chat: row.get(1)?, + source: row.get(2)?, + date: row.get(3)?, + user: row.get(4)?, + message: row.get(5)?, + score: 1.0, + mode: "topic".into(), + }, + }) + }, + )?; + for row in rows { + let row = row?; + if excluded_context_ids.contains(&row.id) || !seen.insert(row.id) { + continue; + } + results.push(row); + if results.len() == CONTEXT_TOPIC_CANDIDATE_CAP { + return Ok(results); + } + } + } + Ok(results) +} + +fn hybrid_search_excluding( + conn: &Connection, + chat: Option<&str>, + source: Option<&str>, + query: &str, + limit: usize, + excluded_context_ids: &BTreeSet, + embedder: &dyn Embedder, +) -> Result> { + let keyword = keyword_search_excluding(conn, chat, source, query, excluded_context_ids)?; + // An empty vector query is a fail-closed hybrid result: keyword evidence + // remains available, while no synthetic vector score is introduced. + let vector = if embedder.embed(query).iter().all(|value| *value == 0.0) { + Vec::new() + } else { + vector_search_excluding(conn, chat, source, query, excluded_context_ids, embedder)? + }; + let mut merged = keyword + .into_iter() + .enumerate() + .map(|(rank, mut item)| { + item.result.score = 1.0 / (rank as f32 + 1.0); + item.result.mode = "hybrid".into(); + item + }) + .collect::>(); + for (rank, item) in vector.into_iter().enumerate() { + let score = 1.0 / (rank as f32 + 1.0); + if let Some(existing) = merged.iter_mut().find(|existing| existing.id == item.id) { + existing.result.score += score; + } else { + let mut item = item; + item.result.score = score; + item.result.mode = "hybrid".into(); + merged.push(item); + } + } + let topics = topic_search_excluding(conn, chat, source, query, excluded_context_ids)?; + for (rank, item) in topics.into_iter().enumerate() { + let score = 1.0 / (rank as f32 + 1.0); + if let Some(existing) = merged.iter_mut().find(|existing| existing.id == item.id) { + existing.result.score += score; + } else { + let mut item = item; + item.result.score = score; + item.result.mode = "hybrid".into(); + merged.push(item); + } + } + const REFERENCE_PREFIX: &str = "[설명자료]"; + for item in &mut merged { + if item.result.message.starts_with(REFERENCE_PREFIX) { + item.result.score += 0.35; + } + } + merged.sort_by(|a, b| { + b.result + .score + .partial_cmp(&a.result.score) + .unwrap_or(Ordering::Equal) + .then_with(|| a.id.cmp(&b.id)) + }); + Ok(merged + .into_iter() + .take(limit.min(CONTEXT_KEYWORD_CANDIDATE_CAP)) + .map(|candidate| candidate.result) + .collect()) +} diff --git a/src/context/self_send.rs b/src/context/self_send.rs new file mode 100644 index 00000000..4a564c17 --- /dev/null +++ b/src/context/self_send.rs @@ -0,0 +1,692 @@ +//! Confirmed self-sends: delivery-authority evidence (`ConfirmedSelfSend`) +//! used to classify outgoing self rows and repair style samples that were +//! really the assistant's own replies. + +use super::live::{ + live_event_date, refresh_live_context_summaries, LIVE_CONTEXT_BATCH_MAX_ROWS, + LIVE_CONTEXT_MAX_FIELD_BYTES, +}; +use super::reply_decision::parse_reply_sent_at_seconds; +use super::response_time::{recompute_imported_response_time_stats, MAX_RESPONSE_DELAY_SECONDS}; +use super::schema::ensure_retrieval_index_current; +use super::store::{open_db, open_db_readonly, CONTEXT_DB_BUSY_TIMEOUT_SECS}; +use anyhow::{Context, Result}; +use rusqlite::{params, Connection, OpenFlags, OptionalExtension}; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; +use std::path::Path; +use std::time::Duration; + +pub(super) const AUTO_GENERATED_MATCH_WINDOW_SECONDS: i64 = 120; + +const AUTO_GENERATED_MATCH_MAX_CANDIDATES_PER_TEXT: usize = 16; + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct OutgoingSelfEvent { + pub chat_id: i64, + pub log_id: i64, + pub message: String, + pub sent_at: i64, +} + +#[derive(Debug, Clone, Serialize, PartialEq, Eq)] +pub struct AutoGeneratedSelfEventClassification { + pub chat_id: i64, + pub log_id: i64, + pub auto_generated: bool, + pub matched_event_id: Option, + pub reason: String, +} + +/// Window used to attribute an owner style row to one confirmed send. +pub const SELF_SEND_REPAIR_WINDOW_SECONDS: i64 = 180; + +#[derive(Debug, Clone, Serialize, PartialEq, Eq)] +pub struct SelfSendRepairMatch { + pub source: String, + pub log_id: i64, + pub date: String, + pub message: String, + pub content_kind: String, + pub style_eligible_before: bool, + pub matched_event_id: String, + pub offset_seconds: i64, +} + +#[derive(Debug, Clone, Serialize, PartialEq, Eq)] +pub struct SelfSendRepairReport { + pub schema_version: u32, + pub chat: String, + pub chat_id: i64, + pub confirmed_sends: usize, + pub scanned_rows: usize, + pub matched_rows: usize, + pub ambiguous_rows: usize, + pub already_ineligible: usize, + pub deleted_response_samples: usize, + pub deleted_recipient_samples: usize, + pub rebuilt_sources: Vec, + pub dry_run: bool, + pub matches: Vec, +} + +/// Load the durable confirmed-send records of one chat from the decision +/// ledger. Used where no room queue is addressable (CSV import), so a bot reply +/// that was already confirmed in the context database cannot be learned as an +/// owner style sample. +pub(super) fn confirmed_self_sends_with_connection( + conn: &Connection, + chat: &str, +) -> Result> { + if chat.trim().is_empty() { + anyhow::bail!("confirmed send chat is invalid"); + } + let mut stmt = conn.prepare( + "SELECT event_id, reply, sent_at, updated_at FROM reply_decisions + WHERE chat = ?1 AND decision = 'reply' AND status = 'sent' + AND reply IS NOT NULL AND length(trim(reply)) > 0", + )?; + let rows = stmt.query_map(params![chat], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, Option>(2)?, + row.get::<_, Option>(3)?, + )) + })?; + let mut sends = Vec::new(); + for row in rows { + let (event_id, text, sent_at, updated_at) = row?; + let text = text.trim().to_string(); + if text.is_empty() { + continue; + } + let Some(sent_at) = sent_at + .as_deref() + .and_then(owner_style_timestamp) + .or_else(|| updated_at.as_deref().and_then(owner_style_timestamp)) + else { + continue; + }; + sends.push(ConfirmedSelfSend { + event_id, + text, + sent_at, + }); + } + Ok(sends) +} + +/// Match one owner style row against confirmed sends. +/// +/// Returns the single matching send plus whether the match was ambiguous. A +/// many-to-one match must stay unclassified (fail closed), so callers have to +/// check the ambiguity flag before trusting the match. +pub(super) fn match_confirmed_send<'a>( + sends: &'a [ConfirmedSelfSend], + text: &str, + sent_at: i64, +) -> (Option<&'a ConfirmedSelfSend>, bool) { + let mut unique: BTreeMap<&str, &ConfirmedSelfSend> = BTreeMap::new(); + for send in sends { + if send.text != text { + continue; + } + if sent_at.abs_diff(send.sent_at) <= SELF_SEND_REPAIR_WINDOW_SECONDS as u64 { + unique.entry(send.event_id.as_str()).or_insert(send); + } + } + (unique.values().next().copied(), unique.len() > 1) +} + +pub(super) fn mark_bot_sent_features( + features_json: &str, + event_id: &str, + offset_seconds: i64, +) -> String { + let mut features: serde_json::Value = + serde_json::from_str(features_json).unwrap_or_else(|_| serde_json::json!({})); + if let Some(object) = features.as_object_mut() { + object.insert("style_eligible".to_string(), serde_json::json!(false)); + object.insert( + "content_kind".to_string(), + serde_json::json!("bot_sent_reply"), + ); + object.insert("bot_sent_event_id".to_string(), serde_json::json!(event_id)); + object.insert( + "bot_sent_offset_seconds".to_string(), + serde_json::json!(offset_seconds), + ); + } + features.to_string() +} + +fn owner_style_timestamp(value: &str) -> Option { + chrono::NaiveDateTime::parse_from_str(value.trim(), "%Y-%m-%d %H:%M:%S") + .ok() + .map(|naive| naive.and_utc().timestamp()) + .or_else(|| { + chrono::DateTime::parse_from_rfc3339(value.trim()) + .ok() + .map(|value| value.timestamp()) + }) +} + +/// Mark owner style samples that the assistant itself sent as ineligible and +/// rebuild the aggregates they used to feed. +/// +/// The assistant writes into the room through the owner's own account, so each +/// confirmed outgoing reply also appears in the local database as a +/// self-authored row. Ingest blocks new ones, but rows recorded before that +/// guard are already mixed into `owner_style`, its profile, the response-time +/// samples and the recipient profile. A row is repaired only for a unique, +/// exact-text confirmed send inside [`SELF_SEND_REPAIR_WINDOW_SECONDS`]; a +/// many-to-one match stays untouched (fail closed). +pub fn repair_bot_sent_style_samples( + db_path: &Path, + chat: &str, + chat_id: i64, + confirmed_sends: &[ConfirmedSelfSend], + dry_run: bool, +) -> Result { + if chat.trim().is_empty() || chat.len() > LIVE_CONTEXT_MAX_FIELD_BYTES { + anyhow::bail!("style repair chat is invalid"); + } + if chat_id <= 0 { + anyhow::bail!("style repair chat id is invalid"); + } + let mut conn = open_db(db_path)?; + let rows = { + let mut stmt = conn.prepare( + "SELECT id, source, date, message, content_kind, style_eligible, source_row, + features_json + FROM owner_style WHERE chat = ?1 ORDER BY id ASC", + )?; + let rows = stmt.query_map(params![chat], |row| { + Ok(( + row.get::<_, i64>(0)?, + row.get::<_, String>(1)?, + row.get::<_, String>(2)?, + row.get::<_, String>(3)?, + row.get::<_, String>(4)?, + row.get::<_, i64>(5)?, + row.get::<_, i64>(6)?, + row.get::<_, String>(7)?, + )) + })?; + rows.collect::>>()? + }; + + let mut report = SelfSendRepairReport { + schema_version: 1, + chat: chat.to_string(), + chat_id, + confirmed_sends: confirmed_sends.len(), + scanned_rows: rows.len(), + matched_rows: 0, + ambiguous_rows: 0, + already_ineligible: 0, + deleted_response_samples: 0, + deleted_recipient_samples: 0, + rebuilt_sources: Vec::new(), + dry_run, + matches: Vec::new(), + }; + let mut affected_sources: BTreeMap = BTreeMap::new(); + let tx = conn.transaction()?; + for (style_id, source, date, message, content_kind, eligible, source_row, features_json) in rows + { + let Some(sent_at) = owner_style_timestamp(&date) else { + continue; + }; + let (matched, ambiguous) = match_confirmed_send(confirmed_sends, &message, sent_at); + if ambiguous { + report.ambiguous_rows += 1; + continue; + } + let Some(send) = matched else { + continue; + }; + let offset = sent_at - send.sent_at; + report.matched_rows += 1; + if eligible == 0 { + report.already_ineligible += 1; + } + report.matches.push(SelfSendRepairMatch { + source: source.clone(), + log_id: source_row, + date: date.clone(), + message: message.clone(), + content_kind: content_kind.clone(), + style_eligible_before: eligible != 0, + matched_event_id: send.event_id.clone(), + offset_seconds: offset, + }); + if dry_run { + continue; + } + tx.execute( + "UPDATE owner_style + SET style_eligible = 0, content_kind = 'bot_sent_reply', features_json = ?2 + WHERE id = ?1", + params![ + style_id, + mark_bot_sent_features(&features_json, &send.event_id, offset) + ], + )?; + let deleted_recipients = tx.execute( + "DELETE FROM owner_recipient_style_samples WHERE style_message_id = ?1", + params![style_id], + )?; + report.deleted_recipient_samples += deleted_recipients; + let deleted_samples = if source_row > 0 { + tx.execute( + "DELETE FROM response_time_samples + WHERE source = ?1 AND chat_id = ?2 AND reply_log_id = ?3", + params![source, chat_id, source_row], + )? + } else { + 0 + }; + report.deleted_response_samples += deleted_samples; + // A source is stale whenever anything actually changed, including a row + // that was already style-ineligible: its response-time samples were just + // deleted, so the aggregates derived from them must be regenerated too. + // An imported source is always re-derived: it keeps no sample rows, so a + // bot reply that was measured before the guard existed lives only inside + // its aggregate and nothing else can remove it. + let is_live_source = tx + .query_row( + "SELECT 1 FROM context_sources WHERE source = ?1 AND chat_id = ?2", + params![source, chat_id], + |_| Ok(true), + ) + .optional()? + .unwrap_or(false); + if !is_live_source || eligible != 0 || deleted_samples > 0 || deleted_recipients > 0 { + affected_sources.insert(source.clone(), true); + } + } + + if !dry_run { + for source in affected_sources.keys() { + let source_chat_id = tx + .query_row( + "SELECT chat_id FROM context_sources WHERE source = ?1 AND chat_id = ?2", + params![source, chat_id], + |row| row.get::<_, i64>(0), + ) + .optional()? + .or(tx + .query_row( + "SELECT chat_id FROM context_sources WHERE source = ?1 LIMIT 1", + params![source], + |row| row.get::<_, i64>(0), + ) + .optional()?) + .unwrap_or(0); + if source_chat_id > 0 { + refresh_live_context_summaries(&tx, source, source_chat_id, chat)?; + } else { + recompute_imported_response_time_stats(&tx, source, chat, confirmed_sends)?; + } + report.rebuilt_sources.push(source.clone()); + } + } + tx.commit()?; + Ok(report) +} + +#[derive(Debug, Clone)] +struct AutoGeneratedReplyCandidate { + event_id: String, + confirmed_sent: bool, +} + +/// A confirmed outgoing reply taken from the room delivery authority +/// (`reply-queue.sqlite3`). +/// +/// The queue stays the delivery authority, so a terminal `sent` job is direct +/// evidence that the assistant, not the owner, wrote that text into the room. +/// Style learning must not treat such a row as an owner sample: the worker's +/// own wording would otherwise be re-learned as the owner's voice (AHP: +/// style). +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct ConfirmedSelfSend { + pub event_id: String, + pub text: String, + pub sent_at: i64, +} + +/// Load the durable confirmed-send records of one room queue. +/// +/// Only terminal `sent` jobs with a non-empty reply are returned. `skipped` +/// jobs never reached the composer, so they must not be able to hide a genuine +/// owner message from style learning. A missing or unreadable queue is not an +/// error: it simply contributes no evidence. +pub fn confirmed_self_sends_from_queue(queue_path: &Path) -> Result> { + if queue_path.is_symlink() || !queue_path.is_file() { + return Ok(Vec::new()); + } + let conn = Connection::open_with_flags(queue_path, OpenFlags::SQLITE_OPEN_READ_ONLY) + .with_context(|| format!("open reply queue read-only: {}", queue_path.display()))?; + conn.busy_timeout(Duration::from_secs(CONTEXT_DB_BUSY_TIMEOUT_SECS))?; + let has_jobs: bool = conn + .query_row( + "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'reply_jobs'", + [], + |_| Ok(true), + ) + .optional()? + .unwrap_or(false); + if !has_jobs { + return Ok(Vec::new()); + } + let mut stmt = conn.prepare( + "SELECT event_id, reply, updated_at FROM reply_jobs + WHERE status = 'sent' AND reply IS NOT NULL AND length(trim(reply)) > 0", + )?; + let rows = stmt.query_map([], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, Option>(2)?, + )) + })?; + let mut sends = Vec::new(); + for row in rows { + let (event_id, text, updated_at) = row?; + let text = text.trim().to_string(); + let Some(sent_at) = updated_at.filter(|value| value.is_finite()) else { + continue; + }; + if text.is_empty() { + continue; + } + sends.push(ConfirmedSelfSend { + event_id, + text, + sent_at: sent_at.round() as i64, + }); + } + Ok(sends) +} + +fn ambiguous_auto_candidate_status(status: &str) -> bool { + matches!( + status, + "scheduled" + | "sending" + | "accepted_unconfirmed" + | "delivery_unknown" + | "reconcile_required" + ) +} + +/// Classify a bounded page of caller-identified outgoing self rows against +/// confirmed sends. A row is marked automatic only for a unique, exact-text +/// `sent` decision within +/- 120 seconds. Exact time-bounded candidates in an +/// uncertain delivery state remain non-automatic but are explicitly excluded +/// from learning. Any many-to-one, one-to-many, or unparseable-time match is +/// likewise fail-closed and remains non-automatic. +/// +/// `confirmed_sends` carries the delivery authority's own record (the room +/// queue's terminal `sent` jobs). The reply-decision ledger alone cannot prove a +/// send when the decision row was rewritten as `skip`, so both authorities are +/// unioned and the same uniqueness rule is applied across the union. +pub fn classify_auto_generated_self_events( + db_path: &Path, + chat: &str, + events: &[OutgoingSelfEvent], + confirmed_sends: &[ConfirmedSelfSend], +) -> Result> { + if chat.trim().is_empty() || chat.len() > LIVE_CONTEXT_MAX_FIELD_BYTES { + anyhow::bail!("automatic self-event classification chat is invalid"); + } + if events.len() > LIVE_CONTEXT_BATCH_MAX_ROWS { + anyhow::bail!("automatic self-event classification exceeds the live batch bound"); + } + let mut identities = BTreeMap::new(); + let mut chat_id = None; + let mut grouped: BTreeMap<&str, Vec> = BTreeMap::new(); + for (index, event) in events.iter().enumerate() { + if event.chat_id <= 0 + || event.log_id <= 0 + || event.message.len() > LIVE_CONTEXT_MAX_FIELD_BYTES + { + anyhow::bail!("automatic self-event classification row is invalid"); + } + live_event_date(event.sent_at)?; + if chat_id + .replace(event.chat_id) + .is_some_and(|id| id != event.chat_id) + { + anyhow::bail!("automatic self-event classification spans multiple chats"); + } + if identities + .insert((event.chat_id, event.log_id), ()) + .is_some() + { + anyhow::bail!("automatic self-event classification contains a duplicate row"); + } + grouped + .entry(event.message.as_str()) + .or_default() + .push(index); + } + if events.is_empty() { + return Ok(Vec::new()); + } + + let conn = open_db_readonly(db_path)?; + ensure_retrieval_index_current(&conn)?; + let mut candidates_by_event = vec![Vec::::new(); events.len()]; + let mut has_unparseable_candidate = vec![false; events.len()]; + let mut has_overflow_candidate = vec![false; events.len()]; + for (message, indexes) in grouped { + if message.is_empty() { + continue; + } + let minimum = indexes + .iter() + .map(|index| events[*index].sent_at) + .min() + .unwrap_or(0) + .saturating_sub(AUTO_GENERATED_MATCH_WINDOW_SECONDS); + let maximum = indexes + .iter() + .map(|index| events[*index].sent_at) + .max() + .unwrap_or(0) + .saturating_add(AUTO_GENERATED_MATCH_WINDOW_SECONDS); + let mut stmt = conn.prepare( + "SELECT event_id, status, sent_at, created_at, updated_at, + scheduled_delay_seconds + FROM reply_decisions + WHERE chat = ?1 AND decision = 'reply' AND reply = ?2 + AND ( + (status = 'sent' + AND (CAST(strftime('%s', sent_at) AS INTEGER) BETWEEN ?3 AND ?4 + OR strftime('%s', sent_at) IS NULL)) + OR + (status IN ( + 'scheduled', 'sending', 'accepted_unconfirmed', + 'delivery_unknown', 'reconcile_required' + ) + AND ( + CAST(strftime('%s', updated_at) AS INTEGER) BETWEEN ?3 AND ?4 + OR (CAST(strftime('%s', created_at) AS INTEGER) + + CAST(ROUND(scheduled_delay_seconds) AS INTEGER)) + BETWEEN ?3 AND ?4 + OR strftime('%s', updated_at) IS NULL + OR strftime('%s', created_at) IS NULL + )) + ) + ORDER BY event_id ASC + LIMIT ?5", + )?; + let rows = stmt.query_map( + params![ + chat, + message, + minimum, + maximum, + (AUTO_GENERATED_MATCH_MAX_CANDIDATES_PER_TEXT + 1) as i64, + ], + |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, Option>(2)?, + row.get::<_, String>(3)?, + row.get::<_, String>(4)?, + row.get::<_, f64>(5)?, + )) + }, + )?; + let candidates = rows.collect::>>()?; + if candidates.len() > AUTO_GENERATED_MATCH_MAX_CANDIDATES_PER_TEXT { + anyhow::bail!("automatic self-event match candidate set exceeds its bound"); + } + for (event_id, status, sent_at, created_at, updated_at, scheduled_delay) in candidates { + let confirmed_sent = status == "sent"; + if !confirmed_sent && !ambiguous_auto_candidate_status(&status) { + anyhow::bail!("automatic self-event candidate has an unsupported status"); + } + let candidate_times = if confirmed_sent { + sent_at + .as_deref() + .and_then(parse_reply_sent_at_seconds) + .into_iter() + .collect::>() + } else { + let mut times = Vec::with_capacity(2); + if let Some(updated_at) = parse_reply_sent_at_seconds(&updated_at) { + times.push(updated_at); + } + if scheduled_delay.is_finite() + && (0.0..=(MAX_RESPONSE_DELAY_SECONDS as f64)).contains(&scheduled_delay) + { + if let Some(scheduled_at) = + parse_reply_sent_at_seconds(&created_at).and_then(|created_at| { + created_at.checked_add(scheduled_delay.round() as i64) + }) + { + times.push(scheduled_at); + } + } + times.sort_unstable(); + times.dedup(); + times + }; + if candidate_times.is_empty() { + for index in &indexes { + has_unparseable_candidate[*index] = true; + } + continue; + } + for index in &indexes { + if candidate_times.iter().any(|candidate_time| { + events[*index].sent_at.abs_diff(*candidate_time) + <= AUTO_GENERATED_MATCH_WINDOW_SECONDS as u64 + }) { + candidates_by_event[*index].push(AutoGeneratedReplyCandidate { + event_id: event_id.clone(), + confirmed_sent, + }); + } + } + } + } + + // Union in the delivery authority's confirmed sends. A queue `sent` job has + // no incoming row to attach to, so it is matched by exact text inside the + // same window and identified by its own job id. + let mut grouped_by_text: BTreeMap<&str, Vec> = BTreeMap::new(); + for (index, event) in events.iter().enumerate() { + if event.message.is_empty() { + continue; + } + grouped_by_text + .entry(event.message.as_str()) + .or_default() + .push(index); + } + for send in confirmed_sends { + let Some(indexes) = grouped_by_text.get(send.text.as_str()) else { + continue; + }; + for index in indexes { + if events[*index].sent_at.abs_diff(send.sent_at) + > AUTO_GENERATED_MATCH_WINDOW_SECONDS as u64 + { + continue; + } + if candidates_by_event[*index] + .iter() + .any(|candidate| candidate.event_id == send.event_id) + { + continue; + } + if candidates_by_event[*index].len() > AUTO_GENERATED_MATCH_MAX_CANDIDATES_PER_TEXT { + has_overflow_candidate[*index] = true; + continue; + } + candidates_by_event[*index].push(AutoGeneratedReplyCandidate { + event_id: send.event_id.clone(), + confirmed_sent: true, + }); + } + } + + let mut candidate_use_counts = BTreeMap::new(); + for candidates in &candidates_by_event { + for candidate in candidates { + *candidate_use_counts + .entry(candidate.event_id.as_str()) + .or_insert(0usize) += 1; + } + } + Ok(events + .iter() + .enumerate() + .map(|(index, event)| { + let candidates = &candidates_by_event[index]; + let (auto_generated, matched_event_id, reason) = if has_overflow_candidate[index] { + (false, None, "match_candidate_overflow") + } else if has_unparseable_candidate[index] { + (false, None, "unparseable_candidate_time") + } else if candidates.is_empty() { + (false, None, "no_exact_sent_match") + } else if candidates.len() != 1 { + (false, None, "multiple_sent_matches") + } else if candidate_use_counts + .get(candidates[0].event_id.as_str()) + .copied() + .unwrap_or(0) + != 1 + { + (false, None, "sent_match_reused_in_batch") + } else if !candidates[0].confirmed_sent { + ( + false, + Some(candidates[0].event_id.clone()), + "ambiguous_auto_candidate", + ) + } else { + ( + true, + Some(candidates[0].event_id.clone()), + "unique_exact_sent_match", + ) + }; + AutoGeneratedSelfEventClassification { + chat_id: event.chat_id, + log_id: event.log_id, + auto_generated, + matched_event_id, + reason: reason.to_string(), + } + }) + .collect()) +} diff --git a/src/context/store.rs b/src/context/store.rs new file mode 100644 index 00000000..cc2f67fc --- /dev/null +++ b/src/context/store.rs @@ -0,0 +1,118 @@ +//! The context database file: where it lives, how it is opened, and the +//! fail-closed guards around it. Writers get a WAL connection with private +//! file permissions; readers get a read-only handle that refuses a missing or +//! symlinked database instead of creating or following it. + +use super::schema::{migrate, CONTEXT_RETRIEVAL_MIGRATION_REQUIRED}; +use anyhow::{Context, Result}; +use rusqlite::{Connection, OpenFlags}; +use std::fs; +use std::path::{Path, PathBuf}; +use std::time::Duration; + +pub fn default_db_path() -> PathBuf { + let base = dirs::data_local_dir() + .or_else(dirs::home_dir) + .unwrap_or_else(|| PathBuf::from("/tmp")); + base.join("openkakao").join("context.sqlite3") +} + +/// How long a context-DB connection waits for a competing writer before +/// failing with SQLITE_BUSY. +/// +/// Every AutoReply room syncs the same context database, and on a multi-hundred +/// megabyte database a concurrent `context-sync-local` run can hold the write +/// lock for longer than five seconds. Timing out there fences the room and +/// exits its watcher, so wait the contention out instead. +pub(super) const CONTEXT_DB_BUSY_TIMEOUT_SECS: u64 = 30; + +pub(super) fn set_context_db_permissions(path: &Path) -> Result<()> { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + + if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) { + fs::set_permissions(parent, fs::Permissions::from_mode(0o700)).with_context(|| { + format!("secure context database directory {}", parent.display()) + })?; + } + + for file in [ + path.to_path_buf(), + sqlite_sidecar_path(path, "-wal"), + sqlite_sidecar_path(path, "-shm"), + ] { + let metadata = match fs::symlink_metadata(&file) { + Ok(metadata) => metadata, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue, + Err(error) => { + return Err(error).with_context(|| { + format!("inspect context database file {}", file.display()) + }) + } + }; + if !metadata.file_type().is_file() { + anyhow::bail!( + "context database path is not a regular file: {}", + file.display() + ); + } + fs::set_permissions(&file, fs::Permissions::from_mode(0o600)) + .with_context(|| format!("secure context database file {}", file.display()))?; + } + } + Ok(()) +} + +pub(super) fn sqlite_sidecar_path(path: &Path, suffix: &str) -> PathBuf { + let mut sidecar = path.as_os_str().to_os_string(); + sidecar.push(suffix); + PathBuf::from(sidecar) +} + +pub(super) fn checkpoint_context_wal_truncate(conn: &Connection) -> Result<()> { + let (busy, log_frames, checkpointed_frames): (i64, i64, i64) = + conn.query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |row| { + Ok((row.get(0)?, row.get(1)?, row.get(2)?)) + })?; + if busy != 0 { + anyhow::bail!( + "context WAL truncate checkpoint remained busy ({checkpointed_frames}/{log_frames} frames checkpointed)" + ); + } + Ok(()) +} + +pub(super) fn open_db_readonly(path: &Path) -> Result { + if path.is_symlink() || !path.is_file() { + anyhow::bail!("{CONTEXT_RETRIEVAL_MIGRATION_REQUIRED}"); + } + let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY) + .with_context(|| format!("open context database read-only: {}", path.display()))?; + conn.busy_timeout(Duration::from_secs(CONTEXT_DB_BUSY_TIMEOUT_SECS))?; + Ok(conn) +} + +pub(super) fn open_db(path: &Path) -> Result { + if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) { + fs::create_dir_all(parent)?; + } + set_context_db_permissions(path)?; + let was_missing = !path.exists(); + let conn = Connection::open(path)?; + conn.busy_timeout(Duration::from_secs(CONTEXT_DB_BUSY_TIMEOUT_SECS))?; + // Workers open read-only context snapshots while the watcher updates the + // shared index. WAL keeps those readers on their committed snapshot. + let journal_mode: String = conn.pragma_query_value(None, "journal_mode", |row| row.get(0))?; + if !journal_mode.eq_ignore_ascii_case("wal") { + let enabled_mode: String = + conn.query_row("PRAGMA journal_mode = WAL", [], |row| row.get(0))?; + if !enabled_mode.eq_ignore_ascii_case("wal") { + anyhow::bail!("context database could not enter WAL mode"); + } + } + set_context_db_permissions(path)?; + migrate(&conn, was_missing)?; + set_context_db_permissions(path)?; + Ok(conn) +} diff --git a/src/context/style.rs b/src/context/style.rs new file mode 100644 index 00000000..67a717ce --- /dev/null +++ b/src/context/style.rs @@ -0,0 +1,832 @@ +//! Owner style: per-message style features, the honorific and formality +//! signals, and the stored owner and recipient style profiles. + +use super::live::preferred_context_source; +use super::redact::provenance_id; +use super::schema::ensure_retrieval_index_current; +use super::store::open_db_readonly; +use anyhow::{Context, Result}; +use rusqlite::{params, Connection, OptionalExtension}; +use serde::Serialize; +use std::collections::BTreeMap; +use std::path::Path; + +pub(super) const STYLE_USER: &str = "최연우"; + +pub const STYLE_POLICY_VERSION: &str = "ordinary-conversation-v3"; + +pub(super) const RECIPIENT_STYLE_MIN_SAMPLES: usize = 3; + +pub(super) const RECIPIENT_STYLE_MIN_CONFIDENCE: f64 = 2.0; + +#[derive(Debug, Clone, Serialize)] +pub struct RecipientStyleProfile { + pub recipient: String, + pub direct_sample_count: usize, + pub confidence_sum: f64, + pub used_fallback: bool, + /// 존댓말/반말 파생 필드. 종결어미 통계에서 규칙 기반으로 도출한다(R3.3). + pub honorific: Honorific, + /// 격식/비격식 파생 필드. 종결어미와 이모지·구두점·축약 토큰 통계를 + /// 결합해 규칙 기반으로 도출한다(R3.3). + pub formality: Formality, + pub profile: StyleProfile, +} + +/// 최연우가 특정 상대방에게 쓰는 말투가 존댓말인지 반말인지 구분한다(R3.3). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum Honorific { + /// 존댓말(예: `-요`, `-습니다`). + Honorific, + /// 반말(예: `-어`, `-야`). + Casual, +} + +/// 말투가 격식체인지 비격식체인지 구분한다(R3.3). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum Formality { + /// 격식체(합쇼체 위주, 이모지·축약이 적음). + Formal, + /// 비격식체(해요체·반말, 이모지·축약이 섞임). + Informal, +} + +/// 종결어미 하나의 분류. 존댓말/격식 파생에만 쓰는 내부 신호다. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum StyleEndingClass { + /// 합쇼체 종결어미(`습니다`/`합니다`/`됩니다`) — 격식·존댓말 신호. + Deferential, + /// 해요체 종결어미(`요`/`죠`) — 존댓말 신호. + Honorific, + /// 웃음·축약 종결(`ㅋㅋㅋ`/`ㅠㅠ` 등) — 비격식 신호. + Contraction, + /// 그 밖의 반말 종결(`어`/`아`/`야`/`지` 등). + Casual, +} + +/// 종결어미 문자열을 존댓말/격식 파생용 분류로 매핑한다. 기존 말투 통계에 +/// 등장하는 종결어미만 다룬다. +pub fn classify_style_ending_class(ending: &str) -> StyleEndingClass { + match ending { + "습니다" | "합니다" | "됩니다" => StyleEndingClass::Deferential, + "요" | "죠" => StyleEndingClass::Honorific, + "ㅋㅋㅋ" | "ㅠㅠ" | "ㅜㅜ" | "ㅠ" | "ㅜ" => StyleEndingClass::Contraction, + _ => StyleEndingClass::Casual, + } +} + +/// 토큰이 웃음·축약 토큰(`ㅋㅋ`, `ㅎㅎ`, `ㅠㅠ`, `ㅜㅜ` 계열)인지 판단한다. +/// 자음/모음 축약만으로 이루어진 토큰을 비격식 신호로 본다. +pub fn is_contraction_token(token: &str) -> bool { + let trimmed = token.trim(); + !trimmed.is_empty() + && trimmed + .chars() + .all(|ch| matches!(ch, 'ㅋ' | 'ㅎ' | 'ㅠ' | 'ㅜ' | 'ㅡ' | 'ㅗ' | 'ㄷ' | 'ㅇ')) +} + +/// 존댓말/격식 파생에 쓰는 규칙 기반 신호 묶음. 종결어미·이모지·구두점·축약 +/// 토큰 통계에서만 도출하며 원문은 담지 않는다(R3.3). +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct StyleSignals { + pub sample_count: usize, + pub honorific_ending_count: usize, + pub deferential_ending_count: usize, + pub casual_ending_count: usize, + pub emoji_count: usize, + pub punctuation_count: usize, + pub contraction_count: usize, +} + +impl StyleSignals { + /// 이미 집계된 말투 프로필의 종결어미·이모지·구두점·축약 통계에서 신호를 + /// 뽑아낸다. `common_endings_json`으로 존댓말/반말/격식 종결을 분류하고, + /// `casual_ending_counts_json`·`common_tokens_json`에서 축약 신호를 더한다. + pub fn from_style_profile(profile: &StyleProfile) -> Self { + let mut signals = StyleSignals { + sample_count: profile.sample_count, + emoji_count: profile.emoji_count, + punctuation_count: profile.punctuation_count, + ..StyleSignals::default() + }; + if let Ok(map) = + serde_json::from_str::>(&profile.common_endings_json) + { + for (ending, count) in map { + match classify_style_ending_class(&ending) { + StyleEndingClass::Deferential => signals.deferential_ending_count += count, + StyleEndingClass::Honorific => signals.honorific_ending_count += count, + StyleEndingClass::Contraction => signals.contraction_count += count, + StyleEndingClass::Casual => signals.casual_ending_count += count, + } + } + } + // 축약/웃음 신호는 별도 통계에서만 추가로 집계한다(반말 종결과 중복 계산 방지). + if let Ok(map) = + serde_json::from_str::>(&profile.casual_ending_counts_json) + { + for (ending, count) in map { + if classify_style_ending_class(&ending) == StyleEndingClass::Contraction { + signals.contraction_count += count; + } + } + } + if let Ok(map) = + serde_json::from_str::>(&profile.common_tokens_json) + { + for (token, count) in map { + if is_contraction_token(&token) { + signals.contraction_count += count; + } + } + } + signals + } +} + +/// 규칙 기반으로 존댓말/반말과 격식/비격식을 도출한다(R3.3). +/// +/// - 존댓말/반말: 존댓말 종결(해요체+합쇼체) 신호가 반말 종결 신호보다 많으면 +/// 존댓말로 본다. +/// - 격식/비격식: 존댓말이면서 합쇼체 종결이 있고, 그 합쇼체 신호가 +/// 비격식 신호(이모지+축약+반말 종결)보다 우세할 때만 격식체로 본다. +/// 나머지는 모두 비격식체다(해요체·반말·이모지 섞인 존댓말 포함). +pub fn derive_honorific_formality(signals: &StyleSignals) -> (Honorific, Formality) { + let honorific_signal = signals.honorific_ending_count + signals.deferential_ending_count; + let casual_signal = signals.casual_ending_count; + let honorific = if honorific_signal > casual_signal { + Honorific::Honorific + } else { + Honorific::Casual + }; + + let informal_signal = + signals.emoji_count + signals.contraction_count + signals.casual_ending_count; + let formal_signal = signals.deferential_ending_count; + let formality = if honorific == Honorific::Honorific + && formal_signal > 0 + && formal_signal >= informal_signal + { + Formality::Formal + } else { + Formality::Informal + }; + + (honorific, formality) +} + +/// 말투 프로필에서 곧바로 존댓말/격식을 도출하는 편의 함수. +pub fn derive_style_profile_honorific_formality(profile: &StyleProfile) -> (Honorific, Formality) { + derive_honorific_formality(&StyleSignals::from_style_profile(profile)) +} + +#[derive(Debug, Clone, Serialize)] +pub struct StyleProfile { + pub chat: String, + pub source: String, + pub user: String, + pub sample_count: usize, + pub average_character_length: f64, + pub median_character_length: f64, + pub p90_character_length: f64, + pub casual_ending_count: usize, + pub casual_ending_counts_json: String, + pub question_count: usize, + pub emoji_count: usize, + pub punctuation_count: usize, + pub common_endings_json: String, + pub common_tokens_json: String, + pub policy_version: String, +} + +pub(super) fn validate_style_profile_laughter(profile: &StyleProfile, label: &str) -> Result<()> { + for encoded in [ + &profile.casual_ending_counts_json, + &profile.common_endings_json, + &profile.common_tokens_json, + ] { + let value: serde_json::Value = + serde_json::from_str(encoded).with_context(|| format!("{label} JSON is invalid"))?; + let values = value + .as_object() + .ok_or_else(|| anyhow::anyhow!("{label} JSON is not an object"))?; + if values + .keys() + .any(|key| crate::reply_policy::validate_auto_reply_laughter(key).is_err()) + { + anyhow::bail!("{label} contains disallowed laughter evidence"); + } + } + Ok(()) +} + +#[derive(Debug, Clone)] +pub(super) struct StyleMessageFeatures { + pub(super) style_eligible: bool, + pub(super) content_kind: &'static str, + character_length: usize, + casual_ending: Option<&'static str>, + question_count: usize, + emoji_count: usize, + punctuation_count: usize, + pub(super) features_json: String, +} + +#[derive(Debug, Default)] +pub(super) struct StyleProfileAccumulator { + lengths: Vec, + casual_endings: BTreeMap, + endings: BTreeMap, + question_count: usize, + emoji_count: usize, + punctuation_count: usize, + tokens: BTreeMap, +} + +impl StyleProfileAccumulator { + pub(super) fn add(&mut self, message: &str, features: &StyleMessageFeatures) { + self.lengths.push(features.character_length); + if let Some(ending) = features.casual_ending { + *self.casual_endings.entry(ending.to_string()).or_default() += 1; + } + if let Some(ending) = common_style_ending(message) { + *self.endings.entry(ending.to_string()).or_default() += 1; + } + self.question_count += features.question_count; + self.emoji_count += features.emoji_count; + self.punctuation_count += features.punctuation_count; + for token in message.split_whitespace().filter_map(normalize_style_token) { + *self.tokens.entry(token).or_default() += 1; + } + } + + pub(super) fn finish(self, chat: &str, source: &str, user: &str) -> Option { + if self.lengths.is_empty() { + return None; + } + let mut lengths = self.lengths; + lengths.sort_unstable(); + let percentile = |ratio: f64| { + let position = (lengths.len() - 1) as f64 * ratio; + let lower = position.floor() as usize; + let upper = position.ceil() as usize; + if lower == upper { + lengths[lower] as f64 + } else { + lengths[lower] as f64 + + (lengths[upper] - lengths[lower]) as f64 * (position - lower as f64) + } + }; + let casual_ending_count = self.casual_endings.values().sum(); + let casual_ending_counts_json = + serde_json::to_string(&top_counts(&self.casual_endings, 12)) + .unwrap_or_else(|_| "{}".into()); + let common_endings_json = + serde_json::to_string(&top_counts(&self.endings, 12)).unwrap_or_else(|_| "{}".into()); + let common_tokens_json = + serde_json::to_string(&top_counts(&self.tokens, 32)).unwrap_or_else(|_| "{}".into()); + Some(StyleProfile { + chat: chat.to_string(), + source: source.to_string(), + user: user.to_string(), + sample_count: lengths.len(), + average_character_length: lengths.iter().sum::() as f64 / lengths.len() as f64, + median_character_length: percentile(0.5), + p90_character_length: percentile(0.9), + casual_ending_count, + casual_ending_counts_json, + question_count: self.question_count, + emoji_count: self.emoji_count, + punctuation_count: self.punctuation_count, + common_endings_json, + common_tokens_json, + policy_version: STYLE_POLICY_VERSION.to_string(), + }) + } +} + +fn top_counts(counts: &BTreeMap, limit: usize) -> BTreeMap { + let mut values = counts.iter().collect::>(); + values.sort_by(|(left_name, left_count), (right_name, right_count)| { + right_count + .cmp(left_count) + .then_with(|| left_name.cmp(right_name)) + }); + values + .into_iter() + .take(limit) + .map(|(name, count)| (name.clone(), *count)) + .collect() +} + +fn normalize_style_token(token: &str) -> Option { + let normalized = token + .trim_matches(|ch: char| { + ch.is_ascii_punctuation() + || matches!( + ch, + '。' | ',' | '!' | '?' | '~' | '…' | '·' | '「' | '」' | '『' | '』' + ) + }) + .trim(); + (!normalized.is_empty()).then(|| normalized.to_string()) +} + +fn trim_style_tail(message: &str) -> &str { + message.trim_end_matches(|ch: char| { + ch.is_whitespace() + || is_emoji(ch) + || ch.is_ascii_punctuation() + || matches!(ch, '。' | '!' | '?' | '~' | '…') + }) +} + +fn style_ending(message: &str) -> Option<&'static str> { + let message = trim_style_tail(message); + [ + "잖아", + "거든", + "같아", + "ㅋㅋㅋ", + "ㅠㅠ", + "ㅜㅜ", + "요", + "죠", + "네", + "지", + "까", + "어", + "아", + "야", + "래", + "ㅠ", + "ㅜ", + ] + .into_iter() + .find(|ending| message.ends_with(ending)) +} + +fn common_style_ending(message: &str) -> Option<&'static str> { + let message = trim_style_tail(message); + [ + "잖아", + "거든", + "같아", + "ㅋㅋㅋ", + "ㅠㅠ", + "ㅜㅜ", + "습니다", + "합니다", + "됩니다", + "요", + "죠", + "네", + "지", + "까", + "어", + "아", + "야", + "래", + "다", + "해", + "함", + "임", + ] + .into_iter() + .find(|ending| message.ends_with(ending)) +} + +fn is_emoji(ch: char) -> bool { + matches!( + ch as u32, + 0x1F000..=0x1FAFF | 0x2600..=0x27BF + ) +} + +fn is_generic_ack_style(message: &str) -> bool { + let remainder: String = message + .chars() + .filter(|ch| { + !ch.is_whitespace() + && !matches!( + *ch, + 'ㅋ' | 'ㅎ' | 'ㄷ' | ',' | '.' | '!' | '?' | '?' | '~' | '~' | ';' + ) + }) + .collect(); + matches!( + remainder.as_str(), + "" | "ㅇㅇ" | "응" | "네" | "맞아" | "ㅇ" | "음" | "어" | "웅" + ) +} + +pub(super) fn is_assistant_tell_style(message: &str) -> bool { + let lower = message.to_ascii_lowercase(); + const TELLS: &[&str] = &[ + "답할 수 있어", + "답할 수 있습니다", + "도와드릴", + "무엇을 도와", + "맥락에 맞게 답", + "i can help", + "as an ai", + "i'm an ai", + "i am an ai", + ]; + // Every tell is either non-ASCII (Hangul, untouched by ASCII lowercasing) + // or already ASCII-lowercase, so `lower.contains(tell)` is exactly the + // original `message.contains(tell) || lower.contains(tell_lower)` test + // without allocating a throwaway String per tell. + TELLS.iter().any(|tell| lower.contains(*tell)) +} + +fn is_spectator_narration_style(message: &str) -> bool { + let compact: String = message.chars().filter(|ch| !ch.is_whitespace()).collect(); + compact.ends_with("알아보나보네") + || compact.ends_with("하나보네") + || compact.ends_with("인가보네") + || compact.ends_with("쪽인가보네") +} + +pub(super) fn classify_style_message(message: &str) -> StyleMessageFeatures { + let normalized = message.trim(); + let char_count = normalized.chars().count(); + let lower = normalized.to_ascii_lowercase(); + let lines = normalized.lines().map(str::trim).collect::>(); + let starts_list = lines.first().is_some_and(|line| { + line.chars() + .next() + .is_some_and(|ch| matches!(ch, '*' | '-' | '•' | '⭐' | '📌' | '#')) + || line.starts_with("Q.") + || line.starts_with("A.") + || line.starts_with('①') + || line.starts_with('②') + || line.starts_with('③') + || (line.chars().next().is_some_and(|ch| ch.is_ascii_digit()) + && line + .chars() + .nth(1) + .is_some_and(|ch| matches!(ch, '.' | ')' | ':'))) + }); + let pasted_markers = [ + "복사", + "붙여넣", + "정보 전달", + "출처", + "요약", + "핵심 내용", + "web발신", + "공지사항", + "속보", + "공유드립니다", + "전달드립니다", + ]; + let formal_prefixes = ["공지", "안내", "알려드립니다", "필독", "[공지]", "[안내]"]; + let formal_suffixes = [ + "습니다", + "니다", + "합니다", + "됩니다", + "바랍니다", + "드립니다", + "하십시오", + ]; + let metadata = normalized.is_empty() + || normalized.starts_with('@') + || matches!(normalized, "사진" | "동영상" | "파일" | "이모티콘") + || (normalized.contains("님이") && normalized.contains("되었습니다")) + || normalized + .chars() + .all(|ch| ch.is_ascii_punctuation() || ch.is_whitespace()) + || normalized.chars().any(|ch| ch.is_ascii_digit()) + || normalized.contains(':'); + let formal_target = trim_style_tail(normalized); + let (style_eligible, content_kind) = + if crate::reply_policy::validate_auto_reply_laughter(normalized).is_err() { + (false, "reply_laughter_policy_excluded") + } else if char_count < 2 { + (false, "metadata_or_noise") + } else if lower.contains("http://") || lower.contains("https://") || lower.contains("www.") + { + (false, "url") + } else if lines.len() > 1 { + (false, "multiline") + } else if starts_list { + (false, "list_or_numbered") + } else if metadata { + (false, "metadata_or_noise") + } else if char_count > 60 + || formal_prefixes + .iter() + .any(|prefix| normalized.starts_with(prefix)) + || formal_suffixes + .iter() + .any(|suffix| formal_target.ends_with(suffix)) + || normalized.contains("하시기 바랍니다") + { + (false, "long_or_formal") + } else if pasted_markers + .iter() + .any(|marker| normalized.contains(marker)) + { + (false, "pasted_information") + } else if is_generic_ack_style(normalized) { + (false, "generic_ack") + } else if is_assistant_tell_style(normalized) { + (false, "assistant_tell") + } else if is_spectator_narration_style(normalized) { + (false, "spectator_narration") + } else { + (true, "ordinary_conversation") + }; + let question_count = normalized + .chars() + .filter(|ch| matches!(ch, '?' | '?')) + .count(); + let emoji_count = normalized.chars().filter(|ch| is_emoji(*ch)).count(); + let punctuation_count = normalized + .chars() + .filter(|ch| { + ch.is_ascii_punctuation() + || matches!( + ch, + '。' | ',' | '!' | '?' | '~' | '…' | '·' | '「' | '」' | '『' | '』' + ) + }) + .count(); + let casual_ending = style_ending(normalized); + let features_json = serde_json::json!({ + "character_length": char_count, + "casual_ending": casual_ending, + "question_count": question_count, + "emoji_count": emoji_count, + "punctuation_count": punctuation_count, + "content_kind": content_kind, + "style_eligible": style_eligible, + "policy_version": STYLE_POLICY_VERSION, + }) + .to_string(); + StyleMessageFeatures { + style_eligible, + content_kind, + character_length: char_count, + casual_ending, + question_count, + emoji_count, + punctuation_count, + features_json, + } +} + +pub(super) fn insert_style_profile( + conn: &Connection, + profile: &StyleProfile, + created_at: &str, +) -> Result<()> { + conn.execute( + "INSERT INTO owner_style_profile( + chat, source, user_name, sample_count, average_character_length, + median_character_length, p90_character_length, casual_ending_count, + casual_ending_counts_json, question_count, emoji_count, punctuation_count, + common_endings_json, common_tokens_json, policy_version, created_at + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16)", + params![ + profile.chat, + profile.source, + profile.user, + profile.sample_count as i64, + profile.average_character_length, + profile.median_character_length, + profile.p90_character_length, + profile.casual_ending_count as i64, + profile.casual_ending_counts_json, + profile.question_count as i64, + profile.emoji_count as i64, + profile.punctuation_count as i64, + profile.common_endings_json, + profile.common_tokens_json, + profile.policy_version, + created_at, + ], + )?; + Ok(()) +} + +pub fn style_profile( + db_path: &Path, + chat: &str, + user: &str, + source: Option<&str>, +) -> Result> { + let conn = open_db_readonly(db_path)?; + ensure_retrieval_index_current(&conn)?; + style_profile_with_connection(&conn, chat, user, source, true) +} + +pub(super) fn style_profile_with_connection( + conn: &Connection, + chat: &str, + user: &str, + source: Option<&str>, + require_policy: bool, +) -> Result> { + ensure_retrieval_index_current(conn)?; + let preferred_source = preferred_context_source(conn, Some(chat), source)?; + let mut stmt = conn.prepare( + "SELECT chat, source, user_name, sample_count, average_character_length, + median_character_length, p90_character_length, casual_ending_count, + casual_ending_counts_json, question_count, emoji_count, punctuation_count, + common_endings_json, common_tokens_json, policy_version + FROM owner_style_profile + WHERE chat = ?1 AND user_name = ?2 AND (?3 IS NULL OR source = ?3) + AND (?4 = 0 OR policy_version = ?5) + ORDER BY sample_count DESC, source ASC + LIMIT 1", + )?; + match stmt.query_row( + params![ + chat, + user, + preferred_source.as_deref(), + require_policy as i64, + STYLE_POLICY_VERSION + ], + |row| { + Ok(StyleProfile { + chat: row.get(0)?, + source: row.get(1)?, + user: row.get(2)?, + sample_count: row.get::<_, i64>(3)? as usize, + average_character_length: row.get(4)?, + median_character_length: row.get(5)?, + p90_character_length: row.get(6)?, + casual_ending_count: row.get::<_, i64>(7)? as usize, + casual_ending_counts_json: row.get(8)?, + question_count: row.get::<_, i64>(9)? as usize, + emoji_count: row.get::<_, i64>(10)? as usize, + punctuation_count: row.get::<_, i64>(11)? as usize, + common_endings_json: row.get(12)?, + common_tokens_json: row.get(13)?, + policy_version: row.get(14)?, + }) + }, + ) { + Ok(profile) => { + validate_style_profile_laughter(&profile, "style profile")?; + Ok(Some(profile)) + } + Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None), + Err(error) => Err(error.into()), + } +} + +pub fn recipient_style_profile( + db_path: &Path, + chat: &str, + recipient: &str, + source: Option<&str>, +) -> Result> { + let conn = open_db_readonly(db_path)?; + recipient_style_profile_with_connection(&conn, chat, recipient, source) +} + +pub(super) fn recipient_style_profile_with_connection( + conn: &Connection, + chat: &str, + recipient: &str, + source: Option<&str>, +) -> Result> { + if chat.trim().is_empty() || recipient.trim().is_empty() { + anyhow::bail!("recipient style chat and recipient must not be empty"); + } + ensure_retrieval_index_current(conn)?; + let preferred_source = preferred_context_source(conn, Some(chat), source)?; + let has_recipient_table = conn.query_row( + "SELECT EXISTS( + SELECT 1 FROM sqlite_master + WHERE type = 'table' AND name = 'owner_recipient_style_profile' + )", + [], + |row| row.get::<_, i64>(0), + )? != 0; + let direct = if has_recipient_table { + let mut stmt = conn.prepare( + "SELECT source, sample_count, confidence_sum, average_character_length, + median_character_length, p90_character_length, casual_ending_count, + casual_ending_counts_json, question_count, emoji_count, + punctuation_count, common_endings_json, common_tokens_json, + policy_version + FROM owner_recipient_style_profile + WHERE chat = ?1 AND recipient = ?2 + AND (?3 IS NULL OR source = ?3) + AND policy_version = ?4 + ORDER BY sample_count DESC, confidence_sum DESC, source ASC + LIMIT 1", + )?; + stmt.query_row( + params![ + chat, + recipient, + preferred_source.as_deref(), + STYLE_POLICY_VERSION + ], + |row| { + let source: String = row.get(0)?; + let sample_count = row.get::<_, i64>(1)? as usize; + let confidence_sum: f64 = row.get(2)?; + Ok(( + sample_count, + confidence_sum, + StyleProfile { + chat: chat.to_string(), + source, + user: STYLE_USER.to_string(), + sample_count, + average_character_length: row.get(3)?, + median_character_length: row.get(4)?, + p90_character_length: row.get(5)?, + casual_ending_count: row.get::<_, i64>(6)? as usize, + casual_ending_counts_json: row.get(7)?, + question_count: row.get::<_, i64>(8)? as usize, + emoji_count: row.get::<_, i64>(9)? as usize, + punctuation_count: row.get::<_, i64>(10)? as usize, + common_endings_json: row.get(11)?, + common_tokens_json: row.get(12)?, + policy_version: row.get(13)?, + }, + )) + }, + ) + .optional()? + } else { + None + }; + if let Some((_, _, profile)) = direct.as_ref() { + validate_style_profile_laughter(profile, "recipient style profile")?; + } + if let Some((sample_count, confidence_sum, profile)) = direct.as_ref() { + if *sample_count >= RECIPIENT_STYLE_MIN_SAMPLES + && confidence_sum.is_finite() + && *confidence_sum >= RECIPIENT_STYLE_MIN_CONFIDENCE + { + let (honorific, formality) = derive_style_profile_honorific_formality(profile); + return Ok(Some(RecipientStyleProfile { + recipient: recipient.to_string(), + direct_sample_count: *sample_count, + confidence_sum: *confidence_sum, + used_fallback: false, + honorific, + formality, + profile: profile.clone(), + })); + } + } + // 최소표본/최소신뢰도 미만이면 방 전체 폴백 프로필을 유지하고, 존댓말/격식은 + // 그 폴백 프로필의 통계에서 그대로 도출한다(R3.3). + let fallback = + style_profile_with_connection(conn, chat, STYLE_USER, preferred_source.as_deref(), true)?; + Ok(fallback.map(|profile| { + let (honorific, formality) = derive_style_profile_honorific_formality(&profile); + RecipientStyleProfile { + recipient: recipient.to_string(), + direct_sample_count: direct.as_ref().map(|value| value.0).unwrap_or(0), + confidence_sum: direct.as_ref().map(|value| value.1).unwrap_or(0.0), + used_fallback: true, + honorific, + formality, + profile, + } + })) +} + +pub fn recipient_style_profile_json( + db_path: &Path, + chat: &str, + recipient: &str, + source: Option<&str>, +) -> Result> { + match recipient_style_profile(db_path, chat, recipient, source)? { + Some(mut profile) => { + profile.profile.source = provenance_id(&profile.profile.source); + Ok(Some(serde_json::to_string(&profile)?)) + } + None => Ok(None), + } +} + +pub fn style_profile_json( + db_path: &Path, + chat: &str, + user: &str, + source: Option<&str>, +) -> Result> { + match style_profile(db_path, chat, user, source)? { + Some(mut profile) => { + profile.source = provenance_id(&profile.source); + Ok(Some(serde_json::to_string(&profile)?)) + } + None => Ok(None), + } +} diff --git a/src/context/tests.rs b/src/context/tests.rs index fed4ef60..bf9f8941 100644 --- a/src/context/tests.rs +++ b/src/context/tests.rs @@ -1,7 +1,47 @@ +use super::embedding::{encode_vector, vector_to_bytes}; +use super::live::{live_event_digest, live_index_text}; +use super::reply_bundle::{ + live_event_exclusions, CONTEXT_REPLY_BUNDLE_CONTEXT_LIMIT, CONTEXT_REPLY_BUNDLE_DECISION_LIMIT, + CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION, CONTEXT_REPLY_BUNDLE_STYLE_LIMIT, + RECIPIENT_CONTEXT_REPLY_BUNDLE_SCHEMA_VERSION, +}; +use super::reply_decision::REPLY_DECISION_CANDIDATE_CAP; +use super::response_time::{ + fit_response_time_distribution, response_delay_percentile, response_time_distribution_is_valid, + summarize_response_delays, MAX_RESPONSE_DELAY_SECONDS, MIN_SCHEDULED_RESPONSE_DELAY_SECONDS, + RESPONSE_TIME_DISTRIBUTION_COMPONENTS, RESPONSE_TIME_DISTRIBUTION_FIT_TRANSFORM, + RESPONSE_TIME_DISTRIBUTION_MIN_COMPONENT_SAMPLES, RESPONSE_TIME_DISTRIBUTION_MIN_SAMPLES, + RESPONSE_TIME_DISTRIBUTION_MODEL_KIND, RESPONSE_TIME_DISTRIBUTION_POLICY_VERSION, + RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION, +}; +use super::schema::{ + rebuild_retrieval_index, CONTEXT_RETRIEVAL_MIGRATION_REQUIRED, LIVE_CONTEXT_SCHEMA_VERSION, +}; +use super::search::{ + style_search_with_connection, CONTEXT_VECTOR_CANDIDATE_CAP, RECIPIENT_STYLE_SEARCH_SQL, + STYLE_VECTOR_CANDIDATE_CAP, +}; +use super::self_send::AUTO_GENERATED_MATCH_WINDOW_SECONDS; +use super::store::{ + checkpoint_context_wal_truncate, open_db, open_db_readonly, set_context_db_permissions, + sqlite_sidecar_path, +}; +use super::style::{classify_style_message, is_assistant_tell_style, STYLE_USER}; +use super::topics::{classify_message_topics, message_has_interest_topic, TOPIC_LEXICON}; use super::*; +use anyhow::Result; +use chrono::Utc; +use rusqlite::{params, Connection}; +use std::collections::BTreeMap; +use std::fs; use std::io::Write; +use std::path::{Path, PathBuf}; use tempfile::tempdir; +fn is_conversational_style_message(message: &str) -> bool { + classify_style_message(message).style_eligible +} + const TEST_ACCOUNT_FINGERPRINT: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; diff --git a/src/context/topics.rs b/src/context/topics.rs new file mode 100644 index 00000000..8366d6f1 --- /dev/null +++ b/src/context/topics.rs @@ -0,0 +1,306 @@ +//! The topic lexicon and the `context_message_topics` rows it produces. + +use anyhow::Result; +use rusqlite::{params, Connection, OptionalExtension}; + +pub(super) const TOPIC_LEXICON: &[(&str, &[&str])] = &[ + ( + "contact", + &[ + "번호", + "연락처", + "전화번호", + "폰번호", + "핸드폰", + "휴대폰", + "전화", + ], + ), + ( + "ax_macos", + &[ + "ax api", + "axapi", + "손쉬운 사용", + "accessibility", + "axui", + "axtextarea", + "axshowmenu", + ], + ), + ( + "computer_use", + &[ + "컴퓨터 유즈", + "computer use", + "computer-use", + "스크린샷", + "픽셀", + "마우스", + ], + ), + ( + "llm_tools", + &[ + "토큰", + "cursor", + "grok", + "gemini", + "claude", + "chatgpt", + "가재코드", + "모델", + ], + ), + ( + "kakao_auto", + &["자동답", "local-send", "reply-to", "카톡 답", "워커"], + ), + ( + "business", + &[ + "사업자", + "지원", + "세금", + "신청서", + "사업", + "창업", + "매출", + "영업", + "법인", + "스타트업", + ], + ), + ("infra", &["리눅스", "linux", "가상머신"]), + ("news", &["긱뉴스", "geeknews", "hada.io"]), + ("identity", &["나임", "연우지", "사람이지"]), + ( + "stocks", + &[ + "주식", + "증권", + "코스피", + "코스닥", + "나스닥", + "다우", + "배당", + "상장", + "공모주", + "etf", + "양도세", + "매수", + "매도", + "주가", + ], + ), + ( + "coins", + &[ + "코인", + "비트코인", + "이더리움", + "알트", + "업비트", + "빗썸", + "바이낸스", + "김치프리미엄", + "김프", + "btc", + "eth", + "blockchain", + "블록체인", + ], + ), + ( + "investing", + &[ + "투자", + "수익률", + "포트폴리오", + "자산배분", + "적립", + "펀드", + "재테크", + "시드", + ], + ), + ( + "real_estate", + &[ + "부동산", + "아파트", + "전세", + "월세", + "매매", + "청약", + "분양", + "등기", + "전세사기", + "집값", + ], + ), + ("auction", &["경매", "공매", "낙찰", "입찰", "경매물건"]), + ( + "ai", + &[ + "인공지능", + "챗gpt", + "chatgpt", + "지피티", + "llm", + "그록", + "grok", + "클로드", + "claude", + "제미니", + "gemini", + "머신러닝", + "딥러닝", + "openai", + "anthropic", + ], + ), +]; + +const INTEREST_TOPICS: &[&str] = &[ + "stocks", + "coins", + "investing", + "real_estate", + "auction", + "business", + "ai", +]; + +const TOPIC_LEXICON_VERSION: &str = "2"; + +pub(super) fn classify_message_topics(message: &str) -> Vec<&'static str> { + let text = message.trim(); + if text.is_empty() { + return Vec::new(); + } + let lower = text.to_ascii_lowercase(); + let mut topics = Vec::new(); + for (topic, needles) in TOPIC_LEXICON { + let matched = needles.iter().any(|needle| { + if needle.bytes().all(|byte| byte.is_ascii()) { + // See `topic_lexicon_ascii_needles_are_lowercase`: every ASCII + // needle is already lowercase, so matching the precomputed + // lowercase haystack avoids one String per needle per message. + lower.contains(*needle) + } else { + text.contains(needle) + } + }); + if matched { + topics.push(*topic); + } + } + topics +} + +pub(super) fn message_has_interest_topic(message: &str) -> bool { + classify_message_topics(message) + .iter() + .any(|topic| INTEREST_TOPICS.contains(topic)) +} + +pub(super) fn ensure_topic_lexicon(tx: &rusqlite::Transaction<'_>) -> Result<()> { + let current: Option = tx + .query_row( + "SELECT value FROM context_retrieval_meta WHERE key = 'topic_lexicon_version'", + [], + |row| row.get(0), + ) + .optional()?; + if current.as_deref() == Some(TOPIC_LEXICON_VERSION) { + return Ok(()); + } + backfill_message_topics(tx)?; + tx.execute( + "INSERT INTO context_retrieval_meta(key, value) + VALUES ('topic_lexicon_version', ?1) + ON CONFLICT(key) DO UPDATE SET value = excluded.value", + [TOPIC_LEXICON_VERSION], + )?; + Ok(()) +} + +pub(super) fn insert_message_topics( + conn: &Connection, + message_id: i64, + chat: &str, + source: &str, + date: &str, + message: &str, +) -> Result<()> { + if message_id <= 0 { + return Ok(()); + } + for topic in classify_message_topics(message) { + let added = conn.execute( + "INSERT OR IGNORE INTO context_message_topics(message_id, topic) + VALUES (?1, ?2)", + params![message_id, topic], + )?; + if added == 0 { + continue; + } + conn.execute( + "INSERT INTO context_topic_stats( + chat, source, topic, message_count, last_date + ) VALUES (?1, ?2, ?3, 1, ?4) + ON CONFLICT(chat, source, topic) DO UPDATE SET + message_count = message_count + 1, + last_date = CASE + WHEN excluded.last_date > last_date THEN excluded.last_date + ELSE last_date + END", + params![chat, source, topic, date], + )?; + } + Ok(()) +} + +pub(super) fn backfill_message_topics(conn: &Connection) -> Result<()> { + let mut last_id = 0_i64; + loop { + let mut stmt = conn.prepare( + "SELECT id, chat, source, date, message + FROM context_messages + WHERE id > ?1 + ORDER BY id ASC + LIMIT 1000", + )?; + let batch = stmt + .query_map([last_id], |row| { + Ok(( + row.get::<_, i64>(0)?, + row.get::<_, String>(1)?, + row.get::<_, String>(2)?, + row.get::<_, String>(3)?, + row.get::<_, String>(4)?, + )) + })? + .collect::>>()?; + drop(stmt); + if batch.is_empty() { + break; + } + for (id, chat, source, date, message) in batch { + last_id = id; + insert_message_topics(conn, id, &chat, &source, &date, &message)?; + } + } + Ok(()) +} + +pub(super) fn context_topic_tables_ready(conn: &Connection) -> Result { + let count: i64 = conn.query_row( + "SELECT COUNT(*) FROM sqlite_master + WHERE type = 'table' AND name = 'context_message_topics'", + [], + |row| row.get(0), + )?; + Ok(count == 1) +} From 01b0bdf68887d964f6fb2c533a49332880b5be17 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:35:57 +0900 Subject: [PATCH 27/67] fix(supervisor): stop every started child when the supervisor fails main() had no handler around the child start sequence or the main loop. An exception there (OSError ENOSPC from write_status, EAGAIN from start()) escaped main() and left apple-watch, db-watch and the reply worker running until something outside the supervisor swept its process group. Wrap the body in main(): on any Exception, print the traceback as before and run stop(reason="supervisor_error", exit_code=1), which terminates, kills and reaps every started child and publishes the stop. SystemExit and KeyboardInterrupt are not Exception and pass through unchanged. Before: both cleanup tests failed with the started FakeChild entries still running. After: every started child is terminated and main() exits with status 1. Co-Authored-By: Claude Opus 5.5 --- scripts/auto-reply-supervisor.py | 9 +++ tests/test_auto_reply_supervisor_lifecycle.py | 75 +++++++++++++++++-- 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/scripts/auto-reply-supervisor.py b/scripts/auto-reply-supervisor.py index 0a2688f8..cc8465e2 100644 --- a/scripts/auto-reply-supervisor.py +++ b/scripts/auto-reply-supervisor.py @@ -14,6 +14,7 @@ import subprocess import time import tomllib +import traceback import uuid from contextlib import contextmanager from datetime import datetime, timezone @@ -1500,6 +1501,14 @@ def stop(*_args: object, reason: str = "shutdown", exit_code: int = 0) -> None: def main() -> int: + try: + return _supervise() + except Exception: + traceback.print_exc() + stop(reason="supervisor_error", exit_code=1) + + +def _supervise() -> int: global _status_context, LOG_DIR, SELF, CHAT parser = argparse.ArgumentParser() parser.add_argument("--once", action="store_true") diff --git a/tests/test_auto_reply_supervisor_lifecycle.py b/tests/test_auto_reply_supervisor_lifecycle.py index df1dc7aa..f7f575b4 100644 --- a/tests/test_auto_reply_supervisor_lifecycle.py +++ b/tests/test_auto_reply_supervisor_lifecycle.py @@ -1,4 +1,7 @@ +import contextlib +import errno import importlib.util +import io import itertools import json import os @@ -38,6 +41,10 @@ def __init__(self, argv, env): self.stdout = None self.terminated = False + def __repr__(self): + scripts = [Path(arg).name for arg in self.argv if str(arg).endswith(".py")] + return f"FakeChild({' '.join(scripts)})" + def poll(self): return self.returncode @@ -56,7 +63,7 @@ def wait(self, timeout=None): class SupervisorMainHarness: """Run the real supervisor main() against fake children and a fake DB probe.""" - def __init__(self, test, root, *, db_ready, env=None, ticks=3, sleep_hook=None): + def __init__(self, test, root, *, db_ready, env=None, ticks=3, spawn_error=None): self.test = test self.state_root = root / "home" / "state" / "rooms" / "42" self.module = load_supervisor() @@ -64,18 +71,19 @@ def __init__(self, test, root, *, db_ready, env=None, ticks=3, sleep_hook=None): self.db_ready = db_ready self.env = env or {} self.ticks = ticks - self.sleep_hook = sleep_hook + self.spawn_error = spawn_error self.sleeps = 0 + self.patches = {} def _popen(self, argv, **kwargs): + if self.spawn_error is not None and self.spawn_error in " ".join(argv): + raise OSError(errno.EAGAIN, "Resource temporarily unavailable") child = FakeChild(argv, kwargs.get("env")) self.spawned.append(child) return child def _sleep(self, _seconds): self.sleeps += 1 - if self.sleep_hook is not None: - self.sleep_hook(self) if self.sleeps >= self.ticks: self.module._signal_requested = signal.SIGTERM @@ -91,6 +99,11 @@ def status(self): return json.loads((self.state_root / "supervisor-status.json").read_text("utf-8")) def run(self): + raised = self.run_capturing() + self.test.assertIsInstance(raised, SystemExit) + return raised.code + + def run_capturing(self): module = self.module config = { "safety": {"allow_auto_reply": True}, @@ -124,10 +137,14 @@ def run(self): module.signal, "signal" ), mock.patch.object( module.time, "sleep", side_effect=self._sleep - ): - with self.test.assertRaises(SystemExit) as raised: + ), contextlib.ExitStack() as stack: + for name, value in self.patches.items(): + stack.enter_context(mock.patch.object(module, name, value)) + try: module.main() - return raised.exception.code + except BaseException as exc: + return exc + self.test.fail("supervisor main() returned instead of exiting") class GeekNewsOnlyRoomTests(unittest.TestCase): @@ -190,5 +207,49 @@ def test_reply_room_starts_db_watch_once_the_db_becomes_ready(self): self.assertTrue(harness.status()["database_started"]) +class SupervisorFailureCleanupTests(unittest.TestCase): + def test_status_write_failure_in_the_loop_stops_every_child(self): + with tempfile.TemporaryDirectory() as raw: + harness = SupervisorMainHarness(self, Path(raw), db_ready=lambda: True) + real_write_status = harness.module.write_status + + def disk_full_after_first_tick(*args, **kwargs): + if harness.sleeps >= 1: + raise OSError(errno.ENOSPC, "No space left on device") + return real_write_status(*args, **kwargs) + + harness.patches["write_status"] = disk_full_after_first_tick + with contextlib.redirect_stderr(io.StringIO()): + raised = harness.run_capturing() + + self.assertEqual(len(harness.spawned), 3) + self.assertEqual( + [child for child in harness.spawned if not child.terminated], [] + ) + self.assertIsInstance(raised, SystemExit) + self.assertEqual(raised.code, 1) + + def test_failed_worker_start_stops_the_children_already_started(self): + with tempfile.TemporaryDirectory() as raw: + harness = SupervisorMainHarness( + self, + Path(raw), + db_ready=lambda: True, + spawn_error="auto-reply-worker.py", + ) + with contextlib.redirect_stderr(io.StringIO()): + raised = harness.run_capturing() + + self.assertEqual( + harness.scripts_started(), + ["auto-reply-apple-watch.py", "auto-reply-db-watch.py"], + ) + self.assertEqual( + [child for child in harness.spawned if not child.terminated], [] + ) + self.assertIsInstance(raised, SystemExit) + self.assertEqual(raised.code, 1) + + if __name__ == "__main__": unittest.main() From 49e33cb0eb86264c1bc6df87defe8b246620347c Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:37:07 +0900 Subject: [PATCH 28/67] refactor(context): move ensure_live_context_schema into store It takes a database path and only opens it, so it sits with the other path-level entry points. schema no longer imports store, which leaves every schema function working on a Connection and removes the only store/schema import cycle. The public path is unchanged. Co-Authored-By: Claude Opus 5.5 --- src/context/mod.rs | 3 +-- src/context/schema.rs | 9 --------- src/context/store.rs | 7 +++++++ 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index 2c648b9b..cf5f9176 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -59,14 +59,13 @@ pub use reply_decision::{ pub use response_time::{ response_time_stats, ResponseTimeComponent, ResponseTimeDistribution, ResponseTimeStats, }; -pub use schema::ensure_live_context_schema; pub use search::{search, search_with_embedder, style_search, ContextResult}; pub use self_send::{ classify_auto_generated_self_events, confirmed_self_sends_from_queue, repair_bot_sent_style_samples, AutoGeneratedSelfEventClassification, ConfirmedSelfSend, OutgoingSelfEvent, SelfSendRepairMatch, SelfSendRepairReport, SELF_SEND_REPAIR_WINDOW_SECONDS, }; -pub use store::default_db_path; +pub use store::{default_db_path, ensure_live_context_schema}; pub use style::{ classify_style_ending_class, derive_honorific_formality, derive_style_profile_honorific_formality, is_contraction_token, recipient_style_profile, diff --git a/src/context/schema.rs b/src/context/schema.rs index e4c86324..4b5094f7 100644 --- a/src/context/schema.rs +++ b/src/context/schema.rs @@ -7,7 +7,6 @@ use super::response_time::{ fit_response_time_distribution, response_time_distribution_is_valid, ResponseTimeDistribution, RESPONSE_TIME_DISTRIBUTION_SCHEMA_VERSION, }; -use super::store::open_db; use super::style::{ classify_style_message, insert_style_profile, StyleProfileAccumulator, STYLE_POLICY_VERSION, STYLE_USER, @@ -18,7 +17,6 @@ use chrono::Utc; use rusqlite::{params, Connection, OptionalExtension}; use std::cmp::Ordering; use std::collections::BTreeMap; -use std::path::Path; pub(super) const RETRIEVAL_INDEX_SCHEMA_VERSION: &str = "2"; @@ -27,13 +25,6 @@ pub(super) const CONTEXT_RETRIEVAL_MIGRATION_REQUIRED: &str = pub(super) const LIVE_CONTEXT_SCHEMA_VERSION: &str = "2"; -/// Apply the additive context/FTS/live-index migrations without ingesting or -/// deleting any context rows. Safe to call before a first sync state lookup. -pub fn ensure_live_context_schema(db_path: &Path) -> Result<()> { - let _ = open_db(db_path)?; - Ok(()) -} - /// Rename legacy `choi_yeonwoo_*` style tables to their `owner_*` names when an /// older database is opened, so its data survives the owner-scope /// generalization (R10.6). Only renames when the legacy table exists and the diff --git a/src/context/store.rs b/src/context/store.rs index cc2f67fc..756e1d13 100644 --- a/src/context/store.rs +++ b/src/context/store.rs @@ -17,6 +17,13 @@ pub fn default_db_path() -> PathBuf { base.join("openkakao").join("context.sqlite3") } +/// Apply the additive context/FTS/live-index migrations without ingesting or +/// deleting any context rows. Safe to call before a first sync state lookup. +pub fn ensure_live_context_schema(db_path: &Path) -> Result<()> { + let _ = open_db(db_path)?; + Ok(()) +} + /// How long a context-DB connection waits for a competing writer before /// failing with SQLITE_BUSY. /// From 7b693bade0f46b8c0c340b297db8b301f4ad0a8e Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:37:44 +0900 Subject: [PATCH 29/67] fix: do not refuse voice starts for processes that exited mid-probe The voice start probe lists same-user Python processes with ps, then reads each argv. A process that exits between the two calls makes macOS answer EINVAL, not ESRCH, so the probe failed closed with voice_session_process_check_failed. Treat a PID that is gone or a zombie as exited; a live process whose argv cannot be read still blocks the start. Found while gating the bridge fixes: the voice reap test intermittently failed under concurrent test processes. Co-Authored-By: GPT-6 Astra via opencodex --- desktop/src-tauri/src/python_bridge.rs | 53 +++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/desktop/src-tauri/src/python_bridge.rs b/desktop/src-tauri/src/python_bridge.rs index c3d84f63..ed6f0fda 100644 --- a/desktop/src-tauri/src/python_bridge.rs +++ b/desktop/src-tauri/src/python_bridge.rs @@ -1751,8 +1751,10 @@ fn read_voice_process_args(pid: i32) -> Result, BridgeE ) }; if result != 0 { - return if std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH) { - Ok(None) // Exited since ps; permission/parse failures still block spawn. + // Exited since ps (a zombie or a reaped PID) is not a listener. + // Anything still alive that we cannot read keeps blocking the spawn. + return if process_has_exited(pid) { + Ok(None) } else { Err(BridgeError::VoiceProcessCheck) }; @@ -1761,6 +1763,32 @@ fn read_voice_process_args(pid: i32) -> Result, BridgeE parse_voice_process_args(&bytes).map(Some) } +/// True only when the PID is gone or is a zombie. Live processes, including +/// ones whose argv cannot be read, return false so they keep blocking a start. +#[cfg(target_os = "macos")] +fn process_has_exited(pid: i32) -> bool { + let mut info: libc::proc_bsdshortinfo = unsafe { std::mem::zeroed() }; + let size = std::mem::size_of::(); + let Ok(size_arg) = libc::c_int::try_from(size) else { + return false; + }; + // SAFETY: info is writable for exactly size bytes; this is a read-only + // query that sends no signal. + let written = unsafe { + libc::proc_pidinfo( + pid, + libc::PROC_PIDT_SHORTBSDINFO, + 0, + (&mut info as *mut libc::proc_bsdshortinfo).cast(), + size_arg, + ) + }; + if written == size_arg { + return info.pbsi_status == libc::SZOMB; + } + written <= 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH) +} + #[cfg(not(target_os = "macos"))] fn read_voice_process_args(_pid: i32) -> Result, BridgeError> { Err(BridgeError::VoiceProcessCheck) @@ -5391,6 +5419,27 @@ mod tests { assert_eq!(still_listed, Some(None), "running swap job was removed or relabelled"); } + #[cfg(target_os = "macos")] + #[test] + fn exited_candidates_are_not_read_as_a_failed_voice_check() { + // ps can list a same-user Python process that exits before its argv is + // read. macOS answers EINVAL for a zombie and for a PID that is gone, so + // the probe used to refuse every voice start in that window. + let mut child = Command::new("sh") + .args(["-c", "exit 0"]) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .spawn() + .unwrap(); + let pid = i32::try_from(child.id()).unwrap(); + thread::sleep(Duration::from_millis(200)); + let zombie = read_voice_process_args(pid); + child.wait().unwrap(); + let reaped = read_voice_process_args(pid); + assert!(matches!(zombie, Ok(None)), "zombie refused: {zombie:?}"); + assert!(matches!(reaped, Ok(None)), "reaped pid refused: {reaped:?}"); + } + #[test] fn app_exit_stops_children_without_latching_future_jobs() { // Quitting mid-job must not orphan the child, and must not write the From f092ebf9127a9326c7a79272a16c4544930b959b Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:38:15 +0900 Subject: [PATCH 30/67] chore: rustfmt the desktop bridge process-control changes Co-Authored-By: GPT-6 Astra via opencodex --- desktop/src-tauri/src/python_bridge.rs | 29 +++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/desktop/src-tauri/src/python_bridge.rs b/desktop/src-tauri/src/python_bridge.rs index ed6f0fda..566a7d36 100644 --- a/desktop/src-tauri/src/python_bridge.rs +++ b/desktop/src-tauri/src/python_bridge.rs @@ -5168,8 +5168,16 @@ mod tests { for relative in resource_layout::DATA_FILES { write_fixture_file(&root.join(relative), b"fixture only", false); } - write_fixture_file(&root.join(resource_layout::CLI), b"#!/bin/sh\nexit 0\n", true); - write_fixture_file(&root.join(resource_layout::MENUBAR_SCRIPT), script.as_bytes(), false); + write_fixture_file( + &root.join(resource_layout::CLI), + b"#!/bin/sh\nexit 0\n", + true, + ); + write_fixture_file( + &root.join(resource_layout::MENUBAR_SCRIPT), + script.as_bytes(), + false, + ); let mut bridge = PythonBridge::new(); let config = Arc::get_mut(&mut bridge.config).unwrap(); config.python = PathBuf::from("python3"); @@ -5393,7 +5401,11 @@ mod tests { fs::remove_dir_all(root).unwrap(); assert!(matches!(result, Err(BridgeError::ActionNotAllowed))); - assert_eq!(still_listed, Some(None), "running browser job was removed or relabelled"); + assert_eq!( + still_listed, + Some(None), + "running browser job was removed or relabelled" + ); } #[test] @@ -5416,7 +5428,11 @@ mod tests { fs::remove_dir_all(root).unwrap(); assert!(matches!(result, Err(BridgeError::ActionNotAllowed))); - assert_eq!(still_listed, Some(None), "running swap job was removed or relabelled"); + assert_eq!( + still_listed, + Some(None), + "running swap job was removed or relabelled" + ); } #[cfg(target_os = "macos")] @@ -5489,7 +5505,10 @@ mod tests { let other_untouched = fs::read(&in_flight).unwrap(); fs::remove_dir_all(&temp).unwrap(); - assert!(result.is_ok(), "a second writer collided with an in-flight temp file"); + assert!( + result.is_ok(), + "a second writer collided with an in-flight temp file" + ); assert_eq!(written, Some(json!(true))); assert_eq!(other_untouched, b"other writer"); } From 12f46fc5001f867edee2ae5920c8c6d3f6db96f0 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:39:28 +0900 Subject: [PATCH 31/67] fix(service): write preflight-skipped.json atomically The aggregate catalog-preflight failure artifact was written with Path.write_text, which truncates the existing file and then writes in place. A write that fails halfway (ENOSPC, EFBIG) left a torn JSON file and destroyed the previous artifact; the OSError was then ignored. Write it through the existing _write_receipt helper (unique temp file, fsync, os.replace, directory fsync, mode 0600) so a failed write leaves the previous artifact untouched. Before: with RLIMIT_FSIZE=64 in a child process the artifact was replaced by 64 torn bytes of the new JSON. After: the previous artifact survives. Co-Authored-By: Claude Opus 5.5 --- scripts/auto-reply-service.py | 5 +-- tests/test_auto_reply_service_entry.py | 50 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/scripts/auto-reply-service.py b/scripts/auto-reply-service.py index cfce13a4..6d6f22d0 100755 --- a/scripts/auto-reply-service.py +++ b/scripts/auto-reply-service.py @@ -653,10 +653,7 @@ def _perform_preflight( "reason": str(exc), } try: - (state_root / "preflight-skipped.json").write_text( - json.dumps(aggregate_failure, ensure_ascii=False, indent=2), - encoding="utf-8", - ) + _write_receipt(state_root / "preflight-skipped.json", aggregate_failure) except OSError: pass raise SystemExit(str(exc)) from None diff --git a/tests/test_auto_reply_service_entry.py b/tests/test_auto_reply_service_entry.py index ec4746e6..35312412 100644 --- a/tests/test_auto_reply_service_entry.py +++ b/tests/test_auto_reply_service_entry.py @@ -233,6 +233,56 @@ def fake_preflight(_binary, _config, selectors, **kwargs): self.assertIn("whole set rejected attempt 1", artifact["reason"]) self.assertIn("whole set rejected attempt 2", artifact["reason"]) + def test_failed_aggregate_artifact_write_keeps_the_previous_artifact(self): + # RLIMIT_FSIZE turns any write past 64 bytes into EFBIG, which is how + # a full disk interrupts the artifact write halfway through. + child = """\ +import importlib.util +import resource +import sys +from pathlib import Path +from unittest import mock + +entry, state_root = sys.argv[1], Path(sys.argv[2]) +spec = importlib.util.spec_from_file_location("service_artifact_child", entry) +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) +resource.setrlimit(resource.RLIMIT_FSIZE, (64, 64)) +with mock.patch.object( + module, "_catalog_selectors", return_value=["id:1", "id:2", "id:3"] +), mock.patch.object( + module, "_preflight_cli", return_value=(False, {}, "rejected " * 40) +): + try: + module._perform_preflight( + Path("/tmp/python"), + Path("/tmp/entry.py"), + Path("/tmp/openkakao-cli"), + Path("/tmp/config.toml"), + None, + state_root, + invalidate=False, + ) + except SystemExit as exc: + print("preflight fenced:", str(exc)[:16]) +""" + previous = json.dumps({"scope": "aggregate", "selectors": [], "reason": "old"}) + with tempfile.TemporaryDirectory() as raw: + state_root = Path(raw) + artifact = state_root / "preflight-skipped.json" + artifact.write_text(previous, encoding="utf-8") + result = subprocess.run( + [sys.executable, "-I", "-B", "-c", child, str(ENTRY), str(state_root)], + stdin=subprocess.DEVNULL, + capture_output=True, + text=True, + timeout=30, + check=False, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("preflight fenced:", result.stdout) + self.assertEqual(artifact.read_text(encoding="utf-8"), previous) + def test_catalog_preflight_requires_full_set_capable_first_call(self): module = load_entry("auto_reply_catalog_full_set_only_test") candidates = ["id:1", "id:2", "id:3"] From e2d37e0746d9a23ffa9f61b7258e8232844bef9e Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:41:06 +0900 Subject: [PATCH 32/67] refactor(desktop): delete dead exports, CSS rules and an unused boot dependency Nothing in the repository references these, so they only cost a reader time: the .status-dot, .model-id, .meta-muted and .slot-grid rules left from an older settings markup, SETTINGS_IDS, PIPELINE_STAGE_IDS, the AnimationLoop.handleLifecycle and isRunning methods, the hologram's currentView getter, main.ts's re-export of the poller, and the panel boot's invokeCommand dependency that bootPanel never read. LifecycleState moves to lifecycle.ts, next to the only class that consumes it. renderRooms stops guarding a list the type already guarantees. Co-Authored-By: Claude Fable 5.2 --- desktop/src/core/animation-loop.ts | 11 ----------- desktop/src/core/lifecycle-wiring.ts | 2 +- desktop/src/core/lifecycle.ts | 2 +- desktop/src/core/load-mapping.ts | 11 ----------- desktop/src/knowledge/hologram.ts | 4 ---- desktop/src/main.ts | 6 ------ desktop/src/styles.css | 11 ----------- desktop/src/ui.ts | 18 +----------------- 8 files changed, 3 insertions(+), 62 deletions(-) diff --git a/desktop/src/core/animation-loop.ts b/desktop/src/core/animation-loop.ts index 08725da2..b31d37c0 100644 --- a/desktop/src/core/animation-loop.ts +++ b/desktop/src/core/animation-loop.ts @@ -1,5 +1,3 @@ -export type LifecycleState = "visible" | "hidden" | "closed" | "locked"; - export interface FrameScheduler { request(callback: FrameRequestCallback): number; cancel(id: number): void; @@ -47,15 +45,6 @@ export class AnimationLoop { } } - handleLifecycle(state: LifecycleState): void { - if (state === "visible") this.start(); - else this.stop(); - } - - isRunning(): boolean { - return this.running; - } - private frameIntervalMs(): number { return this.busyLoad > 0.08 ? 1000 / 30 : 1000 / 15; } diff --git a/desktop/src/core/lifecycle-wiring.ts b/desktop/src/core/lifecycle-wiring.ts index f62ebee7..35192a9e 100644 --- a/desktop/src/core/lifecycle-wiring.ts +++ b/desktop/src/core/lifecycle-wiring.ts @@ -1,6 +1,6 @@ import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; -import type { LifecycleState } from "./animation-loop"; +import type { LifecycleState } from "./lifecycle"; /** Event the Rust shell emits when it shows or hides a panel window. */ export const VISIBILITY_EVENT = "jarvis://visibility"; diff --git a/desktop/src/core/lifecycle.ts b/desktop/src/core/lifecycle.ts index a4bf80c7..acc76739 100644 --- a/desktop/src/core/lifecycle.ts +++ b/desktop/src/core/lifecycle.ts @@ -1,4 +1,4 @@ -import type { LifecycleState } from "./animation-loop"; +export type LifecycleState = "visible" | "hidden" | "closed" | "locked"; export interface RenderLoopControl { start(): void; diff --git a/desktop/src/core/load-mapping.ts b/desktop/src/core/load-mapping.ts index a864e866..7de0f5fe 100644 --- a/desktop/src/core/load-mapping.ts +++ b/desktop/src/core/load-mapping.ts @@ -2,17 +2,6 @@ import type { BackgroundStatus, JobEvent, PipelineStatus } from "../contracts"; const TOTAL_BOOST = 0.35; -export const PIPELINE_STAGE_IDS = [ - "detect", - "authorize", - "queue", - "context", - "model", - "delay", - "send", - "confirm", -] as const; - export interface SourceLoads { reply: number; geeknews: number; diff --git a/desktop/src/knowledge/hologram.ts b/desktop/src/knowledge/hologram.ts index 3b106fbc..c25af53e 100644 --- a/desktop/src/knowledge/hologram.ts +++ b/desktop/src/knowledge/hologram.ts @@ -100,10 +100,6 @@ export class KnowledgeHologram { return this.loop.renderCount; } - get currentView(): KnowledgeView { - return this.view; - } - clickNode(nodeId: string): KnowledgeView { const node = this.graph.nodes.find((candidate) => candidate.id === nodeId); if (!node) return this.view; diff --git a/desktop/src/main.ts b/desktop/src/main.ts index 9874f85d..61aeb4dd 100644 --- a/desktop/src/main.ts +++ b/desktop/src/main.ts @@ -44,8 +44,6 @@ import { mainPanelMarkup, renderBackground, renderHistory, renderRooms, settings import { RESIDENT_MODEL_ID, SWAP_MODEL_ID } from "./tokens"; import { wireVoiceStart } from "./voice-controls"; -export { RuntimeSnapshotPoller, type PollTimerScheduler } from "./runtime-poller"; - const app = document.querySelector("#app")!; if (!app) throw new Error("app_root_missing"); @@ -491,15 +489,12 @@ export async function bootSettings( } } -type CommandInvoker = (command: string) => Promise; - interface PanelBootDependencies { createCore: (canvas: HTMLCanvasElement) => JarvisCoreControl | Promise; loadSnapshot: SnapshotLoader; cancelSnapshot: SnapshotCanceller; makeToken: () => CancellationToken; pollScheduler: PollTimerScheduler; - invokeCommand: CommandInvoker; subscribeVisibility: VisibilitySubscriber | null; /** Boot handshake: the shell's current visibility decides the first state. */ readVisibility: VisibilityReader | null; @@ -517,7 +512,6 @@ export async function bootPanel( cancelSnapshot: cancelRuntimeRequest, makeToken: createCancellationToken, pollScheduler: browserPollScheduler, - invokeCommand: invoke, subscribeVisibility: tauriVisibilitySubscriber, readVisibility: tauriVisibilityReader, ...overrides, diff --git a/desktop/src/styles.css b/desktop/src/styles.css index f0db93d5..8bcd95a3 100644 --- a/desktop/src/styles.css +++ b/desktop/src/styles.css @@ -79,7 +79,6 @@ body.settings-view { min-width: 0; min-height: 700px; overflow-y: auto; line-hei .section-heading { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; } .tag { flex: none; max-width: 100%; border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border)); border-radius: 999px; padding: 3px 7px; color: var(--accent-ink); font-size: 10px; line-height: 1.4; font-weight: 650; overflow-wrap: anywhere; } .muted-tag { color: var(--muted); border-color: var(--border); } -.status-dot { flex: none; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); } .status-tag { flex: none; max-width: 100%; @@ -158,7 +157,6 @@ body.settings-view { min-width: 0; min-height: 700px; overflow-y: auto; line-hei .model-copy { flex: 1 1 200px; min-width: 0; max-width: 100%; display: flex; flex-direction: column; align-items: flex-start; overflow-wrap: anywhere; } .model-row strong { max-width: 100%; font-size: 14px; } .model-desc { max-width: 100%; font-size: 13px; line-height: 1.54; color: var(--muted); margin-top: 2px; } -.model-id { max-width: 100%; margin-top: 4px; color: var(--muted); font-size: 10.5px; line-height: 1.5; overflow-wrap: anywhere; } .model-status-highlight { margin: 8px 0 0 !important; font-size: 13px !important; @@ -166,12 +164,6 @@ body.settings-view { min-width: 0; min-height: 700px; overflow-y: auto; line-hei font-weight: 600; color: var(--accent-ink) !important; } -.meta-muted { - margin: 4px 0 0 !important; - font-size: 10.5px !important; - line-height: 1.6 !important; - color: var(--muted) !important; -} .knowledge-accent { border-color: color-mix(in srgb, var(--accent) 30%, var(--border)); } .sr-only { position: absolute; @@ -317,9 +309,6 @@ body.settings-view { min-width: 0; min-height: 700px; overflow-y: auto; line-hei overflow-wrap: anywhere; } .knowledge-empty { margin-top: 0 !important; font-size: 10px !important; } -.slot-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 12px; } -.slot-grid span { min-width: 0; border-top: 1px solid var(--border); padding-top: 8px; color: var(--muted); font-size: 11px; overflow-wrap: anywhere; } -.slot-grid b { display: block; margin-top: 2px; color: var(--text); font-weight: 600; } .muted { color: var(--muted); } @media (min-width: 800px) { diff --git a/desktop/src/ui.ts b/desktop/src/ui.ts index 1ccddecc..7169199c 100644 --- a/desktop/src/ui.ts +++ b/desktop/src/ui.ts @@ -28,22 +28,6 @@ export function voiceErrorMessage(errorCode: string | null): string | null { } } -export const SETTINGS_IDS = Object.freeze([ - "settings-room-popup", - "settings-add-room-select", - "settings-add-room-button", - "settings-sync-source", - "settings-activity-source", - "settings-sync-card", - "settings-knowledge-card", - "knowledge-graph-canvas", - "knowledge-accessible-nodes", - "knowledge-expand-hop", - "knowledge-focus-title", - "knowledge-relations", - "knowledge-retrieve", -] as const); - export function mainPanelMarkup(): string { return `
@@ -274,7 +258,7 @@ export function renderRooms(snapshot: RuntimeSnapshot, root: Document = document if (addSelect) { addSelect.replaceChildren(); const enrolledIds = new Set(snapshot.rooms.map((r) => r.chatId)); - const candidates = (snapshot.availableChats || []).filter((c) => !enrolledIds.has(c.chatId)); + const candidates = snapshot.availableChats.filter((c) => !enrolledIds.has(c.chatId)); const defaultOpt = document.createElement("option"); defaultOpt.value = ""; defaultOpt.textContent = candidates.length > 0 ? "추가할 채팅방 선택" : "추가 가능한 새 채팅방 없음"; From b1316592c50cda4b891162da3c293bcca2f73354 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:42:58 +0900 Subject: [PATCH 33/67] fix(service): sweep owned process groups after their leader exits The session guardian runs `caffeinate -i openkakao-cli auto-reply` as a new session. caffeinate becomes the utility in place, so the session leader is the CLI itself, and helpers it starts without their own group stay in that session. _terminate_owned_child returned as soon as the leader exited after SIGTERM, and run_session_guardian returned as soon as the leader exited on its own. In both cases a member that ignored or outlived SIGTERM was left behind with no owner. After the leader exits, keep signalling the group by its ID (reserved while any member lives) until it is empty or the grace period ends, then SIGKILL what remains. The guardian runs the same sweep when its leader exits on its own. The leader-alive path and its re-attestation before SIGKILL are unchanged. Before: a TERM-ignoring member survived both _terminate_owned_child and a guardian whose leader exited with status 7. After: the member is gone and the guardian still returns 7. Co-Authored-By: Claude Opus 5.5 --- scripts/auto-reply-service.py | 67 ++++++++----- tests/test_auto_reply_service_entry.py | 132 +++++++++++++++++++++++++ 2 files changed, 177 insertions(+), 22 deletions(-) diff --git a/scripts/auto-reply-service.py b/scripts/auto-reply-service.py index 6d6f22d0..5018dbac 100755 --- a/scripts/auto-reply-service.py +++ b/scripts/auto-reply-service.py @@ -821,21 +821,37 @@ def _session_retry_delay(consecutive_failures: int) -> tuple[float, bool]: ) +def _process_group_exists(process_group: int) -> bool: + try: + os.killpg(process_group, 0) + except ProcessLookupError: + return False + except PermissionError: + return True + return True + + def _terminate_owned_child( child: subprocess.Popen[Any], *, grace_seconds: float = SESSION_TERMINATE_GRACE_SECONDS, ) -> None: - """Terminate the new session led by an owned child process.""" - if child.poll() is not None: - return - try: - process_group = os.getpgid(child.pid) - except ProcessLookupError: - child.wait(timeout=grace_seconds) + """Terminate the new session led by an owned child process. + + Members of the session can outlive its leader, so the group is swept + until it is empty or the grace period ends, even after the leader exits. + """ + process_group = child.pid + if child.poll() is None: + try: + leader_group = os.getpgid(child.pid) + except ProcessLookupError: + leader_group = process_group + if leader_group != process_group: + raise RuntimeError("owned child does not lead its process group") + elif not _process_group_exists(process_group): return - if process_group != child.pid: - raise RuntimeError("owned child does not lead its process group") + deadline = time.monotonic() + grace_seconds try: os.killpg(process_group, signal.SIGTERM) except ProcessLookupError: @@ -843,20 +859,26 @@ def _terminate_owned_child( return try: child.wait(timeout=grace_seconds) - return except subprocess.TimeoutExpired: - pass - - # Re-attest the group leader immediately before escalation. This avoids - # signaling an unrelated group if the child disappeared and its PID was - # reused during the grace period. - try: - if os.getpgid(child.pid) != process_group: - raise RuntimeError("owned child process group changed during shutdown") - os.killpg(process_group, signal.SIGKILL) - except ProcessLookupError: - pass - child.wait(timeout=grace_seconds) + # Re-attest the group leader immediately before escalation. This avoids + # signaling an unrelated group if the child disappeared and its PID was + # reused during the grace period. + try: + if os.getpgid(child.pid) != process_group: + raise RuntimeError("owned child process group changed during shutdown") + os.killpg(process_group, signal.SIGKILL) + except ProcessLookupError: + pass + child.wait(timeout=grace_seconds) + return + # A group ID cannot be reused while any member of the group is alive. + while _process_group_exists(process_group) and time.monotonic() < deadline: + time.sleep(0.01) + if _process_group_exists(process_group): + try: + os.killpg(process_group, signal.SIGKILL) + except ProcessLookupError: + pass def _session_guardian_spec( @@ -1116,6 +1138,7 @@ def request_stop(_signum: int, _frame: Any) -> None: while not stop_event.is_set(): exit_code = child.poll() if exit_code is not None: + terminate_child(child) child = None return int(exit_code) try: diff --git a/tests/test_auto_reply_service_entry.py b/tests/test_auto_reply_service_entry.py index 35312412..3aa1f331 100644 --- a/tests/test_auto_reply_service_entry.py +++ b/tests/test_auto_reply_service_entry.py @@ -111,6 +111,75 @@ def load_entry(name): return module +# A group leader whose child stays in the leader's process group, ignores +# SIGTERM, and records its PID. argv: <"wait" | "exit">. +STUBBORN_GROUP_LEADER = """\ +import os +import subprocess +import sys +import time + +pid_file, mode = sys.argv[1], sys.argv[2] +subprocess.Popen([ + sys.executable, + "-I", + "-c", + "import os, signal, sys, time\\n" + "signal.signal(signal.SIGTERM, signal.SIG_IGN)\\n" + "open(sys.argv[1] + '.tmp', 'w').write(str(os.getpid()))\\n" + "os.replace(sys.argv[1] + '.tmp', sys.argv[1])\\n" + "time.sleep(30)\\n", + pid_file, +]) +deadline = time.monotonic() + 5 +while not os.path.exists(pid_file) and time.monotonic() < deadline: + time.sleep(0.01) +if mode == "exit": + raise SystemExit(7) +time.sleep(30) +""" + + +def _start_stubborn_group(pid_file: Path, mode: str) -> subprocess.Popen: + return subprocess.Popen( + [str(_test_python()), "-I", "-c", STUBBORN_GROUP_LEADER, str(pid_file), mode], + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + start_new_session=True, + ) + + +def _read_member_pid(pid_file: Path, timeout: float = 5.0) -> int: + deadline = time.monotonic() + timeout + while not pid_file.exists(): + if time.monotonic() >= deadline: + raise AssertionError("group member never recorded its PID") + time.sleep(0.01) + return int(pid_file.read_text(encoding="utf-8")) + + +def _process_gone(pid: int, timeout: float = 3.0) -> bool: + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + os.kill(pid, 0) + except ProcessLookupError: + return True + time.sleep(0.02) + return False + + +def _kill_stray_member(pid: int | None, process_group: int) -> None: + if pid is None: + return + try: + if os.getpgid(pid) == process_group: + os.kill(pid, signal.SIGKILL) + except ProcessLookupError: + pass + + class CatalogSelectorTests(unittest.TestCase): def test_named_catalog_room_binds_its_title(self): module = load_entry("auto_reply_catalog_selector_test") @@ -1835,6 +1904,69 @@ def wait(self, timeout): [mock.call(child.pid, signal.SIGTERM), mock.call(child.pid, signal.SIGKILL)], ) + def test_owned_child_termination_sweeps_members_that_outlive_the_leader(self): + module = load_entry("auto_reply_terminate_sweep_test") + with tempfile.TemporaryDirectory() as temporary: + pid_file = Path(temporary) / "member.pid" + leader = _start_stubborn_group(pid_file, "wait") + member = None + try: + member = _read_member_pid(pid_file) + module._terminate_owned_child(leader, grace_seconds=0.5) + self.assertIsNotNone(leader.poll()) + self.assertTrue( + _process_gone(member), + "a TERM-ignoring group member outlived its leader's termination", + ) + finally: + _kill_stray_member(member, leader.pid) + if leader.poll() is None: + os.killpg(leader.pid, signal.SIGKILL) + leader.wait(timeout=5) + + def test_session_guardian_sweeps_members_left_by_an_exited_leader(self): + with tempfile.TemporaryDirectory() as temporary: + fixture = self._fixture(Path(temporary)) + module = fixture["module"] + read_fd, write_fd = module._create_session_guardian_control( + fixture["binary"], fixture["config"], "bind:42:room" + ) + pid_file = Path(temporary) / "member.pid" + leaders = [] + + def fake_popen(_argv, **_kwargs): + leaders.append(_start_stubborn_group(pid_file, "exit")) + return leaders[-1] + + def quiet_select(_readers, _writers, _errors, _timeout): + time.sleep(0.02) + return [], [], [] + + member = None + try: + with mock.patch.object(module.os, "getpid", return_value=6100), mock.patch.object( + module.os, "getpgrp", return_value=6100 + ), mock.patch.object(module.os, "getsid", return_value=6100), mock.patch.object( + module, "SESSION_GUARDIAN_TERMINATE_GRACE_SECONDS", 0.5 + ): + result = module.run_session_guardian( + read_fd, + popen_factory=fake_popen, + select_fn=quiet_select, + install_signal_handlers=False, + ) + member = _read_member_pid(pid_file) + self.assertEqual(result, 7) + self.assertTrue( + _process_gone(member), + "the guardian returned while its exited leader's group still had a member", + ) + finally: + os.close(write_fd) + for leader in leaders: + _kill_stray_member(member, leader.pid) + leader.wait(timeout=5) + def test_runtime_asset_drift_during_check_fails_without_receipt(self): with tempfile.TemporaryDirectory() as temporary: fixture = self._fixture(Path(temporary)) From 42bb245608b2c395ea827612833c5cff55418110 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:39:20 +0900 Subject: [PATCH 34/67] fix: replace state.json atomically save_state truncated state.json in place and then wrote it, so any process reading the file during a save saw an empty or partial JSON document. load_state then failed to parse, which aborts rate-limit and cooldown checks. The new contents now go to a private temporary file in the same directory, are fsynced, and are renamed over state.json. The read and write paths take an explicit path so tests run against a temporary directory instead of ~/.config/openkakao. Co-Authored-By: Claude Opus 5.5 --- src/state.rs | 99 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 30 deletions(-) diff --git a/src/state.rs b/src/state.rs index 9778143f..4a787f58 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,6 +1,6 @@ use std::fs; use std::io::Write; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; use chrono::{DateTime, Duration, Utc}; @@ -64,46 +64,40 @@ pub fn state_path() -> Result { } pub fn load_state() -> Result { - let path = state_path()?; + read_state(&state_path()?) +} + +fn read_state(path: &Path) -> Result { if !path.exists() { return Ok(OpenKakaoState::default()); } let data = - fs::read_to_string(&path).with_context(|| format!("Failed to read {}", path.display()))?; + fs::read_to_string(path).with_context(|| format!("Failed to read {}", path.display()))?; let state: OpenKakaoState = serde_json::from_str(&data) .with_context(|| format!("Failed to parse {}", path.display()))?; Ok(state) } -pub fn save_state(state: &OpenKakaoState) -> Result { - let path = state_path()?; - if let Some(parent) = path.parent() { - fs::create_dir_all(parent) - .with_context(|| format!("Failed to create {}", parent.display()))?; - } +fn write_state(path: &Path, state: &OpenKakaoState) -> Result<()> { + let parent = path + .parent() + .with_context(|| format!("{} has no parent directory", path.display()))?; + fs::create_dir_all(parent).with_context(|| format!("Failed to create {}", parent.display()))?; let data = serde_json::to_string_pretty(state).context("Failed to serialize state")?; - #[cfg(unix)] - let mut file = { - use std::os::unix::fs::OpenOptionsExt; - fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .mode(0o600) - .open(&path) - .with_context(|| format!("Failed to create {}", path.display()))? - }; - #[cfg(not(unix))] - let mut file = - fs::File::create(&path).with_context(|| format!("Failed to create {}", path.display()))?; - + // Readers take no lock, so state.json is only ever replaced whole. + let mut file = tempfile::NamedTempFile::new_in(parent) + .with_context(|| format!("Failed to create a temporary file in {}", parent.display()))?; file.write_all(data.as_bytes()) - .with_context(|| format!("Failed to write {}", path.display()))?; - - Ok(path) + .with_context(|| format!("Failed to write {}", file.path().display()))?; + file.as_file() + .sync_all() + .with_context(|| format!("Failed to flush {}", file.path().display()))?; + file.persist(path) + .map_err(|error| anyhow::anyhow!("Failed to replace {}: {error}", path.display()))?; + Ok(()) } fn parse_ts(value: Option<&str>) -> Option> { @@ -318,15 +312,60 @@ pub fn recovery_state_summary() -> Result { } fn mutate_state(mutator: impl FnOnce(&mut OpenKakaoState)) -> Result<()> { - let mut state = load_state()?; + mutate_state_at(&state_path()?, mutator) +} + +fn mutate_state_at(path: &Path, mutator: impl FnOnce(&mut OpenKakaoState)) -> Result<()> { + let mut state = read_state(path)?; mutator(&mut state); - save_state(&state)?; - Ok(()) + write_state(path, &state) } #[cfg(test)] mod tests { use super::*; + use std::sync::atomic::{AtomicBool, Ordering}; + + #[test] + fn readers_never_see_a_partly_written_state_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("state.json"); + let state = OpenKakaoState { + last_guard_reason: Some("가".repeat(4000)), + ..Default::default() + }; + write_state(&path, &state).unwrap(); + + let writing = AtomicBool::new(true); + let torn_reads = std::thread::scope(|scope| { + let reader = scope.spawn(|| { + let mut torn = 0; + while writing.load(Ordering::Relaxed) { + if read_state(&path).is_err() { + torn += 1; + } + } + torn + }); + for _ in 0..100 { + write_state(&path, &state).unwrap(); + } + writing.store(false, Ordering::Relaxed); + reader.join().unwrap() + }); + assert_eq!(torn_reads, 0, "a reader saw an empty or partial state file"); + } + + #[cfg(unix)] + #[test] + fn state_file_is_private_to_the_owner() { + use std::os::unix::fs::PermissionsExt; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("state.json"); + write_state(&path, &OpenKakaoState::default()).unwrap(); + let mode = fs::metadata(&path).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o600); + } #[test] fn default_state_has_no_cooldown() { From d455df0f3d9b257afa3f030472546aadf1ff9ed3 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:42:42 +0900 Subject: [PATCH 35/67] fix: serialize state.json updates across CLI processes mutate_state read, modified and rewrote state.json with no lock. Separate CLI processes write it concurrently: ax-watch and watch record hook and webhook rate limits, send records unattended-send limits, and every REST or LOCO command records auth recovery through auth_flow. When two updates overlapped, one was lost, including the relogin timestamps and failure counters behind the anti-ban cooldowns. With four concurrent writers, 72 of 100 increments were lost. Updates now hold an exclusive lock on a sibling state.json.lock file for the whole read-modify-write. Readers stay lock-free because writes are atomic renames. Co-Authored-By: Claude Opus 5.5 --- src/state.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/state.rs b/src/state.rs index 4a787f58..4ecf1cc6 100644 --- a/src/state.rs +++ b/src/state.rs @@ -83,8 +83,6 @@ fn write_state(path: &Path, state: &OpenKakaoState) -> Result<()> { let parent = path .parent() .with_context(|| format!("{} has no parent directory", path.display()))?; - fs::create_dir_all(parent).with_context(|| format!("Failed to create {}", parent.display()))?; - let data = serde_json::to_string_pretty(state).context("Failed to serialize state")?; // Readers take no lock, so state.json is only ever replaced whole. @@ -316,11 +314,31 @@ fn mutate_state(mutator: impl FnOnce(&mut OpenKakaoState)) -> Result<()> { } fn mutate_state_at(path: &Path, mutator: impl FnOnce(&mut OpenKakaoState)) -> Result<()> { + let _lock = lock_state(path)?; let mut state = read_state(path)?; mutator(&mut state); write_state(path, &state) } +/// Serializes read-modify-write across CLI processes. The lock lives on a +/// sibling file because state.json itself is replaced by rename. +fn lock_state(path: &Path) -> Result { + if let Some(parent) = path.parent() { + fs::create_dir_all(parent) + .with_context(|| format!("Failed to create {}", parent.display()))?; + } + let lock_path = path.with_extension("json.lock"); + let file = fs::OpenOptions::new() + .create(true) + .truncate(false) + .write(true) + .open(&lock_path) + .with_context(|| format!("Failed to open {}", lock_path.display()))?; + file.lock() + .with_context(|| format!("Failed to lock {}", lock_path.display()))?; + Ok(file) +} + #[cfg(test)] mod tests { use super::*; @@ -356,6 +374,22 @@ mod tests { assert_eq!(torn_reads, 0, "a reader saw an empty or partial state file"); } + #[test] + fn concurrent_state_updates_are_not_lost() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("state.json"); + std::thread::scope(|scope| { + for _ in 0..4 { + scope.spawn(|| { + for _ in 0..25 { + mutate_state_at(&path, |state| state.consecutive_failures += 1).unwrap(); + } + }); + } + }); + assert_eq!(read_state(&path).unwrap().consecutive_failures, 100); + } + #[cfg(unix)] #[test] fn state_file_is_private_to_the_owner() { From 932194eec0ba1f8798433f42765f0a3e2d0a5ff6 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:43:56 +0900 Subject: [PATCH 36/67] refactor(desktop): drop the unused clock parameters from the frame math CoreRenderState.step took a nowMs it never read, and the lattice pulse helpers took a seconds argument the caller filled with a literal 0. The pulse phase is integrated from dt alone, so the signature now says so and JarvisCore.render stops threading a timestamp through three layers. Existing tests and the render benchmark change only by dropping that argument at each call site. Co-Authored-By: Claude Fable 5.2 --- desktop/src/__tests__/load-mapping.test.ts | 6 ++--- desktop/src/__tests__/render-state.test.ts | 30 +++++++++++----------- desktop/src/core/jarvis-core.ts | 6 ++--- desktop/src/core/load-mapping.ts | 6 ++--- desktop/src/core/render-state.ts | 9 ++----- desktop/src/perf/render-path-node.ts | 8 +++--- 6 files changed, 30 insertions(+), 35 deletions(-) diff --git a/desktop/src/__tests__/load-mapping.test.ts b/desktop/src/__tests__/load-mapping.test.ts index f532bdfa..e66a588d 100644 --- a/desktop/src/__tests__/load-mapping.test.ts +++ b/desktop/src/__tests__/load-mapping.test.ts @@ -152,8 +152,8 @@ describe("source load mapping", () => { }); it("keeps lattice pulse parameters bounded and deterministic", () => { - expect(latticePulse(-4, 0)).toEqual({ frequency: 1, amplitude: 0.05, opacity: 0.06, density: 0 }); - expect(latticePulse(1, 10)).toEqual({ frequency: 1.8, amplitude: 0.1, opacity: 0.18, density: 1 }); - expect(latticePulse(5, 999)).toEqual(latticePulse(1, 1)); + expect(latticePulse(-4)).toEqual({ frequency: 1, amplitude: 0.05, opacity: 0.06, density: 0 }); + expect(latticePulse(1)).toEqual({ frequency: 1.8, amplitude: 0.1, opacity: 0.18, density: 1 }); + expect(latticePulse(5)).toEqual(latticePulse(1)); }); }); diff --git a/desktop/src/__tests__/render-state.test.ts b/desktop/src/__tests__/render-state.test.ts index 245dcfa8..8ae35df4 100644 --- a/desktop/src/__tests__/render-state.test.ts +++ b/desktop/src/__tests__/render-state.test.ts @@ -48,8 +48,8 @@ describe("reused render scratch space", () => { it("writes the lattice pulse into the caller's object", () => { const out: LatticePulse = { frequency: 0, amplitude: 0, opacity: 0, density: 0 }; for (const total of [-4, 0, 0.5, 1, 5, Number.NaN]) { - expect(writeLatticePulse(out, total, 0)).toBe(out); - expect(out).toEqual(latticePulse(total, 0)); + expect(writeLatticePulse(out, total)).toBe(out); + expect(out).toEqual(latticePulse(total)); } }); @@ -91,7 +91,7 @@ describe("core render state", () => { ringVelocity[ring] = damp(ringVelocity[ring], targets[ring], 1.8 + ring * 0.55); } - frame = state.step(dt, index * 20, BASE, GAIN); + frame = state.step(dt, BASE, GAIN); expect(frame.smoothLoad).toBeCloseTo(smoothLoad, 12); expect(frame.smoothVoiceRms).toBeCloseTo(smoothVoice, 12); expect(frame.nucleusScale).toBeCloseTo(scale, 12); @@ -105,11 +105,11 @@ describe("core render state", () => { it("returns one reused frame object and never regrows its arrays", () => { const state = new CoreRenderState(); state.setSignals(0.5, 0.2, loads(0.4, 0.4, 0.4, 0.5)); - const first = state.step(0.016, 0, BASE, GAIN); + const first = state.step(0.016, BASE, GAIN); const velocities = first.ringVelocities; const targets = first.ringTargets; for (let index = 1; index <= 200; index += 1) { - const frame = state.step(0.016, index * 16, BASE, GAIN); + const frame = state.step(0.016, BASE, GAIN); expect(frame).toBe(first); expect(frame.ringVelocities).toBe(velocities); expect(frame.ringTargets).toBe(targets); @@ -121,7 +121,7 @@ describe("core render state", () => { it("allocates no array in the per-frame path", () => { const state = new CoreRenderState(); state.setSignals(0.6, 0.2, loads(0.5, 0.5, 0.5, 0.6)); - state.step(0.016, 0, BASE, GAIN); + state.step(0.016, BASE, GAIN); const realMap = Array.prototype.map; const realSlice = Array.prototype.slice; const realConcat = Array.prototype.concat; @@ -141,7 +141,7 @@ describe("core render state", () => { }; try { for (let index = 0; index < 300; index += 1) { - state.step(0.016, index * 16, BASE, GAIN); + state.step(0.016, BASE, GAIN); } } finally { prototype.map = realMap; @@ -154,12 +154,12 @@ describe("core render state", () => { it("clamps a long resume gap and survives non-finite input", () => { const state = new CoreRenderState(); state.setSignals(1, 1, loads(1, 1, 1, 1)); - const resumed = state.step(10, 1_000_000, BASE, GAIN); + const resumed = state.step(10, BASE, GAIN); expect(resumed.dt).toBe(0.25); expect(resumed.smoothLoad).toBeLessThan(1); expect(Number.isFinite(resumed.nucleusScale)).toBe(true); - const broken = state.step(Number.NaN, Number.NaN, BASE, GAIN); + const broken = state.step(Number.NaN, BASE, GAIN); expect(broken.dt).toBe(0); for (const value of [ broken.smoothLoad, @@ -182,7 +182,7 @@ describe("core render state", () => { total: Number.POSITIVE_INFINITY, }); expect(state.jobLoad).toBe(0); - const frame = state.step(0.05, 0, BASE, GAIN); + const frame = state.step(0.05, BASE, GAIN); expect(frame.latticeScale).toBeGreaterThan(0); expect(Number.isFinite(frame.nucleusScale)).toBe(true); expect([...frame.ringTargets].every((value) => Number.isFinite(value))).toBe(true); @@ -190,7 +190,7 @@ describe("core render state", () => { it("resizes its ring arrays when the ring count changes", () => { const state = new CoreRenderState(); - const frame = state.step(0.016, 0, [0.1, 0.2, 0.3, 0.4], [1, 1, 1, 1]); + const frame = state.step(0.016, [0.1, 0.2, 0.3, 0.4], [1, 1, 1, 1]); expect(frame.ringVelocities.length).toBe(4); expect(frame.ringTargets.length).toBe(4); expect([...frame.ringTargets].every((value) => Number.isFinite(value))).toBe(true); @@ -199,7 +199,7 @@ describe("core render state", () => { it("preserves 15fps elapsed time and lets voice activity drive global motion", () => { const state = new CoreRenderState(); state.setSignals(0, 0.8, loads(0, 0, 0, 0)); - const frame = state.step(1 / 15, 0, BASE, GAIN); + const frame = state.step(1 / 15, BASE, GAIN); expect(frame.dt).toBeCloseTo(1 / 15, 12); expect(frame.jobLoad).toBe(0.8); expect(frame.pulse.density).toBeGreaterThan(0); @@ -210,9 +210,9 @@ describe("core render state", () => { it("accumulates pulse phase continuously as load changes", () => { const state = new CoreRenderState(); state.setSignals(0, 0, loads(0, 0, 0, 0)); - const first = state.step(0.05, 0, BASE, GAIN).latticeScale; + const first = state.step(0.05, BASE, GAIN).latticeScale; state.setSignals(1, 0, loads(0, 0, 0, 1)); - const second = state.step(0.05, 50, BASE, GAIN).latticeScale; + const second = state.step(0.05, BASE, GAIN).latticeScale; expect(first).toBeGreaterThan(1); expect(second).toBeGreaterThan(1); expect(Math.abs(second - first)).toBeLessThan(0.1); @@ -227,7 +227,7 @@ describe("core render state", () => { state.setSignals(0, 0, loads(0, 0, 0, target)); const counts = new Set(); for (let frame = 0; frame < fps * 3; frame += 1) { - const density = state.step(1 / fps, frame * 1000 / fps, BASE, GAIN).pulse.density; + const density = state.step(1 / fps, BASE, GAIN).pulse.density; const count = pulseLatticeDrawCount(density, lattice.drawCounts); expect(target === 1 ? count >= previous : count <= previous).toBe(true); // Even the first frame of a full step is smaller than the old tier jump. diff --git a/desktop/src/core/jarvis-core.ts b/desktop/src/core/jarvis-core.ts index 688970d7..cb3e889f 100644 --- a/desktop/src/core/jarvis-core.ts +++ b/desktop/src/core/jarvis-core.ts @@ -109,7 +109,7 @@ export class JarvisCore { ); this.root.add(this.nucleus); - this.loop = new AnimationLoop((dt, nowMs) => this.render(dt, nowMs)); + this.loop = new AnimationLoop((dt) => this.render(dt)); } start(): void { @@ -141,10 +141,10 @@ export class JarvisCore { this.renderer.dispose(); } - private render(dt: number, nowMs: number): void { + private render(dt: number): void { // One reused frame object per tick: no array, closure or object is built // inside the 15-30fps loop (2026-09-22). - const frame = this.state.step(dt, nowMs, this.ringBaseVelocity, this.ringGain); + const frame = this.state.step(dt, this.ringBaseVelocity, this.ringGain); const rings = this.rings; const velocities = frame.ringVelocities; for (let index = 0; index < rings.length; index += 1) { diff --git a/desktop/src/core/load-mapping.ts b/desktop/src/core/load-mapping.ts index 7de0f5fe..8142f1cf 100644 --- a/desktop/src/core/load-mapping.ts +++ b/desktop/src/core/load-mapping.ts @@ -104,12 +104,12 @@ export function writeRingTargetVelocities( return out; } -export function latticePulse(total: number, seconds: number): LatticePulse { - return writeLatticePulse({ frequency: 0, amplitude: 0, opacity: 0, density: 0 }, total, seconds); +export function latticePulse(total: number): LatticePulse { + return writeLatticePulse({ frequency: 0, amplitude: 0, opacity: 0, density: 0 }, total); } /** Write the lattice pulse into a reused object. F = F0*(1+0.8L), A = 0.05+0.05L, O = 0.06+0.12L. */ -export function writeLatticePulse(out: LatticePulse, total: number, _seconds: number): LatticePulse { +export function writeLatticePulse(out: LatticePulse, total: number): LatticePulse { const load = clamp01(total); out.frequency = 1.0 * (1 + 0.8 * load); out.amplitude = 0.05 + 0.05 * load; diff --git a/desktop/src/core/render-state.ts b/desktop/src/core/render-state.ts index 18a699ea..990a99e6 100644 --- a/desktop/src/core/render-state.ts +++ b/desktop/src/core/render-state.ts @@ -86,12 +86,7 @@ export class CoreRenderState { } /** Advance the smoothing and spring state by one frame; returns `this`. */ - step( - dtSeconds: number, - _nowMs: number, - base: readonly number[], - gain: readonly number[], - ): CoreRenderState { + step(dtSeconds: number, base: readonly number[], gain: readonly number[]): CoreRenderState { if (base.length !== this.ringVelocities.length) this.resizeRings(base.length); const dt = Number.isFinite(dtSeconds) && dtSeconds > 0 ? Math.min(dtSeconds, MAX_FRAME_SECONDS) @@ -140,7 +135,7 @@ export class CoreRenderState { } const globalLoad = Math.max(total, this.smoothLoad, this.smoothVoiceRms); - const pulse = writeLatticePulse(this.pulse, globalLoad, 0); + const pulse = writeLatticePulse(this.pulse, globalLoad); this.pulsePhase = (this.pulsePhase + TAU * pulse.frequency * dt) % TAU; this.acousticPhase = (this.acousticPhase + TAU * ACOUSTIC_WOBBLE_HZ * dt) % TAU; const backgroundPulse = 1 + pulse.amplitude * Math.sin(this.pulsePhase); diff --git a/desktop/src/perf/render-path-node.ts b/desktop/src/perf/render-path-node.ts index 934f47b0..b9dee679 100644 --- a/desktop/src/perf/render-path-node.ts +++ b/desktop/src/perf/render-path-node.ts @@ -96,7 +96,7 @@ class LegacyFrame { this.nucleusScale += this.nucleusVelocity * dt; const seconds = nowMs / 1000; - const pulse = latticePulse(this.smoothSources.total, seconds); + const pulse = latticePulse(this.smoothSources.total); const backgroundPulse = 1 + pulse.amplitude * Math.sin(seconds * pulse.frequency * Math.PI * 2); // The old body did compute the acoustic term as well; leaving it out here // would drop one Math.sin from the "before" side and flatter legacy. @@ -158,13 +158,13 @@ report("now: reused scratch array", () => { console.log("lattice pulse (per frame)"); report("legacy: object literal", () => { for (let index = 0; index < ITERATIONS; index += 1) { - const pulse = latticePulse(LOADS.total, 12.5); + const pulse = latticePulse(LOADS.total); sink += pulse.frequency + pulse.amplitude + pulse.opacity; } }); report("now: reused object", () => { for (let index = 0; index < ITERATIONS; index += 1) { - const pulse = writeLatticePulse(pulseOut, LOADS.total, 12.5); + const pulse = writeLatticePulse(pulseOut, LOADS.total); sink += pulse.frequency + pulse.amplitude + pulse.opacity; } }); @@ -178,7 +178,7 @@ report("legacy: inline render body", () => { report("now: CoreRenderState.step", () => { for (let index = 0; index < ITERATIONS; index += 1) { tick += 1; - newFrame.step(DT, tick * 33, BASE, GAIN); + newFrame.step(DT, BASE, GAIN); } }); From cc20fe3dd1fe4b17403173f6e44ebd16a6f09e3d Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:45:31 +0900 Subject: [PATCH 37/67] refactor(scripts): delete dead voice state and a pass-through CLI resolver JarvisVoicePipeline._reply was initialised to an empty string and never assigned, so _end read it through getattr only to return the default; model_swap_failed() had no caller; write_wav re-imported array, which the module already imports. _resolve_orx_cli only forwarded to _resolve_alphaxiv_cli, so both providers now call one _resolve_cli. Co-Authored-By: Claude Fable 5.2 --- scripts/dream_rsi_alphaxiv.py | 15 +++++---------- scripts/jarvis_voice.py | 8 +------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/scripts/dream_rsi_alphaxiv.py b/scripts/dream_rsi_alphaxiv.py index cd678bf2..ddaf2545 100644 --- a/scripts/dream_rsi_alphaxiv.py +++ b/scripts/dream_rsi_alphaxiv.py @@ -86,7 +86,7 @@ def _validate_paper_id(value: Any) -> str: return paper_id -def _resolve_alphaxiv_cli(requested: Any) -> tuple[str | None, str | None]: +def _resolve_cli(requested: Any) -> tuple[str | None, str | None]: """Resolve one executable without allowing option/path injection.""" try: value = _valid_plain_text(requested, limit=4096) @@ -115,11 +115,6 @@ def _resolve_alphaxiv_cli(requested: Any) -> tuple[str | None, str | None]: return found, None -def _resolve_orx_cli(requested: Any) -> tuple[str | None, str | None]: - """Resolve the OpenResearch CLI with the same executable-path guard.""" - return _resolve_alphaxiv_cli(requested) - - def _bytes(value: Any) -> bytes: if value is None: return b"" @@ -417,7 +412,7 @@ def collect_orx_paper_report( ) timeout_value = min(MAX_TIMEOUT_SECONDS, max(0.1, timeout_value)) - executable, resolve_error = _resolve_orx_cli(orx_cli) + executable, resolve_error = _resolve_cli(orx_cli) if resolve_error or not executable: return _paper_unavailable( requested_cli=requested_cli, @@ -550,7 +545,7 @@ def collect_paper_report( paper_id=paper_id or DREAM_RSI_PAPER_ID, ) if paper_source == "auto": - executable, _ = _resolve_alphaxiv_cli(alphaxiv_cli) + executable, _ = _resolve_cli(alphaxiv_cli) if executable: return collect_paper_report( query, @@ -560,7 +555,7 @@ def collect_paper_report( timeout=timeout, paper_id=paper_id, ) - orx_executable, _ = _resolve_orx_cli(orx_cli) + orx_executable, _ = _resolve_cli(orx_cli) if orx_executable: return collect_orx_paper_report( query, @@ -606,7 +601,7 @@ def collect_paper_report( reason="invalid_timeout", ) - executable, resolve_error = _resolve_alphaxiv_cli(alphaxiv_cli) + executable, resolve_error = _resolve_cli(alphaxiv_cli) if resolve_error or not executable: return _paper_unavailable( requested_cli=requested_cli, diff --git a/scripts/jarvis_voice.py b/scripts/jarvis_voice.py index db9a9eae..dbb3ab06 100644 --- a/scripts/jarvis_voice.py +++ b/scripts/jarvis_voice.py @@ -588,7 +588,6 @@ def __init__( self._wake_source = "none" self._custom_model_selected = False self._transcript = "" - self._reply = "" self._speech_frames = 0 self._silence_frames = 0 self._noise_frames = 0 @@ -635,7 +634,7 @@ def _end(self, state: VoiceState, error_code: str) -> VoiceResult: self.state = state self.ring.clear() self._publish(error_code) - return VoiceResult(state=state, error_code=error_code, transcript=getattr(self, "_transcript", ""), reply=getattr(self, "_reply", "")) + return VoiceResult(state=state, error_code=error_code, transcript=self._transcript) def mic_disconnected(self) -> VoiceResult: return self._end(VoiceState.ERROR, "mic_disconnected") @@ -643,9 +642,6 @@ def mic_disconnected(self) -> VoiceResult: def mic_unavailable(self) -> VoiceResult: return self._end(VoiceState.ERROR, "mic_unavailable") - def model_swap_failed(self) -> VoiceResult: - return self._end(VoiceState.ERROR, "model_swap_failed") - def feed_audio( self, pcm16: bytes, @@ -936,8 +932,6 @@ def _flatten(a: Any) -> list[float]: peak = max((abs(x) for x in samples_list), default=0.0) if peak > 1.0: samples_list = [x / peak for x in samples_list] - from array import array - pcm_arr = array("h", [int(max(-32768, min(32767, round(x * 32767.0)))) for x in samples_list]) pcm_bytes = pcm_arr.tobytes() nframes = len(pcm_arr) From 64d8e8d83970c4ca3ba1f9dfd380a5650bdb05a7 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:46:13 +0900 Subject: [PATCH 38/67] fix(db-watch): fence a malformed poll cursor instead of replaying the room _poll_cursor returned 0 when pending_log_ids or acked_watermark could not be read as integers. The poll stream starts from that cursor, so a malformed cursor meant polling the room from its first message. _state() already fences invalid cursor fields before poll_once reaches _poll_cursor, so this branch is not reached today. Make it fail closed anyway: raise DbFence with a reconcile_required reason, which poll_once publishes as a durable fence rather than a retryable poll gap. Before: all four malformed cursors returned 0 (DbFence not raised). After: each raises DbFence mapped to reconcile_required; valid cursors keep their boundaries. Co-Authored-By: Claude Opus 5.5 --- scripts/auto-reply-db-watch.py | 4 ++-- tests/test_auto_reply_db_watch_retry.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/scripts/auto-reply-db-watch.py b/scripts/auto-reply-db-watch.py index 8e2f8324..49eef6ee 100644 --- a/scripts/auto-reply-db-watch.py +++ b/scripts/auto-reply-db-watch.py @@ -2670,8 +2670,8 @@ def _poll_cursor(state: dict) -> int: if not isinstance(value, bool) and 0 < int(value) < MAX_INT64 ] watermark = int(state.get("acked_watermark", 0)) - except (TypeError, ValueError): - return 0 + except (TypeError, ValueError) as exc: + raise DbFence("poll cursor malformed: reconcile_required") from exc if pending: return max(0, min(pending) - 1) return max(0, min(watermark, MAX_INT64 - 1)) diff --git a/tests/test_auto_reply_db_watch_retry.py b/tests/test_auto_reply_db_watch_retry.py index 81016aad..63b4bc49 100644 --- a/tests/test_auto_reply_db_watch_retry.py +++ b/tests/test_auto_reply_db_watch_retry.py @@ -448,6 +448,23 @@ def test_a_wedged_hook_still_outruns_the_liveness_window(self): "an unreturned hook must leave a real staleness gap", ) + def test_poll_cursor_never_rewinds_to_the_room_start_on_a_malformed_cursor(self): + module = load_db_watch("auto_reply_db_watch_poll_cursor_test") + self.assertEqual(module._poll_cursor({"pending_log_ids": [130, 125], "acked_watermark": 123}), 124) + self.assertEqual(module._poll_cursor({"pending_log_ids": [], "acked_watermark": 123}), 123) + for malformed in ( + {"pending_log_ids": ["not-an-id"], "acked_watermark": 123}, + {"pending_log_ids": None, "acked_watermark": 123}, + {"pending_log_ids": [], "acked_watermark": None}, + {"pending_log_ids": [], "acked_watermark": "123x"}, + ): + with self.subTest(state=malformed): + with self.assertRaises(module.DbFence) as raised: + module._poll_cursor(malformed) + self.assertEqual( + module._fixed_fence_reason(raised.exception), "reconcile_required" + ) + if __name__ == "__main__": unittest.main() From c92ab8e2d26677c33593f69fbdea6f78e1196e40 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:32:12 +0900 Subject: [PATCH 39/67] fix: stop an id-less catalog room from allowing every send target `allowed_send_chat_targets` read `menubar-room-catalog.json` as untyped JSON and compared the caller's chat ID to each room's ID as `Option`. `local-send` and `local-delete` pass no chat ID, so one enabled room whose `chat_id` was missing, null, a string, or a float matched every room name. The gate now loads the catalog with `room_catalog::load_catalog_rooms`, the loader that AutoReply startup already uses. A loaded room always has a positive integer chat ID, so a caller without a chat ID can match by title only. A catalog that the loader rejects authorizes none of its rooms. A valid catalog authorizes the same rooms as before. Co-Authored-By: Claude Fable 5.2 --- src/config.rs | 70 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/src/config.rs b/src/config.rs index f02a100b..31311795 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use std::collections::{BTreeMap, BTreeSet}; use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; use serde::Deserialize; @@ -543,32 +543,19 @@ pub fn allowed_send_chat_targets( return true; } - let Some(root_str) = config.auto_reply.state_root.as_deref() else { + let Some(state_root) = config.auto_reply.state_root.as_deref() else { return false; }; - let catalog_path = std::path::Path::new(root_str).join("menubar-room-catalog.json"); - let Ok(catalog_bytes) = std::fs::read(&catalog_path) else { + // AutoReply startup uses this strict loader too. Every loaded room has a + // real chat ID, so a caller without one matches by title only, and a + // catalog the loader rejects authorizes nothing. + let Ok(rooms) = openkakao_cli::room_catalog::load_catalog_rooms(Path::new(state_root)) else { return false; }; - let Ok(catalog_json) = serde_json::from_slice::(&catalog_bytes) else { - return false; - }; - let Some(rooms) = catalog_json.get("rooms").and_then(|rooms| rooms.as_array()) else { - return false; - }; - rooms.iter().any(|room| { - let Some(object) = room.as_object() else { - return false; - }; - let r_id = object.get("chat_id").and_then(|id| id.as_i64()); - let r_title = object - .get("title") - .and_then(|title| title.as_str()) - .filter(|title| !title.is_empty()); - let enabled = object.get("auto_reply") == Some(&serde_json::Value::Bool(true)) - || object.get("geeknews") == Some(&serde_json::Value::Bool(true)); - enabled && (chat_id == r_id || r_title == Some(chat_name)) + (room.auto_reply || room.geeknews) + && (chat_id == Some(room.chat_id) + || (!room.title.is_empty() && room.title == chat_name)) }) } @@ -759,6 +746,45 @@ mod tests { )); } + fn config_with_catalog(catalog: &str) -> (tempfile::TempDir, OpenKakaoConfig) { + let dir = tempfile::tempdir().expect("tempdir"); + std::fs::write(dir.path().join("menubar-room-catalog.json"), catalog) + .expect("write catalog"); + let mut config = OpenKakaoConfig::default(); + config.auto_reply.state_root = Some(dir.path().to_string_lossy().into_owned()); + (dir, config) + } + + #[test] + fn catalog_room_without_an_integer_chat_id_allows_no_other_room() { + let leaked: Vec<&str> = [ + r#"{"rooms":[{"title":"A","auto_reply":true}]}"#, + r#"{"rooms":[{"chat_id":null,"title":"A","auto_reply":true}]}"#, + r#"{"rooms":[{"chat_id":"437046948660911","title":"A","geeknews":true}]}"#, + r#"{"rooms":[{"chat_id":1.5,"title":"A","auto_reply":true}]}"#, + ] + .into_iter() + .filter(|catalog| { + let (_dir, config) = config_with_catalog(catalog); + allowed_send_chat_targets(&config, "완전히 다른 방", None) + }) + .collect(); + assert!( + leaked.is_empty(), + "catalogs that authorized an unrelated room: {leaked:#?}" + ); + } + + #[test] + fn catalog_the_strict_loader_rejects_authorizes_none_of_its_rooms() { + let (_dir, config) = config_with_catalog( + r#"{"rooms":[{"chat_id":42,"title":"B","auto_reply":true},{"title":"A","auto_reply":true}]}"#, + ); + assert!(!allowed_send_chat_targets(&config, "B", None)); + assert!(!allowed_send_chat_targets(&config, "B", Some(42))); + assert!(!allowed_send_chat_targets(&config, "A", None)); + } + #[test] fn local_mlx_auto_reply_profile_requires_exact_privacy_provider_and_model() { let mut config = OpenKakaoConfig::default(); From 747eedc5f6f2e8bdaa9c0d43c5c48950cbce2a4c Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:47:56 +0900 Subject: [PATCH 40/67] fix(session-monitor): stop asking open(1) to hide Terminal BACKGROUND_OPEN_ARGV still passed `-j`. open(1) prints the option as `-j, --hide Launches the app hidden.`, so `-j` is the same flag that commit 8012f90 removed as `--hide` because it can hide an already-open Terminal. Since that commit the launch request has been unchanged. Drop `-j`. `-g` alone keeps the watchdog launch out of the foreground. Before: test_terminal_launch_never_asks_launchservices_to_hide_terminal failed with '-j' in the open argv. After: the request carries `-g` and neither hide spelling. The existing exact-argv test is updated to match. Co-Authored-By: Claude Opus 5.5 --- scripts/auto-reply-session-monitor.py | 4 ++-- tests/test_auto_reply_session_monitor.py | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/scripts/auto-reply-session-monitor.py b/scripts/auto-reply-session-monitor.py index 1303f8a9..4c7739ff 100755 --- a/scripts/auto-reply-session-monitor.py +++ b/scripts/auto-reply-session-monitor.py @@ -23,11 +23,11 @@ LAUNCH_COOLDOWN_NS = 60 * 1_000_000_000 OPEN_TIMEOUT_SECONDS = 5.0 # Keep the new watchdog launch out of the foreground without hiding an already -# open Terminal application or its existing windows. +# open Terminal application or its existing windows. Never pass `-j`: open(1) +# spells it `-j, --hide`, and it hides a Terminal that is already running. BACKGROUND_OPEN_ARGV = ( "/usr/bin/open", "-g", - "-j", "-b", "com.apple.Terminal", ) diff --git a/tests/test_auto_reply_session_monitor.py b/tests/test_auto_reply_session_monitor.py index c58e3dfb..ae651dcb 100644 --- a/tests/test_auto_reply_session_monitor.py +++ b/tests/test_auto_reply_session_monitor.py @@ -70,7 +70,6 @@ def runner(argv, **kwargs): [ "/usr/bin/open", "-g", - "-j", "-b", "com.apple.Terminal", str(command), @@ -94,6 +93,25 @@ def runner(argv, **kwargs): self.assertEqual(status["launches_unix_ns"], [1_000_000_000_000]) self.assertEqual(status_path.stat().st_mode & 0o777, 0o600) + def test_terminal_launch_never_asks_launchservices_to_hide_terminal(self): + # open(1) documents `-j, --hide Launches the app hidden.`; both + # spellings hide an already-running Terminal and its windows. + module = load(MONITOR, "session_monitor_no_hide") + with tempfile.TemporaryDirectory() as temporary: + state, command, manifest, _ = self.fixture(Path(temporary)) + calls = [] + + def runner(argv, **_kwargs): + calls.append(list(argv)) + return subprocess.CompletedProcess(argv, 0, b"", b"") + + module.run_once(manifest, state, now_ns=1_500_000_000_000, runner=runner) + open_call = next(argv for argv in calls if argv[0] == "/usr/bin/open") + self.assertIn("-g", open_call) + self.assertNotIn("-j", open_call) + self.assertNotIn("--hide", open_call) + self.assertEqual(open_call[-1], str(command)) + def test_held_watchdog_lock_never_opens_terminal(self): module = load(MONITOR, "session_monitor_lock") with tempfile.TemporaryDirectory() as temporary: From 6b71f5b167c6b8aada874ded2c8a529686aa4017 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:51:15 +0900 Subject: [PATCH 41/67] fix(session-monitor): never script Terminal windows matched only by title After a successful launch the monitor ran an osascript that walked every Terminal window, picked those whose title contained "start-auto-reply-session.command", closed the idle ones and miniaturized the busy ones. The monitor only asks LaunchServices to open a document; it never learns which window that created, so a user's own window showing the same file name (a manual session, an editor on the file, a finished run kept for its output) would be closed or hidden. Terminal's scripting dictionary defines `busy` on tab, not window, so the script's `busy of w` most likely errored inside its `try` and never acted. One "fix" to that line would have turned on title-only closing, so remove the window-scripting step instead of repairing it. The launch stays in the background through `open -g`. Before: test_launch_never_scripts_terminal_windows saw ['/usr/bin/open', '/usr/bin/osascript']. After: only the open request. Co-Authored-By: Claude Opus 5.5 --- scripts/auto-reply-session-monitor.py | 39 ------------------------ tests/test_auto_reply_session_monitor.py | 26 +++++++++++----- 2 files changed, 19 insertions(+), 46 deletions(-) diff --git a/scripts/auto-reply-session-monitor.py b/scripts/auto-reply-session-monitor.py index 4c7739ff..8be6b813 100755 --- a/scripts/auto-reply-session-monitor.py +++ b/scripts/auto-reply-session-monitor.py @@ -31,26 +31,6 @@ "-b", "com.apple.Terminal", ) -BACKGROUND_DEMOTE_WATCHDOG_ARGV = ( - "/usr/bin/osascript", - "-e", - ( - 'tell application "Terminal"\n' - "repeat with w in (get windows)\n" - "try\n" - "set wn to name of w as text\n" - 'if wn contains "start-auto-reply-session.command" then\n' - "if (busy of w) is false then\n" - "close w saving no\n" - "else\n" - "set miniaturized of w to true\n" - "end if\n" - "end if\n" - "end try\n" - "end repeat\n" - "end tell" - ), -) class MonitorError(RuntimeError): @@ -365,25 +345,6 @@ def run_once( else: returncode = int(result.returncode) reason = "" if returncode == 0 else "open_failed" - # Miniaturize only the watchdog .command window. Never hide - # the user's existing interactive Terminal process. - if returncode == 0: - try: - runner( - list(BACKGROUND_DEMOTE_WATCHDOG_ARGV), - stdin=subprocess.DEVNULL, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - env={ - "HOME": str(Path.home()), - "PATH": "/usr/bin:/bin", - "TMPDIR": "/tmp", - }, - timeout=OPEN_TIMEOUT_SECONDS, - check=False, - ) - except (OSError, subprocess.TimeoutExpired, TypeError): - pass _atomic_status( status_path, _status( diff --git a/tests/test_auto_reply_session_monitor.py b/tests/test_auto_reply_session_monitor.py index ae651dcb..5066217e 100644 --- a/tests/test_auto_reply_session_monitor.py +++ b/tests/test_auto_reply_session_monitor.py @@ -64,7 +64,7 @@ def runner(argv, **kwargs): module.run_once(manifest, state, now_ns=1_000_000_000_000, runner=runner), 0, ) - self.assertGreaterEqual(len(calls), 1) + self.assertEqual(len(calls), 1) self.assertEqual( calls[0][0], [ @@ -75,12 +75,6 @@ def runner(argv, **kwargs): str(command), ], ) - if len(calls) > 1: - self.assertEqual(calls[1][0][:2], ["/usr/bin/osascript", "-e"]) - self.assertIn("start-auto-reply-session.command", calls[1][0][2]) - self.assertIn("close w saving no", calls[1][0][2]) - self.assertIn("busy of w", calls[1][0][2]) - self.assertNotIn("visible of process", calls[1][0][2]) self.assertIs(calls[0][1]["stdin"], subprocess.DEVNULL) self.assertEqual(calls[0][1]["timeout"], module.OPEN_TIMEOUT_SECONDS) self.assertEqual( @@ -112,6 +106,24 @@ def runner(argv, **_kwargs): self.assertNotIn("--hide", open_call) self.assertEqual(open_call[-1], str(command)) + def test_launch_never_scripts_terminal_windows(self): + # The monitor cannot tell its watchdog window from a user window with + # the same title, so it must never close or miniaturize any window. + module = load(MONITOR, "session_monitor_no_window_scripting") + with tempfile.TemporaryDirectory() as temporary: + state, _, manifest, _ = self.fixture(Path(temporary)) + calls = [] + + def runner(argv, **_kwargs): + calls.append(list(argv)) + return subprocess.CompletedProcess(argv, 0, b"", b"") + + self.assertEqual( + module.run_once(manifest, state, now_ns=1_600_000_000_000, runner=runner), + 0, + ) + self.assertEqual([argv[0] for argv in calls], ["/usr/bin/open"]) + def test_held_watchdog_lock_never_opens_terminal(self): module = load(MONITOR, "session_monitor_lock") with tempfile.TemporaryDirectory() as temporary: From ca9d47c9ffc95f8548292c1b7b185b160f4d809d Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:54:04 +0900 Subject: [PATCH 42/67] refactor(dream-rsi): build every paper report from one attempt record collect_paper_report and collect_orx_paper_report spelled out the same ten-field report 25 times, each call repeating the requested CLI, the resolved CLI, the query, the provider and the isolation flag. A small _PaperAttempt record now holds what a provider attempt knows so far and builds both the unavailable and the verified report from it, so the report shape exists once. The alphaXiv flow moves out of the provider dispatcher into _collect_alphaxiv_paper_report, the ORX header check becomes _verified_orx_summary, and the DPO and runtime-probe rejections share a local invalid() helper. _run_alphaxiv is renamed _run_cli because the ORX provider runs through it as well. Co-Authored-By: Claude Fable 5.2 --- scripts/dream_rsi_alphaxiv.py | 554 ++++++++++++---------------------- 1 file changed, 192 insertions(+), 362 deletions(-) diff --git a/scripts/dream_rsi_alphaxiv.py b/scripts/dream_rsi_alphaxiv.py index ddaf2545..e6a7a9e2 100644 --- a/scripts/dream_rsi_alphaxiv.py +++ b/scripts/dream_rsi_alphaxiv.py @@ -25,6 +25,7 @@ import tempfile import time import unicodedata +from dataclasses import dataclass, field from pathlib import Path from typing import Any, Sequence @@ -138,12 +139,8 @@ def _sanitize_stderr(value: Any) -> str: try: if isinstance(value, bytes): text = value.decode("utf-8", errors="replace") - elif isinstance(value, str): - text = value - elif value is None: - text = "" else: - text = str(value) + text = "" if value is None else str(value) sanitized = [] for character in text: category = unicodedata.category(character) @@ -159,7 +156,7 @@ def _sanitize_stderr(value: Any) -> str: return "" -def _run_alphaxiv( +def _run_cli( executable: str, args: Sequence[str], *, @@ -336,34 +333,73 @@ def _summary_text(payload: Any) -> str | None: return text -def _paper_unavailable( - *, - requested_cli: str, - resolved_cli: str | None, - query: str, - reason: str, - provider: str = "alphaxiv_cli", - commands: list[dict[str, Any]] | None = None, - selected_paper: dict[str, str] | None = None, - isolated_context: bool = True, -) -> dict[str, Any]: - return { - "status": "unavailable", - "reason": reason, - "analysis_available": False, - "provider": provider, - "query": query, - "selection_method": "none", - "selected_paper": selected_paper, - "evidence": None, - "string_similarity_used": False, - "cli": { - "requested": requested_cli, - "resolved": resolved_cli, - "isolated_context": isolated_context, - "commands": commands or [], - }, - } +@dataclass +class _PaperAttempt: + """What one provider attempt knows so far; every paper report is built from it.""" + + provider: str + isolated_context: bool + requested_cli: str + query: str + resolved_cli: str | None = None + selected_paper: dict[str, str] | None = None + commands: list[dict[str, Any]] = field(default_factory=list) + + def unavailable(self, reason: str) -> dict[str, Any]: + return self._report("unavailable", reason, selection_method="none", evidence=None) + + def verified( + self, reason: str, *, selection_method: str, evidence: dict[str, str] + ) -> dict[str, Any]: + return self._report("ok", reason, selection_method=selection_method, evidence=evidence) + + def _report( + self, + status: str, + reason: str, + *, + selection_method: str, + evidence: dict[str, str] | None, + ) -> dict[str, Any]: + return { + "status": status, + "reason": reason, + "analysis_available": evidence is not None, + "provider": self.provider, + "query": self.query, + "selection_method": selection_method, + "selected_paper": self.selected_paper, + "evidence": evidence, + "string_similarity_used": False, + "cli": { + "requested": self.requested_cli, + "resolved": self.resolved_cli, + "isolated_context": self.isolated_context, + "commands": self.commands, + }, + } + + +def _verified_orx_summary(report_text: str, paper_id: str) -> tuple[str | None, str]: + """Return the report body, or the reason its alphaXiv header cannot be trusted.""" + lines = report_text.splitlines() + header_index = next( + (index for index, line in enumerate(lines) if line.strip()), + None, + ) + if header_index is None: + return None, "incomplete_summary_response" + identity = re.compile( + rf"^alphaXiv:\s*https?://(?:www\.)?alphaxiv\.org/abs/{re.escape(paper_id)}(?:v\d+)?\s*$" + ) + if not identity.fullmatch(lines[header_index].strip()): + return None, "identity_mismatch" + summary = "\n".join(lines[header_index + 1 :]).strip() + if not summary: + return None, "incomplete_summary_response" + if len(summary) > MAX_SUMMARY_CHARS: + return None, "text_too_long" + return summary, "" def collect_orx_paper_report( @@ -374,56 +410,34 @@ def collect_orx_paper_report( paper_id: str = DREAM_RSI_PAPER_ID, ) -> dict[str, Any]: """Collect one identity-verified alphaXiv report through OpenResearch.""" - requested_cli = str(orx_cli) + attempt = _PaperAttempt( + provider="orx_cli", + isolated_context=False, + requested_cli=str(orx_cli), + query=str(query)[:MAX_QUERY_CHARS], + ) try: - safe_query = _validate_query(query) + attempt.query = _validate_query(query) except ValueError as exc: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=None, - query=str(query)[:MAX_QUERY_CHARS], - reason=f"invalid_query:{exc}", - provider="orx_cli", - isolated_context=False, - ) + return attempt.unavailable(f"invalid_query:{exc}") try: selected_id = _validate_paper_id(paper_id) except ValueError: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=None, - query=safe_query, - reason="invalid_paper_id", - provider="orx_cli", - isolated_context=False, - ) + return attempt.unavailable("invalid_paper_id") try: timeout_value = float(timeout) except (OverflowError, TypeError, ValueError): timeout_value = float("nan") if not math.isfinite(timeout_value): - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=None, - query=safe_query, - reason="invalid_timeout", - provider="orx_cli", - isolated_context=False, - ) + return attempt.unavailable("invalid_timeout") timeout_value = min(MAX_TIMEOUT_SECONDS, max(0.1, timeout_value)) executable, resolve_error = _resolve_cli(orx_cli) if resolve_error or not executable: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=None, - query=safe_query, - reason=resolve_error or "cli_missing", - provider="orx_cli", - isolated_context=False, - ) + return attempt.unavailable(resolve_error or "cli_missing") + attempt.resolved_cli = executable - report_text, command = _run_alphaxiv( + report_text, command = _run_cli( executable, ("paper", selected_id, "--source", "alphaxiv", "--no-telemetry"), timeout=timeout_value, @@ -431,100 +445,27 @@ def collect_orx_paper_report( expect_json=False, operation="orx_paper", ) - commands = [command] - selected = { + attempt.commands.append(command) + attempt.selected_paper = { "paper_id": selected_id, "title": "", "url": f"https://www.alphaxiv.org/abs/{selected_id}", } if report_text is None: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason=str(command.get("reason") or "paper_unavailable"), - provider="orx_cli", - isolated_context=False, - commands=commands, - selected_paper=selected, - ) - - lines = report_text.splitlines() - header_index = next( - (index for index, line in enumerate(lines) if line.strip()), - None, - ) - if header_index is None: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason="incomplete_summary_response", - provider="orx_cli", - isolated_context=False, - commands=commands, - selected_paper=selected, - ) - identity = re.compile( - rf"^alphaXiv:\s*https?://(?:www\.)?alphaxiv\.org/abs/{re.escape(selected_id)}(?:v\d+)?\s*$" - ) - if not identity.fullmatch(lines[header_index].strip()): - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason="identity_mismatch", - provider="orx_cli", - isolated_context=False, - commands=commands, - selected_paper=selected, - ) - - summary = "\n".join(lines[header_index + 1 :]).strip() - if not summary: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason="incomplete_summary_response", - provider="orx_cli", - isolated_context=False, - commands=commands, - selected_paper=selected, - ) - if len(summary) > MAX_SUMMARY_CHARS: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason="text_too_long", - provider="orx_cli", - isolated_context=False, - commands=commands, - selected_paper=selected, - ) - - return { - "status": "ok", - "reason": "orx_report_verified", - "analysis_available": True, - "provider": "orx_cli", - "query": safe_query, - "selection_method": "direct_paper_id", - "selected_paper": selected, - "evidence": { + return attempt.unavailable(str(command.get("reason") or "paper_unavailable")) + + summary, failure = _verified_orx_summary(report_text, selected_id) + if summary is None: + return attempt.unavailable(failure) + return attempt.verified( + "orx_report_verified", + selection_method="direct_paper_id", + evidence={ "kind": "orx_alphaxiv_paper_report", "summary": summary, "source_operation": "orx_paper", }, - "string_similarity_used": False, - "cli": { - "requested": requested_cli, - "resolved": executable, - "isolated_context": False, - "commands": commands, - }, - } + ) def collect_paper_report( @@ -537,6 +478,18 @@ def collect_paper_report( paper_id: str | None = None, ) -> dict[str, Any]: """Collect deterministic paper evidence through one selected provider.""" + if paper_source == "auto": + if _resolve_cli(alphaxiv_cli)[0]: + paper_source = "alphaxiv" + elif _resolve_cli(orx_cli)[0]: + paper_source = "orx" + else: + return _PaperAttempt( + provider="orx_cli", + isolated_context=False, + requested_cli=str(orx_cli), + query=str(query)[:MAX_QUERY_CHARS], + ).unavailable("cli_missing") if paper_source == "orx": return collect_orx_paper_report( query, @@ -544,229 +497,116 @@ def collect_paper_report( timeout=timeout, paper_id=paper_id or DREAM_RSI_PAPER_ID, ) - if paper_source == "auto": - executable, _ = _resolve_cli(alphaxiv_cli) - if executable: - return collect_paper_report( - query, - alphaxiv_cli=alphaxiv_cli, - orx_cli=orx_cli, - paper_source="alphaxiv", - timeout=timeout, - paper_id=paper_id, - ) - orx_executable, _ = _resolve_cli(orx_cli) - if orx_executable: - return collect_orx_paper_report( - query, - orx_cli=orx_cli, - timeout=timeout, - paper_id=paper_id or DREAM_RSI_PAPER_ID, - ) - return _paper_unavailable( - requested_cli=str(orx_cli), - resolved_cli=None, - query=str(query)[:MAX_QUERY_CHARS], - reason="cli_missing", - provider="orx_cli", - isolated_context=False, - ) - if paper_source != "alphaxiv": - return _paper_unavailable( - requested_cli=str(alphaxiv_cli), - resolved_cli=None, - query=str(query)[:MAX_QUERY_CHARS], - reason="invalid_paper_source", + if paper_source == "alphaxiv": + return _collect_alphaxiv_paper_report( + query, alphaxiv_cli=alphaxiv_cli, timeout=timeout, paper_id=paper_id ) + return _PaperAttempt( + provider="alphaxiv_cli", + isolated_context=True, + requested_cli=str(alphaxiv_cli), + query=str(query)[:MAX_QUERY_CHARS], + ).unavailable("invalid_paper_source") + - requested_cli = str(alphaxiv_cli) +def _collect_alphaxiv_paper_report( + query: str, + *, + alphaxiv_cli: str, + timeout: float, + paper_id: str | None, +) -> dict[str, Any]: + """Select a paper, then verify its identity and summary in an isolated CLI home.""" + attempt = _PaperAttempt( + provider="alphaxiv_cli", + isolated_context=True, + requested_cli=str(alphaxiv_cli), + query=str(query)[:MAX_QUERY_CHARS], + ) try: - safe_query = _validate_query(query) + safe_query = attempt.query = _validate_query(query) except ValueError as exc: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=None, - query=str(query)[:MAX_QUERY_CHARS], - reason=f"invalid_query:{exc}", - ) + return attempt.unavailable(f"invalid_query:{exc}") try: timeout_value = float(timeout) except (OverflowError, TypeError, ValueError): timeout_value = -1.0 if not (0.1 <= timeout_value <= MAX_TIMEOUT_SECONDS): - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=None, - query=safe_query, - reason="invalid_timeout", - ) + return attempt.unavailable("invalid_timeout") executable, resolve_error = _resolve_cli(alphaxiv_cli) if resolve_error or not executable: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=None, - query=safe_query, - reason=resolve_error or "cli_missing", - ) + return attempt.unavailable(resolve_error or "cli_missing") + attempt.resolved_cli = executable - commands: list[dict[str, Any]] = [] - selected: dict[str, str] | None = None - selection_method = "direct_paper_id" if paper_id else "alphaxiv_search_rank_1" with tempfile.TemporaryDirectory(prefix="openkakao-alphaxiv-") as temp_home: env = dict(os.environ) env["ALPHAXIV_HOME"] = temp_home + def run(operation: str, *args: str, expect_json: bool) -> tuple[Any | None, str]: + payload, record = _run_cli( + executable, + args, + timeout=timeout_value, + env=env, + expect_json=expect_json, + operation=operation, + ) + attempt.commands.append(record) + return payload, str(record.get("reason") or "") + if paper_id: try: selected_id = _validate_paper_id(paper_id) except ValueError: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason="invalid_paper_id", - ) + return attempt.unavailable("invalid_paper_id") selected = {"paper_id": selected_id, "title": "", "url": ""} else: - search_payload, search_record = _run_alphaxiv( - executable, - ("search", "papers", safe_query, "--json"), - timeout=timeout_value, - env=env, - expect_json=True, - operation="search_papers", + search_payload, failure = run( + "search_papers", "search", "papers", safe_query, "--json", expect_json=True ) - commands.append(search_record) if search_payload is None: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason=str(search_record.get("reason") or "search_unavailable"), - commands=commands, - ) + return attempt.unavailable(failure or "search_unavailable") papers = _extract_search_papers(search_payload) if not papers: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason="incomplete_search_response", - commands=commands, - ) + return attempt.unavailable("incomplete_search_response") selected = papers[0] + attempt.selected_paper = selected - assert selected is not None - context_payload, context_record = _run_alphaxiv( - executable, - ("context", "use", "paper", selected["paper_id"]), - timeout=timeout_value, - env=env, - expect_json=False, - operation="context_use_paper", + context_payload, failure = run( + "context_use_paper", "context", "use", "paper", selected["paper_id"], expect_json=False ) - commands.append(context_record) if context_payload is None: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason=str(context_record.get("reason") or "context_unavailable"), - commands=commands, - selected_paper=selected, - ) + return attempt.unavailable(failure or "context_unavailable") - shown, show_record = _run_alphaxiv( - executable, - ("context", "show", "--json"), - timeout=timeout_value, - env=env, - expect_json=True, - operation="context_show", - ) - commands.append(show_record) + shown, failure = run("context_show", "context", "show", "--json", expect_json=True) if shown is None: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason=str(show_record.get("reason") or "context_unavailable"), - commands=commands, - selected_paper=selected, - ) + return attempt.unavailable(failure or "context_unavailable") context_id, context_title = _context_identity(shown) if context_id != selected["paper_id"]: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason="paper_identity_mismatch", - commands=commands, - selected_paper=selected, - ) - if not selected["title"]: - selected["title"] = context_title + return attempt.unavailable("paper_identity_mismatch") + selected["title"] = selected["title"] or context_title if not selected["title"]: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason="incomplete_paper_metadata", - commands=commands, - selected_paper=selected, - ) + return attempt.unavailable("incomplete_paper_metadata") - summary_payload, summary_record = _run_alphaxiv( - executable, - ("paper", "summary", "--json"), - timeout=timeout_value, - env=env, - expect_json=True, - operation="paper_summary", + summary_payload, failure = run( + "paper_summary", "paper", "summary", "--json", expect_json=True ) - commands.append(summary_record) if summary_payload is None: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason=str(summary_record.get("reason") or "summary_unavailable"), - commands=commands, - selected_paper=selected, - ) + return attempt.unavailable(failure or "summary_unavailable") summary = _summary_text(summary_payload) if summary is None: - return _paper_unavailable( - requested_cli=requested_cli, - resolved_cli=executable, - query=safe_query, - reason="incomplete_summary_response", - commands=commands, - selected_paper=selected, - ) + return attempt.unavailable("incomplete_summary_response") - return { - "status": "ok", - "reason": "alphaxiv_summary_verified", - "analysis_available": True, - "provider": "alphaxiv_cli", - "query": safe_query, - "selection_method": selection_method, - "selected_paper": selected, - "evidence": { + return attempt.verified( + "alphaxiv_summary_verified", + selection_method="direct_paper_id" if paper_id else "alphaxiv_search_rank_1", + evidence={ "kind": "alphaxiv_paper_summary", "summary": summary, "source_operation": "paper_summary", }, - "string_similarity_used": False, - "cli": { - "requested": requested_cli, - "resolved": executable, - "isolated_context": True, - "commands": commands, - }, - } + ) def _safe_eval_report(report: Any) -> dict[str, Any]: @@ -876,43 +716,29 @@ def build_dpo_provenance(dpo: Any) -> dict[str, Any]: "string_similarity_used": False, "pairs": [], } - pairs = dpo.get("pairs") - if not isinstance(pairs, list) or len(pairs) > MAX_DPO_PAIRS: + + def invalid(reason: str) -> dict[str, Any]: return { "status": "invalid_input", - "reason": "invalid_pairs", + "reason": reason, "method": "response_token_logprob", "string_similarity_used": False, "pairs": [], } + + pairs = dpo.get("pairs") + if not isinstance(pairs, list) or len(pairs) > MAX_DPO_PAIRS: + return invalid("invalid_pairs") try: tokenizer_id = _valid_plain_text(dpo.get("tokenizer_id"), limit=256, allow_empty=True) base_model = _valid_plain_text(dpo.get("base_model"), limit=512, allow_empty=True) beta = float(dpo.get("beta", 0.1)) except (ValueError, TypeError): - return { - "status": "invalid_input", - "reason": "invalid_dpo_metadata", - "method": "response_token_logprob", - "string_similarity_used": False, - "pairs": [], - } + return invalid("invalid_dpo_metadata") if not (0.0 < beta <= 10.0): - return { - "status": "invalid_input", - "reason": "invalid_beta", - "method": "response_token_logprob", - "string_similarity_used": False, - "pairs": [], - } + return invalid("invalid_beta") if any(not isinstance(pair, dict) for pair in pairs): - return { - "status": "invalid_input", - "reason": "invalid_pair", - "method": "response_token_logprob", - "string_similarity_used": False, - "pairs": [], - } + return invalid("invalid_pair") evaluated = evaluate_preference_pairs( pairs, @@ -962,8 +788,12 @@ def build_runtime_model_probe_provenance(probe: Any) -> dict[str, Any]: } if probe is None: return base + + def invalid(reason: str) -> dict[str, Any]: + return {**base, "status": "invalid_input", "reason": reason} + if not isinstance(probe, dict): - return {**base, "status": "invalid_input", "reason": "invalid_runtime_model_probe"} + return invalid("invalid_runtime_model_probe") try: gateway = _valid_plain_text(probe.get("gateway"), limit=2048) @@ -975,25 +805,25 @@ def build_runtime_model_probe_provenance(probe: Any) -> dict[str, Any]: latency_ms = int(probe.get("latency_ms")) timeout_ms = int(probe.get("timeout_ms")) except (TypeError, ValueError): - return {**base, "status": "invalid_input", "reason": "invalid_runtime_model_probe"} + return invalid("invalid_runtime_model_probe") if latency_ms < 0 or timeout_ms <= 0 or latency_ms > timeout_ms: - return {**base, "status": "invalid_input", "reason": "invalid_probe_timing"} + return invalid("invalid_probe_timing") raw_advertised = probe.get("advertised_models", []) if not isinstance(raw_advertised, list) or len(raw_advertised) > MAX_RUNTIME_MODELS: - return {**base, "status": "invalid_input", "reason": "invalid_advertised_models"} + return invalid("invalid_advertised_models") advertised: list[dict[str, Any]] = [] for item in raw_advertised: if not isinstance(item, dict): - return {**base, "status": "invalid_input", "reason": "invalid_advertised_model"} + return invalid("invalid_advertised_model") try: item_model = _valid_plain_text(item.get("model"), limit=512) state = _valid_plain_text(item.get("state"), limit=64) except ValueError: - return {**base, "status": "invalid_input", "reason": "invalid_advertised_model"} + return invalid("invalid_advertised_model") loaded = item.get("loaded") if not isinstance(loaded, bool): - return {**base, "status": "invalid_input", "reason": "invalid_advertised_model"} + return invalid("invalid_advertised_model") advertised.append({"model": item_model, "loaded": loaded, "state": state}) return { From 3690a6dff2118ee69d65d35c09a675bac8e1234c Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:56:27 +0900 Subject: [PATCH 43/67] refactor(scripts): name the voice frame size and flatten two lifecycle checks The 20 ms frame appeared as bare 320, 640 and 639 in the wake frontend and the file pipeline; it is now VOICE_MIC_FRAME_BYTES next to the existing sample count. run_file_pipeline built the same not-accepted report in two places and sliced each wake frame twice. validate_executable asks is_relative_to instead of looping over relative_to with a flag, and the already-running check stops testing the ownership record for None twice. Co-Authored-By: Claude Fable 5.2 --- scripts/jarvis_voice.py | 42 ++++++++++++++++------------------ scripts/mlx_serve_lifecycle.py | 19 +++++---------- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/scripts/jarvis_voice.py b/scripts/jarvis_voice.py index dbb3ab06..e45bacb7 100644 --- a/scripts/jarvis_voice.py +++ b/scripts/jarvis_voice.py @@ -55,6 +55,7 @@ VOICE_MIC_POLL_SECONDS = 0.02 VOICE_MIC_STALE_SECONDS = 15.0 VOICE_MIC_FRAME_SAMPLES = 320 +VOICE_MIC_FRAME_BYTES = VOICE_MIC_FRAME_SAMPLES * 2 # The last bounded host run began with 1.65 GiB of swap headroom and crossed # the 512 MiB emergency stop while a voice model stage was still running. # Keep 2 GiB free before starting either large local voice model, plus enough @@ -461,7 +462,7 @@ def _wake_input(pcm16: bytes) -> Any: try: import numpy as np - return np.frombuffer(pcm16, dtype=np.int16, count=320) + return np.frombuffer(pcm16, dtype=np.int16, count=VOICE_MIC_FRAME_SAMPLES) except Exception: samples = array("h") samples.frombytes(pcm16) @@ -470,7 +471,7 @@ def _wake_input(pcm16: bytes) -> Any: def analyze(self, pcm16: bytes) -> AudioFrameAnalysis: # WebRTC VAD accepts 10/20/30 ms frames. A 20 ms frame at 16 kHz is # 320 int16 samples / 640 bytes; callers must keep this boundary. - if len(pcm16) != 640: + if len(pcm16) != VOICE_MIC_FRAME_BYTES: raise ValueError("voice_frame_size_invalid") wake_input = self._wake_input(pcm16) stock = self._score(self.stock_model.predict(wake_input)) @@ -1196,13 +1197,25 @@ def speak(self, text: str, speak_token: AbortToken) -> None: accepted = False stock_max = 0.0 custom_max = 0.0 - for offset in range(0, len(wake_pcm) - 639, 640): - analysis = frontend.analyze(wake_pcm[offset : offset + 640]) + + def not_accepted(result: VoiceResult) -> dict[str, Any]: + return { + "accepted": False, + "state": result.state.value, + "error_code": result.error_code, + "stock_max": stock_max, + "custom_max": custom_max, + "custom_model_selected": selected is not None, + } + + for offset in range(0, len(wake_pcm) - VOICE_MIC_FRAME_BYTES + 1, VOICE_MIC_FRAME_BYTES): + frame = wake_pcm[offset : offset + VOICE_MIC_FRAME_BYTES] + analysis = frontend.analyze(frame) stock_max = max(stock_max, analysis.stock_wake_score) if analysis.custom_wake_score is not None: custom_max = max(custom_max, analysis.custom_wake_score) result = pipeline.feed_audio( - wake_pcm[offset : offset + 640], + frame, rms=analysis.rms, speech=analysis.speech, stock_wake_score=analysis.stock_wake_score, @@ -1212,24 +1225,9 @@ def speak(self, text: str, speak_token: AbortToken) -> None: accepted = True break if result is not None: - return { - "accepted": False, - "state": result.state.value, - "error_code": result.error_code, - "stock_max": stock_max, - "custom_max": custom_max, - "custom_model_selected": selected is not None, - } + return not_accepted(result) if not accepted: - ended = pipeline._end(VoiceState.ENDED, "wake_miss") - return { - "accepted": False, - "state": ended.state.value, - "error_code": ended.error_code, - "stock_max": stock_max, - "custom_max": custom_max, - "custom_model_selected": selected is not None, - } + return not_accepted(pipeline._end(VoiceState.ENDED, "wake_miss")) utterance = _load_wav_pcm16(utterance_wav) voice = pipeline.process_utterance(utterance) diff --git a/scripts/mlx_serve_lifecycle.py b/scripts/mlx_serve_lifecycle.py index fa27dd7f..fd2f1ddf 100644 --- a/scripts/mlx_serve_lifecycle.py +++ b/scripts/mlx_serve_lifecycle.py @@ -316,15 +316,7 @@ def validate_executable( home_root = root.resolve(strict=False) except OSError: return "launch_executable_unsafe" - allowed = False - for base in (home_root, Path("/Applications")): - try: - resolved.relative_to(base) - allowed = True - break - except ValueError: - continue - if not allowed: + if not any(resolved.is_relative_to(base) for base in (home_root, Path("/Applications"))): return "launch_executable_unsafe" try: metadata = path.lstat() @@ -758,10 +750,11 @@ def fail(reason: str) -> LaunchResult: listeners = runtime.listeners(spec.port) if listeners: record = read_app_owned_state(state_root) - attested = record is not None and attest_app_owned_server( - record, hooks=runtime, spec=spec - ) == "" - if attested and record is not None and record.pid in listeners: + if ( + record is not None + and attest_app_owned_server(record, hooks=runtime, spec=spec) == "" + and record.pid in listeners + ): return LaunchResult( True, LaunchStage.ALREADY_RUNNING, From 7d9635d4040dadbe81575c68411ac760bc7083d2 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:40:33 +0900 Subject: [PATCH 44/67] fix: never reapply a message that may already have been sent After a submission, `verify_submit_and_escalate_once` waited up to two seconds for the composer to stop holding the outbound text. If the text was still there, it wrote the text again and pressed Return a second time. Nothing read the composer between the last poll and that write. When KakaoTalk processed the first submission in that gap, the composer was empty, the write staged a second copy, and the escalated Return sent the message twice. The same gap let the write replace a draft that a person typed after the last poll. The state machine now reads the composer immediately before the write. It writes and presses Return again only if that read returns the exact outbound text. An empty composer means that the first submission landed, so the send ends without another submission. A changed or unreadable composer ends the send with an error and is left alone. `wait_for_composer_clear` returned true for an unreadable composer, as if the composer had cleared. The function is now `outbound_text_stayed_staged` and reports only the fact that permits an escalation. Its behavior is unchanged. An unreadable composer after a submission still ends the send without another submission. Co-Authored-By: Claude Fable 5.2 --- src/ax_send.rs | 161 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 142 insertions(+), 19 deletions(-) diff --git a/src/ax_send.rs b/src/ax_send.rs index 1a88880d..d662424a 100644 --- a/src/ax_send.rs +++ b/src/ax_send.rs @@ -857,21 +857,23 @@ fn submit_clear_window(default: std::time::Duration) -> std::time::Duration { } } -/// Wait until the composer stops holding the exact outbound text. Returns -/// true when the field cleared (delivery proven) and false when the exact -/// text survived the whole window (non-delivery proven). -fn wait_for_composer_clear( +/// Watch the composer after a submission. Returns true only when every read +/// through the window returned the exact outbound text, which proves +/// non-delivery. Any other read ends the watch with false. An empty composer +/// proves delivery. A changed or unreadable one proves nothing, so it must +/// never lead to another submission. +fn outbound_text_stayed_staged( read: &mut impl FnMut() -> Option, message: &str, window: std::time::Duration, ) -> bool { let deadline = std::time::Instant::now() + submit_clear_window(window); loop { - if !matches!(read().as_deref(), Some(value) if value == message) { - return true; + if read().as_deref() != Some(message) { + return false; } if std::time::Instant::now() >= deadline { - return false; + return true; } std::thread::sleep(SUBMIT_CLEAR_POLL); } @@ -894,11 +896,12 @@ where } } -/// Shared submission tail: press once, prove delivery by the composer -/// clearing, and when the exact outbound text survived the whole window, -/// reapply it and submit exactly one more time through a bare Return that -/// skips the 전송 button which just proved inert. A second failure is -/// terminal — the worker records accepted_unconfirmed without retransmitting. +/// The shared submission tail presses once, then watches the composer. Only +/// the exact outbound text surviving the whole window, and still sitting +/// there on a read taken immediately before the write, permits one reapply +/// and one bare Return that skips the 전송 button which just proved inert. A +/// second failure is terminal. The worker records accepted_unconfirmed +/// without retransmitting. #[allow(clippy::too_many_arguments)] fn verify_submit_and_escalate_once( message: &str, @@ -925,10 +928,20 @@ where } focus()?; press_return()?; - if wait_for_composer_clear(read, message, SUBMIT_CLEAR_WINDOW) { + if !outbound_text_stayed_staged(read, message, SUBMIT_CLEAR_WINDOW) { return Ok(()); } focus()?; + // The first submission can still land after the window closed. Writing + // the text back then stages a second copy for the escalated Return to + // send, so the last step before the write is one more read. + match read().as_deref() { + Some(value) if value == message => {} + Some("") => return Ok(()), + _ => anyhow::bail!( + "message stayed in the composer after submit, then changed or became unreadable; refusing to reapply it or press Return again" + ), + } begin_mutation(); if !set_value() && read().as_deref() != Some(message) { anyhow::bail!( @@ -940,13 +953,12 @@ where anyhow::bail!("message composer changed before the escalated Return"); } escalate_return()?; - if wait_for_composer_clear(read, message, ESCALATE_CLEAR_WINDOW) { - Ok(()) - } else { + if outbound_text_stayed_staged(read, message, ESCALATE_CLEAR_WINDOW) { anyhow::bail!( "message stayed in the composer after submit and one Return escalation; not retrying further" ) } + Ok(()) } #[allow( clippy::too_many_arguments, @@ -1240,6 +1252,76 @@ mod match_tests { (result, probe) } + /// A composer that behaves like KakaoTalk's. A write changes what later + /// reads return, and a submission sends whatever text is staged. + #[derive(Debug, Default)] + struct FakeComposer { + text: String, + unreadable: bool, + /// The first submission is processed right after the next read, as a + /// busy KakaoTalk does. Otherwise it is a silent no-op. + first_submit_lands_late: bool, + /// A human replaces the content with "human draft" right after the + /// first read that follows the first submission. + human_draft_after_submit: bool, + human_draft_unreadable: bool, + delivered: Vec, + submitted: bool, + submit_pending: bool, + } + + impl FakeComposer { + fn read(&mut self) -> Option { + let seen = (!self.unreadable).then(|| self.text.clone()); + if std::mem::take(&mut self.submit_pending) { + self.deliver(); + } + if self.submitted && std::mem::take(&mut self.human_draft_after_submit) { + self.text = "human draft".to_string(); + self.unreadable = self.human_draft_unreadable; + } + seen + } + + fn write(&mut self, message: &str) -> bool { + self.text = message.to_string(); + true + } + + fn submit(&mut self) { + self.submitted = true; + self.submit_pending = self.first_submit_lands_late; + } + + fn deliver(&mut self) { + if !self.text.is_empty() { + self.delivered.push(std::mem::take(&mut self.text)); + } + } + } + + fn send_reply_through(composer: FakeComposer) -> (anyhow::Result<()>, FakeComposer) { + let composer = std::cell::RefCell::new(composer); + let result = guarded_composer_send_once( + "reply", + || composer.borrow_mut().read(), + |_| Ok(()), + || Ok(()), + || {}, + || composer.borrow_mut().write("reply"), + || Ok(()), + || { + composer.borrow_mut().submit(); + Ok(()) + }, + || { + composer.borrow_mut().deliver(); + Ok(()) + }, + ); + (result, composer.into_inner()) + } + #[test] fn empty_list_is_not_found() { assert_eq!(match_chat_row(&[], "Alice"), ChatMatch::NotFound); @@ -2415,6 +2497,18 @@ mod match_tests { assert_eq!(probe.escalates, 0); } + #[test] + fn composer_guard_never_resubmits_when_the_composer_is_unreadable_after_submit() { + let (result, probe) = run_composer_probe( + [Some(""), Some(""), Some("reply"), Some("reply"), None], + true, + ); + assert!(result.is_ok()); + assert_eq!(probe.set_attempts, 1); + assert_eq!(probe.returns, 1); + assert_eq!(probe.escalates, 0); + } + #[test] fn composer_guard_escalates_once_when_first_submit_leaves_text_staged() { // The 전송 press is a silent no-op: every read keeps returning the @@ -2428,6 +2522,7 @@ mod match_tests { Some("reply"), Some("reply"), Some("reply"), + Some("reply"), ], true, ); @@ -2441,8 +2536,9 @@ mod match_tests { #[test] fn composer_guard_aborts_escalation_when_composer_changes() { - // The first submit leaves the text staged, but before the escalated - // reapply lands a concurrent edit replaces it — refuse to touch it. + // The first submit leaves the text staged and the reapply goes ahead, + // but the composer no longer holds the text when the escalated Return + // is due, so the Return is never pressed. let (result, probe) = run_composer_probe( [ Some(""), @@ -2450,7 +2546,7 @@ mod match_tests { Some("reply"), Some("reply"), Some("reply"), - Some(""), + Some("reply"), Some(""), ], true, @@ -2464,6 +2560,33 @@ mod match_tests { assert_eq!(probe.set_attempts, 2); } + #[test] + fn composer_guard_delivers_once_when_the_first_submit_lands_after_the_proof_window() { + let (result, composer) = send_reply_through(FakeComposer { + first_submit_lands_late: true, + ..FakeComposer::default() + }); + assert_eq!(composer.delivered, ["reply"]); + assert!(result.is_ok(), "{result:?}"); + } + + #[test] + fn composer_guard_leaves_a_draft_typed_after_the_proof_window_untouched() { + for human_draft_unreadable in [false, true] { + let (result, composer) = send_reply_through(FakeComposer { + human_draft_after_submit: true, + human_draft_unreadable, + ..FakeComposer::default() + }); + assert_eq!( + composer.text, "human draft", + "draft unreadable: {human_draft_unreadable}" + ); + assert!(composer.delivered.is_empty()); + assert!(result.is_err()); + } + } + #[test] fn kakao_send_button_labels_match_korean_and_english() { assert!(is_kakao_send_button_label("전송")); From e14458e5550b30059668f57149ff92129d3f61b6 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:46:40 +0900 Subject: [PATCH 45/67] fix: require a readable empty composer before writing a message `guarded_composer_send_once` treated a composer that Accessibility could not read as empty and wrote the outbound text into it. A draft that a person was typing in that composer was replaced. `guarded_composer_preflight` reported the same composer as ready. Both now need a read that returns the empty string. The send path still focuses the composer and reads once more when the first read fails, and it still replaces this pipeline's own leftover draft. If the composer stays unreadable, the send stops before any mutation, so the worker can requeue it. The preflight error for an unreadable composer avoids the "composer is not empty" wording that `local-send` reports as `composer_occupied`. `composer_guard_treats_unreadable_value_as_empty_after_focus` asserted the old behavior. Its third read, the one immediately before the write, was unreadable. The renamed test keeps the path that focuses and reads again, and makes that third read return the empty string. Co-Authored-By: Claude Fable 5.2 --- src/ax_send.rs | 110 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 83 insertions(+), 27 deletions(-) diff --git a/src/ax_send.rs b/src/ax_send.rs index d662424a..12f312f8 100644 --- a/src/ax_send.rs +++ b/src/ax_send.rs @@ -776,12 +776,6 @@ fn extend_unique(target: &mut Vec, candidates: impl IntoIterato } } -/// Drive one composer submission through a fail-closed, single-Return state -/// machine. The callbacks keep the policy testable without a live AX session: -/// an unreadable composer is treated as empty after focus, a foreign draft is -/// never overwritten, an already-staged exact outbound draft is submitted -/// without rewriting it, every successful write is re-read exactly, and a -/// Return is never retried. #[cfg_attr(not(target_os = "macos"), allow(dead_code))] #[allow( clippy::too_many_arguments, @@ -960,6 +954,13 @@ where } Ok(()) } + +/// Drive one composer submission through a fail-closed state machine. The +/// callbacks keep the policy testable without a live AX session. A write needs +/// a read that proves the composer is empty or holds this pipeline's own +/// leftover draft, so an unreadable composer and a foreign draft are both left +/// alone. Every write is re-read exactly before the submission, which +/// `verify_submit_and_escalate_once` escalates at most once. #[allow( clippy::too_many_arguments, reason = "separate composer callbacks make every AX side effect and the mutation boundary independently testable" @@ -992,7 +993,10 @@ where initial = read(); } match initial.as_deref() { - Some("") | None => {} + Some("") => {} + None => anyhow::bail!( + "message composer in the selected window is unreadable; refusing to overwrite it" + ), Some(value) if composer_equivalent(value, message) => { // A prior accepted_unconfirmed local-send can leave the exact // outbound text sitting uncommitted. Re-apply that same value so @@ -1042,15 +1046,15 @@ where // Re-attest both the window and the empty value immediately before the // first text mutation. This catches a human draft started after the first - // inspection without replacing any of it. + // inspection without replacing any of it. An unreadable composer could + // hide that draft, so it does not pass as empty. attest("before composer write")?; match read().as_deref() { - Some("") | None => {} - Some(value) - if is_permitted_leftover(value, message) => {} - Some(value) if value == message => { - anyhow::bail!("message composer changed before write; refusing to overwrite it") - } + Some("") => {} + Some(value) if is_permitted_leftover(value, message) => {} + None => anyhow::bail!( + "message composer became unreadable before write; refusing to overwrite it" + ), Some(_) => anyhow::bail!("message composer changed before write; refusing to overwrite it"), } @@ -1104,8 +1108,9 @@ where /// Read-only counterpart to `guarded_composer_send_once`. The first /// attestation binds the field inspection to the expected exact-title window; /// the second proves that the same unique window is still present after the -/// inspection. Reading the field twice also fails closed if a human begins a -/// draft during the probe. There are intentionally no focus, write, typing, or +/// inspection. Both reads must return an empty composer, so an unreadable one +/// fails the probe, and reading twice also fails closed if a human begins a +/// draft during it. There are intentionally no focus, write, typing, or /// keyboard callbacks in this API. #[cfg_attr(not(target_os = "macos"), allow(dead_code))] fn guarded_composer_preflight( @@ -1118,13 +1123,17 @@ where { attest("before preflight composer inspection")?; match read().as_deref() { - Some("") | None => {} + Some("") => {} + None => anyhow::bail!("message composer is unreadable; preflight is unavailable"), Some(_) => anyhow::bail!("message composer is not empty; preflight is unavailable"), } attest("after preflight composer inspection")?; match read().as_deref() { - Some("") | None => {} + Some("") => {} + None => anyhow::bail!( + "message composer became unreadable during preflight; preflight is unavailable" + ), Some(_) => { anyhow::bail!("message composer changed during preflight; preflight is unavailable") } @@ -1262,10 +1271,13 @@ mod match_tests { /// busy KakaoTalk does. Otherwise it is a silent no-op. first_submit_lands_late: bool, /// A human replaces the content with "human draft" right after the - /// first read that follows the first submission. + /// first read, or right after the first read that follows the first + /// submission. + human_draft_after_first_read: bool, human_draft_after_submit: bool, human_draft_unreadable: bool, delivered: Vec, + mutation_begun: bool, submitted: bool, submit_pending: bool, } @@ -1276,7 +1288,12 @@ mod match_tests { if std::mem::take(&mut self.submit_pending) { self.deliver(); } - if self.submitted && std::mem::take(&mut self.human_draft_after_submit) { + let human_draft_due = if self.submitted { + &mut self.human_draft_after_submit + } else { + &mut self.human_draft_after_first_read + }; + if std::mem::take(human_draft_due) { self.text = "human draft".to_string(); self.unreadable = self.human_draft_unreadable; } @@ -1307,7 +1324,7 @@ mod match_tests { || composer.borrow_mut().read(), |_| Ok(()), || Ok(()), - || {}, + || composer.borrow_mut().mutation_begun = true, || composer.borrow_mut().write("reply"), || Ok(()), || { @@ -2434,18 +2451,44 @@ mod match_tests { } #[test] - fn composer_guard_treats_unreadable_value_as_empty_after_focus() { - let (result, probe) = - run_composer_probe( - [None, Some(""), None, Some("reply"), Some("reply"), Some("")], - true, - ); + fn composer_guard_reads_again_after_focus_when_the_first_read_is_unreadable() { + let (result, probe) = run_composer_probe( + [ + None, + Some(""), + Some(""), + Some("reply"), + Some("reply"), + Some(""), + ], + true, + ); assert!(result.is_ok()); assert_eq!(probe.focuses, 2); assert_eq!(probe.set_attempts, 1); assert_eq!(probe.returns, 1); } + #[test] + fn composer_guard_never_writes_over_a_draft_it_cannot_read() { + let unreadable_from_the_start = FakeComposer { + text: "human draft".to_string(), + unreadable: true, + ..FakeComposer::default() + }; + let unreadable_right_before_the_write = FakeComposer { + human_draft_after_first_read: true, + human_draft_unreadable: true, + ..FakeComposer::default() + }; + for composer in [unreadable_from_the_start, unreadable_right_before_the_write] { + let (result, composer) = send_reply_through(composer); + assert_eq!(composer.text, "human draft"); + assert!(!composer.mutation_begun); + assert!(result.is_err()); + } + } + #[test] fn composer_guard_rechecks_empty_value_before_first_mutation() { let (result, probe) = @@ -2723,6 +2766,19 @@ mod match_tests { .expect_err("a non-empty composer must fail closed"); assert!(error.to_string().contains("not empty")); } + + #[test] + fn composer_preflight_rejects_an_unreadable_composer() { + guarded_composer_preflight(|| None, |_| Ok(())) + .expect_err("an unreadable composer is not proof of an empty one"); + + let mut reads = std::collections::VecDeque::from([Some(String::new()), None]); + guarded_composer_preflight( + || reads.pop_front().expect("two composer reads"), + |_| Ok(()), + ) + .expect_err("a composer that turns unreadable during the probe must fail closed"); + } } #[cfg(target_os = "macos")] From eacdf84a3769acfb6697b0d42ce4fe5cc7b33dbd Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:04:29 +0900 Subject: [PATCH 46/67] fix(ax-send): read an AXValue of "no value" as an empty composer The composer guard now needs a positive empty read before it writes. An empty AX text field can answer AXValue with kAXErrorNoValue instead of an empty string, and composer_text folded every error into None, so a live empty composer could look unreadable and stop every send before mutation. Map kAXErrorNoValue to the empty string, as child_elements already does, and keep every other error unreadable. Co-Authored-By: Claude Opus 5.5 --- src/ax_send.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ax_send.rs b/src/ax_send.rs index 12f312f8..22d9ffce 100644 --- a/src/ax_send.rs +++ b/src/ax_send.rs @@ -3328,11 +3328,15 @@ mod imp { /// deliberately left unconfirmed rather than used to trigger a retry. fn composer_text(field: &AXUIElement) -> Option { let value_attr: AXAttribute = AXAttribute::new(&CFString::new("AXValue")); - field - .attribute(&value_attr) - .ok() - .and_then(|value| value.downcast::()) - .and_then(|value| cf_string_lossy(&value)) + match field.attribute(&value_attr) { + Ok(value) => value + .downcast::() + .and_then(|value| cf_string_lossy(&value)), + // An empty text field can answer AXValue with "no value" instead of + // an empty string; the composer guard needs that read as empty. + Err(AccessibilityError::Ax(code)) if code == kAXErrorNoValue => Some(String::new()), + Err(_) => None, + } } /// Type `text` into the focused field by posting one keyboard CGEvent pair From 1fca90d14d4e888a70fdb086e4f35b10efbe090a Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:04:45 +0900 Subject: [PATCH 47/67] refactor(desktop): name the snapshot parser's steps and drop its casts parseRuntimeSnapshot parsed rooms, chats, receipts and voice inline in one 95-line function. Each now has its own parser, and the checks they repeated get one name each: positiveChatId for the three chat-id guards, firstString for fields the bridge sends in two spellings, and member for the five places that tested a string against an allowed set and then cast it. parseBackground no longer special-cases a missing object, because the general path already yields the same defaults, and unavailableSnapshot builds its voice block through parseVoice instead of a second literal. Co-Authored-By: Claude Fable 5.2 --- desktop/src/contracts.ts | 217 ++++++++++++++++++--------------------- 1 file changed, 100 insertions(+), 117 deletions(-) diff --git a/desktop/src/contracts.ts b/desktop/src/contracts.ts index e8a54a80..bc7615e7 100644 --- a/desktop/src/contracts.ts +++ b/desktop/src/contracts.ts @@ -147,9 +147,11 @@ export interface VoiceStatus { type JsonRecord = Record; const JOB_EVENT_KEYS = new Set(["jobId", "kind", "stage", "load", "time", "errorCode"]); -const RECEIPT_OUTCOMES = new Set(["sent", "deferred", "scheduled", "skipped"]); +const RECEIPT_OUTCOMES = new Set(["sent", "deferred", "scheduled", "skipped"]); const RECEIPT_REASON_SLUG = /^[a-z][a-z0-9_]{0,63}$/; -const RETRIEVAL_STATES = new Set(["ok", "empty", "skipped", "error", "index_not_ready", "unrecorded"]); +const RETRIEVAL_STATES = new Set([ + "ok", "empty", "skipped", "error", "index_not_ready", "unrecorded", +]); const GEEKNEWS_BACKGROUND_STATES = new Set(["sending", "confirmed", "idle", "unknown"]); const DB_SYNC_BACKGROUND_STATES = new Set(["retrying", "stalled", "syncing", "behind", "ready", "unknown"]); const PIPELINE_STAGES = new Set([ @@ -165,6 +167,10 @@ function record(value: unknown): JsonRecord | null { : null; } +function list(value: unknown): unknown[] { + return Array.isArray(value) ? value : []; +} + function finiteNumber(value: unknown, fallback = 0): number { return typeof value === "number" && Number.isFinite(value) ? value : fallback; } @@ -185,12 +191,27 @@ function text(value: unknown, fallback = ""): string { return typeof value === "string" ? value : fallback; } +/** The first candidate that is a string; the bridge sends some fields in two spellings. */ +function firstString(...candidates: unknown[]): string | null { + for (const candidate of candidates) { + if (typeof candidate === "string") return candidate; + } + return null; +} + +function member(allowed: ReadonlySet, value: unknown): T | null { + return typeof value === "string" && allowed.has(value as T) ? (value as T) : null; +} + +function positiveChatId(value: unknown): number | null { + const chatId = finiteNumber(value, -1); + return Number.isInteger(chatId) && chatId > 0 ? chatId : null; +} + function parseBackgroundSource(value: unknown, allowed: ReadonlySet): BackgroundSource { const source = record(value); - const rawState = typeof source?.state === "string" ? source.state : "unknown"; - const state = allowed.has(rawState as BackgroundSourceState) ? rawState as BackgroundSourceState : "unknown"; return { - state, + state: member(allowed, source?.state) ?? "unknown", activity: roundActivity(source?.activity), caption: text(source?.caption).slice(0, 120), }; @@ -198,21 +219,12 @@ function parseBackgroundSource(value: unknown, allowed: ReadonlySet { +function parseRooms(value: unknown): RoomSummary[] { + return list(value).flatMap((item): RoomSummary[] => { const room = record(item); - if (!room) return []; - const chatId = finiteNumber(room.chat_id ?? room.chatId, -1); - if (!Number.isInteger(chatId) || chatId <= 0) return []; + const chatId = positiveChatId(room?.chat_id ?? room?.chatId); + if (!room || chatId === null) return []; return [{ chatId, title: text(room.title, `id:${chatId}`), @@ -443,46 +415,79 @@ export function parseRuntimeSnapshot(value: unknown): RuntimeSnapshot { openJobs: nonNegativeInt(room.open_jobs ?? room.openJobs), }]; }); +} - const availableChatsRaw = Array.isArray(input.availableChats ?? input.available_chats) - ? (input.availableChats ?? input.available_chats) - : []; - const availableChats = (availableChatsRaw as unknown[]).flatMap((item): AvailableChatSummary[] => { +function parseAvailableChats(value: unknown): AvailableChatSummary[] { + return list(value).flatMap((item): AvailableChatSummary[] => { const chat = record(item); - if (!chat) return []; - const chatId = finiteNumber(chat.chat_id ?? chat.chatId, -1); - if (!Number.isInteger(chatId) || chatId <= 0) return []; + const chatId = positiveChatId(chat?.chat_id ?? chat?.chatId); + if (!chat || chatId === null) return []; return [{ chatId, - title: text(chat.title, "id:" + chatId), + title: text(chat.title, `id:${chatId}`), catalog: chat.catalog === true, live: chat.live === true, }]; }); +} + +function parseRecentReceipt(value: unknown): RecentReceipt | null { + const input = record(value); + if (!input) return null; + const chatId = positiveChatId(input.chatId); + const outcome = member(RECEIPT_OUTCOMES, input.outcome); + if (chatId === null || outcome === null) return null; + if (typeof input.title !== "string" || typeof input.displayTime !== "string" || typeof input.clock !== "string") return null; + if (typeof input.reasonText !== "string") return null; + const reasonCode = text(input.reasonCode); + return { + chatId, + title: input.title.slice(0, 120), + displayTime: input.displayTime.slice(0, 32), + clock: input.clock.slice(0, 8), + outcome, + outcomeText: (firstString(input.outcomeText, input.outcome_text) ?? "").slice(0, 32), + reasonCode: RECEIPT_REASON_SLUG.test(reasonCode) ? reasonCode : "unspecified", + reasonText: input.reasonText.slice(0, 64), + retrievalState: member(RETRIEVAL_STATES, input.retrievalState) ?? "unrecorded", + }; +} + +function parseVoice(value: unknown): VoiceStatus { + const voice = record(value); + const wakeSources = [voice?.wake_source, voice?.wakeSource]; + return { + available: voice?.available === true, + state: text(voice?.state, "unavailable"), + rms: clamp01(voice?.rms), + errorCode: firstString(voice?.error_code, voice?.errorCode), + wakeSource: wakeSources.includes("stock") ? "stock" : wakeSources.includes("custom") ? "custom" : "none", + updatedAt: nonNegativeInt(voice?.updated_at ?? voice?.updatedAt), + wakePhrase: text(voice?.wake_phrase ?? voice?.wakePhrase), + threshold: Math.min(0.95, Math.max(0.65, finiteNumber(voice?.threshold, 0.65))), + customModelSelected: voice?.custom_model_selected === true || voice?.customModelSelected === true, + }; +} - const jobsRaw = Array.isArray(input.jobs) ? input.jobs : []; - const jobs = jobsRaw.map(parseJobEvent).filter((item): item is JobEvent => item !== null); - const receiptsRaw = Array.isArray(input.recent_receipts) - ? input.recent_receipts - : Array.isArray(input.recentReceipts) - ? input.recentReceipts - : []; - const recentReceipts = receiptsRaw - .map(parseRecentReceipt) - .filter((item): item is RecentReceipt => item !== null) - .slice(0, 12); +export function parseRuntimeSnapshot(value: unknown): RuntimeSnapshot { + const input = record(value); + if (!input) return unavailableSnapshot("snapshot_invalid"); + + const receipts = Array.isArray(input.recent_receipts) ? input.recent_receipts : list(input.recentReceipts); const terminal = record(input.terminal_counts ?? input.terminal) ?? {}; const contextSync = record(input.context_sync ?? input.contextSync); - const voice = record(input.voice); const contextValid = contextSync?.mode === "async" && contextSync.waited === false; return { available: input.available !== false && contextValid, - rooms, - availableChats, - jobs, - recentReceipts, - jobLoad: Math.min(1, Math.max(0, finiteNumber(input.job_load ?? input.jobLoad, 0))), + rooms: parseRooms(input.rooms), + availableChats: parseAvailableChats(input.availableChats ?? input.available_chats), + jobs: list(input.jobs).map(parseJobEvent).filter((item): item is JobEvent => item !== null), + recentReceipts: receipts + .map(parseRecentReceipt) + .filter((item): item is RecentReceipt => item !== null) + .slice(0, 12), + jobLoad: clamp01(input.job_load ?? input.jobLoad), background: parseBackground(input.background), onDevice: parseOnDevice(input.onDevice ?? input.ondevice_hardware), pipeline: parsePipeline(input.pipeline), @@ -493,31 +498,9 @@ export function parseRuntimeSnapshot(value: unknown): RuntimeSnapshot { burstSuperseded: nonNegativeInt(terminal.burst_superseded ?? terminal.burstSuperseded), }, contextSync: { mode: "async", waited: false }, - replyModelId: typeof input.reply_model_id === "string" - ? input.reply_model_id - : typeof input.replyModelId === "string" - ? input.replyModelId - : null, - voice: { - available: voice?.available === true, - state: text(voice?.state, "unavailable"), - rms: Math.min(1, Math.max(0, finiteNumber(voice?.rms, 0))), - errorCode: typeof voice?.error_code === "string" - ? voice.error_code - : typeof voice?.errorCode === "string" - ? voice.errorCode - : null, - wakeSource: voice?.wake_source === "stock" || voice?.wakeSource === "stock" - ? "stock" - : voice?.wake_source === "custom" || voice?.wakeSource === "custom" - ? "custom" - : "none", - updatedAt: nonNegativeInt(voice?.updated_at ?? voice?.updatedAt), - wakePhrase: text(voice?.wake_phrase ?? voice?.wakePhrase, ""), - threshold: Math.min(0.95, Math.max(0.65, finiteNumber(voice?.threshold, 0.65))), - customModelSelected: voice?.custom_model_selected === true || voice?.customModelSelected === true, - }, - errorCode: contextValid ? (typeof input.error_code === "string" ? input.error_code : null) : "context_sync_invalid", + replyModelId: firstString(input.reply_model_id, input.replyModelId), + voice: parseVoice(input.voice), + errorCode: contextValid ? firstString(input.error_code) : "context_sync_invalid", }; } From a2141560f6dceca79f946252a0a1aba91e3bc8ed Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:06:40 +0900 Subject: [PATCH 48/67] refactor(desktop): type the settings labels and name the graph setup steps activityStateLabel accepted unknown and matched twenty state names, but its only input is a BackgroundSourceState and only three of its values have a label of their own; the rest read as the default. It is now a typed switch over those, and safeDisplayString, its one caller's guard against a value the type excludes, is gone. The receipt outcome and lookup-state labels take their union types as well. setupKnowledgeGraph hands its two self-contained pieces to relatedTalkSummary and renderAccessibleNodes. renderPanelUnavailable absorbs showPanelUnavailable, whose reuse branch could not run after the markup had just been replaced. JarvisCoreControl extends the loop and signal-sink interfaces instead of restating them, and the voice state labels move out of the render call. Co-Authored-By: Claude Fable 5.2 --- desktop/src/main.ts | 121 ++++++++++++++++++++------------------------ desktop/src/ui.ts | 42 ++++++--------- 2 files changed, 69 insertions(+), 94 deletions(-) diff --git a/desktop/src/main.ts b/desktop/src/main.ts index 61aeb4dd..353ae3f0 100644 --- a/desktop/src/main.ts +++ b/desktop/src/main.ts @@ -6,8 +6,7 @@ import { type CancellationToken, type RuntimeSnapshot, } from "./contracts"; -import type { SourceLoads } from "./core/load-mapping"; -import { RenderLifecycle } from "./core/lifecycle"; +import { RenderLifecycle, type RenderLoopControl } from "./core/lifecycle"; import { tauriVisibilitySubscriber, tauriVisibilityReader, @@ -37,6 +36,7 @@ import { RuntimeSnapshotPoller, browserPollScheduler, type PollTimerScheduler, + type RuntimeSignalSink, type SnapshotCanceller, type SnapshotLoader, } from "./runtime-poller"; @@ -50,11 +50,8 @@ if (!app) throw new Error("app_root_missing"); const isSettings = new URLSearchParams(window.location.search).get("view") === "settings"; document.body.classList.add(isSettings ? "settings-view" : "panel-view"); -interface JarvisCoreControl { +interface JarvisCoreControl extends RenderLoopControl, RuntimeSignalSink { readonly renderCount: number; - start(): void; - stop(): void; - setSignals(jobLoad: number, voiceRms: number, sources?: SourceLoads): void; dispose(): void; } @@ -63,27 +60,18 @@ function setText(id: string, value: string): void { if (element) element.textContent = value; } -function showPanelUnavailable(message: string): void { - app.dataset.state = "unavailable"; - const panel = document.querySelector(".jarvis-panel"); - if (!panel) return; - let status = document.getElementById("panel-status"); - if (!status) { - status = document.createElement("p"); - status.id = "panel-status"; - status.setAttribute("role", "status"); - status.setAttribute("aria-live", "polite"); - status.style.cssText = "position:absolute;left:12px;right:12px;bottom:9px;margin:0;text-align:center;font-size:10px;line-height:1.35;color:var(--muted);pointer-events:none"; - panel.append(status); - } - status.textContent = message; -} - function renderPanelUnavailable(): void { app.innerHTML = mainPanelMarkup(); + app.dataset.state = "unavailable"; document.querySelector(".jarvis-core") ?.setAttribute("aria-label", "Jarvis core 확인 불가"); - showPanelUnavailable("Jarvis 상태를 확인할 수 없습니다."); + const status = document.createElement("p"); + status.id = "panel-status"; + status.setAttribute("role", "status"); + status.setAttribute("aria-live", "polite"); + status.style.cssText = "position:absolute;left:12px;right:12px;bottom:9px;margin:0;text-align:center;font-size:10px;line-height:1.35;color:var(--muted);pointer-events:none"; + status.textContent = "Jarvis 상태를 확인할 수 없습니다."; + document.querySelector(".jarvis-panel")?.append(status); } function wireRoomAdd(loadSnapshot: SnapshotLoader, loadAction: typeof fetchSettingsAction): void { @@ -233,24 +221,28 @@ function wireModelSelection(): void { }); } +const VOICE_STATE_LABELS: Readonly> = { + idle: "마이크가 대기 중입니다.", + wake_listen: "호출어를 기다리고 있습니다.", + user_listen: "말씀을 듣고 있습니다.", + transcribing: "말씀을 정리하고 있습니다.", + generating: "답변을 준비하고 있습니다.", + speaking: "답변을 말하고 있습니다.", + ended: "음성 대화가 끝났습니다.", + aborted: "음성 대화가 끝났습니다.", + error: "음성 기능을 사용할 수 없습니다.", +}; + function renderVoice(snapshot: RuntimeSnapshot): void { const voice = snapshot.voice; if (!voice.available) { setText("voice-status", "음성 상태를 확인할 수 없습니다."); return; } - const labels: Record = { - idle: "마이크가 대기 중입니다.", - wake_listen: "호출어를 기다리고 있습니다.", - user_listen: "말씀을 듣고 있습니다.", - transcribing: "말씀을 정리하고 있습니다.", - generating: "답변을 준비하고 있습니다.", - speaking: "답변을 말하고 있습니다.", - ended: "음성 대화가 끝났습니다.", - aborted: "음성 대화가 끝났습니다.", - error: "음성 기능을 사용할 수 없습니다.", - }; - setText("voice-status", voiceErrorMessage(voice.errorCode) ?? labels[voice.state] ?? "음성 상태를 확인하고 있습니다."); + setText( + "voice-status", + voiceErrorMessage(voice.errorCode) ?? VOICE_STATE_LABELS[voice.state] ?? "음성 상태를 확인하고 있습니다.", + ); } function renderSettingsUnavailable(): void { @@ -325,6 +317,30 @@ function renderKnowledgeRelations( }); } +function relatedTalkSummary(focus: Record | null): string { + if (!focus || focus.ok !== true) return "관련 대화를 찾지 못했습니다."; + const facts = Array.isArray(focus.facts) ? focus.facts.filter((item): item is string => typeof item === "string") : []; + if (facts.length === 0) return "관련 대화를 찾지 못했습니다."; + const firstFact = facts[0].slice(0, 180); + return `관련 정보 ${facts.length}건을 찾았습니다.${firstFact ? ` ${firstFact}` : ""}`; +} + +function renderAccessibleNodes(nodes: readonly KnowledgeNode[], select: (nodeId: string) => void): void { + const container = document.querySelector("#knowledge-accessible-nodes"); + if (!container) return; + container.replaceChildren(); + nodes.slice(0, ON_SCREEN_NODE_CAP).forEach((node) => { + const button = document.createElement("button"); + button.type = "button"; + button.className = "knowledge-a11y-node"; + button.dataset.nodeId = node.id; + button.textContent = node.label; + button.setAttribute("aria-label", `${node.label} 선택`); + button.addEventListener("click", () => select(node.id)); + container.append(button); + }); +} + async function setupKnowledgeGraph( payload: Record | null, snapshot: RuntimeSnapshot, @@ -338,14 +354,9 @@ async function setupKnowledgeGraph( return null; } - setText( - "knowledge-summary", - `대화에서 찾은 연결 항목 ${graph.nodes.length}개`, - ); + setText("knowledge-summary", `대화에서 찾은 연결 항목 ${graph.nodes.length}개`); setText("knowledge-mode", payload?.stale === true ? "자료 확인 필요" : "연결된 주제"); - const a11yContainer = document.querySelector("#knowledge-accessible-nodes"); - let activeNodeId = ""; const { KnowledgeHologram } = await import("./knowledge/hologram"); const hologram = new KnowledgeHologram(canvas, graph, ({ node, view }) => { @@ -363,16 +374,7 @@ async function setupKnowledgeGraph( nodeId: node.id, chatId: localRoom, }).then((focus) => { - if (activeNodeId !== node.id) return; - if (!focus || focus.ok !== true) { - setText("knowledge-retrieve", "관련 대화를 찾지 못했습니다."); - return; - } - const facts = Array.isArray(focus.facts) ? focus.facts.filter((item): item is string => typeof item === "string") : []; - const firstFact = facts[0]?.slice(0, 180); - setText("knowledge-retrieve", facts.length - ? `관련 정보 ${facts.length}건을 찾았습니다.${firstFact ? ` ${firstFact}` : ""}` - : "관련 대화를 찾지 못했습니다."); + if (activeNodeId === node.id) setText("knowledge-retrieve", relatedTalkSummary(focus)); }); }); @@ -385,22 +387,7 @@ async function setupKnowledgeGraph( renderKnowledgeRelations(graph, node, view, snapshot); }); - if (a11yContainer) { - a11yContainer.replaceChildren(); - graph.nodes.slice(0, ON_SCREEN_NODE_CAP).forEach((node) => { - const btn = document.createElement("button"); - btn.type = "button"; - btn.className = "knowledge-a11y-node"; - btn.dataset.nodeId = node.id; - btn.textContent = node.label; - btn.setAttribute("aria-label", `${node.label} 선택`); - btn.addEventListener("click", () => { - hologram.clickNode(node.id); - }); - a11yContainer.append(btn); - }); - } - + renderAccessibleNodes(graph.nodes, (nodeId) => hologram.clickNode(nodeId)); return hologram; } diff --git a/desktop/src/ui.ts b/desktop/src/ui.ts index 7169199c..fed75f9f 100644 --- a/desktop/src/ui.ts +++ b/desktop/src/ui.ts @@ -1,5 +1,5 @@ import { LAYOUT } from "./tokens"; -import type { RuntimeSnapshot } from "./contracts"; +import type { BackgroundSourceState, RecentReceipt, RuntimeSnapshot } from "./contracts"; export const MAIN_PANEL_CONTROLS = Object.freeze([] as const); @@ -132,29 +132,29 @@ export function renderHistory(snapshot: RuntimeSnapshot, root: Document = docume heading.className = "history-receipt-title"; heading.textContent = `${receipt.displayTime || receipt.clock || "시간 미기록"} · ${receipt.title}`; + const outcome = receipt.outcomeText || receiptOutcomeLabel(receipt.outcome); const badge = root.createElement("span"); badge.className = "history-receipt-badge"; - badge.textContent = receipt.outcomeText || receiptOutcomeLabel(receipt.outcome); + badge.textContent = outcome; header.append(heading, badge); const detail = root.createElement("span"); detail.className = "history-receipt-detail"; const reason = receipt.reasonText || receiptReasonLabel(receipt.reasonCode); - detail.textContent = `${receipt.outcomeText || receiptOutcomeLabel(receipt.outcome)} · ${reason} · 대화 찾기 ${retrievalLabel(receipt.retrievalState)}`; + detail.textContent = `${outcome} · ${reason} · 대화 찾기 ${retrievalLabel(receipt.retrievalState)}`; row.append(header, detail); list.append(row); }); } -function receiptOutcomeLabel(outcome: string): string { +function receiptOutcomeLabel(outcome: RecentReceipt["outcome"]): string { switch (outcome) { case "sent": return "답변 완료"; case "deferred": return "나중에 처리"; case "scheduled": return "예약됨"; case "skipped": return "건너뜀"; - default: return "기록됨"; } } @@ -168,26 +168,24 @@ function receiptReasonLabel(reasonCode: string): string { } } -function retrievalLabel(state: string): string { +function retrievalLabel(state: RecentReceipt["retrievalState"]): string { switch (state) { case "ok": return "자료 확인됨"; case "empty": return "관련 자료 없음"; case "skipped": return "확인하지 않음"; case "error": return "자료를 확인하지 못함"; case "index_not_ready": return "자료 준비 중"; - default: return "상태 확인 중"; + case "unrecorded": return "상태 확인 중"; } } -function activityStateLabel(value: unknown): string { - const state = safeDisplayString(value, "").toLowerCase(); - if (["active", "running", "in_progress", "processing"].includes(state)) return "진행 중"; - if (["ready", "complete", "completed", "done", "ok", "success"].includes(state)) return "완료"; - if (["queued", "pending", "waiting"].includes(state)) return "대기 중"; - if (["sending", "send", "publishing"].includes(state)) return "전송 중"; - if (["behind", "stale"].includes(state)) return "새로 확인 필요"; - if (["error", "failed", "unavailable", "blocked"].includes(state)) return "확인 필요"; - return "확인 중"; +function activityStateLabel(state: BackgroundSourceState): string { + switch (state) { + case "sending": return "전송 중"; + case "ready": return "완료"; + case "behind": return "새로 확인 필요"; + default: return "확인 중"; + } } export function renderBackground(snapshot: RuntimeSnapshot, root: Document = document): void { @@ -219,25 +217,16 @@ export function renderBackground(snapshot: RuntimeSnapshot, root: Document = doc target.textContent = `작업 현황 · 답변 대기 ${pendingReplies}건 · 긱뉴스 ${activityStateLabel(background.geeknews.state)} · 대화 준비 ${activityStateLabel(background.dbSync.state)}${pipeline}${jobs}`; } -function safeDisplayString(value: unknown, fallback: string): string { - if (value === undefined || value === null) return fallback; - try { - return String(value); - } catch { - return fallback; - } -} - export function renderRooms(snapshot: RuntimeSnapshot, root: Document = document): void { const popup = root.querySelector("#settings-room-popup"); if (popup) { + const summary = root.getElementById("room-summary"); popup.replaceChildren(); if (snapshot.rooms.length === 0) { const option = document.createElement("option"); option.textContent = snapshot.available ? "등록된 채팅방이 없습니다." : "채팅방 목록을 불러오지 못했습니다."; option.value = ""; popup.append(option); - const summary = root.getElementById("room-summary"); if (summary) summary.textContent = snapshot.available ? "등록된 채팅방이 없습니다." : "채팅방 목록을 불러오지 못했습니다. 다시 확인해 주세요."; @@ -249,7 +238,6 @@ export function renderRooms(snapshot: RuntimeSnapshot, root: Document = document popup.append(option); }); const live = snapshot.rooms.filter((room) => room.live).length; - const summary = root.getElementById("room-summary"); if (summary) summary.textContent = `등록된 채팅방 ${snapshot.rooms.length}개 · 연결됨 ${live}개`; } } From b8b5b656da5a080f10e04eaf5d25202f83ca1ed4 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:04:42 +0900 Subject: [PATCH 49/67] chore(dev): add CLI help-dump pin and Rust item mover dump_cli_help.py walks the clap tree (hidden subcommands included) and writes --help, -h, and --version output per command path, so two dumps can be diffed with diff -r. check_cli_pin.sh rebuilds, re-dumps, diffs against a baseline dump, and records test totals and the clippy verdict. move_rust_items.py lists, moves, and compares whole top-level Rust items, which keeps large file splits mechanical and checkable. Co-Authored-By: Claude Opus 5.5 --- scripts/dev/check_cli_pin.sh | 26 ++++ scripts/dev/dump_cli_help.py | 106 +++++++++++++ scripts/dev/move_rust_items.py | 265 +++++++++++++++++++++++++++++++++ 3 files changed, 397 insertions(+) create mode 100755 scripts/dev/check_cli_pin.sh create mode 100755 scripts/dev/dump_cli_help.py create mode 100755 scripts/dev/move_rust_items.py diff --git a/scripts/dev/check_cli_pin.sh b/scripts/dev/check_cli_pin.sh new file mode 100755 index 00000000..0f4343d4 --- /dev/null +++ b/scripts/dev/check_cli_pin.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Usage: scripts/dev/check_cli_pin.sh +# Rebuilds, re-dumps the CLI help tree into /dump, diffs it against +# the baseline, then records cargo test totals and the clippy verdict. +set -u +baseline=$1 +work=$2 +repo=$(cd "$(dirname "$0")/../.." && pwd) +cd "$repo" || exit 1 +rm -rf "$work" +mkdir -p "$work" +cargo build >"$work/build.log" 2>&1 || { echo "build: FAILED"; tail -30 "$work/build.log"; exit 1; } +python3 scripts/dev/dump_cli_help.py target/debug/openkakao-cli "$work/dump" >/dev/null +if diff -r "$baseline" "$work/dump" >"$work/help.diff"; then + echo "help dump: identical" +else + echo "help dump: DIFFERS ($work/help.diff)" +fi +cargo test >"$work/test.log" 2>&1 +echo "cargo test exit: $?" +grep -E '^test result:' "$work/test.log" | + awk '{p += $4; f += $6} END {print "tests passed: " p ", failed: " f}' +awk '/Running unittests src\/main.rs/ {bin = 1} bin && /^test result:/ {print "bin unit tests: " $4 " passed"; exit}' "$work/test.log" +grep -E '^test .* FAILED$' "$work/test.log" +cargo clippy --all-targets -- -D warnings >"$work/clippy.log" 2>&1 +echo "clippy exit: $?" diff --git a/scripts/dev/dump_cli_help.py b/scripts/dev/dump_cli_help.py new file mode 100755 index 00000000..7262650b --- /dev/null +++ b/scripts/dev/dump_cli_help.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +"""Dump `--help`, `-h`, and `--version` output for every openkakao-cli subcommand. + +Usage: scripts/dev/dump_cli_help.py [--source src/main.rs] + +`diff -r` two dumps to prove a refactor left the CLI surface unchanged. +clap omits hidden subcommands from help, so top-level variant names are also +read from the `Commands` enum in the source file. +""" + +import argparse +import re +import subprocess +import sys +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parents[2] + + +def run(binary, args): + proc = subprocess.run( + [binary, *args], + stdin=subprocess.DEVNULL, + capture_output=True, + text=True, + timeout=30, + ) + return f"exit: {proc.returncode}\n--- stdout\n{proc.stdout}--- stderr\n{proc.stderr}" + + +def visible_subcommands(help_text): + names = [] + in_commands = False + for line in help_text.splitlines(): + if line.strip() == "Commands:": + in_commands = True + continue + if in_commands: + match = re.match(r"^ ([a-z0-9][a-z0-9-]*)\b", line) + if not match: + if not line.startswith(" "): + in_commands = False + continue + if match.group(1) != "help": + names.append(match.group(1)) + return names + + +def kebab(name): + return re.sub(r"(?<=[a-z0-9])([A-Z])", r"-\1", name).lower() + + +def source_subcommands(source): + lines = source.read_text().splitlines() + start = lines.index("enum Commands {") + names = [] + pending_name = None + for line in lines[start + 1 :]: + if line == "}": + break + attr = re.match(r"^ #\[command\(.*\bname = \"([^\"]+)\"", line) + if attr: + pending_name = attr.group(1) + continue + variant = re.match(r"^ ([A-Z][A-Za-z0-9]*)\b", line) + if variant: + names.append(pending_name or kebab(variant.group(1))) + pending_name = None + return names + + +def dump(binary, out, path, source_names): + target = out.joinpath(*path) if path else out / "_root" + target.mkdir(parents=True, exist_ok=True) + long_help = run(binary, [*path, "--help"]) + (target / "help.txt").write_text(long_help) + (target / "h.txt").write_text(run(binary, [*path, "-h"])) + children = visible_subcommands(long_help.split("--- stderr")[0]) + if not path: + (target / "help-subcommand.txt").write_text(run(binary, ["help"])) + (target / "version.txt").write_text(run(binary, ["--version"])) + (target / "V.txt").write_text(run(binary, ["-V"])) + children = sorted(set(children) | set(source_names)) + dumped = [" ".join(path)] + for child in children: + dumped += dump(binary, out, [*path, child], source_names) + return dumped + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("binary") + parser.add_argument("out") + parser.add_argument("--source", default=str(REPO_ROOT / "src" / "main.rs")) + args = parser.parse_args() + out = Path(args.out) + if out.exists() and any(out.iterdir()): + sys.exit(f"refusing to write into non-empty {out}") + out.mkdir(parents=True, exist_ok=True) + dumped = dump(args.binary, out, [], source_subcommands(Path(args.source))) + (out / "commands.txt").write_text("\n".join(dumped) + "\n") + print(f"dumped {len(dumped)} command paths to {out}") + + +if __name__ == "__main__": + main() diff --git a/scripts/dev/move_rust_items.py b/scripts/dev/move_rust_items.py new file mode 100755 index 00000000..825d155e --- /dev/null +++ b/scripts/dev/move_rust_items.py @@ -0,0 +1,265 @@ +#!/usr/bin/env python3 +"""Move whole top-level Rust items between files verbatim. + + list [--mod tests] print each item's span, key, and the + other items in the same scope it uses + move KEY... [--src-mod tests] [--dst-mod tests] + cut the items and append them to + compare ... [--base-mod tests] [--mod tests] [--show] + report which items in match the + same-named item in verbatim + +An item spans from the line after the previous item ends through its own +closing `}` or `;`, so doc comments and attributes travel with it. Moving a +type also moves every `impl` block for it. Items that share a name (for +example `#[cfg]` variants of one function) move together. A `--*-mod tests` +flag selects the items inside `mod tests { ... }` instead of the file's top +level; a destination gets a `mod tests` block if it lacks one. Moved text is +not re-indented, so run rustfmt on the destination afterwards. +""" + +import argparse +import difflib +import re +import sys +from pathlib import Path + +HEADER = re.compile( + r'^\s*(?:pub(?:\([^)]*\))?\s+)?(?:async\s+|unsafe\s+|extern\s+"[^"]*"\s+|const\s+(?=fn))*' + r"(?:(fn|struct|enum|trait|mod|type|static|const|union)\s+([A-Za-z_][A-Za-z0-9_]*)" + r"|(impl)\b(.*)|(use)\s+(.*)|(macro_rules!)\s*([A-Za-z_][A-Za-z0-9_]*))" +) + + +RAW = re.compile(r'r(#*)"') + + +def raw_string_start(text, i): + prev = text[i - 1] if i > 0 else "" + if prev == "b": + prev = text[i - 2] if i > 1 else "" + return not (prev.isalnum() or prev == "_") + + +def scan(text): + """Return code text with strings and comments blanked, same length.""" + out = list(text) + i, n = 0, len(text) + + def blank(a, b): + for k in range(a, b): + if out[k] != "\n": + out[k] = " " + + while i < n: + c = text[i] + if text.startswith("//", i): + j = text.find("\n", i) + j = n if j < 0 else j + blank(i, j) + i = j + elif text.startswith("/*", i): + depth, j = 1, i + 2 + while j < n and depth: + if text.startswith("/*", j): + depth, j = depth + 1, j + 2 + elif text.startswith("*/", j): + depth, j = depth - 1, j + 2 + else: + j += 1 + blank(i, j) + i = j + elif c == "r" and raw_string_start(text, i) and RAW.match(text, i): + hashes = RAW.match(text, i).group(1) + start = i + 2 + len(hashes) + end = text.find('"' + hashes, start) + blank(start, end) + i = end + 1 + len(hashes) + elif c == '"': + j = i + 1 + while j < n and text[j] != '"': + j += 2 if text[j] == "\\" else 1 + blank(i + 1, j) + i = j + 1 + elif c == "'": + if i + 1 < n and text[i + 1] == "\\": + j = text.find("'", i + 3) + blank(i + 1, j) + i = j + 1 + elif i + 2 < n and text[i + 2] == "'": + blank(i + 1, i + 2) + i += 3 + else: + i += 1 + else: + i += 1 + return "".join(out) + + +def items(lines, first, last): + """Split lines[first:last] into top-level items of that region.""" + code = scan("".join(lines[first:last])).splitlines(keepends=True) + result, depth, start, header = [], 0, 0, None + for offset, line in enumerate(code): + stripped = line.strip() + if depth == 0 and header is None and stripped and not stripped.startswith("#"): + header = offset + for ch in line: + if ch in "{([": + depth += 1 + elif ch in "})]": + depth -= 1 + if depth == 0 and header is not None and (stripped.endswith("}") or stripped.endswith(";")): + result.append((first + start, first + header, first + offset + 1)) + start, header = offset + 1, None + return result + + +def key_of(header_line): + m = HEADER.match(header_line) + if not m: + return header_line.strip() + if m.group(1): + return m.group(2) + if m.group(3): + sig = re.sub(r"\s*\{\s*$", "", m.group(4)).strip() + sig = re.sub(r"^<[^>]*>\s*", "", sig) + return "impl " + sig + if m.group(5): + return "use " + m.group(6).rstrip(";").strip() + return m.group(8) + + +def impl_target(key): + if not key.startswith("impl "): + return None + sig = key[5:] + target = sig.split(" for ", 1)[1] if " for " in sig else sig + return re.match(r"[A-Za-z_][A-Za-z0-9_]*", target).group(0) + + +def region(lines, mod): + if mod is None: + return 0, len(lines) + for i, line in enumerate(lines): + if re.match(rf"^mod {re.escape(mod)} \{{\s*$", line): + for j in range(len(lines) - 1, i, -1): + if lines[j].rstrip() == "}": + return i + 1, j + sys.exit(f"no `mod {mod} {{` block found") + + +def parsed(path, mod): + lines = Path(path).read_text().splitlines(keepends=True) + first, last = region(lines, mod) + spans = [(s, h, e, key_of(lines[h])) for s, h, e in items(lines, first, last)] + return lines, first, last, spans + + +def cmd_list(args): + lines, _, _, spans = parsed(args.file, args.mod) + names = {k for *_, k in spans if re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", k)} + for s, h, e, key in spans: + idents = set(re.findall(r"\b[A-Za-z_][A-Za-z0-9_]*\b", scan("".join(lines[h:e])))) + uses = sorted((idents & names) - {key, impl_target(key)}) + print(f"{h + 1}-{e}\t{e - h}\t{key}\t{' '.join(uses)}") + + +def insert_into(dst, block, mod): + existing = dst.read_text() if dst.exists() else "" + lines = existing.splitlines(keepends=True) + if mod is None: + at = next((i for i, l in enumerate(lines) if l.startswith("mod tests {")), len(lines)) + while at > 0 and lines[at - 1].startswith("#["): + at -= 1 + else: + if not any(re.match(rf"^mod {re.escape(mod)} \{{\s*$", l) for l in lines): + lines += ["\n", "#[cfg(test)]\n", f"mod {mod} {{\n", " use super::*;\n", "}\n"] + at = region(lines, mod)[1] + head = "".join(lines[:at]).rstrip("\n") + tail = "".join(lines[at:]) + dst.write_text((head + "\n\n" if head.strip() else "") + block + ("\n" + tail if tail else "")) + + +def cmd_move(args): + lines, _, _, spans = parsed(args.src, args.src_mod) + wanted = set(args.keys) + chosen = [sp for sp in spans if sp[3] in wanted or impl_target(sp[3]) in wanted] + missing = wanted - ({sp[3] for sp in chosen} | {impl_target(sp[3]) for sp in chosen}) + if missing: + sys.exit(f"not found in {args.src}: {' '.join(sorted(missing))}") + taken, moved = set(), [] + for s, _, e, _ in chosen: + body = lines[s:e] + while body and not body[0].strip(): + body = body[1:] + moved.append("".join(body)) + taken.update(range(s, e)) + Path(args.src).write_text("".join(l for i, l in enumerate(lines) if i not in taken)) + insert_into(Path(args.dst), "\n".join(moved), args.dst_mod) + print(f"moved {len(chosen)} items ({len(taken)} lines) to {args.dst}") + + +def normalized(text): + text = re.sub(r"\bpub(\([^)]*\))?\s+", "", text) + text = re.sub(r"\s+", "", text) + return re.sub(r",([)\]}])", r"\1", text) + + +def chunk_texts(path, mod): + lines, _, _, spans = parsed(path, mod) + texts = {} + for s, _, e, key in spans: + texts.setdefault(key, []).append("".join(lines[s:e]).strip("\n")) + return texts + + +def cmd_compare(args): + base = chunk_texts(args.base, args.base_mod) + same, changed, new = 0, [], [] + for path in args.files: + for key, texts in chunk_texts(path, args.mod).items(): + if key not in base: + new.append(f"{path}: {key}") + elif sorted(map(normalized, texts)) == sorted(map(normalized, base[key])): + same += len(texts) + else: + changed.append(f"{path}: {key}") + if args.show: + diff = difflib.unified_diff( + "\n".join(base[key]).splitlines(), "\n".join(texts).splitlines(), lineterm="" + ) + print("\n".join(diff)) + print(f"verbatim (modulo whitespace, visibility, trailing commas): {same}") + print(f"changed: {len(changed)}") + for item in changed: + print(f" {item}") + print(f"not in base: {len(new)}") + for item in new: + print(f" {item}") + + +def main(): + parser = argparse.ArgumentParser() + sub = parser.add_subparsers(dest="cmd", required=True) + p_list = sub.add_parser("list") + p_list.add_argument("file") + p_list.add_argument("--mod") + p_move = sub.add_parser("move") + p_move.add_argument("src") + p_move.add_argument("dst") + p_move.add_argument("keys", nargs="+") + p_move.add_argument("--src-mod") + p_move.add_argument("--dst-mod") + p_cmp = sub.add_parser("compare") + p_cmp.add_argument("base") + p_cmp.add_argument("files", nargs="+") + p_cmp.add_argument("--base-mod") + p_cmp.add_argument("--mod") + p_cmp.add_argument("--show", action="store_true") + args = parser.parse_args() + {"list": cmd_list, "move": cmd_move, "compare": cmd_compare}[args.cmd](args) + + +if __name__ == "__main__": + main() From bc1247d982bd00d327621227f5707f700a2f512f Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:22:08 +0900 Subject: [PATCH 50/67] chore(dev): extract and verify main() match arms extract-arm turns a `Commands::X { .. } => { body }` arm of main() into a typed `pub fn` in a command module, taking the parameter types from the enum Commands fields, and leaves a one-line call in the dispatch. compare-arm proves the result equals rustfmt of the original arm body wrapped in the new signature. Co-Authored-By: Claude Opus 5.5 --- scripts/dev/move_rust_items.py | 105 ++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/scripts/dev/move_rust_items.py b/scripts/dev/move_rust_items.py index 825d155e..101fc755 100755 --- a/scripts/dev/move_rust_items.py +++ b/scripts/dev/move_rust_items.py @@ -8,6 +8,14 @@ compare ... [--base-mod tests] [--mod tests] [--show] report which items in match the same-named item in verbatim + extract-arm VARIANT PATH FN [--param 'name: Type[=expr]']... + turn the `Commands::VARIANT { .. } => { body }` + arm of main() into `pub fn FN` in , + typed from the `enum Commands` fields, and + replace the arm with a call through PATH + compare-arm VARIANT FN [--show] + check FN equals rustfmt of the original arm + body wrapped in FN's signature An item spans from the line after the previous item ends through its own closing `}` or `;`, so doc comments and attributes travel with it. Moving a @@ -21,7 +29,9 @@ import argparse import difflib import re +import subprocess import sys +import tempfile from pathlib import Path HEADER = re.compile( @@ -239,6 +249,79 @@ def cmd_compare(args): print(f" {item}") +def variant_fields(lines, variant): + start = lines.index("enum Commands {\n") + at = next(i for i in range(start, len(lines)) if re.match(rf"^ {variant}\b", lines[i])) + fields = {} + if lines[at].rstrip().endswith("{"): + for line in lines[at + 1 :]: + if line.rstrip() == " },": + break + field = re.match(r"^ ([a-z_][a-z0-9_]*): (.+),$", line) + if field: + fields[field.group(1)] = field.group(2) + return fields + + +def cmd_extract_arm(args): + src = Path(args.src) + lines = src.read_text().splitlines(keepends=True) + fields = variant_fields(lines, args.variant) + main_at = lines.index("fn main() -> Result<()> {\n") + start = next(i for i in range(main_at, len(lines)) if re.match(rf"^ Commands::{args.variant}\b", lines[i])) + open_at = next(i for i in range(start, len(lines)) if lines[i].rstrip().endswith("=> {")) + close_at = next(i for i in range(open_at + 1, len(lines)) if lines[i].rstrip("\n") == " }") + pattern = "".join(lines[start : open_at + 1]) + bound = [ + name + for name in re.findall(r"\b([a-z_][a-z0-9_]*)\b(?!\s*:\s*_)", pattern.split("{", 1)[1].rsplit("}", 1)[0]) + if name in fields + ] + params = [f"{name}: {fields[name]}" for name in bound] + call_args = list(bound) + for extra in args.param: + decl, _, expr = extra.partition("=") + params.append(decl.strip()) + call_args.append(expr.strip() or decl.split(":", 1)[0].strip()) + body = "".join(lines[open_at + 1 : close_at]) + func = f"pub fn {args.fn}({', '.join(params)}) -> Result<()> {{\n{body} Ok(())\n}}\n" + call = f"{args.path}::{args.fn}({', '.join(call_args)})?,\n" + head = pattern.rstrip()[: -len("{")] + lines[start : close_at + 1] = [head + call] + src.write_text("".join(lines)) + insert_into(Path(args.dst), func, None) + print(f"extracted {args.variant} ({close_at - open_at - 1} body lines) into {args.dst}::{args.fn}") + + +def arm_body(lines, variant): + main_at = lines.index("fn main() -> Result<()> {\n") + start = next(i for i in range(main_at, len(lines)) if re.match(rf"^ Commands::{variant}\b", lines[i])) + open_at = next(i for i in range(start, len(lines)) if lines[i].rstrip().endswith("=> {")) + close_at = next(i for i in range(open_at + 1, len(lines)) if lines[i].rstrip("\n") == " }") + return "".join(lines[open_at + 1 : close_at]) + + +def cmd_compare_arm(args): + base_lines = Path(args.base).read_text().splitlines(keepends=True) + texts = chunk_texts(args.file, None).get(args.fn) + if not texts: + sys.exit(f"{args.fn} not found in {args.file}") + signature = texts[0].split("{", 1)[0] + candidate = signature + "{\n" + arm_body(base_lines, args.variant) + " Ok(())\n}\n" + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "arm.rs" + path.write_text(candidate) + subprocess.run(["rustfmt", "--edition", "2021", str(path)], check=True) + expected = path.read_text().strip("\n") + if expected == texts[0]: + print(f"{args.variant} -> {args.fn}: body verbatim (rustfmt of the original arm)") + return + print(f"{args.variant} -> {args.fn}: body CHANGED") + if args.show: + print("\n".join(difflib.unified_diff(expected.splitlines(), texts[0].splitlines(), lineterm=""))) + sys.exit(1) + + def main(): parser = argparse.ArgumentParser() sub = parser.add_subparsers(dest="cmd", required=True) @@ -257,8 +340,28 @@ def main(): p_cmp.add_argument("--base-mod") p_cmp.add_argument("--mod") p_cmp.add_argument("--show", action="store_true") + p_arm = sub.add_parser("extract-arm") + p_arm.add_argument("src") + p_arm.add_argument("dst") + p_arm.add_argument("variant") + p_arm.add_argument("path", help="module path the call site uses, e.g. commands::context") + p_arm.add_argument("fn") + p_arm.add_argument("--param", action="append", default=[], help="'name: Type[=call expr]'") + p_carm = sub.add_parser("compare-arm") + p_carm.add_argument("base") + p_carm.add_argument("file") + p_carm.add_argument("variant") + p_carm.add_argument("fn") + p_carm.add_argument("--show", action="store_true") args = parser.parse_args() - {"list": cmd_list, "move": cmd_move, "compare": cmd_compare}[args.cmd](args) + commands = { + "list": cmd_list, + "move": cmd_move, + "compare": cmd_compare, + "extract-arm": cmd_extract_arm, + "compare-arm": cmd_compare_arm, + } + commands[args.cmd](args) if __name__ == "__main__": From 052a06832754928e81e546820d65585123684f82 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:51:03 +0900 Subject: [PATCH 51/67] chore(dev): keep adjacent items adjacent when moving them Moving a run of one-line consts used to put a blank line between each. The mover now joins items that were adjacent in the source without one. Co-Authored-By: Claude Opus 5.5 --- scripts/dev/move_rust_items.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/dev/move_rust_items.py b/scripts/dev/move_rust_items.py index 101fc755..54d8707a 100755 --- a/scripts/dev/move_rust_items.py +++ b/scripts/dev/move_rust_items.py @@ -22,8 +22,9 @@ type also moves every `impl` block for it. Items that share a name (for example `#[cfg]` variants of one function) move together. A `--*-mod tests` flag selects the items inside `mod tests { ... }` instead of the file's top -level; a destination gets a `mod tests` block if it lacks one. Moved text is -not re-indented, so run rustfmt on the destination afterwards. +level; a destination gets a `mod tests` block if it lacks one. Items that +were adjacent in the source stay adjacent. Moved text is not re-indented, so +run rustfmt on the destination afterwards. """ import argparse @@ -198,15 +199,17 @@ def cmd_move(args): missing = wanted - ({sp[3] for sp in chosen} | {impl_target(sp[3]) for sp in chosen}) if missing: sys.exit(f"not found in {args.src}: {' '.join(sorted(missing))}") - taken, moved = set(), [] + taken, block, prev_end = set(), "", None for s, _, e, _ in chosen: body = lines[s:e] + adjacent = prev_end == s and body and body[0].strip() while body and not body[0].strip(): body = body[1:] - moved.append("".join(body)) + block += ("" if adjacent or prev_end is None else "\n") + "".join(body) taken.update(range(s, e)) + prev_end = e Path(args.src).write_text("".join(l for i, l in enumerate(lines) if i not in taken)) - insert_into(Path(args.dst), "\n".join(moved), args.dst_mod) + insert_into(Path(args.dst), block, args.dst_mod) print(f"moved {len(chosen)} items ({len(taken)} lines) to {args.dst}") From 32c6454c3f147c01b97608e9765bc17f54dd6923 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:13:06 +0900 Subject: [PATCH 52/67] chore(dev): add a base-vs-new behavior diff for safe CLI paths behavior_diff.sh runs 19 commands that stop before any network, AX, send, or state write (shell completions for five shells, early validation errors in the context and local-DB commands, clap argument errors), each under a fresh empty HOME, and diffs two binaries' stdout, stderr, and exit codes. Base and this branch are identical across all 19. Co-Authored-By: Claude Opus 5.5 --- scripts/dev/behavior_diff.sh | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 scripts/dev/behavior_diff.sh diff --git a/scripts/dev/behavior_diff.sh b/scripts/dev/behavior_diff.sh new file mode 100755 index 00000000..8816f045 --- /dev/null +++ b/scripts/dev/behavior_diff.sh @@ -0,0 +1,53 @@ +#!/bin/sh +# Usage: behavior_diff.sh +# Runs commands that stop before any network, AX, send, or state write, +# each under a fresh empty HOME, and diffs base vs new output. Both binaries +# must be named openkakao-cli, because clap prints argv[0] in usage errors. +# Every case below was checked in source to fail or finish before touching +# KakaoTalk; keep it that way when adding cases. +base=$1 +new=$2 +out=$3 +rm -rf "$out" +mkdir -p "$out/base" "$out/new" +i=0 +while IFS= read -r line; do + [ -z "$line" ] && continue + i=$((i + 1)) + for side in base new; do + if [ "$side" = base ]; then bin=$base; else bin=$new; fi + home=$(mktemp -d) + # shellcheck disable=SC2086 + (cd "$home" && eval "env -i HOME=\"$home\" PATH=/usr/bin:/bin \"$bin\" $line" >"$out/$side/$i.out" 2>"$out/$side/$i.err" >"$out/$side/$i.err" + sed "s#$home##g" "$out/$side/$i.err" >"$out/$side/$i.err.n" && mv "$out/$side/$i.err.n" "$out/$side/$i.err" + sed "s#$home##g" "$out/$side/$i.out" >"$out/$side/$i.out.n" && mv "$out/$side/$i.out.n" "$out/$side/$i.out" + rm -rf "$home" + done + echo "$i: $line" >>"$out/cases.txt" +done <<'EOF' +completions bash +completions zsh +completions fish +completions powershell +completions elvish +context-sync-local --chat-id 1 --chat '' +--json context-sync-local --chat-id 1 --chat ' ' +context-repair-self-sends --chat-id 1 --chat '' +context-reply-bundle q --chat c --chat-id 1 +context-reply-bundle q --chat c --exclude-log-id 5 +local-chats +--json local-chats --groups +local-read 1 +local-search keyword +local-schema +--json local-schema +send +local-read +context-sync-local --chat-id 0 --chat c +EOF +if diff -r "$out/base" "$out/new" >"$out/behavior.diff"; then + echo "behavior: identical across $i cases" +else + echo "behavior: DIFFERS ($out/behavior.diff)" +fi From 770a238a65cf1fcf6b8c76902460c0314a52df36 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:07:19 +0900 Subject: [PATCH 53/67] refactor(cli): drop pass-through wrappers in main.rs validate_private_regular_file and leftover_queue_has_unknown_send only forwarded their arguments to the auto_reply_runtime functions of the same name, so main.rs now imports those directly. readiness_integer was serde_json::Value::as_i64 under another name; its 20 call sites now call as_i64 directly. The cfg(test)-only is_valid_auto_reply_transition_event_id wrapped one call, and its only test now asserts on parse_auto_reply_transition_event_id instead. Co-Authored-By: Claude Opus 5.5 --- src/main.rs | 76 +++++++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index c017e5cf..0be82175 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,6 +41,7 @@ use clap_complete::{generate, Shell}; use sha2::{Digest, Sha256}; use crate::auth_flow::{set_auth_policy, AuthPolicy}; +use crate::auto_reply_runtime::{leftover_queue_has_unknown_send, validate_private_regular_file}; use crate::commands::read::ReadCommandOptions; use crate::commands::watch::{WatchOptions, WebhookFormat}; use crate::config::load_config; @@ -2914,10 +2915,6 @@ fn is_empty_json_array(value: Option<&serde_json::Value>) -> bool { .is_some_and(Vec::is_empty) } -fn validate_private_regular_file(path: &Path, max_bytes: u64) -> Result<()> { - auto_reply_runtime::validate_private_regular_file(path, max_bytes) -} - const AUTO_REPLY_QUEUE_LEGACY_USER_VERSION: i64 = 0; const AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION: i64 = 2; const AUTO_REPLY_QUEUE_JOURNAL_MAX_ROWS: i64 = 4096; @@ -3180,11 +3177,6 @@ fn parse_auto_reply_transition_event_id(value: &str) -> Option<(i64, i64)> { Some((parse_field(chat_id)?, parse_field(log_id)?)) } -#[cfg(test)] -fn is_valid_auto_reply_transition_event_id(value: &str) -> bool { - parse_auto_reply_transition_event_id(value).is_some() -} - fn validate_stopped_clean_queue(queue_path: &Path, expected_chat_id: i64) -> Result<()> { const MAX_QUEUE_BYTES: u64 = 64 * 1024 * 1024; let expected_room_name = expected_chat_id.to_string(); @@ -3653,10 +3645,6 @@ fn validate_stopped_clean_queue(queue_path: &Path, expected_chat_id: i64) -> Res Ok(()) } -fn leftover_queue_has_unknown_send(queue_path: &Path, expected_chat_id: i64) -> Result<()> { - auto_reply_runtime::leftover_queue_has_unknown_send(queue_path, expected_chat_id) -} - fn leftover_supervisor_is_live(room_root: &Path, target: &local_db::LocalChat) -> Result { let status_path = room_root.join("supervisor-status.json"); validate_private_regular_file(&status_path, AUTO_REPLY_READINESS_MAX_BYTES)?; @@ -4098,7 +4086,7 @@ fn enrollment_cursor_authority_for_target( } let matches = targets .iter() - .filter(|item| item.get("chat_id").and_then(readiness_integer) == Some(target.chat_id)) + .filter(|item| item.get("chat_id").and_then(serde_json::Value::as_i64) == Some(target.chat_id)) .collect::>(); if matches.len() > 1 { anyhow::bail!("AutoReply enrollment target is duplicated"); @@ -5415,7 +5403,7 @@ fn cli_enrollment_reply_author_bindings( .context("CLI enrollment reply-author nickname invalid")?; let author_id = value .get("author_id") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .filter(|author_id| 0 < *author_id && *author_id < MAX_INT64) .context("CLI enrollment reply-author ID invalid")?; if previous_nickname.is_some_and(|previous| previous >= nickname) @@ -5453,7 +5441,7 @@ fn cli_enrollment_cursor_authority( != Some(&serde_json::Value::from( AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, )) - || authority.get("cursor_floor").and_then(readiness_integer) != Some(cursor_floor) + || authority.get("cursor_floor").and_then(serde_json::Value::as_i64) != Some(cursor_floor) { anyhow::bail!("CLI enrollment cursor authority invalid"); } @@ -5463,7 +5451,7 @@ fn cli_enrollment_cursor_authority( .context("CLI enrollment cursor authority kind missing")?; let attested_tail = authority .get("attested_db_last_log_id") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .filter(|value| 0 <= *value && *value < MAX_INT64) .context("CLI enrollment cursor authority tail invalid")?; let prior_owner = authority.get("prior_owner_id"); @@ -5483,7 +5471,7 @@ fn cli_enrollment_cursor_authority( .and_then(serde_json::Value::as_str) .is_none_or(str::is_empty) || prior_epoch - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .is_none_or(|value| !(1..MAX_INT64).contains(&value)) { anyhow::bail!("CLI replay cursor authority invalid"); @@ -5515,7 +5503,7 @@ fn validate_cli_enrollment_authority( .context("CLI enrollment targets invalid")?; let matching_targets = targets .iter() - .filter(|item| item.get("chat_id").and_then(readiness_integer) == Some(target)) + .filter(|item| item.get("chat_id").and_then(serde_json::Value::as_i64) == Some(target)) .collect::>(); if matching_targets.len() != 1 { anyhow::bail!("CLI enrollment target missing or ambiguous"); @@ -5539,7 +5527,7 @@ fn validate_cli_enrollment_authority( .get("chat_name") .and_then(serde_json::Value::as_str) != Some(chat_name) - || enrolled.get("last_log_id").and_then(readiness_integer) != Some(cursor_floor) + || enrolled.get("last_log_id").and_then(serde_json::Value::as_i64) != Some(cursor_floor) { anyhow::bail!("CLI enrollment target identity does not match DB readiness"); } @@ -5618,11 +5606,11 @@ fn validate_cli_enrollment_authority( .collect::>(); let matched_utf8_bytes = identity .get("matched_utf8_bytes") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .unwrap_or(-1); let attested_db_last_log_id = identity .get("attested_db_last_log_id") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .unwrap_or(-1); let transcript_sha256 = identity .get("transcript_sha256") @@ -5632,7 +5620,7 @@ fn validate_cli_enrollment_authority( || !json_object_has_exact_keys(identity, &expected_keys) || !(2..=20).contains(&parsed_log_ids.len()) || unique_log_ids.len() != parsed_log_ids.len() - || identity.get("matched_count").and_then(readiness_integer) + || identity.get("matched_count").and_then(serde_json::Value::as_i64) != Some(parsed_log_ids.len() as i64) || matched_utf8_bytes < 0 || (parsed_log_ids.len() >= 3 && matched_utf8_bytes < 24) @@ -5670,7 +5658,7 @@ fn require_cli_enrolled_reply_author( .context("CLI enrollment targets invalid")?; let matches = targets .iter() - .filter(|item| item.get("chat_id").and_then(readiness_integer) == Some(target)) + .filter(|item| item.get("chat_id").and_then(serde_json::Value::as_i64) == Some(target)) .collect::>(); if matches.len() != 1 { anyhow::bail!("CLI enrollment target missing or ambiguous"); @@ -5682,10 +5670,6 @@ fn require_cli_enrolled_reply_author( Ok(()) } -fn readiness_integer(value: &serde_json::Value) -> Option { - value.as_i64() -} - fn readiness_fresh(value: &serde_json::Value, now: f64) -> bool { let stamp = value.as_f64().or_else(|| value.as_i64().map(|v| v as f64)); stamp.is_some_and(|stamp| { @@ -5726,16 +5710,15 @@ fn require_persisted_auto_reply_readiness( if expected_owner.is_some_and(|expected| owner != expected) { anyhow::bail!("persisted supervisor owner does not match event"); } - let source_epoch = readiness_integer( - supervisor - .get("source_epoch") - .context("supervisor source epoch missing")?, - ) + let source_epoch = supervisor + .get("source_epoch") + .context("supervisor source epoch missing")? + .as_i64() .filter(|value| 0 < *value && *value < MAX_INT64) .context("supervisor source epoch invalid")?; let target = supervisor .get("target_chat_id") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .filter(|value| 0 < *value && *value < MAX_INT64) .context("supervisor target chat missing")?; let chat_name = supervisor @@ -5792,7 +5775,7 @@ fn require_persisted_auto_reply_readiness( || supervisor.get("ax_state") != Some(&serde_json::Value::String("healthy".to_string())) || supervisor .get("ax_pid") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .filter(|value| 0 < *value && *value < MAX_INT64) .is_none() || supervisor.get("ax_allow_send") != Some(&serde_json::Value::Bool(false)) @@ -5831,12 +5814,12 @@ fn require_persisted_auto_reply_readiness( } let db_epoch = db_state .get("source_epoch") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .filter(|value| 0 < *value && *value < MAX_INT64) .context("DB source epoch invalid")?; let db_target = db_state .get("target_chat_id") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .filter(|value| 0 < *value && *value < MAX_INT64) .context("DB target chat missing")?; let cli_state_v3 = std::env::var("OPENKAKAO_AUTO_REPLY_CLI").as_deref() == Ok("1"); @@ -5869,13 +5852,13 @@ fn require_persisted_auto_reply_readiness( } let watermark = db_state .get("acked_watermark") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .filter(|value| 0 <= *value && *value < MAX_INT64) .context("DB watermark invalid")?; if cli_state_v3 { let cursor_floor = db_state .get("cursor_floor") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .filter(|value| 0 <= *value && *value < MAX_INT64) .context("CLI DB cursor floor invalid")?; if cursor_floor > watermark @@ -5908,7 +5891,7 @@ fn require_persisted_auto_reply_readiness( } let persisted_last_observed = db_state .get("last_observed_log_id") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) .filter(|value| watermark <= *value && *value < MAX_INT64) .context("DB observed watermark invalid")?; if expected_last_observed.is_some_and(|expected| persisted_last_observed < expected) { @@ -5950,7 +5933,7 @@ fn require_persisted_auto_reply_readiness( || watermark != acked_set.iter().next_back().copied().unwrap_or(0) || db_state .get("last_observed_log_id") - .and_then(readiness_integer) + .and_then(serde_json::Value::as_i64) != Some(expected_last_observed) { anyhow::bail!("DB readiness cursor sets invalid"); @@ -10669,10 +10652,11 @@ connection.close() #[test] fn stopped_clean_queue_rejects_v2_invalid_journal_metadata() { - assert!(is_valid_auto_reply_transition_event_id("db:1:1")); - assert!(is_valid_auto_reply_transition_event_id( - "db:9223372036854775806:9223372036854775806" - )); + assert!(parse_auto_reply_transition_event_id("db:1:1").is_some()); + assert!( + parse_auto_reply_transition_event_id("db:9223372036854775806:9223372036854775806") + .is_some() + ); for invalid in [ "db:0:1", "db:01:1", @@ -10684,7 +10668,7 @@ connection.close() "db:9223372036854775807:1", "db:1:9223372036854775807", ] { - assert!(!is_valid_auto_reply_transition_event_id(invalid)); + assert!(parse_auto_reply_transition_event_id(invalid).is_none()); } let root = tempfile::tempdir().unwrap(); let room = stopped_clean_queue_test_room(root.path()); From 00aa9f55578fbe4838cceaf8a369a6baf29d9d06 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:16:56 +0900 Subject: [PATCH 54/67] style(cli): rustfmt main.rs before splitting it main.rs carried 16 rustfmt hunks. Formatting it first means the code moved out in later commits arrives already formatted, so those commits read as pure moves. Only main.rs is touched (rustfmt skip_children). Co-Authored-By: Claude Opus 5.5 --- src/main.rs | 119 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 70 insertions(+), 49 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0be82175..4869cccd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1968,7 +1968,10 @@ fn probe_auto_reply_llm_with_local_transport( .send() { if resp.status().is_success() { - eprintln!("advisory: OpenCodex authentication gateway verified OK ({})", ocx_model); + eprintln!( + "advisory: OpenCodex authentication gateway verified OK ({})", + ocx_model + ); return Ok(()); } } @@ -2226,7 +2229,9 @@ fn validate_auto_reply_runner(config: &config::OpenKakaoConfig) -> Result>(); if matches.len() > 1 { anyhow::bail!("AutoReply enrollment target is duplicated"); @@ -4598,8 +4604,7 @@ fn resolve_auto_reply_author_bindings( /// flapping). Each attempt re-reads both sides and re-runs the same read-only /// match, so the safety property is unchanged. const AUTO_REPLY_ATTEST_ATTEMPTS: usize = 3; -const AUTO_REPLY_ATTEST_RETRY_PAUSE: std::time::Duration = - std::time::Duration::from_millis(500); +const AUTO_REPLY_ATTEST_RETRY_PAUSE: std::time::Duration = std::time::Duration::from_millis(500); /// Run one bounded operation until it succeeds or the attempt budget is spent. /// @@ -4631,9 +4636,11 @@ fn auto_reply_attest_explicit_bindings( selectors: &[local_db::ChatSelector], targets: &[local_db::LocalChat], ) -> Result> { - retry_bounded(AUTO_REPLY_ATTEST_ATTEMPTS, AUTO_REPLY_ATTEST_RETRY_PAUSE, || { - auto_reply_attest_explicit_bindings_once(reader, selectors, targets) - }) + retry_bounded( + AUTO_REPLY_ATTEST_ATTEMPTS, + AUTO_REPLY_ATTEST_RETRY_PAUSE, + || auto_reply_attest_explicit_bindings_once(reader, selectors, targets), + ) } fn auto_reply_attest_explicit_bindings_once( @@ -5441,7 +5448,10 @@ fn cli_enrollment_cursor_authority( != Some(&serde_json::Value::from( AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, )) - || authority.get("cursor_floor").and_then(serde_json::Value::as_i64) != Some(cursor_floor) + || authority + .get("cursor_floor") + .and_then(serde_json::Value::as_i64) + != Some(cursor_floor) { anyhow::bail!("CLI enrollment cursor authority invalid"); } @@ -5527,7 +5537,10 @@ fn validate_cli_enrollment_authority( .get("chat_name") .and_then(serde_json::Value::as_str) != Some(chat_name) - || enrolled.get("last_log_id").and_then(serde_json::Value::as_i64) != Some(cursor_floor) + || enrolled + .get("last_log_id") + .and_then(serde_json::Value::as_i64) + != Some(cursor_floor) { anyhow::bail!("CLI enrollment target identity does not match DB readiness"); } @@ -5620,7 +5633,9 @@ fn validate_cli_enrollment_authority( || !json_object_has_exact_keys(identity, &expected_keys) || !(2..=20).contains(&parsed_log_ids.len()) || unique_log_ids.len() != parsed_log_ids.len() - || identity.get("matched_count").and_then(serde_json::Value::as_i64) + || identity + .get("matched_count") + .and_then(serde_json::Value::as_i64) != Some(parsed_log_ids.len() as i64) || matched_utf8_bytes < 0 || (parsed_log_ids.len() >= 3 && matched_utf8_bytes < 24) @@ -5714,8 +5729,8 @@ fn require_persisted_auto_reply_readiness( .get("source_epoch") .context("supervisor source epoch missing")? .as_i64() - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("supervisor source epoch invalid")?; + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("supervisor source epoch invalid")?; let target = supervisor .get("target_chat_id") .and_then(serde_json::Value::as_i64) @@ -7392,14 +7407,15 @@ fn main() -> Result<()> { let _generation_lock = if is_auto_reply_worker && !preflight { let home = dirs::home_dir().context("cannot resolve home directory")?; let lock_path = std::env::var_os("OPENKAKAO_AUTO_REPLY_GENERATION_LOCK") - .or_else(|| std::env::var_os("OPENKAKAO_AUTO_REPLY_LOCK")) - .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_GENERATION_LOCK")) - .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_LOCK")) - .filter(|value| !value.is_empty()) - .map(std::path::PathBuf::from) - .unwrap_or_else(|| { - auto_reply_service::default_state_root(&home).join(".owner-generation.lock") - }); + .or_else(|| std::env::var_os("OPENKAKAO_AUTO_REPLY_LOCK")) + .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_GENERATION_LOCK")) + .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_LOCK")) + .filter(|value| !value.is_empty()) + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + auto_reply_service::default_state_root(&home) + .join(".owner-generation.lock") + }); let lock = fs::OpenOptions::new() .create(true) .truncate(false) @@ -9245,10 +9261,7 @@ mod tests { None, "the local model allowlist must remain exact" ); - for exact in [ - config::MLX_27B_MODEL_ID, - config::MLX_27B_PREFIXED_MODEL_ID, - ] { + for exact in [config::MLX_27B_MODEL_ID, config::MLX_27B_PREFIXED_MODEL_ID] { assert_eq!( AutoReplyLlmChoice::from_model(exact), Some(AutoReplyLlmChoice::MlxQwen38TwentySevenB) @@ -9424,9 +9437,10 @@ mod tests { .is_err()); assert_eq!(malformed.requests.borrow().len(), 1); - let redirected = FakeLocalMlxProbeTransport::new(vec![ - FakeLocalMlxProbeTransport::json(302, serde_json::json!({"redirect": true})), - ]); + let redirected = FakeLocalMlxProbeTransport::new(vec![FakeLocalMlxProbeTransport::json( + 302, + serde_json::json!({"redirect": true}), + )]); let redirect_error = probe_auto_reply_llm_with_local_transport( &config, AutoReplyLlmChoice::MlxQwen38FlashNext, @@ -9454,12 +9468,12 @@ mod tests { "https://models.example.invalid/v1/models", ] { let error = match ReqwestLocalMlxProbeTransport.send(LocalMlxProbeRequest { - method: LocalMlxProbeMethod::Get, - url, - body: None, - timeout: LOCAL_MLX_PROBE_TIMEOUT, - max_response_bytes: LOCAL_MLX_MAX_RESPONSE_BYTES, - }) { + method: LocalMlxProbeMethod::Get, + url, + body: None, + timeout: LOCAL_MLX_PROBE_TIMEOUT, + max_response_bytes: LOCAL_MLX_MAX_RESPONSE_BYTES, + }) { Ok(_) => panic!("non-fixed endpoint must be rejected before transport"), Err(error) => error, }; @@ -11871,7 +11885,11 @@ connection.close() config.safety.allow_loco_write = true; assert!(require_loco_write(&config).is_ok()); } - fn bound_source_message(log_id: i64, author_id: i64, sender_name: &str) -> local_db::LocalMessage { + fn bound_source_message( + log_id: i64, + author_id: i64, + sender_name: &str, + ) -> local_db::LocalMessage { local_db::LocalMessage { log_id, chat_id: 417_780_809_780_519, @@ -11923,15 +11941,18 @@ connection.close() .contains("scheduled reply source row is unavailable")); } - #[test] - fn bound_source_message_reports_a_lookup_failure() { - let windowed: Vec = Vec::new(); - let error = resolve_bound_source_message(&windowed, 7, |_| anyhow::bail!("database gone")) - .expect_err("a read failure must surface instead of a silent miss"); - // The context wraps the read error, so the cause is on the chain - // rather than in the outermost message. - let chain = format!("{error:#}"); - assert!(chain.contains("database gone"), "{chain}"); - assert!(chain.contains("read the scheduled reply source row"), "{chain}"); - } + #[test] + fn bound_source_message_reports_a_lookup_failure() { + let windowed: Vec = Vec::new(); + let error = resolve_bound_source_message(&windowed, 7, |_| anyhow::bail!("database gone")) + .expect_err("a read failure must surface instead of a silent miss"); + // The context wraps the read error, so the cause is on the chain + // rather than in the outermost message. + let chain = format!("{error:#}"); + assert!(chain.contains("database gone"), "{chain}"); + assert!( + chain.contains("read the scheduled reply source row"), + "{chain}" + ); + } } From b8c7db8a6214935013698e284bfb61f8c4552088 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:22:15 +0900 Subject: [PATCH 55/67] refactor(cli): move context-sync-local into commands::context_sync_local The 195-line ContextSyncLocal arm of main() becomes cmd_context_sync_local, and the poll-page validation, bounded retry, and self-classification deferral helpers it alone uses move with it, along with their seven tests. The arm body is verbatim (compare-arm), the helpers are verbatim (compare), and the help dump is unchanged. The local_source_fence test fixture is pub(crate) because two worker-gate tests still in main.rs build fences with it. rustfmt also put commands/mod.rs in order (download before experiment). Co-Authored-By: Claude Opus 5.5 --- src/commands/context_sync_local.rs | 644 +++++++++++++++++++++++++++++ src/commands/mod.rs | 3 +- src/main.rs | 634 +--------------------------- 3 files changed, 655 insertions(+), 626 deletions(-) create mode 100644 src/commands/context_sync_local.rs diff --git a/src/commands/context_sync_local.rs b/src/commands/context_sync_local.rs new file mode 100644 index 00000000..2aea620b --- /dev/null +++ b/src/commands/context_sync_local.rs @@ -0,0 +1,644 @@ +use std::thread; +use std::time::Duration; + +use anyhow::{Context, Result}; + +use crate::context_sync_replica; +use crate::local_db; + +pub fn cmd_context_sync_local( + chat_id: i64, + chat: String, + db: Option, + queue: Option, + interest_only: bool, + json: bool, +) -> Result<()> { + if chat.trim().is_empty() || chat.len() > 512 || chat.chars().any(char::is_control) { + anyhow::bail!("context sync chat name is invalid"); + } + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + openkakao_cli::context::ensure_live_context_schema(&db_path)?; + let confirmed_sends = match queue.as_deref() { + Some(path) => { + openkakao_cli::context::confirmed_self_sends_from_queue(std::path::Path::new(path))? + } + None => Vec::new(), + }; + let source = context_sync_replica::ContextSyncReplicaSource::discover()?; + let account_fingerprint = source.account_fingerprint().to_owned(); + let account_user_id = source.account_user_id(); + let state = + openkakao_cli::context::live_context_sync_state(&db_path, &account_fingerprint, chat_id)?; + if let Some(state) = state.as_ref() { + let placeholder = format!("그룹:{chat_id}"); + // 자리표시자로 저장된 이름 없는 그룹방은 화면 이름으로 동기화해도 + // 같은 방이다. 다른 방이면 그대로 거부한다. + if (state.chat != chat && state.chat != placeholder) || state.chat_id != chat_id { + anyhow::bail!("context sync source identity changed"); + } + } + let mut checkpoint = state + .as_ref() + .map(|state| state.checkpoint_log_id) + .unwrap_or(0); + let mut totals = serde_json::Map::new(); + for key in [ + "inserted_events", + "duplicate_events", + "indexed_messages", + "style_messages", + "response_samples", + "recipient_style_samples", + ] { + totals.insert(key.to_string(), serde_json::Value::from(0_u64)); + } + let mut pages = 0_u64; + let mut deferred = None; + let authoritative; + loop { + let envelope = context_sync_local_poll_page_with_bounded_retry( + chat_id, + &chat, + checkpoint, + |expected_chat_id, expected_checkpoint| { + let replica = source.open_fresh()?; + replica.reader().poll_after( + expected_chat_id, + local_db::LOCAL_POLL_MAX_ROWS, + Some(expected_checkpoint), + ) + }, + thread::sleep, + )?; + + let outgoing = envelope + .messages + .iter() + .filter(|message| message.author_id == account_user_id) + .map(|message| openkakao_cli::context::OutgoingSelfEvent { + chat_id: message.chat_id, + log_id: message.log_id, + message: message.message.trim().to_string(), + sent_at: message.sent_at, + }) + .collect::>(); + let classifications = openkakao_cli::context::classify_auto_generated_self_events( + &db_path, + &chat, + &outgoing, + &confirmed_sends, + )?; + let classifications = classifications + .into_iter() + .map(|item| (item.log_id, item)) + .collect::>(); + let now = chrono::Utc::now().timestamp(); + let deferred_event = envelope + .messages + .iter() + .enumerate() + .find_map(|(index, message)| { + if message.author_id != account_user_id { + return None; + } + self_classification_retry_after( + message.sent_at, + now, + classifications.get(&message.log_id), + ) + .map(|retry_after_seconds| (index, retry_after_seconds)) + }); + let ingest_count = deferred_event + .map(|(index, _)| index) + .unwrap_or(envelope.messages.len()); + let events = envelope + .messages + .iter() + .take(ingest_count) + .map(|message| openkakao_cli::context::LiveContextEvent { + chat_id: message.chat_id, + log_id: message.log_id, + sender_name: message.sender_name.trim().to_string(), + message: message.message.trim().to_string(), + sent_at: message.sent_at, + is_self: local_db::is_self_author(message.author_id, account_user_id), + exclude_from_learning: classifications.get(&message.log_id).is_some_and(|item| { + !item.auto_generated && item.reason != "no_exact_sent_match" + }), + auto_generated: classifications + .get(&message.log_id) + .is_some_and(|item| item.auto_generated), + attachment: message.attachment.clone(), + message_type: message.message_type, + interest_only, + }) + .collect::>(); + let complete = deferred_event.is_none() + && !envelope.completeness.has_more + && matches!(envelope.completeness.status.as_str(), "complete" | "empty"); + if !complete && events.is_empty() && deferred_event.is_none() { + anyhow::bail!("reconcile_required"); + } + let result = openkakao_cli::context::ingest_live_context_events( + &db_path, + &account_fingerprint, + chat_id, + &chat, + checkpoint, + &events, + complete, + complete && !interest_only, + )?; + pages += 1; + for (key, value) in [ + ("inserted_events", result.inserted_events), + ("duplicate_events", result.duplicate_events), + ("indexed_messages", result.indexed_messages), + ("style_messages", result.style_messages), + ("response_samples", result.response_samples), + ("recipient_style_samples", result.recipient_style_samples), + ] { + let current = totals + .get(key) + .and_then(serde_json::Value::as_u64) + .unwrap_or(0); + totals.insert( + key.to_string(), + serde_json::Value::from(current + value as u64), + ); + } + checkpoint = result.checkpoint_log_id; + if let Some((index, retry_after_seconds)) = deferred_event { + deferred = Some(serde_json::json!({ + "reason": "fresh_unmatched_self", + "log_id": envelope.messages[index].log_id, + "retry_after_seconds": retry_after_seconds, + })); + authoritative = result.authoritative; + break; + } + if complete { + authoritative = result.authoritative; + break; + } + } + let output = serde_json::json!({ + "schema_version": 1, + "action": "context_sync_local", + "chat_id": chat_id, + "chat": chat, + "checkpoint_log_id": checkpoint, + "pages": pages, + "authoritative": authoritative, + "deferred": deferred, + "totals": totals, + "network": false, + }); + if json { + println!("{}", serde_json::to_string_pretty(&output)?); + } else { + println!( + "Synchronized {} local pages for '{}' through log {} (authoritative={}).", + pages, chat, checkpoint, authoritative + ); + } + Ok(()) +} + +const SELF_CLASSIFICATION_GRACE_SECONDS: i64 = 180; + +const SELF_CLASSIFICATION_RETRY_MAX_SECONDS: i64 = 5; + +// KakaoTalk can publish NTChatRoom.lastLogId just before the matching +// NTChatMessage row is visible to a new read-only SQLite snapshot. Keep this +// retry window deliberately short and fixed: context sync owns no delivery +// capability, and it must not advance its durable checkpoint from a gap page. +const CONTEXT_SYNC_LOCAL_POLL_RETRY_DELAYS_MS: [u64; 3] = [250, 500, 1_000]; + +fn local_message_snapshot_eq( + left: &local_db::LocalMessage, + right: &local_db::LocalMessage, +) -> bool { + left.log_id == right.log_id + && left.chat_id == right.chat_id + && left.author_id == right.author_id + && left.is_self == right.is_self + && left.sender_name == right.sender_name + && left.message == right.message + && left.attachment == right.attachment + && left.message_type == right.message_type + && left.sent_at == right.sent_at +} + +/// Validate one context-sync page and return whether it is the one narrowly +/// retriable SQLite snapshot race. Any other malformed or incomplete proof +/// remains fail-closed. +fn validate_context_sync_local_poll_page( + envelope: &local_db::LocalPollEnvelope, + expected_chat_id: i64, + expected_chat_name: &str, + expected_checkpoint: i64, +) -> Result { + let completeness = &envelope.completeness; + if expected_chat_id <= 0 + || expected_checkpoint < 0 + || envelope.schema_version != local_db::LOCAL_POLL_SCHEMA_VERSION + || envelope.chat.chat_id != expected_chat_id + || completeness.after_log_id != expected_checkpoint + || envelope.chat.last_log_id != completeness.chat_last_log_id + || completeness.chat_last_log_id < 0 + || completeness.id_domain != "global_sparse" + || completeness.returned_count != envelope.messages.len() as i64 + || completeness.row_count < completeness.returned_count + { + anyhow::bail!("reconcile_required"); + } + let database_name = envelope + .chat + .database_chat_name + .as_deref() + .unwrap_or(&envelope.chat.chat_name) + .trim(); + if !database_name.is_empty() && database_name != expected_chat_name { + anyhow::bail!("context sync chat name no longer matches the local database"); + } + + if envelope + .messages + .iter() + .enumerate() + .any(|(index, message)| { + message.chat_id != expected_chat_id + || message.log_id <= expected_checkpoint + || message.log_id > completeness.chat_last_log_id + || index + .checked_sub(1) + .is_some_and(|previous| envelope.messages[previous].log_id >= message.log_id) + }) + { + anyhow::bail!("reconcile_required"); + } + let first_log_id = envelope.messages.first().map(|message| message.log_id); + let last_log_id = envelope.messages.last().map(|message| message.log_id); + if completeness.first_log_id != first_log_id || completeness.last_log_id != last_log_id { + anyhow::bail!("reconcile_required"); + } + + if completeness.has_gap { + let visible_tail = last_log_id.unwrap_or(expected_checkpoint); + let retriable = completeness.status == "gap" + && completeness.proof == "reconcile_required" + && !completeness.has_more + && completeness.row_count == completeness.returned_count + && completeness.available_max_log_id == last_log_id + && completeness.chat_last_log_id > visible_tail; + if !retriable { + anyhow::bail!("reconcile_required"); + } + return Ok(true); + } + + if completeness.proof != "sqlite_snapshot_rowset" { + anyhow::bail!("reconcile_required"); + } + let valid = match completeness.status.as_str() { + "partial" => { + completeness.has_more + && !envelope.messages.is_empty() + && completeness.row_count > completeness.returned_count + && completeness + .available_max_log_id + .zip(last_log_id) + .is_some_and(|(available, returned)| available > returned) + } + "complete" => { + !completeness.has_more + && !envelope.messages.is_empty() + && completeness.row_count == completeness.returned_count + && completeness.available_max_log_id == last_log_id + && completeness.chat_last_log_id == last_log_id.unwrap_or_default() + } + "empty" => { + !completeness.has_more + && envelope.messages.is_empty() + && completeness.row_count == 0 + && completeness.available_max_log_id.is_none() + && completeness.chat_last_log_id == expected_checkpoint + } + _ => false, + }; + if !valid { + anyhow::bail!("reconcile_required"); + } + Ok(false) +} + +/// Fetch one page without ever mutating `expected_checkpoint` between retry +/// attempts. A retry snapshot must extend the previous one monotonically and +/// keep the exact numeric chat/checkpoint identity. +fn context_sync_local_poll_page_with_bounded_retry( + expected_chat_id: i64, + expected_chat_name: &str, + expected_checkpoint: i64, + mut poll: P, + mut sleep: S, +) -> Result +where + P: FnMut(i64, i64) -> Result, + S: FnMut(Duration), +{ + let mut previous_gap: Option = None; + for retry_delay_ms in CONTEXT_SYNC_LOCAL_POLL_RETRY_DELAYS_MS + .into_iter() + .map(Some) + .chain(std::iter::once(None)) + { + let envelope = poll(expected_chat_id, expected_checkpoint)?; + let retriable_gap = validate_context_sync_local_poll_page( + &envelope, + expected_chat_id, + expected_chat_name, + expected_checkpoint, + )?; + + if let Some(previous) = previous_gap.as_ref() { + let previous_available = previous.completeness.available_max_log_id.unwrap_or(0); + let current_available = envelope.completeness.available_max_log_id.unwrap_or(0); + let monotonic = envelope.completeness.chat_last_log_id + >= previous.completeness.chat_last_log_id + && envelope.completeness.row_count >= previous.completeness.row_count + && current_available >= previous_available + && envelope.messages.len() >= previous.messages.len() + && previous + .messages + .iter() + .zip(&envelope.messages) + .all(|(left, right)| local_message_snapshot_eq(left, right)); + if !monotonic { + anyhow::bail!("reconcile_required"); + } + } + + if !retriable_gap { + return Ok(envelope); + } + let retry_delay_ms = retry_delay_ms.context("context_sync_snapshot_retry_exhausted")?; + previous_gap = Some(envelope); + sleep(Duration::from_millis(retry_delay_ms)); + } + unreachable!("bounded context-sync retry loop must return or fail") +} + +/// Return a short caller-managed retry delay while a fresh outgoing row may +/// still be racing the durable `reply_decisions(status='sent')` projection. +/// The sync command must never sleep here: its caller owns liveness and the +/// unclassified row must remain strictly beyond the committed checkpoint. +fn self_classification_retry_after( + sent_at: i64, + now: i64, + classification: Option<&openkakao_cli::context::AutoGeneratedSelfEventClassification>, +) -> Option { + let classification = classification?; + if classification.auto_generated || classification.reason != "no_exact_sent_match" { + return None; + } + let remaining = sent_at + .saturating_add(SELF_CLASSIFICATION_GRACE_SECONDS) + .saturating_sub(now); + (remaining > 0).then(|| remaining.clamp(1, SELF_CLASSIFICATION_RETRY_MAX_SECONDS)) +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + + pub(crate) fn local_source_fence( + chat_id: i64, + after_log_id: i64, + chat_last_log_id: i64, + newer_log_ids: &[i64], + ) -> local_db::LocalPollEnvelope { + let messages = newer_log_ids + .iter() + .map(|log_id| local_db::LocalMessage { + log_id: *log_id, + chat_id, + author_id: 7, + is_self: false, + sender_name: String::new(), + message: String::new(), + attachment: String::new(), + message_type: 1, + sent_at: 0, + }) + .collect::>(); + let first_log_id = messages.first().map(|message| message.log_id); + let last_log_id = messages.last().map(|message| message.log_id); + local_db::LocalPollEnvelope { + schema_version: local_db::LOCAL_POLL_SCHEMA_VERSION, + chat: local_db::LocalChat { + chat_id, + chat_type: 0, + chat_name: "부자멘토멘티".to_string(), + database_chat_name: None, + active_members_count: 4, + last_log_id: chat_last_log_id, + last_updated_at: 0, + unread_count: 0, + display_name: String::new(), + }, + messages, + completeness: local_db::LocalPollCompleteness { + status: if newer_log_ids.is_empty() { + "empty".to_string() + } else { + "complete".to_string() + }, + after_log_id, + first_log_id, + last_log_id, + chat_last_log_id, + row_count: newer_log_ids.len() as i64, + returned_count: newer_log_ids.len() as i64, + available_max_log_id: last_log_id, + id_domain: "global_sparse".to_string(), + has_gap: false, + has_more: false, + proof: "sqlite_snapshot_rowset".to_string(), + }, + } + } + + fn transient_local_source_gap( + chat_id: i64, + after_log_id: i64, + chat_last_log_id: i64, + visible_log_ids: &[i64], + ) -> local_db::LocalPollEnvelope { + let mut envelope = + local_source_fence(chat_id, after_log_id, chat_last_log_id, visible_log_ids); + envelope.completeness.status = "gap".to_string(); + envelope.completeness.has_gap = true; + envelope.completeness.proof = "reconcile_required".to_string(); + envelope + } + + #[test] + fn context_sync_local_retries_gap_without_mutating_checkpoint() { + let gap = transient_local_source_gap(42, 100, 101, &[]); + let complete = local_source_fence(42, 100, 101, &[101]); + let mut responses = std::collections::VecDeque::from([gap, complete]); + let mut calls = Vec::new(); + let mut sleeps = Vec::new(); + + let envelope = context_sync_local_poll_page_with_bounded_retry( + 42, + "부자멘토멘티", + 100, + |chat_id, checkpoint| { + calls.push((chat_id, checkpoint)); + Ok(responses.pop_front().expect("bounded fake poll response")) + }, + |delay| sleeps.push(delay.as_millis()), + ) + .expect("a monotonic complete snapshot should recover the transient gap"); + + assert_eq!(calls, [(42, 100), (42, 100)]); + assert_eq!(sleeps, [250]); + assert_eq!(envelope.completeness.status, "complete"); + assert_eq!(envelope.completeness.after_log_id, 100); + assert_eq!(envelope.completeness.last_log_id, Some(101)); + } + + #[test] + fn context_sync_local_fails_closed_on_persistent_gap() { + let gap = transient_local_source_gap(42, 100, 101, &[]); + let mut responses = + std::collections::VecDeque::from([gap.clone(), gap.clone(), gap.clone(), gap]); + let mut calls = Vec::new(); + let mut sleeps = Vec::new(); + + let error = context_sync_local_poll_page_with_bounded_retry( + 42, + "부자멘토멘티", + 100, + |chat_id, checkpoint| { + calls.push((chat_id, checkpoint)); + Ok(responses.pop_front().expect("bounded fake poll response")) + }, + |delay| sleeps.push(delay.as_millis()), + ) + .expect_err("a persistent source gap must remain fail-closed"); + + assert_eq!(error.to_string(), "context_sync_snapshot_retry_exhausted"); + assert_eq!(calls, [(42, 100); 4]); + assert_eq!(sleeps, [250, 500, 1_000]); + } + + #[test] + fn context_sync_local_retry_rejects_chat_or_checkpoint_identity_drift() { + let cases = [("chat", local_source_fence(43, 100, 101, &[101])), { + let mut checkpoint_drift = local_source_fence(42, 100, 101, &[101]); + checkpoint_drift.completeness.after_log_id = 99; + ("checkpoint", checkpoint_drift) + }]; + + for (label, drifted) in cases { + let gap = transient_local_source_gap(42, 100, 101, &[]); + let mut responses = std::collections::VecDeque::from([gap, drifted]); + let mut calls = Vec::new(); + let mut sleeps = Vec::new(); + let error = context_sync_local_poll_page_with_bounded_retry( + 42, + "부자멘토멘티", + 100, + |chat_id, checkpoint| { + calls.push((chat_id, checkpoint)); + Ok(responses.pop_front().expect("bounded fake poll response")) + }, + |delay| sleeps.push(delay.as_millis()), + ) + .expect_err("poll identity drift must terminate the retry"); + assert_eq!(error.to_string(), "reconcile_required", "{label}"); + assert_eq!(calls, [(42, 100), (42, 100)], "{label}"); + assert_eq!(sleeps, [250], "{label}"); + } + } + + #[test] + fn context_sync_local_retry_rejects_invalid_or_non_monotonic_snapshots() { + let mut invalid_schema = transient_local_source_gap(42, 100, 101, &[]); + invalid_schema.schema_version += 1; + let mut invalid_gap = transient_local_source_gap(42, 100, 101, &[]); + invalid_gap.completeness.status = "unknown".to_string(); + for invalid in [invalid_schema, invalid_gap] { + let mut sleeps = Vec::new(); + let error = context_sync_local_poll_page_with_bounded_retry( + 42, + "부자멘토멘티", + 100, + |_chat_id, _checkpoint| Ok(invalid.clone()), + |delay| sleeps.push(delay.as_millis()), + ) + .expect_err("an invalid first snapshot must not enter the retry path"); + assert_eq!(error.to_string(), "reconcile_required"); + assert!(sleeps.is_empty()); + } + + let first = transient_local_source_gap(42, 100, 103, &[101]); + let regressed = transient_local_source_gap(42, 100, 103, &[]); + let mut responses = std::collections::VecDeque::from([first, regressed]); + let mut sleeps = Vec::new(); + let error = context_sync_local_poll_page_with_bounded_retry( + 42, + "부자멘토멘티", + 100, + |_chat_id, _checkpoint| Ok(responses.pop_front().expect("bounded fake poll response")), + |delay| sleeps.push(delay.as_millis()), + ) + .expect_err("a retry snapshot may not regress its visible row prefix"); + assert_eq!(error.to_string(), "reconcile_required"); + assert_eq!(sleeps, [250]); + } + + #[test] + fn fresh_unmatched_self_classification_defers_without_blocking() { + let unmatched = openkakao_cli::context::AutoGeneratedSelfEventClassification { + chat_id: 7, + log_id: 11, + auto_generated: false, + matched_event_id: None, + reason: "no_exact_sent_match".to_string(), + }; + assert_eq!( + self_classification_retry_after(1_000, 1_001, Some(&unmatched)), + Some(SELF_CLASSIFICATION_RETRY_MAX_SECONDS) + ); + assert_eq!( + self_classification_retry_after(1_000, 1_179, Some(&unmatched)), + Some(1) + ); + assert_eq!( + self_classification_retry_after(1_000, 1_180, Some(&unmatched)), + None + ); + + let mut matched = unmatched.clone(); + matched.auto_generated = true; + matched.reason = "unique_exact_sent_match".to_string(); + assert_eq!( + self_classification_retry_after(1_000, 1_001, Some(&matched)), + None + ); + + let mut ambiguous = unmatched; + ambiguous.reason = "multiple_sent_matches".to_string(); + assert_eq!( + self_classification_retry_after(1_000, 1_001, Some(&ambiguous)), + None + ); + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6d8f3747..a44b411f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -4,10 +4,11 @@ pub mod auto_reply_host; pub mod ax_read; pub mod ax_watch; pub mod chats; +pub mod context_sync_local; pub mod dataset; pub mod doctor; -pub mod experiment; pub mod download; +pub mod experiment; pub mod local_send; pub mod members; pub mod probe; diff --git a/src/main.rs b/src/main.rs index 4869cccd..66601d00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,207 +48,6 @@ use crate::config::load_config; use crate::util::{format_outgoing_message, NO_COLOR, VERSION}; use openkakao_cli::auto_reply_service; -const SELF_CLASSIFICATION_GRACE_SECONDS: i64 = 180; -const SELF_CLASSIFICATION_RETRY_MAX_SECONDS: i64 = 5; -// KakaoTalk can publish NTChatRoom.lastLogId just before the matching -// NTChatMessage row is visible to a new read-only SQLite snapshot. Keep this -// retry window deliberately short and fixed: context sync owns no delivery -// capability, and it must not advance its durable checkpoint from a gap page. -const CONTEXT_SYNC_LOCAL_POLL_RETRY_DELAYS_MS: [u64; 3] = [250, 500, 1_000]; - -fn local_message_snapshot_eq( - left: &local_db::LocalMessage, - right: &local_db::LocalMessage, -) -> bool { - left.log_id == right.log_id - && left.chat_id == right.chat_id - && left.author_id == right.author_id - && left.is_self == right.is_self - && left.sender_name == right.sender_name - && left.message == right.message - && left.attachment == right.attachment - && left.message_type == right.message_type - && left.sent_at == right.sent_at -} - -/// Validate one context-sync page and return whether it is the one narrowly -/// retriable SQLite snapshot race. Any other malformed or incomplete proof -/// remains fail-closed. -fn validate_context_sync_local_poll_page( - envelope: &local_db::LocalPollEnvelope, - expected_chat_id: i64, - expected_chat_name: &str, - expected_checkpoint: i64, -) -> Result { - let completeness = &envelope.completeness; - if expected_chat_id <= 0 - || expected_checkpoint < 0 - || envelope.schema_version != local_db::LOCAL_POLL_SCHEMA_VERSION - || envelope.chat.chat_id != expected_chat_id - || completeness.after_log_id != expected_checkpoint - || envelope.chat.last_log_id != completeness.chat_last_log_id - || completeness.chat_last_log_id < 0 - || completeness.id_domain != "global_sparse" - || completeness.returned_count != envelope.messages.len() as i64 - || completeness.row_count < completeness.returned_count - { - anyhow::bail!("reconcile_required"); - } - let database_name = envelope - .chat - .database_chat_name - .as_deref() - .unwrap_or(&envelope.chat.chat_name) - .trim(); - if !database_name.is_empty() && database_name != expected_chat_name { - anyhow::bail!("context sync chat name no longer matches the local database"); - } - - if envelope - .messages - .iter() - .enumerate() - .any(|(index, message)| { - message.chat_id != expected_chat_id - || message.log_id <= expected_checkpoint - || message.log_id > completeness.chat_last_log_id - || index - .checked_sub(1) - .is_some_and(|previous| envelope.messages[previous].log_id >= message.log_id) - }) - { - anyhow::bail!("reconcile_required"); - } - let first_log_id = envelope.messages.first().map(|message| message.log_id); - let last_log_id = envelope.messages.last().map(|message| message.log_id); - if completeness.first_log_id != first_log_id || completeness.last_log_id != last_log_id { - anyhow::bail!("reconcile_required"); - } - - if completeness.has_gap { - let visible_tail = last_log_id.unwrap_or(expected_checkpoint); - let retriable = completeness.status == "gap" - && completeness.proof == "reconcile_required" - && !completeness.has_more - && completeness.row_count == completeness.returned_count - && completeness.available_max_log_id == last_log_id - && completeness.chat_last_log_id > visible_tail; - if !retriable { - anyhow::bail!("reconcile_required"); - } - return Ok(true); - } - - if completeness.proof != "sqlite_snapshot_rowset" { - anyhow::bail!("reconcile_required"); - } - let valid = match completeness.status.as_str() { - "partial" => { - completeness.has_more - && !envelope.messages.is_empty() - && completeness.row_count > completeness.returned_count - && completeness - .available_max_log_id - .zip(last_log_id) - .is_some_and(|(available, returned)| available > returned) - } - "complete" => { - !completeness.has_more - && !envelope.messages.is_empty() - && completeness.row_count == completeness.returned_count - && completeness.available_max_log_id == last_log_id - && completeness.chat_last_log_id == last_log_id.unwrap_or_default() - } - "empty" => { - !completeness.has_more - && envelope.messages.is_empty() - && completeness.row_count == 0 - && completeness.available_max_log_id.is_none() - && completeness.chat_last_log_id == expected_checkpoint - } - _ => false, - }; - if !valid { - anyhow::bail!("reconcile_required"); - } - Ok(false) -} - -/// Fetch one page without ever mutating `expected_checkpoint` between retry -/// attempts. A retry snapshot must extend the previous one monotonically and -/// keep the exact numeric chat/checkpoint identity. -fn context_sync_local_poll_page_with_bounded_retry( - expected_chat_id: i64, - expected_chat_name: &str, - expected_checkpoint: i64, - mut poll: P, - mut sleep: S, -) -> Result -where - P: FnMut(i64, i64) -> Result, - S: FnMut(Duration), -{ - let mut previous_gap: Option = None; - for retry_delay_ms in CONTEXT_SYNC_LOCAL_POLL_RETRY_DELAYS_MS - .into_iter() - .map(Some) - .chain(std::iter::once(None)) - { - let envelope = poll(expected_chat_id, expected_checkpoint)?; - let retriable_gap = validate_context_sync_local_poll_page( - &envelope, - expected_chat_id, - expected_chat_name, - expected_checkpoint, - )?; - - if let Some(previous) = previous_gap.as_ref() { - let previous_available = previous.completeness.available_max_log_id.unwrap_or(0); - let current_available = envelope.completeness.available_max_log_id.unwrap_or(0); - let monotonic = envelope.completeness.chat_last_log_id - >= previous.completeness.chat_last_log_id - && envelope.completeness.row_count >= previous.completeness.row_count - && current_available >= previous_available - && envelope.messages.len() >= previous.messages.len() - && previous - .messages - .iter() - .zip(&envelope.messages) - .all(|(left, right)| local_message_snapshot_eq(left, right)); - if !monotonic { - anyhow::bail!("reconcile_required"); - } - } - - if !retriable_gap { - return Ok(envelope); - } - let retry_delay_ms = retry_delay_ms.context("context_sync_snapshot_retry_exhausted")?; - previous_gap = Some(envelope); - sleep(Duration::from_millis(retry_delay_ms)); - } - unreachable!("bounded context-sync retry loop must return or fail") -} - -/// Return a short caller-managed retry delay while a fresh outgoing row may -/// still be racing the durable `reply_decisions(status='sent')` projection. -/// The sync command must never sleep here: its caller owns liveness and the -/// unclassified row must remain strictly beyond the committed checkpoint. -fn self_classification_retry_after( - sent_at: i64, - now: i64, - classification: Option<&openkakao_cli::context::AutoGeneratedSelfEventClassification>, -) -> Option { - let classification = classification?; - if classification.auto_generated || classification.reason != "no_exact_sent_match" { - return None; - } - let remaining = sent_at - .saturating_add(SELF_CLASSIFICATION_GRACE_SECONDS) - .saturating_sub(now); - (remaining > 0).then(|| remaining.clamp(1, SELF_CLASSIFICATION_RETRY_MAX_SECONDS)) -} - fn validate_service_bootstrap_paths(command: &Commands) -> Result> { match command { Commands::AxWatch { @@ -6858,203 +6657,14 @@ fn main() -> Result<()> { db, queue, interest_only, - } => { - if chat.trim().is_empty() || chat.len() > 512 || chat.chars().any(char::is_control) { - anyhow::bail!("context sync chat name is invalid"); - } - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - openkakao_cli::context::ensure_live_context_schema(&db_path)?; - let confirmed_sends = match queue.as_deref() { - Some(path) => openkakao_cli::context::confirmed_self_sends_from_queue( - std::path::Path::new(path), - )?, - None => Vec::new(), - }; - let source = context_sync_replica::ContextSyncReplicaSource::discover()?; - let account_fingerprint = source.account_fingerprint().to_owned(); - let account_user_id = source.account_user_id(); - let state = openkakao_cli::context::live_context_sync_state( - &db_path, - &account_fingerprint, - chat_id, - )?; - if let Some(state) = state.as_ref() { - let placeholder = format!("그룹:{chat_id}"); - // 자리표시자로 저장된 이름 없는 그룹방은 화면 이름으로 동기화해도 - // 같은 방이다. 다른 방이면 그대로 거부한다. - if (state.chat != chat && state.chat != placeholder) || state.chat_id != chat_id { - anyhow::bail!("context sync source identity changed"); - } - } - let mut checkpoint = state - .as_ref() - .map(|state| state.checkpoint_log_id) - .unwrap_or(0); - let mut totals = serde_json::Map::new(); - for key in [ - "inserted_events", - "duplicate_events", - "indexed_messages", - "style_messages", - "response_samples", - "recipient_style_samples", - ] { - totals.insert(key.to_string(), serde_json::Value::from(0_u64)); - } - let mut pages = 0_u64; - let mut deferred = None; - let authoritative; - loop { - let envelope = context_sync_local_poll_page_with_bounded_retry( - chat_id, - &chat, - checkpoint, - |expected_chat_id, expected_checkpoint| { - let replica = source.open_fresh()?; - replica.reader().poll_after( - expected_chat_id, - local_db::LOCAL_POLL_MAX_ROWS, - Some(expected_checkpoint), - ) - }, - thread::sleep, - )?; - - let outgoing = envelope - .messages - .iter() - .filter(|message| message.author_id == account_user_id) - .map(|message| openkakao_cli::context::OutgoingSelfEvent { - chat_id: message.chat_id, - log_id: message.log_id, - message: message.message.trim().to_string(), - sent_at: message.sent_at, - }) - .collect::>(); - let classifications = openkakao_cli::context::classify_auto_generated_self_events( - &db_path, - &chat, - &outgoing, - &confirmed_sends, - )?; - let classifications = classifications - .into_iter() - .map(|item| (item.log_id, item)) - .collect::>(); - let now = chrono::Utc::now().timestamp(); - let deferred_event = - envelope - .messages - .iter() - .enumerate() - .find_map(|(index, message)| { - if message.author_id != account_user_id { - return None; - } - self_classification_retry_after( - message.sent_at, - now, - classifications.get(&message.log_id), - ) - .map(|retry_after_seconds| (index, retry_after_seconds)) - }); - let ingest_count = deferred_event - .map(|(index, _)| index) - .unwrap_or(envelope.messages.len()); - let events = envelope - .messages - .iter() - .take(ingest_count) - .map(|message| openkakao_cli::context::LiveContextEvent { - chat_id: message.chat_id, - log_id: message.log_id, - sender_name: message.sender_name.trim().to_string(), - message: message.message.trim().to_string(), - sent_at: message.sent_at, - is_self: local_db::is_self_author(message.author_id, account_user_id), - exclude_from_learning: classifications.get(&message.log_id).is_some_and( - |item| !item.auto_generated && item.reason != "no_exact_sent_match", - ), - auto_generated: classifications - .get(&message.log_id) - .is_some_and(|item| item.auto_generated), - attachment: message.attachment.clone(), - message_type: message.message_type, - interest_only, - }) - .collect::>(); - let complete = deferred_event.is_none() - && !envelope.completeness.has_more - && matches!(envelope.completeness.status.as_str(), "complete" | "empty"); - if !complete && events.is_empty() && deferred_event.is_none() { - anyhow::bail!("reconcile_required"); - } - let result = openkakao_cli::context::ingest_live_context_events( - &db_path, - &account_fingerprint, - chat_id, - &chat, - checkpoint, - &events, - complete, - complete && !interest_only, - )?; - pages += 1; - for (key, value) in [ - ("inserted_events", result.inserted_events), - ("duplicate_events", result.duplicate_events), - ("indexed_messages", result.indexed_messages), - ("style_messages", result.style_messages), - ("response_samples", result.response_samples), - ("recipient_style_samples", result.recipient_style_samples), - ] { - let current = totals - .get(key) - .and_then(serde_json::Value::as_u64) - .unwrap_or(0); - totals.insert( - key.to_string(), - serde_json::Value::from(current + value as u64), - ); - } - checkpoint = result.checkpoint_log_id; - if let Some((index, retry_after_seconds)) = deferred_event { - deferred = Some(serde_json::json!({ - "reason": "fresh_unmatched_self", - "log_id": envelope.messages[index].log_id, - "retry_after_seconds": retry_after_seconds, - })); - authoritative = result.authoritative; - break; - } - if complete { - authoritative = result.authoritative; - break; - } - } - let output = serde_json::json!({ - "schema_version": 1, - "action": "context_sync_local", - "chat_id": chat_id, - "chat": chat, - "checkpoint_log_id": checkpoint, - "pages": pages, - "authoritative": authoritative, - "deferred": deferred, - "totals": totals, - "network": false, - }); - if json { - println!("{}", serde_json::to_string_pretty(&output)?); - } else { - println!( - "Synchronized {} local pages for '{}' through log {} (authoritative={}).", - pages, chat, checkpoint, authoritative - ); - } - } + } => commands::context_sync_local::cmd_context_sync_local( + chat_id, + chat, + db, + queue, + interest_only, + json, + )?, Commands::ContextRepairSelfSends { chat_id, chat, @@ -7922,6 +7532,7 @@ mod tests { } #[cfg(unix)] use crate::auto_reply_runtime::acquire_worker_setup_lock_nonblocking; + use crate::commands::context_sync_local::tests::local_source_fence; use crate::commands::members::LocoMemberProfile; use crate::commands::profile::{ build_syncmainpf_candidate, collect_hint_chat_ids, local_graph_hint_summary, @@ -7962,195 +7573,6 @@ mod tests { assert!(validate_auto_reply_codex_auth(&source, uid).is_err()); } - fn local_source_fence( - chat_id: i64, - after_log_id: i64, - chat_last_log_id: i64, - newer_log_ids: &[i64], - ) -> local_db::LocalPollEnvelope { - let messages = newer_log_ids - .iter() - .map(|log_id| local_db::LocalMessage { - log_id: *log_id, - chat_id, - author_id: 7, - is_self: false, - sender_name: String::new(), - message: String::new(), - attachment: String::new(), - message_type: 1, - sent_at: 0, - }) - .collect::>(); - let first_log_id = messages.first().map(|message| message.log_id); - let last_log_id = messages.last().map(|message| message.log_id); - local_db::LocalPollEnvelope { - schema_version: local_db::LOCAL_POLL_SCHEMA_VERSION, - chat: local_db::LocalChat { - chat_id, - chat_type: 0, - chat_name: "부자멘토멘티".to_string(), - database_chat_name: None, - active_members_count: 4, - last_log_id: chat_last_log_id, - last_updated_at: 0, - unread_count: 0, - display_name: String::new(), - }, - messages, - completeness: local_db::LocalPollCompleteness { - status: if newer_log_ids.is_empty() { - "empty".to_string() - } else { - "complete".to_string() - }, - after_log_id, - first_log_id, - last_log_id, - chat_last_log_id, - row_count: newer_log_ids.len() as i64, - returned_count: newer_log_ids.len() as i64, - available_max_log_id: last_log_id, - id_domain: "global_sparse".to_string(), - has_gap: false, - has_more: false, - proof: "sqlite_snapshot_rowset".to_string(), - }, - } - } - - fn transient_local_source_gap( - chat_id: i64, - after_log_id: i64, - chat_last_log_id: i64, - visible_log_ids: &[i64], - ) -> local_db::LocalPollEnvelope { - let mut envelope = - local_source_fence(chat_id, after_log_id, chat_last_log_id, visible_log_ids); - envelope.completeness.status = "gap".to_string(); - envelope.completeness.has_gap = true; - envelope.completeness.proof = "reconcile_required".to_string(); - envelope - } - - #[test] - fn context_sync_local_retries_gap_without_mutating_checkpoint() { - let gap = transient_local_source_gap(42, 100, 101, &[]); - let complete = local_source_fence(42, 100, 101, &[101]); - let mut responses = std::collections::VecDeque::from([gap, complete]); - let mut calls = Vec::new(); - let mut sleeps = Vec::new(); - - let envelope = context_sync_local_poll_page_with_bounded_retry( - 42, - "부자멘토멘티", - 100, - |chat_id, checkpoint| { - calls.push((chat_id, checkpoint)); - Ok(responses.pop_front().expect("bounded fake poll response")) - }, - |delay| sleeps.push(delay.as_millis()), - ) - .expect("a monotonic complete snapshot should recover the transient gap"); - - assert_eq!(calls, [(42, 100), (42, 100)]); - assert_eq!(sleeps, [250]); - assert_eq!(envelope.completeness.status, "complete"); - assert_eq!(envelope.completeness.after_log_id, 100); - assert_eq!(envelope.completeness.last_log_id, Some(101)); - } - - #[test] - fn context_sync_local_fails_closed_on_persistent_gap() { - let gap = transient_local_source_gap(42, 100, 101, &[]); - let mut responses = - std::collections::VecDeque::from([gap.clone(), gap.clone(), gap.clone(), gap]); - let mut calls = Vec::new(); - let mut sleeps = Vec::new(); - - let error = context_sync_local_poll_page_with_bounded_retry( - 42, - "부자멘토멘티", - 100, - |chat_id, checkpoint| { - calls.push((chat_id, checkpoint)); - Ok(responses.pop_front().expect("bounded fake poll response")) - }, - |delay| sleeps.push(delay.as_millis()), - ) - .expect_err("a persistent source gap must remain fail-closed"); - - assert_eq!(error.to_string(), "context_sync_snapshot_retry_exhausted"); - assert_eq!(calls, [(42, 100); 4]); - assert_eq!(sleeps, [250, 500, 1_000]); - } - - #[test] - fn context_sync_local_retry_rejects_chat_or_checkpoint_identity_drift() { - let cases = [("chat", local_source_fence(43, 100, 101, &[101])), { - let mut checkpoint_drift = local_source_fence(42, 100, 101, &[101]); - checkpoint_drift.completeness.after_log_id = 99; - ("checkpoint", checkpoint_drift) - }]; - - for (label, drifted) in cases { - let gap = transient_local_source_gap(42, 100, 101, &[]); - let mut responses = std::collections::VecDeque::from([gap, drifted]); - let mut calls = Vec::new(); - let mut sleeps = Vec::new(); - let error = context_sync_local_poll_page_with_bounded_retry( - 42, - "부자멘토멘티", - 100, - |chat_id, checkpoint| { - calls.push((chat_id, checkpoint)); - Ok(responses.pop_front().expect("bounded fake poll response")) - }, - |delay| sleeps.push(delay.as_millis()), - ) - .expect_err("poll identity drift must terminate the retry"); - assert_eq!(error.to_string(), "reconcile_required", "{label}"); - assert_eq!(calls, [(42, 100), (42, 100)], "{label}"); - assert_eq!(sleeps, [250], "{label}"); - } - } - - #[test] - fn context_sync_local_retry_rejects_invalid_or_non_monotonic_snapshots() { - let mut invalid_schema = transient_local_source_gap(42, 100, 101, &[]); - invalid_schema.schema_version += 1; - let mut invalid_gap = transient_local_source_gap(42, 100, 101, &[]); - invalid_gap.completeness.status = "unknown".to_string(); - for invalid in [invalid_schema, invalid_gap] { - let mut sleeps = Vec::new(); - let error = context_sync_local_poll_page_with_bounded_retry( - 42, - "부자멘토멘티", - 100, - |_chat_id, _checkpoint| Ok(invalid.clone()), - |delay| sleeps.push(delay.as_millis()), - ) - .expect_err("an invalid first snapshot must not enter the retry path"); - assert_eq!(error.to_string(), "reconcile_required"); - assert!(sleeps.is_empty()); - } - - let first = transient_local_source_gap(42, 100, 103, &[101]); - let regressed = transient_local_source_gap(42, 100, 103, &[]); - let mut responses = std::collections::VecDeque::from([first, regressed]); - let mut sleeps = Vec::new(); - let error = context_sync_local_poll_page_with_bounded_retry( - 42, - "부자멘토멘티", - 100, - |_chat_id, _checkpoint| Ok(responses.pop_front().expect("bounded fake poll response")), - |delay| sleeps.push(delay.as_millis()), - ) - .expect_err("a retry snapshot may not regress its visible row prefix"); - assert_eq!(error.to_string(), "reconcile_required"); - assert_eq!(sleeps, [250]); - } - #[test] fn final_local_source_fence_allows_later_rows_after_scheduled_source() { let watcher_last_observed = 100; @@ -11841,44 +11263,6 @@ connection.close() assert!(require_ax_send(&config).is_ok()); } - #[test] - fn fresh_unmatched_self_classification_defers_without_blocking() { - let unmatched = openkakao_cli::context::AutoGeneratedSelfEventClassification { - chat_id: 7, - log_id: 11, - auto_generated: false, - matched_event_id: None, - reason: "no_exact_sent_match".to_string(), - }; - assert_eq!( - self_classification_retry_after(1_000, 1_001, Some(&unmatched)), - Some(SELF_CLASSIFICATION_RETRY_MAX_SECONDS) - ); - assert_eq!( - self_classification_retry_after(1_000, 1_179, Some(&unmatched)), - Some(1) - ); - assert_eq!( - self_classification_retry_after(1_000, 1_180, Some(&unmatched)), - None - ); - - let mut matched = unmatched.clone(); - matched.auto_generated = true; - matched.reason = "unique_exact_sent_match".to_string(); - assert_eq!( - self_classification_retry_after(1_000, 1_001, Some(&matched)), - None - ); - - let mut ambiguous = unmatched; - ambiguous.reason = "multiple_sent_matches".to_string(); - assert_eq!( - self_classification_retry_after(1_000, 1_001, Some(&ambiguous)), - None - ); - } - #[test] fn loco_write_enabled_when_configured() { let mut config = crate::config::OpenKakaoConfig::default(); From 0a56d3c9d96c6d462b70a266c37e2c705ae16147 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:25:53 +0900 Subject: [PATCH 56/67] refactor(cli): move send permission gates next to the commands they guard require_loco_write moves to commands::send, which owns the LOCO write commands it gates. require_ax_send and require_allowed_send_chat move to commands::local_send, which owns the AX send path. Their five tests move with them. All three gates are verbatim apart from becoming pub. Co-Authored-By: Claude Opus 5.5 --- src/commands/local_send.rs | 72 ++++++++++++++++++++++++++ src/commands/send.rs | 35 +++++++++++++ src/main.rs | 102 +------------------------------------ 3 files changed, 109 insertions(+), 100 deletions(-) diff --git a/src/commands/local_send.rs b/src/commands/local_send.rs index aa4038bc..b22207c0 100644 --- a/src/commands/local_send.rs +++ b/src/commands/local_send.rs @@ -4,6 +4,7 @@ use std::time::{Duration, Instant}; use anyhow::Result; use crate::ax_send; +use crate::config; use crate::local_db::{LocalDbReader, LocalMessage, LocalPollEnvelope, LOCAL_POLL_SCHEMA_VERSION}; use crate::util::{confirm, normalize_outgoing_message, truncate, validate_outbound_message}; use openkakao_cli::reply_policy::validate_auto_reply_laughter; @@ -434,6 +435,39 @@ pub fn cmd_local_send(opts: LocalSendOptions) -> Result<()> { Ok(()) } +pub fn require_ax_send(config: &config::OpenKakaoConfig) -> Result<()> { + if !config.safety.allow_ax_send { + anyhow::bail!( + "AX-automation send is disabled by default.\n\ + local-send drives the real KakaoTalk window (types text and hits Enter\n\ + on your behalf) — treat it with the same care as `send`.\n\n\ + To enable, add to ~/.config/openkakao/config.toml:\n\n\ + [safety]\n\ + allow_ax_send = true\n\n\ + KakaoTalk must be running and already logged in; no server/LOCO contact is made." + ); + } + Ok(()) +} + +/// `local-send` has no chat-id to cross-check against (the local DB it would +/// normally verify with is unreadable on current KakaoTalk builds), so an +/// exact-match allowlist in config and the shared catalog guard are used to +/// prevent typos or substring collisions sending to the wrong chat. +pub fn require_allowed_send_chat(config: &config::OpenKakaoConfig, chat_name: &str) -> Result<()> { + if !config::allowed_send_chat_targets(config, chat_name, None) { + anyhow::bail!( + "chat \"{chat_name}\" is not in the local-send allowlist.\n\n\ + local-send matches chats by display-name text scraped from the KakaoTalk UI,\n\ + not a chat-id, so an explicit allowlist is required to avoid sending to the\n\ + wrong chat. Add to ~/.config/openkakao/config.toml:\n\n\ + [safety]\n\ + allowed_send_chats = [\"{chat_name}\"]" + ); + } + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -649,4 +683,42 @@ mod tests { .expect_err("a post-mutation failure must retain an error outcome"); assert_eq!(error.to_string(), "after mutation"); } + + #[test] + fn require_allowed_send_chat_accepts_a_catalog_enabled_room_title() { + let mut config = config::OpenKakaoConfig::default(); + config.safety.allowed_send_chats = vec!["부자멘토멘티".into()]; + assert!(require_allowed_send_chat(&config, "부자멘토멘티").is_ok()); + // No substring, no surrounding whitespace, no case folding. + assert!(require_allowed_send_chat(&config, "부자멘토").is_err()); + assert!(require_allowed_send_chat(&config, "부자멘토멘티 ").is_err()); + assert!(require_allowed_send_chat(&config, "Vision AI 경진대회").is_err()); + + let dir = tempfile::tempdir().expect("tempdir"); + std::fs::write( + dir.path().join("menubar-room-catalog.json"), + r#"{"rooms":[{"chat_id":437046948660911,"title":"Vision AI 경진대회","auto_reply":true,"geeknews":true}]}"#, + ) + .expect("write catalog"); + config.auto_reply.state_root = Some(dir.path().to_string_lossy().into_owned()); + // The menubar catalog is the same trust level as the config allowlist, + // so enabling the room in the app must be enough to let local-send run. + assert!(require_allowed_send_chat(&config, "Vision AI 경진대회").is_ok()); + assert!(require_allowed_send_chat(&config, "Vision AI").is_err()); + assert!(require_allowed_send_chat(&config, "부자멘토").is_err()); + } + + #[test] + fn ax_send_disabled_by_default() { + let config = crate::config::OpenKakaoConfig::default(); + assert!(!config.safety.allow_ax_send); + assert!(require_ax_send(&config).is_err()); + } + + #[test] + fn ax_send_enabled_when_configured() { + let mut config = crate::config::OpenKakaoConfig::default(); + config.safety.allow_ax_send = true; + assert!(require_ax_send(&config).is_ok()); + } } diff --git a/src/commands/send.rs b/src/commands/send.rs index 1315759a..44d5991e 100644 --- a/src/commands/send.rs +++ b/src/commands/send.rs @@ -2,6 +2,7 @@ use std::path::Path; use anyhow::Result; +use crate::config; use crate::error::OpenKakaoError; use crate::loco_helpers::{check_loco_status, loco_connect_with_auto_refresh}; use crate::media::{detect_media_type, jpeg_dimensions, png_dimensions}; @@ -626,3 +627,37 @@ pub fn cmd_mark_read(opts: MarkReadOptions) -> Result<()> { Ok(()) }) } + +pub fn require_loco_write(config: &config::OpenKakaoConfig) -> Result<()> { + if !config.safety.allow_loco_write { + anyhow::bail!( + "LOCO write operations are disabled by default to protect your account.\n\ + These operations (send, delete, edit, react) use the LOCO protocol which\n\ + may result in account suspension or deletion by Kakao.\n\n\ + To enable, add to ~/.config/openkakao/config.toml:\n\n\ + [safety]\n\ + allow_loco_write = true\n\n\ + Consider using local-read / local-chats / local-search for safe read-only access." + ); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn loco_write_disabled_by_default() { + let config = crate::config::OpenKakaoConfig::default(); + assert!(!config.safety.allow_loco_write); + assert!(require_loco_write(&config).is_err()); + } + + #[test] + fn loco_write_enabled_when_configured() { + let mut config = crate::config::OpenKakaoConfig::default(); + config.safety.allow_loco_write = true; + assert!(require_loco_write(&config).is_ok()); + } +} diff --git a/src/main.rs b/src/main.rs index 66601d00..67240d09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,9 @@ use sha2::{Digest, Sha256}; use crate::auth_flow::{set_auth_policy, AuthPolicy}; use crate::auto_reply_runtime::{leftover_queue_has_unknown_send, validate_private_regular_file}; +use crate::commands::local_send::{require_allowed_send_chat, require_ax_send}; use crate::commands::read::ReadCommandOptions; +use crate::commands::send::require_loco_write; use crate::commands::watch::{WatchOptions, WebhookFormat}; use crate::config::load_config; use crate::util::{format_outgoing_message, NO_COLOR, VERSION}; @@ -5119,21 +5121,6 @@ fn run_auto_reply( Ok(()) } -fn require_loco_write(config: &config::OpenKakaoConfig) -> Result<()> { - if !config.safety.allow_loco_write { - anyhow::bail!( - "LOCO write operations are disabled by default to protect your account.\n\ - These operations (send, delete, edit, react) use the LOCO protocol which\n\ - may result in account suspension or deletion by Kakao.\n\n\ - To enable, add to ~/.config/openkakao/config.toml:\n\n\ - [safety]\n\ - allow_loco_write = true\n\n\ - Consider using local-read / local-chats / local-search for safe read-only access." - ); - } - Ok(()) -} - const AUTO_REPLY_READINESS_MAX_AGE_SECONDS: f64 = 15.0; const AUTO_REPLY_READINESS_MAX_BYTES: u64 = 64 * 1024; const MAX_INT64: i64 = i64::MAX; @@ -5828,39 +5815,6 @@ fn require_expected_local_source_tail( Ok(()) } -fn require_ax_send(config: &config::OpenKakaoConfig) -> Result<()> { - if !config.safety.allow_ax_send { - anyhow::bail!( - "AX-automation send is disabled by default.\n\ - local-send drives the real KakaoTalk window (types text and hits Enter\n\ - on your behalf) — treat it with the same care as `send`.\n\n\ - To enable, add to ~/.config/openkakao/config.toml:\n\n\ - [safety]\n\ - allow_ax_send = true\n\n\ - KakaoTalk must be running and already logged in; no server/LOCO contact is made." - ); - } - Ok(()) -} - -/// `local-send` has no chat-id to cross-check against (the local DB it would -/// normally verify with is unreadable on current KakaoTalk builds), so an -/// exact-match allowlist in config and the shared catalog guard are used to -/// prevent typos or substring collisions sending to the wrong chat. -fn require_allowed_send_chat(config: &config::OpenKakaoConfig, chat_name: &str) -> Result<()> { - if !config::allowed_send_chat_targets(config, chat_name, None) { - anyhow::bail!( - "chat \"{chat_name}\" is not in the local-send allowlist.\n\n\ - local-send matches chats by display-name text scraped from the KakaoTalk UI,\n\ - not a chat-id, so an explicit allowlist is required to avoid sending to the\n\ - wrong chat. Add to ~/.config/openkakao/config.toml:\n\n\ - [safety]\n\ - allowed_send_chats = [\"{chat_name}\"]" - ); - } - Ok(()) -} - fn require_auto_reply_worker_preflight(preflight: bool, worker_identity: bool) -> Result<()> { if preflight && !worker_identity { anyhow::bail!( @@ -11217,58 +11171,6 @@ connection.close() other => panic!("expected ax-watch, got {other:?}"), } } - - #[test] - fn require_allowed_send_chat_accepts_a_catalog_enabled_room_title() { - let mut config = config::OpenKakaoConfig::default(); - config.safety.allowed_send_chats = vec!["부자멘토멘티".into()]; - assert!(require_allowed_send_chat(&config, "부자멘토멘티").is_ok()); - // No substring, no surrounding whitespace, no case folding. - assert!(require_allowed_send_chat(&config, "부자멘토").is_err()); - assert!(require_allowed_send_chat(&config, "부자멘토멘티 ").is_err()); - assert!(require_allowed_send_chat(&config, "Vision AI 경진대회").is_err()); - - let dir = tempfile::tempdir().expect("tempdir"); - std::fs::write( - dir.path().join("menubar-room-catalog.json"), - r#"{"rooms":[{"chat_id":437046948660911,"title":"Vision AI 경진대회","auto_reply":true,"geeknews":true}]}"#, - ) - .expect("write catalog"); - config.auto_reply.state_root = Some(dir.path().to_string_lossy().into_owned()); - // The menubar catalog is the same trust level as the config allowlist, - // so enabling the room in the app must be enough to let local-send run. - assert!(require_allowed_send_chat(&config, "Vision AI 경진대회").is_ok()); - assert!(require_allowed_send_chat(&config, "Vision AI").is_err()); - assert!(require_allowed_send_chat(&config, "부자멘토").is_err()); - } - - #[test] - fn loco_write_disabled_by_default() { - let config = crate::config::OpenKakaoConfig::default(); - assert!(!config.safety.allow_loco_write); - assert!(require_loco_write(&config).is_err()); - } - - #[test] - fn ax_send_disabled_by_default() { - let config = crate::config::OpenKakaoConfig::default(); - assert!(!config.safety.allow_ax_send); - assert!(require_ax_send(&config).is_err()); - } - - #[test] - fn ax_send_enabled_when_configured() { - let mut config = crate::config::OpenKakaoConfig::default(); - config.safety.allow_ax_send = true; - assert!(require_ax_send(&config).is_ok()); - } - - #[test] - fn loco_write_enabled_when_configured() { - let mut config = crate::config::OpenKakaoConfig::default(); - config.safety.allow_loco_write = true; - assert!(require_loco_write(&config).is_ok()); - } fn bound_source_message( log_id: i64, author_id: i64, From 6059c4adbdcf51444c58b6f6bc645a2486b5fc22 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:31:21 +0900 Subject: [PATCH 57/67] refactor(cli): lift the local-send worker setup out of main() The LocalSend arm carried a 190-line immediately-invoked closure that takes the AutoReply worker's locks, checks persisted readiness and the CLI enrollment, and binds the source message. It is now prepare_local_send, returning a LocalSendSetup that names the two lock guards held for the send. The closure body is unchanged apart from taking config and chat_name by reference. main() keeps the let-else early return, so a failed worker setup still skips --completion-promise exactly as before. Co-Authored-By: Claude Opus 5.5 --- src/main.rs | 424 +++++++++++++++++++++++++++------------------------- 1 file changed, 218 insertions(+), 206 deletions(-) diff --git a/src/main.rs b/src/main.rs index 67240d09..4819755d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5844,6 +5844,222 @@ fn finish_worker_bound_local_send_setup( } } +/// What `local-send` holds for its whole run. An AutoReply worker holds the +/// owner-generation and AX send locks until this drops, and carries the +/// database-authoritative binding the AX sender re-attests. A plain run holds +/// neither lock and carries no binding. +struct LocalSendSetup { + _generation_lock: Option, + _send_lock: Option, + bound_chat: Option, +} + +/// Run the AutoReply worker's lock, readiness, and binding gates before a +/// `local-send` mutates anything. `None` means the worker setup failed and the +/// pre-send unavailable result was already emitted. +fn prepare_local_send( + config: &config::OpenKakaoConfig, + chat_name: &str, + dry_run: bool, + preflight: bool, + json: bool, +) -> Result> { + let worker_identity = std::env::var("OPENKAKAO_AUTO_REPLY_WORKER") + .or_else(|_| std::env::var("OPENKAKAO_BUJAMENTOR_WORKER")) + .as_deref() + == Ok("1"); + require_auto_reply_worker_preflight(preflight, worker_identity)?; + let is_auto_reply_worker = !dry_run && worker_identity; + let setup = (|| -> Result<_> { + let _generation_lock = if is_auto_reply_worker && !preflight { + let home = dirs::home_dir().context("cannot resolve home directory")?; + let lock_path = std::env::var_os("OPENKAKAO_AUTO_REPLY_GENERATION_LOCK") + .or_else(|| std::env::var_os("OPENKAKAO_AUTO_REPLY_LOCK")) + .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_GENERATION_LOCK")) + .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_LOCK")) + .filter(|value| !value.is_empty()) + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + auto_reply_service::default_state_root(&home).join(".owner-generation.lock") + }); + let lock = fs::OpenOptions::new() + .create(true) + .truncate(false) + .read(true) + .write(true) + .open(lock_path)?; + #[cfg(unix)] + acquire_worker_setup_lock( + &lock, + "owner-generation lock", + std::time::Duration::from_millis(400), + )?; + Some(lock) + } else { + None + }; + let _send_lock = if is_auto_reply_worker && !preflight { + if let Some(lock_path) = + std::env::var_os("OPENKAKAO_AUTO_REPLY_SEND_LOCK").filter(|value| !value.is_empty()) + { + let lock = fs::OpenOptions::new() + .create(true) + .truncate(false) + .read(true) + .write(true) + .open(lock_path)?; + #[cfg(unix)] + acquire_worker_setup_lock( + &lock, + "AX send lock", + std::time::Duration::from_millis(400), + )?; + Some(lock) + } else { + None + } + } else { + None + }; + let mut bound_chat = None; + if !dry_run { + let mut worker_target = None; + if is_auto_reply_worker { + config::validate_auto_reply(config)?; + let expected_owner = std::env::var("OPENKAKAO_SUPERVISOR_OWNER") + .ok() + .filter(|value| !value.trim().is_empty()) + .context("worker owner missing")?; + let expected_epoch = std::env::var("OPENKAKAO_DB_SOURCE_EPOCH") + .ok() + .and_then(|value| value.parse::().ok()) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("worker source epoch invalid")?; + let expected_target = std::env::var("OPENKAKAO_TARGET_CHAT_ID") + .ok() + .and_then(|value| value.parse::().ok()) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("worker target chat invalid")?; + let expected_last_observed = std::env::var("OPENKAKAO_EXPECTED_SOURCE_LOG_ID") + .ok() + .and_then(|value| value.parse::().ok()) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("worker scheduled source log ID invalid")?; + let expected_author_id = std::env::var("OPENKAKAO_EXPECTED_SOURCE_AUTHOR_ID") + .ok() + .and_then(|value| value.parse::().ok()) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("worker scheduled source author ID invalid")?; + let expected_author_nickname = + std::env::var("OPENKAKAO_EXPECTED_SOURCE_AUTHOR_NICKNAME") + .ok() + .filter(|value| { + !value.is_empty() + && value.len() <= 1024 + && value.trim() == value + && !value.chars().any(char::is_control) + }) + .context("worker scheduled source author nickname invalid")?; + let proactive_send = std::env::var("OPENKAKAO_PROACTIVE_SEND") + .ok() + .is_some_and(|value| value == "1"); + require_persisted_auto_reply_readiness( + Some(expected_owner.as_str()), + Some(expected_epoch), + Some(expected_target), + Some(chat_name), + Some(expected_last_observed), + )?; + worker_target = Some(( + expected_target, + expected_last_observed, + expected_author_id, + expected_author_nickname, + proactive_send, + )); + } + require_ax_send(config)?; + require_allowed_send_chat(config, chat_name)?; + if let Some(( + target_chat_id, + expected_last_observed, + expected_author_id, + expected_author_nickname, + proactive_send, + )) = worker_target + { + // Fetch the authoritative local tail only after both + // process locks and all persisted/safety gates are held. + // The AX sender will re-attest this numeric ID's latest + // suffix against the exact open window before mutation. + let reader = local_db::LocalDbReader::open_no_mutation() + .context("open local database for bound AX send attestation")?; + let mut messages = reader.read_messages(target_chat_id, 20, None)?; + let source_fence = + reader.poll_after(target_chat_id, 1, Some(expected_last_observed))?; + require_expected_local_source_tail( + target_chat_id, + expected_last_observed, + &source_fence, + )?; + let source_message = + resolve_bound_source_message(&messages, expected_last_observed, |log_id| { + reader.read_message_by_log_id(target_chat_id, log_id) + })?; + if proactive_send { + if !source_message.is_self + && (source_message.author_id != expected_author_id + || source_message.sender_name.trim() != expected_author_nickname) + { + anyhow::bail!( + "scheduled reply source author identity drifted; re-enrollment is required" + ); + } + } else if source_message.is_self + || source_message.author_id != expected_author_id + || source_message.sender_name.trim() != expected_author_nickname + { + anyhow::bail!( + "scheduled reply source author identity drifted; re-enrollment is required" + ); + } + let enrollment_path = std::env::var_os("OPENKAKAO_ENROLLMENT_PATH") + .filter(|value| !value.is_empty()) + .map(PathBuf::from) + .context("CLI enrollment authority missing")?; + let enrollment_raw = read_bounded_file(&enrollment_path)?; + let expected_enrollment_digest = + std::env::var("OPENKAKAO_ENROLLMENT_SHA256").unwrap_or_default(); + validate_cli_enrollment_digest(&enrollment_raw, expected_enrollment_digest.trim())?; + let enrollment: serde_json::Value = serde_json::from_slice(&enrollment_raw) + .context("malformed CLI enrollment JSON")?; + require_cli_enrolled_reply_author( + &enrollment, + target_chat_id, + expected_author_id, + &expected_author_nickname, + )?; + messages.reverse(); + let local_tail = ax_send::normalize_local_binding_suffix(&messages) + .into_iter() + .map(|(_, token)| token) + .collect(); + bound_chat = Some(commands::local_send::BoundLocalSend { + chat_id: target_chat_id, + expected_source_log_id: expected_last_observed, + local_tail, + }); + } + } + Ok(LocalSendSetup { + _generation_lock, + _send_lock, + bound_chat, + }) + })(); + finish_worker_bound_local_send_setup(setup, is_auto_reply_worker, preflight, chat_name, json) +} + fn main() -> Result<()> { let cli = Cli::parse(); // The record window's data source must work while the config is broken: @@ -6961,211 +7177,7 @@ fn main() -> Result<()> { preflight, } => { let msg = format_outgoing_message(&message, no_prefix); - let worker_identity = std::env::var("OPENKAKAO_AUTO_REPLY_WORKER") - .or_else(|_| std::env::var("OPENKAKAO_BUJAMENTOR_WORKER")) - .as_deref() - == Ok("1"); - require_auto_reply_worker_preflight(preflight, worker_identity)?; - let is_auto_reply_worker = !dry_run && worker_identity; - let setup = (|| -> Result<_> { - let _generation_lock = if is_auto_reply_worker && !preflight { - let home = dirs::home_dir().context("cannot resolve home directory")?; - let lock_path = std::env::var_os("OPENKAKAO_AUTO_REPLY_GENERATION_LOCK") - .or_else(|| std::env::var_os("OPENKAKAO_AUTO_REPLY_LOCK")) - .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_GENERATION_LOCK")) - .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_LOCK")) - .filter(|value| !value.is_empty()) - .map(std::path::PathBuf::from) - .unwrap_or_else(|| { - auto_reply_service::default_state_root(&home) - .join(".owner-generation.lock") - }); - let lock = fs::OpenOptions::new() - .create(true) - .truncate(false) - .read(true) - .write(true) - .open(lock_path)?; - #[cfg(unix)] - acquire_worker_setup_lock( - &lock, - "owner-generation lock", - std::time::Duration::from_millis(400), - )?; - Some(lock) - } else { - None - }; - let _send_lock = if is_auto_reply_worker && !preflight { - if let Some(lock_path) = std::env::var_os("OPENKAKAO_AUTO_REPLY_SEND_LOCK") - .filter(|value| !value.is_empty()) - { - let lock = fs::OpenOptions::new() - .create(true) - .truncate(false) - .read(true) - .write(true) - .open(lock_path)?; - #[cfg(unix)] - acquire_worker_setup_lock( - &lock, - "AX send lock", - std::time::Duration::from_millis(400), - )?; - Some(lock) - } else { - None - } - } else { - None - }; - let mut bound_chat = None; - if !dry_run { - let mut worker_target = None; - if is_auto_reply_worker { - config::validate_auto_reply(&config)?; - let expected_owner = std::env::var("OPENKAKAO_SUPERVISOR_OWNER") - .ok() - .filter(|value| !value.trim().is_empty()) - .context("worker owner missing")?; - let expected_epoch = std::env::var("OPENKAKAO_DB_SOURCE_EPOCH") - .ok() - .and_then(|value| value.parse::().ok()) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("worker source epoch invalid")?; - let expected_target = std::env::var("OPENKAKAO_TARGET_CHAT_ID") - .ok() - .and_then(|value| value.parse::().ok()) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("worker target chat invalid")?; - let expected_last_observed = - std::env::var("OPENKAKAO_EXPECTED_SOURCE_LOG_ID") - .ok() - .and_then(|value| value.parse::().ok()) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("worker scheduled source log ID invalid")?; - let expected_author_id = - std::env::var("OPENKAKAO_EXPECTED_SOURCE_AUTHOR_ID") - .ok() - .and_then(|value| value.parse::().ok()) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("worker scheduled source author ID invalid")?; - let expected_author_nickname = - std::env::var("OPENKAKAO_EXPECTED_SOURCE_AUTHOR_NICKNAME") - .ok() - .filter(|value| { - !value.is_empty() - && value.len() <= 1024 - && value.trim() == value - && !value.chars().any(char::is_control) - }) - .context("worker scheduled source author nickname invalid")?; - let proactive_send = std::env::var("OPENKAKAO_PROACTIVE_SEND") - .ok() - .is_some_and(|value| value == "1"); - require_persisted_auto_reply_readiness( - Some(expected_owner.as_str()), - Some(expected_epoch), - Some(expected_target), - Some(&chat_name), - Some(expected_last_observed), - )?; - worker_target = Some(( - expected_target, - expected_last_observed, - expected_author_id, - expected_author_nickname, - proactive_send, - )); - } - require_ax_send(&config)?; - require_allowed_send_chat(&config, &chat_name)?; - if let Some(( - target_chat_id, - expected_last_observed, - expected_author_id, - expected_author_nickname, - proactive_send, - )) = worker_target - { - // Fetch the authoritative local tail only after both - // process locks and all persisted/safety gates are held. - // The AX sender will re-attest this numeric ID's latest - // suffix against the exact open window before mutation. - let reader = local_db::LocalDbReader::open_no_mutation() - .context("open local database for bound AX send attestation")?; - let mut messages = reader.read_messages(target_chat_id, 20, None)?; - let source_fence = - reader.poll_after(target_chat_id, 1, Some(expected_last_observed))?; - require_expected_local_source_tail( - target_chat_id, - expected_last_observed, - &source_fence, - )?; - let source_message = resolve_bound_source_message( - &messages, - expected_last_observed, - |log_id| reader.read_message_by_log_id(target_chat_id, log_id), - )?; - if proactive_send { - if !source_message.is_self - && (source_message.author_id != expected_author_id - || source_message.sender_name.trim() - != expected_author_nickname) - { - anyhow::bail!( - "scheduled reply source author identity drifted; re-enrollment is required" - ); - } - } else if source_message.is_self - || source_message.author_id != expected_author_id - || source_message.sender_name.trim() != expected_author_nickname - { - anyhow::bail!( - "scheduled reply source author identity drifted; re-enrollment is required" - ); - } - let enrollment_path = std::env::var_os("OPENKAKAO_ENROLLMENT_PATH") - .filter(|value| !value.is_empty()) - .map(PathBuf::from) - .context("CLI enrollment authority missing")?; - let enrollment_raw = read_bounded_file(&enrollment_path)?; - let expected_enrollment_digest = - std::env::var("OPENKAKAO_ENROLLMENT_SHA256").unwrap_or_default(); - validate_cli_enrollment_digest( - &enrollment_raw, - expected_enrollment_digest.trim(), - )?; - let enrollment: serde_json::Value = serde_json::from_slice(&enrollment_raw) - .context("malformed CLI enrollment JSON")?; - require_cli_enrolled_reply_author( - &enrollment, - target_chat_id, - expected_author_id, - &expected_author_nickname, - )?; - messages.reverse(); - let local_tail = ax_send::normalize_local_binding_suffix(&messages) - .into_iter() - .map(|(_, token)| token) - .collect(); - bound_chat = Some(commands::local_send::BoundLocalSend { - chat_id: target_chat_id, - expected_source_log_id: expected_last_observed, - local_tail, - }); - } - } - Ok((_generation_lock, _send_lock, bound_chat)) - })(); - let Some((_generation_lock, _send_lock, bound_chat)) = - finish_worker_bound_local_send_setup( - setup, - is_auto_reply_worker, - preflight, - &chat_name, - json, - )? + let Some(setup) = prepare_local_send(&config, &chat_name, dry_run, preflight, json)? else { return Ok(()); }; @@ -7176,7 +7188,7 @@ fn main() -> Result<()> { dry_run, preflight, json, - bound_chat, + bound_chat: setup.bound_chat, reply_to, })? } From 51cf50490f417f697f5f35cd5e542ef58ebce84b Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:51:13 +0900 Subject: [PATCH 58/67] refactor(cli): split auto-reply out of main.rs into commands::auto_reply About 4,900 lines of auto-reply machinery and 2,400 lines of its tests lived in main.rs as free functions sharing constants. They now live in one package, grouped by what each part owns: - mod.rs: run_auto_reply, room selectors, context checks, preflight - process.rs: signals, guardian liveness pipe, child process groups - runner.rs: Python supervisor and reply-runner executable checks - llm.rs: reply-model choice and readiness probes (local MLX included) - state_root.rs: private state dirs, owner lock, enrollment and aggregate writes, bounded reads - queue.rs: stopped-room reply-queue schema attestation - enrollment.rs: CLI enrollment writer and the validators that read it - bindings.rs: numeric author bindings and bind: selector attestation - worker_gate.rs: what the worker proves before local-send mutates - tests.rs: the 52 tests and fixtures for all of the above Every item is verbatim apart from visibility (compare reports 205 top-level and 65 test items unchanged). Visibility is pub(super), which is narrower than the crate-wide reach the items had at the crate root. Only run_auto_reply and worker_gate::prepare_local_send are pub. The same 229 binary unit tests pass and the help dump is unchanged. Co-Authored-By: Claude Opus 5.5 --- src/commands/auto_reply/bindings.rs | 308 + src/commands/auto_reply/enrollment.rs | 934 ++ src/commands/auto_reply/llm.rs | 598 ++ src/commands/auto_reply/mod.rs | 748 ++ src/commands/auto_reply/process.rs | 259 + src/commands/auto_reply/queue.rs | 739 ++ src/commands/auto_reply/runner.rs | 403 + src/commands/auto_reply/state_root.rs | 618 ++ src/commands/auto_reply/tests.rs | 2428 +++++ src/commands/auto_reply/worker_gate.rs | 611 ++ src/commands/mod.rs | 1 + src/main.rs | 11766 +++++------------------ 12 files changed, 9803 insertions(+), 9610 deletions(-) create mode 100644 src/commands/auto_reply/bindings.rs create mode 100644 src/commands/auto_reply/enrollment.rs create mode 100644 src/commands/auto_reply/llm.rs create mode 100644 src/commands/auto_reply/mod.rs create mode 100644 src/commands/auto_reply/process.rs create mode 100644 src/commands/auto_reply/queue.rs create mode 100644 src/commands/auto_reply/runner.rs create mode 100644 src/commands/auto_reply/state_root.rs create mode 100644 src/commands/auto_reply/tests.rs create mode 100644 src/commands/auto_reply/worker_gate.rs diff --git a/src/commands/auto_reply/bindings.rs b/src/commands/auto_reply/bindings.rs new file mode 100644 index 00000000..e680c786 --- /dev/null +++ b/src/commands/auto_reply/bindings.rs @@ -0,0 +1,308 @@ +//! Numeric reply-author bindings and the bounded AX attestation of explicit +//! `bind:` room selectors. + +use std::process::Command; + +use anyhow::{Context, Result}; +use sha2::{Digest, Sha256}; + +use super::MAX_INT64; +use crate::{ax_send, local_db}; + +#[derive(Debug, Clone)] +pub(super) struct AutoReplyBindingEvidence { + pub(super) chat_id: i64, + pub(super) ax_name: String, + pub(super) local_name: String, + pub(super) matched_log_ids: Vec, + pub(super) matched_count: usize, + pub(super) matched_utf8_bytes: usize, + pub(super) transcript_sha256: String, + pub(super) attested_db_last_log_id: i64, +} + +#[derive(Debug, Clone, serde::Serialize, PartialEq, Eq)] +pub(super) struct AutoReplyAuthorBinding { + pub(super) nickname: String, + pub(super) author_id: i64, +} + +pub(super) fn auto_reply_bind_author_ids( + reader: &local_db::LocalDbReader, + targets: &[local_db::LocalChat], + reply_authors_by_room: &std::collections::BTreeMap>, +) -> Result>> { + let mut by_chat = std::collections::BTreeMap::new(); + for target in targets { + let reply_authors = reply_authors_by_room + .get(&target.chat_id) + .context("AutoReply per-room reply-author allowlist is incomplete")?; + let configured = reply_authors + .iter() + .map(|nickname| nickname.trim().to_string()) + .collect::>(); + if configured.is_empty() + || configured.len() != reply_authors.len() + || configured.iter().any(|nickname| { + nickname.is_empty() + || nickname.len() > 128 + || nickname.chars().any(char::is_control) + }) + { + anyhow::bail!( + "AutoReply reply-author allowlist for room {} is invalid or duplicated", + target.chat_id + ); + } + let identities = reader.room_author_identities(target.chat_id)?; + let present = identities + .iter() + .map(|identity| identity.nickname.trim().to_string()) + .filter(|nickname| configured.contains(nickname)) + .collect::>(); + let effective = if present.is_empty() { + identities + .iter() + .filter(|identity| !identity.is_self && !identity.nickname.trim().is_empty()) + .map(|identity| identity.nickname.trim().to_string()) + .collect::>() + } else { + present + }; + if effective.is_empty() { + anyhow::bail!( + "AutoReply reply author allowlist for room {:?} has no bindable members", + target.chat_name + ); + } + let bindings = resolve_auto_reply_author_bindings( + &target.chat_name, + reader.account_user_id(), + &identities, + &effective, + )?; + by_chat.insert(target.chat_id, bindings); + } + Ok(by_chat) +} + +pub(super) fn configure_auto_reply_supervisor_author_policy( + command: &mut Command, + reply_authors: &[String], + author_bindings_json: &str, +) { + command + .env("OPENKAKAO_REPLY_AUTHORS", reply_authors.join(",")) + .env("OPENKAKAO_REPLY_AUTHOR_BINDINGS", author_bindings_json); + for author in reply_authors { + command.arg("--reply-author").arg(author); + } +} + +pub(super) fn resolve_auto_reply_author_bindings( + room_name: &str, + account_user_id: i64, + identities: &[local_db::LocalAuthorIdentity], + configured: &std::collections::BTreeSet, +) -> Result> { + let mut ids_by_nickname = + std::collections::BTreeMap::>::new(); + let mut nicknames_by_id = + std::collections::BTreeMap::>::new(); + for identity in identities { + if identity.author_id <= 0 + || identity.author_id == MAX_INT64 + || identity.is_self != (identity.author_id == account_user_id) + { + anyhow::bail!("AutoReply room author identity proof is inconsistent"); + } + nicknames_by_id + .entry(identity.author_id) + .or_default() + .insert(identity.nickname.clone()); + if configured.contains(&identity.nickname) { + ids_by_nickname + .entry(identity.nickname.clone()) + .or_default() + .insert(identity.author_id); + } + } + let mut bindings = Vec::new(); + let mut bound_ids = std::collections::BTreeSet::new(); + for nickname in configured { + let ids = ids_by_nickname.get(nickname).cloned().unwrap_or_default(); + if ids.is_empty() { + // A configured member simply not present in this room is skipped, + // not an error. + continue; + } + if ids.len() > 1 { + // Fail closed: a configured display name that resolves to more than + // one author ID is ambiguous and could bind the wrong person. + anyhow::bail!( + "AutoReply reply author {nickname:?} in room {room_name:?} maps to multiple author IDs" + ); + } + let author_id = *ids.iter().next().expect("one author ID"); + match nicknames_by_id.get(&author_id) { + // The bound author must carry exactly this one display name. + Some(names) if names.len() == 1 && names.contains(nickname) => {} + // Display-name drift: the same author ID appears under more than one + // name. Fail closed rather than risk binding a renamed identity. + Some(names) if names.contains(nickname) => anyhow::bail!( + "AutoReply reply author {nickname:?} (ID {author_id}) in room {room_name:?} has drifting display names" + ), + _ => continue, + } + if author_id == account_user_id { + continue; + } + if !bound_ids.insert(author_id) { + continue; + } + bindings.push(AutoReplyAuthorBinding { + nickname: nickname.clone(), + author_id, + }); + } + if bindings.is_empty() { + anyhow::bail!( + "AutoReply reply author allowlist for room {room_name:?} has no bindable members" + ); + } + Ok(bindings) +} + +/// Bound on transient read-only attestation failures. +/// +/// KakaoTalk can expose a chat window mid-repaint, where one read returns rows +/// from another view or none at all. The unattended host treated the single +/// read as final and fenced every bound room when that landed (2026-09-18 +/// flapping). Each attempt re-reads both sides and re-runs the same read-only +/// match, so the safety property is unchanged. +pub(super) const AUTO_REPLY_ATTEST_ATTEMPTS: usize = 3; +pub(super) const AUTO_REPLY_ATTEST_RETRY_PAUSE: std::time::Duration = + std::time::Duration::from_millis(500); + +/// Run one bounded operation until it succeeds or the attempt budget is spent. +/// +/// Returns the last error so callers keep the original diagnostics, and always +/// runs at least once even when the budget is configured to zero. +pub(super) fn retry_bounded( + attempts: usize, + pause: std::time::Duration, + mut op: impl FnMut() -> Result, +) -> Result { + let budget = attempts.max(1); + let mut last_error: Option = None; + for index in 0..budget { + match op() { + Ok(value) => return Ok(value), + Err(error) => { + last_error = Some(error); + if index + 1 < budget { + std::thread::sleep(pause); + } + } + } + } + Err(last_error.unwrap_or_else(|| anyhow::anyhow!("bounded retry produced no attempt"))) +} + +pub(super) fn auto_reply_attest_explicit_bindings( + reader: &local_db::LocalDbReader, + selectors: &[local_db::ChatSelector], + targets: &[local_db::LocalChat], +) -> Result> { + retry_bounded( + AUTO_REPLY_ATTEST_ATTEMPTS, + AUTO_REPLY_ATTEST_RETRY_PAUSE, + || auto_reply_attest_explicit_bindings_once(reader, selectors, targets), + ) +} + +fn auto_reply_attest_explicit_bindings_once( + reader: &local_db::LocalDbReader, + selectors: &[local_db::ChatSelector], + targets: &[local_db::LocalChat], +) -> Result> { + const AX_ATTEST_COUNT: usize = 40; + let mut seen = std::collections::BTreeMap::::new(); + let mut evidence = Vec::new(); + for selector in selectors { + let local_db::ChatSelector::Binding { id, name } = selector else { + continue; + }; + if let Some(previous) = seen.insert(*id, name.clone()) { + if previous != *name { + anyhow::bail!("chat ID {id} has conflicting explicit AX bindings"); + } + continue; + } + let target = targets + .iter() + .find(|target| target.chat_id == *id && target.chat_name == *name) + .context("explicit chat binding did not resolve to one local target")?; + let ax_messages = ax_send::read_open_exact_via_ax(name, AX_ATTEST_COUNT) + .with_context(|| format!("read exact AX transcript for {name:?}"))?; + let ax_texts = ax_messages + .iter() + .map(|item| ax_send::normalize_binding_message(&item.text)) + .filter(|item| !item.is_empty()) + .collect::>(); + let mut local_messages = reader.read_messages(target.chat_id, AX_ATTEST_COUNT, None)?; + local_messages.reverse(); + let local_pairs = ax_send::normalize_local_binding_suffix(&local_messages); + let local_texts = local_pairs + .iter() + .map(|(_, token)| token.text.clone()) + .collect::>(); + let suffix_match = ax_send::match_local_binding_suffix(&ax_texts, &local_pairs); + let matched_pairs = local_pairs + .iter() + .rev() + .take(suffix_match.matched_count) + .cloned() + .collect::>(); + let matched_texts = matched_pairs + .iter() + .map(|(_, token)| token.text.clone()) + .collect::>(); + // Operator-confirmed rooms with a short transcript may pass with one + // exact row; the flag is off unless the operator sets it deliberately. + if !suffix_match.is_acceptable(openkakao_cli::ax_send::manual_attestation_enabled()) { + anyhow::bail!( + "explicit chat binding {id}:{name} failed read-only AX/local transcript attestation \ + (matched={}, distinct={}, utf8_bytes={}, ax_rows={}, local_rows={}, ax_tail={}, local_tail={})", + suffix_match.matched_count, + suffix_match.matched_distinct, + suffix_match.matched_utf8_bytes, + ax_texts.len(), + local_texts.len(), + ax_send::binding_kind_tail(&ax_texts, 4), + ax_send::binding_kind_tail(&local_texts, 4) + ); + } + let matched_log_ids = matched_pairs + .iter() + .map(|(log_id, _)| *log_id) + .collect::>(); + let mut transcript_hasher = Sha256::new(); + for (log_id, text) in matched_log_ids.iter().zip(matched_texts.iter()) { + transcript_hasher.update(log_id.to_be_bytes()); + transcript_hasher.update((text.len() as u64).to_be_bytes()); + transcript_hasher.update(text.as_bytes()); + } + evidence.push(AutoReplyBindingEvidence { + chat_id: *id, + ax_name: name.clone(), + local_name: target.database_chat_name.clone().unwrap_or_default(), + matched_log_ids, + matched_count: suffix_match.matched_count, + matched_utf8_bytes: suffix_match.matched_utf8_bytes, + transcript_sha256: hex::encode(transcript_hasher.finalize()), + attested_db_last_log_id: target.last_log_id, + }); + } + Ok(evidence) +} diff --git a/src/commands/auto_reply/enrollment.rs b/src/commands/auto_reply/enrollment.rs new file mode 100644 index 00000000..9cc00682 --- /dev/null +++ b/src/commands/auto_reply/enrollment.rs @@ -0,0 +1,934 @@ +//! The CLI enrollment contract: the writer that records each room's cursor +//! authority and the validators the worker gate applies to it. + +use std::fs; +use std::path::{Path, PathBuf}; + +use anyhow::{Context, Result}; +use sha2::{Digest, Sha256}; + +use super::bindings::{AutoReplyAuthorBinding, AutoReplyBindingEvidence}; +use super::queue::validate_stopped_clean_queue; +use super::state_root::{ + read_bounded_json_file, write_private_auto_reply_enrollment, AUTO_REPLY_READINESS_MAX_BYTES, +}; +use super::MAX_INT64; +use crate::auto_reply_runtime::{leftover_queue_has_unknown_send, validate_private_regular_file}; +use crate::local_db; + +pub(super) const AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION: i64 = 4; +pub(super) const AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION: i64 = 1; +pub(super) const AUTO_REPLY_CURSOR_FRESH_KIND: &str = "fresh_attested_tail"; +pub(super) const AUTO_REPLY_CURSOR_REPLAY_KIND: &str = "stopped_clean_ack_replay"; +pub(super) const AUTO_REPLY_CURSOR_LEFTOVER_KIND: &str = "fenced_leftover_ack_resume"; + +fn is_empty_json_array(value: Option<&serde_json::Value>) -> bool { + value + .and_then(serde_json::Value::as_array) + .is_some_and(Vec::is_empty) +} + +fn leftover_supervisor_is_live(room_root: &Path, target: &local_db::LocalChat) -> Result { + let status_path = room_root.join("supervisor-status.json"); + validate_private_regular_file(&status_path, AUTO_REPLY_READINESS_MAX_BYTES)?; + let status = read_bounded_json_file(&status_path)?; + let state = status + .get("state") + .and_then(serde_json::Value::as_str) + .unwrap_or(""); + Ok( + status.get("schema_version") == Some(&serde_json::Value::from(1)) + && status.get("mode") + == Some(&serde_json::Value::String( + "database_authoritative".to_string(), + )) + && status.get("target_chat_id") == Some(&serde_json::Value::from(target.chat_id)) + && status.get("target_chat_name") + == Some(&serde_json::Value::String(target.chat_name.clone())) + && matches!(state, "running" | "starting" | "stopping") + && status.get("all_children_exited") != Some(&serde_json::Value::Bool(true)), + ) +} + +fn leftover_in_flight_is_absent_or_orphaned(state: &serde_json::Value) -> bool { + match state.get("in_flight_candidate") { + None | Some(serde_json::Value::Null) => true, + Some(candidate) => { + let persisted_owner = state.get("owner_id").and_then(serde_json::Value::as_str); + let in_flight_owner = candidate + .get("owner_id") + .and_then(serde_json::Value::as_str); + matches!( + (persisted_owner, in_flight_owner), + (Some(owner), Some(in_flight)) if !owner.is_empty() && owner == in_flight + ) + } + } +} + +fn leftover_supervisor_is_terminal( + room_root: &Path, + target: &local_db::LocalChat, + state: &serde_json::Value, +) -> Result<()> { + let status_path = room_root.join("supervisor-status.json"); + validate_private_regular_file(&status_path, AUTO_REPLY_READINESS_MAX_BYTES)?; + let status = read_bounded_json_file(&status_path)?; + let _owner = state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .filter(|value| !value.is_empty()) + .context("AutoReply leftover owner is invalid")?; + let _epoch = state + .get("source_epoch") + .and_then(serde_json::Value::as_i64) + .filter(|value| (1..MAX_INT64).contains(value)) + .context("AutoReply leftover epoch is invalid")?; + let child_states = status + .get("child_states") + .and_then(serde_json::Value::as_object) + .context("AutoReply leftover child states are missing")?; + if status.get("schema_version") != Some(&serde_json::Value::from(1)) + || status.get("mode") + != Some(&serde_json::Value::String( + "database_authoritative".to_string(), + )) + || status.get("state") != Some(&serde_json::Value::String("stopped".to_string())) + || status.get("shutdown_state") + != Some(&serde_json::Value::String("stopped_unclean".to_string())) + || status.get("all_children_exited") != Some(&serde_json::Value::Bool(true)) + || status + .get("owner") + .and_then(serde_json::Value::as_str) + .is_none_or(str::is_empty) + || status + .get("source_epoch") + .and_then(serde_json::Value::as_i64) + .is_none_or(|value| !(1..MAX_INT64).contains(&value)) + || status.get("target_chat_id") != Some(&serde_json::Value::from(target.chat_id)) + || status.get("target_chat_name") + != Some(&serde_json::Value::String(target.chat_name.clone())) + || ["ax_watch", "db_watch", "reply_worker"].iter().any(|role| { + child_states.get(*role) != Some(&serde_json::Value::String("exited".to_string())) + }) + { + anyhow::bail!("AutoReply leftover supervisor proof is invalid"); + } + leftover_queue_has_unknown_send(&room_root.join("reply-queue.sqlite3"), target.chat_id) +} + +fn validate_stopped_clean_supervisor( + room_root: &Path, + target: &local_db::LocalChat, + state: &serde_json::Value, +) -> Result<()> { + let status_path = room_root.join("supervisor-status.json"); + validate_private_regular_file(&status_path, AUTO_REPLY_READINESS_MAX_BYTES)?; + let status = read_bounded_json_file(&status_path)?; + let owner = state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .filter(|value| !value.is_empty()) + .context("AutoReply stopped-clean owner is invalid")?; + let epoch = state + .get("source_epoch") + .and_then(serde_json::Value::as_i64) + .filter(|value| (1..MAX_INT64).contains(value)) + .context("AutoReply stopped-clean epoch is invalid")?; + let expected_roles = ["ax_watch", "db_watch", "reply_worker"]; + let child_states = status + .get("child_states") + .and_then(serde_json::Value::as_object) + .context("AutoReply stopped-clean child states are missing")?; + if status.get("schema_version") != Some(&serde_json::Value::from(1)) + || status.get("mode") + != Some(&serde_json::Value::String( + "database_authoritative".to_string(), + )) + || status.get("state") != Some(&serde_json::Value::String("stopped".to_string())) + || status.get("shutdown_state") + != Some(&serde_json::Value::String("stopped_clean".to_string())) + || status.get("all_children_exited") != Some(&serde_json::Value::Bool(true)) + || status.get("readiness") != Some(&serde_json::Value::String("fenced".to_string())) + || status.get("fence_reason") + != Some(&serde_json::Value::String("stopped_clean".to_string())) + || status.get("owner") != Some(&serde_json::Value::String(owner.to_string())) + || status.get("source_epoch") != Some(&serde_json::Value::from(epoch)) + || status.get("target_chat_id") != Some(&serde_json::Value::from(target.chat_id)) + || status.get("target_chat_name") + != Some(&serde_json::Value::String(target.chat_name.clone())) + || child_states.len() != expected_roles.len() + || expected_roles.iter().any(|role| { + child_states.get(*role) != Some(&serde_json::Value::String("exited".to_string())) + }) + { + anyhow::bail!("AutoReply stopped-clean supervisor proof is invalid"); + } + validate_stopped_clean_queue(&room_root.join("reply-queue.sqlite3"), target.chat_id) +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(super) struct AutoReplyCursorAuthority { + pub(super) kind: &'static str, + pub(super) cursor_floor: i64, + pub(super) attested_db_last_log_id: i64, + pub(super) prior_owner_id: Option, + pub(super) prior_source_epoch: Option, +} + +pub(super) fn enrollment_cursor_authority_for_target( + root: &Path, + target: &local_db::LocalChat, + freshly_attested_watermark: i64, +) -> Result { + if !(0..MAX_INT64).contains(&freshly_attested_watermark) + || freshly_attested_watermark != target.last_log_id + { + anyhow::bail!("AutoReply fresh enrollment watermark is invalid"); + } + let room_root = root.join("rooms").join(target.chat_id.to_string()); + let state_path = room_root.join("db-watch-state.json"); + if let Ok(metadata) = fs::symlink_metadata(&state_path) { + if metadata.file_type().is_symlink() || !metadata.file_type().is_file() { + anyhow::bail!("AutoReply room state is not a regular file"); + } + let state = read_bounded_json_file(&state_path)?; + let cursor_floor = state + .get("cursor_floor") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 <= *value && *value < MAX_INT64) + .context("AutoReply room cursor floor is invalid")?; + let acked_watermark = state + .get("acked_watermark") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 <= *value && *value < MAX_INT64) + .context("AutoReply room ACK watermark is invalid")?; + let last_observed = state + .get("last_observed_log_id") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 <= *value && *value < MAX_INT64) + .context("AutoReply room observed watermark is invalid")?; + let parse_ids = |key: &str| -> Result> { + let values = state + .get(key) + .and_then(serde_json::Value::as_array) + .filter(|values| values.len() <= 500) + .with_context(|| format!("AutoReply room {key} is invalid"))?; + let parsed = values + .iter() + .map(|value| { + value + .as_i64() + .filter(|id| 0 < *id && *id < MAX_INT64) + .with_context(|| format!("AutoReply room {key} contains an invalid ID")) + }) + .collect::>>()?; + if parsed.len() != values.len() { + anyhow::bail!("AutoReply room {key} contains duplicate IDs"); + } + Ok(parsed) + }; + let observed = parse_ids("observed_log_ids")?; + let acked = parse_ids("acked_log_ids")?; + let stopped_clean_state = state.get("capability_state") + == Some(&serde_json::Value::String("stopped_clean".to_string())) + && state.get("delivery_enabled") == Some(&serde_json::Value::Bool(false)) + && state.get("fence") == Some(&serde_json::Value::String("stopped_clean".to_string())) + && state.get("fence_reason") == Some(&serde_json::Value::String(String::new())); + let leftover_fenced_state = state.get("schema_version") + == Some(&serde_json::Value::from(3)) + && state.get("target_chat_id") == Some(&serde_json::Value::from(target.chat_id)) + && state.get("target_chat_name") + == Some(&serde_json::Value::String(target.chat_name.clone())) + && state.get("capability_state") + == Some(&serde_json::Value::String("fenced".to_string())) + && state.get("delivery_enabled") == Some(&serde_json::Value::Bool(false)) + && matches!( + state + .get("candidate_phase") + .and_then(serde_json::Value::as_str), + Some("idle" | "hooking") + ) + && leftover_in_flight_is_absent_or_orphaned(&state) + && state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .is_some_and(|value| !value.is_empty()) + && state + .get("source_epoch") + .and_then(serde_json::Value::as_i64) + .is_some_and(|value| (1..MAX_INT64).contains(&value)) + && cursor_floor <= acked_watermark + && acked_watermark == acked.iter().next_back().copied().unwrap_or(0) + && last_observed == observed.iter().next_back().copied().unwrap_or(0) + && acked.is_subset(&observed) + && acked_watermark > 0 + && acked_watermark <= freshly_attested_watermark; + // Shared shape for a leftover room whose cursor is idle and whose acked + // watermark is still the last confirmed delivery boundary. + let leftover_idle_shape_ok = matches!( + state + .get("candidate_phase") + .and_then(serde_json::Value::as_str), + // The watcher's own recovery (`_reconcile_ingress_journal`) accepts a + // candidate in any of these pre-delivery phases, so refusing to + // start on them asked for a manual reconciliation the system could + // perform itself. `sending` and later stay excluded: delivery may + // already have begun there. + Some("idle" | "pending" | "hooking" | "acknowledging") + ) && leftover_in_flight_is_absent_or_orphaned(&state) + && (is_empty_json_array(state.get("pending_gaps")) + || state.get("pending_gaps") == Some(&serde_json::json!(["reconcile_required"]))) + && state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .is_some_and(|value| !value.is_empty()) + && state + .get("source_epoch") + .and_then(serde_json::Value::as_i64) + .is_some_and(|value| (1..MAX_INT64).contains(&value)) + && cursor_floor <= acked_watermark + && acked_watermark == acked.iter().next_back().copied().unwrap_or(0) + && last_observed == observed.iter().next_back().copied().unwrap_or(0) + && acked.is_subset(&observed) + && acked_watermark > 0 + && acked_watermark <= freshly_attested_watermark; + let leftover_identity_ok = state.get("schema_version") == Some(&serde_json::Value::from(3)) + && state.get("target_chat_id") == Some(&serde_json::Value::from(target.chat_id)) + && state.get("target_chat_name") + == Some(&serde_json::Value::String(target.chat_name.clone())); + let leftover_ready_idle = leftover_identity_ok + && leftover_idle_shape_ok + && state.get("capability_state") + == Some(&serde_json::Value::String("ready".to_string())) + && state.get("delivery_enabled") == Some(&serde_json::Value::Bool(true)) + && state.get("fence") == Some(&serde_json::Value::String("ready".to_string())); + // A start that was interrupted, or that fenced on a retryable condition + // such as `context_sync_transient`, leaves the persisted capability at + // "starting". Nothing is in flight and the acked watermark is still the + // last confirmed boundary, so it is exactly as resumable as a "ready" + // leftover. Without this branch the room stayed blocked on "requires + // reconciliation before restart" until an operator intervened. + let leftover_starting_idle = leftover_identity_ok + && leftover_idle_shape_ok + && state.get("capability_state") + == Some(&serde_json::Value::String("starting".to_string())) + && state.get("delivery_enabled") == Some(&serde_json::Value::Bool(false)) + && state.get("fence") == Some(&serde_json::Value::String("starting".to_string())); + let live_supervisor = leftover_supervisor_is_live(&room_root, target).unwrap_or(false); + if live_supervisor + && state.get("schema_version") == Some(&serde_json::Value::from(3)) + && state.get("target_chat_id") == Some(&serde_json::Value::from(target.chat_id)) + && state.get("target_chat_name") + == Some(&serde_json::Value::String(target.chat_name.clone())) + && state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .is_some_and(|value| !value.is_empty()) + && state + .get("source_epoch") + .and_then(serde_json::Value::as_i64) + .is_some_and(|value| (1..MAX_INT64).contains(&value)) + && acked_watermark > 0 + && acked_watermark <= freshly_attested_watermark + { + leftover_queue_has_unknown_send( + &room_root.join("reply-queue.sqlite3"), + target.chat_id, + )?; + validate_private_regular_file(&state_path, 256 * 1024)?; + return Ok(AutoReplyCursorAuthority { + kind: AUTO_REPLY_CURSOR_LEFTOVER_KIND, + cursor_floor: acked_watermark, + attested_db_last_log_id: freshly_attested_watermark, + prior_owner_id: state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .map(str::to_string), + prior_source_epoch: state + .get("source_epoch") + .and_then(serde_json::Value::as_i64), + }); + } + if (leftover_ready_idle || leftover_starting_idle) && !live_supervisor { + leftover_supervisor_is_terminal(&room_root, target, &state)?; + validate_private_regular_file(&state_path, 256 * 1024)?; + return Ok(AutoReplyCursorAuthority { + kind: AUTO_REPLY_CURSOR_LEFTOVER_KIND, + cursor_floor: acked_watermark, + attested_db_last_log_id: freshly_attested_watermark, + prior_owner_id: state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .map(str::to_string), + prior_source_epoch: state + .get("source_epoch") + .and_then(serde_json::Value::as_i64), + }); + } + if leftover_fenced_state && !stopped_clean_state { + leftover_supervisor_is_terminal(&room_root, target, &state)?; + validate_private_regular_file(&state_path, 256 * 1024)?; + return Ok(AutoReplyCursorAuthority { + kind: AUTO_REPLY_CURSOR_LEFTOVER_KIND, + cursor_floor: acked_watermark, + attested_db_last_log_id: freshly_attested_watermark, + prior_owner_id: state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .map(str::to_string), + prior_source_epoch: state + .get("source_epoch") + .and_then(serde_json::Value::as_i64), + }); + } + if state.get("schema_version") != Some(&serde_json::Value::from(3)) + || state.get("target_chat_id") != Some(&serde_json::Value::from(target.chat_id)) + || state.get("target_chat_name") + != Some(&serde_json::Value::String(target.chat_name.clone())) + || !is_empty_json_array(state.get("pending_log_ids")) + || !is_empty_json_array(state.get("pending_gaps")) + || state.get("candidate_phase") != Some(&serde_json::Value::String("idle".to_string())) + || state.get("in_flight_candidate") != Some(&serde_json::Value::Null) + || !stopped_clean_state + || state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .is_none_or(str::is_empty) + || state + .get("source_epoch") + .and_then(serde_json::Value::as_i64) + .is_none_or(|value| !(1..MAX_INT64).contains(&value)) + || cursor_floor > acked_watermark + || observed != acked + || acked_watermark != acked.iter().next_back().copied().unwrap_or(0) + || last_observed != observed.iter().next_back().copied().unwrap_or(0) + { + anyhow::bail!( + "AutoReply room {} requires reconciliation before restart", + target.chat_name + ); + } + validate_private_regular_file(&state_path, 256 * 1024)?; + validate_stopped_clean_supervisor(&room_root, target, &state)?; + if acked_watermark > freshly_attested_watermark { + anyhow::bail!( + "AutoReply room {} database tail regressed below its authoritative ACK", + target.chat_name + ); + } + // A fresh DB/AX attestation proves which room is currently open; it + // is not an acknowledgement of rows that arrived while this watcher + // was stopped. Resume a fully attested stopped-clean generation at + // its durable ACK so local-poll can replay those intervening rows. + return Ok(AutoReplyCursorAuthority { + kind: AUTO_REPLY_CURSOR_REPLAY_KIND, + cursor_floor: acked_watermark, + attested_db_last_log_id: freshly_attested_watermark, + prior_owner_id: state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .map(str::to_string), + prior_source_epoch: state + .get("source_epoch") + .and_then(serde_json::Value::as_i64), + }); + } else if let Err(error) = fs::symlink_metadata(&state_path) { + if error.kind() != std::io::ErrorKind::NotFound { + return Err(error) + .with_context(|| format!("inspect AutoReply room state {}", state_path.display())); + } + } + + let enrollment_path = root.join("enrollment.json"); + let enrollment_metadata = match fs::symlink_metadata(&enrollment_path) { + Ok(metadata) => Some(metadata), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => None, + Err(error) => { + return Err(error).with_context(|| format!("inspect {}", enrollment_path.display())); + } + }; + if let Some(metadata) = enrollment_metadata { + if metadata.file_type().is_symlink() || !metadata.file_type().is_file() { + anyhow::bail!("AutoReply enrollment is not a regular file"); + } + let enrollment = read_bounded_json_file(&enrollment_path)?; + let targets = enrollment + .get("targets") + .and_then(serde_json::Value::as_array) + .context("AutoReply enrollment targets are missing")?; + if enrollment.get("schema_version") + != Some(&serde_json::Value::from( + AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION, + )) + || targets.is_empty() + || targets.len() > 32 + { + anyhow::bail!("AutoReply enrollment authority is invalid"); + } + let matches = targets + .iter() + .filter(|item| { + item.get("chat_id").and_then(serde_json::Value::as_i64) == Some(target.chat_id) + }) + .collect::>(); + if matches.len() > 1 { + anyhow::bail!("AutoReply enrollment target is duplicated"); + } + if !matches.is_empty() { + anyhow::bail!( + "AutoReply room {} has enrollment authority but no clean v3 DB state", + target.chat_name + ); + } + } + Ok(AutoReplyCursorAuthority { + kind: AUTO_REPLY_CURSOR_FRESH_KIND, + cursor_floor: freshly_attested_watermark, + attested_db_last_log_id: freshly_attested_watermark, + prior_owner_id: None, + prior_source_epoch: None, + }) +} + +pub(super) fn enrollment_floor_for_target( + root: &Path, + target: &local_db::LocalChat, + freshly_attested_watermark: i64, +) -> Result { + Ok( + enrollment_cursor_authority_for_target(root, target, freshly_attested_watermark)? + .cursor_floor, + ) +} + +pub(super) fn write_auto_reply_enrollment( + root: &Path, + selector_values: &[String], + targets: &[local_db::LocalChat], + binding_evidence: &[AutoReplyBindingEvidence], + author_bindings: &std::collections::BTreeMap>, + runtime_root: &Path, +) -> Result<(Vec, String)> { + if targets.iter().any(|target| { + author_bindings + .get(&target.chat_id) + .is_none_or(|bindings| bindings.is_empty()) + }) { + anyhow::bail!("AutoReply numeric reply-author enrollment is incomplete"); + } + let fresh_watermarks = targets + .iter() + .map(|target| { + binding_evidence + .iter() + .find(|item| item.chat_id == target.chat_id) + .map_or(target.last_log_id, |item| item.attested_db_last_log_id) + }) + .collect::>(); + let cursor_authorities = targets + .iter() + .zip(fresh_watermarks.iter()) + .map(|(target, fresh_watermark)| { + enrollment_cursor_authority_for_target(root, target, *fresh_watermark) + }) + .collect::>>()?; + if targets + .iter() + .zip(cursor_authorities.iter()) + .any(|(target, authority)| { + matches!( + authority.kind, + AUTO_REPLY_CURSOR_REPLAY_KIND | AUTO_REPLY_CURSOR_LEFTOVER_KIND + ) && binding_evidence + .iter() + .all(|binding| binding.chat_id != target.chat_id) + }) + { + anyhow::bail!("AutoReply replay enrollment requires fresh AX transcript authority"); + } + let payload = serde_json::json!({ + "schema_version": AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION, + "activation": "foreground", + "selectors": selector_values, + "runtime_root": runtime_root, + "created_at": chrono::Utc::now().to_rfc3339(), + "targets": targets.iter().zip(cursor_authorities.iter()).map(|(target, authority)| { + let binding = binding_evidence.iter().find(|item| item.chat_id == target.chat_id); + let identity = if let Some(binding) = binding { + serde_json::json!({ + "schema_version": 1, + "kind": "ax_transcript", + "local_name": binding.local_name, + "ax_name": binding.ax_name, + "matched_log_ids": binding.matched_log_ids, + "matched_count": binding.matched_count, + "matched_utf8_bytes": binding.matched_utf8_bytes, + "transcript_sha256": binding.transcript_sha256, + "attested_db_last_log_id": binding.attested_db_last_log_id, + }) + } else { + serde_json::json!({ + "schema_version": 1, + "kind": "local_name", + "local_name": target.chat_name, + "ax_name": target.chat_name, + }) + }; + serde_json::json!({ + "chat_id": target.chat_id, + "chat_name": target.chat_name, + "last_log_id": authority.cursor_floor, + "room_state_root": root.join("rooms").join(target.chat_id.to_string()), + "identity": identity, + "cursor_authority": { + "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, + "kind": authority.kind, + "cursor_floor": authority.cursor_floor, + "attested_db_last_log_id": authority.attested_db_last_log_id, + "prior_owner_id": authority.prior_owner_id, + "prior_source_epoch": authority.prior_source_epoch, + }, + "reply_author_bindings": author_bindings.get(&target.chat_id), + }) + }).collect::>(), + }); + let bytes = serde_json::to_vec_pretty(&payload)?; + let digest = hex::encode(Sha256::digest(&bytes)); + write_private_auto_reply_enrollment(root, &bytes)?; + Ok(( + cursor_authorities + .iter() + .map(|authority| authority.cursor_floor) + .collect(), + digest, + )) +} + +fn is_lower_hex_sha256(value: &str) -> bool { + value.len() == 64 + && value + .as_bytes() + .iter() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(byte)) +} + +pub(super) fn validate_cli_enrollment_digest(raw: &[u8], expected: &str) -> Result<()> { + if !is_lower_hex_sha256(expected) { + anyhow::bail!("CLI enrollment authority digest missing"); + } + let actual = hex::encode(Sha256::digest(raw)); + if actual != expected { + anyhow::bail!("CLI enrollment authority digest mismatch"); + } + Ok(()) +} + +fn json_object_has_exact_keys(value: &serde_json::Value, expected: &[&str]) -> bool { + let Some(object) = value.as_object() else { + return false; + }; + object.len() == expected.len() && expected.iter().all(|key| object.contains_key(*key)) +} + +fn cli_enrollment_reply_author_bindings( + enrolled: &serde_json::Value, +) -> Result> { + let values = enrolled + .get("reply_author_bindings") + .and_then(serde_json::Value::as_array) + .filter(|values| !values.is_empty() && values.len() <= 64) + .context("CLI enrollment reply-author bindings missing")?; + let mut bindings = std::collections::BTreeMap::new(); + let mut author_ids = std::collections::BTreeSet::new(); + let mut previous_nickname: Option<&str> = None; + for value in values { + if !json_object_has_exact_keys(value, &["nickname", "author_id"]) { + anyhow::bail!("CLI enrollment reply-author binding schema invalid"); + } + let nickname = value + .get("nickname") + .and_then(serde_json::Value::as_str) + .filter(|nickname| { + !nickname.is_empty() + && nickname.len() <= 1024 + && nickname.trim() == *nickname + && !nickname.chars().any(char::is_control) + }) + .context("CLI enrollment reply-author nickname invalid")?; + let author_id = value + .get("author_id") + .and_then(serde_json::Value::as_i64) + .filter(|author_id| 0 < *author_id && *author_id < MAX_INT64) + .context("CLI enrollment reply-author ID invalid")?; + if previous_nickname.is_some_and(|previous| previous >= nickname) + || bindings.insert(nickname.to_string(), author_id).is_some() + || !author_ids.insert(author_id) + { + anyhow::bail!("CLI enrollment reply-author bindings are ambiguous or unsorted"); + } + previous_nickname = Some(nickname); + } + Ok(bindings) +} + +fn cli_enrollment_cursor_authority( + enrolled: &serde_json::Value, + cursor_floor: i64, +) -> Result<(&str, i64)> { + let authority = enrolled + .get("cursor_authority") + .and_then(serde_json::Value::as_object) + .context("CLI enrollment cursor authority missing")?; + let expected_keys = [ + "schema_version", + "kind", + "cursor_floor", + "attested_db_last_log_id", + "prior_owner_id", + "prior_source_epoch", + ]; + if authority.len() != expected_keys.len() + || expected_keys + .iter() + .any(|key| !authority.contains_key(*key)) + || authority.get("schema_version") + != Some(&serde_json::Value::from( + AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, + )) + || authority + .get("cursor_floor") + .and_then(serde_json::Value::as_i64) + != Some(cursor_floor) + { + anyhow::bail!("CLI enrollment cursor authority invalid"); + } + let kind = authority + .get("kind") + .and_then(serde_json::Value::as_str) + .context("CLI enrollment cursor authority kind missing")?; + let attested_tail = authority + .get("attested_db_last_log_id") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 <= *value && *value < MAX_INT64) + .context("CLI enrollment cursor authority tail invalid")?; + let prior_owner = authority.get("prior_owner_id"); + let prior_epoch = authority.get("prior_source_epoch"); + match kind { + AUTO_REPLY_CURSOR_FRESH_KIND => { + if cursor_floor != attested_tail + || prior_owner != Some(&serde_json::Value::Null) + || prior_epoch != Some(&serde_json::Value::Null) + { + anyhow::bail!("CLI fresh cursor authority invalid"); + } + } + AUTO_REPLY_CURSOR_REPLAY_KIND | AUTO_REPLY_CURSOR_LEFTOVER_KIND => { + if cursor_floor > attested_tail + || prior_owner + .and_then(serde_json::Value::as_str) + .is_none_or(str::is_empty) + || prior_epoch + .and_then(serde_json::Value::as_i64) + .is_none_or(|value| !(1..MAX_INT64).contains(&value)) + { + anyhow::bail!("CLI replay cursor authority invalid"); + } + } + _ => anyhow::bail!("CLI enrollment cursor authority kind invalid"), + } + Ok((kind, attested_tail)) +} + +pub(super) fn validate_cli_enrollment_authority( + enrollment: &serde_json::Value, + target: i64, + chat_name: &str, + cursor_floor: i64, + expected_room_root: &Path, +) -> Result<()> { + if enrollment.get("schema_version") + != Some(&serde_json::Value::from( + AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION, + )) + { + anyhow::bail!("CLI enrollment authority schema invalid"); + } + let targets = enrollment + .get("targets") + .and_then(serde_json::Value::as_array) + .filter(|targets| !targets.is_empty() && targets.len() <= 32) + .context("CLI enrollment targets invalid")?; + let matching_targets = targets + .iter() + .filter(|item| item.get("chat_id").and_then(serde_json::Value::as_i64) == Some(target)) + .collect::>(); + if matching_targets.len() != 1 { + anyhow::bail!("CLI enrollment target missing or ambiguous"); + } + let enrolled = matching_targets[0]; + if !json_object_has_exact_keys( + enrolled, + &[ + "chat_id", + "chat_name", + "last_log_id", + "room_state_root", + "identity", + "cursor_authority", + "reply_author_bindings", + ], + ) { + anyhow::bail!("CLI enrollment target schema invalid"); + } + if enrolled + .get("chat_name") + .and_then(serde_json::Value::as_str) + != Some(chat_name) + || enrolled + .get("last_log_id") + .and_then(serde_json::Value::as_i64) + != Some(cursor_floor) + { + anyhow::bail!("CLI enrollment target identity does not match DB readiness"); + } + let enrolled_room_root = enrolled + .get("room_state_root") + .and_then(serde_json::Value::as_str) + .filter(|value| !value.is_empty()) + .map(PathBuf::from) + .context("CLI enrollment room root missing")?; + let enrolled_room_root = + fs::canonicalize(enrolled_room_root).context("canonicalize CLI enrollment room root")?; + let expected_room_root = + fs::canonicalize(expected_room_root).context("canonicalize CLI DB state room root")?; + if enrolled_room_root != expected_room_root { + anyhow::bail!("CLI enrollment room root does not match DB readiness"); + } + let (cursor_kind, cursor_attested_tail) = + cli_enrollment_cursor_authority(enrolled, cursor_floor)?; + + let identity = enrolled + .get("identity") + .filter(|value| value.is_object()) + .context("CLI enrollment identity missing")?; + if identity.get("schema_version") != Some(&serde_json::Value::from(1)) + || identity.get("ax_name").and_then(serde_json::Value::as_str) != Some(chat_name) + { + anyhow::bail!("CLI enrollment identity invalid"); + } + let kind = identity + .get("kind") + .and_then(serde_json::Value::as_str) + .context("CLI enrollment identity kind missing")?; + let local_name = identity + .get("local_name") + .and_then(serde_json::Value::as_str) + .context("CLI enrollment local identity missing")?; + match kind { + "local_name" => { + if local_name != chat_name + || !json_object_has_exact_keys( + identity, + &["schema_version", "kind", "local_name", "ax_name"], + ) + { + anyhow::bail!("CLI enrollment local-name identity invalid"); + } + } + "ax_transcript" => { + let expected_keys = [ + "schema_version", + "kind", + "local_name", + "ax_name", + "matched_log_ids", + "matched_count", + "matched_utf8_bytes", + "transcript_sha256", + "attested_db_last_log_id", + ]; + let matched_log_ids = identity + .get("matched_log_ids") + .and_then(serde_json::Value::as_array) + .context("CLI enrollment transcript log IDs missing")?; + let parsed_log_ids = matched_log_ids + .iter() + .map(|value| { + value + .as_i64() + .filter(|id| 0 < *id && *id < MAX_INT64) + .context("CLI enrollment transcript log ID invalid") + }) + .collect::>>()?; + let unique_log_ids = parsed_log_ids + .iter() + .copied() + .collect::>(); + let matched_utf8_bytes = identity + .get("matched_utf8_bytes") + .and_then(serde_json::Value::as_i64) + .unwrap_or(-1); + let attested_db_last_log_id = identity + .get("attested_db_last_log_id") + .and_then(serde_json::Value::as_i64) + .unwrap_or(-1); + let transcript_sha256 = identity + .get("transcript_sha256") + .and_then(serde_json::Value::as_str) + .unwrap_or(""); + if !local_name.is_empty() + || !json_object_has_exact_keys(identity, &expected_keys) + || !(2..=20).contains(&parsed_log_ids.len()) + || unique_log_ids.len() != parsed_log_ids.len() + || identity + .get("matched_count") + .and_then(serde_json::Value::as_i64) + != Some(parsed_log_ids.len() as i64) + || matched_utf8_bytes < 0 + || (parsed_log_ids.len() >= 3 && matched_utf8_bytes < 24) + || !(0 < attested_db_last_log_id && attested_db_last_log_id < MAX_INT64) + || attested_db_last_log_id != cursor_attested_tail + || attested_db_last_log_id + < parsed_log_ids.iter().copied().max().unwrap_or(MAX_INT64) + || !is_lower_hex_sha256(transcript_sha256) + { + anyhow::bail!("CLI enrollment transcript identity invalid"); + } + } + _ => anyhow::bail!("CLI enrollment identity kind invalid"), + } + if matches!( + cursor_kind, + AUTO_REPLY_CURSOR_REPLAY_KIND | AUTO_REPLY_CURSOR_LEFTOVER_KIND + ) && kind != "ax_transcript" + { + anyhow::bail!("CLI leftover or replay cursor requires transcript identity"); + } + cli_enrollment_reply_author_bindings(enrolled)?; + Ok(()) +} + +pub(super) fn require_cli_enrolled_reply_author( + enrollment: &serde_json::Value, + target: i64, + author_id: i64, + nickname: &str, +) -> Result<()> { + let targets = enrollment + .get("targets") + .and_then(serde_json::Value::as_array) + .context("CLI enrollment targets invalid")?; + let matches = targets + .iter() + .filter(|item| item.get("chat_id").and_then(serde_json::Value::as_i64) == Some(target)) + .collect::>(); + if matches.len() != 1 { + anyhow::bail!("CLI enrollment target missing or ambiguous"); + } + let bindings = cli_enrollment_reply_author_bindings(matches[0])?; + if bindings.get(nickname) != Some(&author_id) { + anyhow::bail!("scheduled reply author identity drifted; re-enrollment is required"); + } + Ok(()) +} diff --git a/src/commands/auto_reply/llm.rs b/src/commands/auto_reply/llm.rs new file mode 100644 index 00000000..4b48e66a --- /dev/null +++ b/src/commands/auto_reply/llm.rs @@ -0,0 +1,598 @@ +//! Reply-model choice for `auto-reply` and the read-only readiness probes +//! (including the loopback-only local MLX probe). + +use std::io::{IsTerminal, Read}; +use std::process::{Command, Stdio}; +use std::time::Duration; + +use anyhow::{Context, Result}; + +use super::runner::validate_auto_reply_runner; +use crate::config; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(super) enum AutoReplyLlmChoice { + MlxQwen38FlashNext, + MlxQwen38TwentySevenB, + GjcGemini37Flash, + GjcOpencodeDeepseek41Flash, + CodexGpt56Luna, +} + +impl AutoReplyLlmChoice { + pub(super) fn all() -> [Self; 5] { + [ + Self::MlxQwen38FlashNext, + Self::MlxQwen38TwentySevenB, + Self::GjcGemini37Flash, + Self::GjcOpencodeDeepseek41Flash, + Self::CodexGpt56Luna, + ] + } + + pub(super) fn from_model(model: &str) -> Option { + match config::canonical_mlx_local_model(Some(model)) { + Some(config::MLX_FLASH_NEXT_MODEL_ID) => return Some(Self::MlxQwen38FlashNext), + Some(config::MLX_27B_MODEL_ID) => return Some(Self::MlxQwen38TwentySevenB), + _ => {} + } + match model.trim() { + "google-antigravity/gemini-3.8-flash" + | "google-antigravity/gemini-3.7-flash-high" + | "google-antigravity/gemini-3.7-flash-tiered" + | "google-antigravity/gemini-3.8-flash-tiered" + | "google-antigravity/gemini-3.8-flash-high" + | "google-antigravity/gemini-3.6-flash-tiered" + | "gjc" + | "gemini" + | "gemini-3.7-flash" + | "gemini-3.8-flash" + | "gemini-3.8-flash-high" + | "gemini-3.6-flash" => Some(Self::GjcGemini37Flash), + "opencode-go-session/deepseek-v4.1-flash" + | "opencode-go/deepseek-v4.1-flash" + | "deepseek-v4.1-flash" + | "opencode-go" => Some(Self::GjcOpencodeDeepseek41Flash), + "gpt-5.6-luna" | "codex" | "luna" => Some(Self::CodexGpt56Luna), + _ => None, + } + } + + pub(super) fn label(self) -> &'static str { + match self { + Self::MlxQwen38FlashNext => "Local MLX Qwen3.8 Flash-Next", + Self::MlxQwen38TwentySevenB => "Local MLX Qwen3.8 27B", + Self::GjcGemini37Flash => "Gajae-Code Gemini 3.7 Flash (high)", + Self::GjcOpencodeDeepseek41Flash => "OpenCode Go DeepSeek V4.1 Flash", + Self::CodexGpt56Luna => "Codex GPT-5.6 Luna", + } + } + + pub(super) fn model(self) -> &'static str { + match self { + Self::MlxQwen38FlashNext => config::MLX_FLASH_NEXT_MODEL_ID, + Self::MlxQwen38TwentySevenB => config::MLX_27B_MODEL_ID, + Self::GjcGemini37Flash => "google-antigravity/gemini-3.7-flash-tiered", + Self::GjcOpencodeDeepseek41Flash => "opencode-go-session/deepseek-v4.1-flash", + Self::CodexGpt56Luna => "gpt-5.6-luna", + } + } + + pub(super) fn apply(self, config: &mut config::OpenKakaoConfig) { + if matches!(self, Self::MlxQwen38FlashNext | Self::MlxQwen38TwentySevenB) { + config.model.privacy_mode = Some("local".into()); + config.model.allow_egress = false; + config.model.provider = Some(config::MLX_SERVE_PROVIDER.into()); + config.model.retention = None; + config.auto_reply.reply_runner_kind = Some("opencodex".into()); + config.auto_reply.reply_model = Some(self.model().into()); + config.auto_reply.reply_reasoning_effort = Some("medium".into()); + config.auto_reply.reply_service_tier = Some("default".into()); + if config.auto_reply.reply_runner.is_none() { + config.auto_reply.reply_runner = Some("/opt/homebrew/bin/opencodex".into()); + } + return; + } + if config.auto_reply.reply_runner_kind.as_deref() == Some("opencodex") { + config.model.privacy_mode = Some("remote_explicit".into()); + config.model.allow_egress = true; + config.model.provider = Some("opencodex".into()); + config.model.retention = Some("provider-policy".into()); + config.auto_reply.reply_model = Some(self.model().into()); + config.auto_reply.reply_reasoning_effort = Some("high".into()); + config.auto_reply.reply_service_tier = Some("default".into()); + if config.auto_reply.reply_runner.is_none() { + config.auto_reply.reply_runner = Some("/opt/homebrew/bin/opencodex".into()); + } + return; + } + match self { + Self::MlxQwen38FlashNext => unreachable!("local MLX choice returns above"), + Self::MlxQwen38TwentySevenB => unreachable!("local MLX choice returns above"), + Self::GjcGemini37Flash | Self::GjcOpencodeDeepseek41Flash => { + config.model.privacy_mode = Some("remote_explicit".into()); + config.model.allow_egress = true; + config.model.provider = Some( + if self == Self::GjcOpencodeDeepseek41Flash { + // OpenCode Go is reached through the session-headed + // local provider entry in ~/.gjc/agent/models.yml. + "opencode-go-session" + } else { + "google-antigravity" + } + .into(), + ); + config.model.retention = Some("provider-policy".into()); + config.auto_reply.reply_runner_kind = Some("gjc".into()); + config.auto_reply.reply_model = Some(self.model().into()); + config.auto_reply.reply_reasoning_effort = Some("high".into()); + config.auto_reply.reply_service_tier = Some("default".into()); + if config.auto_reply.reply_runner.is_none() { + if let Some(home) = dirs::home_dir() { + let wrapper = home.join(".local/lib/openkakao/gjc.js"); + if wrapper.is_file() { + config.auto_reply.reply_runner = + Some(wrapper.to_string_lossy().into_owned()); + } + } + } + } + Self::CodexGpt56Luna => { + config.model.privacy_mode = Some("remote_explicit".into()); + config.model.allow_egress = true; + config.model.provider = Some("openai-codex".into()); + config.model.retention = Some("provider-policy".into()); + config.auto_reply.reply_runner_kind = Some("codex".into()); + config.auto_reply.reply_model = Some(self.model().into()); + config.auto_reply.reply_reasoning_effort = Some("max".into()); + config.auto_reply.reply_service_tier = Some("priority".into()); + } + } + } +} + +pub(super) fn select_auto_reply_llm( + config: &mut config::OpenKakaoConfig, + requested: Option<&str>, + json_output: bool, +) -> Result { + if let Some(requested) = requested { + return AutoReplyLlmChoice::from_model(requested).with_context(|| { + format!("unknown reply model {requested:?}; use the exact local Qwen3.8 Flash-Next or Qwen3.8 27B ID, gemini-3.7-flash, deepseek-v4.1-flash or gpt-5.6-luna") + }); + } + if json_output || !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() { + let configured = config.auto_reply.reply_model.as_deref().unwrap_or(""); + return AutoReplyLlmChoice::from_model(configured).with_context(|| { + "no working reply model selected; pass --model or run interactively to pick one" + }); + } + let items = AutoReplyLlmChoice::all() + .into_iter() + .map(|choice| crate::util::ArrowMenuItem { + label: choice.label().to_owned(), + value: choice.model().to_owned(), + }) + .collect::>(); + let selected = config + .auto_reply + .reply_model + .as_deref() + .and_then(AutoReplyLlmChoice::from_model) + .and_then(|current| { + AutoReplyLlmChoice::all() + .iter() + .position(|item| *item == current) + }) + .unwrap_or(0); + let index = crate::util::select_with_arrows("Select the reply LLM", &items, selected)?; + Ok(AutoReplyLlmChoice::all()[index]) +} + +pub(super) const LOCAL_MLX_BASE_URL: &str = "http://127.0.0.1:11234/v1"; +pub(super) const LOCAL_MLX_MODELS_URL: &str = "http://127.0.0.1:11234/v1/models"; +pub(super) const LOCAL_MLX_CHAT_URL: &str = "http://127.0.0.1:11234/v1/chat/completions"; +// 2026-09-22 실측 cold-start 첫 토큰은 23.09초였다. 15초 중단은 warm-up도 +// 취소해 preflight_failed가 5회 연속 발생했으므로 충분한 기동 여유를 둔다. +pub(super) const LOCAL_MLX_PROBE_TIMEOUT: Duration = Duration::from_secs(120); +pub(super) const LOCAL_MLX_MAX_RESPONSE_BYTES: usize = 64 * 1024; +const LOCAL_MLX_MAX_MODEL_ROWS: usize = 256; +const LOCAL_MLX_MAX_CHOICES: usize = 8; +const LOCAL_MLX_MAX_CONTENT_BYTES: usize = 1024; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(super) enum LocalMlxProbeMethod { + Get, + Post, +} + +pub(super) struct LocalMlxProbeRequest { + pub(super) method: LocalMlxProbeMethod, + pub(super) url: &'static str, + pub(super) body: Option>, + pub(super) timeout: Duration, + pub(super) max_response_bytes: usize, +} + +pub(super) struct LocalMlxProbeResponse { + pub(super) status: u16, + pub(super) body: Vec, +} + +pub(super) trait LocalMlxProbeTransport { + fn send(&self, request: LocalMlxProbeRequest) -> Result; +} + +pub(super) struct ReqwestLocalMlxProbeTransport; + +impl LocalMlxProbeTransport for ReqwestLocalMlxProbeTransport { + fn send(&self, request: LocalMlxProbeRequest) -> Result { + if !request.url.starts_with(LOCAL_MLX_BASE_URL) + || !matches!(request.url, LOCAL_MLX_MODELS_URL | LOCAL_MLX_CHAT_URL) + { + anyhow::bail!("local MLX probe rejected a non-loopback endpoint"); + } + if request.timeout.is_zero() + || request.timeout > LOCAL_MLX_PROBE_TIMEOUT + || request.max_response_bytes > LOCAL_MLX_MAX_RESPONSE_BYTES + { + anyhow::bail!("local MLX probe request exceeds its fixed bounds"); + } + + let client = reqwest::blocking::Client::builder() + .no_proxy() + .redirect(reqwest::redirect::Policy::none()) + .connect_timeout(Duration::from_secs(2)) + .timeout(request.timeout) + .build() + .context("build bounded local MLX probe client")?; + let request_builder = match request.method { + LocalMlxProbeMethod::Get => { + if request.body.is_some() { + anyhow::bail!("local MLX models request must not contain a body"); + } + client.get(request.url) + } + LocalMlxProbeMethod::Post => client + .post(request.url) + .header(reqwest::header::CONTENT_TYPE, "application/json") + .body( + request + .body + .context("local MLX generation request body is missing")?, + ), + } + .header(reqwest::header::ACCEPT, "application/json"); + let response = request_builder + .send() + .context("send bounded local MLX probe request")?; + let status = response.status().as_u16(); + let mut body = Vec::new(); + response + .take(request.max_response_bytes.saturating_add(1) as u64) + .read_to_end(&mut body) + .context("read bounded local MLX probe response")?; + if body.len() > request.max_response_bytes { + anyhow::bail!("local MLX probe response is too large"); + } + Ok(LocalMlxProbeResponse { status, body }) + } +} + +fn parse_local_mlx_probe_json( + response: LocalMlxProbeResponse, + stage: &str, +) -> Result { + if response.status != 200 { + anyhow::bail!("local MLX {stage} probe returned HTTP {}", response.status); + } + if response.body.len() > LOCAL_MLX_MAX_RESPONSE_BYTES { + anyhow::bail!("local MLX {stage} probe response is too large"); + } + serde_json::from_slice(&response.body) + .with_context(|| format!("local MLX {stage} probe returned malformed JSON")) +} + +fn probe_local_mlx_auto_reply( + config: &config::OpenKakaoConfig, + transport: &dyn LocalMlxProbeTransport, +) -> Result<()> { + config::validate_auto_reply_local_mlx_profile(config)?; + let model = config::canonical_mlx_local_model(config.auto_reply.reply_model.as_deref()) + .context("local MLX AutoReply model is not allowlisted")?; + + let models = parse_local_mlx_probe_json( + transport.send(LocalMlxProbeRequest { + method: LocalMlxProbeMethod::Get, + url: LOCAL_MLX_MODELS_URL, + body: None, + timeout: LOCAL_MLX_PROBE_TIMEOUT, + max_response_bytes: LOCAL_MLX_MAX_RESPONSE_BYTES, + })?, + "models", + )?; + let rows = models + .get("data") + .and_then(serde_json::Value::as_array) + .context("local MLX models probe response is malformed")?; + if rows.len() > LOCAL_MLX_MAX_MODEL_ROWS { + anyhow::bail!("local MLX models probe returned too many rows"); + } + let mut target = None; + for row in rows { + let row = row + .as_object() + .context("local MLX models probe row is malformed")?; + let row_id = row + .get("id") + .and_then(serde_json::Value::as_str) + .context("local MLX models probe row has no model ID")?; + if config::canonical_mlx_local_model(Some(row_id)) == Some(model) { + if target.is_some() { + anyhow::bail!("local MLX models probe returned an ambiguous model"); + } + target = Some(row); + } + } + let target = target.context("local MLX model is not advertised")?; + if target.get("loaded").and_then(serde_json::Value::as_bool) != Some(true) + || target.get("state").and_then(serde_json::Value::as_str) != Some("ready") + { + anyhow::bail!("local MLX model is not ready"); + } + + let body = serde_json::to_vec(&serde_json::json!({ + "model": model, + "messages": [{"role": "user", "content": "확인이라고만 답하세요."}], + "max_tokens": 8, + "temperature": 0, + "stream": false, + }))?; + let generation = parse_local_mlx_probe_json( + transport.send(LocalMlxProbeRequest { + method: LocalMlxProbeMethod::Post, + url: LOCAL_MLX_CHAT_URL, + body: Some(body), + timeout: LOCAL_MLX_PROBE_TIMEOUT, + max_response_bytes: LOCAL_MLX_MAX_RESPONSE_BYTES, + })?, + "generation", + )?; + let choices = generation + .get("choices") + .and_then(serde_json::Value::as_array) + .context("local MLX generation probe response is malformed")?; + if choices.len() > LOCAL_MLX_MAX_CHOICES { + anyhow::bail!("local MLX generation probe returned too many choices"); + } + let content = choices + .first() + .and_then(|choice| choice.get("message")) + .and_then(|message| message.get("content")) + .and_then(serde_json::Value::as_str) + .context("local MLX generation probe returned no content")?; + if content.trim().is_empty() || content.len() > LOCAL_MLX_MAX_CONTENT_BYTES { + anyhow::bail!("local MLX generation probe content is invalid"); + } + Ok(()) +} + +pub(super) fn probe_auto_reply_llm( + config: &config::OpenKakaoConfig, + choice: AutoReplyLlmChoice, +) -> Result<()> { + probe_auto_reply_llm_with_local_transport(config, choice, &ReqwestLocalMlxProbeTransport) +} + +pub(super) fn probe_auto_reply_llm_with_local_transport( + config: &config::OpenKakaoConfig, + choice: AutoReplyLlmChoice, + local_transport: &dyn LocalMlxProbeTransport, +) -> Result<()> { + if matches!( + choice, + AutoReplyLlmChoice::MlxQwen38FlashNext | AutoReplyLlmChoice::MlxQwen38TwentySevenB + ) { + return probe_local_mlx_auto_reply(config, local_transport); + } + + // Remote choices retain the existing OpenCodex authentication-gateway probe. + if let Ok(client) = reqwest::blocking::Client::builder() + .timeout(std::time::Duration::from_secs(8)) + .build() + { + let ocx_model = match choice { + AutoReplyLlmChoice::GjcGemini37Flash => "google-antigravity/gemini-3.8-flash", + AutoReplyLlmChoice::GjcOpencodeDeepseek41Flash => "google-antigravity/gemini-3.8-flash", + _ => "", + }; + if !ocx_model.is_empty() { + if let Ok(resp) = client + .post("http://127.0.0.1:10100/v1/chat/completions") + .header("Content-Type", "application/json") + .header("Authorization", "Bearer not-needed") + .json(&serde_json::json!({ + "model": ocx_model, + "messages": [{"role": "user", "content": "Reply with exactly OK"}] + })) + .send() + { + if resp.status().is_success() { + eprintln!( + "advisory: OpenCodex authentication gateway verified OK ({})", + ocx_model + ); + return Ok(()); + } + } + } + } + + let runner = validate_auto_reply_runner(config)?; + if runner.kind == "opencodex" { + let output = Command::new(&runner.path) + .arg("--version") + .stdin(Stdio::null()) + .output() + .context("probe opencodex reply runner")?; + let stdout = String::from_utf8_lossy(&output.stdout); + if output.status.success() && stdout.contains("opencodex") { + return Ok(()); + } + anyhow::bail!("opencodex runner failed probe: {}", stdout.trim()); + } + match choice { + AutoReplyLlmChoice::MlxQwen38FlashNext => { + unreachable!("local MLX choice returns before remote probes") + } + AutoReplyLlmChoice::MlxQwen38TwentySevenB => { + unreachable!("local MLX choice returns before remote probes") + } + AutoReplyLlmChoice::GjcGemini37Flash => { + let output = Command::new(&runner.path) + .args([ + "-p", + "--no-session", + "--no-rules", + "--no-lsp", + "--no-title", + "--no-tools", + "--mode", + "text", + "--model", + choice.model(), + "--thinking", + "high", + "Reply with exactly OK", + ]) + .stdin(Stdio::null()) + .output() + .context("probe Gemini reply model")?; + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + if output.status.success() && stdout.contains("OK") { + return Ok(()); + } + if stderr.to_ascii_lowercase().contains("no api key") { + return Ok(()); + } + anyhow::bail!( + "selected LLM {} did not respond; runner={} stdout={} stderr={}", + choice.label(), + runner.path, + stdout.trim(), + stderr.trim() + ); + } + AutoReplyLlmChoice::GjcOpencodeDeepseek41Flash => { + let mut child = Command::new(&runner.path) + .args([ + "-p", + "--no-session", + "--no-rules", + "--no-lsp", + "--no-title", + "--no-tools", + "--mode", + "text", + "--model", + choice.model(), + "Reply with exactly OK", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .context("probe Gajae-Code reply model")?; + + let start = std::time::Instant::now(); + let mut timed_out = false; + let status = loop { + match child.try_wait()? { + Some(status) => break Some(status), + None => { + if start.elapsed() >= std::time::Duration::from_secs(15) { + let _ = child.kill(); + let _ = child.wait(); + timed_out = true; + break None; + } + std::thread::sleep(std::time::Duration::from_millis(200)); + } + } + }; + let mut stdout = Vec::new(); + let mut stderr = Vec::new(); + if let Some(mut out) = child.stdout.take() { + use std::io::Read; + let _ = out.read_to_end(&mut stdout); + } + if let Some(mut err) = child.stderr.take() { + use std::io::Read; + let _ = err.read_to_end(&mut stderr); + } + let stdout_str = String::from_utf8_lossy(&stdout); + let stderr_str = String::from_utf8_lossy(&stderr); + let stderr_lower = stderr_str.to_ascii_lowercase(); + + if let Some(status) = status { + if status.success() && stdout_str.contains("OK") { + return Ok(()); + } + } + + if timed_out + || stderr_lower.contains("usage limit") + || stderr_lower.contains("rate limit") + || stderr_lower.contains("quota") + || stderr_lower.contains("429") + || stderr_lower.contains("no api key") + { + eprintln!("advisory: OpenCode Go usage limit/auth issue detected; probing Antigravity Gemini Flash high fallback"); + let fallback_output = Command::new(&runner.path) + .args([ + "-p", + "--no-session", + "--no-rules", + "--no-lsp", + "--no-title", + "--no-tools", + "--mode", + "text", + "--model", + "google-antigravity/gemini-3.7-flash-tiered", + "--thinking", + "high", + "Reply with exactly OK", + ]) + .stdin(Stdio::null()) + .output() + .context("probe Antigravity Gemini fallback model")?; + let fb_stdout = String::from_utf8_lossy(&fallback_output.stdout); + if fallback_output.status.success() && fb_stdout.contains("OK") { + eprintln!("advisory: Antigravity Gemini Flash high fallback verified OK"); + return Ok(()); + } + } + anyhow::bail!( + "selected LLM {} did not respond; runner={} stdout={} stderr={}", + choice.label(), + runner.path, + stdout_str.trim(), + stderr_str.trim() + ); + } + AutoReplyLlmChoice::CodexGpt56Luna => { + let output = Command::new(&runner.path) + .arg("--version") + .stdin(Stdio::null()) + .output() + .context("probe Codex reply model")?; + let stdout = String::from_utf8_lossy(&output.stdout); + if !output.status.success() || !stdout.trim().starts_with("codex-cli ") { + anyhow::bail!("selected LLM {} is not working", choice.label()); + } + Ok(()) + } + } +} diff --git a/src/commands/auto_reply/mod.rs b/src/commands/auto_reply/mod.rs new file mode 100644 index 00000000..baa05fb1 --- /dev/null +++ b/src/commands/auto_reply/mod.rs @@ -0,0 +1,748 @@ +//! Foreground `auto-reply`: resolve exact rooms, attest them, enroll them, +//! and supervise one Python supervisor per room until Ctrl-C. + +mod bindings; +mod enrollment; +mod llm; +mod process; +mod queue; +mod runner; +mod state_root; +#[cfg(test)] +mod tests; +pub mod worker_gate; + +use std::path::Path; +use std::process::{Command, Stdio}; +use std::sync::atomic::Ordering; +use std::thread; +use std::time::Duration; + +use anyhow::{Context, Result}; + +use crate::{config, local_db, room_catalog}; +use bindings::{ + auto_reply_attest_explicit_bindings, auto_reply_bind_author_ids, + configure_auto_reply_supervisor_author_policy, +}; +use enrollment::{enrollment_floor_for_target, write_auto_reply_enrollment}; +use llm::{probe_auto_reply_llm, select_auto_reply_llm}; +use process::{ + configure_auto_reply_process_group, install_auto_reply_signal_handlers, + start_auto_reply_guardian_liveness_monitor, AutoReplyChildrenGuard, + AUTO_REPLY_GUARDIAN_LIVENESS_ENV, AUTO_REPLY_GUARDIAN_LOST, AUTO_REPLY_STOP, +}; +use runner::{ + resolve_auto_reply_supervisor, validate_auto_reply_executable, validate_auto_reply_runner, + AUTO_REPLY_PYTHON_ISOLATION_ARGS, +}; +use state_root::{ + acquire_auto_reply_owner_lock, auto_reply_legacy_conflict, auto_reply_state_root, + configure_auto_reply_supervisor_shared_state, prepare_auto_reply_room_state, + write_auto_reply_aggregate, +}; + +fn auto_reply_selector_values( + config: &config::OpenKakaoConfig, + cli_values: Vec, + chats: &[local_db::LocalChat], + state_root: &Path, + group_titles: &[(i64, String)], +) -> Result> { + let configured = config.auto_reply.chats.clone(); + let catalog_ids = room_catalog::catalog_auto_reply_chat_ids(state_root)?; + let values = if cli_values.is_empty() { + room_catalog::merge_configured_and_catalog_selectors_named( + &configured, + &catalog_ids, + chats, + group_titles, + )? + } else { + // 명시한 방만 검사한다. 카탈로그의 다른 방까지 합치면 인증이 되지 않는 + // 방 하나가 모든 방의 검사를 실패시킨다. + let expanded = expand_plain_chat_names_with_configured_bindings(cli_values, &configured); + room_catalog::merge_configured_and_catalog_selectors_named( + &expanded, + &[], + chats, + group_titles, + )? + }; + if values.is_empty() { + anyhow::bail!("no chat selected; pass --chat 부자멘토멘티, configure [auto_reply].chats, or enable a menubar room"); + } + Ok(values) +} + +fn expand_plain_chat_names_with_configured_bindings( + cli_values: Vec, + configured: &[String], +) -> Vec { + let bindings = configured + .iter() + .filter_map(|value| { + let selector = value.trim(); + let binding = selector.strip_prefix("bind:")?; + let (_id, name) = binding.split_once(':')?; + let name = name.trim(); + if name.is_empty() { + None + } else { + Some((name.to_owned(), selector.to_owned())) + } + }) + .collect::>(); + cli_values + .into_iter() + .map(|value| { + let trimmed = value.trim(); + if trimmed.starts_with("id:") + || trimmed.starts_with("name:") + || trimmed.starts_with("bind:") + || trimmed.bytes().all(|byte| byte.is_ascii_digit()) + { + return value; + } + bindings.get(trimmed).cloned().unwrap_or(value) + }) + .collect() +} + +fn emit_auto_reply_preflight( + json_output: bool, + check: bool, + targets: &[local_db::LocalChat], + root: &Path, + valid: bool, + error: Option<&str>, + workers_started: bool, +) { + let payload = serde_json::json!({ + "command": "auto-reply", + "valid": valid, + "check": check, + "network": false, + "will_send": !check && workers_started, + "workers_started": workers_started, + "ax_runtime_readiness": if check { "not_evaluated" } else { "starting" }, + "state_root": root, + "targets": targets.iter().map(|target| serde_json::json!({ + "chat_id": target.chat_id, + "chat_name": target.chat_name, + "last_log_id": target.last_log_id, + "room_state_root": root.join("rooms").join(target.chat_id.to_string()), + })).collect::>(), + "error": error, + }); + if json_output { + println!( + "{}", + serde_json::to_string(&payload).unwrap_or_else(|_| "{}".to_string()) + ); + } else { + println!("auto-reply: {} target(s)", targets.len()); + for target in targets { + println!(" {} ({})", target.chat_name, target.chat_id); + } + if let Some(error) = error { + println!(" error: {error}"); + } else if check { + println!(" static check passed; AX runtime readiness not evaluated"); + } else if workers_started { + println!(" foreground workers started; press Ctrl-C to stop"); + } + } +} + +fn validate_auto_reply_context( + reader: &local_db::LocalDbReader, + targets: &[local_db::LocalChat], +) -> Result<()> { + let db_path = openkakao_cli::context::default_db_path(); + for target in targets { + let state = openkakao_cli::context::live_context_sync_state( + &db_path, + reader.account_fingerprint(), + target.chat_id, + )? + .with_context(|| { + format!( + "authoritative live context is unavailable for {:?}", + target.chat_name + ) + })?; + if !state.allows_auto_reply_startup(target.chat_id, &target.chat_name) { + anyhow::bail!( + "authoritative live context is not ready for {:?}", + target.chat_name + ); + } + // 이름 없는 그룹방은 '그룹:' 이름으로 프로파일이 쌓인다. 화면 이름으로 + // 조회가 비면 자리표시자 이름으로 한 번 더 시도한다. + let mut profile_chat = target.chat_name.clone(); + let placeholder = format!("그룹:{}", target.chat_id); + if openkakao_cli::context::style_profile( + &db_path, + &profile_chat, + "최연우", + Some(&state.source), + )? + .is_none() + && openkakao_cli::context::style_profile( + &db_path, + &placeholder, + "최연우", + Some(&state.source), + )? + .is_some() + { + profile_chat = placeholder; + } + let profile = openkakao_cli::context::style_profile( + &db_path, + &profile_chat, + "최연우", + Some(&state.source), + )? + .with_context(|| format!("style profile is unavailable for {:?}", target.chat_name))?; + if profile.source != state.source + || profile.sample_count == 0 + || profile.policy_version != openkakao_cli::context::STYLE_POLICY_VERSION + { + anyhow::bail!("style profile is invalid for {:?}", target.chat_name); + } + let timing = openkakao_cli::context::response_time_stats( + &db_path, + &profile_chat, + "최연우", + Some(&state.source), + )? + .with_context(|| { + format!( + "response-time distribution is unavailable for {:?}", + target.chat_name + ) + })?; + if timing.source != state.source + || timing.sample_count == 0 + || !timing.average_seconds.is_finite() + || !timing.median_seconds.is_finite() + || !timing.p90_seconds.is_finite() + || !timing.stddev_seconds.is_finite() + || timing.average_seconds < 0.0 + || timing.median_seconds < 0.0 + || timing.p90_seconds < 0.0 + || timing.stddev_seconds < 0.0 + { + anyhow::bail!( + "response-time distribution is invalid for {:?}", + target.chat_name + ); + } + if timing.sample_count >= 32 + && (timing.distribution.is_none() + || (timing.stddev_seconds == 0.0 + && timing.p90_seconds <= timing.average_seconds + && timing.median_seconds == timing.average_seconds)) + { + anyhow::bail!( + "response-time distribution is invalid for {:?}", + target.chat_name + ); + } + } + Ok(()) +} + +// Each parameter maps one-to-one onto a `Commands::AutoReply` CLI flag and the +// command has a single call site, so bundling them would only add a translation +// layer between clap and the run. +#[allow( + clippy::too_many_arguments, + reason = "AutoReply CLI flags map one-to-one onto the run parameters" +)] +pub fn run_auto_reply( + config: &config::OpenKakaoConfig, + selector_values: Vec, + check: bool, + self_nickname_override: Option, + reply_author_overrides: Vec, + interval: f64, + requested_model: Option, + json_output: bool, +) -> Result<()> { + let mut effective_config = config.clone(); + let choice = select_auto_reply_llm( + &mut effective_config, + requested_model.as_deref(), + json_output, + )?; + choice.apply(&mut effective_config); + let reply_author_override_active = !reply_author_overrides.is_empty(); + if self_nickname_override.is_some() { + effective_config.auto_reply.self_nickname = self_nickname_override; + } + if reply_author_override_active { + effective_config.auto_reply.reply_authors = reply_author_overrides.clone(); + } + let config = &effective_config; + if !interval.is_finite() || !(0.2..=60.0).contains(&interval) { + anyhow::bail!("auto-reply interval must be between 0.2 and 60 seconds"); + } + AUTO_REPLY_STOP.store(false, Ordering::Release); + AUTO_REPLY_GUARDIAN_LOST.store(false, Ordering::Release); + let _guardian_liveness = start_auto_reply_guardian_liveness_monitor(check)?; + let root = auto_reply_state_root(config)?; + let (canonical_config_path, config_digest) = match config::verify_config_attestation(config) { + Ok(attestation) => attestation, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &[], + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + let reader = match local_db::LocalDbReader::open_no_mutation() { + Ok(reader) => reader, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &[], + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + let chats = match reader.list_all_chats() { + Ok(chats) => chats, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &[], + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + let group_titles = reader + .list_group_chats(10_000) + .unwrap_or_default() + .into_iter() + .map(|chat| (chat.chat_id, chat.title)) + .collect::>(); + let selector_values = + match auto_reply_selector_values(config, selector_values, &chats, &root, &group_titles) { + Ok(values) => values, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &[], + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + let selectors = match local_db::parse_chat_selectors(&selector_values) { + Ok(selectors) => selectors, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &[], + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + let targets = match local_db::resolve_chat_selectors(&chats, &selectors) { + Ok(targets) => targets, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &[], + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + let group_titles = reader + .list_group_chats(10_000) + .unwrap_or_default() + .into_iter() + .map(|chat| (chat.chat_id, chat.title)) + .collect::>(); + let mut targets = targets; + for target in &mut targets { + if target.chat_name.trim().is_empty() { + if let Some(title) = group_titles.get(&target.chat_id) { + target.chat_name = title.clone(); + } else if !target.display_name.trim().is_empty() { + target.chat_name = target.display_name.clone(); + } + } + } + let target_names = targets + .iter() + .map(|target| target.chat_name.clone()) + .collect::>(); + let target_ids = targets + .iter() + .map(|target| target.chat_id) + .collect::>(); + if let Err(error) = config::validate_auto_reply_startup(config, &target_names, &target_ids) { + emit_auto_reply_preflight( + json_output, + check, + &targets, + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + let target_ids = targets + .iter() + .map(|target| target.chat_id) + .collect::>(); + let mut reply_authors_by_room = + match config::auto_reply_reply_authors_by_room(config, &target_ids) { + Ok(values) => values, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &targets, + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + if reply_author_override_active { + let override_authors = + match config::validate_auto_reply_reply_author_override(&reply_author_overrides) { + Ok(values) => values, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &targets, + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + for authors in reply_authors_by_room.values_mut() { + *authors = override_authors.clone(); + } + } + let binding_evidence = match auto_reply_attest_explicit_bindings(&reader, &selectors, &targets) + { + Ok(evidence) => evidence, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &targets, + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + let author_bindings = + match auto_reply_bind_author_ids(&reader, &targets, &reply_authors_by_room) { + Ok(bindings) => bindings, + Err(error) => { + emit_auto_reply_preflight( + json_output, + check, + &targets, + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + }; + if let Err(error) = validate_auto_reply_context(&reader, &targets) { + emit_auto_reply_preflight( + json_output, + check, + &targets, + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + let binary = std::env::current_exe().context("resolve current openkakao binary")?; + let (supervisor, runtime_root) = resolve_auto_reply_supervisor(&binary)?; + let python = validate_auto_reply_executable( + config.auto_reply.python_interpreter.as_deref(), + "AutoReply python_interpreter", + "python3", + !check, + )?; + let runner = validate_auto_reply_runner(config)?; + if check { + if let Err(error) = auto_reply_legacy_conflict(&root) { + emit_auto_reply_preflight( + json_output, + true, + &targets, + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + for target in &targets { + let fresh_watermark = binding_evidence + .iter() + .find(|item| item.chat_id == target.chat_id) + .map_or(target.last_log_id, |item| item.attested_db_last_log_id); + if let Err(error) = enrollment_floor_for_target(&root, target, fresh_watermark) { + emit_auto_reply_preflight( + json_output, + true, + &targets, + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + } + emit_auto_reply_preflight(json_output, true, &targets, &root, true, None, false); + return Ok(()); + } + + if let Err(error) = probe_auto_reply_llm(config, choice) { + emit_auto_reply_preflight( + json_output, + false, + &targets, + &root, + false, + Some(&error.to_string()), + false, + ); + return Err(error); + } + + auto_reply_legacy_conflict(&root)?; + let _owner_lock = acquire_auto_reply_owner_lock(&root)?; + let self_nickname = config::auto_reply_self_nickname(config) + .context("AutoReply self nickname is not configured")?; + let (enrollment_floors, enrollment_digest) = write_auto_reply_enrollment( + &root, + &selector_values, + &targets, + &binding_evidence, + &author_bindings, + &runtime_root, + )?; + + install_auto_reply_signal_handlers(); + let mut children = AutoReplyChildrenGuard::new(targets.len()); + for (target, enrollment_floor) in targets.iter().zip(enrollment_floors.iter()) { + if AUTO_REPLY_STOP.load(Ordering::Relaxed) { + anyhow::bail!("auto-reply activation interrupted before worker startup"); + } + let room_root = prepare_auto_reply_room_state(&root, target)?; + let target_author_bindings = serde_json::to_string( + author_bindings + .get(&target.chat_id) + .context("AutoReply numeric reply-author enrollment is incomplete")?, + )?; + let target_reply_authors = author_bindings + .get(&target.chat_id) + .context("AutoReply numeric reply-author enrollment is incomplete")? + .iter() + .map(|binding| binding.nickname.clone()) + .collect::>(); + let _ = reply_authors_by_room + .get(&target.chat_id) + .context("AutoReply per-room reply-author allowlist is incomplete")?; + let mut command = Command::new(&python); + command + .current_dir(&runtime_root) + .args(AUTO_REPLY_PYTHON_ISOLATION_ARGS) + .arg(&supervisor) + .arg("--interval") + .arg(interval.to_string()) + .arg("--state-root") + .arg(&room_root) + .arg("--target-chat-id") + .arg(target.chat_id.to_string()) + .arg("--target-chat-name") + .arg(&target.chat_name) + .arg("--self-nickname") + .arg(&self_nickname) + .env("OPENKAKAO_AUTO_REPLY_CLI", "1") + .env("OPENKAKAO_CONFIG", &canonical_config_path) + .env("OPENKAKAO_CONFIG_SHA256", &config_digest) + .env("OPENKAKAO_ENROLLMENT_PATH", root.join("enrollment.json")) + .env("OPENKAKAO_ENROLLMENT_SHA256", &enrollment_digest) + .env("OPENKAKAO_TARGET_CHAT_ID", target.chat_id.to_string()) + .env("OPENKAKAO_TARGET_CHAT_NAME", &target.chat_name) + .env("OPENKAKAO_INITIAL_CURSOR", enrollment_floor.to_string()) + .env( + "OPENKAKAO_GEEKNEWS_ENABLED", + if room_catalog::catalog_geeknews_chat_ids(&root) + .ok() + .is_some_and(|ids| ids.contains(&target.chat_id)) + { + "1" + } else { + "0" + }, + ) + .env("OPENKAKAO_SELF_NICKNAME", &self_nickname) + .env( + "OPENKAKAO_ALLOW_LINK_FETCH", + if config.auto_reply.allow_link_fetch { + "1" + } else { + "0" + }, + ) + .env( + "OPENKAKAO_ALLOW_IMAGE_ANALYSIS", + if config.auto_reply.allow_image_analysis { + "1" + } else { + "0" + }, + ) + .env_remove(AUTO_REPLY_GUARDIAN_LIVENESS_ENV) + .env("OPENKAKAO_BINARY", &binary) + .env("OPENKAKAO_PYTHON", &python) + .env("OPENKAKAO_AUTO_REPLY_RUNTIME_ROOT", &runtime_root) + .stdout(if json_output { + Stdio::null() + } else { + Stdio::inherit() + }) + .stderr(if json_output { + Stdio::null() + } else { + Stdio::inherit() + }); + configure_auto_reply_supervisor_shared_state(&mut command, &root); + configure_auto_reply_supervisor_author_policy( + &mut command, + &target_reply_authors, + &target_author_bindings, + ); + command + .env("OPENKAKAO_REPLY_RUNNER", &runner.path) + .env("OPENKAKAO_REPLY_RUNNER_KIND", &runner.kind) + .env("OPENKAKAO_REPLY_RUNNER_SHA256", &runner.sha256) + .env("OPENKAKAO_REPLY_MODEL", &runner.model) + .env("OPENKAKAO_REPLY_REASONING_EFFORT", &runner.reasoning_effort) + .env("OPENKAKAO_REPLY_SERVICE_TIER", &runner.service_tier); + if let Some(codex_home) = &runner.codex_home { + command.env("OPENKAKAO_REPLY_CODEX_HOME", codex_home); + } + configure_auto_reply_process_group(&mut command); + match command.spawn() { + Ok(child) => children.children.push(child), + Err(error) => { + anyhow::bail!("start AutoReply worker for {}: {error}", target.chat_name); + } + } + } + + if AUTO_REPLY_STOP.load(Ordering::Relaxed) { + anyhow::bail!("auto-reply activation interrupted during worker startup"); + } + write_auto_reply_aggregate(&root, &targets, &children.children, "running")?; + emit_auto_reply_preflight(json_output, false, &targets, &root, true, None, true); + + let mut failed = false; + let mut next_aggregate_update = std::time::Instant::now(); + loop { + if AUTO_REPLY_STOP.load(Ordering::Relaxed) { + children.stop(); + break; + } + if std::time::Instant::now() >= next_aggregate_update { + // Aggregate status is observational only; a transient dashboard + // write must not stop otherwise healthy per-room safety workers. + let _ = write_auto_reply_aggregate(&root, &targets, &children.children, "running"); + next_aggregate_update = std::time::Instant::now() + Duration::from_secs(1); + } + let mut child_exited = false; + for child in &mut children.children { + if child.try_wait()?.is_some() { + // A room supervisor is persistent. Any exit before the parent + // receives its own stop signal means that room lost coverage, + // even when the child happened to return status 0. + failed = true; + child_exited = true; + } + } + if child_exited { + children.stop(); + break; + } + thread::sleep(Duration::from_millis(200)); + } + children.stop(); + for child in &mut children.children { + let status = child.wait()?; + if !status.success() { + failed = true; + } + } + write_auto_reply_aggregate(&root, &targets, &children.children, "stopped")?; + children.disarm(); + if AUTO_REPLY_GUARDIAN_LOST.load(Ordering::Acquire) { + anyhow::bail!("session guardian liveness was lost; all AutoReply workers were stopped"); + } + if failed { + anyhow::bail!("one or more AutoReply workers exited unsuccessfully"); + } + Ok(()) +} +const MAX_INT64: i64 = i64::MAX; diff --git a/src/commands/auto_reply/process.rs b/src/commands/auto_reply/process.rs new file mode 100644 index 00000000..c8a00f0f --- /dev/null +++ b/src/commands/auto_reply/process.rs @@ -0,0 +1,259 @@ +//! Process lifecycle for `auto-reply`: signal handling, the session-guardian +//! liveness pipe, and reaping supervisor process groups. + +use std::fs; +use std::io::{self, Read}; +#[cfg(unix)] +use std::os::unix::io::FromRawFd; +#[cfg(unix)] +use std::os::unix::process::CommandExt; +use std::process::{Child, Command}; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; +use std::thread; +use std::time::Duration; + +use anyhow::{Context, Result}; + +pub(super) static AUTO_REPLY_STOP: AtomicBool = AtomicBool::new(false); +pub(super) static AUTO_REPLY_GUARDIAN_LOST: AtomicBool = AtomicBool::new(false); + +pub(super) const AUTO_REPLY_GUARDIAN_LIVENESS_ENV: &str = "OPENKAKAO_SESSION_GUARDIAN_LIVENESS_FD"; + +extern "C" fn handle_auto_reply_signal(_: i32) { + AUTO_REPLY_STOP.store(true, Ordering::Relaxed); +} + +#[cfg(unix)] +pub(super) fn validate_auto_reply_guardian_liveness_fd(fd: libc::c_int) -> Result { + if fd <= libc::STDERR_FILENO { + anyhow::bail!("session guardian liveness descriptor is invalid"); + } + + let mut metadata = std::mem::MaybeUninit::::uninit(); + if unsafe { libc::fstat(fd, metadata.as_mut_ptr()) } != 0 { + anyhow::bail!("session guardian liveness descriptor is unavailable"); + } + let metadata = unsafe { metadata.assume_init() }; + let status_flags = unsafe { libc::fcntl(fd, libc::F_GETFL) }; + if status_flags < 0 { + anyhow::bail!("session guardian liveness descriptor is unavailable"); + } + if metadata.st_mode & libc::S_IFMT != libc::S_IFIFO + || status_flags & libc::O_ACCMODE != libc::O_RDONLY + { + anyhow::bail!("session guardian liveness descriptor is not a read-only pipe"); + } + + let descriptor_flags = unsafe { libc::fcntl(fd, libc::F_GETFD) }; + if descriptor_flags < 0 + || unsafe { libc::fcntl(fd, libc::F_SETFD, descriptor_flags | libc::FD_CLOEXEC) } != 0 + { + anyhow::bail!("session guardian liveness descriptor cannot be isolated"); + } + + let mut readiness = libc::pollfd { + fd, + events: libc::POLLIN | libc::POLLHUP | libc::POLLERR, + revents: 0, + }; + let poll_result = unsafe { libc::poll(&mut readiness, 1, 0) }; + if poll_result < 0 { + anyhow::bail!("session guardian liveness handshake failed"); + } + if poll_result > 0 { + anyhow::bail!("session guardian liveness was lost before activation"); + } + + // SAFETY: validation above proves this process owns an open descriptor. + // The returned File becomes its sole Rust owner and closes it on drop. + Ok(unsafe { fs::File::from_raw_fd(fd) }) +} + +#[cfg(unix)] +pub(super) fn start_auto_reply_guardian_liveness_monitor( + check: bool, +) -> Result>> { + let Some(raw_value) = std::env::var_os(AUTO_REPLY_GUARDIAN_LIVENESS_ENV) else { + return Ok(None); + }; + if check { + anyhow::bail!("session guardian liveness is invalid in read-only check mode"); + } + let value = raw_value + .to_str() + .context("session guardian liveness descriptor is not valid UTF-8")?; + if value.is_empty() + || value.len() > 10 + || !value.bytes().all(|byte| byte.is_ascii_digit()) + || (value.len() > 1 && value.starts_with('0')) + { + anyhow::bail!("session guardian liveness descriptor is malformed"); + } + let fd = value + .parse::() + .context("session guardian liveness descriptor is out of range")?; + let mut pipe = validate_auto_reply_guardian_liveness_fd(fd)?; + + // Do not expose even the descriptor number to worker environments. The + // descriptor itself is already FD_CLOEXEC before any worker can spawn. + std::env::remove_var(AUTO_REPLY_GUARDIAN_LIVENESS_ENV); + let monitor = thread::Builder::new() + .name("guardian-liveness".to_string()) + .spawn(move || { + let mut byte = [0_u8; 1]; + loop { + match pipe.read(&mut byte) { + Ok(0) | Ok(_) => break, + Err(error) if error.kind() == io::ErrorKind::Interrupted => continue, + Err(_) => break, + } + } + AUTO_REPLY_GUARDIAN_LOST.store(true, Ordering::Release); + AUTO_REPLY_STOP.store(true, Ordering::Release); + }) + .context("start session guardian liveness monitor")?; + Ok(Some(monitor)) +} + +#[cfg(not(unix))] +pub(super) fn start_auto_reply_guardian_liveness_monitor( + _check: bool, +) -> Result>> { + if std::env::var_os(AUTO_REPLY_GUARDIAN_LIVENESS_ENV).is_some() { + anyhow::bail!("session guardian liveness is unsupported on this platform"); + } + Ok(None) +} + +pub(super) struct AutoReplyChildrenGuard { + pub(super) children: Vec, + pub(super) disarmed: bool, +} + +impl AutoReplyChildrenGuard { + pub(super) fn new(capacity: usize) -> Self { + Self { + children: Vec::with_capacity(capacity), + disarmed: false, + } + } + + pub(super) fn disarm(&mut self) { + self.disarmed = true; + } + + pub(super) fn stop(&mut self) { + if self.disarmed || self.children.is_empty() { + return; + } + stop_auto_reply_children(&mut self.children); + self.disarm(); + } +} + +impl Drop for AutoReplyChildrenGuard { + fn drop(&mut self) { + if !self.disarmed && !self.children.is_empty() { + stop_auto_reply_children(&mut self.children); + } + } +} + +fn stop_auto_reply_children(children: &mut [Child]) { + #[cfg(unix)] + fn process_group_alive(pid: libc::pid_t) -> bool { + unsafe { + libc::kill(-pid, 0) == 0 + || std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM) + } + } + + #[cfg(unix)] + let process_group_ids = children + .iter() + .map(|child| child.id() as libc::pid_t) + .collect::>(); + + #[cfg(unix)] + for pid in &process_group_ids { + unsafe { + if libc::kill(-*pid, libc::SIGTERM) != 0 { + libc::kill(*pid, libc::SIGTERM); + } + } + } + let deadline = std::time::Instant::now() + Duration::from_secs(3); + while std::time::Instant::now() < deadline { + let direct_children_stopped = children + .iter_mut() + .all(|child| child.try_wait().ok().flatten().is_some()); + #[cfg(unix)] + let process_groups_stopped = process_group_ids + .iter() + .all(|pid| !process_group_alive(*pid)); + #[cfg(not(unix))] + let process_groups_stopped = true; + if direct_children_stopped && process_groups_stopped { + return; + } + thread::sleep(Duration::from_millis(50)); + } + + #[cfg(unix)] + for pid in &process_group_ids { + if process_group_alive(*pid) { + unsafe { + if libc::kill(-*pid, libc::SIGKILL) != 0 { + let _ = libc::kill(*pid, libc::SIGKILL); + } + } + } + } + for child in children.iter_mut() { + if child.try_wait().ok().flatten().is_none() { + #[cfg(unix)] + let _ = child.kill(); + #[cfg(not(unix))] + let _ = child.kill(); + } + let _ = child.wait(); + } + + // `SIGKILL` is asynchronous, and a grandchild that ignored `SIGTERM` is not + // our direct child: reaping the leader can therefore complete while the rest + // of the group is still exiting. Drain the groups so `stop` returns with the + // guarantee callers and tests rely on, that no descendant is left behind. + #[cfg(unix)] + { + let drain_deadline = std::time::Instant::now() + Duration::from_secs(2); + while std::time::Instant::now() < drain_deadline + && process_group_ids + .iter() + .any(|pid| process_group_alive(*pid)) + { + thread::sleep(Duration::from_millis(20)); + } + } +} + +pub(super) fn install_auto_reply_signal_handlers() { + #[cfg(unix)] + unsafe { + let handler = handle_auto_reply_signal as *const () as libc::sighandler_t; + libc::signal(libc::SIGINT, handler); + libc::signal(libc::SIGTERM, handler); + } +} + +pub(super) fn configure_auto_reply_process_group(command: &mut Command) { + #[cfg(unix)] + unsafe { + command.pre_exec(|| { + if libc::setpgid(0, 0) != 0 { + return Err(std::io::Error::last_os_error()); + } + Ok(()) + }); + } +} diff --git a/src/commands/auto_reply/queue.rs b/src/commands/auto_reply/queue.rs new file mode 100644 index 00000000..59576e3d --- /dev/null +++ b/src/commands/auto_reply/queue.rs @@ -0,0 +1,739 @@ +//! Read-only attestation that a stopped room's reply queue has the exact +//! schema and only terminal rows before a restart may resume it. + +use std::path::Path; + +use anyhow::{Context, Result}; + +use super::MAX_INT64; +use crate::auto_reply_runtime::validate_private_regular_file; + +pub(super) const AUTO_REPLY_QUEUE_LEGACY_USER_VERSION: i64 = 0; +pub(super) const AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION: i64 = 2; +pub(super) const AUTO_REPLY_QUEUE_JOURNAL_MAX_ROWS: i64 = 4096; + +const AUTO_REPLY_REPLY_JOBS_TABLE_SQL: &str = r#" +CREATE TABLE reply_jobs( + event_id TEXT PRIMARY KEY, + event_json TEXT NOT NULL, + status TEXT NOT NULL, + due_at REAL, + decision TEXT, + reason TEXT, + category TEXT, + reply TEXT, + scheduled_delay_seconds REAL, + error_class TEXT, + created_at REAL NOT NULL, + updated_at REAL NOT NULL +); +"#; +pub(super) const AUTO_REPLY_REPLY_JOBS_V2_TABLE_SQL: &str = r#" +CREATE TABLE reply_jobs( + event_id TEXT PRIMARY KEY, + event_json TEXT NOT NULL, + status TEXT NOT NULL, + due_at REAL, + decision TEXT, + reason TEXT, + category TEXT, + reply TEXT, + scheduled_delay_seconds REAL, + error_class TEXT, + created_at REAL NOT NULL, + updated_at REAL NOT NULL, + attempt_no INTEGER NOT NULL DEFAULT 0 CHECK(attempt_no BETWEEN 0 AND 1000000) +); +"#; +pub(super) const AUTO_REPLY_REPLY_JOBS_STATUS_INDEX_SQL: &str = + "CREATE INDEX idx_reply_jobs_status_due ON reply_jobs(status, due_at);"; +const AUTO_REPLY_REPLY_JOB_TOMBSTONES_TABLE_SQL: &str = r#" +CREATE TABLE reply_job_tombstones( + event_id TEXT PRIMARY KEY, + status TEXT NOT NULL, + archived_at REAL NOT NULL +); +"#; +const AUTO_REPLY_REPLY_JOB_SUPERSESSIONS_TABLE_SQL: &str = r#" +CREATE TABLE reply_job_supersessions( + event_id TEXT PRIMARY KEY, + superseded_by_event_id TEXT NOT NULL, + created_at REAL NOT NULL, + CHECK(event_id <> superseded_by_event_id) +); +"#; +const AUTO_REPLY_MODEL_CIRCUIT_BREAKER_TABLE_SQL: &str = r#" +CREATE TABLE model_circuit_breaker( + model_key TEXT PRIMARY KEY, + state TEXT NOT NULL, + failure_class TEXT NOT NULL, + consecutive_failures INTEGER NOT NULL, + open_until REAL NOT NULL, + lease_token TEXT, + updated_at REAL NOT NULL +); +"#; + +pub(super) const AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL: &str = r#" +CREATE TABLE pipeline_transitions( + seq INTEGER PRIMARY KEY, + schema_version INTEGER NOT NULL CHECK(schema_version = 1), + event_id TEXT NOT NULL CHECK( + length(event_id) BETWEEN 6 AND 80 + AND substr(event_id, 1, 3) = 'db:' + AND instr(substr(event_id, 4), ':') BETWEEN 2 AND 20 + AND instr( + substr(substr(event_id, 4), instr(substr(event_id, 4), ':') + 1), + ':' + ) = 0 + AND substr(event_id, 4, 1) BETWEEN '1' AND '9' + AND substr( + substr(event_id, 4), + instr(substr(event_id, 4), ':') + 1, + 1 + ) BETWEEN '1' AND '9' + AND substr( + substr(event_id, 4), + 1, + instr(substr(event_id, 4), ':') - 1 + ) NOT GLOB '*[^0-9]*' + AND substr( + substr(event_id, 4), + instr(substr(event_id, 4), ':') + 1 + ) NOT GLOB '*[^0-9]*' + AND CAST( + substr(substr(event_id, 4), 1, instr(substr(event_id, 4), ':') - 1) + AS INTEGER + ) BETWEEN 1 AND 9223372036854775806 + AND CAST( + substr(substr(event_id, 4), instr(substr(event_id, 4), ':') + 1) + AS INTEGER + ) BETWEEN 1 AND 9223372036854775806 + ), + attempt_no INTEGER NOT NULL CHECK(attempt_no BETWEEN 0 AND 1000000), + component TEXT NOT NULL CHECK(component IN ('authorization','ax','burst','context','delay','ingress','media','model','pre_send','projection','queue','recovery','terminal')), + from_state TEXT NOT NULL CHECK(from_state IN ('acknowledging','deferred','delivery_unknown','detected','failed','hooking','idle','none','pending','poison','processing','projection_pending','ready','reconcile_required','scheduled','sending','sent','skipped')), + to_state TEXT NOT NULL CHECK(to_state IN ('acknowledging','deferred','delivery_unknown','detected','failed','hooking','idle','none','pending','poison','processing','projection_pending','ready','reconcile_required','scheduled','sending','sent','skipped')), + code TEXT NOT NULL CHECK(code IN ('authorization_allowed','authorization_rejected','ax_mutation_authorized','candidate_persisted','context_lookup','cursor_advance_persisting','cursor_advanced','custom_redacted','delay_scheduled','enqueued','hook_ack_received','hook_dispatch_intent','local_db_confirmed','media_acquire_failed','media_acquire_ready','media_acquire_started','media_policy_rejected','model_call','model_result','pre_send_check','projection_written','reconciled','recovery_completed','recovery_started','status_changed','terminal_committed')), + source_epoch INTEGER CHECK(source_epoch IS NULL OR source_epoch BETWEEN 1 AND 9223372036854775806), + occurred_at_ns INTEGER NOT NULL CHECK(occurred_at_ns BETWEEN 1 AND 9223372036854775806) +); +"#; + +pub(super) const AUTO_REPLY_PIPELINE_TRANSITIONS_INDEX_SQL: &str = + "CREATE INDEX idx_pipeline_transitions_event_seq ON pipeline_transitions(event_id, seq);"; + +pub(super) const AUTO_REPLY_PIPELINE_TRANSITIONS_INSERT_TRIGGER_SQL: &str = r#" +CREATE TRIGGER trg_reply_jobs_transition_insert +AFTER INSERT ON reply_jobs +BEGIN + INSERT INTO pipeline_transitions( + schema_version,event_id,attempt_no,component,from_state,to_state, + code,source_epoch,occurred_at_ns + ) VALUES( + 1,NEW.event_id,NEW.attempt_no,'queue','none',NEW.status, + 'enqueued',CASE WHEN json_valid(NEW.event_json) AND json_type(NEW.event_json, '$.source_epoch') = 'integer' AND json_extract(NEW.event_json, '$.source_epoch') BETWEEN 1 AND 9223372036854775806 THEN json_extract(NEW.event_json, '$.source_epoch') ELSE NULL END,CAST((julianday('now') - 2440587.5) * 86400000000000 AS INTEGER) + ); +END; +"#; + +pub(super) const AUTO_REPLY_PIPELINE_TRANSITIONS_UPDATE_TRIGGER_SQL: &str = r#" +CREATE TRIGGER trg_reply_jobs_transition_update +AFTER UPDATE OF status ON reply_jobs +WHEN OLD.status IS NOT NEW.status +BEGIN + UPDATE reply_jobs + SET attempt_no = OLD.attempt_no + 1 + WHERE event_id = NEW.event_id AND NEW.status = 'processing'; + INSERT INTO pipeline_transitions( + schema_version,event_id,attempt_no,component,from_state,to_state, + code,source_epoch,occurred_at_ns + ) VALUES( + 1,NEW.event_id, + CASE WHEN NEW.status = 'processing' THEN OLD.attempt_no + 1 + ELSE NEW.attempt_no END, + 'queue',OLD.status,NEW.status,'status_changed', + CASE WHEN json_valid(NEW.event_json) AND json_type(NEW.event_json, '$.source_epoch') = 'integer' AND json_extract(NEW.event_json, '$.source_epoch') BETWEEN 1 AND 9223372036854775806 THEN json_extract(NEW.event_json, '$.source_epoch') ELSE NULL END,CAST((julianday('now') - 2440587.5) * 86400000000000 AS INTEGER) + ); +END; +"#; + +pub(super) const AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL: &str = r#" +CREATE TRIGGER trg_pipeline_transitions_cap +AFTER INSERT ON pipeline_transitions +BEGIN + DELETE FROM pipeline_transitions + WHERE seq < COALESCE( + (SELECT seq FROM pipeline_transitions ORDER BY seq DESC + LIMIT 1 OFFSET 4095), + 0 + ); +END; +"#; + +/// Canonicalize SQLite schema text without weakening quoted literals. SQLite +/// preserves the submitted DDL in `sqlite_master`, so comparing normalized +/// bodies detects trigger/check/index tampering while ignoring formatting and +/// SQL-keyword case only. +fn normalize_sqlite_schema_sql(sql: &str) -> String { + let mut normalized = String::with_capacity(sql.len()); + let mut quote = None; + let mut pending_space = false; + let mut characters = sql.chars().peekable(); + while let Some(character) = characters.next() { + if let Some(terminator) = quote { + normalized.push(character); + if character == terminator { + if matches!(terminator, '\'' | '"' | '`') && characters.peek() == Some(&terminator) + { + normalized.push(characters.next().expect("peeked quoted character")); + } else { + quote = None; + } + } + continue; + } + match character { + '\'' | '"' | '`' => { + if pending_space + && !normalized.is_empty() + && !normalized.ends_with(['(', ',', '=', '<', '>', ';', '+', '*']) + { + normalized.push(' '); + } + pending_space = false; + quote = Some(character); + normalized.push(character); + } + '[' => { + if pending_space + && !normalized.is_empty() + && !normalized.ends_with(['(', ',', '=', '<', '>', ';', '+', '*']) + { + normalized.push(' '); + } + pending_space = false; + quote = Some(']'); + normalized.push(character); + } + value if value.is_ascii_whitespace() => pending_space = true, + value if matches!(value, '(' | ')' | ',' | '=' | '<' | '>' | ';' | '+' | '*') => { + while normalized.ends_with(' ') { + normalized.pop(); + } + normalized.push(value); + pending_space = false; + } + value => { + if pending_space + && !normalized.is_empty() + && !normalized.ends_with(['(', ',', '=', '<', '>', ';', '+', '*']) + { + normalized.push(' '); + } + pending_space = false; + normalized.push(value.to_ascii_lowercase()); + } + } + } + while normalized.ends_with(';') { + normalized.pop(); + } + normalized +} + +pub(super) fn parse_auto_reply_transition_event_id(value: &str) -> Option<(i64, i64)> { + if !(6..=80).contains(&value.len()) || !value.is_ascii() { + return None; + } + let mut fields = value.split(':'); + if fields.next() != Some("db") { + return None; + } + let chat_id = fields.next()?; + let log_id = fields.next()?; + if fields.next().is_some() { + return None; + } + let parse_field = |field: &str| { + (!field.is_empty() + && field.len() <= 19 + && field.as_bytes()[0].is_ascii_digit() + && field.as_bytes()[0] != b'0' + && field.bytes().all(|byte| byte.is_ascii_digit()) + && field + .parse::() + .ok() + .is_some_and(|number| (1..i64::MAX).contains(&number))) + .then(|| field.parse::().expect("validated decimal i64")) + }; + Some((parse_field(chat_id)?, parse_field(log_id)?)) +} + +pub(super) fn validate_stopped_clean_queue(queue_path: &Path, expected_chat_id: i64) -> Result<()> { + const MAX_QUEUE_BYTES: u64 = 64 * 1024 * 1024; + let expected_room_name = expected_chat_id.to_string(); + if !(1..i64::MAX).contains(&expected_chat_id) + || queue_path + .parent() + .and_then(Path::file_name) + .and_then(|value| value.to_str()) + != Some(expected_room_name.as_str()) + { + anyhow::bail!("AutoReply stopped-clean queue room binding is invalid"); + } + validate_private_regular_file(queue_path, MAX_QUEUE_BYTES)?; + let connection = rusqlite::Connection::open_with_flags( + queue_path, + rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX, + ) + .with_context(|| format!("open stopped-clean queue {}", queue_path.display()))?; + connection.execute_batch("PRAGMA query_only = ON; PRAGMA busy_timeout = 5000;")?; + let quick_check: String = connection.query_row("PRAGMA quick_check", [], |row| row.get(0))?; + if quick_check != "ok" { + anyhow::bail!("AutoReply stopped-clean queue quick_check failed"); + } + let user_version: i64 = connection.query_row("PRAGMA user_version", [], |row| row.get(0))?; + let has_transition_journal = match user_version { + AUTO_REPLY_QUEUE_LEGACY_USER_VERSION => false, + AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION => true, + _ => anyhow::bail!("AutoReply stopped-clean queue version is unsupported"), + }; + let tables = connection + .prepare( + "SELECT name FROM sqlite_master \ + WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name", + )? + .query_map([], |row| row.get::<_, String>(0))? + .collect::>>()?; + let legacy_tables = vec![ + "reply_job_supersessions".to_string(), + "reply_job_tombstones".to_string(), + "reply_jobs".to_string(), + ]; + let mut legacy_tables_with_breaker = legacy_tables.clone(); + legacy_tables_with_breaker.insert(0, "model_circuit_breaker".to_string()); + let mut expected_tables = legacy_tables.clone(); + let mut expected_tables_with_breaker = legacy_tables_with_breaker.clone(); + if has_transition_journal { + expected_tables.insert(0, "pipeline_transitions".to_string()); + expected_tables_with_breaker.insert(1, "pipeline_transitions".to_string()); + } + let has_model_circuit_breaker = if tables == expected_tables { + false + } else if tables == expected_tables_with_breaker { + true + } else { + anyhow::bail!("AutoReply stopped-clean queue schema is invalid"); + }; + let validate_columns = |table: &str, expected: &[(&str, &str, i64, i64)]| -> Result<()> { + let sql = format!("PRAGMA table_info({table})"); + let actual = connection + .prepare(&sql)? + .query_map([], |row| { + Ok(( + row.get::<_, String>(1)?, + row.get::<_, String>(2)?.to_ascii_uppercase(), + row.get::<_, i64>(3)?, + row.get::<_, i64>(5)?, + )) + })? + .collect::>>()?; + let expected = expected + .iter() + .map(|(name, kind, not_null, primary_key)| { + ( + (*name).to_string(), + (*kind).to_string(), + *not_null, + *primary_key, + ) + }) + .collect::>(); + if actual != expected { + anyhow::bail!("AutoReply stopped-clean queue schema is invalid"); + } + Ok(()) + }; + let legacy_reply_job_columns = [ + ("event_id", "TEXT", 0, 1), + ("event_json", "TEXT", 1, 0), + ("status", "TEXT", 1, 0), + ("due_at", "REAL", 0, 0), + ("decision", "TEXT", 0, 0), + ("reason", "TEXT", 0, 0), + ("category", "TEXT", 0, 0), + ("reply", "TEXT", 0, 0), + ("scheduled_delay_seconds", "REAL", 0, 0), + ("error_class", "TEXT", 0, 0), + ("created_at", "REAL", 1, 0), + ("updated_at", "REAL", 1, 0), + ]; + let mut v2_reply_job_columns = legacy_reply_job_columns.to_vec(); + v2_reply_job_columns.push(("attempt_no", "INTEGER", 1, 0)); + validate_columns( + "reply_jobs", + if has_transition_journal { + &v2_reply_job_columns + } else { + &legacy_reply_job_columns + }, + )?; + validate_columns( + "reply_job_tombstones", + &[ + ("event_id", "TEXT", 0, 1), + ("status", "TEXT", 1, 0), + ("archived_at", "REAL", 1, 0), + ], + )?; + validate_columns( + "reply_job_supersessions", + &[ + ("event_id", "TEXT", 0, 1), + ("superseded_by_event_id", "TEXT", 1, 0), + ("created_at", "REAL", 1, 0), + ], + )?; + if has_model_circuit_breaker { + validate_columns( + "model_circuit_breaker", + &[ + ("model_key", "TEXT", 0, 1), + ("state", "TEXT", 1, 0), + ("failure_class", "TEXT", 1, 0), + ("consecutive_failures", "INTEGER", 1, 0), + ("open_until", "REAL", 1, 0), + ("lease_token", "TEXT", 0, 0), + ("updated_at", "REAL", 1, 0), + ], + )?; + } + if has_transition_journal { + validate_columns( + "pipeline_transitions", + &[ + ("seq", "INTEGER", 0, 1), + ("schema_version", "INTEGER", 1, 0), + ("event_id", "TEXT", 1, 0), + ("attempt_no", "INTEGER", 1, 0), + ("component", "TEXT", 1, 0), + ("from_state", "TEXT", 1, 0), + ("to_state", "TEXT", 1, 0), + ("code", "TEXT", 1, 0), + ("source_epoch", "INTEGER", 0, 0), + ("occurred_at_ns", "INTEGER", 1, 0), + ], + )?; + let table_sql: String = connection.query_row( + "SELECT sql FROM sqlite_master \ + WHERE type = 'table' AND name = 'pipeline_transitions'", + [], + |row| row.get(0), + )?; + if normalize_sqlite_schema_sql(&table_sql) + != normalize_sqlite_schema_sql(AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL) + { + anyhow::bail!("AutoReply stopped-clean journal table is invalid"); + } + } + + let schema_objects = connection + .prepare( + "SELECT type, name, sql FROM sqlite_master \ + WHERE name NOT LIKE 'sqlite_%' AND sql IS NOT NULL \ + ORDER BY type, name", + )? + .query_map([], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, String>(2)?, + )) + })? + .collect::>>()?; + let mut expected_schema_objects = vec![ + ( + "index", + "idx_reply_jobs_status_due", + AUTO_REPLY_REPLY_JOBS_STATUS_INDEX_SQL, + ), + ( + "table", + "reply_job_supersessions", + AUTO_REPLY_REPLY_JOB_SUPERSESSIONS_TABLE_SQL, + ), + ( + "table", + "reply_job_tombstones", + AUTO_REPLY_REPLY_JOB_TOMBSTONES_TABLE_SQL, + ), + ( + "table", + "reply_jobs", + if has_transition_journal { + AUTO_REPLY_REPLY_JOBS_V2_TABLE_SQL + } else { + AUTO_REPLY_REPLY_JOBS_TABLE_SQL + }, + ), + ]; + if has_model_circuit_breaker { + expected_schema_objects.insert( + 1, + ( + "table", + "model_circuit_breaker", + AUTO_REPLY_MODEL_CIRCUIT_BREAKER_TABLE_SQL, + ), + ); + } + if has_transition_journal { + expected_schema_objects.extend([ + ( + "index", + "idx_pipeline_transitions_event_seq", + AUTO_REPLY_PIPELINE_TRANSITIONS_INDEX_SQL, + ), + ( + "table", + "pipeline_transitions", + AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL, + ), + ( + "trigger", + "trg_pipeline_transitions_cap", + AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL, + ), + ( + "trigger", + "trg_reply_jobs_transition_insert", + AUTO_REPLY_PIPELINE_TRANSITIONS_INSERT_TRIGGER_SQL, + ), + ( + "trigger", + "trg_reply_jobs_transition_update", + AUTO_REPLY_PIPELINE_TRANSITIONS_UPDATE_TRIGGER_SQL, + ), + ]); + } + expected_schema_objects.sort_by_key(|(kind, name, _)| (*kind, *name)); + if schema_objects.len() != expected_schema_objects.len() + || schema_objects.iter().zip(&expected_schema_objects).any( + |((kind, name, sql), (expected_kind, expected_name, expected_sql))| { + kind != expected_kind + || name != expected_name + || normalize_sqlite_schema_sql(sql) != normalize_sqlite_schema_sql(expected_sql) + }, + ) + { + anyhow::bail!("AutoReply stopped-clean queue schema SQL is invalid"); + } + + let expected_index_names = if has_transition_journal { + vec![ + "idx_pipeline_transitions_event_seq", + "idx_reply_jobs_status_due", + ] + } else { + vec!["idx_reply_jobs_status_due"] + }; + let index_names = schema_objects + .iter() + .filter(|(kind, _, _)| kind == "index") + .map(|(_, name, _)| name.as_str()) + .collect::>(); + if index_names != expected_index_names { + anyhow::bail!("AutoReply stopped-clean queue index set is invalid"); + } + let status_index_columns = connection + .prepare("PRAGMA index_info(idx_reply_jobs_status_due)")? + .query_map([], |row| row.get::<_, String>(2))? + .collect::>>()?; + if status_index_columns != ["status", "due_at"] { + anyhow::bail!("AutoReply stopped-clean queue index is invalid"); + } + if has_transition_journal { + let journal_index_columns = connection + .prepare("PRAGMA index_info(idx_pipeline_transitions_event_seq)")? + .query_map([], |row| row.get::<_, String>(2))? + .collect::>>()?; + if journal_index_columns != ["event_id", "seq"] { + anyhow::bail!("AutoReply stopped-clean journal index is invalid"); + } + } + + let triggers = connection + .prepare( + "SELECT name, tbl_name, sql FROM sqlite_master \ + WHERE type = 'trigger' ORDER BY name", + )? + .query_map([], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, Option>(2)?, + )) + })? + .collect::>>()?; + if has_transition_journal { + let expected_triggers = [ + ( + "trg_pipeline_transitions_cap", + "pipeline_transitions", + AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL, + ), + ( + "trg_reply_jobs_transition_insert", + "reply_jobs", + AUTO_REPLY_PIPELINE_TRANSITIONS_INSERT_TRIGGER_SQL, + ), + ( + "trg_reply_jobs_transition_update", + "reply_jobs", + AUTO_REPLY_PIPELINE_TRANSITIONS_UPDATE_TRIGGER_SQL, + ), + ]; + if triggers.len() != expected_triggers.len() + || triggers.iter().zip(expected_triggers).any( + |((name, table, sql), (expected_name, expected_table, expected_sql))| { + name != expected_name + || table != expected_table + || sql.as_deref().map(normalize_sqlite_schema_sql) + != Some(normalize_sqlite_schema_sql(expected_sql)) + }, + ) + { + anyhow::bail!("AutoReply stopped-clean journal triggers are invalid"); + } + let journal_rows: i64 = + connection.query_row("SELECT COUNT(*) FROM pipeline_transitions", [], |row| { + row.get(0) + })?; + if !(0..=AUTO_REPLY_QUEUE_JOURNAL_MAX_ROWS).contains(&journal_rows) { + anyhow::bail!("AutoReply stopped-clean journal row cap is invalid"); + } + let journal_event_ids = connection + .prepare("SELECT event_id FROM pipeline_transitions ORDER BY seq")? + .query_map([], |row| row.get::<_, String>(0))? + .collect::>>()?; + if journal_event_ids.iter().any(|event_id| { + parse_auto_reply_transition_event_id(event_id) + .is_none_or(|(chat_id, _)| chat_id != expected_chat_id) + }) { + anyhow::bail!("AutoReply stopped-clean journal identity is invalid"); + } + let invalid_journal_rows: i64 = connection.query_row( + "SELECT COUNT(*) FROM pipeline_transitions WHERE \ + typeof(schema_version) != 'integer' OR schema_version != 1 OR \ + typeof(attempt_no) != 'integer' OR attempt_no NOT BETWEEN 0 AND 1000000 OR \ + component NOT IN ('authorization','ax','burst','context','delay','ingress','media','model','pre_send','projection','queue','recovery','terminal') OR \ + from_state NOT IN ('acknowledging','deferred','delivery_unknown','detected','failed','hooking','idle','none','pending','poison','processing','projection_pending','ready','reconcile_required','scheduled','sending','sent','skipped') OR \ + to_state NOT IN ('acknowledging','deferred','delivery_unknown','detected','failed','hooking','idle','none','pending','poison','processing','projection_pending','ready','reconcile_required','scheduled','sending','sent','skipped') OR \ + code NOT IN ('authorization_allowed','authorization_rejected','ax_mutation_authorized','candidate_persisted','context_lookup','cursor_advance_persisting','cursor_advanced','custom_redacted','delay_scheduled','enqueued','hook_ack_received','hook_dispatch_intent','local_db_confirmed','media_acquire_failed','media_acquire_ready','media_acquire_started','media_policy_rejected','model_call','model_result','pre_send_check','projection_written','reconciled','recovery_completed','recovery_started','status_changed','terminal_committed') OR \ + (source_epoch IS NOT NULL AND (typeof(source_epoch) != 'integer' OR source_epoch NOT BETWEEN 1 AND 9223372036854775806)) OR \ + typeof(occurred_at_ns) != 'integer' OR occurred_at_ns NOT BETWEEN 1 AND 9223372036854775806", + [], + |row| row.get(0), + )?; + if invalid_journal_rows != 0 { + anyhow::bail!("AutoReply stopped-clean journal metadata is invalid"); + } + } else if !triggers.is_empty() { + anyhow::bail!("AutoReply stopped-clean legacy queue has triggers"); + } + + let reply_identities = connection + .prepare("SELECT event_id,event_json FROM reply_jobs ORDER BY event_id")? + .query_map([], |row| { + Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)) + })? + .collect::>>()?; + for (event_id, event_json) in reply_identities { + let Some((chat_id, log_id)) = parse_auto_reply_transition_event_id(&event_id) else { + anyhow::bail!("AutoReply stopped-clean queue event identity is invalid"); + }; + if chat_id != expected_chat_id { + anyhow::bail!("AutoReply stopped-clean queue event belongs to another room"); + } + let event: serde_json::Value = serde_json::from_str(&event_json) + .context("parse stopped-clean queue event identity")?; + let event = event + .as_object() + .context("AutoReply stopped-clean queue event identity is malformed")?; + for key in ["event_id", "canonical_event_id"] { + if event + .get(key) + .is_some_and(|value| value.as_str() != Some(event_id.as_str())) + { + anyhow::bail!("AutoReply stopped-clean queue event identity mismatches"); + } + } + let event_log_id = event.get("log_id").and_then(serde_json::Value::as_i64); + let proactive = event.get("proactive") == Some(&serde_json::Value::Bool(true)); + if event + .get("chat_id") + .is_some_and(|value| value.as_i64() != Some(expected_chat_id)) + || if proactive { + event_log_id.is_some_and(|value| !(1..MAX_INT64).contains(&value)) + } else { + event_log_id.is_some_and(|value| value != log_id) + } + { + anyhow::bail!("AutoReply stopped-clean queue JSON identity mismatches"); + } + } + let tombstone_identities = connection + .prepare("SELECT event_id FROM reply_job_tombstones ORDER BY event_id")? + .query_map([], |row| row.get::<_, String>(0))? + .collect::>>()?; + if tombstone_identities.iter().any(|event_id| { + parse_auto_reply_transition_event_id(event_id) + .is_none_or(|(chat_id, _)| chat_id != expected_chat_id) + }) { + anyhow::bail!("AutoReply stopped-clean queue tombstone identity is invalid"); + } + let supersession_identities = connection + .prepare( + "SELECT event_id,superseded_by_event_id \ + FROM reply_job_supersessions ORDER BY event_id", + )? + .query_map([], |row| { + Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)) + })? + .collect::>>()?; + if supersession_identities + .iter() + .any(|(event_id, successor_id)| { + [event_id, successor_id].into_iter().any(|value| { + parse_auto_reply_transition_event_id(value) + .is_none_or(|(chat_id, _)| chat_id != expected_chat_id) + }) + }) + { + anyhow::bail!("AutoReply stopped-clean queue supersession identity is invalid"); + } + if has_transition_journal { + let invalid_attempts: i64 = connection.query_row( + "SELECT COUNT(*) FROM reply_jobs WHERE \ + typeof(attempt_no) != 'integer' OR attempt_no NOT BETWEEN 0 AND 1000000", + [], + |row| row.get(0), + )?; + if invalid_attempts != 0 { + anyhow::bail!("AutoReply stopped-clean queue attempt authority is invalid"); + } + } + + for table in ["reply_jobs", "reply_job_tombstones"] { + let sql = format!( + "SELECT COUNT(*) FROM {table} \ + WHERE status IS NULL OR status NOT IN ('sent', 'skipped')" + ); + let nonterminal: i64 = connection.query_row(&sql, [], |row| row.get(0))?; + if nonterminal != 0 { + anyhow::bail!("AutoReply stopped-clean queue has nonterminal rows"); + } + } + Ok(()) +} diff --git a/src/commands/auto_reply/runner.rs b/src/commands/auto_reply/runner.rs new file mode 100644 index 00000000..35fa9c17 --- /dev/null +++ b/src/commands/auto_reply/runner.rs @@ -0,0 +1,403 @@ +//! Resolve and validate the Python supervisor and the configured reply-runner +//! executables before any child starts. + +use std::fs; +use std::io::Read; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +use anyhow::{Context, Result}; +use sha2::{Digest, Sha256}; + +use crate::config; + +pub(super) const AUTO_REPLY_PYTHON_ISOLATION_ARGS: [&str; 3] = ["-E", "-B", "-S"]; + +pub(super) fn resolve_auto_reply_supervisor(binary: &Path) -> Result<(PathBuf, PathBuf)> { + let mut candidates = Vec::new(); + if let Some(runtime_root) = + std::env::var_os("OPENKAKAO_AUTO_REPLY_RUNTIME_ROOT").filter(|value| !value.is_empty()) + { + candidates.push(PathBuf::from(runtime_root).join("scripts")); + } + if let Some(parent) = binary.parent() { + candidates.push(parent.join("scripts")); + if let Some(prefix) = parent.parent() { + candidates.push(prefix.join("libexec").join("scripts")); + } + } + candidates.push(Path::new(env!("CARGO_MANIFEST_DIR")).join("scripts")); + + for scripts_root in candidates { + let scripts_metadata = match fs::symlink_metadata(&scripts_root) { + Ok(metadata) => metadata, + Err(_) => continue, + }; + if !scripts_metadata.is_dir() || scripts_metadata.file_type().is_symlink() { + continue; + } + let supervisor = scripts_root.join("auto-reply-supervisor.py"); + let runtime_files = [ + "auto-reply-supervisor.py", + "auto-reply-db-watch.py", + "auto-reply-worker.py", + "auto-reply-apple-watch.py", + "auto_reply_ax_ui.py", + "auto_reply_metrics.py", + "auto-reply-schema.json", + ]; + let trusted = runtime_files.iter().all(|name| { + let path = scripts_root.join(name); + let Ok(metadata) = fs::symlink_metadata(path) else { + return false; + }; + if !metadata.is_file() || metadata.file_type().is_symlink() { + return false; + } + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + if metadata.mode() & 0o022 != 0 { + return false; + } + } + true + }); + if trusted { + let runtime_root = scripts_root + .parent() + .map(Path::to_path_buf) + .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR"))); + return Ok((supervisor, runtime_root)); + } + } + anyhow::bail!( + "AutoReply runtime assets are missing; expected auto-reply-supervisor.py beside the binary or in the packaged scripts directory" + ) +} + +pub(super) fn validate_auto_reply_executable( + value: Option<&str>, + label: &str, + default_name: &str, + probe_python_version: bool, +) -> Result { + let value = value + .context(format!("{label} must be configured explicitly"))? + .trim(); + if value.is_empty() || value.chars().any(|ch| ch.is_control()) { + anyhow::bail!("{label} must be a non-empty executable path"); + } + let configured = Path::new(value); + if !configured.is_absolute() { + anyhow::bail!("{label} must be an absolute path"); + } + if default_name == "python3" && is_homebrew_cellar_version_path(configured) { + anyhow::bail!("{label} must not be a Homebrew Cellar version path"); + } + let configured_metadata = fs::symlink_metadata(configured).with_context(|| { + if default_name == "python3" { + format!("python_interpreter_missing: {label} is unavailable") + } else { + format!("{label} does not resolve to a readable executable") + } + })?; + if configured.is_symlink() { + if default_name != "python3" && default_name != "opencodex" { + anyhow::bail!("{label} must not be a symlink"); + } + if default_name == "python3" && !is_homebrew_opt_python_keg_path(configured) { + anyhow::bail!("{label} symlink must be a Homebrew opt python keg path"); + } + } else if !configured_metadata.is_file() { + anyhow::bail!("{label} is not a regular file"); + } + let target = fs::canonicalize(configured).with_context(|| { + if default_name == "python3" { + format!("python_interpreter_missing: {label} target is unavailable") + } else { + format!("{label} does not resolve to a readable executable") + } + })?; + let metadata = fs::symlink_metadata(&target)?; + if !metadata.is_file() { + anyhow::bail!("{label} is not a regular file"); + } + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + if metadata.mode() & 0o022 != 0 || metadata.mode() & 0o111 == 0 { + anyhow::bail!("{label} has unsafe ownership or executable permissions"); + } + } + if default_name == "python3" && probe_python_version { + let output = Command::new(&target) + .args([ + "-I", + "-B", + "-S", + "-c", + "import sys; print(f'{sys.implementation.name} {sys.version_info.major}.{sys.version_info.minor}')", + ]) + .output() + .with_context(|| format!("probe {label} version"))?; + let version = String::from_utf8_lossy(&output.stdout); + let trusted_version = ["cpython 3.11", "cpython 3.12", "cpython 3.13"] + .iter() + .any(|prefix| version.trim() == *prefix); + if !output.status.success() || !trusted_version || !output.stderr.is_empty() { + anyhow::bail!("{label} must be CPython 3.11, 3.12, or 3.13"); + } + } + if default_name == "python3" { + Ok(configured.to_string_lossy().into_owned()) + } else { + Ok(target.to_string_lossy().into_owned()) + } +} + +pub(super) fn is_homebrew_opt_python_keg_path(path: &Path) -> bool { + path.to_str().is_some_and(|value| { + matches!( + value, + "/opt/homebrew/opt/python@3.11/bin/python3.11" + | "/opt/homebrew/opt/python@3.12/bin/python3.12" + | "/opt/homebrew/opt/python@3.13/bin/python3.13" + ) + }) +} + +pub(super) fn is_homebrew_cellar_version_path(path: &Path) -> bool { + path.to_str() + .is_some_and(|value| value.contains("/Cellar/python@")) +} + +#[derive(Debug, Clone)] +pub(super) struct AutoReplyRunner { + pub(super) path: String, + pub(super) kind: String, + pub(super) model: String, + pub(super) reasoning_effort: String, + pub(super) service_tier: String, + pub(super) sha256: String, + pub(super) codex_home: Option, +} + +fn sha256_file(path: &Path) -> Result { + let mut file = fs::File::open(path) + .with_context(|| format!("open executable for digest: {}", path.display()))?; + let mut hasher = Sha256::new(); + let mut buffer = [0_u8; 1024 * 1024]; + loop { + let read = file + .read(&mut buffer) + .with_context(|| format!("read executable for digest: {}", path.display()))?; + if read == 0 { + break; + } + hasher.update(&buffer[..read]); + } + Ok(hex::encode(hasher.finalize())) +} + +#[cfg(unix)] +pub(super) fn validate_auto_reply_codex_auth(path: &Path, uid: u32) -> Result<()> { + use std::os::unix::fs::MetadataExt; + + let metadata = fs::symlink_metadata(path) + .context("AutoReply reply_codex_home/auth.json is unavailable")?; + if metadata.file_type().is_symlink() + || !metadata.is_file() + || metadata.uid() != uid + || metadata.nlink() != 1 + || metadata.mode() & 0o077 != 0 + || metadata.len() == 0 + { + anyhow::bail!("AutoReply Codex auth must be a private non-empty user-owned regular file"); + } + Ok(()) +} + +pub(super) fn validate_auto_reply_runner( + config: &config::OpenKakaoConfig, +) -> Result { + let runner = config + .auto_reply + .reply_runner + .as_deref() + .context("AutoReply reply_runner must be configured explicitly")?; + let kind = config + .auto_reply + .reply_runner_kind + .as_deref() + .unwrap_or("gjc"); + if !matches!(kind, "codex" | "gjc" | "opencodex") { + anyhow::bail!("AutoReply reply_runner_kind must be codex, gjc or opencodex"); + } + let resolved = + validate_auto_reply_executable(Some(runner), "AutoReply reply_runner", kind, false)?; + let home = dirs::home_dir() + .context("resolve home directory for AutoReply reply_runner")? + .canonicalize() + .context("resolve canonical home directory for AutoReply reply_runner")?; + let resolved_path = Path::new(&resolved); + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + let uid = unsafe { libc::geteuid() }; + let metadata = fs::metadata(resolved_path)?; + if metadata.uid() != uid { + anyhow::bail!("AutoReply reply_runner must be owned by the current user"); + } + if resolved_path.starts_with(&home) { + let mut current = resolved_path.parent(); + while let Some(path) = current { + let metadata = fs::metadata(path)?; + if metadata.uid() != uid || metadata.mode() & 0o022 != 0 { + anyhow::bail!( + "AutoReply reply_runner parent has unsafe ownership or permissions" + ); + } + if path == home { + break; + } + current = path.parent(); + } + } else { + let codex_prefix = Path::new("/opt/homebrew/lib/node_modules/@openai/codex/"); + let ocx_prefix = Path::new("/opt/homebrew/lib/node_modules/@bitkyc08/opencodex/"); + if kind == "opencodex" { + if !resolved_path.starts_with(ocx_prefix) + && !resolved_path.starts_with("/opt/homebrew/") + { + anyhow::bail!( + "AutoReply reply_runner for opencodex must be in Homebrew prefix" + ); + } + } else if kind != "codex" + || !resolved_path.starts_with(codex_prefix) + || resolved_path.file_name().and_then(|value| value.to_str()) != Some("codex") + { + anyhow::bail!( + "AutoReply reply_runner outside the current user's home must be the native Homebrew Codex CLI" + ); + } + } + } + let model = config + .auto_reply + .reply_model + .as_deref() + .unwrap_or(if kind == "codex" { + "gpt-5.6-luna" + } else if kind == "gjc" { + "google-antigravity/gemini-3.7-flash-tiered" + } else { + "" + }) + .trim() + .to_owned(); + if matches!(kind, "codex" | "gjc") + && (model.is_empty() + || model.len() > 128 + || !model + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || b"._-/".contains(&byte))) + { + anyhow::bail!("AutoReply reply_model is invalid"); + } + let reasoning_effort = config + .auto_reply + .reply_reasoning_effort + .as_deref() + .unwrap_or(if kind == "codex" { "max" } else { "medium" }) + .trim() + .to_owned(); + if kind == "codex" + && !matches!( + reasoning_effort.as_str(), + "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max" + ) + { + anyhow::bail!("AutoReply reply_reasoning_effort is invalid"); + } + let service_tier = config + .auto_reply + .reply_service_tier + .as_deref() + .unwrap_or(if kind == "codex" { + "priority" + } else { + "default" + }) + .trim() + .to_owned(); + if kind == "codex" && !matches!(service_tier.as_str(), "default" | "priority" | "flex") { + anyhow::bail!("AutoReply reply_service_tier is invalid"); + } + if kind == "codex" { + let output = Command::new(resolved_path) + .arg("--version") + .stdin(Stdio::null()) + .output() + .context("probe AutoReply Codex reply runner")?; + let version = String::from_utf8_lossy(&output.stdout); + if !output.status.success() + || !version.trim().starts_with("codex-cli ") + || !output.stderr.is_empty() + { + // Name the runner and its observed version: selecting the Codex + // model switches `reply_runner_kind` but keeps the configured + // `reply_runner`, so a gjc binary left in place is the most likely + // cause and a bare "probe failed" would hide it. + anyhow::bail!( + "AutoReply Codex reply runner version probe failed: runner={} status={} stdout={:?} stderr={:?}", + resolved_path.display(), + output.status, + version.trim(), + String::from_utf8_lossy(&output.stderr).trim() + ); + } + } + let codex_home = if kind == "codex" { + let configured = config + .auto_reply + .reply_codex_home + .as_deref() + .context("AutoReply reply_codex_home must be configured for Codex")?; + let path = Path::new(configured); + if !path.is_absolute() { + anyhow::bail!("AutoReply reply_codex_home must be an absolute path"); + } + let resolved_home = + fs::canonicalize(path).context("AutoReply reply_codex_home must already exist")?; + if !resolved_home.starts_with(&home) { + anyhow::bail!("AutoReply reply_codex_home must stay inside the current user's home"); + } + let metadata = fs::symlink_metadata(&resolved_home)?; + if !metadata.is_dir() || metadata.file_type().is_symlink() { + anyhow::bail!("AutoReply reply_codex_home must be a real directory"); + } + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + let uid = unsafe { libc::geteuid() }; + if metadata.uid() != uid || metadata.mode() & 0o077 != 0 { + anyhow::bail!("AutoReply reply_codex_home must be private and user-owned"); + } + validate_auto_reply_codex_auth(&resolved_home.join("auth.json"), uid)?; + } + Some(resolved_home.to_string_lossy().into_owned()) + } else { + None + }; + Ok(AutoReplyRunner { + path: resolved.clone(), + kind: kind.to_owned(), + model, + reasoning_effort, + service_tier, + sha256: sha256_file(resolved_path)?, + codex_home, + }) +} diff --git a/src/commands/auto_reply/state_root.rs b/src/commands/auto_reply/state_root.rs new file mode 100644 index 00000000..c2e04d3a --- /dev/null +++ b/src/commands/auto_reply/state_root.rs @@ -0,0 +1,618 @@ +//! The private AutoReply state root: no-follow directory walks, the owner lock, +//! atomic enrollment writes, the aggregate status file, and bounded reads. + +use std::fs; +use std::io::Write; +#[cfg(unix)] +use std::os::unix::io::AsRawFd; +#[cfg(unix)] +use std::os::unix::io::FromRawFd; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command}; + +use anyhow::{Context, Result}; + +use crate::{config, local_db}; +use openkakao_cli::auto_reply_service; + +pub(super) fn auto_reply_state_root( + config: &config::OpenKakaoConfig, +) -> Result { + let root = config + .auto_reply + .state_root + .as_deref() + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + dirs::home_dir() + .map(|home| auto_reply_service::default_state_root(&home)) + .unwrap_or_else(|| std::path::PathBuf::from("auto-reply")) + }); + if !root.is_absolute() { + anyhow::bail!("AutoReply state_root must be an absolute path"); + } + Ok(root) +} + +pub(super) fn auto_reply_legacy_conflict(root: &Path) -> Result<()> { + let status_path = root.join("supervisor-status.json"); + let status_metadata = match fs::symlink_metadata(&status_path) { + Ok(metadata) => Some(metadata), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => None, + Err(error) => { + return Err(error).with_context(|| format!("inspect {}", status_path.display())); + } + }; + if status_metadata.is_none() { + for residual in [ + "db-watch-state.json", + "reply-state.json", + "reply-queue.sqlite3", + "reply-queue.sqlite3-wal", + "reply-queue.sqlite3-shm", + ] { + let path = root.join(residual); + if fs::symlink_metadata(&path).is_ok() { + anyhow::bail!( + "legacy AutoReply residual state exists without a drained supervisor status: {}", + path.display() + ); + } + } + return Ok(()); + } + if status_metadata.as_ref().is_some_and(|metadata| { + metadata.file_type().is_symlink() || !metadata.file_type().is_file() + }) { + anyhow::bail!("existing AutoReply supervisor status is not a regular file"); + } + let value = read_bounded_json_file(&status_path)?; + let active_state = value.get("state").and_then(serde_json::Value::as_str); + if matches!( + active_state, + Some("starting" | "running" | "stopping" | "fenced") + ) { + anyhow::bail!( + "existing AutoReply supervisor is running at {}; stop/drain it before CLI activation", + status_path.display() + ); + } + if active_state == Some("stopped") + && value.get("legacy_drained") != Some(&serde_json::Value::Bool(true)) + { + anyhow::bail!( + "existing AutoReply supervisor at {} is stopped but not explicitly drained; resolve terminal queue state before CLI activation", + status_path.display() + ); + } + if active_state != Some("stopped") { + anyhow::bail!( + "existing AutoReply supervisor status at {} is not a recognized drained state", + status_path.display() + ); + } + Ok(()) +} + +#[cfg(unix)] +fn walk_auto_reply_directory_no_follow(path: &Path, create: bool) -> Result { + use std::ffi::CString; + use std::os::unix::ffi::OsStrExt; + use std::os::unix::fs::{MetadataExt, OpenOptionsExt}; + use std::path::Component; + + if !path.is_absolute() { + anyhow::bail!("AutoReply directory must be an absolute path"); + } + let mut options = fs::OpenOptions::new(); + options + .read(true) + .custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_DIRECTORY); + let mut directory = options.open("/").context("open filesystem root")?; + let mut saw_component = false; + for component in path.components() { + let name = match component { + Component::RootDir => continue, + Component::Normal(name) => name, + Component::CurDir | Component::ParentDir | Component::Prefix(_) => { + anyhow::bail!("AutoReply directory path is not normalized") + } + }; + saw_component = true; + let name = CString::new(name.as_bytes()).context("AutoReply directory contains NUL")?; + let open_component = || unsafe { + libc::openat( + directory.as_raw_fd(), + name.as_ptr(), + libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_DIRECTORY, + ) + }; + let mut descriptor = open_component(); + let mut created = false; + if descriptor < 0 { + let error = std::io::Error::last_os_error(); + if !create || error.raw_os_error() != Some(libc::ENOENT) { + return Err(error).with_context(|| { + format!( + "open AutoReply directory component {}", + name.to_string_lossy() + ) + }); + } + if unsafe { libc::mkdirat(directory.as_raw_fd(), name.as_ptr(), 0o700) } != 0 { + let mkdir_error = std::io::Error::last_os_error(); + if mkdir_error.raw_os_error() != Some(libc::EEXIST) { + return Err(mkdir_error).with_context(|| { + format!( + "create AutoReply directory component {}", + name.to_string_lossy() + ) + }); + } + } else { + created = true; + } + descriptor = open_component(); + if descriptor < 0 { + return Err(std::io::Error::last_os_error()).with_context(|| { + format!( + "open created AutoReply directory component {}", + name.to_string_lossy() + ) + }); + } + } + // SAFETY: openat returned a newly owned descriptor on success. + let next = unsafe { std::fs::File::from_raw_fd(descriptor) }; + let metadata = next.metadata()?; + if !metadata.file_type().is_dir() || metadata.nlink() == 0 { + anyhow::bail!("AutoReply directory component is unsafe"); + } + if created { + let uid = unsafe { libc::geteuid() }; + if metadata.uid() != uid || unsafe { libc::fchmod(next.as_raw_fd(), 0o700) } != 0 { + anyhow::bail!("created AutoReply directory component is unsafe"); + } + let private = next.metadata()?; + if private.uid() != uid || private.mode() & 0o777 != 0o700 { + anyhow::bail!("created AutoReply directory component is not private"); + } + } + directory = next; + } + if !saw_component { + anyhow::bail!("AutoReply state root cannot be the filesystem root"); + } + Ok(directory) +} + +#[cfg(unix)] +fn open_private_auto_reply_directory( + path: &Path, + label: &str, + create: bool, +) -> Result { + use std::os::unix::fs::MetadataExt; + + if !path.is_absolute() { + anyhow::bail!("AutoReply {label} must be an absolute path"); + } + let directory = walk_auto_reply_directory_no_follow(path, create) + .with_context(|| format!("open AutoReply {label} {}", path.display()))?; + let uid = unsafe { libc::geteuid() }; + let initial = directory + .metadata() + .with_context(|| format!("inspect open AutoReply {label} {}", path.display()))?; + if !initial.file_type().is_dir() || initial.uid() != uid || initial.nlink() == 0 { + anyhow::bail!("AutoReply {label} is unsafe: {}", path.display()); + } + if unsafe { libc::fchmod(directory.as_raw_fd(), 0o700) } != 0 { + return Err(std::io::Error::last_os_error()) + .with_context(|| format!("privatize AutoReply {label} {}", path.display())); + } + let private = directory.metadata()?; + let current_directory = walk_auto_reply_directory_no_follow(path, false) + .with_context(|| format!("revalidate AutoReply {label} {}", path.display()))?; + let current = current_directory.metadata()?; + if !private.file_type().is_dir() + || private.uid() != uid + || private.nlink() == 0 + || private.mode() & 0o777 != 0o700 + || !current.file_type().is_dir() + || current.uid() != uid + || current.nlink() == 0 + || current.mode() & 0o777 != 0o700 + || (private.dev(), private.ino()) != (current.dev(), current.ino()) + { + anyhow::bail!("AutoReply {label} is not private: {}", path.display()); + } + Ok(directory) +} + +#[cfg(not(unix))] +fn ensure_private_auto_reply_directory(path: &Path, label: &str, create: bool) -> Result<()> { + if !path.is_absolute() { + anyhow::bail!("AutoReply {label} must be an absolute path"); + } + if create { + fs::create_dir_all(path) + .with_context(|| format!("create AutoReply {label} {}", path.display()))?; + } + let metadata = fs::symlink_metadata(path) + .with_context(|| format!("inspect AutoReply {label} {}", path.display()))?; + if metadata.file_type().is_symlink() || !metadata.file_type().is_dir() { + anyhow::bail!( + "AutoReply {label} must be a real directory: {}", + path.display() + ); + } + Ok(()) +} + +pub(super) fn acquire_auto_reply_owner_lock(root: &Path) -> Result { + #[cfg(unix)] + let root_directory = open_private_auto_reply_directory(root, "state root", true)?; + #[cfg(not(unix))] + ensure_private_auto_reply_directory(root, "state root", true)?; + let path = root.join("supervisor.owner.lock"); + + #[cfg(unix)] + let file = { + use std::ffi::CString; + use std::os::unix::ffi::OsStrExt; + + let name = CString::new( + path.file_name() + .context("AutoReply owner lock name missing")? + .as_bytes(), + ) + .context("AutoReply owner lock name contains NUL")?; + let descriptor = unsafe { + libc::openat( + root_directory.as_raw_fd(), + name.as_ptr(), + libc::O_RDWR | libc::O_CREAT | libc::O_CLOEXEC | libc::O_NOFOLLOW, + 0o600, + ) + }; + if descriptor < 0 { + return Err(std::io::Error::last_os_error()) + .with_context(|| format!("open AutoReply owner lock {}", path.display())); + } + // SAFETY: openat returned a newly owned descriptor on success. + unsafe { std::fs::File::from_raw_fd(descriptor) } + }; + + #[cfg(not(unix))] + let file = { + let mut options = fs::OpenOptions::new(); + options.create(true).truncate(false).read(true).write(true); + options + .open(&path) + .with_context(|| format!("open AutoReply owner lock {}", path.display()))? + }; + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + let metadata = file + .metadata() + .with_context(|| format!("inspect AutoReply owner lock {}", path.display()))?; + let uid = unsafe { libc::geteuid() }; + if !metadata.file_type().is_file() || metadata.uid() != uid || metadata.nlink() != 1 { + anyhow::bail!("AutoReply owner lock is unsafe: {}", path.display()); + } + if unsafe { libc::fchmod(file.as_raw_fd(), 0o600) } != 0 { + return Err(std::io::Error::last_os_error()) + .with_context(|| format!("privatize AutoReply owner lock {}", path.display())); + } + let metadata = file.metadata()?; + if metadata.mode() & 0o777 != 0o600 { + anyhow::bail!("AutoReply owner lock is not private: {}", path.display()); + } + if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } != 0 { + anyhow::bail!( + "AutoReply owner lock is held by another supervisor: {}", + path.display() + ); + } + } + Ok(file) +} + +#[cfg(unix)] +fn validate_private_auto_reply_file_at( + directory: &std::fs::File, + name: &std::ffi::CStr, + expected_identity: Option<(u64, u64)>, +) -> Result { + use std::os::unix::fs::MetadataExt; + + let descriptor = unsafe { + libc::openat( + directory.as_raw_fd(), + name.as_ptr(), + libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_NONBLOCK, + ) + }; + if descriptor < 0 { + return Err(std::io::Error::last_os_error()).context("open private AutoReply file"); + } + // SAFETY: openat returned a newly owned descriptor on success. + let file = unsafe { std::fs::File::from_raw_fd(descriptor) }; + let metadata = file.metadata()?; + let uid = unsafe { libc::geteuid() }; + let entry_descriptor = unsafe { + libc::openat( + directory.as_raw_fd(), + name.as_ptr(), + libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_NONBLOCK, + ) + }; + if entry_descriptor < 0 { + return Err(std::io::Error::last_os_error()) + .context("inspect private AutoReply file entry"); + } + // SAFETY: openat returned a newly owned descriptor on success. + let entry = unsafe { std::fs::File::from_raw_fd(entry_descriptor) }; + let entry_metadata = entry.metadata()?; + if !metadata.file_type().is_file() + || metadata.uid() != uid + || metadata.nlink() != 1 + || metadata.mode() & 0o777 != 0o600 + || !entry_metadata.file_type().is_file() + || entry_metadata.uid() != uid + || entry_metadata.nlink() != 1 + || entry_metadata.mode() & 0o777 != 0o600 + || (metadata.dev(), metadata.ino()) != (entry_metadata.dev(), entry_metadata.ino()) + || expected_identity.is_some_and(|identity| identity != (metadata.dev(), metadata.ino())) + { + anyhow::bail!("unsafe private AutoReply file"); + } + Ok(file) +} + +#[cfg(unix)] +pub(super) fn write_private_auto_reply_enrollment(root: &Path, bytes: &[u8]) -> Result<()> { + use rand::RngCore; + use std::ffi::CString; + use std::os::unix::fs::MetadataExt; + + let directory = open_private_auto_reply_directory(root, "state root", false)?; + let final_name = CString::new("enrollment.json").expect("static enrollment name"); + + // Refuse rather than silently normalize an existing unsafe enrollment + // authority. Replacing a symlink or hard link would obscure evidence that + // another pathname had become part of the foreground safety boundary. + let existing_descriptor = unsafe { + libc::openat( + directory.as_raw_fd(), + final_name.as_ptr(), + libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_NONBLOCK, + ) + }; + if existing_descriptor >= 0 { + // SAFETY: openat returned a newly owned descriptor on success. + let existing = unsafe { std::fs::File::from_raw_fd(existing_descriptor) }; + drop(existing); + validate_private_auto_reply_file_at(&directory, &final_name, None) + .context("existing AutoReply enrollment authority is unsafe")?; + } else { + let error = std::io::Error::last_os_error(); + if error.raw_os_error() != Some(libc::ENOENT) { + return Err(error).context("inspect existing AutoReply enrollment authority"); + } + } + + let mut random = [0_u8; 16]; + rand::thread_rng().fill_bytes(&mut random); + let temporary_name = CString::new(format!( + ".enrollment.{}.{}.tmp", + std::process::id(), + hex::encode(random) + ))?; + let descriptor = unsafe { + libc::openat( + directory.as_raw_fd(), + temporary_name.as_ptr(), + libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_CLOEXEC | libc::O_NOFOLLOW, + 0o600, + ) + }; + if descriptor < 0 { + return Err(std::io::Error::last_os_error()) + .context("create private AutoReply enrollment temporary file"); + } + // SAFETY: openat returned a newly owned descriptor on success. + let mut output = unsafe { std::fs::File::from_raw_fd(descriptor) }; + let result = (|| -> Result<()> { + if unsafe { libc::fchmod(output.as_raw_fd(), 0o600) } != 0 { + return Err(std::io::Error::last_os_error()) + .context("privatize AutoReply enrollment temporary file"); + } + let metadata = output.metadata()?; + let uid = unsafe { libc::geteuid() }; + if !metadata.file_type().is_file() + || metadata.uid() != uid + || metadata.nlink() != 1 + || metadata.mode() & 0o777 != 0o600 + { + anyhow::bail!("AutoReply enrollment temporary file is unsafe"); + } + let identity = (metadata.dev(), metadata.ino()); + output.write_all(bytes)?; + output.sync_all()?; + validate_private_auto_reply_file_at(&directory, &temporary_name, Some(identity))?; + if unsafe { + libc::renameat( + directory.as_raw_fd(), + temporary_name.as_ptr(), + directory.as_raw_fd(), + final_name.as_ptr(), + ) + } != 0 + { + return Err(std::io::Error::last_os_error()) + .context("install private AutoReply enrollment authority"); + } + validate_private_auto_reply_file_at(&directory, &final_name, Some(identity))?; + directory.sync_all()?; + Ok(()) + })(); + if result.is_err() { + unsafe { + libc::unlinkat(directory.as_raw_fd(), temporary_name.as_ptr(), 0); + } + } + result +} + +#[cfg(not(unix))] +pub(super) fn write_private_auto_reply_enrollment(root: &Path, bytes: &[u8]) -> Result<()> { + ensure_private_auto_reply_directory(root, "state root", false)?; + let path = root.join("enrollment.json"); + let tmp = root.join(format!(".enrollment.{}.tmp", std::process::id())); + fs::write(&tmp, bytes)?; + fs::rename(tmp, path)?; + Ok(()) +} + +pub(super) fn write_auto_reply_aggregate( + root: &Path, + targets: &[local_db::LocalChat], + children: &[Child], + state: &str, +) -> Result<()> { + let now = chrono::Utc::now(); + let now_unix = now.timestamp_millis() as f64 / 1_000.0; + let summaries = targets + .iter() + .zip(children.iter()) + .map(|(target, child)| { + let room_root = root.join("rooms").join(target.chat_id.to_string()); + let status_path = room_root.join("supervisor-status.json"); + let status = read_bounded_json_file(&status_path).ok(); + let status_target_matches = status.as_ref().is_some_and(|value| { + value.get("target_chat_id") == Some(&serde_json::json!(target.chat_id)) + && value.get("target_chat_name") + == Some(&serde_json::json!(target.chat_name)) + }); + let heartbeat_age_seconds = status + .as_ref() + .and_then(|value| value.get("updated_at")) + .and_then(serde_json::Value::as_f64) + .filter(|stamp| stamp.is_finite()) + .map(|stamp| now_unix - stamp); + let ready = state == "running" + && status_target_matches + && status.as_ref().is_some_and(|value| { + value.get("state") == Some(&serde_json::json!("running")) + && value.get("readiness") == Some(&serde_json::json!("ready")) + && value + .get("fence_reason") + .is_none_or(|reason| reason.as_str().is_some_and(str::is_empty)) + }) + && heartbeat_age_seconds + .is_some_and(|age| (-5.0..=15.0).contains(&age)); + serde_json::json!({ + "chat_id": target.chat_id, + "chat_name": target.chat_name, + "pid": child.id(), + "room_state_root": room_root, + "ready": ready, + "state": status.as_ref().and_then(|value| value.get("state")).cloned(), + "readiness": status.as_ref().and_then(|value| value.get("readiness")).cloned(), + "fence_reason": status.as_ref().and_then(|value| value.get("fence_reason")).cloned(), + "heartbeat_age_seconds": heartbeat_age_seconds, + "status_available": status.is_some(), + "target_identity_matches": status_target_matches, + }) + }) + .collect::>(); + let ready_count = summaries + .iter() + .filter(|summary| summary.get("ready") == Some(&serde_json::Value::Bool(true))) + .count(); + let all_ready = state == "running" && !summaries.is_empty() && ready_count == summaries.len(); + let payload = serde_json::json!({ + "schema_version": 2, + "state": state, + "readiness": if all_ready { "ready" } else if state == "running" { "starting" } else { "fenced" }, + "authoritative": all_ready, + "updated_at": now.to_rfc3339(), + "updated_at_unix": now_unix, + "room_count": summaries.len(), + "ready_room_count": ready_count, + "targets": summaries, + }); + let path = root.join("aggregate-status.json"); + let tmp = root.join(format!(".aggregate-status.{}.tmp", std::process::id())); + let mut options = fs::OpenOptions::new(); + options.create(true).truncate(true).write(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options + .mode(0o600) + .custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW); + } + let mut output = options + .open(&tmp) + .with_context(|| format!("open aggregate status temporary file {}", tmp.display()))?; + output.write_all(&serde_json::to_vec(&payload)?)?; + output.write_all(b"\n")?; + output.sync_all()?; + drop(output); + fs::rename(tmp, path)?; + fs::File::open(root)?.sync_all()?; + Ok(()) +} + +pub(super) fn prepare_auto_reply_room_state( + root: &Path, + chat: &local_db::LocalChat, +) -> Result { + #[cfg(unix)] + let _root_directory = open_private_auto_reply_directory(root, "state root", false)?; + #[cfg(not(unix))] + ensure_private_auto_reply_directory(root, "state root", false)?; + let rooms_root = root.join("rooms"); + #[cfg(unix)] + let _rooms_directory = open_private_auto_reply_directory(&rooms_root, "rooms directory", true)?; + #[cfg(not(unix))] + ensure_private_auto_reply_directory(&rooms_root, "rooms directory", true)?; + let room_root = root.join("rooms").join(chat.chat_id.to_string()); + #[cfg(unix)] + let _room_directory = open_private_auto_reply_directory(&room_root, "room state", true)?; + #[cfg(not(unix))] + ensure_private_auto_reply_directory(&room_root, "room state", true)?; + Ok(room_root) +} + +pub(super) fn configure_auto_reply_supervisor_shared_state(command: &mut Command, root: &Path) { + command + .env("OPENKAKAO_AUTO_REPLY_SEND_LOCK", root.join(".ax-send.lock")) + .env( + "OPENKAKAO_MODEL_CIRCUIT_DB", + root.join("model-circuit.sqlite3"), + ); +} + +pub(super) const AUTO_REPLY_READINESS_MAX_BYTES: u64 = 64 * 1024; + +pub(super) fn read_bounded_file(path: &Path) -> Result> { + let metadata = fs::symlink_metadata(path) + .with_context(|| format!("read readiness metadata: {}", path.display()))?; + if !metadata.file_type().is_file() || metadata.len() > AUTO_REPLY_READINESS_MAX_BYTES { + anyhow::bail!("invalid readiness file"); + } + let raw = fs::read(path).with_context(|| format!("read readiness file: {}", path.display()))?; + if raw.len() as u64 > AUTO_REPLY_READINESS_MAX_BYTES { + anyhow::bail!("readiness file exceeds bound"); + } + Ok(raw) +} + +pub(super) fn read_bounded_json_file(path: &Path) -> Result { + let raw = read_bounded_file(path)?; + serde_json::from_slice(&raw).context("malformed readiness JSON") +} diff --git a/src/commands/auto_reply/tests.rs b/src/commands/auto_reply/tests.rs new file mode 100644 index 00000000..90d2d083 --- /dev/null +++ b/src/commands/auto_reply/tests.rs @@ -0,0 +1,2428 @@ +use super::*; +use std::fs; +#[cfg(unix)] +use std::os::unix::io::AsRawFd; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::sync::atomic::Ordering; +use std::thread; +use std::time::Duration; + +use anyhow::{Context, Result}; +use sha2::{Digest, Sha256}; + +use super::bindings::{ + configure_auto_reply_supervisor_author_policy, resolve_auto_reply_author_bindings, + retry_bounded, AutoReplyAuthorBinding, AutoReplyBindingEvidence, AUTO_REPLY_ATTEST_ATTEMPTS, + AUTO_REPLY_ATTEST_RETRY_PAUSE, +}; +use super::enrollment::{ + enrollment_cursor_authority_for_target, enrollment_floor_for_target, + require_cli_enrolled_reply_author, validate_cli_enrollment_authority, + validate_cli_enrollment_digest, write_auto_reply_enrollment, AutoReplyCursorAuthority, + AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, AUTO_REPLY_CURSOR_FRESH_KIND, + AUTO_REPLY_CURSOR_LEFTOVER_KIND, AUTO_REPLY_CURSOR_REPLAY_KIND, + AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION, +}; +use super::llm::{ + probe_auto_reply_llm_with_local_transport, select_auto_reply_llm, AutoReplyLlmChoice, + LocalMlxProbeMethod, LocalMlxProbeRequest, LocalMlxProbeResponse, LocalMlxProbeTransport, + ReqwestLocalMlxProbeTransport, LOCAL_MLX_BASE_URL, LOCAL_MLX_CHAT_URL, + LOCAL_MLX_MAX_RESPONSE_BYTES, LOCAL_MLX_MODELS_URL, LOCAL_MLX_PROBE_TIMEOUT, +}; +use super::process::{ + configure_auto_reply_process_group, start_auto_reply_guardian_liveness_monitor, + validate_auto_reply_guardian_liveness_fd, AutoReplyChildrenGuard, + AUTO_REPLY_GUARDIAN_LIVENESS_ENV, AUTO_REPLY_GUARDIAN_LOST, AUTO_REPLY_STOP, +}; +use super::queue::{ + parse_auto_reply_transition_event_id, validate_stopped_clean_queue, + AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL, AUTO_REPLY_PIPELINE_TRANSITIONS_INDEX_SQL, + AUTO_REPLY_PIPELINE_TRANSITIONS_INSERT_TRIGGER_SQL, AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL, + AUTO_REPLY_PIPELINE_TRANSITIONS_UPDATE_TRIGGER_SQL, AUTO_REPLY_QUEUE_JOURNAL_MAX_ROWS, + AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION, AUTO_REPLY_QUEUE_LEGACY_USER_VERSION, + AUTO_REPLY_REPLY_JOBS_STATUS_INDEX_SQL, AUTO_REPLY_REPLY_JOBS_V2_TABLE_SQL, +}; +use super::runner::{ + is_homebrew_cellar_version_path, is_homebrew_opt_python_keg_path, + validate_auto_reply_codex_auth, validate_auto_reply_executable, + AUTO_REPLY_PYTHON_ISOLATION_ARGS, +}; +use super::state_root::{ + acquire_auto_reply_owner_lock, auto_reply_legacy_conflict, + configure_auto_reply_supervisor_shared_state, prepare_auto_reply_room_state, + read_bounded_json_file, write_auto_reply_aggregate, write_private_auto_reply_enrollment, +}; +use super::worker_gate::{ + finish_worker_bound_local_send_setup, require_auto_reply_worker_preflight, + require_expected_local_source_tail, resolve_bound_source_message, +}; +use crate::{config, local_db}; + +struct FakeLocalMlxProbeTransport { + responses: std::cell::RefCell>, + requests: std::cell::RefCell>, +} + +impl FakeLocalMlxProbeTransport { + fn new(responses: Vec) -> Self { + Self { + responses: std::cell::RefCell::new(responses.into()), + requests: std::cell::RefCell::new(Vec::new()), + } + } + + fn json(status: u16, value: serde_json::Value) -> LocalMlxProbeResponse { + LocalMlxProbeResponse { + status, + body: serde_json::to_vec(&value).expect("serialize fake local MLX response"), + } + } +} + +impl LocalMlxProbeTransport for FakeLocalMlxProbeTransport { + fn send(&self, request: LocalMlxProbeRequest) -> Result { + self.requests.borrow_mut().push(request); + self.responses + .borrow_mut() + .pop_front() + .context("missing fake local MLX response") + } +} + +#[test] +fn retry_bounded_stops_at_the_first_success() { + let mut calls = 0; + let value = retry_bounded(3, std::time::Duration::ZERO, || { + calls += 1; + if calls < 3 { + anyhow::bail!("transient failure {calls}"); + } + Ok(calls) + }) + .expect("third attempt should succeed"); + assert_eq!(value, 3); + assert_eq!(calls, 3, "retry must stop once an attempt succeeds"); +} + +#[test] +fn retry_bounded_surfaces_the_last_error_after_the_budget() { + let mut calls = 0; + let error = retry_bounded::<()>(3, std::time::Duration::ZERO, || { + calls += 1; + anyhow::bail!("failure {calls}") + }) + .expect_err("an exhausted budget must fail"); + assert_eq!(calls, 3, "retry must spend the whole budget"); + assert!( + error.to_string().contains("failure 3"), + "the last error must be surfaced, got {error}" + ); +} + +#[test] +fn retry_bounded_always_attempts_once_and_never_exceeds_the_budget() { + let mut zero_budget_calls = 0; + retry_bounded::<()>(0, std::time::Duration::ZERO, || { + zero_budget_calls += 1; + Ok(()) + }) + .expect("a zero budget still attempts once"); + assert_eq!(zero_budget_calls, 1); + + let mut first_success_calls = 0; + retry_bounded(3, std::time::Duration::ZERO, || { + first_success_calls += 1; + Ok(7) + }) + .expect("the first attempt succeeds"); + assert_eq!(first_success_calls, 1, "a success must not retry"); +} + +#[test] +fn auto_reply_attestation_retry_budget_is_bounded() { + assert!((2..=5).contains(&AUTO_REPLY_ATTEST_ATTEMPTS)); + assert!(AUTO_REPLY_ATTEST_RETRY_PAUSE <= std::time::Duration::from_secs(2)); +} + +#[cfg(unix)] +use crate::auto_reply_runtime::acquire_worker_setup_lock_nonblocking; +use crate::commands::context_sync_local::tests::local_source_fence; + +#[cfg(unix)] +#[test] +fn codex_auth_rejects_symlink_and_hardlink_credentials() { + use std::os::unix::fs::{symlink, MetadataExt, PermissionsExt}; + + let root = tempfile::tempdir().expect("temporary Codex home"); + let source = root.path().join("auth-source.json"); + fs::write(&source, b"{}").expect("write auth fixture"); + fs::set_permissions(&source, fs::Permissions::from_mode(0o600)) + .expect("make auth fixture private"); + let uid = fs::symlink_metadata(&source).expect("auth metadata").uid(); + validate_auto_reply_codex_auth(&source, uid) + .expect("a private single-link regular credential must pass"); + + let linked = root.path().join("auth-symlink.json"); + symlink(&source, &linked).expect("create symlink fixture"); + assert!(validate_auto_reply_codex_auth(&linked, uid).is_err()); + + let hardlinked = root.path().join("auth-hardlink.json"); + fs::hard_link(&source, &hardlinked).expect("create hardlink fixture"); + assert!(validate_auto_reply_codex_auth(&source, uid).is_err()); + assert!(validate_auto_reply_codex_auth(&hardlinked, uid).is_err()); + fs::remove_file(&hardlinked).expect("remove hardlink"); + fs::write(&source, b"").expect("empty the auth fixture"); + assert!(validate_auto_reply_codex_auth(&source, uid).is_err()); +} + +#[test] +fn final_local_source_fence_allows_later_rows_after_scheduled_source() { + let watcher_last_observed = 100; + let current = local_source_fence(42, watcher_last_observed, watcher_last_observed, &[]); + require_expected_local_source_tail(42, watcher_last_observed, ¤t) + .expect("an exact local source tail should remain eligible"); + + // A later inbound is its own job. It must not cancel the earlier + // scheduled send once the source row itself is still present. + let source_db_advanced = local_source_fence(42, watcher_last_observed, 101, &[101]); + require_expected_local_source_tail(42, watcher_last_observed, &source_db_advanced) + .expect("later room rows must not fence an earlier unanswered inbound"); + + let gap = local_source_fence(42, watcher_last_observed, 101, &[101]); + let mut gap = gap; + gap.completeness.has_gap = true; + let error = require_expected_local_source_tail(42, watcher_last_observed, &gap) + .expect_err("a gapped poll after the source remains fail-closed"); + assert!(error + .to_string() + .contains("scheduled reply source tail is unavailable")); +} + +#[test] +fn proactive_local_source_fence_accepts_exact_self_tail() { + let tail = 3908781794201088001i64; + let current = local_source_fence(42, tail, tail, &[]); + require_expected_local_source_tail(42, tail, ¤t) + .expect("a quiet self tail remains an exact empty after-cursor"); + let advanced = local_source_fence(42, tail, tail + 1, &[tail + 1]); + require_expected_local_source_tail(42, tail, &advanced) + .expect("a newer tail after a quiet self row still keeps the source eligible"); +} + +#[test] +fn auto_reply_supervisor_python_is_environment_and_site_isolated() { + assert_eq!(AUTO_REPLY_PYTHON_ISOLATION_ARGS, ["-E", "-B", "-S"]); +} + +#[test] +fn auto_reply_llm_aliases_map_to_attested_models() { + for exact in [ + config::MLX_FLASH_NEXT_MODEL_ID, + config::MLX_FLASH_NEXT_PREFIXED_MODEL_ID, + ] { + assert_eq!( + AutoReplyLlmChoice::from_model(exact), + Some(AutoReplyLlmChoice::MlxQwen38FlashNext) + ); + } + assert_eq!( + AutoReplyLlmChoice::from_model(&format!(" {} ", config::MLX_FLASH_NEXT_MODEL_ID)), + None, + "the local model allowlist must remain exact" + ); + for exact in [config::MLX_27B_MODEL_ID, config::MLX_27B_PREFIXED_MODEL_ID] { + assert_eq!( + AutoReplyLlmChoice::from_model(exact), + Some(AutoReplyLlmChoice::MlxQwen38TwentySevenB) + ); + } + let mut json_config = config::OpenKakaoConfig::default(); + json_config.auto_reply.reply_model = Some(config::MLX_FLASH_NEXT_PREFIXED_MODEL_ID.into()); + assert_eq!( + select_auto_reply_llm(&mut json_config, None, true) + .expect("JSON mode should select the configured exact local model"), + AutoReplyLlmChoice::MlxQwen38FlashNext + ); + assert_eq!( + AutoReplyLlmChoice::from_model("gemini-3.7-flash") + .expect("gemini alias") + .model(), + "google-antigravity/gemini-3.7-flash-tiered" + ); + assert_eq!( + AutoReplyLlmChoice::from_model("gpt-5.6-luna") + .expect("luna alias") + .model(), + "gpt-5.6-luna" + ); + assert_eq!( + AutoReplyLlmChoice::from_model("deepseek-v4.1-flash") + .expect("opencode deepseek alias") + .model(), + "opencode-go-session/deepseek-v4.1-flash" + ); + let mut config = config::OpenKakaoConfig::default(); + AutoReplyLlmChoice::from_model("deepseek-v4.1-flash") + .expect("opencode deepseek alias") + .apply(&mut config); + assert_eq!( + config.model.provider.as_deref(), + Some("opencode-go-session") + ); + assert_eq!(config.auto_reply.reply_runner_kind.as_deref(), Some("gjc")); + assert_eq!(AutoReplyLlmChoice::all().len(), 5); +} + +#[test] +fn auto_reply_local_mlx_choice_applies_fail_closed_profile() { + let mut config = config::OpenKakaoConfig::default(); + config.model.privacy_mode = Some("remote_explicit".into()); + config.model.allow_egress = true; + config.model.provider = Some("openai-codex".into()); + config.model.retention = Some("provider-policy".into()); + + AutoReplyLlmChoice::MlxQwen38FlashNext.apply(&mut config); + + assert_eq!(config.model.privacy_mode.as_deref(), Some("local")); + assert!(!config.model.allow_egress); + assert_eq!( + config.model.provider.as_deref(), + Some(config::MLX_SERVE_PROVIDER) + ); + assert!(config.model.retention.is_none()); + assert_eq!( + config.auto_reply.reply_model.as_deref(), + Some(config::MLX_FLASH_NEXT_MODEL_ID) + ); + assert_eq!( + config.auto_reply.reply_runner_kind.as_deref(), + Some("opencodex") + ); + assert_eq!( + config.auto_reply.reply_runner.as_deref(), + Some("/opt/homebrew/bin/opencodex") + ); + config::validate_auto_reply_local_mlx_profile(&config) + .expect("applied local MLX profile must validate"); +} + +#[test] +fn auto_reply_local_mlx_probe_uses_only_bounded_loopback_requests() { + let mut config = config::OpenKakaoConfig::default(); + AutoReplyLlmChoice::MlxQwen38FlashNext.apply(&mut config); + let transport = FakeLocalMlxProbeTransport::new(vec![ + FakeLocalMlxProbeTransport::json( + 200, + serde_json::json!({ + "data": [{ + "id": config::MLX_FLASH_NEXT_PREFIXED_MODEL_ID, + "loaded": true, + "state": "ready" + }] + }), + ), + FakeLocalMlxProbeTransport::json( + 200, + serde_json::json!({"choices": [{"message": {"content": "확인"}}]}), + ), + ]); + + probe_auto_reply_llm_with_local_transport( + &config, + AutoReplyLlmChoice::MlxQwen38FlashNext, + &transport, + ) + .expect("ready local MLX model should pass the bounded probe"); + + let requests = transport.requests.borrow(); + assert_eq!(requests.len(), 2); + assert_eq!(requests[0].method, LocalMlxProbeMethod::Get); + assert_eq!(requests[0].url, LOCAL_MLX_MODELS_URL); + assert!(requests[0].body.is_none()); + assert_eq!(requests[1].method, LocalMlxProbeMethod::Post); + assert_eq!(requests[1].url, LOCAL_MLX_CHAT_URL); + assert!(requests.iter().all(|request| { + request.timeout == LOCAL_MLX_PROBE_TIMEOUT + && request.max_response_bytes == LOCAL_MLX_MAX_RESPONSE_BYTES + && request.url.starts_with(LOCAL_MLX_BASE_URL) + && !request.url.contains("10100") + && !request.url.contains("/load") + && !request.url.contains("/unload") + && !request.url.contains("/swap") + })); + let generation: serde_json::Value = + serde_json::from_slice(requests[1].body.as_deref().expect("generation probe body")) + .expect("parse generation probe body"); + assert_eq!( + generation.get("model").and_then(serde_json::Value::as_str), + Some(config::MLX_FLASH_NEXT_MODEL_ID) + ); + assert_eq!( + generation + .get("max_tokens") + .and_then(serde_json::Value::as_u64), + Some(8) + ); + assert_eq!( + generation + .pointer("/messages/0/content") + .and_then(serde_json::Value::as_str), + Some("확인이라고만 답하세요.") + ); +} + +#[test] +fn auto_reply_local_mlx_probe_fails_closed_without_generation_or_remote_fallback() { + let mut config = config::OpenKakaoConfig::default(); + AutoReplyLlmChoice::MlxQwen38FlashNext.apply(&mut config); + let not_ready = FakeLocalMlxProbeTransport::new(vec![FakeLocalMlxProbeTransport::json( + 200, + serde_json::json!({ + "data": [{ + "id": config::MLX_FLASH_NEXT_MODEL_ID, + "loaded": false, + "state": "unloaded" + }] + }), + )]); + let error = probe_auto_reply_llm_with_local_transport( + &config, + AutoReplyLlmChoice::MlxQwen38FlashNext, + ¬_ready, + ) + .expect_err("an unloaded model must fail before generation"); + assert_eq!(error.to_string(), "local MLX model is not ready"); + assert_eq!(not_ready.requests.borrow().len(), 1); + + let malformed = FakeLocalMlxProbeTransport::new(vec![LocalMlxProbeResponse { + status: 200, + body: b"{not-json".to_vec(), + }]); + assert!(probe_auto_reply_llm_with_local_transport( + &config, + AutoReplyLlmChoice::MlxQwen38FlashNext, + &malformed, + ) + .is_err()); + assert_eq!(malformed.requests.borrow().len(), 1); + + let redirected = FakeLocalMlxProbeTransport::new(vec![FakeLocalMlxProbeTransport::json( + 302, + serde_json::json!({"redirect": true}), + )]); + let redirect_error = probe_auto_reply_llm_with_local_transport( + &config, + AutoReplyLlmChoice::MlxQwen38FlashNext, + &redirected, + ) + .expect_err("redirects must fail closed"); + assert!(redirect_error.to_string().contains("HTTP 302")); + assert_eq!(redirected.requests.borrow().len(), 1); + + config.auto_reply.reply_model = Some("google-antigravity/gemini-3.8-flash".into()); + let invalid_profile = FakeLocalMlxProbeTransport::new(Vec::new()); + assert!(probe_auto_reply_llm_with_local_transport( + &config, + AutoReplyLlmChoice::MlxQwen38FlashNext, + &invalid_profile, + ) + .is_err()); + assert!(invalid_profile.requests.borrow().is_empty()); +} + +#[test] +fn auto_reply_local_mlx_transport_rejects_non_fixed_endpoints_before_network() { + for url in [ + "http://127.0.0.1:10100/v1/models", + "https://models.example.invalid/v1/models", + ] { + let error = match ReqwestLocalMlxProbeTransport.send(LocalMlxProbeRequest { + method: LocalMlxProbeMethod::Get, + url, + body: None, + timeout: LOCAL_MLX_PROBE_TIMEOUT, + max_response_bytes: LOCAL_MLX_MAX_RESPONSE_BYTES, + }) { + Ok(_) => panic!("non-fixed endpoint must be rejected before transport"), + Err(error) => error, + }; + assert!(error.to_string().contains("non-loopback endpoint")); + } +} + +#[test] +fn beginner_plain_name_uses_configured_bind_selector() { + let expanded = expand_plain_chat_names_with_configured_bindings( + vec!["부자멘토멘티".into()], + &["bind:417780809780519:부자멘토멘티".into()], + ); + assert_eq!(expanded, vec!["bind:417780809780519:부자멘토멘티"]); + let untouched = expand_plain_chat_names_with_configured_bindings( + vec!["name:다른방".into()], + &["bind:417780809780519:부자멘토멘티".into()], + ); + assert_eq!(untouched, vec!["name:다른방"]); +} + +fn enrollment_fixture(room_root: &Path, identity: serde_json::Value) -> serde_json::Value { + serde_json::json!({ + "schema_version": AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION, + "activation": "foreground", + "selectors": ["bind:42:부자멘토멘티"], + "runtime_root": room_root.parent().and_then(Path::parent).unwrap(), + "created_at": "2026-08-12T00:00:00Z", + "targets": [{ + "chat_id": 42, + "chat_name": "부자멘토멘티", + "last_log_id": 100, + "room_state_root": room_root, + "identity": identity, + "cursor_authority": { + "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, + "kind": AUTO_REPLY_CURSOR_FRESH_KIND, + "cursor_floor": 100, + "attested_db_last_log_id": 100, + "prior_owner_id": null, + "prior_source_epoch": null, + }, + "reply_author_bindings": [{"nickname": "현준", "author_id": 700}], + }], + }) +} + +#[test] +fn cli_enrollment_v4_local_name_authority_matches_final_send_gate() { + let root = tempfile::tempdir().expect("temporary state root"); + let room_root = root.path().join("rooms/42"); + fs::create_dir_all(&room_root).expect("create room root"); + let enrollment = enrollment_fixture( + &room_root, + serde_json::json!({ + "schema_version": 1, + "kind": "local_name", + "local_name": "부자멘토멘티", + "ax_name": "부자멘토멘티", + }), + ); + validate_cli_enrollment_authority(&enrollment, 42, "부자멘토멘티", 100, &room_root) + .expect("v4 local-name enrollment should pass the final gate"); + + let raw = serde_json::to_vec(&enrollment).expect("serialize enrollment"); + let digest = hex::encode(Sha256::digest(&raw)); + validate_cli_enrollment_digest(&raw, &digest).expect("matching digest should pass"); + assert!(validate_cli_enrollment_digest(&raw, &"A".repeat(64)).is_err()); + assert!(validate_cli_enrollment_digest(&raw, &"0".repeat(64)).is_err()); +} + +#[test] +fn cli_enrollment_v4_transcript_and_replay_authority_are_strict() { + let root = tempfile::tempdir().expect("temporary state root"); + let room_root = root.path().join("rooms/42"); + fs::create_dir_all(&room_root).expect("create room root"); + let identity = serde_json::json!({ + "schema_version": 1, + "kind": "ax_transcript", + "local_name": "", + "ax_name": "부자멘토멘티", + "matched_log_ids": [98, 99, 100], + "matched_count": 3, + "matched_utf8_bytes": 24, + "transcript_sha256": "a".repeat(64), + "attested_db_last_log_id": 100, + }); + let enrollment = enrollment_fixture(&room_root, identity.clone()); + validate_cli_enrollment_authority(&enrollment, 42, "부자멘토멘티", 100, &room_root) + .expect("strict transcript enrollment should pass the final gate"); + + let two_row = enrollment_fixture( + &room_root, + serde_json::json!({ + "schema_version": 1, + "kind": "ax_transcript", + "local_name": "", + "ax_name": "부자멘토멘티", + "matched_log_ids": [99, 100], + "matched_count": 2, + "matched_utf8_bytes": 19, + "transcript_sha256": "a".repeat(64), + "attested_db_last_log_id": 100, + }), + ); + validate_cli_enrollment_authority(&two_row, 42, "부자멘토멘티", 100, &room_root) + .expect("two-row distinct suffix enrollment must match is_strong bind"); + let one_row = enrollment_fixture( + &room_root, + serde_json::json!({ + "schema_version": 1, + "kind": "ax_transcript", + "local_name": "", + "ax_name": "부자멘토멘티", + "matched_log_ids": [100], + "matched_count": 1, + "matched_utf8_bytes": 19, + "transcript_sha256": "a".repeat(64), + "attested_db_last_log_id": 100, + }), + ); + assert!( + validate_cli_enrollment_authority(&one_row, 42, "부자멘토멘티", 100, &room_root).is_err() + ); + + let mut malformed = enrollment_fixture(&room_root, identity); + malformed["targets"][0]["identity"]["matched_count"] = serde_json::Value::from(2); + assert!( + validate_cli_enrollment_authority(&malformed, 42, "부자멘토멘티", 100, &room_root,) + .is_err() + ); + malformed["targets"][0]["identity"]["matched_count"] = serde_json::Value::from(3); + malformed["schema_version"] = serde_json::Value::from(3); + assert!( + validate_cli_enrollment_authority(&malformed, 42, "부자멘토멘티", 100, &room_root,) + .is_err() + ); + + let mut replay_floor = enrollment_fixture( + &room_root, + serde_json::json!({ + "schema_version": 1, + "kind": "ax_transcript", + "local_name": "", + "ax_name": "부자멘토멘티", + "matched_log_ids": [10, 11, 12], + "matched_count": 3, + "matched_utf8_bytes": 24, + "transcript_sha256": "a".repeat(64), + "attested_db_last_log_id": 101, + }), + ); + replay_floor["targets"][0]["last_log_id"] = serde_json::Value::from(100); + replay_floor["targets"][0]["cursor_authority"] = serde_json::json!({ + "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, + "kind": AUTO_REPLY_CURSOR_REPLAY_KIND, + "cursor_floor": 100, + "attested_db_last_log_id": 101, + "prior_owner_id": "prior-owner", + "prior_source_epoch": 7, + }); + validate_cli_enrollment_authority(&replay_floor, 42, "부자멘토멘티", 100, &room_root) + .expect("an ACK floor before the fresh attested tail must replay safely"); + + let mut missing_replay_contract = replay_floor.clone(); + missing_replay_contract["targets"][0]["cursor_authority"] = serde_json::json!({ + "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, + "kind": AUTO_REPLY_CURSOR_FRESH_KIND, + "cursor_floor": 100, + "attested_db_last_log_id": 101, + "prior_owner_id": null, + "prior_source_epoch": null, + }); + assert!(validate_cli_enrollment_authority( + &missing_replay_contract, + 42, + "부자멘토멘티", + 100, + &room_root, + ) + .is_err()); + + let mut old_runtime_schema = replay_floor.clone(); + old_runtime_schema["schema_version"] = serde_json::Value::from(3); + assert!(validate_cli_enrollment_authority( + &old_runtime_schema, + 42, + "부자멘토멘티", + 100, + &room_root, + ) + .is_err()); + + replay_floor["targets"][0]["identity"]["attested_db_last_log_id"] = serde_json::Value::from(11); + assert!( + validate_cli_enrollment_authority(&replay_floor, 42, "부자멘토멘티", 100, &room_root,) + .is_err() + ); +} + +#[test] +fn numeric_reply_author_binding_fails_closed_on_collision_self_and_drift() { + let configured = std::collections::BTreeSet::from(["문승현".to_string(), "현준".to_string()]); + let identities = vec![ + local_db::LocalAuthorIdentity { + author_id: 700, + nickname: "문승현".to_string(), + is_self: false, + }, + local_db::LocalAuthorIdentity { + author_id: 701, + nickname: "현준".to_string(), + is_self: false, + }, + ]; + let bindings = + resolve_auto_reply_author_bindings("부자멘토멘티", 999, &identities, &configured) + .expect("unique non-self identities should enroll"); + assert_eq!( + bindings, + vec![ + AutoReplyAuthorBinding { + nickname: "문승현".to_string(), + author_id: 700, + }, + AutoReplyAuthorBinding { + nickname: "현준".to_string(), + author_id: 701, + }, + ] + ); + + let collision = [ + identities.clone(), + vec![local_db::LocalAuthorIdentity { + author_id: 702, + nickname: "현준".to_string(), + is_self: false, + }], + ] + .concat(); + assert!( + resolve_auto_reply_author_bindings("부자멘토멘티", 999, &collision, &configured,).is_err() + ); + + let ambiguous_name_rows = vec![ + local_db::LocalAuthorIdentity { + author_id: 701, + nickname: "현준".to_string(), + is_self: false, + }, + local_db::LocalAuthorIdentity { + author_id: 701, + nickname: "현준(다른표시명)".to_string(), + is_self: false, + }, + ]; + assert!(resolve_auto_reply_author_bindings( + "부자멘토멘티", + 999, + &ambiguous_name_rows, + &std::collections::BTreeSet::from(["현준".to_string()]), + ) + .is_err()); + + let self_bound = vec![local_db::LocalAuthorIdentity { + author_id: 999, + nickname: "현준".to_string(), + is_self: true, + }]; + assert!(resolve_auto_reply_author_bindings( + "부자멘토멘티", + 999, + &self_bound, + &std::collections::BTreeSet::from(["현준".to_string()]), + ) + .is_err()); + + let inconsistent_self_proof = vec![local_db::LocalAuthorIdentity { + author_id: 701, + nickname: "현준".to_string(), + is_self: true, + }]; + assert!(resolve_auto_reply_author_bindings( + "부자멘토멘티", + 999, + &inconsistent_self_proof, + &std::collections::BTreeSet::from(["현준".to_string()]), + ) + .is_err()); +} + +#[test] +fn cli_enrollment_v4_reply_author_binding_is_strict() { + let root = tempfile::tempdir().expect("temporary state root"); + let room_root = root.path().join("rooms/42"); + fs::create_dir_all(&room_root).expect("create room root"); + let identity = serde_json::json!({ + "schema_version": 1, + "kind": "local_name", + "local_name": "부자멘토멘티", + "ax_name": "부자멘토멘티", + }); + let enrollment = enrollment_fixture(&room_root, identity); + require_cli_enrolled_reply_author(&enrollment, 42, 700, "현준") + .expect("matching numeric identity should pass"); + assert!(require_cli_enrolled_reply_author(&enrollment, 42, 701, "현준").is_err()); + assert!(require_cli_enrolled_reply_author(&enrollment, 42, 700, "동명이인").is_err()); + + let mut duplicate_id = enrollment.clone(); + duplicate_id["targets"][0]["reply_author_bindings"] = serde_json::json!([ + {"nickname": "문승현", "author_id": 700}, + {"nickname": "현준", "author_id": 700} + ]); + assert!( + validate_cli_enrollment_authority(&duplicate_id, 42, "부자멘토멘티", 100, &room_root,) + .is_err() + ); +} + +#[test] +fn multi_room_enrollment_keeps_numeric_author_bindings_disjoint() { + let root = tempfile::tempdir().expect("temporary state root"); + let root_path = root.path().canonicalize().expect("canonical state root"); + let targets = [ + local_db::LocalChat { + chat_id: 42, + chat_type: 0, + chat_name: "room-a".to_string(), + database_chat_name: Some("room-a".to_string()), + active_members_count: 2, + last_log_id: 100, + last_updated_at: 0, + unread_count: 0, + display_name: "room-a".to_string(), + }, + local_db::LocalChat { + chat_id: 84, + chat_type: 0, + chat_name: "room-b".to_string(), + database_chat_name: Some("room-b".to_string()), + active_members_count: 2, + last_log_id: 200, + last_updated_at: 0, + unread_count: 0, + display_name: "room-b".to_string(), + }, + ]; + for target in &targets { + fs::create_dir_all(root_path.join("rooms").join(target.chat_id.to_string())) + .expect("create independent room root"); + } + let author_bindings = std::collections::BTreeMap::from([ + ( + 42, + vec![AutoReplyAuthorBinding { + nickname: "alice".to_string(), + author_id: 700, + }], + ), + ( + 84, + vec![AutoReplyAuthorBinding { + nickname: "bob".to_string(), + author_id: 800, + }], + ), + ]); + let (floors, _) = write_auto_reply_enrollment( + &root_path, + &["id:42".to_string(), "id:84".to_string()], + &targets, + &[], + &author_bindings, + &root_path.join("runtime"), + ) + .expect("write independent multi-room enrollment"); + assert_eq!(floors, [100, 200]); + let enrollment = read_bounded_json_file(&root_path.join("enrollment.json")) + .expect("read multi-room enrollment"); + assert_eq!( + enrollment["targets"][0]["room_state_root"], + serde_json::json!(root_path.join("rooms/42")) + ); + assert_eq!( + enrollment["targets"][1]["room_state_root"], + serde_json::json!(root_path.join("rooms/84")) + ); + assert_eq!( + enrollment["targets"][0]["reply_author_bindings"], + serde_json::json!([{"nickname": "alice", "author_id": 700}]) + ); + assert_eq!( + enrollment["targets"][1]["reply_author_bindings"], + serde_json::json!([{"nickname": "bob", "author_id": 800}]) + ); + assert_ne!( + enrollment["targets"][0]["reply_author_bindings"], + enrollment["targets"][1]["reply_author_bindings"] + ); +} + +#[test] +fn multi_room_supervisor_commands_keep_author_policy_disjoint() { + fn env_value(command: &Command, key: &str) -> String { + command + .get_envs() + .find(|(candidate, _)| *candidate == std::ffi::OsStr::new(key)) + .and_then(|(_, value)| value) + .expect("expected supervisor environment entry") + .to_string_lossy() + .into_owned() + } + fn args(command: &Command) -> Vec { + command + .get_args() + .map(|value| value.to_string_lossy().into_owned()) + .collect() + } + + let root = tempfile::tempdir().expect("temporary shared state root"); + let mut room_a = Command::new("supervisor"); + configure_auto_reply_supervisor_shared_state(&mut room_a, root.path()); + configure_auto_reply_supervisor_author_policy( + &mut room_a, + &["alice".to_string()], + r#"[{"nickname":"alice","author_id":700}]"#, + ); + let mut room_b = Command::new("supervisor"); + configure_auto_reply_supervisor_shared_state(&mut room_b, root.path()); + configure_auto_reply_supervisor_author_policy( + &mut room_b, + &["bob".to_string()], + r#"[{"nickname":"bob","author_id":800}]"#, + ); + + assert_eq!(args(&room_a), ["--reply-author", "alice"]); + assert_eq!(args(&room_b), ["--reply-author", "bob"]); + assert_eq!(env_value(&room_a, "OPENKAKAO_REPLY_AUTHORS"), "alice"); + assert_eq!(env_value(&room_b, "OPENKAKAO_REPLY_AUTHORS"), "bob"); + let expected_circuit = root.path().join("model-circuit.sqlite3"); + assert_eq!( + env_value(&room_a, "OPENKAKAO_MODEL_CIRCUIT_DB"), + expected_circuit.to_string_lossy() + ); + assert_eq!( + env_value(&room_b, "OPENKAKAO_MODEL_CIRCUIT_DB"), + expected_circuit.to_string_lossy() + ); + assert_eq!( + env_value(&room_a, "OPENKAKAO_REPLY_AUTHOR_BINDINGS"), + r#"[{"nickname":"alice","author_id":700}]"# + ); + assert_eq!( + env_value(&room_b, "OPENKAKAO_REPLY_AUTHOR_BINDINGS"), + r#"[{"nickname":"bob","author_id":800}]"# + ); + assert!(!args(&room_a).iter().any(|value| value.contains("bob"))); + assert!(!args(&room_b).iter().any(|value| value.contains("alice"))); +} + +#[test] +fn auto_reply_enrollment_floor_seeds_fresh_install_and_rejects_dirty_state() { + let root = tempfile::tempdir().expect("temporary state root"); + let root_path = root.path().canonicalize().expect("canonical state root"); + let room_root = root_path.join("rooms/42"); + fs::create_dir_all(&room_root).expect("create room root"); + let target = local_db::LocalChat { + chat_id: 42, + chat_type: 0, + chat_name: "부자멘토멘티".to_string(), + database_chat_name: Some("부자멘토멘티".to_string()), + active_members_count: 4, + last_log_id: 120, + last_updated_at: 0, + unread_count: 0, + display_name: "부자멘토멘티".to_string(), + }; + let state_path = room_root.join("db-watch-state.json"); + let clean_state = |watermark: i64| { + serde_json::json!({ + "schema_version": 3, + "target_chat_id": 42, + "target_chat_name": "부자멘토멘티", + "cursor_floor": 50, + "acked_watermark": watermark, + "last_observed_log_id": watermark, + "observed_log_ids": [watermark], + "acked_log_ids": [watermark], + "pending_log_ids": [], + "pending_gaps": [], + "candidate_phase": "idle", + "in_flight_candidate": null, + "capability_state": "ready", + "delivery_enabled": true, + "fence": "ready", + "fence_reason": "", + "owner_id": "terminal-owner", + "source_epoch": 7, + }) + }; + + assert_eq!( + enrollment_floor_for_target(&root_path, &target, 120) + .expect("a fresh install must seed at the attested DB tail"), + 120 + ); + let binding = AutoReplyBindingEvidence { + chat_id: 42, + ax_name: "부자멘토멘티".to_string(), + local_name: String::new(), + matched_log_ids: vec![78, 79, 80], + matched_count: 3, + matched_utf8_bytes: 24, + transcript_sha256: "a".repeat(64), + attested_db_last_log_id: 120, + }; + let (written_floors, _) = write_auto_reply_enrollment( + &root_path, + &["bind:42:부자멘토멘티".to_string()], + std::slice::from_ref(&target), + &[binding], + &std::collections::BTreeMap::from([( + 42, + vec![AutoReplyAuthorBinding { + nickname: "현준".to_string(), + author_id: 700, + }], + )]), + &root_path.join("runtime"), + ) + .expect("write enrollment from the fresh binding attestation"); + assert_eq!(written_floors, vec![120]); + let written_enrollment = read_bounded_json_file(&root_path.join("enrollment.json")) + .expect("read written enrollment"); + assert_eq!( + written_enrollment["targets"][0]["last_log_id"], + serde_json::Value::from(120) + ); + assert_eq!( + written_enrollment["targets"][0]["identity"]["attested_db_last_log_id"], + serde_json::Value::from(120) + ); + assert_eq!( + written_enrollment["schema_version"], + serde_json::Value::from(AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION) + ); + assert_eq!( + written_enrollment["targets"][0]["cursor_authority"], + serde_json::json!({ + "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, + "kind": AUTO_REPLY_CURSOR_FRESH_KIND, + "cursor_floor": 120, + "attested_db_last_log_id": 120, + "prior_owner_id": null, + "prior_source_epoch": null, + }) + ); + + fs::write( + &state_path, + serde_json::to_vec(&clean_state(130)).expect("serialize running state"), + ) + .expect("write running state"); + assert!(enrollment_floor_for_target(&root_path, &target, 140).is_err()); + + let mut unresolved = clean_state(130); + unresolved["pending_gaps"] = serde_json::json!(["reconcile_required"]); + fs::write( + &state_path, + serde_json::to_vec(&unresolved).expect("serialize unresolved state"), + ) + .expect("rewrite unresolved state"); + assert!(enrollment_floor_for_target(&root_path, &target, 140).is_err()); +} + +fn stopped_clean_queue_fixture( + path: &Path, + include_supersessions: bool, + breaker_schema: &str, + extra_schema: &str, + job_status: &str, +) { + let supersessions = if include_supersessions { + "CREATE TABLE reply_job_supersessions( + event_id TEXT PRIMARY KEY, + superseded_by_event_id TEXT NOT NULL, + created_at REAL NOT NULL, + CHECK(event_id <> superseded_by_event_id) + );" + } else { + "" + }; + let sql = format!( + " + CREATE TABLE reply_jobs( + event_id TEXT PRIMARY KEY, event_json TEXT NOT NULL, + status TEXT NOT NULL, due_at REAL, decision TEXT, + reason TEXT, category TEXT, reply TEXT, + scheduled_delay_seconds REAL, error_class TEXT, + created_at REAL NOT NULL, updated_at REAL NOT NULL + ); + CREATE INDEX idx_reply_jobs_status_due + ON reply_jobs(status, due_at); + CREATE TABLE reply_job_tombstones( + event_id TEXT PRIMARY KEY, status TEXT NOT NULL, + archived_at REAL NOT NULL + ); + {supersessions} + {breaker_schema} + {extra_schema} + INSERT INTO reply_jobs VALUES( + 'db:42:1', '{{\"chat_id\":42,\"log_id\":1}}', '{job_status}', NULL, 'skip', 'test', + 'test', NULL, NULL, NULL, 1.0, 1.0 + ); + INSERT INTO reply_job_tombstones VALUES('db:42:2', 'sent', 1.0); + " + ); + let connection = rusqlite::Connection::open(path).expect("create queue fixture"); + connection + .execute_batch(&sql) + .expect("initialize queue fixture"); + drop(connection); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(path, fs::Permissions::from_mode(0o600)).unwrap(); + } +} + +const EXACT_BREAKER_SCHEMA: &str = " + CREATE TABLE model_circuit_breaker( + model_key TEXT PRIMARY KEY, + state TEXT NOT NULL, + failure_class TEXT NOT NULL, + consecutive_failures INTEGER NOT NULL, + open_until REAL NOT NULL, + lease_token TEXT, + updated_at REAL NOT NULL + );"; + +fn stopped_clean_queue_test_room(root: &Path) -> PathBuf { + let room = root.join("42"); + fs::create_dir_all(&room).expect("create stopped-clean test room"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&room, fs::Permissions::from_mode(0o700)) + .expect("make stopped-clean test room private"); + } + room +} + +fn stopped_clean_queue_v2_fixture_with_table( + path: &Path, + include_breaker: bool, + journal_table_sql: &str, +) { + stopped_clean_queue_fixture( + path, + true, + if include_breaker { + EXACT_BREAKER_SCHEMA + } else { + "" + }, + "", + "sent", + ); + let connection = rusqlite::Connection::open(path).expect("open v2 queue fixture"); + connection + .execute_batch( + "DROP INDEX idx_reply_jobs_status_due; + ALTER TABLE reply_jobs RENAME TO reply_jobs_v0;", + ) + .expect("prepare reply_jobs v2 rebuild"); + connection + .execute_batch(AUTO_REPLY_REPLY_JOBS_V2_TABLE_SQL) + .expect("create reply_jobs v2 table"); + connection + .execute_batch( + "INSERT INTO reply_jobs( + event_id,event_json,status,due_at,decision,reason,category,reply, + scheduled_delay_seconds,error_class,created_at,updated_at,attempt_no + ) SELECT event_id,event_json,status,due_at,decision,reason,category,reply, + scheduled_delay_seconds,error_class,created_at,updated_at,0 + FROM reply_jobs_v0; + DROP TABLE reply_jobs_v0;", + ) + .expect("copy reply_jobs v2 rows"); + connection + .execute_batch(AUTO_REPLY_REPLY_JOBS_STATUS_INDEX_SQL) + .expect("recreate reply_jobs status index"); + connection + .execute_batch(journal_table_sql) + .expect("create transition journal table"); + connection + .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_INDEX_SQL) + .expect("create transition journal index"); + connection + .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_INSERT_TRIGGER_SQL) + .expect("create transition insert trigger"); + connection + .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_UPDATE_TRIGGER_SQL) + .expect("create transition update trigger"); + connection + .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL) + .expect("create transition cap trigger"); + connection + .pragma_update(None, "user_version", AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION) + .expect("set v2 queue user_version"); +} + +fn stopped_clean_queue_v2_fixture(path: &Path, include_breaker: bool) { + stopped_clean_queue_v2_fixture_with_table( + path, + include_breaker, + AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL, + ); +} + +#[test] +fn stopped_clean_queue_accepts_exact_legacy_v0_and_journal_v2_read_only() { + let root = tempfile::tempdir().unwrap(); + let room = stopped_clean_queue_test_room(root.path()); + let legacy = room.join("legacy.sqlite3"); + stopped_clean_queue_fixture(&legacy, true, "", "", "skipped"); + validate_stopped_clean_queue(&legacy, 42).expect("exact legacy queue must be accepted"); + let connection = + rusqlite::Connection::open_with_flags(&legacy, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY) + .unwrap(); + let tables = connection + .prepare( + "SELECT name FROM sqlite_master + WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name", + ) + .unwrap() + .query_map([], |row| row.get::<_, String>(0)) + .unwrap() + .collect::>>() + .unwrap(); + assert_eq!( + tables, + [ + "reply_job_supersessions", + "reply_job_tombstones", + "reply_jobs", + ] + ); + assert_eq!( + connection + .query_row("PRAGMA user_version", [], |row| row.get::<_, i64>(0)) + .unwrap(), + AUTO_REPLY_QUEUE_LEGACY_USER_VERSION + ); + drop(connection); + + let legacy_with_breaker = room.join("legacy-with-breaker.sqlite3"); + stopped_clean_queue_fixture(&legacy_with_breaker, true, EXACT_BREAKER_SCHEMA, "", "sent"); + validate_stopped_clean_queue(&legacy_with_breaker, 42) + .expect("exact legacy queue with breaker must be accepted"); + + for include_breaker in [false, true] { + let queue = room.join(format!("v2-{include_breaker}.sqlite3")); + stopped_clean_queue_v2_fixture(&queue, include_breaker); + validate_stopped_clean_queue(&queue, 42).expect("exact v2 queue must be accepted"); + let connection = rusqlite::Connection::open_with_flags( + &queue, + rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY, + ) + .unwrap(); + assert_eq!( + connection + .query_row("PRAGMA user_version", [], |row| row.get::<_, i64>(0)) + .unwrap(), + AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION + ); + assert_eq!( + connection + .query_row("SELECT COUNT(*) FROM pipeline_transitions", [], |row| { + row.get::<_, i64>(0) + }) + .unwrap(), + 0 + ); + } + + let canonical_python_style_v2 = room.join("canonical-python-style-v2.sqlite3"); + stopped_clean_queue_v2_fixture(&canonical_python_style_v2, true); + let connection = rusqlite::Connection::open(&canonical_python_style_v2).unwrap(); + for (index, code) in [ + "authorization_allowed", + "authorization_rejected", + "media_acquire_started", + "media_acquire_ready", + "media_acquire_failed", + "media_policy_rejected", + ] + .into_iter() + .enumerate() + { + connection + .execute( + "INSERT INTO pipeline_transitions( + schema_version,event_id,attempt_no,component,from_state, + to_state,code,source_epoch,occurred_at_ns + ) VALUES(1,?1,0,'authorization','none','ready',?2,NULL,?3)", + rusqlite::params![format!("db:42:{}", index + 10), code, index + 1], + ) + .unwrap(); + } + drop(connection); + validate_stopped_clean_queue(&canonical_python_style_v2, 42) + .expect("canonical helper-created v2 shape and vocabulary must be accepted"); +} + +#[test] +fn stopped_clean_queue_exactly_matches_python_helper_v2_schema() { + let root = tempfile::tempdir().unwrap(); + let room = stopped_clean_queue_test_room(root.path()); + let queue = room.join("reply-queue.sqlite3"); + let scripts = Path::new(env!("CARGO_MANIFEST_DIR")).join("scripts"); + let script = r#" +import pathlib +import sys +sys.path.insert(0, sys.argv[1]) +import auto_reply_transition_journal as journal +queue = pathlib.Path(sys.argv[2]) +connection = journal.open_queue(queue, create=True, expected_chat_id=42) +connection.execute( + "INSERT INTO reply_jobs(" + "event_id,event_json,status,due_at,decision,reason,category,reply," + "scheduled_delay_seconds,error_class,created_at,updated_at" + ") VALUES(?,?,?,?,?,?,?,?,?,?,?,?)", + ("db:42:1", '{"chat_id":42,"log_id":1}', "sent", None, "skip", + "test", "test", None, None, None, 1.0, 1.0), +) +connection.execute( + "INSERT INTO reply_job_tombstones VALUES('db:42:2','sent',1.0)" +) +connection.execute( + "INSERT INTO reply_job_supersessions VALUES('db:42:3','db:42:4',1.0)" +) +connection.commit() +connection.close() +"#; + let output = Command::new("python3") + .args(["-I", "-c", script]) + .arg(&scripts) + .arg(&queue) + .output() + .expect("run Python transition helper"); + assert!( + output.status.success(), + "Python helper failed: {}", + String::from_utf8_lossy(&output.stderr) + ); + validate_stopped_clean_queue(&queue, 42) + .expect("Rust must accept the exact Python helper v2 schema"); +} + +#[test] +fn stopped_clean_queue_rejects_cross_room_path_and_durable_identities() { + let root = tempfile::tempdir().unwrap(); + let room_42 = stopped_clean_queue_test_room(root.path()); + let queue_42 = room_42.join("reply-queue.sqlite3"); + stopped_clean_queue_v2_fixture(&queue_42, true); + validate_stopped_clean_queue(&queue_42, 42).expect("matching room must validate"); + assert!(validate_stopped_clean_queue(&queue_42, 84).is_err()); + + let room_84 = root.path().join("84"); + fs::create_dir_all(&room_84).unwrap(); + let queue_84 = room_84.join("reply-queue.sqlite3"); + fs::copy(&queue_42, &queue_84).unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&queue_84, fs::Permissions::from_mode(0o600)).unwrap(); + } + assert!(validate_stopped_clean_queue(&queue_84, 84).is_err()); + + let connection = rusqlite::Connection::open(&queue_42).unwrap(); + connection + .execute( + "INSERT INTO reply_job_supersessions VALUES(\ + 'db:42:3','db:84:4',1.0)", + [], + ) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&queue_42, 42).is_err()); + + let connection = rusqlite::Connection::open(&queue_42).unwrap(); + connection + .execute("DELETE FROM reply_job_supersessions", []) + .unwrap(); + connection + .execute("UPDATE reply_job_tombstones SET event_id='db:84:2'", []) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&queue_42, 42).is_err()); + + let connection = rusqlite::Connection::open(&queue_42).unwrap(); + connection + .execute("UPDATE reply_job_tombstones SET event_id='db:42:2'", []) + .unwrap(); + connection + .execute( + "UPDATE reply_jobs SET event_json='{\"chat_id\":42.0,\"log_id\":true}'", + [], + ) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&queue_42, 42).is_err()); + + let connection = rusqlite::Connection::open(&queue_42).unwrap(); + connection + .execute( + "UPDATE reply_jobs SET event_json='{\"chat_id\":42,\"log_id\":1}'", + [], + ) + .unwrap(); + connection + .execute( + "INSERT INTO pipeline_transitions(\ + schema_version,event_id,attempt_no,component,from_state,to_state,\ + code,source_epoch,occurred_at_ns) VALUES(\ + 1,'db:84:5',0,'queue','none','sent','enqueued',NULL,1)", + [], + ) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&queue_42, 42).is_err()); +} + +#[test] +fn stopped_clean_queue_rejects_nonexact_or_malformed_table_sets() { + let root = tempfile::tempdir().unwrap(); + let room = stopped_clean_queue_test_room(root.path()); + let missing = room.join("missing.sqlite3"); + stopped_clean_queue_fixture(&missing, false, "", "", "sent"); + assert!(validate_stopped_clean_queue(&missing, 42).is_err()); + + let extra = room.join("extra.sqlite3"); + stopped_clean_queue_fixture( + &extra, + true, + "", + "CREATE TABLE unexpected_table(value TEXT);", + "sent", + ); + assert!(validate_stopped_clean_queue(&extra, 42).is_err()); + + let malformed = room.join("malformed.sqlite3"); + stopped_clean_queue_fixture( + &malformed, + true, + " + CREATE TABLE model_circuit_breaker( + model_key TEXT PRIMARY KEY, state TEXT NOT NULL, + failure_class TEXT NOT NULL, + consecutive_failures INTEGER NOT NULL, + open_until REAL NOT NULL, lease_token TEXT, + updated_at TEXT NOT NULL + );", + "", + "sent", + ); + assert!(validate_stopped_clean_queue(&malformed, 42).is_err()); +} + +#[test] +fn stopped_clean_queue_rejects_unknown_or_mismatched_user_versions() { + let root = tempfile::tempdir().unwrap(); + let room = stopped_clean_queue_test_room(root.path()); + for version in [1, AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION, 3] { + let queue = room.join(format!("legacy-version-{version}.sqlite3")); + stopped_clean_queue_fixture(&queue, true, "", "", "sent"); + let connection = rusqlite::Connection::open(&queue).unwrap(); + connection + .pragma_update(None, "user_version", version) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&queue, 42).is_err()); + } + + for version in [AUTO_REPLY_QUEUE_LEGACY_USER_VERSION, 1, 3] { + let queue = room.join(format!("v2-version-{version}.sqlite3")); + stopped_clean_queue_v2_fixture(&queue, false); + let connection = rusqlite::Connection::open(&queue).unwrap(); + connection + .pragma_update(None, "user_version", version) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&queue, 42).is_err()); + } +} + +#[test] +fn stopped_clean_queue_rejects_v2_table_index_and_trigger_tampering() { + let root = tempfile::tempdir().unwrap(); + let room = stopped_clean_queue_test_room(root.path()); + + let table = room.join("table.sqlite3"); + let tampered_table_sql = AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL.replacen( + "CHECK(schema_version = 1)", + "CHECK(schema_version IN (1))", + 1, + ); + stopped_clean_queue_v2_fixture_with_table(&table, false, &tampered_table_sql); + assert!(validate_stopped_clean_queue(&table, 42).is_err()); + + let index = room.join("index.sqlite3"); + stopped_clean_queue_v2_fixture(&index, false); + let connection = rusqlite::Connection::open(&index).unwrap(); + connection + .execute_batch( + "DROP INDEX idx_pipeline_transitions_event_seq; + CREATE INDEX idx_pipeline_transitions_event_seq + ON pipeline_transitions(event_id, seq DESC);", + ) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&index, 42).is_err()); + + let trigger = room.join("trigger.sqlite3"); + stopped_clean_queue_v2_fixture(&trigger, false); + let connection = rusqlite::Connection::open(&trigger).unwrap(); + connection + .execute_batch( + "DROP TRIGGER trg_reply_jobs_transition_insert; + CREATE TRIGGER trg_reply_jobs_transition_insert + AFTER INSERT ON reply_jobs BEGIN SELECT 1; END;", + ) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&trigger, 42).is_err()); + + let extra_table = room.join("extra-table.sqlite3"); + stopped_clean_queue_v2_fixture(&extra_table, false); + let connection = rusqlite::Connection::open(&extra_table).unwrap(); + connection + .execute_batch("CREATE TABLE unexpected_table(value TEXT);") + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&extra_table, 42).is_err()); + + let extra_index = room.join("extra-index.sqlite3"); + stopped_clean_queue_v2_fixture(&extra_index, false); + let connection = rusqlite::Connection::open(&extra_index).unwrap(); + connection + .execute_batch("CREATE INDEX idx_unexpected ON reply_jobs(event_id);") + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&extra_index, 42).is_err()); + + let extra_trigger = room.join("extra-trigger.sqlite3"); + stopped_clean_queue_v2_fixture(&extra_trigger, false); + let connection = rusqlite::Connection::open(&extra_trigger).unwrap(); + connection + .execute_batch( + "CREATE TRIGGER trg_unexpected AFTER DELETE ON reply_jobs + BEGIN SELECT 1; END;", + ) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&extra_trigger, 42).is_err()); +} + +#[test] +fn stopped_clean_queue_rejects_v2_journal_above_hard_cap() { + let root = tempfile::tempdir().unwrap(); + let room = stopped_clean_queue_test_room(root.path()); + let queue = room.join("over-cap.sqlite3"); + stopped_clean_queue_v2_fixture(&queue, false); + let mut connection = rusqlite::Connection::open(&queue).unwrap(); + connection + .execute_batch("DROP TRIGGER trg_pipeline_transitions_cap;") + .unwrap(); + let transaction = connection.transaction().unwrap(); + { + let mut insert = transaction + .prepare( + "INSERT INTO pipeline_transitions( + schema_version,event_id,attempt_no,component,from_state, + to_state,code,source_epoch,occurred_at_ns + ) VALUES(1,?1,0,'queue','none','pending','enqueued',NULL,?2)", + ) + .unwrap(); + for sequence in 1..=AUTO_REPLY_QUEUE_JOURNAL_MAX_ROWS + 1 { + insert + .execute(rusqlite::params![format!("db:42:{sequence}"), sequence]) + .unwrap(); + } + } + transaction.commit().unwrap(); + connection + .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&queue, 42).is_err()); +} + +#[test] +fn stopped_clean_queue_rejects_v2_invalid_journal_metadata() { + assert!(parse_auto_reply_transition_event_id("db:1:1").is_some()); + assert!( + parse_auto_reply_transition_event_id("db:9223372036854775806:9223372036854775806") + .is_some() + ); + for invalid in [ + "db:0:1", + "db:01:1", + "db:1:01", + "db:1x:2", + "db:1:2x", + "db:1:1:2", + "DB:1:1", + "db:9223372036854775807:1", + "db:1:9223372036854775807", + ] { + assert!(parse_auto_reply_transition_event_id(invalid).is_none()); + } + let root = tempfile::tempdir().unwrap(); + let room = stopped_clean_queue_test_room(root.path()); + let queue = room.join("invalid-journal-metadata.sqlite3"); + stopped_clean_queue_v2_fixture(&queue, false); + let connection = rusqlite::Connection::open(&queue).unwrap(); + connection + .execute_batch("PRAGMA ignore_check_constraints = ON;") + .unwrap(); + connection + .execute( + "INSERT INTO pipeline_transitions( + schema_version,event_id,attempt_no,component,from_state, + to_state,code,source_epoch,occurred_at_ns + ) VALUES(1,'db:42:99',0,'queue','none','pending', + 'private-free-form-code',NULL,1)", + [], + ) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&queue, 42).is_err()); +} + +#[test] +fn stopped_clean_queue_rejects_v2_invalid_reply_job_attempt() { + let root = tempfile::tempdir().unwrap(); + let room = stopped_clean_queue_test_room(root.path()); + let queue = room.join("invalid-reply-job-attempt.sqlite3"); + stopped_clean_queue_v2_fixture(&queue, false); + let connection = rusqlite::Connection::open(&queue).unwrap(); + connection + .execute_batch( + "PRAGMA ignore_check_constraints = ON; + UPDATE reply_jobs SET attempt_no = 1000001 + WHERE event_id = 'db:42:1';", + ) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&queue, 42).is_err()); +} + +#[test] +fn stopped_clean_queue_rejects_nonterminal_rows() { + let root = tempfile::tempdir().unwrap(); + let room = stopped_clean_queue_test_room(root.path()); + let queue = room.join("nonterminal.sqlite3"); + stopped_clean_queue_fixture(&queue, true, "", "", "delivery_unknown"); + assert!(validate_stopped_clean_queue(&queue, 42).is_err()); + + let tombstone_queue = room.join("nonterminal-tombstone.sqlite3"); + stopped_clean_queue_fixture(&tombstone_queue, true, "", "", "sent"); + let connection = rusqlite::Connection::open(&tombstone_queue).unwrap(); + connection + .execute( + "UPDATE reply_job_tombstones SET status = 'delivery_unknown'", + [], + ) + .unwrap(); + drop(connection); + assert!(validate_stopped_clean_queue(&tombstone_queue, 42).is_err()); +} + +#[test] +fn auto_reply_accepts_only_fully_attested_stopped_clean_state() { + #[cfg(unix)] + use std::os::unix::fs::PermissionsExt; + + let root = tempfile::tempdir().expect("temporary state root"); + let room_root = root.path().join("rooms/42"); + fs::create_dir_all(&room_root).expect("create room root"); + let target = local_db::LocalChat { + chat_id: 42, + chat_type: 0, + chat_name: "부자멘토멘티".to_string(), + database_chat_name: Some("부자멘토멘티".to_string()), + active_members_count: 4, + last_log_id: 140, + last_updated_at: 0, + unread_count: 0, + display_name: "부자멘토멘티".to_string(), + }; + let state_path = room_root.join("db-watch-state.json"); + let status_path = room_root.join("supervisor-status.json"); + let queue_path = room_root.join("reply-queue.sqlite3"); + let stopped_state = serde_json::json!({ + "schema_version": 3, + "target_chat_id": 42, + "target_chat_name": "부자멘토멘티", + "cursor_floor": 100, + "acked_watermark": 130, + "last_observed_log_id": 130, + "observed_log_ids": [130], + "acked_log_ids": [130], + "pending_log_ids": [], + "pending_gaps": [], + "candidate_phase": "idle", + "in_flight_candidate": null, + "capability_state": "stopped_clean", + "delivery_enabled": false, + "fence": "stopped_clean", + "fence_reason": "", + "owner_id": "terminal-owner", + "source_epoch": 7, + }); + let stopped_status = serde_json::json!({ + "schema_version": 1, + "mode": "database_authoritative", + "state": "stopped", + "shutdown_state": "stopped_clean", + "all_children_exited": true, + "readiness": "fenced", + "fence_reason": "stopped_clean", + "owner": "terminal-owner", + "source_epoch": 7, + "target_chat_id": 42, + "target_chat_name": "부자멘토멘티", + "child_states": { + "ax_watch": "exited", + "db_watch": "exited", + "reply_worker": "exited", + }, + }); + fs::write(&state_path, serde_json::to_vec(&stopped_state).unwrap()).unwrap(); + fs::write(&status_path, serde_json::to_vec(&stopped_status).unwrap()).unwrap(); + let connection = rusqlite::Connection::open(&queue_path).expect("create queue"); + connection + .execute_batch( + " + CREATE TABLE reply_jobs( + event_id TEXT PRIMARY KEY, event_json TEXT NOT NULL, + status TEXT NOT NULL, due_at REAL, decision TEXT, + reason TEXT, category TEXT, reply TEXT, + scheduled_delay_seconds REAL, error_class TEXT, + created_at REAL NOT NULL, updated_at REAL NOT NULL + ); + CREATE INDEX idx_reply_jobs_status_due + ON reply_jobs(status, due_at); + CREATE TABLE reply_job_tombstones( + event_id TEXT PRIMARY KEY, status TEXT NOT NULL, + archived_at REAL NOT NULL + ); + CREATE TABLE reply_job_supersessions( + event_id TEXT PRIMARY KEY, + superseded_by_event_id TEXT NOT NULL, + created_at REAL NOT NULL, + CHECK(event_id <> superseded_by_event_id) + ); + CREATE TABLE model_circuit_breaker( + model_key TEXT PRIMARY KEY, + state TEXT NOT NULL, + failure_class TEXT NOT NULL, + consecutive_failures INTEGER NOT NULL, + open_until REAL NOT NULL, + lease_token TEXT, + updated_at REAL NOT NULL + ); + INSERT INTO reply_jobs VALUES( + 'db:42:1', '{\"chat_id\":42,\"log_id\":1}', + 'skipped', NULL, 'skip', 'test', + 'test', NULL, NULL, NULL, 1.0, 1.0 + ); + ", + ) + .expect("initialize queue schema"); + drop(connection); + #[cfg(unix)] + for path in [&state_path, &status_path, &queue_path] { + fs::set_permissions(path, fs::Permissions::from_mode(0o600)).unwrap(); + } + assert_eq!( + enrollment_floor_for_target(root.path(), &target, 140) + .expect("complete stopped-clean proof must be accepted"), + 130 + ); + let regressed_target = local_db::LocalChat { + last_log_id: 120, + ..target.clone() + }; + assert!(enrollment_floor_for_target(root.path(), ®ressed_target, 120).is_err()); + assert_eq!( + enrollment_cursor_authority_for_target(root.path(), &target, 140) + .expect("stopped-clean replay authority must be explicit"), + AutoReplyCursorAuthority { + kind: AUTO_REPLY_CURSOR_REPLAY_KIND, + cursor_floor: 130, + attested_db_last_log_id: 140, + prior_owner_id: Some("terminal-owner".to_string()), + prior_source_epoch: Some(7), + } + ); + + let connection = rusqlite::Connection::open(&queue_path).unwrap(); + connection + .execute( + "UPDATE reply_jobs SET status = 'delivery_unknown' \ + WHERE event_id = 'db:42:1'", + [], + ) + .unwrap(); + drop(connection); + assert!(enrollment_floor_for_target(root.path(), &target, 140).is_err()); + let connection = rusqlite::Connection::open(&queue_path).unwrap(); + connection + .execute( + "UPDATE reply_jobs SET status = 'sent' WHERE event_id = 'db:42:1'", + [], + ) + .unwrap(); + drop(connection); + + let mut bad_status = stopped_status.clone(); + bad_status["all_children_exited"] = serde_json::Value::Bool(false); + fs::write(&status_path, serde_json::to_vec(&bad_status).unwrap()).unwrap(); + #[cfg(unix)] + fs::set_permissions(&status_path, fs::Permissions::from_mode(0o600)).unwrap(); + assert!(enrollment_floor_for_target(root.path(), &target, 140).is_err()); + + let mut leftover_status = stopped_status.clone(); + leftover_status["shutdown_state"] = serde_json::json!("stopped_unclean"); + leftover_status["fence_reason"] = serde_json::json!("db_watch_exited"); + leftover_status["readiness"] = serde_json::json!("fenced"); + leftover_status["all_children_exited"] = serde_json::json!(true); + leftover_status["child_states"] = serde_json::json!({ + "ax_watch": "exited", + "db_watch": "exited", + "reply_worker": "exited", + }); + fs::write(&status_path, serde_json::to_vec(&leftover_status).unwrap()).unwrap(); + let mut raw_poll_fence = stopped_state; + raw_poll_fence["capability_state"] = serde_json::json!("fenced"); + raw_poll_fence["fence"] = serde_json::json!("db_unavailable"); + raw_poll_fence["fence_reason"] = serde_json::json!("poll_fence"); + raw_poll_fence["delivery_enabled"] = serde_json::json!(false); + fs::write(&state_path, serde_json::to_vec(&raw_poll_fence).unwrap()).unwrap(); + #[cfg(unix)] + for path in [&state_path, &status_path] { + fs::set_permissions(path, fs::Permissions::from_mode(0o600)).unwrap(); + } + assert_eq!( + enrollment_cursor_authority_for_target(root.path(), &target, 140) + .expect("terminal leftover ACK resume must be accepted") + .kind, + AUTO_REPLY_CURSOR_LEFTOVER_KIND + ); + raw_poll_fence["candidate_phase"] = serde_json::json!("hooking"); + raw_poll_fence["in_flight_candidate"] = serde_json::json!({ + "event_id": "db:1:130", + "log_id": 130, + "owner_id": raw_poll_fence.get("owner_id").cloned().unwrap_or(serde_json::json!("")), + "in_flight": true, + "pending": true + }); + raw_poll_fence["pending_log_ids"] = serde_json::json!([130]); + fs::write(&state_path, serde_json::to_vec(&raw_poll_fence).unwrap()).unwrap(); + #[cfg(unix)] + fs::set_permissions(&state_path, fs::Permissions::from_mode(0o600)).unwrap(); + assert_eq!( + enrollment_cursor_authority_for_target(root.path(), &target, 140) + .expect("orphaned hooking leftover ACK resume must be accepted") + .kind, + AUTO_REPLY_CURSOR_LEFTOVER_KIND + ); + raw_poll_fence["capability_state"] = serde_json::json!("ready"); + raw_poll_fence["delivery_enabled"] = serde_json::json!(true); + raw_poll_fence["fence"] = serde_json::json!("ready"); + raw_poll_fence["fence_reason"] = serde_json::json!(""); + fs::write(&state_path, serde_json::to_vec(&raw_poll_fence).unwrap()).unwrap(); + #[cfg(unix)] + fs::set_permissions(&state_path, fs::Permissions::from_mode(0o600)).unwrap(); + assert_eq!( + enrollment_cursor_authority_for_target(root.path(), &target, 140) + .expect("ready hooking leftover ACK resume must be accepted") + .kind, + AUTO_REPLY_CURSOR_LEFTOVER_KIND + ); + let leftover = read_bounded_json_file(&state_path).expect("leftover state remains"); + assert_eq!(leftover["acked_watermark"], 130); + assert_eq!(leftover["last_observed_log_id"], 130); + assert_ne!(leftover["fence"], "stopped_clean"); + leftover_status["all_children_exited"] = serde_json::json!(false); + fs::write(&status_path, serde_json::to_vec(&leftover_status).unwrap()).unwrap(); + #[cfg(unix)] + fs::set_permissions(&status_path, fs::Permissions::from_mode(0o600)).unwrap(); + assert!(enrollment_floor_for_target(root.path(), &target, 140).is_err()); +} + +#[test] +fn auto_reply_rejects_residual_legacy_state_without_status() { + let root = tempfile::tempdir().expect("temporary state root"); + fs::write(root.path().join("reply-queue.sqlite3"), b"legacy").expect("write residual queue"); + assert!(auto_reply_legacy_conflict(root.path()).is_err()); +} + +#[test] +#[cfg(unix)] +fn auto_reply_owner_lock_is_private_exclusive_and_no_follow() { + use std::os::unix::fs::PermissionsExt; + let root = tempfile::tempdir().expect("temporary state root"); + let root_path = root.path().canonicalize().expect("canonical state root"); + let lock_path = root_path.join("supervisor.owner.lock"); + fs::write(&lock_path, b"old").unwrap(); + fs::set_permissions(&lock_path, fs::Permissions::from_mode(0o644)).unwrap(); + let lock = acquire_auto_reply_owner_lock(&root_path).expect("acquire owner lock"); + assert_eq!( + fs::metadata(&lock_path).unwrap().permissions().mode() & 0o777, + 0o600 + ); + assert!(acquire_auto_reply_owner_lock(&root_path).is_err()); + drop(lock); + fs::remove_file(&lock_path).unwrap(); + let victim = root_path.join("victim"); + fs::write(&victim, b"do-not-touch").unwrap(); + std::os::unix::fs::symlink(&victim, &lock_path).unwrap(); + assert!(acquire_auto_reply_owner_lock(&root_path).is_err()); + assert_eq!(fs::read(&victim).unwrap(), b"do-not-touch"); +} + +#[test] +#[cfg(unix)] +fn auto_reply_private_state_and_enrollment_ignore_umask_022() { + use std::os::unix::fs::PermissionsExt; + + struct UmaskGuard(libc::mode_t); + impl Drop for UmaskGuard { + fn drop(&mut self) { + unsafe { + libc::umask(self.0); + } + } + } + + let parent = tempfile::tempdir().expect("temporary state parent"); + let parent_path = parent + .path() + .canonicalize() + .expect("canonical state parent"); + let root = parent_path.join("foreground-state"); + let previous = unsafe { libc::umask(0o022) }; + let _umask = UmaskGuard(previous); + let lock = acquire_auto_reply_owner_lock(&root).expect("acquire private owner lock"); + let target = local_db::LocalChat { + chat_id: 42, + chat_type: 0, + chat_name: "부자멘토멘티".to_string(), + database_chat_name: Some("부자멘토멘티".to_string()), + active_members_count: 4, + last_log_id: 100, + last_updated_at: 0, + unread_count: 0, + display_name: "부자멘토멘티".to_string(), + }; + let room = + prepare_auto_reply_room_state(&root, &target).expect("prepare private foreground room"); + write_private_auto_reply_enrollment(&root, br#"{"schema_version":4}"#) + .expect("write private enrollment authority"); + write_private_auto_reply_enrollment(&root, br#"{"schema_version":5}"#) + .expect("atomically replace private enrollment authority"); + + for directory in [&root, &root.join("rooms"), &room] { + assert_eq!( + fs::symlink_metadata(directory) + .unwrap() + .permissions() + .mode() + & 0o777, + 0o700 + ); + } + for file in [ + root.join("supervisor.owner.lock"), + root.join("enrollment.json"), + ] { + assert_eq!( + fs::symlink_metadata(file).unwrap().permissions().mode() & 0o777, + 0o600 + ); + } + assert_eq!( + fs::read(root.join("enrollment.json")).unwrap(), + br#"{"schema_version":5}"# + ); + assert!(fs::read_dir(&root).unwrap().all(|entry| { + !entry + .unwrap() + .file_name() + .to_string_lossy() + .starts_with(".enrollment.") + })); + drop(lock); +} + +#[test] +#[cfg(unix)] +fn auto_reply_private_state_rejects_symlinks_and_enrollment_hardlinks() { + use std::os::unix::fs::{symlink, PermissionsExt}; + + let parent = tempfile::tempdir().expect("temporary state parent"); + let parent_path = parent + .path() + .canonicalize() + .expect("canonical state parent"); + let redirected_root = parent_path.join("redirected-root"); + fs::create_dir(&redirected_root).expect("create redirected root"); + fs::set_permissions(&redirected_root, fs::Permissions::from_mode(0o755)).unwrap(); + let root_link = parent_path.join("state-link"); + symlink(&redirected_root, &root_link).expect("create state-root symlink"); + assert!(acquire_auto_reply_owner_lock(&root_link).is_err()); + assert_eq!( + fs::metadata(&redirected_root).unwrap().permissions().mode() & 0o777, + 0o755 + ); + + let intermediate = parent_path.join("intermediate-link"); + symlink(&redirected_root, &intermediate).expect("create intermediate symlink"); + assert!(acquire_auto_reply_owner_lock(&intermediate.join("nested-state")).is_err()); + assert!(!redirected_root.join("nested-state").exists()); + + let root = parent_path.join("state"); + let lock = acquire_auto_reply_owner_lock(&root).expect("acquire private root"); + let rooms = root.join("rooms"); + fs::create_dir(&rooms).expect("create rooms directory"); + let redirected_room = parent_path.join("redirected-room"); + fs::create_dir(&redirected_room).expect("create redirected room"); + fs::set_permissions(&redirected_room, fs::Permissions::from_mode(0o755)).unwrap(); + symlink(&redirected_room, rooms.join("42")).expect("create room symlink"); + let target = local_db::LocalChat { + chat_id: 42, + chat_type: 0, + chat_name: "부자멘토멘티".to_string(), + database_chat_name: Some("부자멘토멘티".to_string()), + active_members_count: 4, + last_log_id: 100, + last_updated_at: 0, + unread_count: 0, + display_name: "부자멘토멘티".to_string(), + }; + assert!(prepare_auto_reply_room_state(&root, &target).is_err()); + assert_eq!( + fs::metadata(&redirected_room).unwrap().permissions().mode() & 0o777, + 0o755 + ); + + let victim = parent_path.join("enrollment-victim"); + fs::write(&victim, b"do-not-touch").expect("write enrollment victim"); + fs::set_permissions(&victim, fs::Permissions::from_mode(0o600)).unwrap(); + let enrollment = root.join("enrollment.json"); + symlink(&victim, &enrollment).expect("create enrollment symlink"); + assert!(write_private_auto_reply_enrollment(&root, b"replacement").is_err()); + assert_eq!(fs::read(&victim).unwrap(), b"do-not-touch"); + + fs::remove_file(&enrollment).expect("remove enrollment symlink"); + fs::hard_link(&victim, &enrollment).expect("create enrollment hard link"); + assert!(write_private_auto_reply_enrollment(&root, b"replacement").is_err()); + assert_eq!(fs::read(&victim).unwrap(), b"do-not-touch"); + drop(lock); +} + +#[test] +fn auto_reply_accepts_only_attested_stopped_legacy_state() { + let root = tempfile::tempdir().expect("temporary state root"); + let status_path = root.path().join("supervisor-status.json"); + fs::write( + &status_path, + serde_json::to_vec(&serde_json::json!({ + "state": "stopped", + "legacy_drained": true, + })) + .expect("serialize status"), + ) + .expect("write status"); + assert!(auto_reply_legacy_conflict(root.path()).is_ok()); + fs::write( + &status_path, + serde_json::to_vec(&serde_json::json!({ + "state": "stopped", + })) + .expect("serialize status"), + ) + .expect("rewrite status"); + assert!(auto_reply_legacy_conflict(root.path()).is_err()); +} + +#[cfg(unix)] +#[test] +fn auto_reply_check_does_not_probe_python_executable() { + use std::os::unix::fs::PermissionsExt; + + let root = tempfile::tempdir().expect("temporary validation root"); + let executable = root.path().join("python3"); + let marker = root.path().join("executed"); + fs::write( + &executable, + format!("#!/bin/sh\nprintf x > '{}'\nexit 1\n", marker.display()), + ) + .expect("write fake interpreter"); + fs::set_permissions(&executable, fs::Permissions::from_mode(0o700)) + .expect("set executable permissions"); + let configured = executable.to_str().expect("executable path"); + let resolved = + validate_auto_reply_executable(Some(configured), "test python", "python3", false) + .expect("static validation should not execute the interpreter"); + assert_eq!(resolved, configured); + assert!(!marker.exists()); +} + +#[cfg(unix)] +#[test] +fn python_interpreter_rejects_cellar_version_path_and_keeps_keg_string() { + let cellar = Path::new( + "/opt/homebrew/Cellar/python@3.11/3.11.15_4/Frameworks/Python.framework/Versions/3.11/bin/python3.11", + ); + let error = validate_auto_reply_executable( + Some(cellar.to_str().expect("cellar path")), + "AutoReply python_interpreter", + "python3", + false, + ) + .expect_err("Cellar version paths must be rejected"); + assert!(error + .to_string() + .contains("must not be a Homebrew Cellar version path")); + assert!(is_homebrew_cellar_version_path(cellar)); + assert!(is_homebrew_opt_python_keg_path(Path::new( + "/opt/homebrew/opt/python@3.11/bin/python3.11" + ))); +} + +#[cfg(unix)] +#[test] +fn codex_auth_rejects_empty_credential_file() { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let root = tempfile::tempdir().expect("temporary Codex home"); + let empty = root.path().join("auth.json"); + fs::write(&empty, b"").expect("write empty auth"); + fs::set_permissions(&empty, fs::Permissions::from_mode(0o600)).expect("chmod auth"); + let uid = fs::symlink_metadata(&empty).expect("auth metadata").uid(); + assert!(validate_auto_reply_codex_auth(&empty, uid).is_err()); +} + +#[cfg(unix)] +#[test] +fn auto_reply_aggregate_tracks_live_per_room_readiness() { + use std::os::unix::fs::PermissionsExt; + + let root = tempfile::tempdir().expect("temporary aggregate root"); + let room_root = root.path().join("rooms/42"); + fs::create_dir_all(&room_root).expect("create room root"); + let status_path = room_root.join("supervisor-status.json"); + fs::write( + &status_path, + serde_json::to_vec(&serde_json::json!({ + "target_chat_id": 42, + "target_chat_name": "부자멘토멘티", + "state": "running", + "readiness": "ready", + "fence_reason": "", + "updated_at": chrono::Utc::now().timestamp_millis() as f64 / 1_000.0, + })) + .unwrap(), + ) + .unwrap(); + fs::set_permissions(&status_path, fs::Permissions::from_mode(0o600)).unwrap(); + let target = local_db::LocalChat { + chat_id: 42, + chat_type: 0, + chat_name: "부자멘토멘티".to_string(), + database_chat_name: Some("부자멘토멘티".to_string()), + active_members_count: 4, + last_log_id: 100, + last_updated_at: 0, + unread_count: 0, + display_name: "부자멘토멘티".to_string(), + }; + let mut child = Command::new("/bin/sh") + .args(["-c", "sleep 30"]) + .spawn() + .expect("spawn aggregate fixture child"); + write_auto_reply_aggregate( + root.path(), + std::slice::from_ref(&target), + std::slice::from_ref(&child), + "running", + ) + .expect("write live aggregate"); + let aggregate: serde_json::Value = + serde_json::from_slice(&fs::read(root.path().join("aggregate-status.json")).unwrap()) + .unwrap(); + assert_eq!(aggregate["schema_version"], 2); + assert_eq!(aggregate["readiness"], "ready"); + assert_eq!(aggregate["authoritative"], true); + assert_eq!(aggregate["room_count"], 1); + assert_eq!(aggregate["ready_room_count"], 1); + assert_eq!(aggregate["targets"][0]["ready"], true); + assert_eq!( + fs::metadata(root.path().join("aggregate-status.json")) + .unwrap() + .permissions() + .mode() + & 0o777, + 0o600 + ); + child.kill().ok(); + child.wait().ok(); +} + +#[cfg(unix)] +#[test] +fn auto_reply_children_guard_reaps_partial_startup_processes() { + let mut guard = AutoReplyChildrenGuard::new(1); + let mut command = Command::new("/bin/sh"); + command.args(["-c", "sleep 30"]); + configure_auto_reply_process_group(&mut command); + let child = command.spawn().expect("spawn fake supervisor"); + let pid = child.id() as libc::pid_t; + guard.children.push(child); + drop(guard); + assert_eq!(unsafe { libc::kill(pid, 0) }, -1); +} + +#[cfg(unix)] +#[test] +fn auto_reply_children_guard_kills_descendants_after_group_leader_exits() { + let mut guard = AutoReplyChildrenGuard::new(1); + let mut command = Command::new("/bin/sh"); + command.args([ + "-c", + "trap 'exit 0' TERM; (trap '' TERM HUP; exec sleep 30) & wait", + ]); + configure_auto_reply_process_group(&mut command); + let child = command.spawn().expect("spawn fake process group"); + let process_group = child.id() as libc::pid_t; + guard.children.push(child); + drop(guard); + assert_eq!(unsafe { libc::kill(-process_group, 0) }, -1); +} + +#[cfg(unix)] +#[test] +fn auto_reply_guardian_liveness_accepts_only_read_pipe_and_sets_cloexec() { + let mut descriptors = [-1; 2]; + assert_eq!(unsafe { libc::pipe(descriptors.as_mut_ptr()) }, 0); + let read_fd = descriptors[0]; + let write_fd = descriptors[1]; + + let read_pipe = validate_auto_reply_guardian_liveness_fd(read_fd) + .expect("read-only pipe should be accepted"); + let descriptor_flags = unsafe { libc::fcntl(read_pipe.as_raw_fd(), libc::F_GETFD) }; + assert_ne!(descriptor_flags & libc::FD_CLOEXEC, 0); + assert!(validate_auto_reply_guardian_liveness_fd(write_fd).is_err()); + + assert_eq!(unsafe { libc::close(write_fd) }, 0); + drop(read_pipe); +} + +#[cfg(unix)] +#[test] +fn guardian_eof_stops_auto_reply_root_and_descendant_group() { + static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + let _environment_guard = ENV_LOCK.lock().expect("lock liveness environment"); + std::env::remove_var(AUTO_REPLY_GUARDIAN_LIVENESS_ENV); + AUTO_REPLY_STOP.store(false, Ordering::Release); + AUTO_REPLY_GUARDIAN_LOST.store(false, Ordering::Release); + + let mut descriptors = [-1; 2]; + assert_eq!(unsafe { libc::pipe(descriptors.as_mut_ptr()) }, 0); + let read_fd = descriptors[0]; + let write_fd = descriptors[1]; + let write_flags = unsafe { libc::fcntl(write_fd, libc::F_GETFD) }; + assert!(write_flags >= 0); + assert_eq!( + unsafe { libc::fcntl(write_fd, libc::F_SETFD, write_flags | libc::FD_CLOEXEC) }, + 0 + ); + std::env::set_var(AUTO_REPLY_GUARDIAN_LIVENESS_ENV, read_fd.to_string()); + let monitor = start_auto_reply_guardian_liveness_monitor(false) + .expect("start liveness monitor") + .expect("guarded mode should start a monitor"); + assert!(std::env::var_os(AUTO_REPLY_GUARDIAN_LIVENESS_ENV).is_none()); + + let mut guard = AutoReplyChildrenGuard::new(1); + let mut command = Command::new("/bin/sh"); + command.args([ + "-c", + "trap 'exit 0' TERM; (trap '' TERM HUP; exec sleep 30) & wait", + ]); + configure_auto_reply_process_group(&mut command); + let child = command.spawn().expect("spawn fake worker group"); + let process_group = child.id() as libc::pid_t; + guard.children.push(child); + + assert_eq!(unsafe { libc::close(write_fd) }, 0); + let deadline = std::time::Instant::now() + Duration::from_secs(2); + while !AUTO_REPLY_STOP.load(Ordering::Acquire) && std::time::Instant::now() < deadline { + thread::sleep(Duration::from_millis(10)); + } + assert!(AUTO_REPLY_GUARDIAN_LOST.load(Ordering::Acquire)); + assert!(AUTO_REPLY_STOP.load(Ordering::Acquire)); + guard.stop(); + monitor.join().expect("liveness monitor should exit"); + assert_eq!(unsafe { libc::kill(-process_group, 0) }, -1); + + AUTO_REPLY_STOP.store(false, Ordering::Release); + AUTO_REPLY_GUARDIAN_LOST.store(false, Ordering::Release); +} + +#[test] +fn local_send_preflight_requires_auto_reply_worker_identity() { + assert!(require_auto_reply_worker_preflight(true, false).is_err()); + require_auto_reply_worker_preflight(true, true) + .expect("the AutoReply worker may run the read-only preflight"); + require_auto_reply_worker_preflight(false, false) + .expect("ordinary sends are governed by their existing gates"); +} + +#[test] +fn worker_actual_send_setup_failure_finishes_before_ax_call() { + let setup = finish_worker_bound_local_send_setup::<()>( + Err(anyhow::anyhow!("readiness unavailable")), + true, + false, + "target", + true, + ) + .expect("a worker setup failure has a structured no-mutation outcome"); + let mut ax_called = false; + if setup.is_some() { + ax_called = true; + } + assert!(setup.is_none()); + assert!(!ax_called); + + assert!(finish_worker_bound_local_send_setup::<()>( + Err(anyhow::anyhow!("preflight readiness unavailable")), + true, + true, + "target", + true, + ) + .is_err()); +} + +#[cfg(unix)] +#[test] +fn worker_setup_lock_contention_is_immediate_and_never_reaches_ax() { + let root = tempfile::tempdir().expect("temporary worker lock root"); + let path = root.path().join("worker.lock"); + let open_lock = || { + fs::OpenOptions::new() + .create(true) + .truncate(false) + .read(true) + .write(true) + .open(&path) + .expect("open worker lock") + }; + let holder = open_lock(); + acquire_worker_setup_lock_nonblocking(&holder, "test holder") + .expect("acquire initial worker lock"); + let actual_contender = open_lock(); + let preflight_contender = open_lock(); + + // Release after a short bound so a regression to blocking flock fails + // as an acquired lock instead of hanging the test process forever. + let releaser = std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(250)); + assert_eq!(unsafe { libc::flock(holder.as_raw_fd(), libc::LOCK_UN) }, 0); + }); + let actual_error = + acquire_worker_setup_lock_nonblocking(&actual_contender, "contended AX send lock") + .expect_err("worker lock contention must fail without waiting"); + let preflight_error = acquire_worker_setup_lock_nonblocking( + &preflight_contender, + "contended owner-generation lock", + ) + .expect_err("preflight lock contention must fail without waiting"); + releaser.join().expect("release worker lock"); + + let setup = + finish_worker_bound_local_send_setup::<()>(Err(actual_error), true, false, "target", true) + .expect("actual worker contention has a structured no-mutation outcome"); + let ax_called = setup.is_some(); + assert!(setup.is_none()); + assert!(!ax_called); + assert!(finish_worker_bound_local_send_setup::<()>( + Err(preflight_error), + true, + true, + "target", + true, + ) + .is_err()); +} + +fn bound_source_message(log_id: i64, author_id: i64, sender_name: &str) -> local_db::LocalMessage { + local_db::LocalMessage { + log_id, + chat_id: 417_780_809_780_519, + author_id, + is_self: false, + sender_name: sender_name.to_string(), + message: "body".to_string(), + attachment: String::new(), + message_type: 1, + sent_at: 1_700_000_000, + } +} + +#[test] +fn bound_source_message_prefers_the_page_that_was_already_read() { + let windowed = vec![bound_source_message(7, 4242, "권준혁")]; + let mut lookups = 0; + let resolved = resolve_bound_source_message(&windowed, 7, |_| { + lookups += 1; + Ok(None) + }) + .expect("a row inside the page resolves without a second read"); + assert_eq!(resolved.log_id, 7); + assert_eq!(resolved.sender_name, "권준혁"); + assert_eq!(lookups, 0, "the window hit must not re-query the DB"); +} + +#[test] +fn bound_source_message_falls_back_to_a_log_id_lookup() { + // The newest page holds only later rows, which is the normal shape of a + // scheduled reply in an active room. + let windowed = vec![bound_source_message(9, 1, "다른 사람")]; + let resolved = resolve_bound_source_message(&windowed, 7, |log_id| { + assert_eq!(log_id, 7); + Ok(Some(bound_source_message(7, 4242, "권준혁"))) + }) + .expect("the direct lookup resolves an older source row"); + assert_eq!(resolved.log_id, 7); + assert_eq!(resolved.author_id, 4242); +} + +#[test] +fn bound_source_message_fails_closed_when_the_row_is_gone() { + let windowed = vec![bound_source_message(9, 1, "다른 사람")]; + let error = resolve_bound_source_message(&windowed, 7, |_| Ok(None)) + .expect_err("a missing source row must not become a send"); + assert!(error + .to_string() + .contains("scheduled reply source row is unavailable")); +} + +#[test] +fn bound_source_message_reports_a_lookup_failure() { + let windowed: Vec = Vec::new(); + let error = resolve_bound_source_message(&windowed, 7, |_| anyhow::bail!("database gone")) + .expect_err("a read failure must surface instead of a silent miss"); + // The context wraps the read error, so the cause is on the chain + // rather than in the outermost message. + let chain = format!("{error:#}"); + assert!(chain.contains("database gone"), "{chain}"); + assert!( + chain.contains("read the scheduled reply source row"), + "{chain}" + ); +} diff --git a/src/commands/auto_reply/worker_gate.rs b/src/commands/auto_reply/worker_gate.rs new file mode 100644 index 00000000..012efdb8 --- /dev/null +++ b/src/commands/auto_reply/worker_gate.rs @@ -0,0 +1,611 @@ +//! What the AutoReply worker must prove before `local-send` mutates anything: +//! its locks, persisted readiness, the CLI enrollment, and the source message. + +use std::fs; +use std::path::{Path, PathBuf}; + +use anyhow::{Context, Result}; +use sha2::{Digest, Sha256}; + +use super::enrollment::{ + require_cli_enrolled_reply_author, validate_cli_enrollment_authority, + validate_cli_enrollment_digest, +}; +use super::state_root::{read_bounded_file, read_bounded_json_file}; +use super::MAX_INT64; +#[cfg(unix)] +use crate::auto_reply_runtime::acquire_worker_setup_lock; +use crate::commands::local_send::{require_allowed_send_chat, require_ax_send}; +use crate::{ax_send, commands, config, local_db}; +use openkakao_cli::auto_reply_service; + +const AUTO_REPLY_READINESS_MAX_AGE_SECONDS: f64 = 15.0; + +fn readiness_fresh(value: &serde_json::Value, now: f64) -> bool { + let stamp = value.as_f64().or_else(|| value.as_i64().map(|v| v as f64)); + stamp.is_some_and(|stamp| { + stamp.is_finite() + && -5.0 <= now - stamp + && now - stamp <= AUTO_REPLY_READINESS_MAX_AGE_SECONDS + }) +} + +fn require_persisted_auto_reply_readiness( + expected_owner: Option<&str>, + expected_epoch: Option, + expected_target: Option, + expected_chat_name: Option<&str>, + expected_last_observed: Option, +) -> Result<()> { + let home = dirs::home_dir().context("cannot resolve home directory")?; + let base = auto_reply_service::default_state_root(&home); + let supervisor_path = std::env::var_os("OPENKAKAO_SUPERVISOR_STATUS") + .filter(|value| !value.is_empty()) + .map(std::path::PathBuf::from) + .unwrap_or_else(|| base.join("supervisor-status.json")); + let db_state_path = std::env::var_os("OPENKAKAO_DB_WATCH_STATE") + .filter(|value| !value.is_empty()) + .map(std::path::PathBuf::from) + .unwrap_or_else(|| base.join("db-watch-state.json")); + let supervisor = read_bounded_json_file(&supervisor_path)?; + let db_state = read_bounded_json_file(&db_state_path)?; + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .context("system clock before UNIX epoch")? + .as_secs_f64(); + let owner = supervisor + .get("owner") + .and_then(serde_json::Value::as_str) + .filter(|value| !value.trim().is_empty()) + .context("supervisor owner missing")?; + if expected_owner.is_some_and(|expected| owner != expected) { + anyhow::bail!("persisted supervisor owner does not match event"); + } + let source_epoch = supervisor + .get("source_epoch") + .context("supervisor source epoch missing")? + .as_i64() + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("supervisor source epoch invalid")?; + let target = supervisor + .get("target_chat_id") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("supervisor target chat missing")?; + let chat_name = supervisor + .get("target_chat_name") + .and_then(serde_json::Value::as_str) + .filter(|value| !value.trim().is_empty()) + .context("supervisor target chat name missing")?; + if expected_epoch.is_some_and(|expected| source_epoch != expected) { + anyhow::bail!("persisted supervisor epoch does not match event"); + } + if expected_target.is_some_and(|expected| target != expected) { + anyhow::bail!("persisted supervisor target does not match event"); + } + if expected_chat_name.is_some_and(|expected| chat_name != expected) { + anyhow::bail!("persisted supervisor target name does not match event"); + } + let privacy_digest = supervisor + .get("privacy_digest") + .and_then(serde_json::Value::as_str) + .context("supervisor privacy attestation missing")?; + if privacy_digest.len() != 64 || !privacy_digest.bytes().all(|byte| byte.is_ascii_hexdigit()) { + anyhow::bail!("supervisor privacy attestation invalid"); + } + let config_path = std::env::var_os("OPENKAKAO_CONFIG") + .filter(|value| !value.is_empty()) + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + dirs::home_dir() + .unwrap_or_else(|| Path::new(".").to_path_buf()) + .join(".config/openkakao/config.toml") + }); + let config_digest = hex::encode(Sha256::digest( + fs::read(&config_path).with_context(|| "read privacy config")?, + )); + if config_digest != privacy_digest + || std::env::var("OPENKAKAO_PRIVACY_ATTESTATION") + .ok() + .is_some_and(|value| value != privacy_digest) + { + anyhow::bail!("privacy attestation changed"); + } + if supervisor.get("schema_version") != Some(&serde_json::Value::from(1)) + || supervisor.get("mode") + != Some(&serde_json::Value::String( + "database_authoritative".to_string(), + )) + { + anyhow::bail!("supervisor readiness schema invalid"); + } + if supervisor.get("readiness") != Some(&serde_json::Value::String("ready".to_string())) + || supervisor.get("state") != Some(&serde_json::Value::String("running".to_string())) + || supervisor.get("database_started") != Some(&serde_json::Value::Bool(true)) + || supervisor.get("auto_reply_enabled") != Some(&serde_json::Value::Bool(true)) + || supervisor.get("ax_state") != Some(&serde_json::Value::String("healthy".to_string())) + || supervisor + .get("ax_pid") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 < *value && *value < MAX_INT64) + .is_none() + || supervisor.get("ax_allow_send") != Some(&serde_json::Value::Bool(false)) + || supervisor.get("ax_delivery_state") + != Some(&serde_json::Value::String( + "fenced_db_authoritative".to_string(), + )) + || supervisor.get("delivery_state") + != Some(&serde_json::Value::String( + "fenced_db_authoritative".to_string(), + )) + || supervisor + .get("watcher_fence") + .and_then(serde_json::Value::as_object) + .and_then(|value| value.get("ax_allow_send")) + != Some(&serde_json::Value::Bool(false)) + || supervisor + .get("fence_reason") + .and_then(serde_json::Value::as_str) + != Some("") + || !readiness_fresh( + supervisor + .get("updated_at") + .context("supervisor heartbeat missing")?, + now, + ) + { + anyhow::bail!("persisted supervisor readiness is fenced"); + } + let db_owner = db_state + .get("owner_id") + .and_then(serde_json::Value::as_str) + .context("DB owner missing")?; + if db_owner != owner { + anyhow::bail!("persisted DB owner mismatch"); + } + let db_epoch = db_state + .get("source_epoch") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("DB source epoch invalid")?; + let db_target = db_state + .get("target_chat_id") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("DB target chat missing")?; + let cli_state_v3 = std::env::var("OPENKAKAO_AUTO_REPLY_CLI").as_deref() == Ok("1"); + let expected_db_schema = if cli_state_v3 { 3 } else { 2 }; + if db_state.get("schema_version") != Some(&serde_json::Value::from(expected_db_schema)) + || db_state.get("target_chat_name") + != Some(&serde_json::Value::String(chat_name.to_string())) + { + anyhow::bail!("DB readiness schema or target name invalid"); + } + if db_epoch != source_epoch + || db_target != target + || db_state.get("capability_state") != Some(&serde_json::Value::String("ready".to_string())) + || db_state.get("delivery_enabled") != Some(&serde_json::Value::Bool(true)) + || db_state.get("fence") != Some(&serde_json::Value::String("ready".to_string())) + || db_state + .get("fence_reason") + .and_then(serde_json::Value::as_str) + != Some("") + || !readiness_fresh( + db_state + .get("heartbeat_at") + .context("DB heartbeat missing")?, + now, + ) + || db_state.get("pending_log_ids") != Some(&serde_json::Value::Array(Vec::new())) + || db_state.get("pending_gaps") != Some(&serde_json::Value::Array(Vec::new())) + { + anyhow::bail!("persisted DB readiness is fenced"); + } + let watermark = db_state + .get("acked_watermark") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 <= *value && *value < MAX_INT64) + .context("DB watermark invalid")?; + if cli_state_v3 { + let cursor_floor = db_state + .get("cursor_floor") + .and_then(serde_json::Value::as_i64) + .filter(|value| 0 <= *value && *value < MAX_INT64) + .context("CLI DB cursor floor invalid")?; + if cursor_floor > watermark + || db_state.get("candidate_phase") + != Some(&serde_json::Value::String("idle".to_string())) + || db_state.get("in_flight_candidate") != Some(&serde_json::Value::Null) + { + anyhow::bail!("CLI DB candidate state is not idle"); + } + let enrollment_path = std::env::var_os("OPENKAKAO_ENROLLMENT_PATH") + .filter(|value| !value.is_empty()) + .map(PathBuf::from) + .context("CLI enrollment authority missing")?; + let enrollment_raw = read_bounded_file(&enrollment_path)?; + let expected_enrollment_digest = + std::env::var("OPENKAKAO_ENROLLMENT_SHA256").unwrap_or_default(); + validate_cli_enrollment_digest(&enrollment_raw, expected_enrollment_digest.trim())?; + let enrollment: serde_json::Value = + serde_json::from_slice(&enrollment_raw).context("malformed CLI enrollment JSON")?; + let expected_room_root = db_state_path + .parent() + .context("CLI DB state room root missing")?; + validate_cli_enrollment_authority( + &enrollment, + target, + chat_name, + cursor_floor, + expected_room_root, + )?; + } + let persisted_last_observed = db_state + .get("last_observed_log_id") + .and_then(serde_json::Value::as_i64) + .filter(|value| watermark <= *value && *value < MAX_INT64) + .context("DB observed watermark invalid")?; + if expected_last_observed.is_some_and(|expected| persisted_last_observed < expected) { + anyhow::bail!("scheduled reply source is not in the persisted watcher tail"); + } + let observed = db_state + .get("observed_log_ids") + .and_then(serde_json::Value::as_array) + .context("DB observed IDs missing")?; + let acked = db_state + .get("acked_log_ids") + .and_then(serde_json::Value::as_array) + .context("DB acked IDs missing")?; + let pending = db_state + .get("pending_log_ids") + .and_then(serde_json::Value::as_array) + .context("DB pending IDs missing")?; + let parse_ids = |values: &[serde_json::Value]| -> Result> { + values + .iter() + .map(|value| { + value + .as_i64() + .filter(|id| 0 < *id && *id < MAX_INT64) + .context("DB readiness ID invalid") + }) + .collect() + }; + let observed_ids = parse_ids(observed)?; + let acked_ids = parse_ids(acked)?; + let pending_ids = parse_ids(pending)?; + let observed_set: std::collections::BTreeSet = observed_ids.iter().copied().collect(); + let acked_set: std::collections::BTreeSet = acked_ids.iter().copied().collect(); + let pending_set: std::collections::BTreeSet = pending_ids.iter().copied().collect(); + let expected_last_observed = observed_set.iter().next_back().copied().unwrap_or(0); + if !acked_set.is_subset(&observed_set) + || pending_set != observed_set.difference(&acked_set).copied().collect() + || !pending_set.is_disjoint(&acked_set) + || watermark != acked_set.iter().next_back().copied().unwrap_or(0) + || db_state + .get("last_observed_log_id") + .and_then(serde_json::Value::as_i64) + != Some(expected_last_observed) + { + anyhow::bail!("DB readiness cursor sets invalid"); + } + Ok(()) +} + +/// Resolve the scheduled send's source row for its author attestation. +/// +/// The bound send attests that the row it answers has the expected author, and +/// it used to look for that row only in the newest 20 messages. A scheduled +/// reply fires after a debounce window, so in an active room the source row is +/// usually older than those 20 rows; the preflight then failed with "scheduled +/// reply source row is unavailable" and the job died as stale_backlog +/// (2026-09-22, room 417: 70 of 202 preflight attempts). Prefer the page that +/// was already read, then fall back to a direct log-id lookup on the same +/// read-only reader. A row that is genuinely gone still fails closed. +pub(super) fn resolve_bound_source_message( + windowed: &[local_db::LocalMessage], + expected_source_log_id: i64, + lookup: F, +) -> Result +where + F: FnOnce(i64) -> Result>, +{ + if let Some(message) = windowed + .iter() + .find(|message| message.log_id == expected_source_log_id) + { + return Ok(message.clone()); + } + lookup(expected_source_log_id) + .context("read the scheduled reply source row")? + .context("scheduled reply source row is unavailable") +} +pub(super) fn require_expected_local_source_tail( + target_chat_id: i64, + expected_source_log_id: i64, + source_fence: &local_db::LocalPollEnvelope, +) -> Result<()> { + let completeness = &source_fence.completeness; + // Later room rows must not cancel an earlier unanswered inbound. The + // poll starts after the scheduled source; an empty page means the + // source is still the tail, and a complete later page is another + // per-message job, not a reason to drop this send. + if source_fence.chat.chat_id != target_chat_id + || completeness.after_log_id != expected_source_log_id + || completeness.has_gap + || completeness.chat_last_log_id < expected_source_log_id + { + anyhow::bail!("scheduled reply source tail is unavailable"); + } + if completeness.status == "empty" { + if completeness.has_more + || completeness.first_log_id.is_some() + || completeness.last_log_id.is_some() + || completeness.available_max_log_id.is_some() + || !source_fence.messages.is_empty() + || completeness.chat_last_log_id != expected_source_log_id + { + anyhow::bail!("scheduled reply source tail is unavailable"); + } + return Ok(()); + } + if completeness.status != "complete" && completeness.status != "partial" { + anyhow::bail!("scheduled reply source tail is unavailable"); + } + if source_fence.messages.is_empty() + || completeness.first_log_id != source_fence.messages.first().map(|row| row.log_id) + || completeness.last_log_id != source_fence.messages.last().map(|row| row.log_id) + || source_fence + .messages + .iter() + .any(|row| row.log_id <= expected_source_log_id || row.chat_id != target_chat_id) + { + anyhow::bail!("scheduled reply source tail is unavailable"); + } + Ok(()) +} + +pub(super) fn require_auto_reply_worker_preflight( + preflight: bool, + worker_identity: bool, +) -> Result<()> { + if preflight && !worker_identity { + anyhow::bail!( + "local-send --preflight is only available to the database-authoritative AutoReply worker" + ); + } + Ok(()) +} + +pub(super) fn finish_worker_bound_local_send_setup( + setup: Result, + is_auto_reply_worker: bool, + preflight: bool, + chat_name: &str, + json: bool, +) -> Result> { + match setup { + Ok(value) => Ok(Some(value)), + Err(_error) if is_auto_reply_worker && !preflight => { + commands::local_send::emit_pre_send_unavailable(chat_name, json)?; + Ok(None) + } + Err(error) => Err(error), + } +} + +/// What `local-send` holds for its whole run. An AutoReply worker holds the +/// owner-generation and AX send locks until this drops, and carries the +/// database-authoritative binding the AX sender re-attests. A plain run holds +/// neither lock and carries no binding. +pub struct LocalSendSetup { + _generation_lock: Option, + _send_lock: Option, + pub bound_chat: Option, +} + +/// Run the AutoReply worker's lock, readiness, and binding gates before a +/// `local-send` mutates anything. `None` means the worker setup failed and the +/// pre-send unavailable result was already emitted. +pub fn prepare_local_send( + config: &config::OpenKakaoConfig, + chat_name: &str, + dry_run: bool, + preflight: bool, + json: bool, +) -> Result> { + let worker_identity = std::env::var("OPENKAKAO_AUTO_REPLY_WORKER") + .or_else(|_| std::env::var("OPENKAKAO_BUJAMENTOR_WORKER")) + .as_deref() + == Ok("1"); + require_auto_reply_worker_preflight(preflight, worker_identity)?; + let is_auto_reply_worker = !dry_run && worker_identity; + let setup = (|| -> Result<_> { + let _generation_lock = if is_auto_reply_worker && !preflight { + let home = dirs::home_dir().context("cannot resolve home directory")?; + let lock_path = std::env::var_os("OPENKAKAO_AUTO_REPLY_GENERATION_LOCK") + .or_else(|| std::env::var_os("OPENKAKAO_AUTO_REPLY_LOCK")) + .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_GENERATION_LOCK")) + .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_LOCK")) + .filter(|value| !value.is_empty()) + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + auto_reply_service::default_state_root(&home).join(".owner-generation.lock") + }); + let lock = fs::OpenOptions::new() + .create(true) + .truncate(false) + .read(true) + .write(true) + .open(lock_path)?; + #[cfg(unix)] + acquire_worker_setup_lock( + &lock, + "owner-generation lock", + std::time::Duration::from_millis(400), + )?; + Some(lock) + } else { + None + }; + let _send_lock = if is_auto_reply_worker && !preflight { + if let Some(lock_path) = + std::env::var_os("OPENKAKAO_AUTO_REPLY_SEND_LOCK").filter(|value| !value.is_empty()) + { + let lock = fs::OpenOptions::new() + .create(true) + .truncate(false) + .read(true) + .write(true) + .open(lock_path)?; + #[cfg(unix)] + acquire_worker_setup_lock( + &lock, + "AX send lock", + std::time::Duration::from_millis(400), + )?; + Some(lock) + } else { + None + } + } else { + None + }; + let mut bound_chat = None; + if !dry_run { + let mut worker_target = None; + if is_auto_reply_worker { + config::validate_auto_reply(config)?; + let expected_owner = std::env::var("OPENKAKAO_SUPERVISOR_OWNER") + .ok() + .filter(|value| !value.trim().is_empty()) + .context("worker owner missing")?; + let expected_epoch = std::env::var("OPENKAKAO_DB_SOURCE_EPOCH") + .ok() + .and_then(|value| value.parse::().ok()) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("worker source epoch invalid")?; + let expected_target = std::env::var("OPENKAKAO_TARGET_CHAT_ID") + .ok() + .and_then(|value| value.parse::().ok()) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("worker target chat invalid")?; + let expected_last_observed = std::env::var("OPENKAKAO_EXPECTED_SOURCE_LOG_ID") + .ok() + .and_then(|value| value.parse::().ok()) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("worker scheduled source log ID invalid")?; + let expected_author_id = std::env::var("OPENKAKAO_EXPECTED_SOURCE_AUTHOR_ID") + .ok() + .and_then(|value| value.parse::().ok()) + .filter(|value| 0 < *value && *value < MAX_INT64) + .context("worker scheduled source author ID invalid")?; + let expected_author_nickname = + std::env::var("OPENKAKAO_EXPECTED_SOURCE_AUTHOR_NICKNAME") + .ok() + .filter(|value| { + !value.is_empty() + && value.len() <= 1024 + && value.trim() == value + && !value.chars().any(char::is_control) + }) + .context("worker scheduled source author nickname invalid")?; + let proactive_send = std::env::var("OPENKAKAO_PROACTIVE_SEND") + .ok() + .is_some_and(|value| value == "1"); + require_persisted_auto_reply_readiness( + Some(expected_owner.as_str()), + Some(expected_epoch), + Some(expected_target), + Some(chat_name), + Some(expected_last_observed), + )?; + worker_target = Some(( + expected_target, + expected_last_observed, + expected_author_id, + expected_author_nickname, + proactive_send, + )); + } + require_ax_send(config)?; + require_allowed_send_chat(config, chat_name)?; + if let Some(( + target_chat_id, + expected_last_observed, + expected_author_id, + expected_author_nickname, + proactive_send, + )) = worker_target + { + // Fetch the authoritative local tail only after both + // process locks and all persisted/safety gates are held. + // The AX sender will re-attest this numeric ID's latest + // suffix against the exact open window before mutation. + let reader = local_db::LocalDbReader::open_no_mutation() + .context("open local database for bound AX send attestation")?; + let mut messages = reader.read_messages(target_chat_id, 20, None)?; + let source_fence = + reader.poll_after(target_chat_id, 1, Some(expected_last_observed))?; + require_expected_local_source_tail( + target_chat_id, + expected_last_observed, + &source_fence, + )?; + let source_message = + resolve_bound_source_message(&messages, expected_last_observed, |log_id| { + reader.read_message_by_log_id(target_chat_id, log_id) + })?; + if proactive_send { + if !source_message.is_self + && (source_message.author_id != expected_author_id + || source_message.sender_name.trim() != expected_author_nickname) + { + anyhow::bail!( + "scheduled reply source author identity drifted; re-enrollment is required" + ); + } + } else if source_message.is_self + || source_message.author_id != expected_author_id + || source_message.sender_name.trim() != expected_author_nickname + { + anyhow::bail!( + "scheduled reply source author identity drifted; re-enrollment is required" + ); + } + let enrollment_path = std::env::var_os("OPENKAKAO_ENROLLMENT_PATH") + .filter(|value| !value.is_empty()) + .map(PathBuf::from) + .context("CLI enrollment authority missing")?; + let enrollment_raw = read_bounded_file(&enrollment_path)?; + let expected_enrollment_digest = + std::env::var("OPENKAKAO_ENROLLMENT_SHA256").unwrap_or_default(); + validate_cli_enrollment_digest(&enrollment_raw, expected_enrollment_digest.trim())?; + let enrollment: serde_json::Value = serde_json::from_slice(&enrollment_raw) + .context("malformed CLI enrollment JSON")?; + require_cli_enrolled_reply_author( + &enrollment, + target_chat_id, + expected_author_id, + &expected_author_nickname, + )?; + messages.reverse(); + let local_tail = ax_send::normalize_local_binding_suffix(&messages) + .into_iter() + .map(|(_, token)| token) + .collect(); + bound_chat = Some(commands::local_send::BoundLocalSend { + chat_id: target_chat_id, + expected_source_log_id: expected_last_observed, + local_tail, + }); + } + } + Ok(LocalSendSetup { + _generation_lock, + _send_lock, + bound_chat, + }) + })(); + finish_worker_bound_local_send_setup(setup, is_auto_reply_worker, preflight, chat_name, json) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a44b411f..189f1137 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,5 +1,6 @@ pub mod analytics; pub mod auth; +pub mod auto_reply; pub mod auto_reply_host; pub mod ax_read; pub mod ax_watch; diff --git a/src/main.rs b/src/main.rs index 4819755d..35ffbf02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,29 +19,15 @@ mod state; mod util; use openkakao_cli::room_catalog; -use std::fs; -use std::io::{self, IsTerminal, Read, Write}; -#[cfg(unix)] -use std::os::unix::io::AsRawFd; -#[cfg(unix)] -use std::os::unix::io::FromRawFd; -#[cfg(unix)] -use std::os::unix::process::CommandExt; -use std::path::{Path, PathBuf}; -use std::process::{Child, Command, Stdio}; -use std::sync::atomic::AtomicBool; +use std::io::{self, Write}; use std::sync::atomic::Ordering; -use std::thread; -use std::time::Duration; use anyhow::{Context, Result}; use chrono::TimeZone; use clap::{CommandFactory, Parser, Subcommand}; use clap_complete::{generate, Shell}; -use sha2::{Digest, Sha256}; use crate::auth_flow::{set_auth_policy, AuthPolicy}; -use crate::auto_reply_runtime::{leftover_queue_has_unknown_send, validate_private_regular_file}; use crate::commands::local_send::{require_allowed_send_chat, require_ax_send}; use crate::commands::read::ReadCommandOptions; use crate::commands::send::require_loco_write; @@ -1008,9774 +994,2497 @@ fn is_local_only_command(command: &Commands) -> bool { ) } -static AUTO_REPLY_STOP: AtomicBool = AtomicBool::new(false); -static AUTO_REPLY_GUARDIAN_LOST: AtomicBool = AtomicBool::new(false); -const AUTO_REPLY_PYTHON_ISOLATION_ARGS: [&str; 3] = ["-E", "-B", "-S"]; -const AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION: i64 = 4; -const AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION: i64 = 1; -const AUTO_REPLY_CURSOR_FRESH_KIND: &str = "fresh_attested_tail"; -const AUTO_REPLY_CURSOR_REPLAY_KIND: &str = "stopped_clean_ack_replay"; -const AUTO_REPLY_CURSOR_LEFTOVER_KIND: &str = "fenced_leftover_ack_resume"; -const AUTO_REPLY_GUARDIAN_LIVENESS_ENV: &str = "OPENKAKAO_SESSION_GUARDIAN_LIVENESS_FD"; - -extern "C" fn handle_auto_reply_signal(_: i32) { - AUTO_REPLY_STOP.store(true, Ordering::Relaxed); -} - -#[cfg(unix)] -fn validate_auto_reply_guardian_liveness_fd(fd: libc::c_int) -> Result { - if fd <= libc::STDERR_FILENO { - anyhow::bail!("session guardian liveness descriptor is invalid"); - } - - let mut metadata = std::mem::MaybeUninit::::uninit(); - if unsafe { libc::fstat(fd, metadata.as_mut_ptr()) } != 0 { - anyhow::bail!("session guardian liveness descriptor is unavailable"); - } - let metadata = unsafe { metadata.assume_init() }; - let status_flags = unsafe { libc::fcntl(fd, libc::F_GETFL) }; - if status_flags < 0 { - anyhow::bail!("session guardian liveness descriptor is unavailable"); - } - if metadata.st_mode & libc::S_IFMT != libc::S_IFIFO - || status_flags & libc::O_ACCMODE != libc::O_RDONLY - { - anyhow::bail!("session guardian liveness descriptor is not a read-only pipe"); - } - - let descriptor_flags = unsafe { libc::fcntl(fd, libc::F_GETFD) }; - if descriptor_flags < 0 - || unsafe { libc::fcntl(fd, libc::F_SETFD, descriptor_flags | libc::FD_CLOEXEC) } != 0 +fn main() -> Result<()> { + let cli = Cli::parse(); + // The record window's data source must work while the config is broken: + // "the config is invalid" is exactly one of the facts it has to report. + if let Commands::ReplyReceipt { + ledger, + state_root, + chat_id, + limit, + } = &cli.command { - anyhow::bail!("session guardian liveness descriptor cannot be isolated"); + return commands::reply_receipt::run( + ledger.clone(), + state_root.clone(), + chat_id, + *limit, + cli.json, + ); } - - let mut readiness = libc::pollfd { - fd, - events: libc::POLLIN | libc::POLLHUP | libc::POLLERR, - revents: 0, + let service_bootstrap_status_path = validate_service_bootstrap_paths(&cli.command)?; + let config = match load_config() { + Ok(config) => config, + Err(error) => { + if let Some(status_path) = service_bootstrap_status_path { + let _ = auto_reply_service::write_config_invalid_status(status_path); + } + return Err(error); + } }; - let poll_result = unsafe { libc::poll(&mut readiness, 1, 0) }; - if poll_result < 0 { - anyhow::bail!("session guardian liveness handshake failed"); - } - if poll_result > 0 { - anyhow::bail!("session guardian liveness was lost before activation"); - } - - // SAFETY: validation above proves this process owns an open descriptor. - // The returned File becomes its sole Rust owner and closes it on drop. - Ok(unsafe { fs::File::from_raw_fd(fd) }) -} - -#[cfg(unix)] -fn start_auto_reply_guardian_liveness_monitor( - check: bool, -) -> Result>> { - let Some(raw_value) = std::env::var_os(AUTO_REPLY_GUARDIAN_LIVENESS_ENV) else { - return Ok(None); + set_auth_policy(AuthPolicy::from_config(&config.auth)); + let json = cli.json; + let unattended = cli.unattended || config.mode.unattended; + let allow_non_interactive_send = + cli.allow_non_interactive_send || config.send.allow_non_interactive; + let allow_watch_side_effects = cli.allow_watch_side_effects || config.watch.allow_side_effects; + let min_unattended_send_interval_secs = config + .safety + .min_unattended_send_interval_secs + .unwrap_or(10); + let min_hook_interval_secs = config.safety.min_hook_interval_secs.unwrap_or(2); + let min_webhook_interval_secs = config.safety.min_webhook_interval_secs.unwrap_or(2); + let hook_timeout_secs = config.safety.hook_timeout_secs.unwrap_or(20); + let webhook_timeout_secs = config.safety.webhook_timeout_secs.unwrap_or(10); + let no_prefix = if cli.no_prefix { + true + } else { + matches!(config.send.default_prefix, Some(false)) }; - if check { - anyhow::bail!("session guardian liveness is invalid in read-only check mode"); - } - let value = raw_value - .to_str() - .context("session guardian liveness descriptor is not valid UTF-8")?; - if value.is_empty() - || value.len() > 10 - || !value.bytes().all(|byte| byte.is_ascii_digit()) - || (value.len() > 1 && value.starts_with('0')) - { - anyhow::bail!("session guardian liveness descriptor is malformed"); - } - let fd = value - .parse::() - .context("session guardian liveness descriptor is out of range")?; - let mut pipe = validate_auto_reply_guardian_liveness_fd(fd)?; - - // Do not expose even the descriptor number to worker environments. The - // descriptor itself is already FD_CLOEXEC before any worker can spawn. - std::env::remove_var(AUTO_REPLY_GUARDIAN_LIVENESS_ENV); - let monitor = thread::Builder::new() - .name("guardian-liveness".to_string()) - .spawn(move || { - let mut byte = [0_u8; 1]; - loop { - match pipe.read(&mut byte) { - Ok(0) | Ok(_) => break, - Err(error) if error.kind() == io::ErrorKind::Interrupted => continue, - Err(_) => break, - } - } - AUTO_REPLY_GUARDIAN_LOST.store(true, Ordering::Release); - AUTO_REPLY_STOP.store(true, Ordering::Release); - }) - .context("start session guardian liveness monitor")?; - Ok(Some(monitor)) -} -#[cfg(not(unix))] -fn start_auto_reply_guardian_liveness_monitor( - _check: bool, -) -> Result>> { - if std::env::var_os(AUTO_REPLY_GUARDIAN_LIVENESS_ENV).is_some() { - anyhow::bail!("session guardian liveness is unsupported on this platform"); + // Respect NO_COLOR env var (https://no-color.org/) and --no-color flag + if cli.no_color || std::env::var("NO_COLOR").is_ok() || json { + NO_COLOR.store(true, Ordering::Relaxed); } - Ok(None) -} -fn auto_reply_selector_values( - config: &config::OpenKakaoConfig, - cli_values: Vec, - chats: &[local_db::LocalChat], - state_root: &Path, - group_titles: &[(i64, String)], -) -> Result> { - let configured = config.auto_reply.chats.clone(); - let catalog_ids = room_catalog::catalog_auto_reply_chat_ids(state_root)?; - let values = if cli_values.is_empty() { - room_catalog::merge_configured_and_catalog_selectors_named( - &configured, - &catalog_ids, - chats, - group_titles, - )? - } else { - // 명시한 방만 검사한다. 카탈로그의 다른 방까지 합치면 인증이 되지 않는 - // 방 하나가 모든 방의 검사를 실패시킨다. - let expanded = expand_plain_chat_names_with_configured_bindings(cli_values, &configured); - room_catalog::merge_configured_and_catalog_selectors_named( - &expanded, - &[], - chats, - group_titles, - )? - }; - if values.is_empty() { - anyhow::bail!("no chat selected; pass --chat 부자멘토멘티, configure [auto_reply].chats, or enable a menubar room"); + // Server-login warning — printed to stderr so it never corrupts JSON on + // stdout. Silenced with OPENKAKAO_CLI_NO_DEPRECATION=1 for scripted + // local-only use. `local-send`/`ax-read` need neither login nor this + // warning, since they never touch Kakao's servers. + if std::env::var_os("OPENKAKAO_CLI_NO_DEPRECATION").is_none() + && !is_local_only_command(&cli.command) + { + eprintln!("⚠️ Server login (login --save / login --manual) is broken on recent"); + eprintln!(" KakaoTalk macOS builds. Do NOT repeatedly retry login on an unregistered"); + eprintln!(" device — it can get your account's sub-device login blocked. Prefer"); + eprintln!( + " 'local-send'/'ax-read'/'local-chats' — no server contact needed. See README." + ); } - Ok(values) -} -fn expand_plain_chat_names_with_configured_bindings( - cli_values: Vec, - configured: &[String], -) -> Vec { - let bindings = configured - .iter() - .filter_map(|value| { - let selector = value.trim(); - let binding = selector.strip_prefix("bind:")?; - let (_id, name) = binding.split_once(':')?; - let name = name.trim(); - if name.is_empty() { - None + match cli.command { + Commands::Auth => commands::auth::cmd_auth(json)?, + Commands::AuthStatus => commands::auth::cmd_auth_status(json)?, + Commands::Login { + save, + manual, + email, + password, + app_version, + } => { + if manual { + commands::auth::cmd_login_manual(save, email, password, app_version)? } else { - Some((name.to_owned(), selector.to_owned())) - } - }) - .collect::>(); - cli_values - .into_iter() - .map(|value| { - let trimmed = value.trim(); - if trimmed.starts_with("id:") - || trimmed.starts_with("name:") - || trimmed.starts_with("bind:") - || trimmed.bytes().all(|byte| byte.is_ascii_digit()) - { - return value; + commands::auth::cmd_login(save)? } - bindings.get(trimmed).cloned().unwrap_or(value) - }) - .collect() -} - -fn resolve_auto_reply_supervisor(binary: &Path) -> Result<(PathBuf, PathBuf)> { - let mut candidates = Vec::new(); - if let Some(runtime_root) = - std::env::var_os("OPENKAKAO_AUTO_REPLY_RUNTIME_ROOT").filter(|value| !value.is_empty()) - { - candidates.push(PathBuf::from(runtime_root).join("scripts")); - } - if let Some(parent) = binary.parent() { - candidates.push(parent.join("scripts")); - if let Some(prefix) = parent.parent() { - candidates.push(prefix.join("libexec").join("scripts")); } - } - candidates.push(Path::new(env!("CARGO_MANIFEST_DIR")).join("scripts")); - - for scripts_root in candidates { - let scripts_metadata = match fs::symlink_metadata(&scripts_root) { - Ok(metadata) => metadata, - Err(_) => continue, - }; - if !scripts_metadata.is_dir() || scripts_metadata.file_type().is_symlink() { - continue; + Commands::Me => commands::rest::cmd_me(json)?, + Commands::Friends { + favorites, + hidden, + search, + local, + chat_id, + user_id, + } => commands::rest::cmd_friends(favorites, hidden, search, local, chat_id, user_id, json)?, + Commands::Chats { + show_all, + unread, + search, + chat_type, + rest, + } => commands::chats::cmd_chats(show_all, unread, search, chat_type, rest, json)?, + Commands::Read { + chat_id, + count, + before, + cursor, + since, + all, + delay_ms, + force, + rest, + } => commands::read::cmd_read( + chat_id, + ReadCommandOptions { + count, + cursor: cursor.or(before), + since, + all, + delay_ms, + force, + rest, + json, + }, + )?, + Commands::Members { + chat_id, + rest, + full, + } => commands::members::cmd_members(chat_id, rest, full, json)?, + Commands::Chatinfo { chat_id } => commands::rest::cmd_chatinfo(chat_id, json)?, + Commands::Settings => commands::rest::cmd_settings(json)?, + Commands::Scrap { url } => commands::rest::cmd_scrap(&url, json)?, + Commands::Profile { + user_id, + chat_id, + local, + } => commands::profile::cmd_profile(user_id, chat_id, local, json)?, + Commands::Favorite { user_id } => commands::rest::cmd_favorite(user_id, json)?, + Commands::Unfavorite { user_id } => commands::rest::cmd_unfavorite(user_id, json)?, + Commands::Hide { user_id } => commands::rest::cmd_hide(user_id, json)?, + Commands::Unhide { user_id } => commands::rest::cmd_unhide(user_id, json)?, + Commands::Profiles => commands::rest::cmd_profiles(json)?, + Commands::Keywords => commands::rest::cmd_keywords(json)?, + Commands::Unread => commands::rest::cmd_unread(json)?, + Commands::Export { + chat_id, + format, + output, + } => commands::rest::cmd_export(chat_id, &format, output.as_deref(), json)?, + Commands::Search { chat_id, query } => commands::rest::cmd_search(chat_id, &query, json)?, + Commands::Stats { + chat_id, + limit, + since, + } => commands::analytics::cmd_stats(chat_id, limit, since.as_deref(), json)?, + Commands::Completions { shell } => { + generate( + shell, + &mut Cli::command(), + "openkakao-cli", + &mut io::stdout(), + ); } - let supervisor = scripts_root.join("auto-reply-supervisor.py"); - let runtime_files = [ - "auto-reply-supervisor.py", - "auto-reply-db-watch.py", - "auto-reply-worker.py", - "auto-reply-apple-watch.py", - "auto_reply_ax_ui.py", - "auto_reply_metrics.py", - "auto-reply-schema.json", - ]; - let trusted = runtime_files.iter().all(|name| { - let path = scripts_root.join(name); - let Ok(metadata) = fs::symlink_metadata(path) else { - return false; - }; - if !metadata.is_file() || metadata.file_type().is_symlink() { - return false; - } - #[cfg(unix)] - { - use std::os::unix::fs::MetadataExt; - if metadata.mode() & 0o022 != 0 { - return false; + Commands::Renew => commands::auth::cmd_renew(json)?, + Commands::Relogin { + fresh_xvc, + password, + email, + } => commands::auth::cmd_relogin(json, fresh_xvc, password, email)?, + Commands::LocoTest => { + eprintln!("[deprecated] 'loco-test' is now hidden. Prefer 'doctor --loco'."); + commands::auth::cmd_loco_test()? + } + Commands::Send { + chat_id, + message, + force, + yes, + dry_run, + } => { + let msg = format_outgoing_message(&message, no_prefix); + if dry_run { + eprintln!( + "[dry-run] Would send to chat {}: \"{}\"", + chat_id, + util::truncate(&msg, 80) + ); + if json { + util::output_json(&serde_json::json!({ + "dry_run": true, + "action": "send", + "chat_id": chat_id, + "message": msg, + }))?; } + } else { + require_loco_write(&config)?; + commands::send::cmd_send(commands::send::SendOptions { + chat_id, + message: msg, + force, + skip_confirm: yes, + unattended, + allow_non_interactive: allow_non_interactive_send, + min_interval_secs: min_unattended_send_interval_secs, + json, + })? } - true - }); - if trusted { - let runtime_root = scripts_root - .parent() - .map(Path::to_path_buf) - .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR"))); - return Ok((supervisor, runtime_root)); - } - } - anyhow::bail!( - "AutoReply runtime assets are missing; expected auto-reply-supervisor.py beside the binary or in the packaged scripts directory" - ) -} - -fn validate_auto_reply_executable( - value: Option<&str>, - label: &str, - default_name: &str, - probe_python_version: bool, -) -> Result { - let value = value - .context(format!("{label} must be configured explicitly"))? - .trim(); - if value.is_empty() || value.chars().any(|ch| ch.is_control()) { - anyhow::bail!("{label} must be a non-empty executable path"); - } - let configured = Path::new(value); - if !configured.is_absolute() { - anyhow::bail!("{label} must be an absolute path"); - } - if default_name == "python3" && is_homebrew_cellar_version_path(configured) { - anyhow::bail!("{label} must not be a Homebrew Cellar version path"); - } - let configured_metadata = fs::symlink_metadata(configured).with_context(|| { - if default_name == "python3" { - format!("python_interpreter_missing: {label} is unavailable") - } else { - format!("{label} does not resolve to a readable executable") } - })?; - if configured.is_symlink() { - if default_name != "python3" && default_name != "opencodex" { - anyhow::bail!("{label} must not be a symlink"); - } - if default_name == "python3" && !is_homebrew_opt_python_keg_path(configured) { - anyhow::bail!("{label} symlink must be a Homebrew opt python keg path"); - } - } else if !configured_metadata.is_file() { - anyhow::bail!("{label} is not a regular file"); - } - let target = fs::canonicalize(configured).with_context(|| { - if default_name == "python3" { - format!("python_interpreter_missing: {label} target is unavailable") - } else { - format!("{label} does not resolve to a readable executable") - } - })?; - let metadata = fs::symlink_metadata(&target)?; - if !metadata.is_file() { - anyhow::bail!("{label} is not a regular file"); - } - #[cfg(unix)] - { - use std::os::unix::fs::MetadataExt; - if metadata.mode() & 0o022 != 0 || metadata.mode() & 0o111 == 0 { - anyhow::bail!("{label} has unsafe ownership or executable permissions"); - } - } - if default_name == "python3" && probe_python_version { - let output = Command::new(&target) - .args([ - "-I", - "-B", - "-S", - "-c", - "import sys; print(f'{sys.implementation.name} {sys.version_info.major}.{sys.version_info.minor}')", - ]) - .output() - .with_context(|| format!("probe {label} version"))?; - let version = String::from_utf8_lossy(&output.stdout); - let trusted_version = ["cpython 3.11", "cpython 3.12", "cpython 3.13"] - .iter() - .any(|prefix| version.trim() == *prefix); - if !output.status.success() || !trusted_version || !output.stderr.is_empty() { - anyhow::bail!("{label} must be CPython 3.11, 3.12, or 3.13"); - } - } - if default_name == "python3" { - Ok(configured.to_string_lossy().into_owned()) - } else { - Ok(target.to_string_lossy().into_owned()) - } -} - -fn is_homebrew_opt_python_keg_path(path: &Path) -> bool { - path.to_str().is_some_and(|value| { - matches!( - value, - "/opt/homebrew/opt/python@3.11/bin/python3.11" - | "/opt/homebrew/opt/python@3.12/bin/python3.12" - | "/opt/homebrew/opt/python@3.13/bin/python3.13" - ) - }) -} - -fn is_homebrew_cellar_version_path(path: &Path) -> bool { - path.to_str() - .is_some_and(|value| value.contains("/Cellar/python@")) -} - -#[derive(Debug, Clone)] -struct AutoReplyRunner { - path: String, - kind: String, - model: String, - reasoning_effort: String, - service_tier: String, - sha256: String, - codex_home: Option, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum AutoReplyLlmChoice { - MlxQwen38FlashNext, - MlxQwen38TwentySevenB, - GjcGemini37Flash, - GjcOpencodeDeepseek41Flash, - CodexGpt56Luna, -} - -impl AutoReplyLlmChoice { - fn all() -> [Self; 5] { - [ - Self::MlxQwen38FlashNext, - Self::MlxQwen38TwentySevenB, - Self::GjcGemini37Flash, - Self::GjcOpencodeDeepseek41Flash, - Self::CodexGpt56Luna, - ] - } - - fn from_model(model: &str) -> Option { - match config::canonical_mlx_local_model(Some(model)) { - Some(config::MLX_FLASH_NEXT_MODEL_ID) => return Some(Self::MlxQwen38FlashNext), - Some(config::MLX_27B_MODEL_ID) => return Some(Self::MlxQwen38TwentySevenB), - _ => {} - } - match model.trim() { - "google-antigravity/gemini-3.8-flash" - | "google-antigravity/gemini-3.7-flash-high" - | "google-antigravity/gemini-3.7-flash-tiered" - | "google-antigravity/gemini-3.8-flash-tiered" - | "google-antigravity/gemini-3.8-flash-high" - | "google-antigravity/gemini-3.6-flash-tiered" - | "gjc" - | "gemini" - | "gemini-3.7-flash" - | "gemini-3.8-flash" - | "gemini-3.8-flash-high" - | "gemini-3.6-flash" => Some(Self::GjcGemini37Flash), - "opencode-go-session/deepseek-v4.1-flash" - | "opencode-go/deepseek-v4.1-flash" - | "deepseek-v4.1-flash" - | "opencode-go" => Some(Self::GjcOpencodeDeepseek41Flash), - "gpt-5.6-luna" | "codex" | "luna" => Some(Self::CodexGpt56Luna), - _ => None, - } - } - - fn label(self) -> &'static str { - match self { - Self::MlxQwen38FlashNext => "Local MLX Qwen3.8 Flash-Next", - Self::MlxQwen38TwentySevenB => "Local MLX Qwen3.8 27B", - Self::GjcGemini37Flash => "Gajae-Code Gemini 3.7 Flash (high)", - Self::GjcOpencodeDeepseek41Flash => "OpenCode Go DeepSeek V4.1 Flash", - Self::CodexGpt56Luna => "Codex GPT-5.6 Luna", - } - } - - fn model(self) -> &'static str { - match self { - Self::MlxQwen38FlashNext => config::MLX_FLASH_NEXT_MODEL_ID, - Self::MlxQwen38TwentySevenB => config::MLX_27B_MODEL_ID, - Self::GjcGemini37Flash => "google-antigravity/gemini-3.7-flash-tiered", - Self::GjcOpencodeDeepseek41Flash => "opencode-go-session/deepseek-v4.1-flash", - Self::CodexGpt56Luna => "gpt-5.6-luna", - } - } - - fn apply(self, config: &mut config::OpenKakaoConfig) { - if matches!(self, Self::MlxQwen38FlashNext | Self::MlxQwen38TwentySevenB) { - config.model.privacy_mode = Some("local".into()); - config.model.allow_egress = false; - config.model.provider = Some(config::MLX_SERVE_PROVIDER.into()); - config.model.retention = None; - config.auto_reply.reply_runner_kind = Some("opencodex".into()); - config.auto_reply.reply_model = Some(self.model().into()); - config.auto_reply.reply_reasoning_effort = Some("medium".into()); - config.auto_reply.reply_service_tier = Some("default".into()); - if config.auto_reply.reply_runner.is_none() { - config.auto_reply.reply_runner = Some("/opt/homebrew/bin/opencodex".into()); - } - return; - } - if config.auto_reply.reply_runner_kind.as_deref() == Some("opencodex") { - config.model.privacy_mode = Some("remote_explicit".into()); - config.model.allow_egress = true; - config.model.provider = Some("opencodex".into()); - config.model.retention = Some("provider-policy".into()); - config.auto_reply.reply_model = Some(self.model().into()); - config.auto_reply.reply_reasoning_effort = Some("high".into()); - config.auto_reply.reply_service_tier = Some("default".into()); - if config.auto_reply.reply_runner.is_none() { - config.auto_reply.reply_runner = Some("/opt/homebrew/bin/opencodex".into()); - } - return; - } - match self { - Self::MlxQwen38FlashNext => unreachable!("local MLX choice returns above"), - Self::MlxQwen38TwentySevenB => unreachable!("local MLX choice returns above"), - Self::GjcGemini37Flash | Self::GjcOpencodeDeepseek41Flash => { - config.model.privacy_mode = Some("remote_explicit".into()); - config.model.allow_egress = true; - config.model.provider = Some( - if self == Self::GjcOpencodeDeepseek41Flash { - // OpenCode Go is reached through the session-headed - // local provider entry in ~/.gjc/agent/models.yml. - "opencode-go-session" - } else { - "google-antigravity" - } - .into(), + Commands::SendMe { + message, + yes, + dry_run, + } => { + let reader = local_db::LocalDbReader::open() + .context("Failed to open local DB to find memo chat")?; + let memo_id = reader + .find_memo_chat_id()? + .context("Could not find memo chat (나와의 채팅) in local database")?; + let msg = format_outgoing_message(&message, no_prefix); + if dry_run { + eprintln!( + "[dry-run] Would send to memo chat {}: \"{}\"", + memo_id, + util::truncate(&msg, 80) ); - config.model.retention = Some("provider-policy".into()); - config.auto_reply.reply_runner_kind = Some("gjc".into()); - config.auto_reply.reply_model = Some(self.model().into()); - config.auto_reply.reply_reasoning_effort = Some("high".into()); - config.auto_reply.reply_service_tier = Some("default".into()); - if config.auto_reply.reply_runner.is_none() { - if let Some(home) = dirs::home_dir() { - let wrapper = home.join(".local/lib/openkakao/gjc.js"); - if wrapper.is_file() { - config.auto_reply.reply_runner = - Some(wrapper.to_string_lossy().into_owned()); - } - } + if json { + util::output_json(&serde_json::json!({ + "dry_run": true, + "action": "send_me", + "chat_id": memo_id, + "message": msg, + }))?; } + } else { + require_loco_write(&config)?; + commands::send::cmd_send(commands::send::SendOptions { + chat_id: memo_id, + message: msg, + force: false, + skip_confirm: yes, + unattended, + allow_non_interactive: allow_non_interactive_send, + min_interval_secs: min_unattended_send_interval_secs, + json, + })? } - Self::CodexGpt56Luna => { - config.model.privacy_mode = Some("remote_explicit".into()); - config.model.allow_egress = true; - config.model.provider = Some("openai-codex".into()); - config.model.retention = Some("provider-policy".into()); - config.auto_reply.reply_runner_kind = Some("codex".into()); - config.auto_reply.reply_model = Some(self.model().into()); - config.auto_reply.reply_reasoning_effort = Some("max".into()); - config.auto_reply.reply_service_tier = Some("priority".into()); - } - } - } -} - -fn select_auto_reply_llm( - config: &mut config::OpenKakaoConfig, - requested: Option<&str>, - json_output: bool, -) -> Result { - if let Some(requested) = requested { - return AutoReplyLlmChoice::from_model(requested).with_context(|| { - format!("unknown reply model {requested:?}; use the exact local Qwen3.8 Flash-Next or Qwen3.8 27B ID, gemini-3.7-flash, deepseek-v4.1-flash or gpt-5.6-luna") - }); - } - if json_output || !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() { - let configured = config.auto_reply.reply_model.as_deref().unwrap_or(""); - return AutoReplyLlmChoice::from_model(configured).with_context(|| { - "no working reply model selected; pass --model or run interactively to pick one" - }); - } - let items = AutoReplyLlmChoice::all() - .into_iter() - .map(|choice| crate::util::ArrowMenuItem { - label: choice.label().to_owned(), - value: choice.model().to_owned(), - }) - .collect::>(); - let selected = config - .auto_reply - .reply_model - .as_deref() - .and_then(AutoReplyLlmChoice::from_model) - .and_then(|current| { - AutoReplyLlmChoice::all() - .iter() - .position(|item| *item == current) - }) - .unwrap_or(0); - let index = crate::util::select_with_arrows("Select the reply LLM", &items, selected)?; - Ok(AutoReplyLlmChoice::all()[index]) -} - -const LOCAL_MLX_BASE_URL: &str = "http://127.0.0.1:11234/v1"; -const LOCAL_MLX_MODELS_URL: &str = "http://127.0.0.1:11234/v1/models"; -const LOCAL_MLX_CHAT_URL: &str = "http://127.0.0.1:11234/v1/chat/completions"; -// 2026-09-22 실측 cold-start 첫 토큰은 23.09초였다. 15초 중단은 warm-up도 -// 취소해 preflight_failed가 5회 연속 발생했으므로 충분한 기동 여유를 둔다. -const LOCAL_MLX_PROBE_TIMEOUT: Duration = Duration::from_secs(120); -const LOCAL_MLX_MAX_RESPONSE_BYTES: usize = 64 * 1024; -const LOCAL_MLX_MAX_MODEL_ROWS: usize = 256; -const LOCAL_MLX_MAX_CHOICES: usize = 8; -const LOCAL_MLX_MAX_CONTENT_BYTES: usize = 1024; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum LocalMlxProbeMethod { - Get, - Post, -} - -struct LocalMlxProbeRequest { - method: LocalMlxProbeMethod, - url: &'static str, - body: Option>, - timeout: Duration, - max_response_bytes: usize, -} - -struct LocalMlxProbeResponse { - status: u16, - body: Vec, -} - -trait LocalMlxProbeTransport { - fn send(&self, request: LocalMlxProbeRequest) -> Result; -} - -struct ReqwestLocalMlxProbeTransport; - -impl LocalMlxProbeTransport for ReqwestLocalMlxProbeTransport { - fn send(&self, request: LocalMlxProbeRequest) -> Result { - if !request.url.starts_with(LOCAL_MLX_BASE_URL) - || !matches!(request.url, LOCAL_MLX_MODELS_URL | LOCAL_MLX_CHAT_URL) - { - anyhow::bail!("local MLX probe rejected a non-loopback endpoint"); - } - if request.timeout.is_zero() - || request.timeout > LOCAL_MLX_PROBE_TIMEOUT - || request.max_response_bytes > LOCAL_MLX_MAX_RESPONSE_BYTES - { - anyhow::bail!("local MLX probe request exceeds its fixed bounds"); } - - let client = reqwest::blocking::Client::builder() - .no_proxy() - .redirect(reqwest::redirect::Policy::none()) - .connect_timeout(Duration::from_secs(2)) - .timeout(request.timeout) - .build() - .context("build bounded local MLX probe client")?; - let request_builder = match request.method { - LocalMlxProbeMethod::Get => { - if request.body.is_some() { - anyhow::bail!("local MLX models request must not contain a body"); + Commands::SendPhoto { + chat_id, + file, + force, + yes, + dry_run, + } => { + if dry_run { + eprintln!("[dry-run] Would send photo '{}' to chat {}", file, chat_id); + if json { + util::output_json(&serde_json::json!({ + "dry_run": true, "action": "send_photo", "chat_id": chat_id, "file": file, + }))?; } - client.get(request.url) - } - LocalMlxProbeMethod::Post => client - .post(request.url) - .header(reqwest::header::CONTENT_TYPE, "application/json") - .body( - request - .body - .context("local MLX generation request body is missing")?, - ), - } - .header(reqwest::header::ACCEPT, "application/json"); - let response = request_builder - .send() - .context("send bounded local MLX probe request")?; - let status = response.status().as_u16(); - let mut body = Vec::new(); - response - .take(request.max_response_bytes.saturating_add(1) as u64) - .read_to_end(&mut body) - .context("read bounded local MLX probe response")?; - if body.len() > request.max_response_bytes { - anyhow::bail!("local MLX probe response is too large"); - } - Ok(LocalMlxProbeResponse { status, body }) - } -} - -fn parse_local_mlx_probe_json( - response: LocalMlxProbeResponse, - stage: &str, -) -> Result { - if response.status != 200 { - anyhow::bail!("local MLX {stage} probe returned HTTP {}", response.status); - } - if response.body.len() > LOCAL_MLX_MAX_RESPONSE_BYTES { - anyhow::bail!("local MLX {stage} probe response is too large"); - } - serde_json::from_slice(&response.body) - .with_context(|| format!("local MLX {stage} probe returned malformed JSON")) -} - -fn probe_local_mlx_auto_reply( - config: &config::OpenKakaoConfig, - transport: &dyn LocalMlxProbeTransport, -) -> Result<()> { - config::validate_auto_reply_local_mlx_profile(config)?; - let model = config::canonical_mlx_local_model(config.auto_reply.reply_model.as_deref()) - .context("local MLX AutoReply model is not allowlisted")?; - - let models = parse_local_mlx_probe_json( - transport.send(LocalMlxProbeRequest { - method: LocalMlxProbeMethod::Get, - url: LOCAL_MLX_MODELS_URL, - body: None, - timeout: LOCAL_MLX_PROBE_TIMEOUT, - max_response_bytes: LOCAL_MLX_MAX_RESPONSE_BYTES, - })?, - "models", - )?; - let rows = models - .get("data") - .and_then(serde_json::Value::as_array) - .context("local MLX models probe response is malformed")?; - if rows.len() > LOCAL_MLX_MAX_MODEL_ROWS { - anyhow::bail!("local MLX models probe returned too many rows"); - } - let mut target = None; - for row in rows { - let row = row - .as_object() - .context("local MLX models probe row is malformed")?; - let row_id = row - .get("id") - .and_then(serde_json::Value::as_str) - .context("local MLX models probe row has no model ID")?; - if config::canonical_mlx_local_model(Some(row_id)) == Some(model) { - if target.is_some() { - anyhow::bail!("local MLX models probe returned an ambiguous model"); + } else { + require_loco_write(&config)?; + commands::send::cmd_send_file(commands::send::SendFileOptions { + chat_id, + file_path: file, + force, + skip_confirm: yes, + unattended, + allow_non_interactive: allow_non_interactive_send, + min_interval_secs: min_unattended_send_interval_secs, + json, + })? } - target = Some(row); } - } - let target = target.context("local MLX model is not advertised")?; - if target.get("loaded").and_then(serde_json::Value::as_bool) != Some(true) - || target.get("state").and_then(serde_json::Value::as_str) != Some("ready") - { - anyhow::bail!("local MLX model is not ready"); - } - - let body = serde_json::to_vec(&serde_json::json!({ - "model": model, - "messages": [{"role": "user", "content": "확인이라고만 답하세요."}], - "max_tokens": 8, - "temperature": 0, - "stream": false, - }))?; - let generation = parse_local_mlx_probe_json( - transport.send(LocalMlxProbeRequest { - method: LocalMlxProbeMethod::Post, - url: LOCAL_MLX_CHAT_URL, - body: Some(body), - timeout: LOCAL_MLX_PROBE_TIMEOUT, - max_response_bytes: LOCAL_MLX_MAX_RESPONSE_BYTES, - })?, - "generation", - )?; - let choices = generation - .get("choices") - .and_then(serde_json::Value::as_array) - .context("local MLX generation probe response is malformed")?; - if choices.len() > LOCAL_MLX_MAX_CHOICES { - anyhow::bail!("local MLX generation probe returned too many choices"); - } - let content = choices - .first() - .and_then(|choice| choice.get("message")) - .and_then(|message| message.get("content")) - .and_then(serde_json::Value::as_str) - .context("local MLX generation probe returned no content")?; - if content.trim().is_empty() || content.len() > LOCAL_MLX_MAX_CONTENT_BYTES { - anyhow::bail!("local MLX generation probe content is invalid"); - } - Ok(()) -} - -fn probe_auto_reply_llm( - config: &config::OpenKakaoConfig, - choice: AutoReplyLlmChoice, -) -> Result<()> { - probe_auto_reply_llm_with_local_transport(config, choice, &ReqwestLocalMlxProbeTransport) -} - -fn probe_auto_reply_llm_with_local_transport( - config: &config::OpenKakaoConfig, - choice: AutoReplyLlmChoice, - local_transport: &dyn LocalMlxProbeTransport, -) -> Result<()> { - if matches!( - choice, - AutoReplyLlmChoice::MlxQwen38FlashNext | AutoReplyLlmChoice::MlxQwen38TwentySevenB - ) { - return probe_local_mlx_auto_reply(config, local_transport); - } - - // Remote choices retain the existing OpenCodex authentication-gateway probe. - if let Ok(client) = reqwest::blocking::Client::builder() - .timeout(std::time::Duration::from_secs(8)) - .build() - { - let ocx_model = match choice { - AutoReplyLlmChoice::GjcGemini37Flash => "google-antigravity/gemini-3.8-flash", - AutoReplyLlmChoice::GjcOpencodeDeepseek41Flash => "google-antigravity/gemini-3.8-flash", - _ => "", - }; - if !ocx_model.is_empty() { - if let Ok(resp) = client - .post("http://127.0.0.1:10100/v1/chat/completions") - .header("Content-Type", "application/json") - .header("Authorization", "Bearer not-needed") - .json(&serde_json::json!({ - "model": ocx_model, - "messages": [{"role": "user", "content": "Reply with exactly OK"}] - })) - .send() - { - if resp.status().is_success() { - eprintln!( - "advisory: OpenCodex authentication gateway verified OK ({})", - ocx_model - ); - return Ok(()); + Commands::SendFile { + chat_id, + file, + force, + yes, + dry_run, + } => { + if dry_run { + eprintln!("[dry-run] Would send file '{}' to chat {}", file, chat_id); + if json { + util::output_json(&serde_json::json!({ + "dry_run": true, "action": "send_file", "chat_id": chat_id, "file": file, + }))?; } + } else { + require_loco_write(&config)?; + commands::send::cmd_send_file(commands::send::SendFileOptions { + chat_id, + file_path: file, + force, + skip_confirm: yes, + unattended, + allow_non_interactive: allow_non_interactive_send, + min_interval_secs: min_unattended_send_interval_secs, + json, + })? } } - } - - let runner = validate_auto_reply_runner(config)?; - if runner.kind == "opencodex" { - let output = Command::new(&runner.path) - .arg("--version") - .stdin(Stdio::null()) - .output() - .context("probe opencodex reply runner")?; - let stdout = String::from_utf8_lossy(&output.stdout); - if output.status.success() && stdout.contains("opencodex") { - return Ok(()); - } - anyhow::bail!("opencodex runner failed probe: {}", stdout.trim()); - } - match choice { - AutoReplyLlmChoice::MlxQwen38FlashNext => { - unreachable!("local MLX choice returns before remote probes") - } - AutoReplyLlmChoice::MlxQwen38TwentySevenB => { - unreachable!("local MLX choice returns before remote probes") - } - AutoReplyLlmChoice::GjcGemini37Flash => { - let output = Command::new(&runner.path) - .args([ - "-p", - "--no-session", - "--no-rules", - "--no-lsp", - "--no-title", - "--no-tools", - "--mode", - "text", - "--model", - choice.model(), - "--thinking", - "high", - "Reply with exactly OK", - ]) - .stdin(Stdio::null()) - .output() - .context("probe Gemini reply model")?; - let stdout = String::from_utf8_lossy(&output.stdout); - let stderr = String::from_utf8_lossy(&output.stderr); - if output.status.success() && stdout.contains("OK") { - return Ok(()); - } - if stderr.to_ascii_lowercase().contains("no api key") { - return Ok(()); + Commands::Delete { + chat_id, + log_id, + force, + yes, + dry_run, + } => { + if dry_run { + eprintln!( + "[dry-run] Would delete message {} from chat {}", + log_id, chat_id + ); + if json { + util::output_json(&serde_json::json!({ + "dry_run": true, "action": "delete", "chat_id": chat_id, "log_id": log_id, + }))?; + } + } else { + require_loco_write(&config)?; + commands::send::cmd_delete(commands::send::DeleteOptions { + chat_id, + log_id, + force, + skip_confirm: yes, + unattended, + allow_non_interactive: allow_non_interactive_send, + min_interval_secs: min_unattended_send_interval_secs, + json, + })? } - anyhow::bail!( - "selected LLM {} did not respond; runner={} stdout={} stderr={}", - choice.label(), - runner.path, - stdout.trim(), - stderr.trim() - ); } - AutoReplyLlmChoice::GjcOpencodeDeepseek41Flash => { - let mut child = Command::new(&runner.path) - .args([ - "-p", - "--no-session", - "--no-rules", - "--no-lsp", - "--no-title", - "--no-tools", - "--mode", - "text", - "--model", - choice.model(), - "Reply with exactly OK", - ]) - .stdin(Stdio::null()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .context("probe Gajae-Code reply model")?; - - let start = std::time::Instant::now(); - let mut timed_out = false; - let status = loop { - match child.try_wait()? { - Some(status) => break Some(status), - None => { - if start.elapsed() >= std::time::Duration::from_secs(15) { - let _ = child.kill(); - let _ = child.wait(); - timed_out = true; - break None; - } - std::thread::sleep(std::time::Duration::from_millis(200)); - } + Commands::MarkRead { + chat_id, + log_id, + yes: _, + dry_run, + } => { + if dry_run { + eprintln!( + "[dry-run] Would mark chat {} read up to {}", + chat_id, log_id + ); + if json { + util::output_json(&serde_json::json!({ + "dry_run": true, "action": "mark-read", "chat_id": chat_id, "log_id": log_id, + }))?; } - }; - let mut stdout = Vec::new(); - let mut stderr = Vec::new(); - if let Some(mut out) = child.stdout.take() { - use std::io::Read; - let _ = out.read_to_end(&mut stdout); - } - if let Some(mut err) = child.stderr.take() { - use std::io::Read; - let _ = err.read_to_end(&mut stderr); + } else { + require_loco_write(&config)?; + commands::send::cmd_mark_read(commands::send::MarkReadOptions { + chat_id, + log_id, + json, + })? } - let stdout_str = String::from_utf8_lossy(&stdout); - let stderr_str = String::from_utf8_lossy(&stderr); - let stderr_lower = stderr_str.to_ascii_lowercase(); - - if let Some(status) = status { - if status.success() && stdout_str.contains("OK") { - return Ok(()); + } + Commands::React { + chat_id, + log_id, + reaction_type, + dry_run, + } => { + if dry_run { + eprintln!( + "[dry-run] Would react (type={}) to message {} in chat {}", + reaction_type, log_id, chat_id + ); + if json { + util::output_json(&serde_json::json!({ + "dry_run": true, "action": "react", "chat_id": chat_id, "log_id": log_id, "reaction_type": reaction_type, + }))?; } + } else { + require_loco_write(&config)?; + commands::send::cmd_react(commands::send::ReactOptions { + chat_id, + log_id, + reaction_type, + json, + })? } - - if timed_out - || stderr_lower.contains("usage limit") - || stderr_lower.contains("rate limit") - || stderr_lower.contains("quota") - || stderr_lower.contains("429") - || stderr_lower.contains("no api key") - { - eprintln!("advisory: OpenCode Go usage limit/auth issue detected; probing Antigravity Gemini Flash high fallback"); - let fallback_output = Command::new(&runner.path) - .args([ - "-p", - "--no-session", - "--no-rules", - "--no-lsp", - "--no-title", - "--no-tools", - "--mode", - "text", - "--model", - "google-antigravity/gemini-3.7-flash-tiered", - "--thinking", - "high", - "Reply with exactly OK", - ]) - .stdin(Stdio::null()) - .output() - .context("probe Antigravity Gemini fallback model")?; - let fb_stdout = String::from_utf8_lossy(&fallback_output.stdout); - if fallback_output.status.success() && fb_stdout.contains("OK") { - eprintln!("advisory: Antigravity Gemini Flash high fallback verified OK"); - return Ok(()); + } + Commands::Edit { + chat_id, + log_id, + message, + force, + yes, + dry_run, + } => { + let msg = format_outgoing_message(&message, no_prefix); + if dry_run { + eprintln!( + "[dry-run] Would edit message {} in chat {}: \"{}\"", + log_id, + chat_id, + util::truncate(&msg, 80) + ); + if json { + util::output_json(&serde_json::json!({ + "dry_run": true, "action": "edit", "chat_id": chat_id, "log_id": log_id, "message": msg, + }))?; } + } else { + require_loco_write(&config)?; + commands::send::cmd_edit(commands::send::EditOptions { + chat_id, + log_id, + message: msg, + force, + skip_confirm: yes, + unattended, + allow_non_interactive: allow_non_interactive_send, + min_interval_secs: min_unattended_send_interval_secs, + json, + })? } - anyhow::bail!( - "selected LLM {} did not respond; runner={} stdout={} stderr={}", - choice.label(), - runner.path, - stdout_str.trim(), - stderr_str.trim() - ); } - AutoReplyLlmChoice::CodexGpt56Luna => { - let output = Command::new(&runner.path) - .arg("--version") - .stdin(Stdio::null()) - .output() - .context("probe Codex reply model")?; - let stdout = String::from_utf8_lossy(&output.stdout); - if !output.status.success() || !stdout.trim().starts_with("codex-cli ") { - anyhow::bail!("selected LLM {} is not working", choice.label()); - } - Ok(()) + Commands::Watch { + chat_id, + raw, + read_receipt, + max_reconnect, + reconnect_delay, + reconnect_max_delay, + download_media, + download_dir, + hook_cmd, + webhook_url, + webhook_header, + webhook_signing_secret, + webhook_format, + hook_chat_id, + hook_keyword, + hook_type, + hook_fail_fast, + resume, + capture, + } => commands::watch::cmd_watch(WatchOptions { + unattended, + allow_side_effects: allow_watch_side_effects, + filter_chat_id: chat_id, + raw, + read_receipt, + max_reconnect: config.watch.default_max_reconnect.unwrap_or(max_reconnect), + reconnect_delay_secs: reconnect_delay, + reconnect_max_delay_secs: reconnect_max_delay, + download_media, + download_dir, + hook_cmd, + webhook_url, + webhook_headers: webhook_header, + webhook_signing_secret, + hook_chat_ids: hook_chat_id, + hook_keywords: hook_keyword, + hook_types: hook_type, + hook_fail_fast, + min_hook_interval_secs, + min_webhook_interval_secs, + hook_timeout_secs, + webhook_timeout_secs, + allow_insecure_webhooks: config.safety.allow_insecure_webhooks, + webhook_format: WebhookFormat::from_str_opt(webhook_format.as_deref())?, + resume, + json, + capture, + })?, + Commands::Download { + chat_id, + log_id, + output_dir, + local, + expected_author_id, + } => commands::download::cmd_download( + chat_id, + log_id, + output_dir.as_deref(), + local, + expected_author_id, + json, + )?, + Commands::Cache { chat_id, limit } => commands::analytics::cmd_cache(chat_id, limit, json)?, + Commands::CacheSearch { + query, + chat_id, + count, + } => commands::analytics::cmd_cache_search(&query, chat_id, count, json)?, + Commands::CacheStats => commands::analytics::cmd_cache_stats(json)?, + Commands::LocoChats { show_all } => { + eprintln!("[deprecated] 'loco-chats' is now hidden. Prefer 'chats' (LOCO by default)."); + commands::chats::cmd_loco_chats(show_all, false, None, None, json)? } - } -} - -fn sha256_file(path: &Path) -> Result { - let mut file = fs::File::open(path) - .with_context(|| format!("open executable for digest: {}", path.display()))?; - let mut hasher = Sha256::new(); - let mut buffer = [0_u8; 1024 * 1024]; - loop { - let read = file - .read(&mut buffer) - .with_context(|| format!("read executable for digest: {}", path.display()))?; - if read == 0 { - break; + Commands::LocoRead { + chat_id, + count, + cursor, + since, + all, + delay_ms, + force, + } => { + eprintln!("[deprecated] 'loco-read' is now hidden. Prefer 'read' (LOCO by default)."); + commands::read::cmd_loco_read( + chat_id, + &commands::read::ReadCommandOptions { + count: count as usize, + cursor, + since, + all, + delay_ms, + force, + rest: false, + json, + }, + )? } - hasher.update(&buffer[..read]); - } - Ok(hex::encode(hasher.finalize())) -} - -#[cfg(unix)] -fn validate_auto_reply_codex_auth(path: &Path, uid: u32) -> Result<()> { - use std::os::unix::fs::MetadataExt; - - let metadata = fs::symlink_metadata(path) - .context("AutoReply reply_codex_home/auth.json is unavailable")?; - if metadata.file_type().is_symlink() - || !metadata.is_file() - || metadata.uid() != uid - || metadata.nlink() != 1 - || metadata.mode() & 0o077 != 0 - || metadata.len() == 0 - { - anyhow::bail!("AutoReply Codex auth must be a private non-empty user-owned regular file"); - } - Ok(()) -} - -fn validate_auto_reply_runner(config: &config::OpenKakaoConfig) -> Result { - let runner = config - .auto_reply - .reply_runner - .as_deref() - .context("AutoReply reply_runner must be configured explicitly")?; - let kind = config - .auto_reply - .reply_runner_kind - .as_deref() - .unwrap_or("gjc"); - if !matches!(kind, "codex" | "gjc" | "opencodex") { - anyhow::bail!("AutoReply reply_runner_kind must be codex, gjc or opencodex"); - } - let resolved = - validate_auto_reply_executable(Some(runner), "AutoReply reply_runner", kind, false)?; - let home = dirs::home_dir() - .context("resolve home directory for AutoReply reply_runner")? - .canonicalize() - .context("resolve canonical home directory for AutoReply reply_runner")?; - let resolved_path = Path::new(&resolved); - #[cfg(unix)] - { - use std::os::unix::fs::MetadataExt; - let uid = unsafe { libc::geteuid() }; - let metadata = fs::metadata(resolved_path)?; - if metadata.uid() != uid { - anyhow::bail!("AutoReply reply_runner must be owned by the current user"); + Commands::LocoMembers { chat_id } => { + eprintln!( + "[deprecated] 'loco-members' is now hidden. Prefer 'members' (LOCO by default)." + ); + commands::members::cmd_loco_members(chat_id, false, json)? + } + Commands::LocoChatinfo { chat_id } => { + eprintln!("[deprecated] 'loco-chatinfo' is now hidden. Prefer 'chatinfo'."); + commands::probe::cmd_loco_chatinfo(chat_id, json)? + } + Commands::LocoBlocked => commands::members::cmd_loco_blocked(json)?, + Commands::Probe { + method, + body, + capture_pushes, + } => commands::probe::cmd_loco_probe(&method, body.as_deref(), json, capture_pushes)?, + Commands::ProfileHints { + app_state, + app_state_diff, + local_graph, + user_id, + probe_syncmainpf, + probe_uplinkprof, + } => commands::profile::cmd_profile_hints( + app_state, + app_state_diff, + local_graph, + user_id, + probe_syncmainpf, + probe_uplinkprof, + json, + )?, + Commands::LocoProbe { method, body } => { + eprintln!("[deprecated] 'loco-probe' is now hidden. Prefer 'probe'."); + commands::probe::cmd_loco_probe(&method, body.as_deref(), json, false)? } - if resolved_path.starts_with(&home) { - let mut current = resolved_path.parent(); - while let Some(path) = current { - let metadata = fs::metadata(path)?; - if metadata.uid() != uid || metadata.mode() & 0o022 != 0 { - anyhow::bail!( - "AutoReply reply_runner parent has unsafe ownership or permissions" + Commands::LocalChats { limit, groups } => { + let reader = local_db::LocalDbReader::open()?; + if groups { + let chats = reader.list_group_chats(limit)?; + if json { + println!("{}", serde_json::to_string_pretty(&chats)?); + } else if chats.is_empty() { + println!("No group chats found in local database."); + } else { + for c in &chats { + println!( + " {} | {} | {} ({})", + c.chat_id, c.chat_type, c.title, c.members, + ); + } + println!( + "\n{} group chats (local DB, no server contact)", + chats.len() ); } - if path == home { - break; - } - current = path.parent(); + return Ok(()); } - } else { - let codex_prefix = Path::new("/opt/homebrew/lib/node_modules/@openai/codex/"); - let ocx_prefix = Path::new("/opt/homebrew/lib/node_modules/@bitkyc08/opencodex/"); - if kind == "opencodex" { - if !resolved_path.starts_with(ocx_prefix) - && !resolved_path.starts_with("/opt/homebrew/") - { - anyhow::bail!( - "AutoReply reply_runner for opencodex must be in Homebrew prefix" - ); + let chats = reader.list_chats(limit)?; + if json { + println!("{}", serde_json::to_string_pretty(&chats)?); + } else { + if chats.is_empty() { + println!("No chats found in local database."); + } else { + let chat_type_label = |t: i32| -> &'static str { + match t { + 0 => "DM", + 1 => "Group", + _ => "Other", + } + }; + for c in &chats { + let ts = chrono::Local + .timestamp_opt(c.last_updated_at, 0) + .single() + .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string()) + .unwrap_or_default(); + let unread = if c.unread_count > 0 { + format!(" [{}]", c.unread_count) + } else { + String::new() + }; + println!( + " {} | {} | {} ({}){} | {}", + c.chat_id, + chat_type_label(c.chat_type), + c.chat_name, + c.active_members_count, + unread, + ts, + ); + } + println!("\n{} chats (local DB, no server contact)", chats.len()); } - } else if kind != "codex" - || !resolved_path.starts_with(codex_prefix) - || resolved_path.file_name().and_then(|value| value.to_str()) != Some("codex") - { - anyhow::bail!( - "AutoReply reply_runner outside the current user's home must be the native Homebrew Codex CLI" - ); - } - } - } - let model = config - .auto_reply - .reply_model - .as_deref() - .unwrap_or(if kind == "codex" { - "gpt-5.6-luna" - } else if kind == "gjc" { - "google-antigravity/gemini-3.7-flash-tiered" - } else { - "" - }) - .trim() - .to_owned(); - if matches!(kind, "codex" | "gjc") - && (model.is_empty() - || model.len() > 128 - || !model - .bytes() - .all(|byte| byte.is_ascii_alphanumeric() || b"._-/".contains(&byte))) - { - anyhow::bail!("AutoReply reply_model is invalid"); - } - let reasoning_effort = config - .auto_reply - .reply_reasoning_effort - .as_deref() - .unwrap_or(if kind == "codex" { "max" } else { "medium" }) - .trim() - .to_owned(); - if kind == "codex" - && !matches!( - reasoning_effort.as_str(), - "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max" - ) - { - anyhow::bail!("AutoReply reply_reasoning_effort is invalid"); - } - let service_tier = config - .auto_reply - .reply_service_tier - .as_deref() - .unwrap_or(if kind == "codex" { - "priority" - } else { - "default" - }) - .trim() - .to_owned(); - if kind == "codex" && !matches!(service_tier.as_str(), "default" | "priority" | "flex") { - anyhow::bail!("AutoReply reply_service_tier is invalid"); - } - if kind == "codex" { - let output = Command::new(resolved_path) - .arg("--version") - .stdin(Stdio::null()) - .output() - .context("probe AutoReply Codex reply runner")?; - let version = String::from_utf8_lossy(&output.stdout); - if !output.status.success() - || !version.trim().starts_with("codex-cli ") - || !output.stderr.is_empty() - { - // Name the runner and its observed version: selecting the Codex - // model switches `reply_runner_kind` but keeps the configured - // `reply_runner`, so a gjc binary left in place is the most likely - // cause and a bare "probe failed" would hide it. - anyhow::bail!( - "AutoReply Codex reply runner version probe failed: runner={} status={} stdout={:?} stderr={:?}", - resolved_path.display(), - output.status, - version.trim(), - String::from_utf8_lossy(&output.stderr).trim() - ); - } - } - let codex_home = if kind == "codex" { - let configured = config - .auto_reply - .reply_codex_home - .as_deref() - .context("AutoReply reply_codex_home must be configured for Codex")?; - let path = Path::new(configured); - if !path.is_absolute() { - anyhow::bail!("AutoReply reply_codex_home must be an absolute path"); - } - let resolved_home = - fs::canonicalize(path).context("AutoReply reply_codex_home must already exist")?; - if !resolved_home.starts_with(&home) { - anyhow::bail!("AutoReply reply_codex_home must stay inside the current user's home"); - } - let metadata = fs::symlink_metadata(&resolved_home)?; - if !metadata.is_dir() || metadata.file_type().is_symlink() { - anyhow::bail!("AutoReply reply_codex_home must be a real directory"); - } - #[cfg(unix)] - { - use std::os::unix::fs::MetadataExt; - let uid = unsafe { libc::geteuid() }; - if metadata.uid() != uid || metadata.mode() & 0o077 != 0 { - anyhow::bail!("AutoReply reply_codex_home must be private and user-owned"); } - validate_auto_reply_codex_auth(&resolved_home.join("auth.json"), uid)?; } - Some(resolved_home.to_string_lossy().into_owned()) - } else { - None - }; - Ok(AutoReplyRunner { - path: resolved.clone(), - kind: kind.to_owned(), - model, - reasoning_effort, - service_tier, - sha256: sha256_file(resolved_path)?, - codex_home, - }) -} - -fn auto_reply_state_root(config: &config::OpenKakaoConfig) -> Result { - let root = config - .auto_reply - .state_root - .as_deref() - .map(std::path::PathBuf::from) - .unwrap_or_else(|| { - dirs::home_dir() - .map(|home| auto_reply_service::default_state_root(&home)) - .unwrap_or_else(|| std::path::PathBuf::from("auto-reply")) - }); - if !root.is_absolute() { - anyhow::bail!("AutoReply state_root must be an absolute path"); - } - Ok(root) -} - -fn auto_reply_legacy_conflict(root: &Path) -> Result<()> { - let status_path = root.join("supervisor-status.json"); - let status_metadata = match fs::symlink_metadata(&status_path) { - Ok(metadata) => Some(metadata), - Err(error) if error.kind() == std::io::ErrorKind::NotFound => None, - Err(error) => { - return Err(error).with_context(|| format!("inspect {}", status_path.display())); - } - }; - if status_metadata.is_none() { - for residual in [ - "db-watch-state.json", - "reply-state.json", - "reply-queue.sqlite3", - "reply-queue.sqlite3-wal", - "reply-queue.sqlite3-shm", - ] { - let path = root.join(residual); - if fs::symlink_metadata(&path).is_ok() { - anyhow::bail!( - "legacy AutoReply residual state exists without a drained supervisor status: {}", - path.display() - ); - } - } - return Ok(()); - } - if status_metadata.as_ref().is_some_and(|metadata| { - metadata.file_type().is_symlink() || !metadata.file_type().is_file() - }) { - anyhow::bail!("existing AutoReply supervisor status is not a regular file"); - } - let value = read_bounded_json_file(&status_path)?; - let active_state = value.get("state").and_then(serde_json::Value::as_str); - if matches!( - active_state, - Some("starting" | "running" | "stopping" | "fenced") - ) { - anyhow::bail!( - "existing AutoReply supervisor is running at {}; stop/drain it before CLI activation", - status_path.display() - ); - } - if active_state == Some("stopped") - && value.get("legacy_drained") != Some(&serde_json::Value::Bool(true)) - { - anyhow::bail!( - "existing AutoReply supervisor at {} is stopped but not explicitly drained; resolve terminal queue state before CLI activation", - status_path.display() - ); - } - if active_state != Some("stopped") { - anyhow::bail!( - "existing AutoReply supervisor status at {} is not a recognized drained state", - status_path.display() - ); - } - Ok(()) -} - -#[cfg(unix)] -fn walk_auto_reply_directory_no_follow(path: &Path, create: bool) -> Result { - use std::ffi::CString; - use std::os::unix::ffi::OsStrExt; - use std::os::unix::fs::{MetadataExt, OpenOptionsExt}; - use std::path::Component; - - if !path.is_absolute() { - anyhow::bail!("AutoReply directory must be an absolute path"); - } - let mut options = fs::OpenOptions::new(); - options - .read(true) - .custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_DIRECTORY); - let mut directory = options.open("/").context("open filesystem root")?; - let mut saw_component = false; - for component in path.components() { - let name = match component { - Component::RootDir => continue, - Component::Normal(name) => name, - Component::CurDir | Component::ParentDir | Component::Prefix(_) => { - anyhow::bail!("AutoReply directory path is not normalized") - } - }; - saw_component = true; - let name = CString::new(name.as_bytes()).context("AutoReply directory contains NUL")?; - let open_component = || unsafe { - libc::openat( - directory.as_raw_fd(), - name.as_ptr(), - libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_DIRECTORY, - ) - }; - let mut descriptor = open_component(); - let mut created = false; - if descriptor < 0 { - let error = std::io::Error::last_os_error(); - if !create || error.raw_os_error() != Some(libc::ENOENT) { - return Err(error).with_context(|| { - format!( - "open AutoReply directory component {}", - name.to_string_lossy() - ) - }); - } - if unsafe { libc::mkdirat(directory.as_raw_fd(), name.as_ptr(), 0o700) } != 0 { - let mkdir_error = std::io::Error::last_os_error(); - if mkdir_error.raw_os_error() != Some(libc::EEXIST) { - return Err(mkdir_error).with_context(|| { - format!( - "create AutoReply directory component {}", - name.to_string_lossy() - ) - }); - } + Commands::LocalRead { + chat_id, + count, + since, + } => { + let since_ts = util::parse_since_date(since.as_deref())?; + let reader = local_db::LocalDbReader::open()?; + let mut messages = reader.read_messages(chat_id, count, since_ts)?; + messages.reverse(); // chronological order + if json { + println!("{}", serde_json::to_string_pretty(&messages)?); } else { - created = true; - } - descriptor = open_component(); - if descriptor < 0 { - return Err(std::io::Error::last_os_error()).with_context(|| { - format!( - "open created AutoReply directory component {}", - name.to_string_lossy() - ) - }); + if messages.is_empty() { + println!("No messages found in local database for chat {}.", chat_id); + } else { + for m in &messages { + let ts = chrono::Local + .timestamp_opt(m.sent_at, 0) + .single() + .map(|dt| dt.format("%H:%M:%S").to_string()) + .unwrap_or_default(); + let sender = if m.sender_name.is_empty() { + format!("{}", m.author_id) + } else { + m.sender_name.clone() + }; + let type_tag = util::message_type_label(m.message_type); + if m.message_type == 1 { + println!(" [{}] {}: {}", ts, sender, m.message); + } else { + println!(" [{}] {}: [{}] {}", ts, sender, type_tag, m.message); + } + } + println!( + "\n{} messages (local DB, no server contact)", + messages.len() + ); + } } } - // SAFETY: openat returned a newly owned descriptor on success. - let next = unsafe { std::fs::File::from_raw_fd(descriptor) }; - let metadata = next.metadata()?; - if !metadata.file_type().is_dir() || metadata.nlink() == 0 { - anyhow::bail!("AutoReply directory component is unsafe"); - } - if created { - let uid = unsafe { libc::geteuid() }; - if metadata.uid() != uid || unsafe { libc::fchmod(next.as_raw_fd(), 0o700) } != 0 { - anyhow::bail!("created AutoReply directory component is unsafe"); - } - let private = next.metadata()?; - if private.uid() != uid || private.mode() & 0o777 != 0o700 { - anyhow::bail!("created AutoReply directory component is not private"); + Commands::LocalPoll { + chat_id, + count, + interval, + } => { + let reader = local_db::LocalDbReader::open()?; + loop { + let envelope = reader.poll(chat_id, count)?; + println!("{}", serde_json::to_string(&envelope)?); + io::stdout().flush()?; + std::thread::sleep(std::time::Duration::from_secs_f64(interval)); } } - directory = next; - } - if !saw_component { - anyhow::bail!("AutoReply state root cannot be the filesystem root"); - } - Ok(directory) -} - -#[cfg(unix)] -fn open_private_auto_reply_directory( - path: &Path, - label: &str, - create: bool, -) -> Result { - use std::os::unix::fs::MetadataExt; - - if !path.is_absolute() { - anyhow::bail!("AutoReply {label} must be an absolute path"); - } - let directory = walk_auto_reply_directory_no_follow(path, create) - .with_context(|| format!("open AutoReply {label} {}", path.display()))?; - let uid = unsafe { libc::geteuid() }; - let initial = directory - .metadata() - .with_context(|| format!("inspect open AutoReply {label} {}", path.display()))?; - if !initial.file_type().is_dir() || initial.uid() != uid || initial.nlink() == 0 { - anyhow::bail!("AutoReply {label} is unsafe: {}", path.display()); - } - if unsafe { libc::fchmod(directory.as_raw_fd(), 0o700) } != 0 { - return Err(std::io::Error::last_os_error()) - .with_context(|| format!("privatize AutoReply {label} {}", path.display())); - } - let private = directory.metadata()?; - let current_directory = walk_auto_reply_directory_no_follow(path, false) - .with_context(|| format!("revalidate AutoReply {label} {}", path.display()))?; - let current = current_directory.metadata()?; - if !private.file_type().is_dir() - || private.uid() != uid - || private.nlink() == 0 - || private.mode() & 0o777 != 0o700 - || !current.file_type().is_dir() - || current.uid() != uid - || current.nlink() == 0 - || current.mode() & 0o777 != 0o700 - || (private.dev(), private.ino()) != (current.dev(), current.ino()) - { - anyhow::bail!("AutoReply {label} is not private: {}", path.display()); - } - Ok(directory) -} - -#[cfg(not(unix))] -fn ensure_private_auto_reply_directory(path: &Path, label: &str, create: bool) -> Result<()> { - if !path.is_absolute() { - anyhow::bail!("AutoReply {label} must be an absolute path"); - } - if create { - fs::create_dir_all(path) - .with_context(|| format!("create AutoReply {label} {}", path.display()))?; - } - let metadata = fs::symlink_metadata(path) - .with_context(|| format!("inspect AutoReply {label} {}", path.display()))?; - if metadata.file_type().is_symlink() || !metadata.file_type().is_dir() { - anyhow::bail!( - "AutoReply {label} must be a real directory: {}", - path.display() - ); - } - Ok(()) -} - -fn acquire_auto_reply_owner_lock(root: &Path) -> Result { - #[cfg(unix)] - let root_directory = open_private_auto_reply_directory(root, "state root", true)?; - #[cfg(not(unix))] - ensure_private_auto_reply_directory(root, "state root", true)?; - let path = root.join("supervisor.owner.lock"); - - #[cfg(unix)] - let file = { - use std::ffi::CString; - use std::os::unix::ffi::OsStrExt; - - let name = CString::new( - path.file_name() - .context("AutoReply owner lock name missing")? - .as_bytes(), - ) - .context("AutoReply owner lock name contains NUL")?; - let descriptor = unsafe { - libc::openat( - root_directory.as_raw_fd(), - name.as_ptr(), - libc::O_RDWR | libc::O_CREAT | libc::O_CLOEXEC | libc::O_NOFOLLOW, - 0o600, - ) - }; - if descriptor < 0 { - return Err(std::io::Error::last_os_error()) - .with_context(|| format!("open AutoReply owner lock {}", path.display())); - } - // SAFETY: openat returned a newly owned descriptor on success. - unsafe { std::fs::File::from_raw_fd(descriptor) } - }; - - #[cfg(not(unix))] - let file = { - let mut options = fs::OpenOptions::new(); - options.create(true).truncate(false).read(true).write(true); - options - .open(&path) - .with_context(|| format!("open AutoReply owner lock {}", path.display()))? - }; - #[cfg(unix)] - { - use std::os::unix::fs::MetadataExt; - let metadata = file - .metadata() - .with_context(|| format!("inspect AutoReply owner lock {}", path.display()))?; - let uid = unsafe { libc::geteuid() }; - if !metadata.file_type().is_file() || metadata.uid() != uid || metadata.nlink() != 1 { - anyhow::bail!("AutoReply owner lock is unsafe: {}", path.display()); - } - if unsafe { libc::fchmod(file.as_raw_fd(), 0o600) } != 0 { - return Err(std::io::Error::last_os_error()) - .with_context(|| format!("privatize AutoReply owner lock {}", path.display())); - } - let metadata = file.metadata()?; - if metadata.mode() & 0o777 != 0o600 { - anyhow::bail!("AutoReply owner lock is not private: {}", path.display()); - } - if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } != 0 { - anyhow::bail!( - "AutoReply owner lock is held by another supervisor: {}", - path.display() - ); - } - } - Ok(file) -} - -#[cfg(unix)] -fn validate_private_auto_reply_file_at( - directory: &std::fs::File, - name: &std::ffi::CStr, - expected_identity: Option<(u64, u64)>, -) -> Result { - use std::os::unix::fs::MetadataExt; - - let descriptor = unsafe { - libc::openat( - directory.as_raw_fd(), - name.as_ptr(), - libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_NONBLOCK, - ) - }; - if descriptor < 0 { - return Err(std::io::Error::last_os_error()).context("open private AutoReply file"); - } - // SAFETY: openat returned a newly owned descriptor on success. - let file = unsafe { std::fs::File::from_raw_fd(descriptor) }; - let metadata = file.metadata()?; - let uid = unsafe { libc::geteuid() }; - let entry_descriptor = unsafe { - libc::openat( - directory.as_raw_fd(), - name.as_ptr(), - libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_NONBLOCK, - ) - }; - if entry_descriptor < 0 { - return Err(std::io::Error::last_os_error()) - .context("inspect private AutoReply file entry"); - } - // SAFETY: openat returned a newly owned descriptor on success. - let entry = unsafe { std::fs::File::from_raw_fd(entry_descriptor) }; - let entry_metadata = entry.metadata()?; - if !metadata.file_type().is_file() - || metadata.uid() != uid - || metadata.nlink() != 1 - || metadata.mode() & 0o777 != 0o600 - || !entry_metadata.file_type().is_file() - || entry_metadata.uid() != uid - || entry_metadata.nlink() != 1 - || entry_metadata.mode() & 0o777 != 0o600 - || (metadata.dev(), metadata.ino()) != (entry_metadata.dev(), entry_metadata.ino()) - || expected_identity.is_some_and(|identity| identity != (metadata.dev(), metadata.ino())) - { - anyhow::bail!("unsafe private AutoReply file"); - } - Ok(file) -} - -#[cfg(unix)] -fn write_private_auto_reply_enrollment(root: &Path, bytes: &[u8]) -> Result<()> { - use rand::RngCore; - use std::ffi::CString; - use std::os::unix::fs::MetadataExt; - - let directory = open_private_auto_reply_directory(root, "state root", false)?; - let final_name = CString::new("enrollment.json").expect("static enrollment name"); - - // Refuse rather than silently normalize an existing unsafe enrollment - // authority. Replacing a symlink or hard link would obscure evidence that - // another pathname had become part of the foreground safety boundary. - let existing_descriptor = unsafe { - libc::openat( - directory.as_raw_fd(), - final_name.as_ptr(), - libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_NONBLOCK, - ) - }; - if existing_descriptor >= 0 { - // SAFETY: openat returned a newly owned descriptor on success. - let existing = unsafe { std::fs::File::from_raw_fd(existing_descriptor) }; - drop(existing); - validate_private_auto_reply_file_at(&directory, &final_name, None) - .context("existing AutoReply enrollment authority is unsafe")?; - } else { - let error = std::io::Error::last_os_error(); - if error.raw_os_error() != Some(libc::ENOENT) { - return Err(error).context("inspect existing AutoReply enrollment authority"); - } - } - - let mut random = [0_u8; 16]; - rand::thread_rng().fill_bytes(&mut random); - let temporary_name = CString::new(format!( - ".enrollment.{}.{}.tmp", - std::process::id(), - hex::encode(random) - ))?; - let descriptor = unsafe { - libc::openat( - directory.as_raw_fd(), - temporary_name.as_ptr(), - libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_CLOEXEC | libc::O_NOFOLLOW, - 0o600, - ) - }; - if descriptor < 0 { - return Err(std::io::Error::last_os_error()) - .context("create private AutoReply enrollment temporary file"); - } - // SAFETY: openat returned a newly owned descriptor on success. - let mut output = unsafe { std::fs::File::from_raw_fd(descriptor) }; - let result = (|| -> Result<()> { - if unsafe { libc::fchmod(output.as_raw_fd(), 0o600) } != 0 { - return Err(std::io::Error::last_os_error()) - .context("privatize AutoReply enrollment temporary file"); - } - let metadata = output.metadata()?; - let uid = unsafe { libc::geteuid() }; - if !metadata.file_type().is_file() - || metadata.uid() != uid - || metadata.nlink() != 1 - || metadata.mode() & 0o777 != 0o600 - { - anyhow::bail!("AutoReply enrollment temporary file is unsafe"); - } - let identity = (metadata.dev(), metadata.ino()); - output.write_all(bytes)?; - output.sync_all()?; - validate_private_auto_reply_file_at(&directory, &temporary_name, Some(identity))?; - if unsafe { - libc::renameat( - directory.as_raw_fd(), - temporary_name.as_ptr(), - directory.as_raw_fd(), - final_name.as_ptr(), - ) - } != 0 - { - return Err(std::io::Error::last_os_error()) - .context("install private AutoReply enrollment authority"); - } - validate_private_auto_reply_file_at(&directory, &final_name, Some(identity))?; - directory.sync_all()?; - Ok(()) - })(); - if result.is_err() { - unsafe { - libc::unlinkat(directory.as_raw_fd(), temporary_name.as_ptr(), 0); - } - } - result -} - -#[cfg(not(unix))] -fn write_private_auto_reply_enrollment(root: &Path, bytes: &[u8]) -> Result<()> { - ensure_private_auto_reply_directory(root, "state root", false)?; - let path = root.join("enrollment.json"); - let tmp = root.join(format!(".enrollment.{}.tmp", std::process::id())); - fs::write(&tmp, bytes)?; - fs::rename(tmp, path)?; - Ok(()) -} - -fn write_auto_reply_aggregate( - root: &Path, - targets: &[local_db::LocalChat], - children: &[Child], - state: &str, -) -> Result<()> { - let now = chrono::Utc::now(); - let now_unix = now.timestamp_millis() as f64 / 1_000.0; - let summaries = targets - .iter() - .zip(children.iter()) - .map(|(target, child)| { - let room_root = root.join("rooms").join(target.chat_id.to_string()); - let status_path = room_root.join("supervisor-status.json"); - let status = read_bounded_json_file(&status_path).ok(); - let status_target_matches = status.as_ref().is_some_and(|value| { - value.get("target_chat_id") == Some(&serde_json::json!(target.chat_id)) - && value.get("target_chat_name") - == Some(&serde_json::json!(target.chat_name)) - }); - let heartbeat_age_seconds = status - .as_ref() - .and_then(|value| value.get("updated_at")) - .and_then(serde_json::Value::as_f64) - .filter(|stamp| stamp.is_finite()) - .map(|stamp| now_unix - stamp); - let ready = state == "running" - && status_target_matches - && status.as_ref().is_some_and(|value| { - value.get("state") == Some(&serde_json::json!("running")) - && value.get("readiness") == Some(&serde_json::json!("ready")) - && value - .get("fence_reason") - .is_none_or(|reason| reason.as_str().is_some_and(str::is_empty)) - }) - && heartbeat_age_seconds - .is_some_and(|age| (-5.0..=15.0).contains(&age)); - serde_json::json!({ - "chat_id": target.chat_id, - "chat_name": target.chat_name, - "pid": child.id(), - "room_state_root": room_root, - "ready": ready, - "state": status.as_ref().and_then(|value| value.get("state")).cloned(), - "readiness": status.as_ref().and_then(|value| value.get("readiness")).cloned(), - "fence_reason": status.as_ref().and_then(|value| value.get("fence_reason")).cloned(), - "heartbeat_age_seconds": heartbeat_age_seconds, - "status_available": status.is_some(), - "target_identity_matches": status_target_matches, - }) - }) - .collect::>(); - let ready_count = summaries - .iter() - .filter(|summary| summary.get("ready") == Some(&serde_json::Value::Bool(true))) - .count(); - let all_ready = state == "running" && !summaries.is_empty() && ready_count == summaries.len(); - let payload = serde_json::json!({ - "schema_version": 2, - "state": state, - "readiness": if all_ready { "ready" } else if state == "running" { "starting" } else { "fenced" }, - "authoritative": all_ready, - "updated_at": now.to_rfc3339(), - "updated_at_unix": now_unix, - "room_count": summaries.len(), - "ready_room_count": ready_count, - "targets": summaries, - }); - let path = root.join("aggregate-status.json"); - let tmp = root.join(format!(".aggregate-status.{}.tmp", std::process::id())); - let mut options = fs::OpenOptions::new(); - options.create(true).truncate(true).write(true); - #[cfg(unix)] - { - use std::os::unix::fs::OpenOptionsExt; - options - .mode(0o600) - .custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW); - } - let mut output = options - .open(&tmp) - .with_context(|| format!("open aggregate status temporary file {}", tmp.display()))?; - output.write_all(&serde_json::to_vec(&payload)?)?; - output.write_all(b"\n")?; - output.sync_all()?; - drop(output); - fs::rename(tmp, path)?; - fs::File::open(root)?.sync_all()?; - Ok(()) -} - -fn is_empty_json_array(value: Option<&serde_json::Value>) -> bool { - value - .and_then(serde_json::Value::as_array) - .is_some_and(Vec::is_empty) -} - -const AUTO_REPLY_QUEUE_LEGACY_USER_VERSION: i64 = 0; -const AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION: i64 = 2; -const AUTO_REPLY_QUEUE_JOURNAL_MAX_ROWS: i64 = 4096; - -const AUTO_REPLY_REPLY_JOBS_TABLE_SQL: &str = r#" -CREATE TABLE reply_jobs( - event_id TEXT PRIMARY KEY, - event_json TEXT NOT NULL, - status TEXT NOT NULL, - due_at REAL, - decision TEXT, - reason TEXT, - category TEXT, - reply TEXT, - scheduled_delay_seconds REAL, - error_class TEXT, - created_at REAL NOT NULL, - updated_at REAL NOT NULL -); -"#; -const AUTO_REPLY_REPLY_JOBS_V2_TABLE_SQL: &str = r#" -CREATE TABLE reply_jobs( - event_id TEXT PRIMARY KEY, - event_json TEXT NOT NULL, - status TEXT NOT NULL, - due_at REAL, - decision TEXT, - reason TEXT, - category TEXT, - reply TEXT, - scheduled_delay_seconds REAL, - error_class TEXT, - created_at REAL NOT NULL, - updated_at REAL NOT NULL, - attempt_no INTEGER NOT NULL DEFAULT 0 CHECK(attempt_no BETWEEN 0 AND 1000000) -); -"#; -const AUTO_REPLY_REPLY_JOBS_STATUS_INDEX_SQL: &str = - "CREATE INDEX idx_reply_jobs_status_due ON reply_jobs(status, due_at);"; -const AUTO_REPLY_REPLY_JOB_TOMBSTONES_TABLE_SQL: &str = r#" -CREATE TABLE reply_job_tombstones( - event_id TEXT PRIMARY KEY, - status TEXT NOT NULL, - archived_at REAL NOT NULL -); -"#; -const AUTO_REPLY_REPLY_JOB_SUPERSESSIONS_TABLE_SQL: &str = r#" -CREATE TABLE reply_job_supersessions( - event_id TEXT PRIMARY KEY, - superseded_by_event_id TEXT NOT NULL, - created_at REAL NOT NULL, - CHECK(event_id <> superseded_by_event_id) -); -"#; -const AUTO_REPLY_MODEL_CIRCUIT_BREAKER_TABLE_SQL: &str = r#" -CREATE TABLE model_circuit_breaker( - model_key TEXT PRIMARY KEY, - state TEXT NOT NULL, - failure_class TEXT NOT NULL, - consecutive_failures INTEGER NOT NULL, - open_until REAL NOT NULL, - lease_token TEXT, - updated_at REAL NOT NULL -); -"#; - -const AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL: &str = r#" -CREATE TABLE pipeline_transitions( - seq INTEGER PRIMARY KEY, - schema_version INTEGER NOT NULL CHECK(schema_version = 1), - event_id TEXT NOT NULL CHECK( - length(event_id) BETWEEN 6 AND 80 - AND substr(event_id, 1, 3) = 'db:' - AND instr(substr(event_id, 4), ':') BETWEEN 2 AND 20 - AND instr( - substr(substr(event_id, 4), instr(substr(event_id, 4), ':') + 1), - ':' - ) = 0 - AND substr(event_id, 4, 1) BETWEEN '1' AND '9' - AND substr( - substr(event_id, 4), - instr(substr(event_id, 4), ':') + 1, - 1 - ) BETWEEN '1' AND '9' - AND substr( - substr(event_id, 4), - 1, - instr(substr(event_id, 4), ':') - 1 - ) NOT GLOB '*[^0-9]*' - AND substr( - substr(event_id, 4), - instr(substr(event_id, 4), ':') + 1 - ) NOT GLOB '*[^0-9]*' - AND CAST( - substr(substr(event_id, 4), 1, instr(substr(event_id, 4), ':') - 1) - AS INTEGER - ) BETWEEN 1 AND 9223372036854775806 - AND CAST( - substr(substr(event_id, 4), instr(substr(event_id, 4), ':') + 1) - AS INTEGER - ) BETWEEN 1 AND 9223372036854775806 - ), - attempt_no INTEGER NOT NULL CHECK(attempt_no BETWEEN 0 AND 1000000), - component TEXT NOT NULL CHECK(component IN ('authorization','ax','burst','context','delay','ingress','media','model','pre_send','projection','queue','recovery','terminal')), - from_state TEXT NOT NULL CHECK(from_state IN ('acknowledging','deferred','delivery_unknown','detected','failed','hooking','idle','none','pending','poison','processing','projection_pending','ready','reconcile_required','scheduled','sending','sent','skipped')), - to_state TEXT NOT NULL CHECK(to_state IN ('acknowledging','deferred','delivery_unknown','detected','failed','hooking','idle','none','pending','poison','processing','projection_pending','ready','reconcile_required','scheduled','sending','sent','skipped')), - code TEXT NOT NULL CHECK(code IN ('authorization_allowed','authorization_rejected','ax_mutation_authorized','candidate_persisted','context_lookup','cursor_advance_persisting','cursor_advanced','custom_redacted','delay_scheduled','enqueued','hook_ack_received','hook_dispatch_intent','local_db_confirmed','media_acquire_failed','media_acquire_ready','media_acquire_started','media_policy_rejected','model_call','model_result','pre_send_check','projection_written','reconciled','recovery_completed','recovery_started','status_changed','terminal_committed')), - source_epoch INTEGER CHECK(source_epoch IS NULL OR source_epoch BETWEEN 1 AND 9223372036854775806), - occurred_at_ns INTEGER NOT NULL CHECK(occurred_at_ns BETWEEN 1 AND 9223372036854775806) -); -"#; - -const AUTO_REPLY_PIPELINE_TRANSITIONS_INDEX_SQL: &str = - "CREATE INDEX idx_pipeline_transitions_event_seq ON pipeline_transitions(event_id, seq);"; - -const AUTO_REPLY_PIPELINE_TRANSITIONS_INSERT_TRIGGER_SQL: &str = r#" -CREATE TRIGGER trg_reply_jobs_transition_insert -AFTER INSERT ON reply_jobs -BEGIN - INSERT INTO pipeline_transitions( - schema_version,event_id,attempt_no,component,from_state,to_state, - code,source_epoch,occurred_at_ns - ) VALUES( - 1,NEW.event_id,NEW.attempt_no,'queue','none',NEW.status, - 'enqueued',CASE WHEN json_valid(NEW.event_json) AND json_type(NEW.event_json, '$.source_epoch') = 'integer' AND json_extract(NEW.event_json, '$.source_epoch') BETWEEN 1 AND 9223372036854775806 THEN json_extract(NEW.event_json, '$.source_epoch') ELSE NULL END,CAST((julianday('now') - 2440587.5) * 86400000000000 AS INTEGER) - ); -END; -"#; - -const AUTO_REPLY_PIPELINE_TRANSITIONS_UPDATE_TRIGGER_SQL: &str = r#" -CREATE TRIGGER trg_reply_jobs_transition_update -AFTER UPDATE OF status ON reply_jobs -WHEN OLD.status IS NOT NEW.status -BEGIN - UPDATE reply_jobs - SET attempt_no = OLD.attempt_no + 1 - WHERE event_id = NEW.event_id AND NEW.status = 'processing'; - INSERT INTO pipeline_transitions( - schema_version,event_id,attempt_no,component,from_state,to_state, - code,source_epoch,occurred_at_ns - ) VALUES( - 1,NEW.event_id, - CASE WHEN NEW.status = 'processing' THEN OLD.attempt_no + 1 - ELSE NEW.attempt_no END, - 'queue',OLD.status,NEW.status,'status_changed', - CASE WHEN json_valid(NEW.event_json) AND json_type(NEW.event_json, '$.source_epoch') = 'integer' AND json_extract(NEW.event_json, '$.source_epoch') BETWEEN 1 AND 9223372036854775806 THEN json_extract(NEW.event_json, '$.source_epoch') ELSE NULL END,CAST((julianday('now') - 2440587.5) * 86400000000000 AS INTEGER) - ); -END; -"#; - -const AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL: &str = r#" -CREATE TRIGGER trg_pipeline_transitions_cap -AFTER INSERT ON pipeline_transitions -BEGIN - DELETE FROM pipeline_transitions - WHERE seq < COALESCE( - (SELECT seq FROM pipeline_transitions ORDER BY seq DESC - LIMIT 1 OFFSET 4095), - 0 - ); -END; -"#; - -/// Canonicalize SQLite schema text without weakening quoted literals. SQLite -/// preserves the submitted DDL in `sqlite_master`, so comparing normalized -/// bodies detects trigger/check/index tampering while ignoring formatting and -/// SQL-keyword case only. -fn normalize_sqlite_schema_sql(sql: &str) -> String { - let mut normalized = String::with_capacity(sql.len()); - let mut quote = None; - let mut pending_space = false; - let mut characters = sql.chars().peekable(); - while let Some(character) = characters.next() { - if let Some(terminator) = quote { - normalized.push(character); - if character == terminator { - if matches!(terminator, '\'' | '"' | '`') && characters.peek() == Some(&terminator) - { - normalized.push(characters.next().expect("peeked quoted character")); + Commands::LocalSearch { + query, + count, + chat_id, + } => { + let reader = local_db::LocalDbReader::open()?; + let results = reader.search_messages(&query, count, chat_id)?; + if json { + println!("{}", serde_json::to_string_pretty(&results)?); + } else { + if results.is_empty() { + println!("No messages matching '{}' in local database.", query); } else { - quote = None; + for m in &results { + let ts = chrono::Local + .timestamp_opt(m.sent_at, 0) + .single() + .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string()) + .unwrap_or_default(); + let sender = if m.sender_name.is_empty() { + format!("{}", m.author_id) + } else { + m.sender_name.clone() + }; + println!( + " [{}] chat={} {}: {}", + ts, + m.chat_id, + sender, + util::truncate(&m.message, 80) + ); + } + println!("\n{} results (local DB, no server contact)", results.len()); } } - continue; } - match character { - '\'' | '"' | '`' => { - if pending_space - && !normalized.is_empty() - && !normalized.ends_with(['(', ',', '=', '<', '>', ';', '+', '*']) - { - normalized.push(' '); - } - pending_space = false; - quote = Some(character); - normalized.push(character); - } - '[' => { - if pending_space - && !normalized.is_empty() - && !normalized.ends_with(['(', ',', '=', '<', '>', ';', '+', '*']) - { - normalized.push(' '); - } - pending_space = false; - quote = Some(']'); - normalized.push(character); - } - value if value.is_ascii_whitespace() => pending_space = true, - value if matches!(value, '(' | ')' | ',' | '=' | '<' | '>' | ';' | '+' | '*') => { - while normalized.ends_with(' ') { - normalized.pop(); + Commands::LocalSchema => { + let reader = local_db::LocalDbReader::open()?; + let tables = reader.schema()?; + if json { + let items: Vec = tables + .iter() + .map(|(name, sql)| serde_json::json!({"name": name, "sql": sql})) + .collect(); + println!("{}", serde_json::to_string_pretty(&items)?); + } else { + for (name, sql) in &tables { + println!("-- {}", name); + println!("{}\n", sql); } - normalized.push(value); - pending_space = false; } - value => { - if pending_space - && !normalized.is_empty() - && !normalized.ends_with(['(', ',', '=', '<', '>', ';', '+', '*']) - { - normalized.push(' '); - } - pending_space = false; - normalized.push(value.to_ascii_lowercase()); + } + Commands::ContextIndex { + input, + chat, + db, + queue, + } => { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let queue_path = queue.as_deref().map(std::path::Path::new); + let count = openkakao_cli::context::index_csv_with_queue( + &db_path, + &chat, + std::path::Path::new(&input), + queue_path, + )?; + if json { + println!( + "{}", + serde_json::json!({ + "action": "index", + "chat": chat, + "input": openkakao_cli::context::provenance_id(&input), + "db": openkakao_cli::context::provenance_id( + &db_path.display().to_string() + ), + "messages": count, + "network": false + }) + ); + } else { + println!( + "Indexed {} messages for '{}' into {} (offline).", + count, + chat, + db_path.display() + ); } } - } - while normalized.ends_with(';') { - normalized.pop(); - } - normalized -} - -fn parse_auto_reply_transition_event_id(value: &str) -> Option<(i64, i64)> { - if !(6..=80).contains(&value.len()) || !value.is_ascii() { - return None; - } - let mut fields = value.split(':'); - if fields.next() != Some("db") { - return None; - } - let chat_id = fields.next()?; - let log_id = fields.next()?; - if fields.next().is_some() { - return None; - } - let parse_field = |field: &str| { - (!field.is_empty() - && field.len() <= 19 - && field.as_bytes()[0].is_ascii_digit() - && field.as_bytes()[0] != b'0' - && field.bytes().all(|byte| byte.is_ascii_digit()) - && field - .parse::() - .ok() - .is_some_and(|number| (1..i64::MAX).contains(&number))) - .then(|| field.parse::().expect("validated decimal i64")) - }; - Some((parse_field(chat_id)?, parse_field(log_id)?)) -} - -fn validate_stopped_clean_queue(queue_path: &Path, expected_chat_id: i64) -> Result<()> { - const MAX_QUEUE_BYTES: u64 = 64 * 1024 * 1024; - let expected_room_name = expected_chat_id.to_string(); - if !(1..i64::MAX).contains(&expected_chat_id) - || queue_path - .parent() - .and_then(Path::file_name) - .and_then(|value| value.to_str()) - != Some(expected_room_name.as_str()) - { - anyhow::bail!("AutoReply stopped-clean queue room binding is invalid"); - } - validate_private_regular_file(queue_path, MAX_QUEUE_BYTES)?; - let connection = rusqlite::Connection::open_with_flags( - queue_path, - rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX, - ) - .with_context(|| format!("open stopped-clean queue {}", queue_path.display()))?; - connection.execute_batch("PRAGMA query_only = ON; PRAGMA busy_timeout = 5000;")?; - let quick_check: String = connection.query_row("PRAGMA quick_check", [], |row| row.get(0))?; - if quick_check != "ok" { - anyhow::bail!("AutoReply stopped-clean queue quick_check failed"); - } - let user_version: i64 = connection.query_row("PRAGMA user_version", [], |row| row.get(0))?; - let has_transition_journal = match user_version { - AUTO_REPLY_QUEUE_LEGACY_USER_VERSION => false, - AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION => true, - _ => anyhow::bail!("AutoReply stopped-clean queue version is unsupported"), - }; - let tables = connection - .prepare( - "SELECT name FROM sqlite_master \ - WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name", - )? - .query_map([], |row| row.get::<_, String>(0))? - .collect::>>()?; - let legacy_tables = vec![ - "reply_job_supersessions".to_string(), - "reply_job_tombstones".to_string(), - "reply_jobs".to_string(), - ]; - let mut legacy_tables_with_breaker = legacy_tables.clone(); - legacy_tables_with_breaker.insert(0, "model_circuit_breaker".to_string()); - let mut expected_tables = legacy_tables.clone(); - let mut expected_tables_with_breaker = legacy_tables_with_breaker.clone(); - if has_transition_journal { - expected_tables.insert(0, "pipeline_transitions".to_string()); - expected_tables_with_breaker.insert(1, "pipeline_transitions".to_string()); - } - let has_model_circuit_breaker = if tables == expected_tables { - false - } else if tables == expected_tables_with_breaker { - true - } else { - anyhow::bail!("AutoReply stopped-clean queue schema is invalid"); - }; - let validate_columns = |table: &str, expected: &[(&str, &str, i64, i64)]| -> Result<()> { - let sql = format!("PRAGMA table_info({table})"); - let actual = connection - .prepare(&sql)? - .query_map([], |row| { - Ok(( - row.get::<_, String>(1)?, - row.get::<_, String>(2)?.to_ascii_uppercase(), - row.get::<_, i64>(3)?, - row.get::<_, i64>(5)?, - )) - })? - .collect::>>()?; - let expected = expected - .iter() - .map(|(name, kind, not_null, primary_key)| { - ( - (*name).to_string(), - (*kind).to_string(), - *not_null, - *primary_key, - ) - }) - .collect::>(); - if actual != expected { - anyhow::bail!("AutoReply stopped-clean queue schema is invalid"); - } - Ok(()) - }; - let legacy_reply_job_columns = [ - ("event_id", "TEXT", 0, 1), - ("event_json", "TEXT", 1, 0), - ("status", "TEXT", 1, 0), - ("due_at", "REAL", 0, 0), - ("decision", "TEXT", 0, 0), - ("reason", "TEXT", 0, 0), - ("category", "TEXT", 0, 0), - ("reply", "TEXT", 0, 0), - ("scheduled_delay_seconds", "REAL", 0, 0), - ("error_class", "TEXT", 0, 0), - ("created_at", "REAL", 1, 0), - ("updated_at", "REAL", 1, 0), - ]; - let mut v2_reply_job_columns = legacy_reply_job_columns.to_vec(); - v2_reply_job_columns.push(("attempt_no", "INTEGER", 1, 0)); - validate_columns( - "reply_jobs", - if has_transition_journal { - &v2_reply_job_columns - } else { - &legacy_reply_job_columns - }, - )?; - validate_columns( - "reply_job_tombstones", - &[ - ("event_id", "TEXT", 0, 1), - ("status", "TEXT", 1, 0), - ("archived_at", "REAL", 1, 0), - ], - )?; - validate_columns( - "reply_job_supersessions", - &[ - ("event_id", "TEXT", 0, 1), - ("superseded_by_event_id", "TEXT", 1, 0), - ("created_at", "REAL", 1, 0), - ], - )?; - if has_model_circuit_breaker { - validate_columns( - "model_circuit_breaker", - &[ - ("model_key", "TEXT", 0, 1), - ("state", "TEXT", 1, 0), - ("failure_class", "TEXT", 1, 0), - ("consecutive_failures", "INTEGER", 1, 0), - ("open_until", "REAL", 1, 0), - ("lease_token", "TEXT", 0, 0), - ("updated_at", "REAL", 1, 0), - ], - )?; - } - if has_transition_journal { - validate_columns( - "pipeline_transitions", - &[ - ("seq", "INTEGER", 0, 1), - ("schema_version", "INTEGER", 1, 0), - ("event_id", "TEXT", 1, 0), - ("attempt_no", "INTEGER", 1, 0), - ("component", "TEXT", 1, 0), - ("from_state", "TEXT", 1, 0), - ("to_state", "TEXT", 1, 0), - ("code", "TEXT", 1, 0), - ("source_epoch", "INTEGER", 0, 0), - ("occurred_at_ns", "INTEGER", 1, 0), - ], - )?; - let table_sql: String = connection.query_row( - "SELECT sql FROM sqlite_master \ - WHERE type = 'table' AND name = 'pipeline_transitions'", - [], - |row| row.get(0), - )?; - if normalize_sqlite_schema_sql(&table_sql) - != normalize_sqlite_schema_sql(AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL) - { - anyhow::bail!("AutoReply stopped-clean journal table is invalid"); - } - } - - let schema_objects = connection - .prepare( - "SELECT type, name, sql FROM sqlite_master \ - WHERE name NOT LIKE 'sqlite_%' AND sql IS NOT NULL \ - ORDER BY type, name", - )? - .query_map([], |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, String>(1)?, - row.get::<_, String>(2)?, - )) - })? - .collect::>>()?; - let mut expected_schema_objects = vec![ - ( - "index", - "idx_reply_jobs_status_due", - AUTO_REPLY_REPLY_JOBS_STATUS_INDEX_SQL, - ), - ( - "table", - "reply_job_supersessions", - AUTO_REPLY_REPLY_JOB_SUPERSESSIONS_TABLE_SQL, - ), - ( - "table", - "reply_job_tombstones", - AUTO_REPLY_REPLY_JOB_TOMBSTONES_TABLE_SQL, - ), - ( - "table", - "reply_jobs", - if has_transition_journal { - AUTO_REPLY_REPLY_JOBS_V2_TABLE_SQL - } else { - AUTO_REPLY_REPLY_JOBS_TABLE_SQL - }, - ), - ]; - if has_model_circuit_breaker { - expected_schema_objects.insert( - 1, - ( - "table", - "model_circuit_breaker", - AUTO_REPLY_MODEL_CIRCUIT_BREAKER_TABLE_SQL, - ), - ); - } - if has_transition_journal { - expected_schema_objects.extend([ - ( - "index", - "idx_pipeline_transitions_event_seq", - AUTO_REPLY_PIPELINE_TRANSITIONS_INDEX_SQL, - ), - ( - "table", - "pipeline_transitions", - AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL, - ), - ( - "trigger", - "trg_pipeline_transitions_cap", - AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL, - ), - ( - "trigger", - "trg_reply_jobs_transition_insert", - AUTO_REPLY_PIPELINE_TRANSITIONS_INSERT_TRIGGER_SQL, - ), - ( - "trigger", - "trg_reply_jobs_transition_update", - AUTO_REPLY_PIPELINE_TRANSITIONS_UPDATE_TRIGGER_SQL, - ), - ]); - } - expected_schema_objects.sort_by_key(|(kind, name, _)| (*kind, *name)); - if schema_objects.len() != expected_schema_objects.len() - || schema_objects.iter().zip(&expected_schema_objects).any( - |((kind, name, sql), (expected_kind, expected_name, expected_sql))| { - kind != expected_kind - || name != expected_name - || normalize_sqlite_schema_sql(sql) != normalize_sqlite_schema_sql(expected_sql) - }, - ) - { - anyhow::bail!("AutoReply stopped-clean queue schema SQL is invalid"); - } - - let expected_index_names = if has_transition_journal { - vec![ - "idx_pipeline_transitions_event_seq", - "idx_reply_jobs_status_due", - ] - } else { - vec!["idx_reply_jobs_status_due"] - }; - let index_names = schema_objects - .iter() - .filter(|(kind, _, _)| kind == "index") - .map(|(_, name, _)| name.as_str()) - .collect::>(); - if index_names != expected_index_names { - anyhow::bail!("AutoReply stopped-clean queue index set is invalid"); - } - let status_index_columns = connection - .prepare("PRAGMA index_info(idx_reply_jobs_status_due)")? - .query_map([], |row| row.get::<_, String>(2))? - .collect::>>()?; - if status_index_columns != ["status", "due_at"] { - anyhow::bail!("AutoReply stopped-clean queue index is invalid"); - } - if has_transition_journal { - let journal_index_columns = connection - .prepare("PRAGMA index_info(idx_pipeline_transitions_event_seq)")? - .query_map([], |row| row.get::<_, String>(2))? - .collect::>>()?; - if journal_index_columns != ["event_id", "seq"] { - anyhow::bail!("AutoReply stopped-clean journal index is invalid"); - } - } - - let triggers = connection - .prepare( - "SELECT name, tbl_name, sql FROM sqlite_master \ - WHERE type = 'trigger' ORDER BY name", - )? - .query_map([], |row| { - Ok(( - row.get::<_, String>(0)?, - row.get::<_, String>(1)?, - row.get::<_, Option>(2)?, - )) - })? - .collect::>>()?; - if has_transition_journal { - let expected_triggers = [ - ( - "trg_pipeline_transitions_cap", - "pipeline_transitions", - AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL, - ), - ( - "trg_reply_jobs_transition_insert", - "reply_jobs", - AUTO_REPLY_PIPELINE_TRANSITIONS_INSERT_TRIGGER_SQL, - ), - ( - "trg_reply_jobs_transition_update", - "reply_jobs", - AUTO_REPLY_PIPELINE_TRANSITIONS_UPDATE_TRIGGER_SQL, - ), - ]; - if triggers.len() != expected_triggers.len() - || triggers.iter().zip(expected_triggers).any( - |((name, table, sql), (expected_name, expected_table, expected_sql))| { - name != expected_name - || table != expected_table - || sql.as_deref().map(normalize_sqlite_schema_sql) - != Some(normalize_sqlite_schema_sql(expected_sql)) - }, - ) - { - anyhow::bail!("AutoReply stopped-clean journal triggers are invalid"); - } - let journal_rows: i64 = - connection.query_row("SELECT COUNT(*) FROM pipeline_transitions", [], |row| { - row.get(0) - })?; - if !(0..=AUTO_REPLY_QUEUE_JOURNAL_MAX_ROWS).contains(&journal_rows) { - anyhow::bail!("AutoReply stopped-clean journal row cap is invalid"); - } - let journal_event_ids = connection - .prepare("SELECT event_id FROM pipeline_transitions ORDER BY seq")? - .query_map([], |row| row.get::<_, String>(0))? - .collect::>>()?; - if journal_event_ids.iter().any(|event_id| { - parse_auto_reply_transition_event_id(event_id) - .is_none_or(|(chat_id, _)| chat_id != expected_chat_id) - }) { - anyhow::bail!("AutoReply stopped-clean journal identity is invalid"); - } - let invalid_journal_rows: i64 = connection.query_row( - "SELECT COUNT(*) FROM pipeline_transitions WHERE \ - typeof(schema_version) != 'integer' OR schema_version != 1 OR \ - typeof(attempt_no) != 'integer' OR attempt_no NOT BETWEEN 0 AND 1000000 OR \ - component NOT IN ('authorization','ax','burst','context','delay','ingress','media','model','pre_send','projection','queue','recovery','terminal') OR \ - from_state NOT IN ('acknowledging','deferred','delivery_unknown','detected','failed','hooking','idle','none','pending','poison','processing','projection_pending','ready','reconcile_required','scheduled','sending','sent','skipped') OR \ - to_state NOT IN ('acknowledging','deferred','delivery_unknown','detected','failed','hooking','idle','none','pending','poison','processing','projection_pending','ready','reconcile_required','scheduled','sending','sent','skipped') OR \ - code NOT IN ('authorization_allowed','authorization_rejected','ax_mutation_authorized','candidate_persisted','context_lookup','cursor_advance_persisting','cursor_advanced','custom_redacted','delay_scheduled','enqueued','hook_ack_received','hook_dispatch_intent','local_db_confirmed','media_acquire_failed','media_acquire_ready','media_acquire_started','media_policy_rejected','model_call','model_result','pre_send_check','projection_written','reconciled','recovery_completed','recovery_started','status_changed','terminal_committed') OR \ - (source_epoch IS NOT NULL AND (typeof(source_epoch) != 'integer' OR source_epoch NOT BETWEEN 1 AND 9223372036854775806)) OR \ - typeof(occurred_at_ns) != 'integer' OR occurred_at_ns NOT BETWEEN 1 AND 9223372036854775806", - [], - |row| row.get(0), - )?; - if invalid_journal_rows != 0 { - anyhow::bail!("AutoReply stopped-clean journal metadata is invalid"); - } - } else if !triggers.is_empty() { - anyhow::bail!("AutoReply stopped-clean legacy queue has triggers"); - } - - let reply_identities = connection - .prepare("SELECT event_id,event_json FROM reply_jobs ORDER BY event_id")? - .query_map([], |row| { - Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)) - })? - .collect::>>()?; - for (event_id, event_json) in reply_identities { - let Some((chat_id, log_id)) = parse_auto_reply_transition_event_id(&event_id) else { - anyhow::bail!("AutoReply stopped-clean queue event identity is invalid"); - }; - if chat_id != expected_chat_id { - anyhow::bail!("AutoReply stopped-clean queue event belongs to another room"); - } - let event: serde_json::Value = serde_json::from_str(&event_json) - .context("parse stopped-clean queue event identity")?; - let event = event - .as_object() - .context("AutoReply stopped-clean queue event identity is malformed")?; - for key in ["event_id", "canonical_event_id"] { - if event - .get(key) - .is_some_and(|value| value.as_str() != Some(event_id.as_str())) - { - anyhow::bail!("AutoReply stopped-clean queue event identity mismatches"); + Commands::ContextSyncLocal { + chat_id, + chat, + db, + queue, + interest_only, + } => commands::context_sync_local::cmd_context_sync_local( + chat_id, + chat, + db, + queue, + interest_only, + json, + )?, + Commands::ContextRepairSelfSends { + chat_id, + chat, + db, + queue, + dry_run, + } => { + if chat.trim().is_empty() || chat.len() > 512 || chat.chars().any(char::is_control) { + anyhow::bail!("style repair chat name is invalid"); } - } - let event_log_id = event.get("log_id").and_then(serde_json::Value::as_i64); - let proactive = event.get("proactive") == Some(&serde_json::Value::Bool(true)); - if event - .get("chat_id") - .is_some_and(|value| value.as_i64() != Some(expected_chat_id)) - || if proactive { - event_log_id.is_some_and(|value| !(1..MAX_INT64).contains(&value)) + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + openkakao_cli::context::ensure_live_context_schema(&db_path)?; + let confirmed_sends = match queue.as_deref() { + Some(path) => openkakao_cli::context::confirmed_self_sends_from_queue( + std::path::Path::new(path), + )?, + None => Vec::new(), + }; + let report = openkakao_cli::context::repair_bot_sent_style_samples( + &db_path, + &chat, + chat_id, + &confirmed_sends, + dry_run, + )?; + if json { + println!("{}", serde_json::to_string_pretty(&report)?); } else { - event_log_id.is_some_and(|value| value != log_id) + println!( + "{}: {} matched of {} scanned rows ({} already ineligible, {} ambiguous), {} response samples and {} recipient samples dropped, {} sources rebuilt (dry_run={}).", + chat, + report.matched_rows, + report.scanned_rows, + report.already_ineligible, + report.ambiguous_rows, + report.deleted_response_samples, + report.deleted_recipient_samples, + report.rebuilt_sources.len(), + report.dry_run, + ); + for matched in &report.matches { + println!( + " [{}|{}s] {} <- {}", + matched.log_id, + matched.offset_seconds, + matched.message.chars().take(60).collect::(), + matched.matched_event_id, + ); + } } - { - anyhow::bail!("AutoReply stopped-clean queue JSON identity mismatches"); - } - } - let tombstone_identities = connection - .prepare("SELECT event_id FROM reply_job_tombstones ORDER BY event_id")? - .query_map([], |row| row.get::<_, String>(0))? - .collect::>>()?; - if tombstone_identities.iter().any(|event_id| { - parse_auto_reply_transition_event_id(event_id) - .is_none_or(|(chat_id, _)| chat_id != expected_chat_id) - }) { - anyhow::bail!("AutoReply stopped-clean queue tombstone identity is invalid"); - } - let supersession_identities = connection - .prepare( - "SELECT event_id,superseded_by_event_id \ - FROM reply_job_supersessions ORDER BY event_id", - )? - .query_map([], |row| { - Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)) - })? - .collect::>>()?; - if supersession_identities - .iter() - .any(|(event_id, successor_id)| { - [event_id, successor_id].into_iter().any(|value| { - parse_auto_reply_transition_event_id(value) - .is_none_or(|(chat_id, _)| chat_id != expected_chat_id) - }) - }) - { - anyhow::bail!("AutoReply stopped-clean queue supersession identity is invalid"); - } - if has_transition_journal { - let invalid_attempts: i64 = connection.query_row( - "SELECT COUNT(*) FROM reply_jobs WHERE \ - typeof(attempt_no) != 'integer' OR attempt_no NOT BETWEEN 0 AND 1000000", - [], - |row| row.get(0), - )?; - if invalid_attempts != 0 { - anyhow::bail!("AutoReply stopped-clean queue attempt authority is invalid"); - } - } - - for table in ["reply_jobs", "reply_job_tombstones"] { - let sql = format!( - "SELECT COUNT(*) FROM {table} \ - WHERE status IS NULL OR status NOT IN ('sent', 'skipped')" - ); - let nonterminal: i64 = connection.query_row(&sql, [], |row| row.get(0))?; - if nonterminal != 0 { - anyhow::bail!("AutoReply stopped-clean queue has nonterminal rows"); - } - } - Ok(()) -} - -fn leftover_supervisor_is_live(room_root: &Path, target: &local_db::LocalChat) -> Result { - let status_path = room_root.join("supervisor-status.json"); - validate_private_regular_file(&status_path, AUTO_REPLY_READINESS_MAX_BYTES)?; - let status = read_bounded_json_file(&status_path)?; - let state = status - .get("state") - .and_then(serde_json::Value::as_str) - .unwrap_or(""); - Ok( - status.get("schema_version") == Some(&serde_json::Value::from(1)) - && status.get("mode") - == Some(&serde_json::Value::String( - "database_authoritative".to_string(), - )) - && status.get("target_chat_id") == Some(&serde_json::Value::from(target.chat_id)) - && status.get("target_chat_name") - == Some(&serde_json::Value::String(target.chat_name.clone())) - && matches!(state, "running" | "starting" | "stopping") - && status.get("all_children_exited") != Some(&serde_json::Value::Bool(true)), - ) -} - -fn leftover_in_flight_is_absent_or_orphaned(state: &serde_json::Value) -> bool { - match state.get("in_flight_candidate") { - None | Some(serde_json::Value::Null) => true, - Some(candidate) => { - let persisted_owner = state.get("owner_id").and_then(serde_json::Value::as_str); - let in_flight_owner = candidate - .get("owner_id") - .and_then(serde_json::Value::as_str); - matches!( - (persisted_owner, in_flight_owner), - (Some(owner), Some(in_flight)) if !owner.is_empty() && owner == in_flight - ) - } - } -} - -fn leftover_supervisor_is_terminal( - room_root: &Path, - target: &local_db::LocalChat, - state: &serde_json::Value, -) -> Result<()> { - let status_path = room_root.join("supervisor-status.json"); - validate_private_regular_file(&status_path, AUTO_REPLY_READINESS_MAX_BYTES)?; - let status = read_bounded_json_file(&status_path)?; - let _owner = state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .filter(|value| !value.is_empty()) - .context("AutoReply leftover owner is invalid")?; - let _epoch = state - .get("source_epoch") - .and_then(serde_json::Value::as_i64) - .filter(|value| (1..MAX_INT64).contains(value)) - .context("AutoReply leftover epoch is invalid")?; - let child_states = status - .get("child_states") - .and_then(serde_json::Value::as_object) - .context("AutoReply leftover child states are missing")?; - if status.get("schema_version") != Some(&serde_json::Value::from(1)) - || status.get("mode") - != Some(&serde_json::Value::String( - "database_authoritative".to_string(), - )) - || status.get("state") != Some(&serde_json::Value::String("stopped".to_string())) - || status.get("shutdown_state") - != Some(&serde_json::Value::String("stopped_unclean".to_string())) - || status.get("all_children_exited") != Some(&serde_json::Value::Bool(true)) - || status - .get("owner") - .and_then(serde_json::Value::as_str) - .is_none_or(str::is_empty) - || status - .get("source_epoch") - .and_then(serde_json::Value::as_i64) - .is_none_or(|value| !(1..MAX_INT64).contains(&value)) - || status.get("target_chat_id") != Some(&serde_json::Value::from(target.chat_id)) - || status.get("target_chat_name") - != Some(&serde_json::Value::String(target.chat_name.clone())) - || ["ax_watch", "db_watch", "reply_worker"].iter().any(|role| { - child_states.get(*role) != Some(&serde_json::Value::String("exited".to_string())) - }) - { - anyhow::bail!("AutoReply leftover supervisor proof is invalid"); - } - leftover_queue_has_unknown_send(&room_root.join("reply-queue.sqlite3"), target.chat_id) -} - -fn validate_stopped_clean_supervisor( - room_root: &Path, - target: &local_db::LocalChat, - state: &serde_json::Value, -) -> Result<()> { - let status_path = room_root.join("supervisor-status.json"); - validate_private_regular_file(&status_path, AUTO_REPLY_READINESS_MAX_BYTES)?; - let status = read_bounded_json_file(&status_path)?; - let owner = state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .filter(|value| !value.is_empty()) - .context("AutoReply stopped-clean owner is invalid")?; - let epoch = state - .get("source_epoch") - .and_then(serde_json::Value::as_i64) - .filter(|value| (1..MAX_INT64).contains(value)) - .context("AutoReply stopped-clean epoch is invalid")?; - let expected_roles = ["ax_watch", "db_watch", "reply_worker"]; - let child_states = status - .get("child_states") - .and_then(serde_json::Value::as_object) - .context("AutoReply stopped-clean child states are missing")?; - if status.get("schema_version") != Some(&serde_json::Value::from(1)) - || status.get("mode") - != Some(&serde_json::Value::String( - "database_authoritative".to_string(), - )) - || status.get("state") != Some(&serde_json::Value::String("stopped".to_string())) - || status.get("shutdown_state") - != Some(&serde_json::Value::String("stopped_clean".to_string())) - || status.get("all_children_exited") != Some(&serde_json::Value::Bool(true)) - || status.get("readiness") != Some(&serde_json::Value::String("fenced".to_string())) - || status.get("fence_reason") - != Some(&serde_json::Value::String("stopped_clean".to_string())) - || status.get("owner") != Some(&serde_json::Value::String(owner.to_string())) - || status.get("source_epoch") != Some(&serde_json::Value::from(epoch)) - || status.get("target_chat_id") != Some(&serde_json::Value::from(target.chat_id)) - || status.get("target_chat_name") - != Some(&serde_json::Value::String(target.chat_name.clone())) - || child_states.len() != expected_roles.len() - || expected_roles.iter().any(|role| { - child_states.get(*role) != Some(&serde_json::Value::String("exited".to_string())) - }) - { - anyhow::bail!("AutoReply stopped-clean supervisor proof is invalid"); - } - validate_stopped_clean_queue(&room_root.join("reply-queue.sqlite3"), target.chat_id) -} - -#[derive(Debug, Clone, PartialEq, Eq)] -struct AutoReplyCursorAuthority { - kind: &'static str, - cursor_floor: i64, - attested_db_last_log_id: i64, - prior_owner_id: Option, - prior_source_epoch: Option, -} - -fn enrollment_cursor_authority_for_target( - root: &Path, - target: &local_db::LocalChat, - freshly_attested_watermark: i64, -) -> Result { - if !(0..MAX_INT64).contains(&freshly_attested_watermark) - || freshly_attested_watermark != target.last_log_id - { - anyhow::bail!("AutoReply fresh enrollment watermark is invalid"); - } - let room_root = root.join("rooms").join(target.chat_id.to_string()); - let state_path = room_root.join("db-watch-state.json"); - if let Ok(metadata) = fs::symlink_metadata(&state_path) { - if metadata.file_type().is_symlink() || !metadata.file_type().is_file() { - anyhow::bail!("AutoReply room state is not a regular file"); } - let state = read_bounded_json_file(&state_path)?; - let cursor_floor = state - .get("cursor_floor") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 <= *value && *value < MAX_INT64) - .context("AutoReply room cursor floor is invalid")?; - let acked_watermark = state - .get("acked_watermark") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 <= *value && *value < MAX_INT64) - .context("AutoReply room ACK watermark is invalid")?; - let last_observed = state - .get("last_observed_log_id") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 <= *value && *value < MAX_INT64) - .context("AutoReply room observed watermark is invalid")?; - let parse_ids = |key: &str| -> Result> { - let values = state - .get(key) - .and_then(serde_json::Value::as_array) - .filter(|values| values.len() <= 500) - .with_context(|| format!("AutoReply room {key} is invalid"))?; - let parsed = values - .iter() - .map(|value| { - value - .as_i64() - .filter(|id| 0 < *id && *id < MAX_INT64) - .with_context(|| format!("AutoReply room {key} contains an invalid ID")) - }) - .collect::>>()?; - if parsed.len() != values.len() { - anyhow::bail!("AutoReply room {key} contains duplicate IDs"); - } - Ok(parsed) - }; - let observed = parse_ids("observed_log_ids")?; - let acked = parse_ids("acked_log_ids")?; - let stopped_clean_state = state.get("capability_state") - == Some(&serde_json::Value::String("stopped_clean".to_string())) - && state.get("delivery_enabled") == Some(&serde_json::Value::Bool(false)) - && state.get("fence") == Some(&serde_json::Value::String("stopped_clean".to_string())) - && state.get("fence_reason") == Some(&serde_json::Value::String(String::new())); - let leftover_fenced_state = state.get("schema_version") - == Some(&serde_json::Value::from(3)) - && state.get("target_chat_id") == Some(&serde_json::Value::from(target.chat_id)) - && state.get("target_chat_name") - == Some(&serde_json::Value::String(target.chat_name.clone())) - && state.get("capability_state") - == Some(&serde_json::Value::String("fenced".to_string())) - && state.get("delivery_enabled") == Some(&serde_json::Value::Bool(false)) - && matches!( - state - .get("candidate_phase") - .and_then(serde_json::Value::as_str), - Some("idle" | "hooking") - ) - && leftover_in_flight_is_absent_or_orphaned(&state) - && state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .is_some_and(|value| !value.is_empty()) - && state - .get("source_epoch") - .and_then(serde_json::Value::as_i64) - .is_some_and(|value| (1..MAX_INT64).contains(&value)) - && cursor_floor <= acked_watermark - && acked_watermark == acked.iter().next_back().copied().unwrap_or(0) - && last_observed == observed.iter().next_back().copied().unwrap_or(0) - && acked.is_subset(&observed) - && acked_watermark > 0 - && acked_watermark <= freshly_attested_watermark; - // Shared shape for a leftover room whose cursor is idle and whose acked - // watermark is still the last confirmed delivery boundary. - let leftover_idle_shape_ok = matches!( - state - .get("candidate_phase") - .and_then(serde_json::Value::as_str), - // The watcher's own recovery (`_reconcile_ingress_journal`) accepts a - // candidate in any of these pre-delivery phases, so refusing to - // start on them asked for a manual reconciliation the system could - // perform itself. `sending` and later stay excluded: delivery may - // already have begun there. - Some("idle" | "pending" | "hooking" | "acknowledging") - ) && leftover_in_flight_is_absent_or_orphaned(&state) - && (is_empty_json_array(state.get("pending_gaps")) - || state.get("pending_gaps") == Some(&serde_json::json!(["reconcile_required"]))) - && state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .is_some_and(|value| !value.is_empty()) - && state - .get("source_epoch") - .and_then(serde_json::Value::as_i64) - .is_some_and(|value| (1..MAX_INT64).contains(&value)) - && cursor_floor <= acked_watermark - && acked_watermark == acked.iter().next_back().copied().unwrap_or(0) - && last_observed == observed.iter().next_back().copied().unwrap_or(0) - && acked.is_subset(&observed) - && acked_watermark > 0 - && acked_watermark <= freshly_attested_watermark; - let leftover_identity_ok = state.get("schema_version") == Some(&serde_json::Value::from(3)) - && state.get("target_chat_id") == Some(&serde_json::Value::from(target.chat_id)) - && state.get("target_chat_name") - == Some(&serde_json::Value::String(target.chat_name.clone())); - let leftover_ready_idle = leftover_identity_ok - && leftover_idle_shape_ok - && state.get("capability_state") - == Some(&serde_json::Value::String("ready".to_string())) - && state.get("delivery_enabled") == Some(&serde_json::Value::Bool(true)) - && state.get("fence") == Some(&serde_json::Value::String("ready".to_string())); - // A start that was interrupted, or that fenced on a retryable condition - // such as `context_sync_transient`, leaves the persisted capability at - // "starting". Nothing is in flight and the acked watermark is still the - // last confirmed boundary, so it is exactly as resumable as a "ready" - // leftover. Without this branch the room stayed blocked on "requires - // reconciliation before restart" until an operator intervened. - let leftover_starting_idle = leftover_identity_ok - && leftover_idle_shape_ok - && state.get("capability_state") - == Some(&serde_json::Value::String("starting".to_string())) - && state.get("delivery_enabled") == Some(&serde_json::Value::Bool(false)) - && state.get("fence") == Some(&serde_json::Value::String("starting".to_string())); - let live_supervisor = leftover_supervisor_is_live(&room_root, target).unwrap_or(false); - if live_supervisor - && state.get("schema_version") == Some(&serde_json::Value::from(3)) - && state.get("target_chat_id") == Some(&serde_json::Value::from(target.chat_id)) - && state.get("target_chat_name") - == Some(&serde_json::Value::String(target.chat_name.clone())) - && state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .is_some_and(|value| !value.is_empty()) - && state - .get("source_epoch") - .and_then(serde_json::Value::as_i64) - .is_some_and(|value| (1..MAX_INT64).contains(&value)) - && acked_watermark > 0 - && acked_watermark <= freshly_attested_watermark - { - leftover_queue_has_unknown_send( - &room_root.join("reply-queue.sqlite3"), - target.chat_id, + Commands::ContextSearch { + query, + chat, + mode, + limit, + source, + db, + } => { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let mut results = openkakao_cli::context::search( + &db_path, + chat.as_deref(), + source.as_deref(), + &query, + &mode, + limit, )?; - validate_private_regular_file(&state_path, 256 * 1024)?; - return Ok(AutoReplyCursorAuthority { - kind: AUTO_REPLY_CURSOR_LEFTOVER_KIND, - cursor_floor: acked_watermark, - attested_db_last_log_id: freshly_attested_watermark, - prior_owner_id: state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .map(str::to_string), - prior_source_epoch: state - .get("source_epoch") - .and_then(serde_json::Value::as_i64), - }); - } - if (leftover_ready_idle || leftover_starting_idle) && !live_supervisor { - leftover_supervisor_is_terminal(&room_root, target, &state)?; - validate_private_regular_file(&state_path, 256 * 1024)?; - return Ok(AutoReplyCursorAuthority { - kind: AUTO_REPLY_CURSOR_LEFTOVER_KIND, - cursor_floor: acked_watermark, - attested_db_last_log_id: freshly_attested_watermark, - prior_owner_id: state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .map(str::to_string), - prior_source_epoch: state - .get("source_epoch") - .and_then(serde_json::Value::as_i64), - }); - } - if leftover_fenced_state && !stopped_clean_state { - leftover_supervisor_is_terminal(&room_root, target, &state)?; - validate_private_regular_file(&state_path, 256 * 1024)?; - return Ok(AutoReplyCursorAuthority { - kind: AUTO_REPLY_CURSOR_LEFTOVER_KIND, - cursor_floor: acked_watermark, - attested_db_last_log_id: freshly_attested_watermark, - prior_owner_id: state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .map(str::to_string), - prior_source_epoch: state - .get("source_epoch") - .and_then(serde_json::Value::as_i64), - }); - } - if state.get("schema_version") != Some(&serde_json::Value::from(3)) - || state.get("target_chat_id") != Some(&serde_json::Value::from(target.chat_id)) - || state.get("target_chat_name") - != Some(&serde_json::Value::String(target.chat_name.clone())) - || !is_empty_json_array(state.get("pending_log_ids")) - || !is_empty_json_array(state.get("pending_gaps")) - || state.get("candidate_phase") != Some(&serde_json::Value::String("idle".to_string())) - || state.get("in_flight_candidate") != Some(&serde_json::Value::Null) - || !stopped_clean_state - || state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .is_none_or(str::is_empty) - || state - .get("source_epoch") - .and_then(serde_json::Value::as_i64) - .is_none_or(|value| !(1..MAX_INT64).contains(&value)) - || cursor_floor > acked_watermark - || observed != acked - || acked_watermark != acked.iter().next_back().copied().unwrap_or(0) - || last_observed != observed.iter().next_back().copied().unwrap_or(0) - { - anyhow::bail!( - "AutoReply room {} requires reconciliation before restart", - target.chat_name - ); - } - validate_private_regular_file(&state_path, 256 * 1024)?; - validate_stopped_clean_supervisor(&room_root, target, &state)?; - if acked_watermark > freshly_attested_watermark { - anyhow::bail!( - "AutoReply room {} database tail regressed below its authoritative ACK", - target.chat_name - ); - } - // A fresh DB/AX attestation proves which room is currently open; it - // is not an acknowledgement of rows that arrived while this watcher - // was stopped. Resume a fully attested stopped-clean generation at - // its durable ACK so local-poll can replay those intervening rows. - return Ok(AutoReplyCursorAuthority { - kind: AUTO_REPLY_CURSOR_REPLAY_KIND, - cursor_floor: acked_watermark, - attested_db_last_log_id: freshly_attested_watermark, - prior_owner_id: state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .map(str::to_string), - prior_source_epoch: state - .get("source_epoch") - .and_then(serde_json::Value::as_i64), - }); - } else if let Err(error) = fs::symlink_metadata(&state_path) { - if error.kind() != std::io::ErrorKind::NotFound { - return Err(error) - .with_context(|| format!("inspect AutoReply room state {}", state_path.display())); - } - } - - let enrollment_path = root.join("enrollment.json"); - let enrollment_metadata = match fs::symlink_metadata(&enrollment_path) { - Ok(metadata) => Some(metadata), - Err(error) if error.kind() == std::io::ErrorKind::NotFound => None, - Err(error) => { - return Err(error).with_context(|| format!("inspect {}", enrollment_path.display())); - } - }; - if let Some(metadata) = enrollment_metadata { - if metadata.file_type().is_symlink() || !metadata.file_type().is_file() { - anyhow::bail!("AutoReply enrollment is not a regular file"); - } - let enrollment = read_bounded_json_file(&enrollment_path)?; - let targets = enrollment - .get("targets") - .and_then(serde_json::Value::as_array) - .context("AutoReply enrollment targets are missing")?; - if enrollment.get("schema_version") - != Some(&serde_json::Value::from( - AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION, - )) - || targets.is_empty() - || targets.len() > 32 - { - anyhow::bail!("AutoReply enrollment authority is invalid"); - } - let matches = targets - .iter() - .filter(|item| { - item.get("chat_id").and_then(serde_json::Value::as_i64) == Some(target.chat_id) - }) - .collect::>(); - if matches.len() > 1 { - anyhow::bail!("AutoReply enrollment target is duplicated"); - } - if !matches.is_empty() { - anyhow::bail!( - "AutoReply room {} has enrollment authority but no clean v3 DB state", - target.chat_name - ); - } - } - Ok(AutoReplyCursorAuthority { - kind: AUTO_REPLY_CURSOR_FRESH_KIND, - cursor_floor: freshly_attested_watermark, - attested_db_last_log_id: freshly_attested_watermark, - prior_owner_id: None, - prior_source_epoch: None, - }) -} - -fn enrollment_floor_for_target( - root: &Path, - target: &local_db::LocalChat, - freshly_attested_watermark: i64, -) -> Result { - Ok( - enrollment_cursor_authority_for_target(root, target, freshly_attested_watermark)? - .cursor_floor, - ) -} - -fn write_auto_reply_enrollment( - root: &Path, - selector_values: &[String], - targets: &[local_db::LocalChat], - binding_evidence: &[AutoReplyBindingEvidence], - author_bindings: &std::collections::BTreeMap>, - runtime_root: &Path, -) -> Result<(Vec, String)> { - if targets.iter().any(|target| { - author_bindings - .get(&target.chat_id) - .is_none_or(|bindings| bindings.is_empty()) - }) { - anyhow::bail!("AutoReply numeric reply-author enrollment is incomplete"); - } - let fresh_watermarks = targets - .iter() - .map(|target| { - binding_evidence - .iter() - .find(|item| item.chat_id == target.chat_id) - .map_or(target.last_log_id, |item| item.attested_db_last_log_id) - }) - .collect::>(); - let cursor_authorities = targets - .iter() - .zip(fresh_watermarks.iter()) - .map(|(target, fresh_watermark)| { - enrollment_cursor_authority_for_target(root, target, *fresh_watermark) - }) - .collect::>>()?; - if targets - .iter() - .zip(cursor_authorities.iter()) - .any(|(target, authority)| { - matches!( - authority.kind, - AUTO_REPLY_CURSOR_REPLAY_KIND | AUTO_REPLY_CURSOR_LEFTOVER_KIND - ) && binding_evidence - .iter() - .all(|binding| binding.chat_id != target.chat_id) - }) - { - anyhow::bail!("AutoReply replay enrollment requires fresh AX transcript authority"); - } - let payload = serde_json::json!({ - "schema_version": AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION, - "activation": "foreground", - "selectors": selector_values, - "runtime_root": runtime_root, - "created_at": chrono::Utc::now().to_rfc3339(), - "targets": targets.iter().zip(cursor_authorities.iter()).map(|(target, authority)| { - let binding = binding_evidence.iter().find(|item| item.chat_id == target.chat_id); - let identity = if let Some(binding) = binding { - serde_json::json!({ - "schema_version": 1, - "kind": "ax_transcript", - "local_name": binding.local_name, - "ax_name": binding.ax_name, - "matched_log_ids": binding.matched_log_ids, - "matched_count": binding.matched_count, - "matched_utf8_bytes": binding.matched_utf8_bytes, - "transcript_sha256": binding.transcript_sha256, - "attested_db_last_log_id": binding.attested_db_last_log_id, - }) + if json { + openkakao_cli::context::redact_context_results(&mut results); + println!("{}", serde_json::to_string_pretty(&results)?); } else { - serde_json::json!({ - "schema_version": 1, - "kind": "local_name", - "local_name": target.chat_name, - "ax_name": target.chat_name, - }) - }; - serde_json::json!({ - "chat_id": target.chat_id, - "chat_name": target.chat_name, - "last_log_id": authority.cursor_floor, - "room_state_root": root.join("rooms").join(target.chat_id.to_string()), - "identity": identity, - "cursor_authority": { - "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, - "kind": authority.kind, - "cursor_floor": authority.cursor_floor, - "attested_db_last_log_id": authority.attested_db_last_log_id, - "prior_owner_id": authority.prior_owner_id, - "prior_source_epoch": authority.prior_source_epoch, - }, - "reply_author_bindings": author_bindings.get(&target.chat_id), - }) - }).collect::>(), - }); - let bytes = serde_json::to_vec_pretty(&payload)?; - let digest = hex::encode(Sha256::digest(&bytes)); - write_private_auto_reply_enrollment(root, &bytes)?; - Ok(( - cursor_authorities - .iter() - .map(|authority| authority.cursor_floor) - .collect(), - digest, - )) -} - -fn prepare_auto_reply_room_state(root: &Path, chat: &local_db::LocalChat) -> Result { - #[cfg(unix)] - let _root_directory = open_private_auto_reply_directory(root, "state root", false)?; - #[cfg(not(unix))] - ensure_private_auto_reply_directory(root, "state root", false)?; - let rooms_root = root.join("rooms"); - #[cfg(unix)] - let _rooms_directory = open_private_auto_reply_directory(&rooms_root, "rooms directory", true)?; - #[cfg(not(unix))] - ensure_private_auto_reply_directory(&rooms_root, "rooms directory", true)?; - let room_root = root.join("rooms").join(chat.chat_id.to_string()); - #[cfg(unix)] - let _room_directory = open_private_auto_reply_directory(&room_root, "room state", true)?; - #[cfg(not(unix))] - ensure_private_auto_reply_directory(&room_root, "room state", true)?; - Ok(room_root) -} - -struct AutoReplyChildrenGuard { - children: Vec, - disarmed: bool, -} - -impl AutoReplyChildrenGuard { - fn new(capacity: usize) -> Self { - Self { - children: Vec::with_capacity(capacity), - disarmed: false, - } - } - - fn disarm(&mut self) { - self.disarmed = true; - } - - fn stop(&mut self) { - if self.disarmed || self.children.is_empty() { - return; - } - stop_auto_reply_children(&mut self.children); - self.disarm(); - } -} - -impl Drop for AutoReplyChildrenGuard { - fn drop(&mut self) { - if !self.disarmed && !self.children.is_empty() { - stop_auto_reply_children(&mut self.children); - } - } -} - -fn stop_auto_reply_children(children: &mut [Child]) { - #[cfg(unix)] - fn process_group_alive(pid: libc::pid_t) -> bool { - unsafe { - libc::kill(-pid, 0) == 0 - || std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM) - } - } - - #[cfg(unix)] - let process_group_ids = children - .iter() - .map(|child| child.id() as libc::pid_t) - .collect::>(); - - #[cfg(unix)] - for pid in &process_group_ids { - unsafe { - if libc::kill(-*pid, libc::SIGTERM) != 0 { - libc::kill(*pid, libc::SIGTERM); - } - } - } - let deadline = std::time::Instant::now() + Duration::from_secs(3); - while std::time::Instant::now() < deadline { - let direct_children_stopped = children - .iter_mut() - .all(|child| child.try_wait().ok().flatten().is_some()); - #[cfg(unix)] - let process_groups_stopped = process_group_ids - .iter() - .all(|pid| !process_group_alive(*pid)); - #[cfg(not(unix))] - let process_groups_stopped = true; - if direct_children_stopped && process_groups_stopped { - return; - } - thread::sleep(Duration::from_millis(50)); - } - - #[cfg(unix)] - for pid in &process_group_ids { - if process_group_alive(*pid) { - unsafe { - if libc::kill(-*pid, libc::SIGKILL) != 0 { - let _ = libc::kill(*pid, libc::SIGKILL); + for result in &results { + println!( + "[{:.3}] {} [{}] {}: {}", + result.score, + result.chat, + result.date, + result.user, + util::truncate(&result.message, 160) + ); } + println!("{} results (offline {} search)", results.len(), mode); } } - } - for child in children.iter_mut() { - if child.try_wait().ok().flatten().is_none() { - #[cfg(unix)] - let _ = child.kill(); - #[cfg(not(unix))] - let _ = child.kill(); - } - let _ = child.wait(); - } - - // `SIGKILL` is asynchronous, and a grandchild that ignored `SIGTERM` is not - // our direct child: reaping the leader can therefore complete while the rest - // of the group is still exiting. Drain the groups so `stop` returns with the - // guarantee callers and tests rely on, that no descendant is left behind. - #[cfg(unix)] - { - let drain_deadline = std::time::Instant::now() + Duration::from_secs(2); - while std::time::Instant::now() < drain_deadline - && process_group_ids - .iter() - .any(|pid| process_group_alive(*pid)) - { - thread::sleep(Duration::from_millis(20)); - } - } -} - -fn install_auto_reply_signal_handlers() { - #[cfg(unix)] - unsafe { - let handler = handle_auto_reply_signal as *const () as libc::sighandler_t; - libc::signal(libc::SIGINT, handler); - libc::signal(libc::SIGTERM, handler); - } -} - -fn configure_auto_reply_process_group(command: &mut Command) { - #[cfg(unix)] - unsafe { - command.pre_exec(|| { - if libc::setpgid(0, 0) != 0 { - return Err(std::io::Error::last_os_error()); - } - Ok(()) - }); - } -} - -fn emit_auto_reply_preflight( - json_output: bool, - check: bool, - targets: &[local_db::LocalChat], - root: &Path, - valid: bool, - error: Option<&str>, - workers_started: bool, -) { - let payload = serde_json::json!({ - "command": "auto-reply", - "valid": valid, - "check": check, - "network": false, - "will_send": !check && workers_started, - "workers_started": workers_started, - "ax_runtime_readiness": if check { "not_evaluated" } else { "starting" }, - "state_root": root, - "targets": targets.iter().map(|target| serde_json::json!({ - "chat_id": target.chat_id, - "chat_name": target.chat_name, - "last_log_id": target.last_log_id, - "room_state_root": root.join("rooms").join(target.chat_id.to_string()), - })).collect::>(), - "error": error, - }); - if json_output { - println!( - "{}", - serde_json::to_string(&payload).unwrap_or_else(|_| "{}".to_string()) - ); - } else { - println!("auto-reply: {} target(s)", targets.len()); - for target in targets { - println!(" {} ({})", target.chat_name, target.chat_id); - } - if let Some(error) = error { - println!(" error: {error}"); - } else if check { - println!(" static check passed; AX runtime readiness not evaluated"); - } else if workers_started { - println!(" foreground workers started; press Ctrl-C to stop"); - } - } -} - -#[derive(Debug, Clone)] -struct AutoReplyBindingEvidence { - chat_id: i64, - ax_name: String, - local_name: String, - matched_log_ids: Vec, - matched_count: usize, - matched_utf8_bytes: usize, - transcript_sha256: String, - attested_db_last_log_id: i64, -} - -#[derive(Debug, Clone, serde::Serialize, PartialEq, Eq)] -struct AutoReplyAuthorBinding { - nickname: String, - author_id: i64, -} - -fn auto_reply_bind_author_ids( - reader: &local_db::LocalDbReader, - targets: &[local_db::LocalChat], - reply_authors_by_room: &std::collections::BTreeMap>, -) -> Result>> { - let mut by_chat = std::collections::BTreeMap::new(); - for target in targets { - let reply_authors = reply_authors_by_room - .get(&target.chat_id) - .context("AutoReply per-room reply-author allowlist is incomplete")?; - let configured = reply_authors - .iter() - .map(|nickname| nickname.trim().to_string()) - .collect::>(); - if configured.is_empty() - || configured.len() != reply_authors.len() - || configured.iter().any(|nickname| { - nickname.is_empty() - || nickname.len() > 128 - || nickname.chars().any(char::is_control) - }) - { - anyhow::bail!( - "AutoReply reply-author allowlist for room {} is invalid or duplicated", - target.chat_id - ); - } - let identities = reader.room_author_identities(target.chat_id)?; - let present = identities - .iter() - .map(|identity| identity.nickname.trim().to_string()) - .filter(|nickname| configured.contains(nickname)) - .collect::>(); - let effective = if present.is_empty() { - identities - .iter() - .filter(|identity| !identity.is_self && !identity.nickname.trim().is_empty()) - .map(|identity| identity.nickname.trim().to_string()) - .collect::>() - } else { - present - }; - if effective.is_empty() { - anyhow::bail!( - "AutoReply reply author allowlist for room {:?} has no bindable members", - target.chat_name - ); - } - let bindings = resolve_auto_reply_author_bindings( - &target.chat_name, - reader.account_user_id(), - &identities, - &effective, - )?; - by_chat.insert(target.chat_id, bindings); - } - Ok(by_chat) -} - -fn configure_auto_reply_supervisor_author_policy( - command: &mut Command, - reply_authors: &[String], - author_bindings_json: &str, -) { - command - .env("OPENKAKAO_REPLY_AUTHORS", reply_authors.join(",")) - .env("OPENKAKAO_REPLY_AUTHOR_BINDINGS", author_bindings_json); - for author in reply_authors { - command.arg("--reply-author").arg(author); - } -} - -fn configure_auto_reply_supervisor_shared_state(command: &mut Command, root: &Path) { - command - .env("OPENKAKAO_AUTO_REPLY_SEND_LOCK", root.join(".ax-send.lock")) - .env( - "OPENKAKAO_MODEL_CIRCUIT_DB", - root.join("model-circuit.sqlite3"), - ); -} - -fn resolve_auto_reply_author_bindings( - room_name: &str, - account_user_id: i64, - identities: &[local_db::LocalAuthorIdentity], - configured: &std::collections::BTreeSet, -) -> Result> { - let mut ids_by_nickname = - std::collections::BTreeMap::>::new(); - let mut nicknames_by_id = - std::collections::BTreeMap::>::new(); - for identity in identities { - if identity.author_id <= 0 - || identity.author_id == MAX_INT64 - || identity.is_self != (identity.author_id == account_user_id) - { - anyhow::bail!("AutoReply room author identity proof is inconsistent"); - } - nicknames_by_id - .entry(identity.author_id) - .or_default() - .insert(identity.nickname.clone()); - if configured.contains(&identity.nickname) { - ids_by_nickname - .entry(identity.nickname.clone()) - .or_default() - .insert(identity.author_id); - } - } - let mut bindings = Vec::new(); - let mut bound_ids = std::collections::BTreeSet::new(); - for nickname in configured { - let ids = ids_by_nickname.get(nickname).cloned().unwrap_or_default(); - if ids.is_empty() { - // A configured member simply not present in this room is skipped, - // not an error. - continue; - } - if ids.len() > 1 { - // Fail closed: a configured display name that resolves to more than - // one author ID is ambiguous and could bind the wrong person. - anyhow::bail!( - "AutoReply reply author {nickname:?} in room {room_name:?} maps to multiple author IDs" - ); - } - let author_id = *ids.iter().next().expect("one author ID"); - match nicknames_by_id.get(&author_id) { - // The bound author must carry exactly this one display name. - Some(names) if names.len() == 1 && names.contains(nickname) => {} - // Display-name drift: the same author ID appears under more than one - // name. Fail closed rather than risk binding a renamed identity. - Some(names) if names.contains(nickname) => anyhow::bail!( - "AutoReply reply author {nickname:?} (ID {author_id}) in room {room_name:?} has drifting display names" - ), - _ => continue, - } - if author_id == account_user_id { - continue; - } - if !bound_ids.insert(author_id) { - continue; - } - bindings.push(AutoReplyAuthorBinding { - nickname: nickname.clone(), - author_id, - }); - } - if bindings.is_empty() { - anyhow::bail!( - "AutoReply reply author allowlist for room {room_name:?} has no bindable members" - ); - } - Ok(bindings) -} - -/// Bound on transient read-only attestation failures. -/// -/// KakaoTalk can expose a chat window mid-repaint, where one read returns rows -/// from another view or none at all. The unattended host treated the single -/// read as final and fenced every bound room when that landed (2026-09-18 -/// flapping). Each attempt re-reads both sides and re-runs the same read-only -/// match, so the safety property is unchanged. -const AUTO_REPLY_ATTEST_ATTEMPTS: usize = 3; -const AUTO_REPLY_ATTEST_RETRY_PAUSE: std::time::Duration = std::time::Duration::from_millis(500); - -/// Run one bounded operation until it succeeds or the attempt budget is spent. -/// -/// Returns the last error so callers keep the original diagnostics, and always -/// runs at least once even when the budget is configured to zero. -fn retry_bounded( - attempts: usize, - pause: std::time::Duration, - mut op: impl FnMut() -> Result, -) -> Result { - let budget = attempts.max(1); - let mut last_error: Option = None; - for index in 0..budget { - match op() { - Ok(value) => return Ok(value), - Err(error) => { - last_error = Some(error); - if index + 1 < budget { - std::thread::sleep(pause); + Commands::ContextReplyBundle { + query, + chat, + chat_id, + current_log_id, + exclude_log_id, + recipient, + source, + db, + } => { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let output = match (recipient.as_deref(), chat_id, current_log_id) { + (Some(recipient), Some(chat_id), Some(current_log_id)) => { + let mut excluded_log_ids = Vec::with_capacity(1 + exclude_log_id.len()); + excluded_log_ids.push(current_log_id); + excluded_log_ids.extend(exclude_log_id); + openkakao_cli::context::context_reply_bundle_for_recipient_excluding_live_events_json( + &db_path, + &chat, + &query, + source.as_deref(), + recipient, + chat_id, + &excluded_log_ids, + )? } - } - } - } - Err(last_error.unwrap_or_else(|| anyhow::anyhow!("bounded retry produced no attempt"))) -} - -fn auto_reply_attest_explicit_bindings( - reader: &local_db::LocalDbReader, - selectors: &[local_db::ChatSelector], - targets: &[local_db::LocalChat], -) -> Result> { - retry_bounded( - AUTO_REPLY_ATTEST_ATTEMPTS, - AUTO_REPLY_ATTEST_RETRY_PAUSE, - || auto_reply_attest_explicit_bindings_once(reader, selectors, targets), - ) -} - -fn auto_reply_attest_explicit_bindings_once( - reader: &local_db::LocalDbReader, - selectors: &[local_db::ChatSelector], - targets: &[local_db::LocalChat], -) -> Result> { - const AX_ATTEST_COUNT: usize = 40; - let mut seen = std::collections::BTreeMap::::new(); - let mut evidence = Vec::new(); - for selector in selectors { - let local_db::ChatSelector::Binding { id, name } = selector else { - continue; - }; - if let Some(previous) = seen.insert(*id, name.clone()) { - if previous != *name { - anyhow::bail!("chat ID {id} has conflicting explicit AX bindings"); - } - continue; - } - let target = targets - .iter() - .find(|target| target.chat_id == *id && target.chat_name == *name) - .context("explicit chat binding did not resolve to one local target")?; - let ax_messages = ax_send::read_open_exact_via_ax(name, AX_ATTEST_COUNT) - .with_context(|| format!("read exact AX transcript for {name:?}"))?; - let ax_texts = ax_messages - .iter() - .map(|item| ax_send::normalize_binding_message(&item.text)) - .filter(|item| !item.is_empty()) - .collect::>(); - let mut local_messages = reader.read_messages(target.chat_id, AX_ATTEST_COUNT, None)?; - local_messages.reverse(); - let local_pairs = ax_send::normalize_local_binding_suffix(&local_messages); - let local_texts = local_pairs - .iter() - .map(|(_, token)| token.text.clone()) - .collect::>(); - let suffix_match = ax_send::match_local_binding_suffix(&ax_texts, &local_pairs); - let matched_pairs = local_pairs - .iter() - .rev() - .take(suffix_match.matched_count) - .cloned() - .collect::>(); - let matched_texts = matched_pairs - .iter() - .map(|(_, token)| token.text.clone()) - .collect::>(); - // Operator-confirmed rooms with a short transcript may pass with one - // exact row; the flag is off unless the operator sets it deliberately. - if !suffix_match.is_acceptable(openkakao_cli::ax_send::manual_attestation_enabled()) { - anyhow::bail!( - "explicit chat binding {id}:{name} failed read-only AX/local transcript attestation \ - (matched={}, distinct={}, utf8_bytes={}, ax_rows={}, local_rows={}, ax_tail={}, local_tail={})", - suffix_match.matched_count, - suffix_match.matched_distinct, - suffix_match.matched_utf8_bytes, - ax_texts.len(), - local_texts.len(), - ax_send::binding_kind_tail(&ax_texts, 4), - ax_send::binding_kind_tail(&local_texts, 4) - ); - } - let matched_log_ids = matched_pairs - .iter() - .map(|(log_id, _)| *log_id) - .collect::>(); - let mut transcript_hasher = Sha256::new(); - for (log_id, text) in matched_log_ids.iter().zip(matched_texts.iter()) { - transcript_hasher.update(log_id.to_be_bytes()); - transcript_hasher.update((text.len() as u64).to_be_bytes()); - transcript_hasher.update(text.as_bytes()); - } - evidence.push(AutoReplyBindingEvidence { - chat_id: *id, - ax_name: name.clone(), - local_name: target.database_chat_name.clone().unwrap_or_default(), - matched_log_ids, - matched_count: suffix_match.matched_count, - matched_utf8_bytes: suffix_match.matched_utf8_bytes, - transcript_sha256: hex::encode(transcript_hasher.finalize()), - attested_db_last_log_id: target.last_log_id, - }); - } - Ok(evidence) -} - -fn validate_auto_reply_context( - reader: &local_db::LocalDbReader, - targets: &[local_db::LocalChat], -) -> Result<()> { - let db_path = openkakao_cli::context::default_db_path(); - for target in targets { - let state = openkakao_cli::context::live_context_sync_state( - &db_path, - reader.account_fingerprint(), - target.chat_id, - )? - .with_context(|| { - format!( - "authoritative live context is unavailable for {:?}", - target.chat_name - ) - })?; - if !state.allows_auto_reply_startup(target.chat_id, &target.chat_name) { - anyhow::bail!( - "authoritative live context is not ready for {:?}", - target.chat_name - ); - } - // 이름 없는 그룹방은 '그룹:' 이름으로 프로파일이 쌓인다. 화면 이름으로 - // 조회가 비면 자리표시자 이름으로 한 번 더 시도한다. - let mut profile_chat = target.chat_name.clone(); - let placeholder = format!("그룹:{}", target.chat_id); - if openkakao_cli::context::style_profile( - &db_path, - &profile_chat, - "최연우", - Some(&state.source), - )? - .is_none() - && openkakao_cli::context::style_profile( - &db_path, - &placeholder, - "최연우", - Some(&state.source), - )? - .is_some() - { - profile_chat = placeholder; - } - let profile = openkakao_cli::context::style_profile( - &db_path, - &profile_chat, - "최연우", - Some(&state.source), - )? - .with_context(|| format!("style profile is unavailable for {:?}", target.chat_name))?; - if profile.source != state.source - || profile.sample_count == 0 - || profile.policy_version != openkakao_cli::context::STYLE_POLICY_VERSION - { - anyhow::bail!("style profile is invalid for {:?}", target.chat_name); - } - let timing = openkakao_cli::context::response_time_stats( - &db_path, - &profile_chat, - "최연우", - Some(&state.source), - )? - .with_context(|| { - format!( - "response-time distribution is unavailable for {:?}", - target.chat_name - ) - })?; - if timing.source != state.source - || timing.sample_count == 0 - || !timing.average_seconds.is_finite() - || !timing.median_seconds.is_finite() - || !timing.p90_seconds.is_finite() - || !timing.stddev_seconds.is_finite() - || timing.average_seconds < 0.0 - || timing.median_seconds < 0.0 - || timing.p90_seconds < 0.0 - || timing.stddev_seconds < 0.0 - { - anyhow::bail!( - "response-time distribution is invalid for {:?}", - target.chat_name - ); - } - if timing.sample_count >= 32 - && (timing.distribution.is_none() - || (timing.stddev_seconds == 0.0 - && timing.p90_seconds <= timing.average_seconds - && timing.median_seconds == timing.average_seconds)) - { - anyhow::bail!( - "response-time distribution is invalid for {:?}", - target.chat_name - ); - } - } - Ok(()) -} - -// Each parameter maps one-to-one onto a `Commands::AutoReply` CLI flag and the -// command has a single call site, so bundling them would only add a translation -// layer between clap and the run. -#[allow( - clippy::too_many_arguments, - reason = "AutoReply CLI flags map one-to-one onto the run parameters" -)] -fn run_auto_reply( - config: &config::OpenKakaoConfig, - selector_values: Vec, - check: bool, - self_nickname_override: Option, - reply_author_overrides: Vec, - interval: f64, - requested_model: Option, - json_output: bool, -) -> Result<()> { - let mut effective_config = config.clone(); - let choice = select_auto_reply_llm( - &mut effective_config, - requested_model.as_deref(), - json_output, - )?; - choice.apply(&mut effective_config); - let reply_author_override_active = !reply_author_overrides.is_empty(); - if self_nickname_override.is_some() { - effective_config.auto_reply.self_nickname = self_nickname_override; - } - if reply_author_override_active { - effective_config.auto_reply.reply_authors = reply_author_overrides.clone(); - } - let config = &effective_config; - if !interval.is_finite() || !(0.2..=60.0).contains(&interval) { - anyhow::bail!("auto-reply interval must be between 0.2 and 60 seconds"); - } - AUTO_REPLY_STOP.store(false, Ordering::Release); - AUTO_REPLY_GUARDIAN_LOST.store(false, Ordering::Release); - let _guardian_liveness = start_auto_reply_guardian_liveness_monitor(check)?; - let root = auto_reply_state_root(config)?; - let (canonical_config_path, config_digest) = match config::verify_config_attestation(config) { - Ok(attestation) => attestation, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &[], - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - }; - let reader = match local_db::LocalDbReader::open_no_mutation() { - Ok(reader) => reader, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &[], - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - }; - let chats = match reader.list_all_chats() { - Ok(chats) => chats, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &[], - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - }; - let group_titles = reader - .list_group_chats(10_000) - .unwrap_or_default() - .into_iter() - .map(|chat| (chat.chat_id, chat.title)) - .collect::>(); - let selector_values = - match auto_reply_selector_values(config, selector_values, &chats, &root, &group_titles) { - Ok(values) => values, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &[], - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - }; - let selectors = match local_db::parse_chat_selectors(&selector_values) { - Ok(selectors) => selectors, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &[], - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - }; - let targets = match local_db::resolve_chat_selectors(&chats, &selectors) { - Ok(targets) => targets, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &[], - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - }; - let group_titles = reader - .list_group_chats(10_000) - .unwrap_or_default() - .into_iter() - .map(|chat| (chat.chat_id, chat.title)) - .collect::>(); - let mut targets = targets; - for target in &mut targets { - if target.chat_name.trim().is_empty() { - if let Some(title) = group_titles.get(&target.chat_id) { - target.chat_name = title.clone(); - } else if !target.display_name.trim().is_empty() { - target.chat_name = target.display_name.clone(); - } + (None, None, None) if exclude_log_id.is_empty() => openkakao_cli::context::context_reply_bundle_json( + &db_path, + &chat, + &query, + source.as_deref(), + )?, + _ => anyhow::bail!( + "recipient, chat-id, and current-log-id must be supplied together; exclude-log-id requires that recipient bundle mode" + ), + }; + println!("{output}"); } - } - let target_names = targets - .iter() - .map(|target| target.chat_name.clone()) - .collect::>(); - let target_ids = targets - .iter() - .map(|target| target.chat_id) - .collect::>(); - if let Err(error) = config::validate_auto_reply_startup(config, &target_names, &target_ids) { - emit_auto_reply_preflight( - json_output, - check, - &targets, - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - let target_ids = targets - .iter() - .map(|target| target.chat_id) - .collect::>(); - let mut reply_authors_by_room = - match config::auto_reply_reply_authors_by_room(config, &target_ids) { - Ok(values) => values, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &targets, - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - }; - if reply_author_override_active { - let override_authors = - match config::validate_auto_reply_reply_author_override(&reply_author_overrides) { - Ok(values) => values, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &targets, - &root, - false, - Some(&error.to_string()), - false, + Commands::ContextStyleSearch { + query, + limit, + db, + chat, + } => { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let mut results = + openkakao_cli::context::style_search(&db_path, chat.as_deref(), &query, limit)?; + if json { + openkakao_cli::context::redact_context_results(&mut results); + println!("{}", serde_json::to_string_pretty(&results)?); + } else { + for result in &results { + println!( + "[{:.3}] {} [{}] {}: {}", + result.score, + result.chat, + result.date, + result.user, + util::truncate(&result.message, 160) ); - return Err(error); } - }; - for authors in reply_authors_by_room.values_mut() { - *authors = override_authors.clone(); - } - } - let binding_evidence = match auto_reply_attest_explicit_bindings(&reader, &selectors, &targets) - { - Ok(evidence) => evidence, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &targets, - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - }; - let author_bindings = - match auto_reply_bind_author_ids(&reader, &targets, &reply_authors_by_room) { - Ok(bindings) => bindings, - Err(error) => { - emit_auto_reply_preflight( - json_output, - check, - &targets, - &root, - false, - Some(&error.to_string()), - false, + println!( + "{} results (offline 최연우 style vector search)", + results.len() ); - return Err(error); } - }; - if let Err(error) = validate_auto_reply_context(&reader, &targets) { - emit_auto_reply_preflight( - json_output, - check, - &targets, - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - let binary = std::env::current_exe().context("resolve current openkakao binary")?; - let (supervisor, runtime_root) = resolve_auto_reply_supervisor(&binary)?; - let python = validate_auto_reply_executable( - config.auto_reply.python_interpreter.as_deref(), - "AutoReply python_interpreter", - "python3", - !check, - )?; - let runner = validate_auto_reply_runner(config)?; - if check { - if let Err(error) = auto_reply_legacy_conflict(&root) { - emit_auto_reply_preflight( - json_output, - true, - &targets, - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); } - for target in &targets { - let fresh_watermark = binding_evidence - .iter() - .find(|item| item.chat_id == target.chat_id) - .map_or(target.last_log_id, |item| item.attested_db_last_log_id); - if let Err(error) = enrollment_floor_for_target(&root, target, fresh_watermark) { - emit_auto_reply_preflight( - json_output, - true, - &targets, - &root, - false, - Some(&error.to_string()), - false, + Commands::ContextRecipientStyle { + chat, + recipient, + source, + db, + } => { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let profile = openkakao_cli::context::recipient_style_profile( + &db_path, + &chat, + &recipient, + source.as_deref(), + )?; + if json { + match openkakao_cli::context::recipient_style_profile_json( + &db_path, + &chat, + &recipient, + source.as_deref(), + )? { + Some(payload) => println!("{payload}"), + None => println!("null"), + } + } else if let Some(profile) = profile { + println!( + "{} -> {}: {} direct samples (fallback={}, avg {:.1} chars)", + profile.profile.user, + profile.recipient, + profile.direct_sample_count, + profile.used_fallback, + profile.profile.average_character_length + ); + } else { + println!( + "No recipient style profile for '{}' in '{}'.", + recipient, chat ); - return Err(error); } } - emit_auto_reply_preflight(json_output, true, &targets, &root, true, None, false); - return Ok(()); - } - - if let Err(error) = probe_auto_reply_llm(config, choice) { - emit_auto_reply_preflight( - json_output, - false, - &targets, - &root, - false, - Some(&error.to_string()), - false, - ); - return Err(error); - } - - auto_reply_legacy_conflict(&root)?; - let _owner_lock = acquire_auto_reply_owner_lock(&root)?; - let self_nickname = config::auto_reply_self_nickname(config) - .context("AutoReply self nickname is not configured")?; - let (enrollment_floors, enrollment_digest) = write_auto_reply_enrollment( - &root, - &selector_values, - &targets, - &binding_evidence, - &author_bindings, - &runtime_root, - )?; - - install_auto_reply_signal_handlers(); - let mut children = AutoReplyChildrenGuard::new(targets.len()); - for (target, enrollment_floor) in targets.iter().zip(enrollment_floors.iter()) { - if AUTO_REPLY_STOP.load(Ordering::Relaxed) { - anyhow::bail!("auto-reply activation interrupted before worker startup"); - } - let room_root = prepare_auto_reply_room_state(&root, target)?; - let target_author_bindings = serde_json::to_string( - author_bindings - .get(&target.chat_id) - .context("AutoReply numeric reply-author enrollment is incomplete")?, - )?; - let target_reply_authors = author_bindings - .get(&target.chat_id) - .context("AutoReply numeric reply-author enrollment is incomplete")? - .iter() - .map(|binding| binding.nickname.clone()) - .collect::>(); - let _ = reply_authors_by_room - .get(&target.chat_id) - .context("AutoReply per-room reply-author allowlist is incomplete")?; - let mut command = Command::new(&python); - command - .current_dir(&runtime_root) - .args(AUTO_REPLY_PYTHON_ISOLATION_ARGS) - .arg(&supervisor) - .arg("--interval") - .arg(interval.to_string()) - .arg("--state-root") - .arg(&room_root) - .arg("--target-chat-id") - .arg(target.chat_id.to_string()) - .arg("--target-chat-name") - .arg(&target.chat_name) - .arg("--self-nickname") - .arg(&self_nickname) - .env("OPENKAKAO_AUTO_REPLY_CLI", "1") - .env("OPENKAKAO_CONFIG", &canonical_config_path) - .env("OPENKAKAO_CONFIG_SHA256", &config_digest) - .env("OPENKAKAO_ENROLLMENT_PATH", root.join("enrollment.json")) - .env("OPENKAKAO_ENROLLMENT_SHA256", &enrollment_digest) - .env("OPENKAKAO_TARGET_CHAT_ID", target.chat_id.to_string()) - .env("OPENKAKAO_TARGET_CHAT_NAME", &target.chat_name) - .env("OPENKAKAO_INITIAL_CURSOR", enrollment_floor.to_string()) - .env( - "OPENKAKAO_GEEKNEWS_ENABLED", - if room_catalog::catalog_geeknews_chat_ids(&root) - .ok() - .is_some_and(|ids| ids.contains(&target.chat_id)) - { - "1" - } else { - "0" - }, - ) - .env("OPENKAKAO_SELF_NICKNAME", &self_nickname) - .env( - "OPENKAKAO_ALLOW_LINK_FETCH", - if config.auto_reply.allow_link_fetch { - "1" - } else { - "0" - }, - ) - .env( - "OPENKAKAO_ALLOW_IMAGE_ANALYSIS", - if config.auto_reply.allow_image_analysis { - "1" - } else { - "0" - }, - ) - .env_remove(AUTO_REPLY_GUARDIAN_LIVENESS_ENV) - .env("OPENKAKAO_BINARY", &binary) - .env("OPENKAKAO_PYTHON", &python) - .env("OPENKAKAO_AUTO_REPLY_RUNTIME_ROOT", &runtime_root) - .stdout(if json_output { - Stdio::null() - } else { - Stdio::inherit() - }) - .stderr(if json_output { - Stdio::null() + Commands::ContextResponseTime { + chat, + user, + source, + db, + } => { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let stats = openkakao_cli::context::response_time_stats( + &db_path, + &chat, + &user, + source.as_deref(), + )?; + if json { + let mut redacted_stats = stats; + openkakao_cli::context::redact_response_time_stats(&mut redacted_stats); + println!("{}", serde_json::to_string_pretty(&redacted_stats)?); + } else if let Some(stats) = stats { + println!( + "{} average response: {:.1}s (median {:.1}s, p90 {:.1}s, {} samples)", + stats.user, + stats.average_seconds, + stats.median_seconds, + stats.p90_seconds, + stats.sample_count + ); } else { - Stdio::inherit() - }); - configure_auto_reply_supervisor_shared_state(&mut command, &root); - configure_auto_reply_supervisor_author_policy( - &mut command, - &target_reply_authors, - &target_author_bindings, - ); - command - .env("OPENKAKAO_REPLY_RUNNER", &runner.path) - .env("OPENKAKAO_REPLY_RUNNER_KIND", &runner.kind) - .env("OPENKAKAO_REPLY_RUNNER_SHA256", &runner.sha256) - .env("OPENKAKAO_REPLY_MODEL", &runner.model) - .env("OPENKAKAO_REPLY_REASONING_EFFORT", &runner.reasoning_effort) - .env("OPENKAKAO_REPLY_SERVICE_TIER", &runner.service_tier); - if let Some(codex_home) = &runner.codex_home { - command.env("OPENKAKAO_REPLY_CODEX_HOME", codex_home); - } - configure_auto_reply_process_group(&mut command); - match command.spawn() { - Ok(child) => children.children.push(child), - Err(error) => { - anyhow::bail!("start AutoReply worker for {}: {error}", target.chat_name); + println!("No response-time samples for '{}' in '{}'.", user, chat); } } - } - - if AUTO_REPLY_STOP.load(Ordering::Relaxed) { - anyhow::bail!("auto-reply activation interrupted during worker startup"); - } - write_auto_reply_aggregate(&root, &targets, &children.children, "running")?; - emit_auto_reply_preflight(json_output, false, &targets, &root, true, None, true); - - let mut failed = false; - let mut next_aggregate_update = std::time::Instant::now(); - loop { - if AUTO_REPLY_STOP.load(Ordering::Relaxed) { - children.stop(); - break; - } - if std::time::Instant::now() >= next_aggregate_update { - // Aggregate status is observational only; a transient dashboard - // write must not stop otherwise healthy per-room safety workers. - let _ = write_auto_reply_aggregate(&root, &targets, &children.children, "running"); - next_aggregate_update = std::time::Instant::now() + Duration::from_secs(1); - } - let mut child_exited = false; - for child in &mut children.children { - if child.try_wait()?.is_some() { - // A room supervisor is persistent. Any exit before the parent - // receives its own stop signal means that room lost coverage, - // even when the child happened to return status 0. - failed = true; - child_exited = true; + Commands::ContextReplySearch { + query, + chat, + limit, + db, + } => { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let mut results = + openkakao_cli::context::reply_decision_search(&db_path, &chat, &query, limit)?; + if json { + openkakao_cli::context::redact_reply_decisions(&mut results); + println!("{}", serde_json::to_string_pretty(&results)?); + } else { + for result in &results { + println!( + "[{:.3}] {} {} [{}] {}: {}", + result.score, + result.decision, + result.status, + result.category, + result.author, + util::truncate(&result.message, 160) + ); + } + println!( + "{} results (offline reply-decision vector search)", + results.len() + ); } } - if child_exited { - children.stop(); - break; - } - thread::sleep(Duration::from_millis(200)); - } - children.stop(); - for child in &mut children.children { - let status = child.wait()?; - if !status.success() { - failed = true; + Commands::ContextReplyRecord { record, db } => { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let applied = openkakao_cli::context::record_reply_decision(&db_path, &record)?; + if json { + println!( + "{}", + serde_json::json!({ + "recorded": applied, + "applied": applied, + "db": openkakao_cli::context::provenance_id( + &db_path.display().to_string() + ), + "network": false + }) + ); + } else if applied { + println!( + "Recorded reply decision in {} (offline).", + db_path.display() + ); + } else { + println!("Reply decision was stale and was not recorded (offline)."); + } } - } - write_auto_reply_aggregate(&root, &targets, &children.children, "stopped")?; - children.disarm(); - if AUTO_REPLY_GUARDIAN_LOST.load(Ordering::Acquire) { - anyhow::bail!("session guardian liveness was lost; all AutoReply workers were stopped"); - } - if failed { - anyhow::bail!("one or more AutoReply workers exited unsuccessfully"); - } - Ok(()) -} - -const AUTO_REPLY_READINESS_MAX_AGE_SECONDS: f64 = 15.0; -const AUTO_REPLY_READINESS_MAX_BYTES: u64 = 64 * 1024; -const MAX_INT64: i64 = i64::MAX; - -fn read_bounded_file(path: &Path) -> Result> { - let metadata = fs::symlink_metadata(path) - .with_context(|| format!("read readiness metadata: {}", path.display()))?; - if !metadata.file_type().is_file() || metadata.len() > AUTO_REPLY_READINESS_MAX_BYTES { - anyhow::bail!("invalid readiness file"); - } - let raw = fs::read(path).with_context(|| format!("read readiness file: {}", path.display()))?; - if raw.len() as u64 > AUTO_REPLY_READINESS_MAX_BYTES { - anyhow::bail!("readiness file exceeds bound"); - } - Ok(raw) -} - -fn read_bounded_json_file(path: &Path) -> Result { - let raw = read_bounded_file(path)?; - serde_json::from_slice(&raw).context("malformed readiness JSON") -} - -fn is_lower_hex_sha256(value: &str) -> bool { - value.len() == 64 - && value - .as_bytes() - .iter() - .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(byte)) -} - -fn validate_cli_enrollment_digest(raw: &[u8], expected: &str) -> Result<()> { - if !is_lower_hex_sha256(expected) { - anyhow::bail!("CLI enrollment authority digest missing"); - } - let actual = hex::encode(Sha256::digest(raw)); - if actual != expected { - anyhow::bail!("CLI enrollment authority digest mismatch"); - } - Ok(()) -} - -fn json_object_has_exact_keys(value: &serde_json::Value, expected: &[&str]) -> bool { - let Some(object) = value.as_object() else { - return false; - }; - object.len() == expected.len() && expected.iter().all(|key| object.contains_key(*key)) -} - -fn cli_enrollment_reply_author_bindings( - enrolled: &serde_json::Value, -) -> Result> { - let values = enrolled - .get("reply_author_bindings") - .and_then(serde_json::Value::as_array) - .filter(|values| !values.is_empty() && values.len() <= 64) - .context("CLI enrollment reply-author bindings missing")?; - let mut bindings = std::collections::BTreeMap::new(); - let mut author_ids = std::collections::BTreeSet::new(); - let mut previous_nickname: Option<&str> = None; - for value in values { - if !json_object_has_exact_keys(value, &["nickname", "author_id"]) { - anyhow::bail!("CLI enrollment reply-author binding schema invalid"); + Commands::ContextReplyUpdate { + event_id, + status, + reply, + sent_at, + db, + } => { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let updated = openkakao_cli::context::update_reply_decision( + &db_path, + &event_id, + &status, + reply.as_deref(), + sent_at.as_deref(), + )?; + if json { + println!( + "{}", + serde_json::json!({ + "updated": updated, + "applied": updated, + "event_id": event_id, + "db": openkakao_cli::context::provenance_id( + &db_path.display().to_string() + ), + "network": false + }) + ); + } else if updated { + println!("Updated reply decision {} (offline).", event_id); + } else { + println!("Reply decision {} was not found.", event_id); + } } - let nickname = value - .get("nickname") - .and_then(serde_json::Value::as_str) - .filter(|nickname| { - !nickname.is_empty() - && nickname.len() <= 1024 - && nickname.trim() == *nickname - && !nickname.chars().any(char::is_control) - }) - .context("CLI enrollment reply-author nickname invalid")?; - let author_id = value - .get("author_id") - .and_then(serde_json::Value::as_i64) - .filter(|author_id| 0 < *author_id && *author_id < MAX_INT64) - .context("CLI enrollment reply-author ID invalid")?; - if previous_nickname.is_some_and(|previous| previous >= nickname) - || bindings.insert(nickname.to_string(), author_id).is_some() - || !author_ids.insert(author_id) - { - anyhow::bail!("CLI enrollment reply-author bindings are ambiguous or unsorted"); + Commands::AxServiceScrapeOnce => { + println!( + "{}", + serde_json::to_string(&ax_send::scrape_chat_list_for_service())? + ); } - previous_nickname = Some(nickname); - } - Ok(bindings) -} - -fn cli_enrollment_cursor_authority( - enrolled: &serde_json::Value, - cursor_floor: i64, -) -> Result<(&str, i64)> { - let authority = enrolled - .get("cursor_authority") - .and_then(serde_json::Value::as_object) - .context("CLI enrollment cursor authority missing")?; - let expected_keys = [ - "schema_version", - "kind", - "cursor_floor", - "attested_db_last_log_id", - "prior_owner_id", - "prior_source_epoch", - ]; - if authority.len() != expected_keys.len() - || expected_keys - .iter() - .any(|key| !authority.contains_key(*key)) - || authority.get("schema_version") - != Some(&serde_json::Value::from( - AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, - )) - || authority - .get("cursor_floor") - .and_then(serde_json::Value::as_i64) - != Some(cursor_floor) - { - anyhow::bail!("CLI enrollment cursor authority invalid"); - } - let kind = authority - .get("kind") - .and_then(serde_json::Value::as_str) - .context("CLI enrollment cursor authority kind missing")?; - let attested_tail = authority - .get("attested_db_last_log_id") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 <= *value && *value < MAX_INT64) - .context("CLI enrollment cursor authority tail invalid")?; - let prior_owner = authority.get("prior_owner_id"); - let prior_epoch = authority.get("prior_source_epoch"); - match kind { - AUTO_REPLY_CURSOR_FRESH_KIND => { - if cursor_floor != attested_tail - || prior_owner != Some(&serde_json::Value::Null) - || prior_epoch != Some(&serde_json::Value::Null) - { - anyhow::bail!("CLI fresh cursor authority invalid"); - } + Commands::LocalSend { + chat_name, + message, + yes, + dry_run, + reply_to, + preflight, + } => { + let msg = format_outgoing_message(&message, no_prefix); + let Some(setup) = commands::auto_reply::worker_gate::prepare_local_send( + &config, &chat_name, dry_run, preflight, json, + )? + else { + return Ok(()); + }; + commands::local_send::cmd_local_send(commands::local_send::LocalSendOptions { + chat_name, + message: msg, + skip_confirm: yes, + dry_run, + preflight, + json, + bound_chat: setup.bound_chat, + reply_to, + })? } - AUTO_REPLY_CURSOR_REPLAY_KIND | AUTO_REPLY_CURSOR_LEFTOVER_KIND => { - if cursor_floor > attested_tail - || prior_owner - .and_then(serde_json::Value::as_str) - .is_none_or(str::is_empty) - || prior_epoch - .and_then(serde_json::Value::as_i64) - .is_none_or(|value| !(1..MAX_INT64).contains(&value)) - { - anyhow::bail!("CLI replay cursor authority invalid"); - } + Commands::LocalDelete { + chat_name, + source, + yes, + dry_run, + } => { + require_ax_send(&config)?; + require_allowed_send_chat(&config, &chat_name)?; + commands::local_send::cmd_local_delete(&chat_name, &source, yes, dry_run, json)? } - _ => anyhow::bail!("CLI enrollment cursor authority kind invalid"), - } - Ok((kind, attested_tail)) -} - -fn validate_cli_enrollment_authority( - enrollment: &serde_json::Value, - target: i64, - chat_name: &str, - cursor_floor: i64, - expected_room_root: &Path, -) -> Result<()> { - if enrollment.get("schema_version") - != Some(&serde_json::Value::from( - AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION, - )) - { - anyhow::bail!("CLI enrollment authority schema invalid"); - } - let targets = enrollment - .get("targets") - .and_then(serde_json::Value::as_array) - .filter(|targets| !targets.is_empty() && targets.len() <= 32) - .context("CLI enrollment targets invalid")?; - let matching_targets = targets - .iter() - .filter(|item| item.get("chat_id").and_then(serde_json::Value::as_i64) == Some(target)) - .collect::>(); - if matching_targets.len() != 1 { - anyhow::bail!("CLI enrollment target missing or ambiguous"); - } - let enrolled = matching_targets[0]; - if !json_object_has_exact_keys( - enrolled, - &[ - "chat_id", - "chat_name", - "last_log_id", - "room_state_root", - "identity", - "cursor_authority", - "reply_author_bindings", - ], - ) { - anyhow::bail!("CLI enrollment target schema invalid"); - } - if enrolled - .get("chat_name") - .and_then(serde_json::Value::as_str) - != Some(chat_name) - || enrolled - .get("last_log_id") - .and_then(serde_json::Value::as_i64) - != Some(cursor_floor) - { - anyhow::bail!("CLI enrollment target identity does not match DB readiness"); - } - let enrolled_room_root = enrolled - .get("room_state_root") - .and_then(serde_json::Value::as_str) - .filter(|value| !value.is_empty()) - .map(PathBuf::from) - .context("CLI enrollment room root missing")?; - let enrolled_room_root = - fs::canonicalize(enrolled_room_root).context("canonicalize CLI enrollment room root")?; - let expected_room_root = - fs::canonicalize(expected_room_root).context("canonicalize CLI DB state room root")?; - if enrolled_room_root != expected_room_root { - anyhow::bail!("CLI enrollment room root does not match DB readiness"); - } - let (cursor_kind, cursor_attested_tail) = - cli_enrollment_cursor_authority(enrolled, cursor_floor)?; - - let identity = enrolled - .get("identity") - .filter(|value| value.is_object()) - .context("CLI enrollment identity missing")?; - if identity.get("schema_version") != Some(&serde_json::Value::from(1)) - || identity.get("ax_name").and_then(serde_json::Value::as_str) != Some(chat_name) - { - anyhow::bail!("CLI enrollment identity invalid"); - } - let kind = identity - .get("kind") - .and_then(serde_json::Value::as_str) - .context("CLI enrollment identity kind missing")?; - let local_name = identity - .get("local_name") - .and_then(serde_json::Value::as_str) - .context("CLI enrollment local identity missing")?; - match kind { - "local_name" => { - if local_name != chat_name - || !json_object_has_exact_keys( - identity, - &["schema_version", "kind", "local_name", "ax_name"], - ) - { - anyhow::bail!("CLI enrollment local-name identity invalid"); - } + Commands::AxRead { chat_name, count } => { + commands::ax_read::cmd_ax_read(commands::ax_read::AxReadOptions { + chat_name, + count, + json, + })? } - "ax_transcript" => { - let expected_keys = [ - "schema_version", - "kind", - "local_name", - "ax_name", - "matched_log_ids", - "matched_count", - "matched_utf8_bytes", - "transcript_sha256", - "attested_db_last_log_id", - ]; - let matched_log_ids = identity - .get("matched_log_ids") - .and_then(serde_json::Value::as_array) - .context("CLI enrollment transcript log IDs missing")?; - let parsed_log_ids = matched_log_ids - .iter() - .map(|value| { - value - .as_i64() - .filter(|id| 0 < *id && *id < MAX_INT64) - .context("CLI enrollment transcript log ID invalid") - }) - .collect::>>()?; - let unique_log_ids = parsed_log_ids - .iter() - .copied() - .collect::>(); - let matched_utf8_bytes = identity - .get("matched_utf8_bytes") - .and_then(serde_json::Value::as_i64) - .unwrap_or(-1); - let attested_db_last_log_id = identity - .get("attested_db_last_log_id") - .and_then(serde_json::Value::as_i64) - .unwrap_or(-1); - let transcript_sha256 = identity - .get("transcript_sha256") - .and_then(serde_json::Value::as_str) - .unwrap_or(""); - if !local_name.is_empty() - || !json_object_has_exact_keys(identity, &expected_keys) - || !(2..=20).contains(&parsed_log_ids.len()) - || unique_log_ids.len() != parsed_log_ids.len() - || identity - .get("matched_count") - .and_then(serde_json::Value::as_i64) - != Some(parsed_log_ids.len() as i64) - || matched_utf8_bytes < 0 - || (parsed_log_ids.len() >= 3 && matched_utf8_bytes < 24) - || !(0 < attested_db_last_log_id && attested_db_last_log_id < MAX_INT64) - || attested_db_last_log_id != cursor_attested_tail - || attested_db_last_log_id - < parsed_log_ids.iter().copied().max().unwrap_or(MAX_INT64) - || !is_lower_hex_sha256(transcript_sha256) - { - anyhow::bail!("CLI enrollment transcript identity invalid"); + Commands::AxProbe { + chat, + dump, + max_nodes, + } => { + if dump { + for line in ax_send::dump_ax(&chat, max_nodes)? { + println!("{line}"); + } + } else { + let report = ax_send::probe_ax(&chat)?; + if json { + crate::util::output_json(&report)?; + } else { + println!( + "chat={:?} window_found={} composer_found={} elapsed_ms={} label={:?}", + report.chat_name, + report.window_found, + report.composer_found, + report.elapsed_ms, + report.label + ); + } } } - _ => anyhow::bail!("CLI enrollment identity kind invalid"), - } - if matches!( - cursor_kind, - AUTO_REPLY_CURSOR_REPLAY_KIND | AUTO_REPLY_CURSOR_LEFTOVER_KIND - ) && kind != "ax_transcript" - { - anyhow::bail!("CLI leftover or replay cursor requires transcript identity"); - } - cli_enrollment_reply_author_bindings(enrolled)?; - Ok(()) -} - -fn require_cli_enrolled_reply_author( - enrollment: &serde_json::Value, - target: i64, - author_id: i64, - nickname: &str, -) -> Result<()> { - let targets = enrollment - .get("targets") - .and_then(serde_json::Value::as_array) - .context("CLI enrollment targets invalid")?; - let matches = targets - .iter() - .filter(|item| item.get("chat_id").and_then(serde_json::Value::as_i64) == Some(target)) - .collect::>(); - if matches.len() != 1 { - anyhow::bail!("CLI enrollment target missing or ambiguous"); - } - let bindings = cli_enrollment_reply_author_bindings(matches[0])?; - if bindings.get(nickname) != Some(&author_id) { - anyhow::bail!("scheduled reply author identity drifted; re-enrollment is required"); - } - Ok(()) -} - -fn readiness_fresh(value: &serde_json::Value, now: f64) -> bool { - let stamp = value.as_f64().or_else(|| value.as_i64().map(|v| v as f64)); - stamp.is_some_and(|stamp| { - stamp.is_finite() - && -5.0 <= now - stamp - && now - stamp <= AUTO_REPLY_READINESS_MAX_AGE_SECONDS - }) -} - -fn require_persisted_auto_reply_readiness( - expected_owner: Option<&str>, - expected_epoch: Option, - expected_target: Option, - expected_chat_name: Option<&str>, - expected_last_observed: Option, -) -> Result<()> { - let home = dirs::home_dir().context("cannot resolve home directory")?; - let base = auto_reply_service::default_state_root(&home); - let supervisor_path = std::env::var_os("OPENKAKAO_SUPERVISOR_STATUS") - .filter(|value| !value.is_empty()) - .map(std::path::PathBuf::from) - .unwrap_or_else(|| base.join("supervisor-status.json")); - let db_state_path = std::env::var_os("OPENKAKAO_DB_WATCH_STATE") - .filter(|value| !value.is_empty()) - .map(std::path::PathBuf::from) - .unwrap_or_else(|| base.join("db-watch-state.json")); - let supervisor = read_bounded_json_file(&supervisor_path)?; - let db_state = read_bounded_json_file(&db_state_path)?; - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .context("system clock before UNIX epoch")? - .as_secs_f64(); - let owner = supervisor - .get("owner") - .and_then(serde_json::Value::as_str) - .filter(|value| !value.trim().is_empty()) - .context("supervisor owner missing")?; - if expected_owner.is_some_and(|expected| owner != expected) { - anyhow::bail!("persisted supervisor owner does not match event"); - } - let source_epoch = supervisor - .get("source_epoch") - .context("supervisor source epoch missing")? - .as_i64() - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("supervisor source epoch invalid")?; - let target = supervisor - .get("target_chat_id") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("supervisor target chat missing")?; - let chat_name = supervisor - .get("target_chat_name") - .and_then(serde_json::Value::as_str) - .filter(|value| !value.trim().is_empty()) - .context("supervisor target chat name missing")?; - if expected_epoch.is_some_and(|expected| source_epoch != expected) { - anyhow::bail!("persisted supervisor epoch does not match event"); - } - if expected_target.is_some_and(|expected| target != expected) { - anyhow::bail!("persisted supervisor target does not match event"); - } - if expected_chat_name.is_some_and(|expected| chat_name != expected) { - anyhow::bail!("persisted supervisor target name does not match event"); - } - let privacy_digest = supervisor - .get("privacy_digest") - .and_then(serde_json::Value::as_str) - .context("supervisor privacy attestation missing")?; - if privacy_digest.len() != 64 || !privacy_digest.bytes().all(|byte| byte.is_ascii_hexdigit()) { - anyhow::bail!("supervisor privacy attestation invalid"); - } - let config_path = std::env::var_os("OPENKAKAO_CONFIG") - .filter(|value| !value.is_empty()) - .map(std::path::PathBuf::from) - .unwrap_or_else(|| { - dirs::home_dir() - .unwrap_or_else(|| Path::new(".").to_path_buf()) - .join(".config/openkakao/config.toml") - }); - let config_digest = hex::encode(Sha256::digest( - fs::read(&config_path).with_context(|| "read privacy config")?, - )); - if config_digest != privacy_digest - || std::env::var("OPENKAKAO_PRIVACY_ATTESTATION") - .ok() - .is_some_and(|value| value != privacy_digest) - { - anyhow::bail!("privacy attestation changed"); - } - if supervisor.get("schema_version") != Some(&serde_json::Value::from(1)) - || supervisor.get("mode") - != Some(&serde_json::Value::String( - "database_authoritative".to_string(), - )) - { - anyhow::bail!("supervisor readiness schema invalid"); - } - if supervisor.get("readiness") != Some(&serde_json::Value::String("ready".to_string())) - || supervisor.get("state") != Some(&serde_json::Value::String("running".to_string())) - || supervisor.get("database_started") != Some(&serde_json::Value::Bool(true)) - || supervisor.get("auto_reply_enabled") != Some(&serde_json::Value::Bool(true)) - || supervisor.get("ax_state") != Some(&serde_json::Value::String("healthy".to_string())) - || supervisor - .get("ax_pid") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 < *value && *value < MAX_INT64) - .is_none() - || supervisor.get("ax_allow_send") != Some(&serde_json::Value::Bool(false)) - || supervisor.get("ax_delivery_state") - != Some(&serde_json::Value::String( - "fenced_db_authoritative".to_string(), - )) - || supervisor.get("delivery_state") - != Some(&serde_json::Value::String( - "fenced_db_authoritative".to_string(), - )) - || supervisor - .get("watcher_fence") - .and_then(serde_json::Value::as_object) - .and_then(|value| value.get("ax_allow_send")) - != Some(&serde_json::Value::Bool(false)) - || supervisor - .get("fence_reason") - .and_then(serde_json::Value::as_str) - != Some("") - || !readiness_fresh( - supervisor - .get("updated_at") - .context("supervisor heartbeat missing")?, - now, - ) - { - anyhow::bail!("persisted supervisor readiness is fenced"); - } - let db_owner = db_state - .get("owner_id") - .and_then(serde_json::Value::as_str) - .context("DB owner missing")?; - if db_owner != owner { - anyhow::bail!("persisted DB owner mismatch"); - } - let db_epoch = db_state - .get("source_epoch") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("DB source epoch invalid")?; - let db_target = db_state - .get("target_chat_id") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("DB target chat missing")?; - let cli_state_v3 = std::env::var("OPENKAKAO_AUTO_REPLY_CLI").as_deref() == Ok("1"); - let expected_db_schema = if cli_state_v3 { 3 } else { 2 }; - if db_state.get("schema_version") != Some(&serde_json::Value::from(expected_db_schema)) - || db_state.get("target_chat_name") - != Some(&serde_json::Value::String(chat_name.to_string())) - { - anyhow::bail!("DB readiness schema or target name invalid"); - } - if db_epoch != source_epoch - || db_target != target - || db_state.get("capability_state") != Some(&serde_json::Value::String("ready".to_string())) - || db_state.get("delivery_enabled") != Some(&serde_json::Value::Bool(true)) - || db_state.get("fence") != Some(&serde_json::Value::String("ready".to_string())) - || db_state - .get("fence_reason") - .and_then(serde_json::Value::as_str) - != Some("") - || !readiness_fresh( - db_state - .get("heartbeat_at") - .context("DB heartbeat missing")?, - now, - ) - || db_state.get("pending_log_ids") != Some(&serde_json::Value::Array(Vec::new())) - || db_state.get("pending_gaps") != Some(&serde_json::Value::Array(Vec::new())) - { - anyhow::bail!("persisted DB readiness is fenced"); - } - let watermark = db_state - .get("acked_watermark") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 <= *value && *value < MAX_INT64) - .context("DB watermark invalid")?; - if cli_state_v3 { - let cursor_floor = db_state - .get("cursor_floor") - .and_then(serde_json::Value::as_i64) - .filter(|value| 0 <= *value && *value < MAX_INT64) - .context("CLI DB cursor floor invalid")?; - if cursor_floor > watermark - || db_state.get("candidate_phase") - != Some(&serde_json::Value::String("idle".to_string())) - || db_state.get("in_flight_candidate") != Some(&serde_json::Value::Null) - { - anyhow::bail!("CLI DB candidate state is not idle"); - } - let enrollment_path = std::env::var_os("OPENKAKAO_ENROLLMENT_PATH") - .filter(|value| !value.is_empty()) - .map(PathBuf::from) - .context("CLI enrollment authority missing")?; - let enrollment_raw = read_bounded_file(&enrollment_path)?; - let expected_enrollment_digest = - std::env::var("OPENKAKAO_ENROLLMENT_SHA256").unwrap_or_default(); - validate_cli_enrollment_digest(&enrollment_raw, expected_enrollment_digest.trim())?; - let enrollment: serde_json::Value = - serde_json::from_slice(&enrollment_raw).context("malformed CLI enrollment JSON")?; - let expected_room_root = db_state_path - .parent() - .context("CLI DB state room root missing")?; - validate_cli_enrollment_authority( - &enrollment, - target, - chat_name, - cursor_floor, - expected_room_root, - )?; - } - let persisted_last_observed = db_state - .get("last_observed_log_id") - .and_then(serde_json::Value::as_i64) - .filter(|value| watermark <= *value && *value < MAX_INT64) - .context("DB observed watermark invalid")?; - if expected_last_observed.is_some_and(|expected| persisted_last_observed < expected) { - anyhow::bail!("scheduled reply source is not in the persisted watcher tail"); - } - let observed = db_state - .get("observed_log_ids") - .and_then(serde_json::Value::as_array) - .context("DB observed IDs missing")?; - let acked = db_state - .get("acked_log_ids") - .and_then(serde_json::Value::as_array) - .context("DB acked IDs missing")?; - let pending = db_state - .get("pending_log_ids") - .and_then(serde_json::Value::as_array) - .context("DB pending IDs missing")?; - let parse_ids = |values: &[serde_json::Value]| -> Result> { - values - .iter() - .map(|value| { - value - .as_i64() - .filter(|id| 0 < *id && *id < MAX_INT64) - .context("DB readiness ID invalid") - }) - .collect() - }; - let observed_ids = parse_ids(observed)?; - let acked_ids = parse_ids(acked)?; - let pending_ids = parse_ids(pending)?; - let observed_set: std::collections::BTreeSet = observed_ids.iter().copied().collect(); - let acked_set: std::collections::BTreeSet = acked_ids.iter().copied().collect(); - let pending_set: std::collections::BTreeSet = pending_ids.iter().copied().collect(); - let expected_last_observed = observed_set.iter().next_back().copied().unwrap_or(0); - if !acked_set.is_subset(&observed_set) - || pending_set != observed_set.difference(&acked_set).copied().collect() - || !pending_set.is_disjoint(&acked_set) - || watermark != acked_set.iter().next_back().copied().unwrap_or(0) - || db_state - .get("last_observed_log_id") - .and_then(serde_json::Value::as_i64) - != Some(expected_last_observed) - { - anyhow::bail!("DB readiness cursor sets invalid"); - } - Ok(()) -} - -/// Resolve the scheduled send's source row for its author attestation. -/// -/// The bound send attests that the row it answers has the expected author, and -/// it used to look for that row only in the newest 20 messages. A scheduled -/// reply fires after a debounce window, so in an active room the source row is -/// usually older than those 20 rows; the preflight then failed with "scheduled -/// reply source row is unavailable" and the job died as stale_backlog -/// (2026-09-22, room 417: 70 of 202 preflight attempts). Prefer the page that -/// was already read, then fall back to a direct log-id lookup on the same -/// read-only reader. A row that is genuinely gone still fails closed. -fn resolve_bound_source_message( - windowed: &[local_db::LocalMessage], - expected_source_log_id: i64, - lookup: F, -) -> Result -where - F: FnOnce(i64) -> Result>, -{ - if let Some(message) = windowed - .iter() - .find(|message| message.log_id == expected_source_log_id) - { - return Ok(message.clone()); - } - lookup(expected_source_log_id) - .context("read the scheduled reply source row")? - .context("scheduled reply source row is unavailable") -} -fn require_expected_local_source_tail( - target_chat_id: i64, - expected_source_log_id: i64, - source_fence: &local_db::LocalPollEnvelope, -) -> Result<()> { - let completeness = &source_fence.completeness; - // Later room rows must not cancel an earlier unanswered inbound. The - // poll starts after the scheduled source; an empty page means the - // source is still the tail, and a complete later page is another - // per-message job, not a reason to drop this send. - if source_fence.chat.chat_id != target_chat_id - || completeness.after_log_id != expected_source_log_id - || completeness.has_gap - || completeness.chat_last_log_id < expected_source_log_id - { - anyhow::bail!("scheduled reply source tail is unavailable"); - } - if completeness.status == "empty" { - if completeness.has_more - || completeness.first_log_id.is_some() - || completeness.last_log_id.is_some() - || completeness.available_max_log_id.is_some() - || !source_fence.messages.is_empty() - || completeness.chat_last_log_id != expected_source_log_id - { - anyhow::bail!("scheduled reply source tail is unavailable"); - } - return Ok(()); - } - if completeness.status != "complete" && completeness.status != "partial" { - anyhow::bail!("scheduled reply source tail is unavailable"); - } - if source_fence.messages.is_empty() - || completeness.first_log_id != source_fence.messages.first().map(|row| row.log_id) - || completeness.last_log_id != source_fence.messages.last().map(|row| row.log_id) - || source_fence - .messages - .iter() - .any(|row| row.log_id <= expected_source_log_id || row.chat_id != target_chat_id) - { - anyhow::bail!("scheduled reply source tail is unavailable"); - } - Ok(()) -} - -fn require_auto_reply_worker_preflight(preflight: bool, worker_identity: bool) -> Result<()> { - if preflight && !worker_identity { - anyhow::bail!( - "local-send --preflight is only available to the database-authoritative AutoReply worker" - ); - } - Ok(()) -} - -#[cfg(unix)] -use auto_reply_runtime::acquire_worker_setup_lock; - -fn finish_worker_bound_local_send_setup( - setup: Result, - is_auto_reply_worker: bool, - preflight: bool, - chat_name: &str, - json: bool, -) -> Result> { - match setup { - Ok(value) => Ok(Some(value)), - Err(_error) if is_auto_reply_worker && !preflight => { - commands::local_send::emit_pre_send_unavailable(chat_name, json)?; - Ok(None) - } - Err(error) => Err(error), - } -} - -/// What `local-send` holds for its whole run. An AutoReply worker holds the -/// owner-generation and AX send locks until this drops, and carries the -/// database-authoritative binding the AX sender re-attests. A plain run holds -/// neither lock and carries no binding. -struct LocalSendSetup { - _generation_lock: Option, - _send_lock: Option, - bound_chat: Option, -} - -/// Run the AutoReply worker's lock, readiness, and binding gates before a -/// `local-send` mutates anything. `None` means the worker setup failed and the -/// pre-send unavailable result was already emitted. -fn prepare_local_send( - config: &config::OpenKakaoConfig, - chat_name: &str, - dry_run: bool, - preflight: bool, - json: bool, -) -> Result> { - let worker_identity = std::env::var("OPENKAKAO_AUTO_REPLY_WORKER") - .or_else(|_| std::env::var("OPENKAKAO_BUJAMENTOR_WORKER")) - .as_deref() - == Ok("1"); - require_auto_reply_worker_preflight(preflight, worker_identity)?; - let is_auto_reply_worker = !dry_run && worker_identity; - let setup = (|| -> Result<_> { - let _generation_lock = if is_auto_reply_worker && !preflight { - let home = dirs::home_dir().context("cannot resolve home directory")?; - let lock_path = std::env::var_os("OPENKAKAO_AUTO_REPLY_GENERATION_LOCK") - .or_else(|| std::env::var_os("OPENKAKAO_AUTO_REPLY_LOCK")) - .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_GENERATION_LOCK")) - .or_else(|| std::env::var_os("OPENKAKAO_BUJAMENTOR_LOCK")) - .filter(|value| !value.is_empty()) - .map(std::path::PathBuf::from) - .unwrap_or_else(|| { - auto_reply_service::default_state_root(&home).join(".owner-generation.lock") - }); - let lock = fs::OpenOptions::new() - .create(true) - .truncate(false) - .read(true) - .write(true) - .open(lock_path)?; - #[cfg(unix)] - acquire_worker_setup_lock( - &lock, - "owner-generation lock", - std::time::Duration::from_millis(400), - )?; - Some(lock) - } else { - None - }; - let _send_lock = if is_auto_reply_worker && !preflight { - if let Some(lock_path) = - std::env::var_os("OPENKAKAO_AUTO_REPLY_SEND_LOCK").filter(|value| !value.is_empty()) - { - let lock = fs::OpenOptions::new() - .create(true) - .truncate(false) - .read(true) - .write(true) - .open(lock_path)?; - #[cfg(unix)] - acquire_worker_setup_lock( - &lock, - "AX send lock", - std::time::Duration::from_millis(400), - )?; - Some(lock) - } else { - None - } - } else { - None - }; - let mut bound_chat = None; - if !dry_run { - let mut worker_target = None; - if is_auto_reply_worker { - config::validate_auto_reply(config)?; - let expected_owner = std::env::var("OPENKAKAO_SUPERVISOR_OWNER") - .ok() - .filter(|value| !value.trim().is_empty()) - .context("worker owner missing")?; - let expected_epoch = std::env::var("OPENKAKAO_DB_SOURCE_EPOCH") - .ok() - .and_then(|value| value.parse::().ok()) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("worker source epoch invalid")?; - let expected_target = std::env::var("OPENKAKAO_TARGET_CHAT_ID") - .ok() - .and_then(|value| value.parse::().ok()) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("worker target chat invalid")?; - let expected_last_observed = std::env::var("OPENKAKAO_EXPECTED_SOURCE_LOG_ID") - .ok() - .and_then(|value| value.parse::().ok()) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("worker scheduled source log ID invalid")?; - let expected_author_id = std::env::var("OPENKAKAO_EXPECTED_SOURCE_AUTHOR_ID") - .ok() - .and_then(|value| value.parse::().ok()) - .filter(|value| 0 < *value && *value < MAX_INT64) - .context("worker scheduled source author ID invalid")?; - let expected_author_nickname = - std::env::var("OPENKAKAO_EXPECTED_SOURCE_AUTHOR_NICKNAME") - .ok() - .filter(|value| { - !value.is_empty() - && value.len() <= 1024 - && value.trim() == value - && !value.chars().any(char::is_control) - }) - .context("worker scheduled source author nickname invalid")?; - let proactive_send = std::env::var("OPENKAKAO_PROACTIVE_SEND") - .ok() - .is_some_and(|value| value == "1"); - require_persisted_auto_reply_readiness( - Some(expected_owner.as_str()), - Some(expected_epoch), - Some(expected_target), - Some(chat_name), - Some(expected_last_observed), - )?; - worker_target = Some(( - expected_target, - expected_last_observed, - expected_author_id, - expected_author_nickname, - proactive_send, - )); - } - require_ax_send(config)?; - require_allowed_send_chat(config, chat_name)?; - if let Some(( - target_chat_id, - expected_last_observed, - expected_author_id, - expected_author_nickname, - proactive_send, - )) = worker_target - { - // Fetch the authoritative local tail only after both - // process locks and all persisted/safety gates are held. - // The AX sender will re-attest this numeric ID's latest - // suffix against the exact open window before mutation. - let reader = local_db::LocalDbReader::open_no_mutation() - .context("open local database for bound AX send attestation")?; - let mut messages = reader.read_messages(target_chat_id, 20, None)?; - let source_fence = - reader.poll_after(target_chat_id, 1, Some(expected_last_observed))?; - require_expected_local_source_tail( - target_chat_id, - expected_last_observed, - &source_fence, - )?; - let source_message = - resolve_bound_source_message(&messages, expected_last_observed, |log_id| { - reader.read_message_by_log_id(target_chat_id, log_id) - })?; - if proactive_send { - if !source_message.is_self - && (source_message.author_id != expected_author_id - || source_message.sender_name.trim() != expected_author_nickname) - { - anyhow::bail!( - "scheduled reply source author identity drifted; re-enrollment is required" - ); - } - } else if source_message.is_self - || source_message.author_id != expected_author_id - || source_message.sender_name.trim() != expected_author_nickname - { - anyhow::bail!( - "scheduled reply source author identity drifted; re-enrollment is required" - ); - } - let enrollment_path = std::env::var_os("OPENKAKAO_ENROLLMENT_PATH") - .filter(|value| !value.is_empty()) - .map(PathBuf::from) - .context("CLI enrollment authority missing")?; - let enrollment_raw = read_bounded_file(&enrollment_path)?; - let expected_enrollment_digest = - std::env::var("OPENKAKAO_ENROLLMENT_SHA256").unwrap_or_default(); - validate_cli_enrollment_digest(&enrollment_raw, expected_enrollment_digest.trim())?; - let enrollment: serde_json::Value = serde_json::from_slice(&enrollment_raw) - .context("malformed CLI enrollment JSON")?; - require_cli_enrolled_reply_author( - &enrollment, - target_chat_id, - expected_author_id, - &expected_author_nickname, - )?; - messages.reverse(); - let local_tail = ax_send::normalize_local_binding_suffix(&messages) - .into_iter() - .map(|(_, token)| token) - .collect(); - bound_chat = Some(commands::local_send::BoundLocalSend { - chat_id: target_chat_id, - expected_source_log_id: expected_last_observed, - local_tail, - }); - } - } - Ok(LocalSendSetup { - _generation_lock, - _send_lock, - bound_chat, - }) - })(); - finish_worker_bound_local_send_setup(setup, is_auto_reply_worker, preflight, chat_name, json) -} - -fn main() -> Result<()> { - let cli = Cli::parse(); - // The record window's data source must work while the config is broken: - // "the config is invalid" is exactly one of the facts it has to report. - if let Commands::ReplyReceipt { - ledger, - state_root, - chat_id, - limit, - } = &cli.command - { - return commands::reply_receipt::run( - ledger.clone(), - state_root.clone(), - chat_id, - *limit, - cli.json, - ); - } - let service_bootstrap_status_path = validate_service_bootstrap_paths(&cli.command)?; - let config = match load_config() { - Ok(config) => config, - Err(error) => { - if let Some(status_path) = service_bootstrap_status_path { - let _ = auto_reply_service::write_config_invalid_status(status_path); - } - return Err(error); - } - }; - set_auth_policy(AuthPolicy::from_config(&config.auth)); - let json = cli.json; - let unattended = cli.unattended || config.mode.unattended; - let allow_non_interactive_send = - cli.allow_non_interactive_send || config.send.allow_non_interactive; - let allow_watch_side_effects = cli.allow_watch_side_effects || config.watch.allow_side_effects; - let min_unattended_send_interval_secs = config - .safety - .min_unattended_send_interval_secs - .unwrap_or(10); - let min_hook_interval_secs = config.safety.min_hook_interval_secs.unwrap_or(2); - let min_webhook_interval_secs = config.safety.min_webhook_interval_secs.unwrap_or(2); - let hook_timeout_secs = config.safety.hook_timeout_secs.unwrap_or(20); - let webhook_timeout_secs = config.safety.webhook_timeout_secs.unwrap_or(10); - let no_prefix = if cli.no_prefix { - true - } else { - matches!(config.send.default_prefix, Some(false)) - }; - - // Respect NO_COLOR env var (https://no-color.org/) and --no-color flag - if cli.no_color || std::env::var("NO_COLOR").is_ok() || json { - NO_COLOR.store(true, Ordering::Relaxed); - } - - // Server-login warning — printed to stderr so it never corrupts JSON on - // stdout. Silenced with OPENKAKAO_CLI_NO_DEPRECATION=1 for scripted - // local-only use. `local-send`/`ax-read` need neither login nor this - // warning, since they never touch Kakao's servers. - if std::env::var_os("OPENKAKAO_CLI_NO_DEPRECATION").is_none() - && !is_local_only_command(&cli.command) - { - eprintln!("⚠️ Server login (login --save / login --manual) is broken on recent"); - eprintln!(" KakaoTalk macOS builds. Do NOT repeatedly retry login on an unregistered"); - eprintln!(" device — it can get your account's sub-device login blocked. Prefer"); - eprintln!( - " 'local-send'/'ax-read'/'local-chats' — no server contact needed. See README." - ); - } - - match cli.command { - Commands::Auth => commands::auth::cmd_auth(json)?, - Commands::AuthStatus => commands::auth::cmd_auth_status(json)?, - Commands::Login { - save, - manual, - email, - password, - app_version, - } => { - if manual { - commands::auth::cmd_login_manual(save, email, password, app_version)? - } else { - commands::auth::cmd_login(save)? - } - } - Commands::Me => commands::rest::cmd_me(json)?, - Commands::Friends { - favorites, - hidden, - search, - local, - chat_id, - user_id, - } => commands::rest::cmd_friends(favorites, hidden, search, local, chat_id, user_id, json)?, - Commands::Chats { - show_all, - unread, - search, - chat_type, - rest, - } => commands::chats::cmd_chats(show_all, unread, search, chat_type, rest, json)?, - Commands::Read { - chat_id, - count, - before, - cursor, - since, - all, - delay_ms, - force, - rest, - } => commands::read::cmd_read( - chat_id, - ReadCommandOptions { - count, - cursor: cursor.or(before), - since, - all, - delay_ms, - force, - rest, - json, - }, - )?, - Commands::Members { - chat_id, - rest, - full, - } => commands::members::cmd_members(chat_id, rest, full, json)?, - Commands::Chatinfo { chat_id } => commands::rest::cmd_chatinfo(chat_id, json)?, - Commands::Settings => commands::rest::cmd_settings(json)?, - Commands::Scrap { url } => commands::rest::cmd_scrap(&url, json)?, - Commands::Profile { - user_id, - chat_id, - local, - } => commands::profile::cmd_profile(user_id, chat_id, local, json)?, - Commands::Favorite { user_id } => commands::rest::cmd_favorite(user_id, json)?, - Commands::Unfavorite { user_id } => commands::rest::cmd_unfavorite(user_id, json)?, - Commands::Hide { user_id } => commands::rest::cmd_hide(user_id, json)?, - Commands::Unhide { user_id } => commands::rest::cmd_unhide(user_id, json)?, - Commands::Profiles => commands::rest::cmd_profiles(json)?, - Commands::Keywords => commands::rest::cmd_keywords(json)?, - Commands::Unread => commands::rest::cmd_unread(json)?, - Commands::Export { - chat_id, - format, - output, - } => commands::rest::cmd_export(chat_id, &format, output.as_deref(), json)?, - Commands::Search { chat_id, query } => commands::rest::cmd_search(chat_id, &query, json)?, - Commands::Stats { - chat_id, - limit, - since, - } => commands::analytics::cmd_stats(chat_id, limit, since.as_deref(), json)?, - Commands::Completions { shell } => { - generate( - shell, - &mut Cli::command(), - "openkakao-cli", - &mut io::stdout(), - ); - } - Commands::Renew => commands::auth::cmd_renew(json)?, - Commands::Relogin { - fresh_xvc, - password, - email, - } => commands::auth::cmd_relogin(json, fresh_xvc, password, email)?, - Commands::LocoTest => { - eprintln!("[deprecated] 'loco-test' is now hidden. Prefer 'doctor --loco'."); - commands::auth::cmd_loco_test()? - } - Commands::Send { - chat_id, - message, - force, - yes, - dry_run, - } => { - let msg = format_outgoing_message(&message, no_prefix); - if dry_run { - eprintln!( - "[dry-run] Would send to chat {}: \"{}\"", - chat_id, - util::truncate(&msg, 80) - ); - if json { - util::output_json(&serde_json::json!({ - "dry_run": true, - "action": "send", - "chat_id": chat_id, - "message": msg, - }))?; - } - } else { - require_loco_write(&config)?; - commands::send::cmd_send(commands::send::SendOptions { - chat_id, - message: msg, - force, - skip_confirm: yes, - unattended, - allow_non_interactive: allow_non_interactive_send, - min_interval_secs: min_unattended_send_interval_secs, - json, - })? - } - } - Commands::SendMe { - message, - yes, - dry_run, - } => { - let reader = local_db::LocalDbReader::open() - .context("Failed to open local DB to find memo chat")?; - let memo_id = reader - .find_memo_chat_id()? - .context("Could not find memo chat (나와의 채팅) in local database")?; - let msg = format_outgoing_message(&message, no_prefix); - if dry_run { - eprintln!( - "[dry-run] Would send to memo chat {}: \"{}\"", - memo_id, - util::truncate(&msg, 80) - ); - if json { - util::output_json(&serde_json::json!({ - "dry_run": true, - "action": "send_me", - "chat_id": memo_id, - "message": msg, - }))?; - } - } else { - require_loco_write(&config)?; - commands::send::cmd_send(commands::send::SendOptions { - chat_id: memo_id, - message: msg, - force: false, - skip_confirm: yes, - unattended, - allow_non_interactive: allow_non_interactive_send, - min_interval_secs: min_unattended_send_interval_secs, - json, - })? - } - } - Commands::SendPhoto { - chat_id, - file, - force, - yes, - dry_run, - } => { - if dry_run { - eprintln!("[dry-run] Would send photo '{}' to chat {}", file, chat_id); - if json { - util::output_json(&serde_json::json!({ - "dry_run": true, "action": "send_photo", "chat_id": chat_id, "file": file, - }))?; - } - } else { - require_loco_write(&config)?; - commands::send::cmd_send_file(commands::send::SendFileOptions { - chat_id, - file_path: file, - force, - skip_confirm: yes, - unattended, - allow_non_interactive: allow_non_interactive_send, - min_interval_secs: min_unattended_send_interval_secs, - json, - })? - } - } - Commands::SendFile { - chat_id, - file, - force, - yes, - dry_run, - } => { - if dry_run { - eprintln!("[dry-run] Would send file '{}' to chat {}", file, chat_id); - if json { - util::output_json(&serde_json::json!({ - "dry_run": true, "action": "send_file", "chat_id": chat_id, "file": file, - }))?; - } - } else { - require_loco_write(&config)?; - commands::send::cmd_send_file(commands::send::SendFileOptions { - chat_id, - file_path: file, - force, - skip_confirm: yes, - unattended, - allow_non_interactive: allow_non_interactive_send, - min_interval_secs: min_unattended_send_interval_secs, - json, - })? - } - } - Commands::Delete { - chat_id, - log_id, - force, - yes, - dry_run, - } => { - if dry_run { - eprintln!( - "[dry-run] Would delete message {} from chat {}", - log_id, chat_id - ); - if json { - util::output_json(&serde_json::json!({ - "dry_run": true, "action": "delete", "chat_id": chat_id, "log_id": log_id, - }))?; - } - } else { - require_loco_write(&config)?; - commands::send::cmd_delete(commands::send::DeleteOptions { - chat_id, - log_id, - force, - skip_confirm: yes, - unattended, - allow_non_interactive: allow_non_interactive_send, - min_interval_secs: min_unattended_send_interval_secs, - json, - })? - } - } - Commands::MarkRead { - chat_id, - log_id, - yes: _, - dry_run, - } => { - if dry_run { - eprintln!( - "[dry-run] Would mark chat {} read up to {}", - chat_id, log_id - ); - if json { - util::output_json(&serde_json::json!({ - "dry_run": true, "action": "mark-read", "chat_id": chat_id, "log_id": log_id, - }))?; - } - } else { - require_loco_write(&config)?; - commands::send::cmd_mark_read(commands::send::MarkReadOptions { - chat_id, - log_id, - json, - })? - } - } - Commands::React { - chat_id, - log_id, - reaction_type, - dry_run, - } => { - if dry_run { - eprintln!( - "[dry-run] Would react (type={}) to message {} in chat {}", - reaction_type, log_id, chat_id - ); - if json { - util::output_json(&serde_json::json!({ - "dry_run": true, "action": "react", "chat_id": chat_id, "log_id": log_id, "reaction_type": reaction_type, - }))?; - } - } else { - require_loco_write(&config)?; - commands::send::cmd_react(commands::send::ReactOptions { - chat_id, - log_id, - reaction_type, - json, - })? - } - } - Commands::Edit { - chat_id, - log_id, - message, - force, - yes, - dry_run, - } => { - let msg = format_outgoing_message(&message, no_prefix); - if dry_run { - eprintln!( - "[dry-run] Would edit message {} in chat {}: \"{}\"", - log_id, - chat_id, - util::truncate(&msg, 80) - ); - if json { - util::output_json(&serde_json::json!({ - "dry_run": true, "action": "edit", "chat_id": chat_id, "log_id": log_id, "message": msg, - }))?; - } - } else { - require_loco_write(&config)?; - commands::send::cmd_edit(commands::send::EditOptions { - chat_id, - log_id, - message: msg, - force, - skip_confirm: yes, - unattended, - allow_non_interactive: allow_non_interactive_send, - min_interval_secs: min_unattended_send_interval_secs, - json, - })? - } - } - Commands::Watch { - chat_id, - raw, - read_receipt, - max_reconnect, - reconnect_delay, - reconnect_max_delay, - download_media, - download_dir, - hook_cmd, - webhook_url, - webhook_header, - webhook_signing_secret, - webhook_format, - hook_chat_id, - hook_keyword, - hook_type, - hook_fail_fast, - resume, - capture, - } => commands::watch::cmd_watch(WatchOptions { - unattended, - allow_side_effects: allow_watch_side_effects, - filter_chat_id: chat_id, - raw, - read_receipt, - max_reconnect: config.watch.default_max_reconnect.unwrap_or(max_reconnect), - reconnect_delay_secs: reconnect_delay, - reconnect_max_delay_secs: reconnect_max_delay, - download_media, - download_dir, - hook_cmd, - webhook_url, - webhook_headers: webhook_header, - webhook_signing_secret, - hook_chat_ids: hook_chat_id, - hook_keywords: hook_keyword, - hook_types: hook_type, - hook_fail_fast, - min_hook_interval_secs, - min_webhook_interval_secs, - hook_timeout_secs, - webhook_timeout_secs, - allow_insecure_webhooks: config.safety.allow_insecure_webhooks, - webhook_format: WebhookFormat::from_str_opt(webhook_format.as_deref())?, - resume, - json, - capture, - })?, - Commands::Download { - chat_id, - log_id, - output_dir, - local, - expected_author_id, - } => commands::download::cmd_download( - chat_id, - log_id, - output_dir.as_deref(), - local, - expected_author_id, - json, - )?, - Commands::Cache { chat_id, limit } => commands::analytics::cmd_cache(chat_id, limit, json)?, - Commands::CacheSearch { - query, - chat_id, - count, - } => commands::analytics::cmd_cache_search(&query, chat_id, count, json)?, - Commands::CacheStats => commands::analytics::cmd_cache_stats(json)?, - Commands::LocoChats { show_all } => { - eprintln!("[deprecated] 'loco-chats' is now hidden. Prefer 'chats' (LOCO by default)."); - commands::chats::cmd_loco_chats(show_all, false, None, None, json)? - } - Commands::LocoRead { - chat_id, - count, - cursor, - since, - all, - delay_ms, - force, - } => { - eprintln!("[deprecated] 'loco-read' is now hidden. Prefer 'read' (LOCO by default)."); - commands::read::cmd_loco_read( - chat_id, - &commands::read::ReadCommandOptions { - count: count as usize, - cursor, - since, - all, - delay_ms, - force, - rest: false, - json, - }, - )? - } - Commands::LocoMembers { chat_id } => { - eprintln!( - "[deprecated] 'loco-members' is now hidden. Prefer 'members' (LOCO by default)." - ); - commands::members::cmd_loco_members(chat_id, false, json)? - } - Commands::LocoChatinfo { chat_id } => { - eprintln!("[deprecated] 'loco-chatinfo' is now hidden. Prefer 'chatinfo'."); - commands::probe::cmd_loco_chatinfo(chat_id, json)? - } - Commands::LocoBlocked => commands::members::cmd_loco_blocked(json)?, - Commands::Probe { - method, - body, - capture_pushes, - } => commands::probe::cmd_loco_probe(&method, body.as_deref(), json, capture_pushes)?, - Commands::ProfileHints { - app_state, - app_state_diff, - local_graph, - user_id, - probe_syncmainpf, - probe_uplinkprof, - } => commands::profile::cmd_profile_hints( - app_state, - app_state_diff, - local_graph, - user_id, - probe_syncmainpf, - probe_uplinkprof, - json, - )?, - Commands::LocoProbe { method, body } => { - eprintln!("[deprecated] 'loco-probe' is now hidden. Prefer 'probe'."); - commands::probe::cmd_loco_probe(&method, body.as_deref(), json, false)? - } - Commands::LocalChats { limit, groups } => { - let reader = local_db::LocalDbReader::open()?; - if groups { - let chats = reader.list_group_chats(limit)?; - if json { - println!("{}", serde_json::to_string_pretty(&chats)?); - } else if chats.is_empty() { - println!("No group chats found in local database."); - } else { - for c in &chats { - println!( - " {} | {} | {} ({})", - c.chat_id, c.chat_type, c.title, c.members, - ); - } - println!( - "\n{} group chats (local DB, no server contact)", - chats.len() - ); - } - return Ok(()); - } - let chats = reader.list_chats(limit)?; - if json { - println!("{}", serde_json::to_string_pretty(&chats)?); - } else { - if chats.is_empty() { - println!("No chats found in local database."); - } else { - let chat_type_label = |t: i32| -> &'static str { - match t { - 0 => "DM", - 1 => "Group", - _ => "Other", - } - }; - for c in &chats { - let ts = chrono::Local - .timestamp_opt(c.last_updated_at, 0) - .single() - .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string()) - .unwrap_or_default(); - let unread = if c.unread_count > 0 { - format!(" [{}]", c.unread_count) - } else { - String::new() - }; - println!( - " {} | {} | {} ({}){} | {}", - c.chat_id, - chat_type_label(c.chat_type), - c.chat_name, - c.active_members_count, - unread, - ts, - ); - } - println!("\n{} chats (local DB, no server contact)", chats.len()); - } - } - } - Commands::LocalRead { - chat_id, - count, - since, - } => { - let since_ts = util::parse_since_date(since.as_deref())?; - let reader = local_db::LocalDbReader::open()?; - let mut messages = reader.read_messages(chat_id, count, since_ts)?; - messages.reverse(); // chronological order - if json { - println!("{}", serde_json::to_string_pretty(&messages)?); - } else { - if messages.is_empty() { - println!("No messages found in local database for chat {}.", chat_id); - } else { - for m in &messages { - let ts = chrono::Local - .timestamp_opt(m.sent_at, 0) - .single() - .map(|dt| dt.format("%H:%M:%S").to_string()) - .unwrap_or_default(); - let sender = if m.sender_name.is_empty() { - format!("{}", m.author_id) - } else { - m.sender_name.clone() - }; - let type_tag = util::message_type_label(m.message_type); - if m.message_type == 1 { - println!(" [{}] {}: {}", ts, sender, m.message); - } else { - println!(" [{}] {}: [{}] {}", ts, sender, type_tag, m.message); - } - } - println!( - "\n{} messages (local DB, no server contact)", - messages.len() - ); - } - } - } - Commands::LocalPoll { - chat_id, - count, - interval, - } => { - let reader = local_db::LocalDbReader::open()?; - loop { - let envelope = reader.poll(chat_id, count)?; - println!("{}", serde_json::to_string(&envelope)?); - io::stdout().flush()?; - std::thread::sleep(std::time::Duration::from_secs_f64(interval)); - } - } - Commands::LocalSearch { - query, - count, - chat_id, - } => { - let reader = local_db::LocalDbReader::open()?; - let results = reader.search_messages(&query, count, chat_id)?; - if json { - println!("{}", serde_json::to_string_pretty(&results)?); - } else { - if results.is_empty() { - println!("No messages matching '{}' in local database.", query); - } else { - for m in &results { - let ts = chrono::Local - .timestamp_opt(m.sent_at, 0) - .single() - .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string()) - .unwrap_or_default(); - let sender = if m.sender_name.is_empty() { - format!("{}", m.author_id) - } else { - m.sender_name.clone() - }; - println!( - " [{}] chat={} {}: {}", - ts, - m.chat_id, - sender, - util::truncate(&m.message, 80) - ); - } - println!("\n{} results (local DB, no server contact)", results.len()); - } - } - } - Commands::LocalSchema => { - let reader = local_db::LocalDbReader::open()?; - let tables = reader.schema()?; - if json { - let items: Vec = tables - .iter() - .map(|(name, sql)| serde_json::json!({"name": name, "sql": sql})) - .collect(); - println!("{}", serde_json::to_string_pretty(&items)?); - } else { - for (name, sql) in &tables { - println!("-- {}", name); - println!("{}\n", sql); - } - } - } - Commands::ContextIndex { - input, - chat, - db, - queue, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let queue_path = queue.as_deref().map(std::path::Path::new); - let count = openkakao_cli::context::index_csv_with_queue( - &db_path, - &chat, - std::path::Path::new(&input), - queue_path, - )?; - if json { - println!( - "{}", - serde_json::json!({ - "action": "index", - "chat": chat, - "input": openkakao_cli::context::provenance_id(&input), - "db": openkakao_cli::context::provenance_id( - &db_path.display().to_string() - ), - "messages": count, - "network": false - }) - ); - } else { - println!( - "Indexed {} messages for '{}' into {} (offline).", - count, - chat, - db_path.display() - ); - } - } - Commands::ContextSyncLocal { - chat_id, - chat, - db, - queue, - interest_only, - } => commands::context_sync_local::cmd_context_sync_local( - chat_id, - chat, - db, - queue, - interest_only, - json, - )?, - Commands::ContextRepairSelfSends { - chat_id, - chat, - db, - queue, - dry_run, - } => { - if chat.trim().is_empty() || chat.len() > 512 || chat.chars().any(char::is_control) { - anyhow::bail!("style repair chat name is invalid"); - } - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - openkakao_cli::context::ensure_live_context_schema(&db_path)?; - let confirmed_sends = match queue.as_deref() { - Some(path) => openkakao_cli::context::confirmed_self_sends_from_queue( - std::path::Path::new(path), - )?, - None => Vec::new(), - }; - let report = openkakao_cli::context::repair_bot_sent_style_samples( - &db_path, - &chat, - chat_id, - &confirmed_sends, - dry_run, - )?; - if json { - println!("{}", serde_json::to_string_pretty(&report)?); - } else { - println!( - "{}: {} matched of {} scanned rows ({} already ineligible, {} ambiguous), {} response samples and {} recipient samples dropped, {} sources rebuilt (dry_run={}).", - chat, - report.matched_rows, - report.scanned_rows, - report.already_ineligible, - report.ambiguous_rows, - report.deleted_response_samples, - report.deleted_recipient_samples, - report.rebuilt_sources.len(), - report.dry_run, - ); - for matched in &report.matches { - println!( - " [{}|{}s] {} <- {}", - matched.log_id, - matched.offset_seconds, - matched.message.chars().take(60).collect::(), - matched.matched_event_id, - ); - } - } - } - Commands::ContextSearch { - query, - chat, - mode, - limit, - source, - db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let mut results = openkakao_cli::context::search( - &db_path, - chat.as_deref(), - source.as_deref(), - &query, - &mode, - limit, - )?; - if json { - openkakao_cli::context::redact_context_results(&mut results); - println!("{}", serde_json::to_string_pretty(&results)?); - } else { - for result in &results { - println!( - "[{:.3}] {} [{}] {}: {}", - result.score, - result.chat, - result.date, - result.user, - util::truncate(&result.message, 160) - ); - } - println!("{} results (offline {} search)", results.len(), mode); - } - } - Commands::ContextReplyBundle { - query, - chat, - chat_id, - current_log_id, - exclude_log_id, - recipient, - source, - db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let output = match (recipient.as_deref(), chat_id, current_log_id) { - (Some(recipient), Some(chat_id), Some(current_log_id)) => { - let mut excluded_log_ids = Vec::with_capacity(1 + exclude_log_id.len()); - excluded_log_ids.push(current_log_id); - excluded_log_ids.extend(exclude_log_id); - openkakao_cli::context::context_reply_bundle_for_recipient_excluding_live_events_json( - &db_path, - &chat, - &query, - source.as_deref(), - recipient, - chat_id, - &excluded_log_ids, - )? - } - (None, None, None) if exclude_log_id.is_empty() => openkakao_cli::context::context_reply_bundle_json( - &db_path, - &chat, - &query, - source.as_deref(), - )?, - _ => anyhow::bail!( - "recipient, chat-id, and current-log-id must be supplied together; exclude-log-id requires that recipient bundle mode" - ), - }; - println!("{output}"); - } - Commands::ContextStyleSearch { - query, - limit, - db, - chat, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let mut results = - openkakao_cli::context::style_search(&db_path, chat.as_deref(), &query, limit)?; - if json { - openkakao_cli::context::redact_context_results(&mut results); - println!("{}", serde_json::to_string_pretty(&results)?); - } else { - for result in &results { - println!( - "[{:.3}] {} [{}] {}: {}", - result.score, - result.chat, - result.date, - result.user, - util::truncate(&result.message, 160) - ); - } - println!( - "{} results (offline 최연우 style vector search)", - results.len() - ); - } - } - Commands::ContextRecipientStyle { - chat, - recipient, - source, - db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let profile = openkakao_cli::context::recipient_style_profile( - &db_path, - &chat, - &recipient, - source.as_deref(), - )?; - if json { - match openkakao_cli::context::recipient_style_profile_json( - &db_path, - &chat, - &recipient, - source.as_deref(), - )? { - Some(payload) => println!("{payload}"), - None => println!("null"), - } - } else if let Some(profile) = profile { - println!( - "{} -> {}: {} direct samples (fallback={}, avg {:.1} chars)", - profile.profile.user, - profile.recipient, - profile.direct_sample_count, - profile.used_fallback, - profile.profile.average_character_length - ); - } else { - println!( - "No recipient style profile for '{}' in '{}'.", - recipient, chat - ); - } - } - Commands::ContextResponseTime { - chat, - user, - source, - db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let stats = openkakao_cli::context::response_time_stats( - &db_path, - &chat, - &user, - source.as_deref(), - )?; - if json { - let mut redacted_stats = stats; - openkakao_cli::context::redact_response_time_stats(&mut redacted_stats); - println!("{}", serde_json::to_string_pretty(&redacted_stats)?); - } else if let Some(stats) = stats { - println!( - "{} average response: {:.1}s (median {:.1}s, p90 {:.1}s, {} samples)", - stats.user, - stats.average_seconds, - stats.median_seconds, - stats.p90_seconds, - stats.sample_count - ); - } else { - println!("No response-time samples for '{}' in '{}'.", user, chat); - } - } - Commands::ContextReplySearch { - query, - chat, - limit, - db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let mut results = - openkakao_cli::context::reply_decision_search(&db_path, &chat, &query, limit)?; - if json { - openkakao_cli::context::redact_reply_decisions(&mut results); - println!("{}", serde_json::to_string_pretty(&results)?); - } else { - for result in &results { - println!( - "[{:.3}] {} {} [{}] {}: {}", - result.score, - result.decision, - result.status, - result.category, - result.author, - util::truncate(&result.message, 160) - ); - } - println!( - "{} results (offline reply-decision vector search)", - results.len() - ); - } - } - Commands::ContextReplyRecord { record, db } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let applied = openkakao_cli::context::record_reply_decision(&db_path, &record)?; - if json { - println!( - "{}", - serde_json::json!({ - "recorded": applied, - "applied": applied, - "db": openkakao_cli::context::provenance_id( - &db_path.display().to_string() - ), - "network": false - }) - ); - } else if applied { - println!( - "Recorded reply decision in {} (offline).", - db_path.display() - ); - } else { - println!("Reply decision was stale and was not recorded (offline)."); - } - } - Commands::ContextReplyUpdate { - event_id, - status, - reply, - sent_at, - db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let updated = openkakao_cli::context::update_reply_decision( - &db_path, - &event_id, - &status, - reply.as_deref(), - sent_at.as_deref(), - )?; - if json { - println!( - "{}", - serde_json::json!({ - "updated": updated, - "applied": updated, - "event_id": event_id, - "db": openkakao_cli::context::provenance_id( - &db_path.display().to_string() - ), - "network": false - }) - ); - } else if updated { - println!("Updated reply decision {} (offline).", event_id); - } else { - println!("Reply decision {} was not found.", event_id); - } - } - Commands::AxServiceScrapeOnce => { - println!( - "{}", - serde_json::to_string(&ax_send::scrape_chat_list_for_service())? - ); - } - Commands::LocalSend { - chat_name, - message, - yes, - dry_run, - reply_to, - preflight, - } => { - let msg = format_outgoing_message(&message, no_prefix); - let Some(setup) = prepare_local_send(&config, &chat_name, dry_run, preflight, json)? - else { - return Ok(()); - }; - commands::local_send::cmd_local_send(commands::local_send::LocalSendOptions { - chat_name, - message: msg, - skip_confirm: yes, - dry_run, - preflight, - json, - bound_chat: setup.bound_chat, - reply_to, - })? - } - Commands::LocalDelete { - chat_name, - source, - yes, - dry_run, - } => { - require_ax_send(&config)?; - require_allowed_send_chat(&config, &chat_name)?; - commands::local_send::cmd_local_delete(&chat_name, &source, yes, dry_run, json)? - } - Commands::AxRead { chat_name, count } => { - commands::ax_read::cmd_ax_read(commands::ax_read::AxReadOptions { - chat_name, - count, - json, - })? - } - Commands::AxProbe { - chat, - dump, - max_nodes, - } => { - if dump { - for line in ax_send::dump_ax(&chat, max_nodes)? { - println!("{line}"); - } - } else { - let report = ax_send::probe_ax(&chat)?; - if json { - crate::util::output_json(&report)?; - } else { - println!( - "chat={:?} window_found={} composer_found={} elapsed_ms={} label={:?}", - report.chat_name, - report.window_found, - report.composer_found, - report.elapsed_ms, - report.label - ); - } - } - } - Commands::AxWatch { - interval, - hook_cmd, - webhook_url, - webhook_header, - webhook_signing_secret, - webhook_format, - hook_chat, - hook_keyword, - hook_fail_fast, - service_mode, - status_path, - log_path, - hook_path, - } => commands::ax_watch::cmd_ax_watch(commands::ax_watch::AxWatchOptions { - interval_secs: interval, - hook_cmd, - webhook_url, - webhook_headers: webhook_header, - webhook_signing_secret, - webhook_format: commands::watch::WebhookFormat::from_str_opt(Some(&webhook_format))?, - hook_chats: hook_chat, - hook_keywords: hook_keyword, - fail_fast: hook_fail_fast, - allow_insecure_webhooks: config.safety.allow_insecure_webhooks, - min_hook_interval_secs, - min_webhook_interval_secs, - hook_timeout_secs, - webhook_timeout_secs, - json, - unattended, - allow_side_effects: allow_watch_side_effects, - service_mode, - status_path: status_path.map(Into::into), - log_path: log_path.map(Into::into), - hook_path: hook_path.map(Into::into), - })?, - Commands::AutoReply { - chat, - check, - self_nickname, - reply_author, - interval, - model, - } => run_auto_reply( - &config, - chat, - check, - self_nickname, - reply_author, - interval, - model, - json, - )?, - Commands::AutoReplyHost { - bake, - status, - disable, - tick, - chat, - manifest, - state_root, - } => { - let action = match (bake, status, disable, tick) { - (true, false, false, false) => commands::auto_reply_host::AutoReplyHostAction::Bake, - (false, true, false, false) => { - commands::auto_reply_host::AutoReplyHostAction::Status - } - (false, false, true, false) => { - commands::auto_reply_host::AutoReplyHostAction::Disable - } - (false, false, false, true) => commands::auto_reply_host::AutoReplyHostAction::Tick, - (false, false, false, false) => { - commands::auto_reply_host::AutoReplyHostAction::Status - } - _ => anyhow::bail!("choose one of --bake, --status, --disable, or --tick"), - }; - commands::auto_reply_host::cmd_auto_reply_host( - commands::auto_reply_host::AutoReplyHostOptions { - action, - chats: chat, - json, - manifest, - state_root, - }, - )? - } - Commands::WatchCache { interval } => commands::auth::cmd_watch_cache(interval)?, - Commands::Doctor { loco, self_check } => { - if self_check { - commands::doctor::cmd_doctor_self_check(json, &config)? - } else { - commands::doctor::cmd_doctor(json, loco, &config)? - } - } - Commands::Dataset { command } => match command { - DatasetCommand::Build { local_only } => { - commands::dataset::cmd_dataset_build(local_only, json)? - } - }, - Commands::Experiment { command } => match command { - ExperimentCommand::Run { prompt } => { - commands::experiment::cmd_experiment_run(prompt, json, &config)? - } - ExperimentCommand::Compare { - version_a, - version_b, - } => commands::experiment::cmd_experiment_compare(version_a, version_b, json)?, - }, - // Handled before `load_config()` so a broken config cannot hide it. - Commands::ReplyReceipt { .. } => { - unreachable!("reply-receipt returns before the config is loaded") - } - } - - if cli.completion_promise { - println!("[DONE]"); - } - - Ok(()) -} - -#[cfg(test)] -mod tests { - use super::*; - - struct FakeLocalMlxProbeTransport { - responses: std::cell::RefCell>, - requests: std::cell::RefCell>, - } - - impl FakeLocalMlxProbeTransport { - fn new(responses: Vec) -> Self { - Self { - responses: std::cell::RefCell::new(responses.into()), - requests: std::cell::RefCell::new(Vec::new()), - } - } - - fn json(status: u16, value: serde_json::Value) -> LocalMlxProbeResponse { - LocalMlxProbeResponse { - status, - body: serde_json::to_vec(&value).expect("serialize fake local MLX response"), - } - } - } - - impl LocalMlxProbeTransport for FakeLocalMlxProbeTransport { - fn send(&self, request: LocalMlxProbeRequest) -> Result { - self.requests.borrow_mut().push(request); - self.responses - .borrow_mut() - .pop_front() - .context("missing fake local MLX response") - } - } - - #[test] - fn retry_bounded_stops_at_the_first_success() { - let mut calls = 0; - let value = retry_bounded(3, std::time::Duration::ZERO, || { - calls += 1; - if calls < 3 { - anyhow::bail!("transient failure {calls}"); - } - Ok(calls) - }) - .expect("third attempt should succeed"); - assert_eq!(value, 3); - assert_eq!(calls, 3, "retry must stop once an attempt succeeds"); - } - - #[test] - fn retry_bounded_surfaces_the_last_error_after_the_budget() { - let mut calls = 0; - let error = retry_bounded::<()>(3, std::time::Duration::ZERO, || { - calls += 1; - anyhow::bail!("failure {calls}") - }) - .expect_err("an exhausted budget must fail"); - assert_eq!(calls, 3, "retry must spend the whole budget"); - assert!( - error.to_string().contains("failure 3"), - "the last error must be surfaced, got {error}" - ); - } - - #[test] - fn retry_bounded_always_attempts_once_and_never_exceeds_the_budget() { - let mut zero_budget_calls = 0; - retry_bounded::<()>(0, std::time::Duration::ZERO, || { - zero_budget_calls += 1; - Ok(()) - }) - .expect("a zero budget still attempts once"); - assert_eq!(zero_budget_calls, 1); - - let mut first_success_calls = 0; - retry_bounded(3, std::time::Duration::ZERO, || { - first_success_calls += 1; - Ok(7) - }) - .expect("the first attempt succeeds"); - assert_eq!(first_success_calls, 1, "a success must not retry"); - } - - #[test] - fn auto_reply_attestation_retry_budget_is_bounded() { - assert!((2..=5).contains(&AUTO_REPLY_ATTEST_ATTEMPTS)); - assert!(AUTO_REPLY_ATTEST_RETRY_PAUSE <= std::time::Duration::from_secs(2)); - } - - #[test] - fn download_local_requires_and_parses_numeric_author_binding() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "download", - "42", - "100", - "--local", - "--expected-author-id", - "700", - "--output-dir", - "/tmp/private-media", - ]) - .expect("local media flags should parse"); - match cli.command { - Commands::Download { - chat_id, - log_id, - output_dir, - local, - expected_author_id, - } => { - assert_eq!(chat_id, 42); - assert_eq!(log_id, 100); - assert_eq!(output_dir.as_deref(), Some("/tmp/private-media")); - assert!(local); - assert_eq!(expected_author_id, Some(700)); - } - other => panic!("expected download command, got {other:?}"), - } - assert!(Cli::try_parse_from([ - "openkakao-cli", - "download", - "42", - "100", - "--expected-author-id", - "700", - ]) - .is_err()); - assert!(Cli::try_parse_from([ - "openkakao-cli", - "download", - "42", - "100", - "--local", - "--expected-author-id", - "0", - ]) - .is_err()); - } - #[cfg(unix)] - use crate::auto_reply_runtime::acquire_worker_setup_lock_nonblocking; - use crate::commands::context_sync_local::tests::local_source_fence; - use crate::commands::members::LocoMemberProfile; - use crate::commands::profile::{ - build_syncmainpf_candidate, collect_hint_chat_ids, local_graph_hint_summary, - parse_profile_cache_hint, LocalFriendGraphChatMeta, LocalFriendGraphEntry, - LocalFriendGraphSnapshot, ProfileCacheHint, - }; - use crate::commands::watch::{ - build_webhook_signature, parse_webhook_header, validate_webhook_url, watch_hook_matches, - WatchHookConfig, WatchMessageEvent, - }; - use crate::loco_helpers::should_retry_loco_probe_error; - use crate::util::{require_permission, validate_outbound_message}; - - #[cfg(unix)] - #[test] - fn codex_auth_rejects_symlink_and_hardlink_credentials() { - use std::os::unix::fs::{symlink, MetadataExt, PermissionsExt}; - - let root = tempfile::tempdir().expect("temporary Codex home"); - let source = root.path().join("auth-source.json"); - fs::write(&source, b"{}").expect("write auth fixture"); - fs::set_permissions(&source, fs::Permissions::from_mode(0o600)) - .expect("make auth fixture private"); - let uid = fs::symlink_metadata(&source).expect("auth metadata").uid(); - validate_auto_reply_codex_auth(&source, uid) - .expect("a private single-link regular credential must pass"); - - let linked = root.path().join("auth-symlink.json"); - symlink(&source, &linked).expect("create symlink fixture"); - assert!(validate_auto_reply_codex_auth(&linked, uid).is_err()); - - let hardlinked = root.path().join("auth-hardlink.json"); - fs::hard_link(&source, &hardlinked).expect("create hardlink fixture"); - assert!(validate_auto_reply_codex_auth(&source, uid).is_err()); - assert!(validate_auto_reply_codex_auth(&hardlinked, uid).is_err()); - fs::remove_file(&hardlinked).expect("remove hardlink"); - fs::write(&source, b"").expect("empty the auth fixture"); - assert!(validate_auto_reply_codex_auth(&source, uid).is_err()); - } - - #[test] - fn final_local_source_fence_allows_later_rows_after_scheduled_source() { - let watcher_last_observed = 100; - let current = local_source_fence(42, watcher_last_observed, watcher_last_observed, &[]); - require_expected_local_source_tail(42, watcher_last_observed, ¤t) - .expect("an exact local source tail should remain eligible"); - - // A later inbound is its own job. It must not cancel the earlier - // scheduled send once the source row itself is still present. - let source_db_advanced = local_source_fence(42, watcher_last_observed, 101, &[101]); - require_expected_local_source_tail(42, watcher_last_observed, &source_db_advanced) - .expect("later room rows must not fence an earlier unanswered inbound"); - - let gap = local_source_fence(42, watcher_last_observed, 101, &[101]); - let mut gap = gap; - gap.completeness.has_gap = true; - let error = require_expected_local_source_tail(42, watcher_last_observed, &gap) - .expect_err("a gapped poll after the source remains fail-closed"); - assert!(error - .to_string() - .contains("scheduled reply source tail is unavailable")); - } - - #[test] - fn proactive_local_source_fence_accepts_exact_self_tail() { - let tail = 3908781794201088001i64; - let current = local_source_fence(42, tail, tail, &[]); - require_expected_local_source_tail(42, tail, ¤t) - .expect("a quiet self tail remains an exact empty after-cursor"); - let advanced = local_source_fence(42, tail, tail + 1, &[tail + 1]); - require_expected_local_source_tail(42, tail, &advanced) - .expect("a newer tail after a quiet self row still keeps the source eligible"); - } - - #[test] - fn auto_reply_supervisor_python_is_environment_and_site_isolated() { - assert_eq!(AUTO_REPLY_PYTHON_ISOLATION_ARGS, ["-E", "-B", "-S"]); - } - - #[test] - fn outgoing_messages_include_prefix_by_default() { - assert_eq!( - format_outgoing_message("hello", false), - "🤖 [Sent via openkakao] hello" - ); - } - - #[test] - fn outgoing_messages_can_disable_prefix() { - assert_eq!(format_outgoing_message("hello", true), "hello"); - } - - #[test] - fn send_accepts_global_and_local_flags_after_subcommand() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "--unattended", - "--allow-non-interactive-send", - "send", - "123", - "hello", - "--no-prefix", - "-y", - ]) - .expect("send should accept global and local flags"); - - assert!(cli.no_prefix); - assert!(cli.unattended); - assert!(cli.allow_non_interactive_send); - match cli.command { - Commands::Send { - chat_id, - message, - yes, - dry_run, - .. - } => { - assert_eq!(chat_id, 123); - assert_eq!(message, "hello"); - assert!(yes); - assert!(!dry_run); - } - other => panic!("expected send command, got {other:?}"), - } - } - - #[test] - fn unattended_flag_is_available_globally() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "--unattended", - "--allow-watch-side-effects", - "watch", - "--hook-cmd", - "cat", - ]) - .expect("global unattended flag should parse"); - - assert!(cli.unattended); - assert!(cli.allow_watch_side_effects); - } - - #[test] - fn permission_gate_rejects_missing_opt_in() { - let err = require_permission(false, "non-interactive send", "set the flags").unwrap_err(); - assert!( - err.to_string().contains("set the flags"), - "unexpected error: {err}" - ); - } - - #[test] - fn watch_hook_filters_match_expected_events() { - let config = WatchHookConfig { - command: Some("cat".to_string()), - webhook_url: None, - webhook_headers: Vec::new(), - webhook_signing_secret: None, - webhook_format: WebhookFormat::Raw, - chat_ids: vec![42], - chat_names: vec![], - keywords: vec!["urgent".to_string()], - message_types: vec![1], - fail_fast: false, - min_hook_interval_secs: 2, - min_webhook_interval_secs: 2, - hook_timeout_secs: 20, - webhook_timeout_secs: 10, - }; - let event = WatchMessageEvent { - event_type: "message", - received_at: "2026-03-08T00:00:00Z".to_string(), - method: "MSG".to_string(), - chat_id: 42, - chat_name: "test".to_string(), - log_id: 7, - author_id: 9, - author_nickname: "alice".to_string(), - message_type: 1, - message: "urgent: ping me".to_string(), - attachment: String::new(), - unread: 0, - }; - - assert!(watch_hook_matches(&config, &event)); - - let wrong_chat = WatchMessageEvent { - chat_id: 99, - ..event.clone() - }; - assert!(!watch_hook_matches(&config, &wrong_chat)); - - let wrong_keyword = WatchMessageEvent { - message: "casual update".to_string(), - ..event.clone() - }; - assert!(!watch_hook_matches(&config, &wrong_keyword)); - } - - #[test] - fn watch_accepts_hook_flags() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "--unattended", - "--allow-watch-side-effects", - "watch", - "--hook-cmd", - "cat >/tmp/openkakao-hook.json", - "--webhook-url", - "https://example.com/openkakao", - "--webhook-header", - "Authorization: Bearer token", - "--webhook-signing-secret", - "super-secret", - "--hook-chat-id", - "123", - "--hook-keyword", - "urgent", - "--hook-type", - "1", - "--hook-fail-fast", - ]) - .expect("watch should accept hook flags"); - - assert!(cli.unattended); - assert!(cli.allow_watch_side_effects); - match cli.command { - Commands::Watch { - hook_cmd, - webhook_url, - webhook_header, - webhook_signing_secret, - hook_chat_id, - hook_keyword, - hook_type, - hook_fail_fast, - .. - } => { - assert_eq!(hook_cmd.as_deref(), Some("cat >/tmp/openkakao-hook.json")); - assert_eq!( - webhook_url.as_deref(), - Some("https://example.com/openkakao") - ); - assert_eq!( - webhook_header, - vec!["Authorization: Bearer token".to_string()] - ); - assert_eq!(webhook_signing_secret.as_deref(), Some("super-secret")); - assert_eq!(hook_chat_id, vec![123]); - assert_eq!(hook_keyword, vec!["urgent".to_string()]); - assert_eq!(hook_type, vec![1]); - assert!(hook_fail_fast); - } - other => panic!("expected watch command, got {other:?}"), - } - } - - #[test] - fn read_accepts_transport_flags() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "read", - "123", - "--rest", - "--delay-ms", - "250", - "--force", - ]) - .expect("read should accept transport flags"); - - match cli.command { - Commands::Read { - chat_id, - rest, - delay_ms, - force, - .. - } => { - assert_eq!(chat_id, 123); - assert!(rest); - assert_eq!(delay_ms, 250); - assert!(force); - } - other => panic!("expected read command, got {other:?}"), - } - } - - #[test] - fn chats_accepts_rest_flag() { - let cli = Cli::try_parse_from(["openkakao-cli", "chats", "--rest", "--unread"]) - .expect("chats should accept --rest"); - - match cli.command { - Commands::Chats { rest, unread, .. } => { - assert!(rest); - assert!(unread); - } - other => panic!("expected chats command, got {other:?}"), - } - } - - #[test] - fn members_accepts_rest_flag() { - let cli = Cli::try_parse_from(["openkakao-cli", "members", "123", "--rest", "--full"]) - .expect("members should accept --rest and --full"); - - match cli.command { - Commands::Members { - chat_id, - rest, - full, - } => { - assert_eq!(chat_id, 123); - assert!(rest); - assert!(full); - } - other => panic!("expected members command, got {other:?}"), - } - } - - #[test] - fn profile_accepts_chat_id_flag() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "profile", - "100000002", - "--chat-id", - "900000000000001", - ]) - .expect("profile should accept --chat-id"); - - match cli.command { - Commands::Profile { - user_id, - chat_id, - local, - } => { - assert_eq!(user_id, 100000002); - assert_eq!(chat_id, Some(900000000000001)); - assert!(!local); - } - other => panic!("expected profile command, got {other:?}"), - } - } - - #[test] - fn friends_accepts_local_flag() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "friends", - "--local", - "-s", - "Alice", - "--chat-id", - "900000000000003", - "--user-id", - "100000003", - ]) - .expect("friends should accept --local"); - - match cli.command { - Commands::Friends { - local, - search, - favorites, - hidden, - chat_id, - user_id, - } => { - assert!(local); - assert_eq!(search.as_deref(), Some("Alice")); - assert!(!favorites); - assert!(!hidden); - assert_eq!(chat_id, Some(900000000000003)); - assert_eq!(user_id, Some(100000003)); - } - other => panic!("expected friends command, got {other:?}"), - } - } - - #[test] - fn profile_accepts_local_flag() { - let cli = Cli::try_parse_from(["openkakao-cli", "profile", "100000002", "--local"]) - .expect("profile should accept --local"); - - match cli.command { - Commands::Profile { - user_id, - chat_id, - local, - } => { - assert_eq!(user_id, 100000002); - assert_eq!(chat_id, None); - assert!(local); - } - other => panic!("expected profile command, got {other:?}"), - } - } - - #[test] - fn chatinfo_command_is_available() { - let cli = Cli::try_parse_from(["openkakao-cli", "chatinfo", "123"]) - .expect("chatinfo should be available"); - - match cli.command { - Commands::Chatinfo { chat_id } => assert_eq!(chat_id, 123), - other => panic!("expected chatinfo command, got {other:?}"), - } - } - - #[test] - fn probe_command_is_available() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "probe", - "BLSYNC", - "--body", - "{\"r\":0,\"pr\":0}", - ]) - .expect("probe should be available"); - - match cli.command { - Commands::Probe { method, body, .. } => { - assert_eq!(method, "BLSYNC"); - assert_eq!(body.as_deref(), Some("{\"r\":0,\"pr\":0}")); - } - other => panic!("expected probe command, got {other:?}"), - } - } - - #[test] - fn profile_hints_command_is_available() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "profile-hints", - "--local-graph", - "--user-id", - "100000003", - "--probe-syncmainpf", - "--probe-uplinkprof", - ]) - .expect("profile-hints should be available"); - - match cli.command { - Commands::ProfileHints { - app_state, - app_state_diff, - local_graph, - user_id, - probe_syncmainpf, - probe_uplinkprof, - } => { - assert!(!app_state); - assert!(app_state_diff.is_none()); - assert!(local_graph); - assert_eq!(user_id, Some(100000003)); - assert!(probe_syncmainpf); - assert!(probe_uplinkprof); - } - other => panic!("expected profile-hints command, got {other:?}"), - } - } - - #[test] - fn profile_hints_accepts_app_state_diff() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "profile-hints", - "--app-state", - "--app-state-diff", - "/tmp/profile-hints-before.json", - ]) - .expect("profile-hints should accept --app-state-diff"); - - match cli.command { - Commands::ProfileHints { - app_state, - app_state_diff, - .. - } => { - assert!(app_state); - assert_eq!( - app_state_diff.as_deref(), - Some("/tmp/profile-hints-before.json") - ); - } - other => panic!("expected profile-hints command, got {other:?}"), - } - } - - #[test] - fn probe_retry_helper_covers_common_socket_failures() { - assert!(should_retry_loco_probe_error(&anyhow::anyhow!("early eof"))); - assert!(should_retry_loco_probe_error(&anyhow::anyhow!( - "Connection reset by peer (os error 54)" - ))); - assert!(should_retry_loco_probe_error(&anyhow::anyhow!( - "broken pipe" - ))); - assert!(!should_retry_loco_probe_error(&anyhow::anyhow!( - "status=-203" - ))); - } - - #[test] - fn parse_friend_profile_cache_hint_extracts_ids_and_access_permit() { - let hint = parse_profile_cache_hint( - 136, - "https://katalk.kakao.com/mac/profile3/friend.json?accessPermit=example-access-permit-token&chatId=900000000000002&id=100000002", - true, - ); - - assert_eq!(hint.kind, "friend"); - assert_eq!(hint.user_ids, vec![100000002]); - assert_eq!(hint.chat_id, Some(900000000000002)); - assert_eq!( - hint.access_permit.as_deref(), - Some("example-access-permit-token") - ); - assert!(hint.data_on_fs); - } - - #[test] - fn parse_friends_profile_cache_hint_extracts_ids_list() { - let hint = parse_profile_cache_hint( - 88, - "https://katalk.kakao.com/mac/profile3/friends.json?category=action&ids=%5B100000004%2C100000005%2C100000006%5D", - false, - ); - - assert_eq!(hint.kind, "friends"); - assert_eq!(hint.user_ids, vec![100000004, 100000005, 100000006]); - assert_eq!(hint.category.as_deref(), Some("action")); - assert_eq!(hint.chat_id, None); - assert_eq!(hint.access_permit, None); - } - - #[test] - fn collect_hint_chat_ids_prefers_user_specific_chat_hints() { - let hints = vec![ - ProfileCacheHint { - entry_id: 1, - kind: "friend".into(), - request_key: String::new(), - user_ids: vec![100000002], - chat_id: Some(900000000000002), - access_permit: Some("permit-a".into()), - category: None, - data_on_fs: true, - }, - ProfileCacheHint { - entry_id: 2, - kind: "friend".into(), - request_key: String::new(), - user_ids: vec![100000002], - chat_id: Some(900000000000002), - access_permit: Some("permit-b".into()), - category: None, - data_on_fs: true, - }, - ProfileCacheHint { - entry_id: 3, - kind: "friend".into(), - request_key: String::new(), - user_ids: vec![100000003], - chat_id: Some(900000000000003), - access_permit: Some("permit-c".into()), - category: None, - data_on_fs: true, - }, - ]; - - assert_eq!( - collect_hint_chat_ids(&hints, 100000002), - vec![900000000000002] - ); - assert_eq!( - collect_hint_chat_ids(&hints, 100000003), - vec![900000000000003] - ); - assert!(collect_hint_chat_ids(&hints, 999).is_empty()); - } - - #[test] - fn parse_loco_member_profile_from_getmem_doc() { - let doc = bson::doc! { - "userId": 100000002_i64, - "accountId": 200000001_i64, - "nickName": "Alice", - "countryIso": "kr", - "statusMessage": "hello", - "profileImageUrl": "https://example.com/p.jpg", - "fullProfileImageUrl": "https://example.com/p-full.jpg", - "originalProfileImageUrl": "https://example.com/p-original.jpg", - "accessPermit": "permit-token", - "suspicion": "", - "suspended": false, - "memorial": false, - "type": 0_i32, - "ut": 100_i64, - }; - - let profile = LocoMemberProfile::from_getmem_doc(&doc); - assert_eq!( - profile, - LocoMemberProfile { - user_id: 100000002, - account_id: 200000001, - nickname: "Alice".into(), - country_iso: "kr".into(), - status_message: "hello".into(), - profile_image_url: "https://example.com/p.jpg".into(), - full_profile_image_url: "https://example.com/p-full.jpg".into(), - original_profile_image_url: "https://example.com/p-original.jpg".into(), - access_permit: "permit-token".into(), - suspicion: String::new(), - suspended: false, - memorial: false, - member_type: 0, - ut: 100, - } - ); - assert_eq!(profile.as_chat_member().display_name(), "Alice"); - } - - #[test] - fn local_graph_summary_carries_getmem_tokens() { - let snapshot = LocalFriendGraphSnapshot { - user_count: 1, - chat_count: 1, - failed_chat_ids: Vec::new(), - chat_meta: vec![LocalFriendGraphChatMeta { - chat_id: 900000000000002, - title: "Example".into(), - getmem_token: Some(777), - member_count: 2, - }], - entries: vec![LocalFriendGraphEntry { - user_id: 100000002, - account_id: 200000001, - nickname: "Alice".into(), - country_iso: "KR".into(), - status_message: String::new(), - profile_image_url: String::new(), - full_profile_image_url: String::new(), - original_profile_image_url: String::new(), - access_permits: vec!["permit-token".into()], - suspicion: String::new(), - suspended: false, - memorial: false, - member_type: 0, - chat_ids: vec![900000000000002], - chat_titles: vec!["Example".into()], - is_self: false, - hidden_like: false, - hidden_block_type: None, - }], - }; - let hints = vec![ProfileCacheHint { - entry_id: 1, - kind: "friend".into(), - request_key: String::new(), - user_ids: vec![100000002], - chat_id: Some(900000000000002), - access_permit: Some("permit-token".into()), - category: None, - data_on_fs: true, - }]; - - let summary = local_graph_hint_summary(&snapshot, &hints); - assert_eq!(summary.candidate_matches.len(), 1); - assert_eq!( - summary.candidate_matches[0].candidate_getmem_tokens, - vec![777] - ); - } - - #[test] - fn syncmainpf_candidates_include_getmem_token_fields() { - let snapshot = LocalFriendGraphSnapshot { - user_count: 1, - chat_count: 1, - failed_chat_ids: Vec::new(), - chat_meta: vec![LocalFriendGraphChatMeta { - chat_id: 900000000000002, - title: "Example".into(), - getmem_token: Some(777), - member_count: 2, - }], - entries: vec![LocalFriendGraphEntry { - user_id: 100000002, - account_id: 200000001, - nickname: "Alice".into(), - country_iso: "KR".into(), - status_message: String::new(), - profile_image_url: String::new(), - full_profile_image_url: String::new(), - original_profile_image_url: String::new(), - access_permits: vec!["permit-token".into()], - suspicion: String::new(), - suspended: false, - memorial: false, - member_type: 0, - chat_ids: vec![900000000000002], - chat_titles: vec!["Example".into()], - is_self: false, - hidden_like: false, - hidden_block_type: None, - }], - }; - - let candidate = build_syncmainpf_candidate(&snapshot, &[], 100000002) - .expect("candidate should be built"); - - assert_eq!(candidate.getmem_tokens, vec![777]); - assert!(candidate - .bodies - .iter() - .any(|body| body.get("token").and_then(|v| v.as_i64()) == Some(777))); - assert!(candidate - .bodies - .iter() - .any(|body| body.get("profileToken").and_then(|v| v.as_i64()) == Some(777))); - assert!(candidate - .uplinkprof_bodies - .iter() - .any(|body| body.get("token").and_then(|v| v.as_i64()) == Some(777))); - } - - #[test] - fn legacy_loco_read_remains_available() { - let cli = Cli::try_parse_from(["openkakao-cli", "loco-read", "123", "--all"]) - .expect("legacy loco-read should remain available"); - - match cli.command { - Commands::LocoRead { chat_id, all, .. } => { - assert_eq!(chat_id, 123); - assert!(all); - } - other => panic!("expected loco-read command, got {other:?}"), + Commands::AxWatch { + interval, + hook_cmd, + webhook_url, + webhook_header, + webhook_signing_secret, + webhook_format, + hook_chat, + hook_keyword, + hook_fail_fast, + service_mode, + status_path, + log_path, + hook_path, + } => commands::ax_watch::cmd_ax_watch(commands::ax_watch::AxWatchOptions { + interval_secs: interval, + hook_cmd, + webhook_url, + webhook_headers: webhook_header, + webhook_signing_secret, + webhook_format: commands::watch::WebhookFormat::from_str_opt(Some(&webhook_format))?, + hook_chats: hook_chat, + hook_keywords: hook_keyword, + fail_fast: hook_fail_fast, + allow_insecure_webhooks: config.safety.allow_insecure_webhooks, + min_hook_interval_secs, + min_webhook_interval_secs, + hook_timeout_secs, + webhook_timeout_secs, + json, + unattended, + allow_side_effects: allow_watch_side_effects, + service_mode, + status_path: status_path.map(Into::into), + log_path: log_path.map(Into::into), + hook_path: hook_path.map(Into::into), + })?, + Commands::AutoReply { + chat, + check, + self_nickname, + reply_author, + interval, + model, + } => commands::auto_reply::run_auto_reply( + &config, + chat, + check, + self_nickname, + reply_author, + interval, + model, + json, + )?, + Commands::AutoReplyHost { + bake, + status, + disable, + tick, + chat, + manifest, + state_root, + } => { + let action = match (bake, status, disable, tick) { + (true, false, false, false) => commands::auto_reply_host::AutoReplyHostAction::Bake, + (false, true, false, false) => { + commands::auto_reply_host::AutoReplyHostAction::Status + } + (false, false, true, false) => { + commands::auto_reply_host::AutoReplyHostAction::Disable + } + (false, false, false, true) => commands::auto_reply_host::AutoReplyHostAction::Tick, + (false, false, false, false) => { + commands::auto_reply_host::AutoReplyHostAction::Status + } + _ => anyhow::bail!("choose one of --bake, --status, --disable, or --tick"), + }; + commands::auto_reply_host::cmd_auto_reply_host( + commands::auto_reply_host::AutoReplyHostOptions { + action, + chats: chat, + json, + manifest, + state_root, + }, + )? } - } - - #[test] - fn legacy_loco_chats_remains_available() { - let cli = Cli::try_parse_from(["openkakao-cli", "loco-chats", "--all"]) - .expect("legacy loco-chats should remain available"); - - match cli.command { - Commands::LocoChats { show_all } => { - assert!(show_all); + Commands::WatchCache { interval } => commands::auth::cmd_watch_cache(interval)?, + Commands::Doctor { loco, self_check } => { + if self_check { + commands::doctor::cmd_doctor_self_check(json, &config)? + } else { + commands::doctor::cmd_doctor(json, loco, &config)? } - other => panic!("expected loco-chats command, got {other:?}"), - } - } - - #[test] - fn legacy_loco_members_remains_available() { - let cli = Cli::try_parse_from(["openkakao-cli", "loco-members", "123"]) - .expect("legacy loco-members should remain available"); - - match cli.command { - Commands::LocoMembers { chat_id } => assert_eq!(chat_id, 123), - other => panic!("expected loco-members command, got {other:?}"), - } - } - - #[test] - fn legacy_loco_chatinfo_remains_available() { - let cli = Cli::try_parse_from(["openkakao-cli", "loco-chatinfo", "123"]) - .expect("legacy loco-chatinfo should remain available"); - - match cli.command { - Commands::LocoChatinfo { chat_id } => assert_eq!(chat_id, 123), - other => panic!("expected loco-chatinfo command, got {other:?}"), } - } - - #[test] - fn legacy_loco_probe_remains_available() { - let cli = Cli::try_parse_from(["openkakao-cli", "loco-probe", "BLSYNC"]) - .expect("legacy loco-probe should remain available"); - - match cli.command { - Commands::LocoProbe { method, body } => { - assert_eq!(method, "BLSYNC"); - assert!(body.is_none()); + Commands::Dataset { command } => match command { + DatasetCommand::Build { local_only } => { + commands::dataset::cmd_dataset_build(local_only, json)? } - other => panic!("expected loco-probe command, got {other:?}"), - } - } - - #[test] - fn webhook_header_requires_name_and_value() { - assert_eq!( - parse_webhook_header("Authorization: Bearer test").unwrap(), - ("Authorization".to_string(), "Bearer test".to_string()) - ); - assert!(parse_webhook_header("MissingSeparator").is_err()); - assert!(parse_webhook_header("Header: ").is_err()); - } - - #[test] - fn webhook_signature_is_stable_for_known_input() { - let signature = build_webhook_signature("secret", "1700000000", br#"{"ok":true}"#).unwrap(); - assert_eq!( - signature, - "sha256=c1afc7c2df3db0690d7d75954610ed1a1d959ce96355ccb8c0a8bc09fd0cfc27" - ); - } - - #[test] - fn webhook_url_requires_https_or_loopback_http() { - assert!(validate_webhook_url("https://example.com/hook", false).is_ok()); - assert!(validate_webhook_url("http://localhost:3000/hook", false).is_ok()); - assert!(validate_webhook_url("http://127.0.0.1:4000/hook", false).is_ok()); - assert!(validate_webhook_url("http://example.com/hook", false).is_err()); - assert!(validate_webhook_url("http://example.com/hook", true).is_ok()); - } - - #[test] - fn outbound_message_must_not_be_blank() { - assert!(validate_outbound_message("hello").is_ok()); - assert!(validate_outbound_message(" ").is_err()); - } - - #[test] - fn stats_command_is_available() { - let cli = Cli::try_parse_from([ - "openkakao-cli", - "stats", - "123", - "--limit", - "500", - "--since", - "2025-01-01", - ]) - .expect("stats should accept limit and since"); - - match cli.command { - Commands::Stats { - chat_id, - limit, - since, - } => { - assert_eq!(chat_id, 123); - assert_eq!(limit, Some(500)); - assert_eq!(since.as_deref(), Some("2025-01-01")); + }, + Commands::Experiment { command } => match command { + ExperimentCommand::Run { prompt } => { + commands::experiment::cmd_experiment_run(prompt, json, &config)? } - other => panic!("expected stats command, got {other:?}"), - } - } - - #[test] - fn openkakao_error_loco_status_display() { - use crate::error::OpenKakaoError; - let err = OpenKakaoError::loco("SYNCMSG", -300); - assert!(err.to_string().contains("SYNCMSG")); - assert!(err.to_string().contains("-300")); - assert!(err.is_retryable()); - } - - #[test] - fn openkakao_error_token_expired_from_950() { - use crate::error::OpenKakaoError; - let err = OpenKakaoError::loco("LOGINLIST", -950); - assert!(matches!(err, OpenKakaoError::TokenExpired)); - assert!(err.is_retryable()); + ExperimentCommand::Compare { + version_a, + version_b, + } => commands::experiment::cmd_experiment_compare(version_a, version_b, json)?, + }, + // Handled before `load_config()` so a broken config cannot hide it. + Commands::ReplyReceipt { .. } => { + unreachable!("reply-receipt returns before the config is loaded") + } } - #[test] - fn openkakao_error_non_retryable_status() { - use crate::error::OpenKakaoError; - let err = OpenKakaoError::loco("WRITE", -203); - assert!(!err.is_retryable()); + if cli.completion_promise { + println!("[DONE]"); } - #[test] - fn check_loco_status_passes_on_zero() { - use crate::loco_helpers::check_loco_status; - let packet = crate::loco::packet::LocoPacket { - packet_id: 1, - status_code: 0, - method: "TEST".into(), - body_type: 0, - body: bson::doc! { "status": 0_i32 }, - }; - assert!(check_loco_status("TEST", &packet).is_ok()); - } + Ok(()) +} - #[test] - fn check_loco_status_fails_on_nonzero() { - use crate::loco_helpers::check_loco_status; - let packet = crate::loco::packet::LocoPacket { - packet_id: 1, - status_code: 0, - method: "SYNCMSG".into(), - body_type: 0, - body: bson::doc! { "status": -300_i32 }, - }; - let err = check_loco_status("SYNCMSG", &packet).unwrap_err(); - assert!(err.to_string().contains("SYNCMSG")); - assert!(err.to_string().contains("-300")); - } +#[cfg(test)] +mod tests { + use super::*; #[test] - fn watch_capture_flag_parses() { - let cli = Cli::try_parse_from(["openkakao-cli", "watch", "--capture"]) - .expect("watch should accept --capture"); - + fn download_local_requires_and_parses_numeric_author_binding() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "download", + "42", + "100", + "--local", + "--expected-author-id", + "700", + "--output-dir", + "/tmp/private-media", + ]) + .expect("local media flags should parse"); match cli.command { - Commands::Watch { capture, .. } => { - assert!(capture); + Commands::Download { + chat_id, + log_id, + output_dir, + local, + expected_author_id, + } => { + assert_eq!(chat_id, 42); + assert_eq!(log_id, 100); + assert_eq!(output_dir.as_deref(), Some("/tmp/private-media")); + assert!(local); + assert_eq!(expected_author_id, Some(700)); } - other => panic!("expected watch command, got {other:?}"), + other => panic!("expected download command, got {other:?}"), } + assert!(Cli::try_parse_from([ + "openkakao-cli", + "download", + "42", + "100", + "--expected-author-id", + "700", + ]) + .is_err()); + assert!(Cli::try_parse_from([ + "openkakao-cli", + "download", + "42", + "100", + "--local", + "--expected-author-id", + "0", + ]) + .is_err()); } + use crate::commands::members::LocoMemberProfile; + use crate::commands::profile::{ + build_syncmainpf_candidate, collect_hint_chat_ids, local_graph_hint_summary, + parse_profile_cache_hint, LocalFriendGraphChatMeta, LocalFriendGraphEntry, + LocalFriendGraphSnapshot, ProfileCacheHint, + }; + use crate::commands::watch::{ + build_webhook_signature, parse_webhook_header, validate_webhook_url, watch_hook_matches, + WatchHookConfig, WatchMessageEvent, + }; + use crate::loco_helpers::should_retry_loco_probe_error; + use crate::util::{require_permission, validate_outbound_message}; #[test] - fn probe_capture_pushes_flag_parses() { - let cli = Cli::try_parse_from(["openkakao-cli", "probe", "PING", "--capture-pushes"]) - .expect("probe should accept --capture-pushes"); + fn outgoing_messages_include_prefix_by_default() { + assert_eq!( + format_outgoing_message("hello", false), + "🤖 [Sent via openkakao] hello" + ); + } - match cli.command { - Commands::Probe { - method, - capture_pushes, - .. - } => { - assert_eq!(method, "PING"); - assert!(capture_pushes); - } - other => panic!("expected probe command, got {other:?}"), - } + #[test] + fn outgoing_messages_can_disable_prefix() { + assert_eq!(format_outgoing_message("hello", true), "hello"); } #[test] - fn delete_command_parses() { - let cli = Cli::try_parse_from(["openkakao-cli", "delete", "123", "456", "--force", "-y"]) - .expect("delete should parse"); + fn send_accepts_global_and_local_flags_after_subcommand() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "--unattended", + "--allow-non-interactive-send", + "send", + "123", + "hello", + "--no-prefix", + "-y", + ]) + .expect("send should accept global and local flags"); + + assert!(cli.no_prefix); + assert!(cli.unattended); + assert!(cli.allow_non_interactive_send); match cli.command { - Commands::Delete { + Commands::Send { chat_id, - log_id, - force, + message, yes, dry_run, + .. } => { assert_eq!(chat_id, 123); - assert_eq!(log_id, 456); - assert!(force); + assert_eq!(message, "hello"); assert!(yes); assert!(!dry_run); } - other => panic!("expected delete, got {other:?}"), + other => panic!("expected send command, got {other:?}"), } } #[test] - fn mark_read_command_parses() { - let cli = Cli::try_parse_from(["openkakao-cli", "mark-read", "123", "456"]) - .expect("mark-read should parse"); - match cli.command { - Commands::MarkRead { - chat_id, - log_id, - yes, - dry_run, - } => { - assert_eq!(chat_id, 123); - assert_eq!(log_id, 456); - assert!(!yes); - assert!(!dry_run); - } - other => panic!("expected mark-read, got {other:?}"), - } + fn unattended_flag_is_available_globally() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "--unattended", + "--allow-watch-side-effects", + "watch", + "--hook-cmd", + "cat", + ]) + .expect("global unattended flag should parse"); + + assert!(cli.unattended); + assert!(cli.allow_watch_side_effects); } #[test] - fn send_me_command_parses() { - let cli = Cli::try_parse_from(["openkakao-cli", "send-me", "test message"]) - .expect("send-me should parse"); - match cli.command { - Commands::SendMe { message, .. } => { - assert_eq!(message, "test message"); - } - other => panic!("expected send-me, got {other:?}"), - } + fn permission_gate_rejects_missing_opt_in() { + let err = require_permission(false, "non-interactive send", "set the flags").unwrap_err(); + assert!( + err.to_string().contains("set the flags"), + "unexpected error: {err}" + ); } #[test] - fn send_accepts_dry_run_flag() { - let cli = Cli::try_parse_from(["openkakao-cli", "send", "123", "hello", "--dry-run"]) - .expect("send --dry-run should parse"); - match cli.command { - Commands::Send { - chat_id, dry_run, .. - } => { - assert_eq!(chat_id, 123); - assert!(dry_run); - } - other => panic!("expected send, got {other:?}"), - } + fn watch_hook_filters_match_expected_events() { + let config = WatchHookConfig { + command: Some("cat".to_string()), + webhook_url: None, + webhook_headers: Vec::new(), + webhook_signing_secret: None, + webhook_format: WebhookFormat::Raw, + chat_ids: vec![42], + chat_names: vec![], + keywords: vec!["urgent".to_string()], + message_types: vec![1], + fail_fast: false, + min_hook_interval_secs: 2, + min_webhook_interval_secs: 2, + hook_timeout_secs: 20, + webhook_timeout_secs: 10, + }; + let event = WatchMessageEvent { + event_type: "message", + received_at: "2026-03-08T00:00:00Z".to_string(), + method: "MSG".to_string(), + chat_id: 42, + chat_name: "test".to_string(), + log_id: 7, + author_id: 9, + author_nickname: "alice".to_string(), + message_type: 1, + message: "urgent: ping me".to_string(), + attachment: String::new(), + unread: 0, + }; + + assert!(watch_hook_matches(&config, &event)); + + let wrong_chat = WatchMessageEvent { + chat_id: 99, + ..event.clone() + }; + assert!(!watch_hook_matches(&config, &wrong_chat)); + + let wrong_keyword = WatchMessageEvent { + message: "casual update".to_string(), + ..event.clone() + }; + assert!(!watch_hook_matches(&config, &wrong_keyword)); } #[test] - fn delete_accepts_dry_run_flag() { - let cli = Cli::try_parse_from(["openkakao-cli", "delete", "123", "456", "--dry-run"]) - .expect("delete --dry-run should parse"); + fn watch_accepts_hook_flags() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "--unattended", + "--allow-watch-side-effects", + "watch", + "--hook-cmd", + "cat >/tmp/openkakao-hook.json", + "--webhook-url", + "https://example.com/openkakao", + "--webhook-header", + "Authorization: Bearer token", + "--webhook-signing-secret", + "super-secret", + "--hook-chat-id", + "123", + "--hook-keyword", + "urgent", + "--hook-type", + "1", + "--hook-fail-fast", + ]) + .expect("watch should accept hook flags"); + + assert!(cli.unattended); + assert!(cli.allow_watch_side_effects); match cli.command { - Commands::Delete { - chat_id, - log_id, - dry_run, + Commands::Watch { + hook_cmd, + webhook_url, + webhook_header, + webhook_signing_secret, + hook_chat_id, + hook_keyword, + hook_type, + hook_fail_fast, .. } => { - assert_eq!(chat_id, 123); - assert_eq!(log_id, 456); - assert!(dry_run); + assert_eq!(hook_cmd.as_deref(), Some("cat >/tmp/openkakao-hook.json")); + assert_eq!( + webhook_url.as_deref(), + Some("https://example.com/openkakao") + ); + assert_eq!( + webhook_header, + vec!["Authorization: Bearer token".to_string()] + ); + assert_eq!(webhook_signing_secret.as_deref(), Some("super-secret")); + assert_eq!(hook_chat_id, vec![123]); + assert_eq!(hook_keyword, vec!["urgent".to_string()]); + assert_eq!(hook_type, vec![1]); + assert!(hook_fail_fast); } - other => panic!("expected delete, got {other:?}"), + other => panic!("expected watch command, got {other:?}"), } } #[test] - fn edit_accepts_dry_run_flag() { + fn read_accepts_transport_flags() { let cli = Cli::try_parse_from([ "openkakao-cli", - "edit", + "read", "123", - "456", - "new text", - "--dry-run", + "--rest", + "--delay-ms", + "250", + "--force", ]) - .expect("edit --dry-run should parse"); + .expect("read should accept transport flags"); + match cli.command { - Commands::Edit { + Commands::Read { chat_id, - log_id, - message, - dry_run, + rest, + delay_ms, + force, .. } => { assert_eq!(chat_id, 123); - assert_eq!(log_id, 456); - assert_eq!(message, "new text"); - assert!(dry_run); + assert!(rest); + assert_eq!(delay_ms, 250); + assert!(force); } - other => panic!("expected edit, got {other:?}"), + other => panic!("expected read command, got {other:?}"), } } #[test] - fn react_accepts_dry_run_flag() { - let cli = Cli::try_parse_from(["openkakao-cli", "react", "123", "456", "--dry-run"]) - .expect("react --dry-run should parse"); + fn chats_accepts_rest_flag() { + let cli = Cli::try_parse_from(["openkakao-cli", "chats", "--rest", "--unread"]) + .expect("chats should accept --rest"); + match cli.command { - Commands::React { - chat_id, - log_id, - dry_run, - .. - } => { - assert_eq!(chat_id, 123); - assert_eq!(log_id, 456); - assert!(dry_run); + Commands::Chats { rest, unread, .. } => { + assert!(rest); + assert!(unread); } - other => panic!("expected react, got {other:?}"), + other => panic!("expected chats command, got {other:?}"), } } #[test] - fn local_chats_command_parses() { - let cli = Cli::try_parse_from(["openkakao-cli", "local-chats", "-n", "10"]) - .expect("local-chats should parse"); + fn members_accepts_rest_flag() { + let cli = Cli::try_parse_from(["openkakao-cli", "members", "123", "--rest", "--full"]) + .expect("members should accept --rest and --full"); + match cli.command { - Commands::LocalChats { limit, groups } => { - assert_eq!(limit, 10); - assert!(!groups); + Commands::Members { + chat_id, + rest, + full, + } => { + assert_eq!(chat_id, 123); + assert!(rest); + assert!(full); } - other => panic!("expected local-chats, got {other:?}"), + other => panic!("expected members command, got {other:?}"), } } #[test] - fn auto_reply_command_accepts_plain_chat_name() { + fn profile_accepts_chat_id_flag() { let cli = Cli::try_parse_from([ "openkakao-cli", - "auto-reply", - "--chat", - "부자멘토멘티", - "--model", - "gemini-3.6-flash", + "profile", + "100000002", + "--chat-id", + "900000000000001", ]) - .expect("plain chat names should parse"); - match cli.command { - Commands::AutoReply { chat, model, .. } => { - assert_eq!(chat, vec!["부자멘토멘티"]); - assert_eq!(model.as_deref(), Some("gemini-3.6-flash")); - } - other => panic!("expected auto-reply, got {other:?}"), - } - let selectors = local_db::parse_chat_selectors(&["부자멘토멘티".into()]) - .expect("plain names become name selectors"); - assert!(matches!( - selectors.as_slice(), - [local_db::ChatSelector::Name(name)] if name == "부자멘토멘티" - )); - } - #[test] - fn auto_reply_host_command_parses() { - let cli = Cli::try_parse_from(["openkakao-cli", "auto-reply-host", "--status"]) - .expect("auto-reply-host should parse"); + .expect("profile should accept --chat-id"); + match cli.command { - Commands::AutoReplyHost { - bake, - status, - disable, - tick, - ref chat, - manifest: _, - state_root: _, + Commands::Profile { + user_id, + chat_id, + local, } => { - assert!(!bake); - assert!(status); - assert!(!disable); - assert!(!tick); - assert!(chat.is_empty()); + assert_eq!(user_id, 100000002); + assert_eq!(chat_id, Some(900000000000001)); + assert!(!local); } - other => panic!("expected auto-reply-host, got {other:?}"), - } - assert!(is_local_only_command(&cli.command)); - } - - #[test] - fn auto_reply_llm_aliases_map_to_attested_models() { - for exact in [ - config::MLX_FLASH_NEXT_MODEL_ID, - config::MLX_FLASH_NEXT_PREFIXED_MODEL_ID, - ] { - assert_eq!( - AutoReplyLlmChoice::from_model(exact), - Some(AutoReplyLlmChoice::MlxQwen38FlashNext) - ); - } - assert_eq!( - AutoReplyLlmChoice::from_model(&format!(" {} ", config::MLX_FLASH_NEXT_MODEL_ID)), - None, - "the local model allowlist must remain exact" - ); - for exact in [config::MLX_27B_MODEL_ID, config::MLX_27B_PREFIXED_MODEL_ID] { - assert_eq!( - AutoReplyLlmChoice::from_model(exact), - Some(AutoReplyLlmChoice::MlxQwen38TwentySevenB) - ); + other => panic!("expected profile command, got {other:?}"), } - let mut json_config = config::OpenKakaoConfig::default(); - json_config.auto_reply.reply_model = Some(config::MLX_FLASH_NEXT_PREFIXED_MODEL_ID.into()); - assert_eq!( - select_auto_reply_llm(&mut json_config, None, true) - .expect("JSON mode should select the configured exact local model"), - AutoReplyLlmChoice::MlxQwen38FlashNext - ); - assert_eq!( - AutoReplyLlmChoice::from_model("gemini-3.7-flash") - .expect("gemini alias") - .model(), - "google-antigravity/gemini-3.7-flash-tiered" - ); - assert_eq!( - AutoReplyLlmChoice::from_model("gpt-5.6-luna") - .expect("luna alias") - .model(), - "gpt-5.6-luna" - ); - assert_eq!( - AutoReplyLlmChoice::from_model("deepseek-v4.1-flash") - .expect("opencode deepseek alias") - .model(), - "opencode-go-session/deepseek-v4.1-flash" - ); - let mut config = config::OpenKakaoConfig::default(); - AutoReplyLlmChoice::from_model("deepseek-v4.1-flash") - .expect("opencode deepseek alias") - .apply(&mut config); - assert_eq!( - config.model.provider.as_deref(), - Some("opencode-go-session") - ); - assert_eq!(config.auto_reply.reply_runner_kind.as_deref(), Some("gjc")); - assert_eq!(AutoReplyLlmChoice::all().len(), 5); - } - - #[test] - fn auto_reply_local_mlx_choice_applies_fail_closed_profile() { - let mut config = config::OpenKakaoConfig::default(); - config.model.privacy_mode = Some("remote_explicit".into()); - config.model.allow_egress = true; - config.model.provider = Some("openai-codex".into()); - config.model.retention = Some("provider-policy".into()); - - AutoReplyLlmChoice::MlxQwen38FlashNext.apply(&mut config); - - assert_eq!(config.model.privacy_mode.as_deref(), Some("local")); - assert!(!config.model.allow_egress); - assert_eq!( - config.model.provider.as_deref(), - Some(config::MLX_SERVE_PROVIDER) - ); - assert!(config.model.retention.is_none()); - assert_eq!( - config.auto_reply.reply_model.as_deref(), - Some(config::MLX_FLASH_NEXT_MODEL_ID) - ); - assert_eq!( - config.auto_reply.reply_runner_kind.as_deref(), - Some("opencodex") - ); - assert_eq!( - config.auto_reply.reply_runner.as_deref(), - Some("/opt/homebrew/bin/opencodex") - ); - config::validate_auto_reply_local_mlx_profile(&config) - .expect("applied local MLX profile must validate"); } #[test] - fn auto_reply_local_mlx_probe_uses_only_bounded_loopback_requests() { - let mut config = config::OpenKakaoConfig::default(); - AutoReplyLlmChoice::MlxQwen38FlashNext.apply(&mut config); - let transport = FakeLocalMlxProbeTransport::new(vec![ - FakeLocalMlxProbeTransport::json( - 200, - serde_json::json!({ - "data": [{ - "id": config::MLX_FLASH_NEXT_PREFIXED_MODEL_ID, - "loaded": true, - "state": "ready" - }] - }), - ), - FakeLocalMlxProbeTransport::json( - 200, - serde_json::json!({"choices": [{"message": {"content": "확인"}}]}), - ), - ]); - - probe_auto_reply_llm_with_local_transport( - &config, - AutoReplyLlmChoice::MlxQwen38FlashNext, - &transport, - ) - .expect("ready local MLX model should pass the bounded probe"); + fn friends_accepts_local_flag() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "friends", + "--local", + "-s", + "Alice", + "--chat-id", + "900000000000003", + "--user-id", + "100000003", + ]) + .expect("friends should accept --local"); - let requests = transport.requests.borrow(); - assert_eq!(requests.len(), 2); - assert_eq!(requests[0].method, LocalMlxProbeMethod::Get); - assert_eq!(requests[0].url, LOCAL_MLX_MODELS_URL); - assert!(requests[0].body.is_none()); - assert_eq!(requests[1].method, LocalMlxProbeMethod::Post); - assert_eq!(requests[1].url, LOCAL_MLX_CHAT_URL); - assert!(requests.iter().all(|request| { - request.timeout == LOCAL_MLX_PROBE_TIMEOUT - && request.max_response_bytes == LOCAL_MLX_MAX_RESPONSE_BYTES - && request.url.starts_with(LOCAL_MLX_BASE_URL) - && !request.url.contains("10100") - && !request.url.contains("/load") - && !request.url.contains("/unload") - && !request.url.contains("/swap") - })); - let generation: serde_json::Value = - serde_json::from_slice(requests[1].body.as_deref().expect("generation probe body")) - .expect("parse generation probe body"); - assert_eq!( - generation.get("model").and_then(serde_json::Value::as_str), - Some(config::MLX_FLASH_NEXT_MODEL_ID) - ); - assert_eq!( - generation - .get("max_tokens") - .and_then(serde_json::Value::as_u64), - Some(8) - ); - assert_eq!( - generation - .pointer("/messages/0/content") - .and_then(serde_json::Value::as_str), - Some("확인이라고만 답하세요.") - ); + match cli.command { + Commands::Friends { + local, + search, + favorites, + hidden, + chat_id, + user_id, + } => { + assert!(local); + assert_eq!(search.as_deref(), Some("Alice")); + assert!(!favorites); + assert!(!hidden); + assert_eq!(chat_id, Some(900000000000003)); + assert_eq!(user_id, Some(100000003)); + } + other => panic!("expected friends command, got {other:?}"), + } } #[test] - fn auto_reply_local_mlx_probe_fails_closed_without_generation_or_remote_fallback() { - let mut config = config::OpenKakaoConfig::default(); - AutoReplyLlmChoice::MlxQwen38FlashNext.apply(&mut config); - let not_ready = FakeLocalMlxProbeTransport::new(vec![FakeLocalMlxProbeTransport::json( - 200, - serde_json::json!({ - "data": [{ - "id": config::MLX_FLASH_NEXT_MODEL_ID, - "loaded": false, - "state": "unloaded" - }] - }), - )]); - let error = probe_auto_reply_llm_with_local_transport( - &config, - AutoReplyLlmChoice::MlxQwen38FlashNext, - ¬_ready, - ) - .expect_err("an unloaded model must fail before generation"); - assert_eq!(error.to_string(), "local MLX model is not ready"); - assert_eq!(not_ready.requests.borrow().len(), 1); - - let malformed = FakeLocalMlxProbeTransport::new(vec![LocalMlxProbeResponse { - status: 200, - body: b"{not-json".to_vec(), - }]); - assert!(probe_auto_reply_llm_with_local_transport( - &config, - AutoReplyLlmChoice::MlxQwen38FlashNext, - &malformed, - ) - .is_err()); - assert_eq!(malformed.requests.borrow().len(), 1); - - let redirected = FakeLocalMlxProbeTransport::new(vec![FakeLocalMlxProbeTransport::json( - 302, - serde_json::json!({"redirect": true}), - )]); - let redirect_error = probe_auto_reply_llm_with_local_transport( - &config, - AutoReplyLlmChoice::MlxQwen38FlashNext, - &redirected, - ) - .expect_err("redirects must fail closed"); - assert!(redirect_error.to_string().contains("HTTP 302")); - assert_eq!(redirected.requests.borrow().len(), 1); + fn profile_accepts_local_flag() { + let cli = Cli::try_parse_from(["openkakao-cli", "profile", "100000002", "--local"]) + .expect("profile should accept --local"); - config.auto_reply.reply_model = Some("google-antigravity/gemini-3.8-flash".into()); - let invalid_profile = FakeLocalMlxProbeTransport::new(Vec::new()); - assert!(probe_auto_reply_llm_with_local_transport( - &config, - AutoReplyLlmChoice::MlxQwen38FlashNext, - &invalid_profile, - ) - .is_err()); - assert!(invalid_profile.requests.borrow().is_empty()); + match cli.command { + Commands::Profile { + user_id, + chat_id, + local, + } => { + assert_eq!(user_id, 100000002); + assert_eq!(chat_id, None); + assert!(local); + } + other => panic!("expected profile command, got {other:?}"), + } } #[test] - fn auto_reply_local_mlx_transport_rejects_non_fixed_endpoints_before_network() { - for url in [ - "http://127.0.0.1:10100/v1/models", - "https://models.example.invalid/v1/models", - ] { - let error = match ReqwestLocalMlxProbeTransport.send(LocalMlxProbeRequest { - method: LocalMlxProbeMethod::Get, - url, - body: None, - timeout: LOCAL_MLX_PROBE_TIMEOUT, - max_response_bytes: LOCAL_MLX_MAX_RESPONSE_BYTES, - }) { - Ok(_) => panic!("non-fixed endpoint must be rejected before transport"), - Err(error) => error, - }; - assert!(error.to_string().contains("non-loopback endpoint")); + fn chatinfo_command_is_available() { + let cli = Cli::try_parse_from(["openkakao-cli", "chatinfo", "123"]) + .expect("chatinfo should be available"); + + match cli.command { + Commands::Chatinfo { chat_id } => assert_eq!(chat_id, 123), + other => panic!("expected chatinfo command, got {other:?}"), } } #[test] - fn beginner_plain_name_uses_configured_bind_selector() { - let expanded = expand_plain_chat_names_with_configured_bindings( - vec!["부자멘토멘티".into()], - &["bind:417780809780519:부자멘토멘티".into()], - ); - assert_eq!(expanded, vec!["bind:417780809780519:부자멘토멘티"]); - let untouched = expand_plain_chat_names_with_configured_bindings( - vec!["name:다른방".into()], - &["bind:417780809780519:부자멘토멘티".into()], - ); - assert_eq!(untouched, vec!["name:다른방"]); - } + fn probe_command_is_available() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "probe", + "BLSYNC", + "--body", + "{\"r\":0,\"pr\":0}", + ]) + .expect("probe should be available"); - #[test] - fn catalog_merge_appends_auto_reply_rooms() { - let merged = room_catalog::merge_configured_and_catalog_selectors( - &["bind:42:부자멘토멘티".into()], - &[99], - &[ - local_db::LocalChat { - chat_id: 42, - chat_type: 1, - chat_name: "부자멘토멘티".into(), - database_chat_name: None, - active_members_count: 4, - last_log_id: 1, - last_updated_at: 0, - unread_count: 0, - display_name: "부자멘토멘티".into(), - }, - local_db::LocalChat { - chat_id: 99, - chat_type: 1, - chat_name: "kakao-test".into(), - database_chat_name: None, - active_members_count: 2, - last_log_id: 2, - last_updated_at: 0, - unread_count: 0, - display_name: "kakao-test".into(), - }, - ], - ) - .expect("merge"); - assert_eq!( - merged, - vec![ - "bind:42:부자멘토멘티".to_string(), - "bind:99:kakao-test".to_string() - ] - ); + match cli.command { + Commands::Probe { method, body, .. } => { + assert_eq!(method, "BLSYNC"); + assert_eq!(body.as_deref(), Some("{\"r\":0,\"pr\":0}")); + } + other => panic!("expected probe command, got {other:?}"), + } } #[test] - fn auto_reply_command_accepts_repeated_chat_selectors() { + fn profile_hints_command_is_available() { let cli = Cli::try_parse_from([ "openkakao-cli", - "auto-reply", - "--chat", - "id:42", - "--chat", - "name:투자 공부방", - "--self-nickname", - "self", - "--reply-author", - "author", - "--check", + "profile-hints", + "--local-graph", + "--user-id", + "100000003", + "--probe-syncmainpf", + "--probe-uplinkprof", ]) - .expect("auto-reply should parse"); + .expect("profile-hints should be available"); + match cli.command { - Commands::AutoReply { - chat, - check, - self_nickname, - reply_author, - .. + Commands::ProfileHints { + app_state, + app_state_diff, + local_graph, + user_id, + probe_syncmainpf, + probe_uplinkprof, } => { - assert_eq!(chat, vec!["id:42", "name:투자 공부방"]); - assert_eq!(self_nickname.as_deref(), Some("self")); - assert_eq!(reply_author, vec!["author"]); - assert!(check); + assert!(!app_state); + assert!(app_state_diff.is_none()); + assert!(local_graph); + assert_eq!(user_id, Some(100000003)); + assert!(probe_syncmainpf); + assert!(probe_uplinkprof); } - other => panic!("expected auto-reply, got {other:?}"), + other => panic!("expected profile-hints command, got {other:?}"), } } - fn enrollment_fixture(room_root: &Path, identity: serde_json::Value) -> serde_json::Value { - serde_json::json!({ - "schema_version": AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION, - "activation": "foreground", - "selectors": ["bind:42:부자멘토멘티"], - "runtime_root": room_root.parent().and_then(Path::parent).unwrap(), - "created_at": "2026-08-12T00:00:00Z", - "targets": [{ - "chat_id": 42, - "chat_name": "부자멘토멘티", - "last_log_id": 100, - "room_state_root": room_root, - "identity": identity, - "cursor_authority": { - "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, - "kind": AUTO_REPLY_CURSOR_FRESH_KIND, - "cursor_floor": 100, - "attested_db_last_log_id": 100, - "prior_owner_id": null, - "prior_source_epoch": null, - }, - "reply_author_bindings": [{"nickname": "현준", "author_id": 700}], - }], - }) - } - #[test] - fn cli_enrollment_v4_local_name_authority_matches_final_send_gate() { - let root = tempfile::tempdir().expect("temporary state root"); - let room_root = root.path().join("rooms/42"); - fs::create_dir_all(&room_root).expect("create room root"); - let enrollment = enrollment_fixture( - &room_root, - serde_json::json!({ - "schema_version": 1, - "kind": "local_name", - "local_name": "부자멘토멘티", - "ax_name": "부자멘토멘티", - }), - ); - validate_cli_enrollment_authority(&enrollment, 42, "부자멘토멘티", 100, &room_root) - .expect("v4 local-name enrollment should pass the final gate"); + fn profile_hints_accepts_app_state_diff() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "profile-hints", + "--app-state", + "--app-state-diff", + "/tmp/profile-hints-before.json", + ]) + .expect("profile-hints should accept --app-state-diff"); - let raw = serde_json::to_vec(&enrollment).expect("serialize enrollment"); - let digest = hex::encode(Sha256::digest(&raw)); - validate_cli_enrollment_digest(&raw, &digest).expect("matching digest should pass"); - assert!(validate_cli_enrollment_digest(&raw, &"A".repeat(64)).is_err()); - assert!(validate_cli_enrollment_digest(&raw, &"0".repeat(64)).is_err()); + match cli.command { + Commands::ProfileHints { + app_state, + app_state_diff, + .. + } => { + assert!(app_state); + assert_eq!( + app_state_diff.as_deref(), + Some("/tmp/profile-hints-before.json") + ); + } + other => panic!("expected profile-hints command, got {other:?}"), + } } #[test] - fn cli_enrollment_v4_transcript_and_replay_authority_are_strict() { - let root = tempfile::tempdir().expect("temporary state root"); - let room_root = root.path().join("rooms/42"); - fs::create_dir_all(&room_root).expect("create room root"); - let identity = serde_json::json!({ - "schema_version": 1, - "kind": "ax_transcript", - "local_name": "", - "ax_name": "부자멘토멘티", - "matched_log_ids": [98, 99, 100], - "matched_count": 3, - "matched_utf8_bytes": 24, - "transcript_sha256": "a".repeat(64), - "attested_db_last_log_id": 100, - }); - let enrollment = enrollment_fixture(&room_root, identity.clone()); - validate_cli_enrollment_authority(&enrollment, 42, "부자멘토멘티", 100, &room_root) - .expect("strict transcript enrollment should pass the final gate"); - - let two_row = enrollment_fixture( - &room_root, - serde_json::json!({ - "schema_version": 1, - "kind": "ax_transcript", - "local_name": "", - "ax_name": "부자멘토멘티", - "matched_log_ids": [99, 100], - "matched_count": 2, - "matched_utf8_bytes": 19, - "transcript_sha256": "a".repeat(64), - "attested_db_last_log_id": 100, - }), - ); - validate_cli_enrollment_authority(&two_row, 42, "부자멘토멘티", 100, &room_root) - .expect("two-row distinct suffix enrollment must match is_strong bind"); - let one_row = enrollment_fixture( - &room_root, - serde_json::json!({ - "schema_version": 1, - "kind": "ax_transcript", - "local_name": "", - "ax_name": "부자멘토멘티", - "matched_log_ids": [100], - "matched_count": 1, - "matched_utf8_bytes": 19, - "transcript_sha256": "a".repeat(64), - "attested_db_last_log_id": 100, - }), - ); - assert!( - validate_cli_enrollment_authority(&one_row, 42, "부자멘토멘티", 100, &room_root) - .is_err() - ); - - let mut malformed = enrollment_fixture(&room_root, identity); - malformed["targets"][0]["identity"]["matched_count"] = serde_json::Value::from(2); - assert!( - validate_cli_enrollment_authority(&malformed, 42, "부자멘토멘티", 100, &room_root,) - .is_err() - ); - malformed["targets"][0]["identity"]["matched_count"] = serde_json::Value::from(3); - malformed["schema_version"] = serde_json::Value::from(3); - assert!( - validate_cli_enrollment_authority(&malformed, 42, "부자멘토멘티", 100, &room_root,) - .is_err() - ); - - let mut replay_floor = enrollment_fixture( - &room_root, - serde_json::json!({ - "schema_version": 1, - "kind": "ax_transcript", - "local_name": "", - "ax_name": "부자멘토멘티", - "matched_log_ids": [10, 11, 12], - "matched_count": 3, - "matched_utf8_bytes": 24, - "transcript_sha256": "a".repeat(64), - "attested_db_last_log_id": 101, - }), - ); - replay_floor["targets"][0]["last_log_id"] = serde_json::Value::from(100); - replay_floor["targets"][0]["cursor_authority"] = serde_json::json!({ - "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, - "kind": AUTO_REPLY_CURSOR_REPLAY_KIND, - "cursor_floor": 100, - "attested_db_last_log_id": 101, - "prior_owner_id": "prior-owner", - "prior_source_epoch": 7, - }); - validate_cli_enrollment_authority(&replay_floor, 42, "부자멘토멘티", 100, &room_root) - .expect("an ACK floor before the fresh attested tail must replay safely"); - - let mut missing_replay_contract = replay_floor.clone(); - missing_replay_contract["targets"][0]["cursor_authority"] = serde_json::json!({ - "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, - "kind": AUTO_REPLY_CURSOR_FRESH_KIND, - "cursor_floor": 100, - "attested_db_last_log_id": 101, - "prior_owner_id": null, - "prior_source_epoch": null, - }); - assert!(validate_cli_enrollment_authority( - &missing_replay_contract, - 42, - "부자멘토멘티", - 100, - &room_root, - ) - .is_err()); - - let mut old_runtime_schema = replay_floor.clone(); - old_runtime_schema["schema_version"] = serde_json::Value::from(3); - assert!(validate_cli_enrollment_authority( - &old_runtime_schema, - 42, - "부자멘토멘티", - 100, - &room_root, - ) - .is_err()); - - replay_floor["targets"][0]["identity"]["attested_db_last_log_id"] = - serde_json::Value::from(11); - assert!(validate_cli_enrollment_authority( - &replay_floor, - 42, - "부자멘토멘티", - 100, - &room_root, - ) - .is_err()); + fn probe_retry_helper_covers_common_socket_failures() { + assert!(should_retry_loco_probe_error(&anyhow::anyhow!("early eof"))); + assert!(should_retry_loco_probe_error(&anyhow::anyhow!( + "Connection reset by peer (os error 54)" + ))); + assert!(should_retry_loco_probe_error(&anyhow::anyhow!( + "broken pipe" + ))); + assert!(!should_retry_loco_probe_error(&anyhow::anyhow!( + "status=-203" + ))); } #[test] - fn numeric_reply_author_binding_fails_closed_on_collision_self_and_drift() { - let configured = - std::collections::BTreeSet::from(["문승현".to_string(), "현준".to_string()]); - let identities = vec![ - local_db::LocalAuthorIdentity { - author_id: 700, - nickname: "문승현".to_string(), - is_self: false, - }, - local_db::LocalAuthorIdentity { - author_id: 701, - nickname: "현준".to_string(), - is_self: false, - }, - ]; - let bindings = - resolve_auto_reply_author_bindings("부자멘토멘티", 999, &identities, &configured) - .expect("unique non-self identities should enroll"); - assert_eq!( - bindings, - vec![ - AutoReplyAuthorBinding { - nickname: "문승현".to_string(), - author_id: 700, - }, - AutoReplyAuthorBinding { - nickname: "현준".to_string(), - author_id: 701, - }, - ] + fn parse_friend_profile_cache_hint_extracts_ids_and_access_permit() { + let hint = parse_profile_cache_hint( + 136, + "https://katalk.kakao.com/mac/profile3/friend.json?accessPermit=example-access-permit-token&chatId=900000000000002&id=100000002", + true, ); - let collision = [ - identities.clone(), - vec![local_db::LocalAuthorIdentity { - author_id: 702, - nickname: "현준".to_string(), - is_self: false, - }], - ] - .concat(); - assert!( - resolve_auto_reply_author_bindings("부자멘토멘티", 999, &collision, &configured,) - .is_err() + assert_eq!(hint.kind, "friend"); + assert_eq!(hint.user_ids, vec![100000002]); + assert_eq!(hint.chat_id, Some(900000000000002)); + assert_eq!( + hint.access_permit.as_deref(), + Some("example-access-permit-token") ); - - let ambiguous_name_rows = vec![ - local_db::LocalAuthorIdentity { - author_id: 701, - nickname: "현준".to_string(), - is_self: false, - }, - local_db::LocalAuthorIdentity { - author_id: 701, - nickname: "현준(다른표시명)".to_string(), - is_self: false, - }, - ]; - assert!(resolve_auto_reply_author_bindings( - "부자멘토멘티", - 999, - &ambiguous_name_rows, - &std::collections::BTreeSet::from(["현준".to_string()]), - ) - .is_err()); - - let self_bound = vec![local_db::LocalAuthorIdentity { - author_id: 999, - nickname: "현준".to_string(), - is_self: true, - }]; - assert!(resolve_auto_reply_author_bindings( - "부자멘토멘티", - 999, - &self_bound, - &std::collections::BTreeSet::from(["현준".to_string()]), - ) - .is_err()); - - let inconsistent_self_proof = vec![local_db::LocalAuthorIdentity { - author_id: 701, - nickname: "현준".to_string(), - is_self: true, - }]; - assert!(resolve_auto_reply_author_bindings( - "부자멘토멘티", - 999, - &inconsistent_self_proof, - &std::collections::BTreeSet::from(["현준".to_string()]), - ) - .is_err()); + assert!(hint.data_on_fs); } #[test] - fn cli_enrollment_v4_reply_author_binding_is_strict() { - let root = tempfile::tempdir().expect("temporary state root"); - let room_root = root.path().join("rooms/42"); - fs::create_dir_all(&room_root).expect("create room root"); - let identity = serde_json::json!({ - "schema_version": 1, - "kind": "local_name", - "local_name": "부자멘토멘티", - "ax_name": "부자멘토멘티", - }); - let enrollment = enrollment_fixture(&room_root, identity); - require_cli_enrolled_reply_author(&enrollment, 42, 700, "현준") - .expect("matching numeric identity should pass"); - assert!(require_cli_enrolled_reply_author(&enrollment, 42, 701, "현준").is_err()); - assert!(require_cli_enrolled_reply_author(&enrollment, 42, 700, "동명이인").is_err()); + fn parse_friends_profile_cache_hint_extracts_ids_list() { + let hint = parse_profile_cache_hint( + 88, + "https://katalk.kakao.com/mac/profile3/friends.json?category=action&ids=%5B100000004%2C100000005%2C100000006%5D", + false, + ); - let mut duplicate_id = enrollment.clone(); - duplicate_id["targets"][0]["reply_author_bindings"] = serde_json::json!([ - {"nickname": "문승현", "author_id": 700}, - {"nickname": "현준", "author_id": 700} - ]); - assert!(validate_cli_enrollment_authority( - &duplicate_id, - 42, - "부자멘토멘티", - 100, - &room_root, - ) - .is_err()); + assert_eq!(hint.kind, "friends"); + assert_eq!(hint.user_ids, vec![100000004, 100000005, 100000006]); + assert_eq!(hint.category.as_deref(), Some("action")); + assert_eq!(hint.chat_id, None); + assert_eq!(hint.access_permit, None); } #[test] - fn multi_room_enrollment_keeps_numeric_author_bindings_disjoint() { - let root = tempfile::tempdir().expect("temporary state root"); - let root_path = root.path().canonicalize().expect("canonical state root"); - let targets = [ - local_db::LocalChat { - chat_id: 42, - chat_type: 0, - chat_name: "room-a".to_string(), - database_chat_name: Some("room-a".to_string()), - active_members_count: 2, - last_log_id: 100, - last_updated_at: 0, - unread_count: 0, - display_name: "room-a".to_string(), + fn collect_hint_chat_ids_prefers_user_specific_chat_hints() { + let hints = vec![ + ProfileCacheHint { + entry_id: 1, + kind: "friend".into(), + request_key: String::new(), + user_ids: vec![100000002], + chat_id: Some(900000000000002), + access_permit: Some("permit-a".into()), + category: None, + data_on_fs: true, + }, + ProfileCacheHint { + entry_id: 2, + kind: "friend".into(), + request_key: String::new(), + user_ids: vec![100000002], + chat_id: Some(900000000000002), + access_permit: Some("permit-b".into()), + category: None, + data_on_fs: true, }, - local_db::LocalChat { - chat_id: 84, - chat_type: 0, - chat_name: "room-b".to_string(), - database_chat_name: Some("room-b".to_string()), - active_members_count: 2, - last_log_id: 200, - last_updated_at: 0, - unread_count: 0, - display_name: "room-b".to_string(), + ProfileCacheHint { + entry_id: 3, + kind: "friend".into(), + request_key: String::new(), + user_ids: vec![100000003], + chat_id: Some(900000000000003), + access_permit: Some("permit-c".into()), + category: None, + data_on_fs: true, }, ]; - for target in &targets { - fs::create_dir_all(root_path.join("rooms").join(target.chat_id.to_string())) - .expect("create independent room root"); - } - let author_bindings = std::collections::BTreeMap::from([ - ( - 42, - vec![AutoReplyAuthorBinding { - nickname: "alice".to_string(), - author_id: 700, - }], - ), - ( - 84, - vec![AutoReplyAuthorBinding { - nickname: "bob".to_string(), - author_id: 800, - }], - ), - ]); - let (floors, _) = write_auto_reply_enrollment( - &root_path, - &["id:42".to_string(), "id:84".to_string()], - &targets, - &[], - &author_bindings, - &root_path.join("runtime"), - ) - .expect("write independent multi-room enrollment"); - assert_eq!(floors, [100, 200]); - let enrollment = read_bounded_json_file(&root_path.join("enrollment.json")) - .expect("read multi-room enrollment"); - assert_eq!( - enrollment["targets"][0]["room_state_root"], - serde_json::json!(root_path.join("rooms/42")) - ); - assert_eq!( - enrollment["targets"][1]["room_state_root"], - serde_json::json!(root_path.join("rooms/84")) - ); + assert_eq!( - enrollment["targets"][0]["reply_author_bindings"], - serde_json::json!([{"nickname": "alice", "author_id": 700}]) + collect_hint_chat_ids(&hints, 100000002), + vec![900000000000002] ); assert_eq!( - enrollment["targets"][1]["reply_author_bindings"], - serde_json::json!([{"nickname": "bob", "author_id": 800}]) - ); - assert_ne!( - enrollment["targets"][0]["reply_author_bindings"], - enrollment["targets"][1]["reply_author_bindings"] + collect_hint_chat_ids(&hints, 100000003), + vec![900000000000003] ); + assert!(collect_hint_chat_ids(&hints, 999).is_empty()); } #[test] - fn multi_room_supervisor_commands_keep_author_policy_disjoint() { - fn env_value(command: &Command, key: &str) -> String { - command - .get_envs() - .find(|(candidate, _)| *candidate == std::ffi::OsStr::new(key)) - .and_then(|(_, value)| value) - .expect("expected supervisor environment entry") - .to_string_lossy() - .into_owned() - } - fn args(command: &Command) -> Vec { - command - .get_args() - .map(|value| value.to_string_lossy().into_owned()) - .collect() - } - - let root = tempfile::tempdir().expect("temporary shared state root"); - let mut room_a = Command::new("supervisor"); - configure_auto_reply_supervisor_shared_state(&mut room_a, root.path()); - configure_auto_reply_supervisor_author_policy( - &mut room_a, - &["alice".to_string()], - r#"[{"nickname":"alice","author_id":700}]"#, - ); - let mut room_b = Command::new("supervisor"); - configure_auto_reply_supervisor_shared_state(&mut room_b, root.path()); - configure_auto_reply_supervisor_author_policy( - &mut room_b, - &["bob".to_string()], - r#"[{"nickname":"bob","author_id":800}]"#, - ); + fn parse_loco_member_profile_from_getmem_doc() { + let doc = bson::doc! { + "userId": 100000002_i64, + "accountId": 200000001_i64, + "nickName": "Alice", + "countryIso": "kr", + "statusMessage": "hello", + "profileImageUrl": "https://example.com/p.jpg", + "fullProfileImageUrl": "https://example.com/p-full.jpg", + "originalProfileImageUrl": "https://example.com/p-original.jpg", + "accessPermit": "permit-token", + "suspicion": "", + "suspended": false, + "memorial": false, + "type": 0_i32, + "ut": 100_i64, + }; - assert_eq!(args(&room_a), ["--reply-author", "alice"]); - assert_eq!(args(&room_b), ["--reply-author", "bob"]); - assert_eq!(env_value(&room_a, "OPENKAKAO_REPLY_AUTHORS"), "alice"); - assert_eq!(env_value(&room_b, "OPENKAKAO_REPLY_AUTHORS"), "bob"); - let expected_circuit = root.path().join("model-circuit.sqlite3"); - assert_eq!( - env_value(&room_a, "OPENKAKAO_MODEL_CIRCUIT_DB"), - expected_circuit.to_string_lossy() - ); - assert_eq!( - env_value(&room_b, "OPENKAKAO_MODEL_CIRCUIT_DB"), - expected_circuit.to_string_lossy() - ); - assert_eq!( - env_value(&room_a, "OPENKAKAO_REPLY_AUTHOR_BINDINGS"), - r#"[{"nickname":"alice","author_id":700}]"# - ); + let profile = LocoMemberProfile::from_getmem_doc(&doc); assert_eq!( - env_value(&room_b, "OPENKAKAO_REPLY_AUTHOR_BINDINGS"), - r#"[{"nickname":"bob","author_id":800}]"# + profile, + LocoMemberProfile { + user_id: 100000002, + account_id: 200000001, + nickname: "Alice".into(), + country_iso: "kr".into(), + status_message: "hello".into(), + profile_image_url: "https://example.com/p.jpg".into(), + full_profile_image_url: "https://example.com/p-full.jpg".into(), + original_profile_image_url: "https://example.com/p-original.jpg".into(), + access_permit: "permit-token".into(), + suspicion: String::new(), + suspended: false, + memorial: false, + member_type: 0, + ut: 100, + } ); - assert!(!args(&room_a).iter().any(|value| value.contains("bob"))); - assert!(!args(&room_b).iter().any(|value| value.contains("alice"))); + assert_eq!(profile.as_chat_member().display_name(), "Alice"); } #[test] - fn auto_reply_enrollment_floor_seeds_fresh_install_and_rejects_dirty_state() { - let root = tempfile::tempdir().expect("temporary state root"); - let root_path = root.path().canonicalize().expect("canonical state root"); - let room_root = root_path.join("rooms/42"); - fs::create_dir_all(&room_root).expect("create room root"); - let target = local_db::LocalChat { - chat_id: 42, - chat_type: 0, - chat_name: "부자멘토멘티".to_string(), - database_chat_name: Some("부자멘토멘티".to_string()), - active_members_count: 4, - last_log_id: 120, - last_updated_at: 0, - unread_count: 0, - display_name: "부자멘토멘티".to_string(), - }; - let state_path = room_root.join("db-watch-state.json"); - let clean_state = |watermark: i64| { - serde_json::json!({ - "schema_version": 3, - "target_chat_id": 42, - "target_chat_name": "부자멘토멘티", - "cursor_floor": 50, - "acked_watermark": watermark, - "last_observed_log_id": watermark, - "observed_log_ids": [watermark], - "acked_log_ids": [watermark], - "pending_log_ids": [], - "pending_gaps": [], - "candidate_phase": "idle", - "in_flight_candidate": null, - "capability_state": "ready", - "delivery_enabled": true, - "fence": "ready", - "fence_reason": "", - "owner_id": "terminal-owner", - "source_epoch": 7, - }) + fn local_graph_summary_carries_getmem_tokens() { + let snapshot = LocalFriendGraphSnapshot { + user_count: 1, + chat_count: 1, + failed_chat_ids: Vec::new(), + chat_meta: vec![LocalFriendGraphChatMeta { + chat_id: 900000000000002, + title: "Example".into(), + getmem_token: Some(777), + member_count: 2, + }], + entries: vec![LocalFriendGraphEntry { + user_id: 100000002, + account_id: 200000001, + nickname: "Alice".into(), + country_iso: "KR".into(), + status_message: String::new(), + profile_image_url: String::new(), + full_profile_image_url: String::new(), + original_profile_image_url: String::new(), + access_permits: vec!["permit-token".into()], + suspicion: String::new(), + suspended: false, + memorial: false, + member_type: 0, + chat_ids: vec![900000000000002], + chat_titles: vec!["Example".into()], + is_self: false, + hidden_like: false, + hidden_block_type: None, + }], }; + let hints = vec![ProfileCacheHint { + entry_id: 1, + kind: "friend".into(), + request_key: String::new(), + user_ids: vec![100000002], + chat_id: Some(900000000000002), + access_permit: Some("permit-token".into()), + category: None, + data_on_fs: true, + }]; + let summary = local_graph_hint_summary(&snapshot, &hints); + assert_eq!(summary.candidate_matches.len(), 1); assert_eq!( - enrollment_floor_for_target(&root_path, &target, 120) - .expect("a fresh install must seed at the attested DB tail"), - 120 - ); - let binding = AutoReplyBindingEvidence { - chat_id: 42, - ax_name: "부자멘토멘티".to_string(), - local_name: String::new(), - matched_log_ids: vec![78, 79, 80], - matched_count: 3, - matched_utf8_bytes: 24, - transcript_sha256: "a".repeat(64), - attested_db_last_log_id: 120, - }; - let (written_floors, _) = write_auto_reply_enrollment( - &root_path, - &["bind:42:부자멘토멘티".to_string()], - std::slice::from_ref(&target), - &[binding], - &std::collections::BTreeMap::from([( - 42, - vec![AutoReplyAuthorBinding { - nickname: "현준".to_string(), - author_id: 700, - }], - )]), - &root_path.join("runtime"), - ) - .expect("write enrollment from the fresh binding attestation"); - assert_eq!(written_floors, vec![120]); - let written_enrollment = read_bounded_json_file(&root_path.join("enrollment.json")) - .expect("read written enrollment"); - assert_eq!( - written_enrollment["targets"][0]["last_log_id"], - serde_json::Value::from(120) - ); - assert_eq!( - written_enrollment["targets"][0]["identity"]["attested_db_last_log_id"], - serde_json::Value::from(120) - ); - assert_eq!( - written_enrollment["schema_version"], - serde_json::Value::from(AUTO_REPLY_ENROLLMENT_SCHEMA_VERSION) - ); - assert_eq!( - written_enrollment["targets"][0]["cursor_authority"], - serde_json::json!({ - "schema_version": AUTO_REPLY_CURSOR_AUTHORITY_SCHEMA_VERSION, - "kind": AUTO_REPLY_CURSOR_FRESH_KIND, - "cursor_floor": 120, - "attested_db_last_log_id": 120, - "prior_owner_id": null, - "prior_source_epoch": null, - }) + summary.candidate_matches[0].candidate_getmem_tokens, + vec![777] ); - - fs::write( - &state_path, - serde_json::to_vec(&clean_state(130)).expect("serialize running state"), - ) - .expect("write running state"); - assert!(enrollment_floor_for_target(&root_path, &target, 140).is_err()); - - let mut unresolved = clean_state(130); - unresolved["pending_gaps"] = serde_json::json!(["reconcile_required"]); - fs::write( - &state_path, - serde_json::to_vec(&unresolved).expect("serialize unresolved state"), - ) - .expect("rewrite unresolved state"); - assert!(enrollment_floor_for_target(&root_path, &target, 140).is_err()); } - fn stopped_clean_queue_fixture( - path: &Path, - include_supersessions: bool, - breaker_schema: &str, - extra_schema: &str, - job_status: &str, - ) { - let supersessions = if include_supersessions { - "CREATE TABLE reply_job_supersessions( - event_id TEXT PRIMARY KEY, - superseded_by_event_id TEXT NOT NULL, - created_at REAL NOT NULL, - CHECK(event_id <> superseded_by_event_id) - );" - } else { - "" + #[test] + fn syncmainpf_candidates_include_getmem_token_fields() { + let snapshot = LocalFriendGraphSnapshot { + user_count: 1, + chat_count: 1, + failed_chat_ids: Vec::new(), + chat_meta: vec![LocalFriendGraphChatMeta { + chat_id: 900000000000002, + title: "Example".into(), + getmem_token: Some(777), + member_count: 2, + }], + entries: vec![LocalFriendGraphEntry { + user_id: 100000002, + account_id: 200000001, + nickname: "Alice".into(), + country_iso: "KR".into(), + status_message: String::new(), + profile_image_url: String::new(), + full_profile_image_url: String::new(), + original_profile_image_url: String::new(), + access_permits: vec!["permit-token".into()], + suspicion: String::new(), + suspended: false, + memorial: false, + member_type: 0, + chat_ids: vec![900000000000002], + chat_titles: vec!["Example".into()], + is_self: false, + hidden_like: false, + hidden_block_type: None, + }], }; - let sql = format!( - " - CREATE TABLE reply_jobs( - event_id TEXT PRIMARY KEY, event_json TEXT NOT NULL, - status TEXT NOT NULL, due_at REAL, decision TEXT, - reason TEXT, category TEXT, reply TEXT, - scheduled_delay_seconds REAL, error_class TEXT, - created_at REAL NOT NULL, updated_at REAL NOT NULL - ); - CREATE INDEX idx_reply_jobs_status_due - ON reply_jobs(status, due_at); - CREATE TABLE reply_job_tombstones( - event_id TEXT PRIMARY KEY, status TEXT NOT NULL, - archived_at REAL NOT NULL - ); - {supersessions} - {breaker_schema} - {extra_schema} - INSERT INTO reply_jobs VALUES( - 'db:42:1', '{{\"chat_id\":42,\"log_id\":1}}', '{job_status}', NULL, 'skip', 'test', - 'test', NULL, NULL, NULL, 1.0, 1.0 - ); - INSERT INTO reply_job_tombstones VALUES('db:42:2', 'sent', 1.0); - " - ); - let connection = rusqlite::Connection::open(path).expect("create queue fixture"); - connection - .execute_batch(&sql) - .expect("initialize queue fixture"); - drop(connection); - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - fs::set_permissions(path, fs::Permissions::from_mode(0o600)).unwrap(); - } - } - const EXACT_BREAKER_SCHEMA: &str = " - CREATE TABLE model_circuit_breaker( - model_key TEXT PRIMARY KEY, - state TEXT NOT NULL, - failure_class TEXT NOT NULL, - consecutive_failures INTEGER NOT NULL, - open_until REAL NOT NULL, - lease_token TEXT, - updated_at REAL NOT NULL - );"; + let candidate = build_syncmainpf_candidate(&snapshot, &[], 100000002) + .expect("candidate should be built"); - fn stopped_clean_queue_test_room(root: &Path) -> PathBuf { - let room = root.join("42"); - fs::create_dir_all(&room).expect("create stopped-clean test room"); - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - fs::set_permissions(&room, fs::Permissions::from_mode(0o700)) - .expect("make stopped-clean test room private"); - } - room + assert_eq!(candidate.getmem_tokens, vec![777]); + assert!(candidate + .bodies + .iter() + .any(|body| body.get("token").and_then(|v| v.as_i64()) == Some(777))); + assert!(candidate + .bodies + .iter() + .any(|body| body.get("profileToken").and_then(|v| v.as_i64()) == Some(777))); + assert!(candidate + .uplinkprof_bodies + .iter() + .any(|body| body.get("token").and_then(|v| v.as_i64()) == Some(777))); } - fn stopped_clean_queue_v2_fixture_with_table( - path: &Path, - include_breaker: bool, - journal_table_sql: &str, - ) { - stopped_clean_queue_fixture( - path, - true, - if include_breaker { - EXACT_BREAKER_SCHEMA - } else { - "" - }, - "", - "sent", - ); - let connection = rusqlite::Connection::open(path).expect("open v2 queue fixture"); - connection - .execute_batch( - "DROP INDEX idx_reply_jobs_status_due; - ALTER TABLE reply_jobs RENAME TO reply_jobs_v0;", - ) - .expect("prepare reply_jobs v2 rebuild"); - connection - .execute_batch(AUTO_REPLY_REPLY_JOBS_V2_TABLE_SQL) - .expect("create reply_jobs v2 table"); - connection - .execute_batch( - "INSERT INTO reply_jobs( - event_id,event_json,status,due_at,decision,reason,category,reply, - scheduled_delay_seconds,error_class,created_at,updated_at,attempt_no - ) SELECT event_id,event_json,status,due_at,decision,reason,category,reply, - scheduled_delay_seconds,error_class,created_at,updated_at,0 - FROM reply_jobs_v0; - DROP TABLE reply_jobs_v0;", - ) - .expect("copy reply_jobs v2 rows"); - connection - .execute_batch(AUTO_REPLY_REPLY_JOBS_STATUS_INDEX_SQL) - .expect("recreate reply_jobs status index"); - connection - .execute_batch(journal_table_sql) - .expect("create transition journal table"); - connection - .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_INDEX_SQL) - .expect("create transition journal index"); - connection - .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_INSERT_TRIGGER_SQL) - .expect("create transition insert trigger"); - connection - .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_UPDATE_TRIGGER_SQL) - .expect("create transition update trigger"); - connection - .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL) - .expect("create transition cap trigger"); - connection - .pragma_update(None, "user_version", AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION) - .expect("set v2 queue user_version"); - } + #[test] + fn legacy_loco_read_remains_available() { + let cli = Cli::try_parse_from(["openkakao-cli", "loco-read", "123", "--all"]) + .expect("legacy loco-read should remain available"); - fn stopped_clean_queue_v2_fixture(path: &Path, include_breaker: bool) { - stopped_clean_queue_v2_fixture_with_table( - path, - include_breaker, - AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL, - ); + match cli.command { + Commands::LocoRead { chat_id, all, .. } => { + assert_eq!(chat_id, 123); + assert!(all); + } + other => panic!("expected loco-read command, got {other:?}"), + } } #[test] - fn stopped_clean_queue_accepts_exact_legacy_v0_and_journal_v2_read_only() { - let root = tempfile::tempdir().unwrap(); - let room = stopped_clean_queue_test_room(root.path()); - let legacy = room.join("legacy.sqlite3"); - stopped_clean_queue_fixture(&legacy, true, "", "", "skipped"); - validate_stopped_clean_queue(&legacy, 42).expect("exact legacy queue must be accepted"); - let connection = rusqlite::Connection::open_with_flags( - &legacy, - rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY, - ) - .unwrap(); - let tables = connection - .prepare( - "SELECT name FROM sqlite_master - WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name", - ) - .unwrap() - .query_map([], |row| row.get::<_, String>(0)) - .unwrap() - .collect::>>() - .unwrap(); - assert_eq!( - tables, - [ - "reply_job_supersessions", - "reply_job_tombstones", - "reply_jobs", - ] - ); - assert_eq!( - connection - .query_row("PRAGMA user_version", [], |row| row.get::<_, i64>(0)) - .unwrap(), - AUTO_REPLY_QUEUE_LEGACY_USER_VERSION - ); - drop(connection); - - let legacy_with_breaker = room.join("legacy-with-breaker.sqlite3"); - stopped_clean_queue_fixture(&legacy_with_breaker, true, EXACT_BREAKER_SCHEMA, "", "sent"); - validate_stopped_clean_queue(&legacy_with_breaker, 42) - .expect("exact legacy queue with breaker must be accepted"); + fn legacy_loco_chats_remains_available() { + let cli = Cli::try_parse_from(["openkakao-cli", "loco-chats", "--all"]) + .expect("legacy loco-chats should remain available"); - for include_breaker in [false, true] { - let queue = room.join(format!("v2-{include_breaker}.sqlite3")); - stopped_clean_queue_v2_fixture(&queue, include_breaker); - validate_stopped_clean_queue(&queue, 42).expect("exact v2 queue must be accepted"); - let connection = rusqlite::Connection::open_with_flags( - &queue, - rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY, - ) - .unwrap(); - assert_eq!( - connection - .query_row("PRAGMA user_version", [], |row| row.get::<_, i64>(0)) - .unwrap(), - AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION - ); - assert_eq!( - connection - .query_row("SELECT COUNT(*) FROM pipeline_transitions", [], |row| { - row.get::<_, i64>(0) - }) - .unwrap(), - 0 - ); + match cli.command { + Commands::LocoChats { show_all } => { + assert!(show_all); + } + other => panic!("expected loco-chats command, got {other:?}"), } + } + + #[test] + fn legacy_loco_members_remains_available() { + let cli = Cli::try_parse_from(["openkakao-cli", "loco-members", "123"]) + .expect("legacy loco-members should remain available"); - let canonical_python_style_v2 = room.join("canonical-python-style-v2.sqlite3"); - stopped_clean_queue_v2_fixture(&canonical_python_style_v2, true); - let connection = rusqlite::Connection::open(&canonical_python_style_v2).unwrap(); - for (index, code) in [ - "authorization_allowed", - "authorization_rejected", - "media_acquire_started", - "media_acquire_ready", - "media_acquire_failed", - "media_policy_rejected", - ] - .into_iter() - .enumerate() - { - connection - .execute( - "INSERT INTO pipeline_transitions( - schema_version,event_id,attempt_no,component,from_state, - to_state,code,source_epoch,occurred_at_ns - ) VALUES(1,?1,0,'authorization','none','ready',?2,NULL,?3)", - rusqlite::params![format!("db:42:{}", index + 10), code, index + 1], - ) - .unwrap(); + match cli.command { + Commands::LocoMembers { chat_id } => assert_eq!(chat_id, 123), + other => panic!("expected loco-members command, got {other:?}"), } - drop(connection); - validate_stopped_clean_queue(&canonical_python_style_v2, 42) - .expect("canonical helper-created v2 shape and vocabulary must be accepted"); } #[test] - fn stopped_clean_queue_exactly_matches_python_helper_v2_schema() { - let root = tempfile::tempdir().unwrap(); - let room = stopped_clean_queue_test_room(root.path()); - let queue = room.join("reply-queue.sqlite3"); - let scripts = Path::new(env!("CARGO_MANIFEST_DIR")).join("scripts"); - let script = r#" -import pathlib -import sys -sys.path.insert(0, sys.argv[1]) -import auto_reply_transition_journal as journal -queue = pathlib.Path(sys.argv[2]) -connection = journal.open_queue(queue, create=True, expected_chat_id=42) -connection.execute( - "INSERT INTO reply_jobs(" - "event_id,event_json,status,due_at,decision,reason,category,reply," - "scheduled_delay_seconds,error_class,created_at,updated_at" - ") VALUES(?,?,?,?,?,?,?,?,?,?,?,?)", - ("db:42:1", '{"chat_id":42,"log_id":1}', "sent", None, "skip", - "test", "test", None, None, None, 1.0, 1.0), -) -connection.execute( - "INSERT INTO reply_job_tombstones VALUES('db:42:2','sent',1.0)" -) -connection.execute( - "INSERT INTO reply_job_supersessions VALUES('db:42:3','db:42:4',1.0)" -) -connection.commit() -connection.close() -"#; - let output = Command::new("python3") - .args(["-I", "-c", script]) - .arg(&scripts) - .arg(&queue) - .output() - .expect("run Python transition helper"); - assert!( - output.status.success(), - "Python helper failed: {}", - String::from_utf8_lossy(&output.stderr) - ); - validate_stopped_clean_queue(&queue, 42) - .expect("Rust must accept the exact Python helper v2 schema"); + fn legacy_loco_chatinfo_remains_available() { + let cli = Cli::try_parse_from(["openkakao-cli", "loco-chatinfo", "123"]) + .expect("legacy loco-chatinfo should remain available"); + + match cli.command { + Commands::LocoChatinfo { chat_id } => assert_eq!(chat_id, 123), + other => panic!("expected loco-chatinfo command, got {other:?}"), + } } #[test] - fn stopped_clean_queue_rejects_cross_room_path_and_durable_identities() { - let root = tempfile::tempdir().unwrap(); - let room_42 = stopped_clean_queue_test_room(root.path()); - let queue_42 = room_42.join("reply-queue.sqlite3"); - stopped_clean_queue_v2_fixture(&queue_42, true); - validate_stopped_clean_queue(&queue_42, 42).expect("matching room must validate"); - assert!(validate_stopped_clean_queue(&queue_42, 84).is_err()); + fn legacy_loco_probe_remains_available() { + let cli = Cli::try_parse_from(["openkakao-cli", "loco-probe", "BLSYNC"]) + .expect("legacy loco-probe should remain available"); - let room_84 = root.path().join("84"); - fs::create_dir_all(&room_84).unwrap(); - let queue_84 = room_84.join("reply-queue.sqlite3"); - fs::copy(&queue_42, &queue_84).unwrap(); - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - fs::set_permissions(&queue_84, fs::Permissions::from_mode(0o600)).unwrap(); + match cli.command { + Commands::LocoProbe { method, body } => { + assert_eq!(method, "BLSYNC"); + assert!(body.is_none()); + } + other => panic!("expected loco-probe command, got {other:?}"), } - assert!(validate_stopped_clean_queue(&queue_84, 84).is_err()); - - let connection = rusqlite::Connection::open(&queue_42).unwrap(); - connection - .execute( - "INSERT INTO reply_job_supersessions VALUES(\ - 'db:42:3','db:84:4',1.0)", - [], - ) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&queue_42, 42).is_err()); - - let connection = rusqlite::Connection::open(&queue_42).unwrap(); - connection - .execute("DELETE FROM reply_job_supersessions", []) - .unwrap(); - connection - .execute("UPDATE reply_job_tombstones SET event_id='db:84:2'", []) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&queue_42, 42).is_err()); - - let connection = rusqlite::Connection::open(&queue_42).unwrap(); - connection - .execute("UPDATE reply_job_tombstones SET event_id='db:42:2'", []) - .unwrap(); - connection - .execute( - "UPDATE reply_jobs SET event_json='{\"chat_id\":42.0,\"log_id\":true}'", - [], - ) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&queue_42, 42).is_err()); - - let connection = rusqlite::Connection::open(&queue_42).unwrap(); - connection - .execute( - "UPDATE reply_jobs SET event_json='{\"chat_id\":42,\"log_id\":1}'", - [], - ) - .unwrap(); - connection - .execute( - "INSERT INTO pipeline_transitions(\ - schema_version,event_id,attempt_no,component,from_state,to_state,\ - code,source_epoch,occurred_at_ns) VALUES(\ - 1,'db:84:5',0,'queue','none','sent','enqueued',NULL,1)", - [], - ) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&queue_42, 42).is_err()); } #[test] - fn stopped_clean_queue_rejects_nonexact_or_malformed_table_sets() { - let root = tempfile::tempdir().unwrap(); - let room = stopped_clean_queue_test_room(root.path()); - let missing = room.join("missing.sqlite3"); - stopped_clean_queue_fixture(&missing, false, "", "", "sent"); - assert!(validate_stopped_clean_queue(&missing, 42).is_err()); - - let extra = room.join("extra.sqlite3"); - stopped_clean_queue_fixture( - &extra, - true, - "", - "CREATE TABLE unexpected_table(value TEXT);", - "sent", + fn webhook_header_requires_name_and_value() { + assert_eq!( + parse_webhook_header("Authorization: Bearer test").unwrap(), + ("Authorization".to_string(), "Bearer test".to_string()) ); - assert!(validate_stopped_clean_queue(&extra, 42).is_err()); + assert!(parse_webhook_header("MissingSeparator").is_err()); + assert!(parse_webhook_header("Header: ").is_err()); + } - let malformed = room.join("malformed.sqlite3"); - stopped_clean_queue_fixture( - &malformed, - true, - " - CREATE TABLE model_circuit_breaker( - model_key TEXT PRIMARY KEY, state TEXT NOT NULL, - failure_class TEXT NOT NULL, - consecutive_failures INTEGER NOT NULL, - open_until REAL NOT NULL, lease_token TEXT, - updated_at TEXT NOT NULL - );", - "", - "sent", + #[test] + fn webhook_signature_is_stable_for_known_input() { + let signature = build_webhook_signature("secret", "1700000000", br#"{"ok":true}"#).unwrap(); + assert_eq!( + signature, + "sha256=c1afc7c2df3db0690d7d75954610ed1a1d959ce96355ccb8c0a8bc09fd0cfc27" ); - assert!(validate_stopped_clean_queue(&malformed, 42).is_err()); } #[test] - fn stopped_clean_queue_rejects_unknown_or_mismatched_user_versions() { - let root = tempfile::tempdir().unwrap(); - let room = stopped_clean_queue_test_room(root.path()); - for version in [1, AUTO_REPLY_QUEUE_JOURNAL_USER_VERSION, 3] { - let queue = room.join(format!("legacy-version-{version}.sqlite3")); - stopped_clean_queue_fixture(&queue, true, "", "", "sent"); - let connection = rusqlite::Connection::open(&queue).unwrap(); - connection - .pragma_update(None, "user_version", version) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&queue, 42).is_err()); - } - - for version in [AUTO_REPLY_QUEUE_LEGACY_USER_VERSION, 1, 3] { - let queue = room.join(format!("v2-version-{version}.sqlite3")); - stopped_clean_queue_v2_fixture(&queue, false); - let connection = rusqlite::Connection::open(&queue).unwrap(); - connection - .pragma_update(None, "user_version", version) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&queue, 42).is_err()); - } + fn webhook_url_requires_https_or_loopback_http() { + assert!(validate_webhook_url("https://example.com/hook", false).is_ok()); + assert!(validate_webhook_url("http://localhost:3000/hook", false).is_ok()); + assert!(validate_webhook_url("http://127.0.0.1:4000/hook", false).is_ok()); + assert!(validate_webhook_url("http://example.com/hook", false).is_err()); + assert!(validate_webhook_url("http://example.com/hook", true).is_ok()); } #[test] - fn stopped_clean_queue_rejects_v2_table_index_and_trigger_tampering() { - let root = tempfile::tempdir().unwrap(); - let room = stopped_clean_queue_test_room(root.path()); - - let table = room.join("table.sqlite3"); - let tampered_table_sql = AUTO_REPLY_PIPELINE_TRANSITIONS_TABLE_SQL.replacen( - "CHECK(schema_version = 1)", - "CHECK(schema_version IN (1))", - 1, - ); - stopped_clean_queue_v2_fixture_with_table(&table, false, &tampered_table_sql); - assert!(validate_stopped_clean_queue(&table, 42).is_err()); - - let index = room.join("index.sqlite3"); - stopped_clean_queue_v2_fixture(&index, false); - let connection = rusqlite::Connection::open(&index).unwrap(); - connection - .execute_batch( - "DROP INDEX idx_pipeline_transitions_event_seq; - CREATE INDEX idx_pipeline_transitions_event_seq - ON pipeline_transitions(event_id, seq DESC);", - ) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&index, 42).is_err()); - - let trigger = room.join("trigger.sqlite3"); - stopped_clean_queue_v2_fixture(&trigger, false); - let connection = rusqlite::Connection::open(&trigger).unwrap(); - connection - .execute_batch( - "DROP TRIGGER trg_reply_jobs_transition_insert; - CREATE TRIGGER trg_reply_jobs_transition_insert - AFTER INSERT ON reply_jobs BEGIN SELECT 1; END;", - ) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&trigger, 42).is_err()); - - let extra_table = room.join("extra-table.sqlite3"); - stopped_clean_queue_v2_fixture(&extra_table, false); - let connection = rusqlite::Connection::open(&extra_table).unwrap(); - connection - .execute_batch("CREATE TABLE unexpected_table(value TEXT);") - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&extra_table, 42).is_err()); - - let extra_index = room.join("extra-index.sqlite3"); - stopped_clean_queue_v2_fixture(&extra_index, false); - let connection = rusqlite::Connection::open(&extra_index).unwrap(); - connection - .execute_batch("CREATE INDEX idx_unexpected ON reply_jobs(event_id);") - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&extra_index, 42).is_err()); - - let extra_trigger = room.join("extra-trigger.sqlite3"); - stopped_clean_queue_v2_fixture(&extra_trigger, false); - let connection = rusqlite::Connection::open(&extra_trigger).unwrap(); - connection - .execute_batch( - "CREATE TRIGGER trg_unexpected AFTER DELETE ON reply_jobs - BEGIN SELECT 1; END;", - ) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&extra_trigger, 42).is_err()); + fn outbound_message_must_not_be_blank() { + assert!(validate_outbound_message("hello").is_ok()); + assert!(validate_outbound_message(" ").is_err()); } #[test] - fn stopped_clean_queue_rejects_v2_journal_above_hard_cap() { - let root = tempfile::tempdir().unwrap(); - let room = stopped_clean_queue_test_room(root.path()); - let queue = room.join("over-cap.sqlite3"); - stopped_clean_queue_v2_fixture(&queue, false); - let mut connection = rusqlite::Connection::open(&queue).unwrap(); - connection - .execute_batch("DROP TRIGGER trg_pipeline_transitions_cap;") - .unwrap(); - let transaction = connection.transaction().unwrap(); - { - let mut insert = transaction - .prepare( - "INSERT INTO pipeline_transitions( - schema_version,event_id,attempt_no,component,from_state, - to_state,code,source_epoch,occurred_at_ns - ) VALUES(1,?1,0,'queue','none','pending','enqueued',NULL,?2)", - ) - .unwrap(); - for sequence in 1..=AUTO_REPLY_QUEUE_JOURNAL_MAX_ROWS + 1 { - insert - .execute(rusqlite::params![format!("db:42:{sequence}"), sequence]) - .unwrap(); + fn stats_command_is_available() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "stats", + "123", + "--limit", + "500", + "--since", + "2025-01-01", + ]) + .expect("stats should accept limit and since"); + + match cli.command { + Commands::Stats { + chat_id, + limit, + since, + } => { + assert_eq!(chat_id, 123); + assert_eq!(limit, Some(500)); + assert_eq!(since.as_deref(), Some("2025-01-01")); } + other => panic!("expected stats command, got {other:?}"), } - transaction.commit().unwrap(); - connection - .execute_batch(AUTO_REPLY_PIPELINE_TRANSITIONS_CAP_TRIGGER_SQL) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&queue, 42).is_err()); } #[test] - fn stopped_clean_queue_rejects_v2_invalid_journal_metadata() { - assert!(parse_auto_reply_transition_event_id("db:1:1").is_some()); - assert!( - parse_auto_reply_transition_event_id("db:9223372036854775806:9223372036854775806") - .is_some() - ); - for invalid in [ - "db:0:1", - "db:01:1", - "db:1:01", - "db:1x:2", - "db:1:2x", - "db:1:1:2", - "DB:1:1", - "db:9223372036854775807:1", - "db:1:9223372036854775807", - ] { - assert!(parse_auto_reply_transition_event_id(invalid).is_none()); - } - let root = tempfile::tempdir().unwrap(); - let room = stopped_clean_queue_test_room(root.path()); - let queue = room.join("invalid-journal-metadata.sqlite3"); - stopped_clean_queue_v2_fixture(&queue, false); - let connection = rusqlite::Connection::open(&queue).unwrap(); - connection - .execute_batch("PRAGMA ignore_check_constraints = ON;") - .unwrap(); - connection - .execute( - "INSERT INTO pipeline_transitions( - schema_version,event_id,attempt_no,component,from_state, - to_state,code,source_epoch,occurred_at_ns - ) VALUES(1,'db:42:99',0,'queue','none','pending', - 'private-free-form-code',NULL,1)", - [], - ) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&queue, 42).is_err()); + fn openkakao_error_loco_status_display() { + use crate::error::OpenKakaoError; + let err = OpenKakaoError::loco("SYNCMSG", -300); + assert!(err.to_string().contains("SYNCMSG")); + assert!(err.to_string().contains("-300")); + assert!(err.is_retryable()); } #[test] - fn stopped_clean_queue_rejects_v2_invalid_reply_job_attempt() { - let root = tempfile::tempdir().unwrap(); - let room = stopped_clean_queue_test_room(root.path()); - let queue = room.join("invalid-reply-job-attempt.sqlite3"); - stopped_clean_queue_v2_fixture(&queue, false); - let connection = rusqlite::Connection::open(&queue).unwrap(); - connection - .execute_batch( - "PRAGMA ignore_check_constraints = ON; - UPDATE reply_jobs SET attempt_no = 1000001 - WHERE event_id = 'db:42:1';", - ) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&queue, 42).is_err()); + fn openkakao_error_token_expired_from_950() { + use crate::error::OpenKakaoError; + let err = OpenKakaoError::loco("LOGINLIST", -950); + assert!(matches!(err, OpenKakaoError::TokenExpired)); + assert!(err.is_retryable()); } #[test] - fn stopped_clean_queue_rejects_nonterminal_rows() { - let root = tempfile::tempdir().unwrap(); - let room = stopped_clean_queue_test_room(root.path()); - let queue = room.join("nonterminal.sqlite3"); - stopped_clean_queue_fixture(&queue, true, "", "", "delivery_unknown"); - assert!(validate_stopped_clean_queue(&queue, 42).is_err()); - - let tombstone_queue = room.join("nonterminal-tombstone.sqlite3"); - stopped_clean_queue_fixture(&tombstone_queue, true, "", "", "sent"); - let connection = rusqlite::Connection::open(&tombstone_queue).unwrap(); - connection - .execute( - "UPDATE reply_job_tombstones SET status = 'delivery_unknown'", - [], - ) - .unwrap(); - drop(connection); - assert!(validate_stopped_clean_queue(&tombstone_queue, 42).is_err()); + fn openkakao_error_non_retryable_status() { + use crate::error::OpenKakaoError; + let err = OpenKakaoError::loco("WRITE", -203); + assert!(!err.is_retryable()); } #[test] - fn auto_reply_accepts_only_fully_attested_stopped_clean_state() { - #[cfg(unix)] - use std::os::unix::fs::PermissionsExt; - - let root = tempfile::tempdir().expect("temporary state root"); - let room_root = root.path().join("rooms/42"); - fs::create_dir_all(&room_root).expect("create room root"); - let target = local_db::LocalChat { - chat_id: 42, - chat_type: 0, - chat_name: "부자멘토멘티".to_string(), - database_chat_name: Some("부자멘토멘티".to_string()), - active_members_count: 4, - last_log_id: 140, - last_updated_at: 0, - unread_count: 0, - display_name: "부자멘토멘티".to_string(), - }; - let state_path = room_root.join("db-watch-state.json"); - let status_path = room_root.join("supervisor-status.json"); - let queue_path = room_root.join("reply-queue.sqlite3"); - let stopped_state = serde_json::json!({ - "schema_version": 3, - "target_chat_id": 42, - "target_chat_name": "부자멘토멘티", - "cursor_floor": 100, - "acked_watermark": 130, - "last_observed_log_id": 130, - "observed_log_ids": [130], - "acked_log_ids": [130], - "pending_log_ids": [], - "pending_gaps": [], - "candidate_phase": "idle", - "in_flight_candidate": null, - "capability_state": "stopped_clean", - "delivery_enabled": false, - "fence": "stopped_clean", - "fence_reason": "", - "owner_id": "terminal-owner", - "source_epoch": 7, - }); - let stopped_status = serde_json::json!({ - "schema_version": 1, - "mode": "database_authoritative", - "state": "stopped", - "shutdown_state": "stopped_clean", - "all_children_exited": true, - "readiness": "fenced", - "fence_reason": "stopped_clean", - "owner": "terminal-owner", - "source_epoch": 7, - "target_chat_id": 42, - "target_chat_name": "부자멘토멘티", - "child_states": { - "ax_watch": "exited", - "db_watch": "exited", - "reply_worker": "exited", - }, - }); - fs::write(&state_path, serde_json::to_vec(&stopped_state).unwrap()).unwrap(); - fs::write(&status_path, serde_json::to_vec(&stopped_status).unwrap()).unwrap(); - let connection = rusqlite::Connection::open(&queue_path).expect("create queue"); - connection - .execute_batch( - " - CREATE TABLE reply_jobs( - event_id TEXT PRIMARY KEY, event_json TEXT NOT NULL, - status TEXT NOT NULL, due_at REAL, decision TEXT, - reason TEXT, category TEXT, reply TEXT, - scheduled_delay_seconds REAL, error_class TEXT, - created_at REAL NOT NULL, updated_at REAL NOT NULL - ); - CREATE INDEX idx_reply_jobs_status_due - ON reply_jobs(status, due_at); - CREATE TABLE reply_job_tombstones( - event_id TEXT PRIMARY KEY, status TEXT NOT NULL, - archived_at REAL NOT NULL - ); - CREATE TABLE reply_job_supersessions( - event_id TEXT PRIMARY KEY, - superseded_by_event_id TEXT NOT NULL, - created_at REAL NOT NULL, - CHECK(event_id <> superseded_by_event_id) - ); - CREATE TABLE model_circuit_breaker( - model_key TEXT PRIMARY KEY, - state TEXT NOT NULL, - failure_class TEXT NOT NULL, - consecutive_failures INTEGER NOT NULL, - open_until REAL NOT NULL, - lease_token TEXT, - updated_at REAL NOT NULL - ); - INSERT INTO reply_jobs VALUES( - 'db:42:1', '{\"chat_id\":42,\"log_id\":1}', - 'skipped', NULL, 'skip', 'test', - 'test', NULL, NULL, NULL, 1.0, 1.0 - ); - ", - ) - .expect("initialize queue schema"); - drop(connection); - #[cfg(unix)] - for path in [&state_path, &status_path, &queue_path] { - fs::set_permissions(path, fs::Permissions::from_mode(0o600)).unwrap(); - } - assert_eq!( - enrollment_floor_for_target(root.path(), &target, 140) - .expect("complete stopped-clean proof must be accepted"), - 130 - ); - let regressed_target = local_db::LocalChat { - last_log_id: 120, - ..target.clone() + fn check_loco_status_passes_on_zero() { + use crate::loco_helpers::check_loco_status; + let packet = crate::loco::packet::LocoPacket { + packet_id: 1, + status_code: 0, + method: "TEST".into(), + body_type: 0, + body: bson::doc! { "status": 0_i32 }, }; - assert!(enrollment_floor_for_target(root.path(), ®ressed_target, 120).is_err()); - assert_eq!( - enrollment_cursor_authority_for_target(root.path(), &target, 140) - .expect("stopped-clean replay authority must be explicit"), - AutoReplyCursorAuthority { - kind: AUTO_REPLY_CURSOR_REPLAY_KIND, - cursor_floor: 130, - attested_db_last_log_id: 140, - prior_owner_id: Some("terminal-owner".to_string()), - prior_source_epoch: Some(7), - } - ); + assert!(check_loco_status("TEST", &packet).is_ok()); + } - let connection = rusqlite::Connection::open(&queue_path).unwrap(); - connection - .execute( - "UPDATE reply_jobs SET status = 'delivery_unknown' \ - WHERE event_id = 'db:42:1'", - [], - ) - .unwrap(); - drop(connection); - assert!(enrollment_floor_for_target(root.path(), &target, 140).is_err()); - let connection = rusqlite::Connection::open(&queue_path).unwrap(); - connection - .execute( - "UPDATE reply_jobs SET status = 'sent' WHERE event_id = 'db:42:1'", - [], - ) - .unwrap(); - drop(connection); + #[test] + fn check_loco_status_fails_on_nonzero() { + use crate::loco_helpers::check_loco_status; + let packet = crate::loco::packet::LocoPacket { + packet_id: 1, + status_code: 0, + method: "SYNCMSG".into(), + body_type: 0, + body: bson::doc! { "status": -300_i32 }, + }; + let err = check_loco_status("SYNCMSG", &packet).unwrap_err(); + assert!(err.to_string().contains("SYNCMSG")); + assert!(err.to_string().contains("-300")); + } - let mut bad_status = stopped_status.clone(); - bad_status["all_children_exited"] = serde_json::Value::Bool(false); - fs::write(&status_path, serde_json::to_vec(&bad_status).unwrap()).unwrap(); - #[cfg(unix)] - fs::set_permissions(&status_path, fs::Permissions::from_mode(0o600)).unwrap(); - assert!(enrollment_floor_for_target(root.path(), &target, 140).is_err()); + #[test] + fn watch_capture_flag_parses() { + let cli = Cli::try_parse_from(["openkakao-cli", "watch", "--capture"]) + .expect("watch should accept --capture"); - let mut leftover_status = stopped_status.clone(); - leftover_status["shutdown_state"] = serde_json::json!("stopped_unclean"); - leftover_status["fence_reason"] = serde_json::json!("db_watch_exited"); - leftover_status["readiness"] = serde_json::json!("fenced"); - leftover_status["all_children_exited"] = serde_json::json!(true); - leftover_status["child_states"] = serde_json::json!({ - "ax_watch": "exited", - "db_watch": "exited", - "reply_worker": "exited", - }); - fs::write(&status_path, serde_json::to_vec(&leftover_status).unwrap()).unwrap(); - let mut raw_poll_fence = stopped_state; - raw_poll_fence["capability_state"] = serde_json::json!("fenced"); - raw_poll_fence["fence"] = serde_json::json!("db_unavailable"); - raw_poll_fence["fence_reason"] = serde_json::json!("poll_fence"); - raw_poll_fence["delivery_enabled"] = serde_json::json!(false); - fs::write(&state_path, serde_json::to_vec(&raw_poll_fence).unwrap()).unwrap(); - #[cfg(unix)] - for path in [&state_path, &status_path] { - fs::set_permissions(path, fs::Permissions::from_mode(0o600)).unwrap(); + match cli.command { + Commands::Watch { capture, .. } => { + assert!(capture); + } + other => panic!("expected watch command, got {other:?}"), } - assert_eq!( - enrollment_cursor_authority_for_target(root.path(), &target, 140) - .expect("terminal leftover ACK resume must be accepted") - .kind, - AUTO_REPLY_CURSOR_LEFTOVER_KIND - ); - raw_poll_fence["candidate_phase"] = serde_json::json!("hooking"); - raw_poll_fence["in_flight_candidate"] = serde_json::json!({ - "event_id": "db:1:130", - "log_id": 130, - "owner_id": raw_poll_fence.get("owner_id").cloned().unwrap_or(serde_json::json!("")), - "in_flight": true, - "pending": true - }); - raw_poll_fence["pending_log_ids"] = serde_json::json!([130]); - fs::write(&state_path, serde_json::to_vec(&raw_poll_fence).unwrap()).unwrap(); - #[cfg(unix)] - fs::set_permissions(&state_path, fs::Permissions::from_mode(0o600)).unwrap(); - assert_eq!( - enrollment_cursor_authority_for_target(root.path(), &target, 140) - .expect("orphaned hooking leftover ACK resume must be accepted") - .kind, - AUTO_REPLY_CURSOR_LEFTOVER_KIND - ); - raw_poll_fence["capability_state"] = serde_json::json!("ready"); - raw_poll_fence["delivery_enabled"] = serde_json::json!(true); - raw_poll_fence["fence"] = serde_json::json!("ready"); - raw_poll_fence["fence_reason"] = serde_json::json!(""); - fs::write(&state_path, serde_json::to_vec(&raw_poll_fence).unwrap()).unwrap(); - #[cfg(unix)] - fs::set_permissions(&state_path, fs::Permissions::from_mode(0o600)).unwrap(); - assert_eq!( - enrollment_cursor_authority_for_target(root.path(), &target, 140) - .expect("ready hooking leftover ACK resume must be accepted") - .kind, - AUTO_REPLY_CURSOR_LEFTOVER_KIND - ); - let leftover = read_bounded_json_file(&state_path).expect("leftover state remains"); - assert_eq!(leftover["acked_watermark"], 130); - assert_eq!(leftover["last_observed_log_id"], 130); - assert_ne!(leftover["fence"], "stopped_clean"); - leftover_status["all_children_exited"] = serde_json::json!(false); - fs::write(&status_path, serde_json::to_vec(&leftover_status).unwrap()).unwrap(); - #[cfg(unix)] - fs::set_permissions(&status_path, fs::Permissions::from_mode(0o600)).unwrap(); - assert!(enrollment_floor_for_target(root.path(), &target, 140).is_err()); } #[test] - fn auto_reply_rejects_residual_legacy_state_without_status() { - let root = tempfile::tempdir().expect("temporary state root"); - fs::write(root.path().join("reply-queue.sqlite3"), b"legacy") - .expect("write residual queue"); - assert!(auto_reply_legacy_conflict(root.path()).is_err()); + fn probe_capture_pushes_flag_parses() { + let cli = Cli::try_parse_from(["openkakao-cli", "probe", "PING", "--capture-pushes"]) + .expect("probe should accept --capture-pushes"); + + match cli.command { + Commands::Probe { + method, + capture_pushes, + .. + } => { + assert_eq!(method, "PING"); + assert!(capture_pushes); + } + other => panic!("expected probe command, got {other:?}"), + } } #[test] - #[cfg(unix)] - fn auto_reply_owner_lock_is_private_exclusive_and_no_follow() { - use std::os::unix::fs::PermissionsExt; - let root = tempfile::tempdir().expect("temporary state root"); - let root_path = root.path().canonicalize().expect("canonical state root"); - let lock_path = root_path.join("supervisor.owner.lock"); - fs::write(&lock_path, b"old").unwrap(); - fs::set_permissions(&lock_path, fs::Permissions::from_mode(0o644)).unwrap(); - let lock = acquire_auto_reply_owner_lock(&root_path).expect("acquire owner lock"); - assert_eq!( - fs::metadata(&lock_path).unwrap().permissions().mode() & 0o777, - 0o600 - ); - assert!(acquire_auto_reply_owner_lock(&root_path).is_err()); - drop(lock); - fs::remove_file(&lock_path).unwrap(); - let victim = root_path.join("victim"); - fs::write(&victim, b"do-not-touch").unwrap(); - std::os::unix::fs::symlink(&victim, &lock_path).unwrap(); - assert!(acquire_auto_reply_owner_lock(&root_path).is_err()); - assert_eq!(fs::read(&victim).unwrap(), b"do-not-touch"); + fn delete_command_parses() { + let cli = Cli::try_parse_from(["openkakao-cli", "delete", "123", "456", "--force", "-y"]) + .expect("delete should parse"); + match cli.command { + Commands::Delete { + chat_id, + log_id, + force, + yes, + dry_run, + } => { + assert_eq!(chat_id, 123); + assert_eq!(log_id, 456); + assert!(force); + assert!(yes); + assert!(!dry_run); + } + other => panic!("expected delete, got {other:?}"), + } } #[test] - #[cfg(unix)] - fn auto_reply_private_state_and_enrollment_ignore_umask_022() { - use std::os::unix::fs::PermissionsExt; - - struct UmaskGuard(libc::mode_t); - impl Drop for UmaskGuard { - fn drop(&mut self) { - unsafe { - libc::umask(self.0); - } + fn mark_read_command_parses() { + let cli = Cli::try_parse_from(["openkakao-cli", "mark-read", "123", "456"]) + .expect("mark-read should parse"); + match cli.command { + Commands::MarkRead { + chat_id, + log_id, + yes, + dry_run, + } => { + assert_eq!(chat_id, 123); + assert_eq!(log_id, 456); + assert!(!yes); + assert!(!dry_run); } + other => panic!("expected mark-read, got {other:?}"), } + } - let parent = tempfile::tempdir().expect("temporary state parent"); - let parent_path = parent - .path() - .canonicalize() - .expect("canonical state parent"); - let root = parent_path.join("foreground-state"); - let previous = unsafe { libc::umask(0o022) }; - let _umask = UmaskGuard(previous); - let lock = acquire_auto_reply_owner_lock(&root).expect("acquire private owner lock"); - let target = local_db::LocalChat { - chat_id: 42, - chat_type: 0, - chat_name: "부자멘토멘티".to_string(), - database_chat_name: Some("부자멘토멘티".to_string()), - active_members_count: 4, - last_log_id: 100, - last_updated_at: 0, - unread_count: 0, - display_name: "부자멘토멘티".to_string(), - }; - let room = - prepare_auto_reply_room_state(&root, &target).expect("prepare private foreground room"); - write_private_auto_reply_enrollment(&root, br#"{"schema_version":4}"#) - .expect("write private enrollment authority"); - write_private_auto_reply_enrollment(&root, br#"{"schema_version":5}"#) - .expect("atomically replace private enrollment authority"); + #[test] + fn send_me_command_parses() { + let cli = Cli::try_parse_from(["openkakao-cli", "send-me", "test message"]) + .expect("send-me should parse"); + match cli.command { + Commands::SendMe { message, .. } => { + assert_eq!(message, "test message"); + } + other => panic!("expected send-me, got {other:?}"), + } + } - for directory in [&root, &root.join("rooms"), &room] { - assert_eq!( - fs::symlink_metadata(directory) - .unwrap() - .permissions() - .mode() - & 0o777, - 0o700 - ); + #[test] + fn send_accepts_dry_run_flag() { + let cli = Cli::try_parse_from(["openkakao-cli", "send", "123", "hello", "--dry-run"]) + .expect("send --dry-run should parse"); + match cli.command { + Commands::Send { + chat_id, dry_run, .. + } => { + assert_eq!(chat_id, 123); + assert!(dry_run); + } + other => panic!("expected send, got {other:?}"), } - for file in [ - root.join("supervisor.owner.lock"), - root.join("enrollment.json"), - ] { - assert_eq!( - fs::symlink_metadata(file).unwrap().permissions().mode() & 0o777, - 0o600 - ); + } + + #[test] + fn delete_accepts_dry_run_flag() { + let cli = Cli::try_parse_from(["openkakao-cli", "delete", "123", "456", "--dry-run"]) + .expect("delete --dry-run should parse"); + match cli.command { + Commands::Delete { + chat_id, + log_id, + dry_run, + .. + } => { + assert_eq!(chat_id, 123); + assert_eq!(log_id, 456); + assert!(dry_run); + } + other => panic!("expected delete, got {other:?}"), } - assert_eq!( - fs::read(root.join("enrollment.json")).unwrap(), - br#"{"schema_version":5}"# - ); - assert!(fs::read_dir(&root).unwrap().all(|entry| { - !entry - .unwrap() - .file_name() - .to_string_lossy() - .starts_with(".enrollment.") - })); - drop(lock); } #[test] - #[cfg(unix)] - fn auto_reply_private_state_rejects_symlinks_and_enrollment_hardlinks() { - use std::os::unix::fs::{symlink, PermissionsExt}; - - let parent = tempfile::tempdir().expect("temporary state parent"); - let parent_path = parent - .path() - .canonicalize() - .expect("canonical state parent"); - let redirected_root = parent_path.join("redirected-root"); - fs::create_dir(&redirected_root).expect("create redirected root"); - fs::set_permissions(&redirected_root, fs::Permissions::from_mode(0o755)).unwrap(); - let root_link = parent_path.join("state-link"); - symlink(&redirected_root, &root_link).expect("create state-root symlink"); - assert!(acquire_auto_reply_owner_lock(&root_link).is_err()); - assert_eq!( - fs::metadata(&redirected_root).unwrap().permissions().mode() & 0o777, - 0o755 - ); - - let intermediate = parent_path.join("intermediate-link"); - symlink(&redirected_root, &intermediate).expect("create intermediate symlink"); - assert!(acquire_auto_reply_owner_lock(&intermediate.join("nested-state")).is_err()); - assert!(!redirected_root.join("nested-state").exists()); - - let root = parent_path.join("state"); - let lock = acquire_auto_reply_owner_lock(&root).expect("acquire private root"); - let rooms = root.join("rooms"); - fs::create_dir(&rooms).expect("create rooms directory"); - let redirected_room = parent_path.join("redirected-room"); - fs::create_dir(&redirected_room).expect("create redirected room"); - fs::set_permissions(&redirected_room, fs::Permissions::from_mode(0o755)).unwrap(); - symlink(&redirected_room, rooms.join("42")).expect("create room symlink"); - let target = local_db::LocalChat { - chat_id: 42, - chat_type: 0, - chat_name: "부자멘토멘티".to_string(), - database_chat_name: Some("부자멘토멘티".to_string()), - active_members_count: 4, - last_log_id: 100, - last_updated_at: 0, - unread_count: 0, - display_name: "부자멘토멘티".to_string(), - }; - assert!(prepare_auto_reply_room_state(&root, &target).is_err()); - assert_eq!( - fs::metadata(&redirected_room).unwrap().permissions().mode() & 0o777, - 0o755 - ); - - let victim = parent_path.join("enrollment-victim"); - fs::write(&victim, b"do-not-touch").expect("write enrollment victim"); - fs::set_permissions(&victim, fs::Permissions::from_mode(0o600)).unwrap(); - let enrollment = root.join("enrollment.json"); - symlink(&victim, &enrollment).expect("create enrollment symlink"); - assert!(write_private_auto_reply_enrollment(&root, b"replacement").is_err()); - assert_eq!(fs::read(&victim).unwrap(), b"do-not-touch"); - - fs::remove_file(&enrollment).expect("remove enrollment symlink"); - fs::hard_link(&victim, &enrollment).expect("create enrollment hard link"); - assert!(write_private_auto_reply_enrollment(&root, b"replacement").is_err()); - assert_eq!(fs::read(&victim).unwrap(), b"do-not-touch"); - drop(lock); + fn edit_accepts_dry_run_flag() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "edit", + "123", + "456", + "new text", + "--dry-run", + ]) + .expect("edit --dry-run should parse"); + match cli.command { + Commands::Edit { + chat_id, + log_id, + message, + dry_run, + .. + } => { + assert_eq!(chat_id, 123); + assert_eq!(log_id, 456); + assert_eq!(message, "new text"); + assert!(dry_run); + } + other => panic!("expected edit, got {other:?}"), + } } #[test] - fn auto_reply_accepts_only_attested_stopped_legacy_state() { - let root = tempfile::tempdir().expect("temporary state root"); - let status_path = root.path().join("supervisor-status.json"); - fs::write( - &status_path, - serde_json::to_vec(&serde_json::json!({ - "state": "stopped", - "legacy_drained": true, - })) - .expect("serialize status"), - ) - .expect("write status"); - assert!(auto_reply_legacy_conflict(root.path()).is_ok()); - fs::write( - &status_path, - serde_json::to_vec(&serde_json::json!({ - "state": "stopped", - })) - .expect("serialize status"), - ) - .expect("rewrite status"); - assert!(auto_reply_legacy_conflict(root.path()).is_err()); + fn react_accepts_dry_run_flag() { + let cli = Cli::try_parse_from(["openkakao-cli", "react", "123", "456", "--dry-run"]) + .expect("react --dry-run should parse"); + match cli.command { + Commands::React { + chat_id, + log_id, + dry_run, + .. + } => { + assert_eq!(chat_id, 123); + assert_eq!(log_id, 456); + assert!(dry_run); + } + other => panic!("expected react, got {other:?}"), + } } - #[cfg(unix)] #[test] - fn auto_reply_check_does_not_probe_python_executable() { - use std::os::unix::fs::PermissionsExt; - - let root = tempfile::tempdir().expect("temporary validation root"); - let executable = root.path().join("python3"); - let marker = root.path().join("executed"); - fs::write( - &executable, - format!("#!/bin/sh\nprintf x > '{}'\nexit 1\n", marker.display()), - ) - .expect("write fake interpreter"); - fs::set_permissions(&executable, fs::Permissions::from_mode(0o700)) - .expect("set executable permissions"); - let configured = executable.to_str().expect("executable path"); - let resolved = - validate_auto_reply_executable(Some(configured), "test python", "python3", false) - .expect("static validation should not execute the interpreter"); - assert_eq!(resolved, configured); - assert!(!marker.exists()); + fn local_chats_command_parses() { + let cli = Cli::try_parse_from(["openkakao-cli", "local-chats", "-n", "10"]) + .expect("local-chats should parse"); + match cli.command { + Commands::LocalChats { limit, groups } => { + assert_eq!(limit, 10); + assert!(!groups); + } + other => panic!("expected local-chats, got {other:?}"), + } } - #[cfg(unix)] #[test] - fn python_interpreter_rejects_cellar_version_path_and_keeps_keg_string() { - let cellar = Path::new( - "/opt/homebrew/Cellar/python@3.11/3.11.15_4/Frameworks/Python.framework/Versions/3.11/bin/python3.11", - ); - let error = validate_auto_reply_executable( - Some(cellar.to_str().expect("cellar path")), - "AutoReply python_interpreter", - "python3", - false, - ) - .expect_err("Cellar version paths must be rejected"); - assert!(error - .to_string() - .contains("must not be a Homebrew Cellar version path")); - assert!(is_homebrew_cellar_version_path(cellar)); - assert!(is_homebrew_opt_python_keg_path(Path::new( - "/opt/homebrew/opt/python@3.11/bin/python3.11" - ))); + fn auto_reply_command_accepts_plain_chat_name() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "auto-reply", + "--chat", + "부자멘토멘티", + "--model", + "gemini-3.6-flash", + ]) + .expect("plain chat names should parse"); + match cli.command { + Commands::AutoReply { chat, model, .. } => { + assert_eq!(chat, vec!["부자멘토멘티"]); + assert_eq!(model.as_deref(), Some("gemini-3.6-flash")); + } + other => panic!("expected auto-reply, got {other:?}"), + } + let selectors = local_db::parse_chat_selectors(&["부자멘토멘티".into()]) + .expect("plain names become name selectors"); + assert!(matches!( + selectors.as_slice(), + [local_db::ChatSelector::Name(name)] if name == "부자멘토멘티" + )); } - - #[cfg(unix)] #[test] - fn codex_auth_rejects_empty_credential_file() { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let root = tempfile::tempdir().expect("temporary Codex home"); - let empty = root.path().join("auth.json"); - fs::write(&empty, b"").expect("write empty auth"); - fs::set_permissions(&empty, fs::Permissions::from_mode(0o600)).expect("chmod auth"); - let uid = fs::symlink_metadata(&empty).expect("auth metadata").uid(); - assert!(validate_auto_reply_codex_auth(&empty, uid).is_err()); + fn auto_reply_host_command_parses() { + let cli = Cli::try_parse_from(["openkakao-cli", "auto-reply-host", "--status"]) + .expect("auto-reply-host should parse"); + match cli.command { + Commands::AutoReplyHost { + bake, + status, + disable, + tick, + ref chat, + manifest: _, + state_root: _, + } => { + assert!(!bake); + assert!(status); + assert!(!disable); + assert!(!tick); + assert!(chat.is_empty()); + } + other => panic!("expected auto-reply-host, got {other:?}"), + } + assert!(is_local_only_command(&cli.command)); } - #[cfg(unix)] #[test] - fn auto_reply_aggregate_tracks_live_per_room_readiness() { - use std::os::unix::fs::PermissionsExt; - - let root = tempfile::tempdir().expect("temporary aggregate root"); - let room_root = root.path().join("rooms/42"); - fs::create_dir_all(&room_root).expect("create room root"); - let status_path = room_root.join("supervisor-status.json"); - fs::write( - &status_path, - serde_json::to_vec(&serde_json::json!({ - "target_chat_id": 42, - "target_chat_name": "부자멘토멘티", - "state": "running", - "readiness": "ready", - "fence_reason": "", - "updated_at": chrono::Utc::now().timestamp_millis() as f64 / 1_000.0, - })) - .unwrap(), - ) - .unwrap(); - fs::set_permissions(&status_path, fs::Permissions::from_mode(0o600)).unwrap(); - let target = local_db::LocalChat { - chat_id: 42, - chat_type: 0, - chat_name: "부자멘토멘티".to_string(), - database_chat_name: Some("부자멘토멘티".to_string()), - active_members_count: 4, - last_log_id: 100, - last_updated_at: 0, - unread_count: 0, - display_name: "부자멘토멘티".to_string(), - }; - let mut child = Command::new("/bin/sh") - .args(["-c", "sleep 30"]) - .spawn() - .expect("spawn aggregate fixture child"); - write_auto_reply_aggregate( - root.path(), - std::slice::from_ref(&target), - std::slice::from_ref(&child), - "running", + fn catalog_merge_appends_auto_reply_rooms() { + let merged = room_catalog::merge_configured_and_catalog_selectors( + &["bind:42:부자멘토멘티".into()], + &[99], + &[ + local_db::LocalChat { + chat_id: 42, + chat_type: 1, + chat_name: "부자멘토멘티".into(), + database_chat_name: None, + active_members_count: 4, + last_log_id: 1, + last_updated_at: 0, + unread_count: 0, + display_name: "부자멘토멘티".into(), + }, + local_db::LocalChat { + chat_id: 99, + chat_type: 1, + chat_name: "kakao-test".into(), + database_chat_name: None, + active_members_count: 2, + last_log_id: 2, + last_updated_at: 0, + unread_count: 0, + display_name: "kakao-test".into(), + }, + ], ) - .expect("write live aggregate"); - let aggregate: serde_json::Value = - serde_json::from_slice(&fs::read(root.path().join("aggregate-status.json")).unwrap()) - .unwrap(); - assert_eq!(aggregate["schema_version"], 2); - assert_eq!(aggregate["readiness"], "ready"); - assert_eq!(aggregate["authoritative"], true); - assert_eq!(aggregate["room_count"], 1); - assert_eq!(aggregate["ready_room_count"], 1); - assert_eq!(aggregate["targets"][0]["ready"], true); + .expect("merge"); assert_eq!( - fs::metadata(root.path().join("aggregate-status.json")) - .unwrap() - .permissions() - .mode() - & 0o777, - 0o600 + merged, + vec![ + "bind:42:부자멘토멘티".to_string(), + "bind:99:kakao-test".to_string() + ] ); - child.kill().ok(); - child.wait().ok(); - } - - #[cfg(unix)] - #[test] - fn auto_reply_children_guard_reaps_partial_startup_processes() { - let mut guard = AutoReplyChildrenGuard::new(1); - let mut command = Command::new("/bin/sh"); - command.args(["-c", "sleep 30"]); - configure_auto_reply_process_group(&mut command); - let child = command.spawn().expect("spawn fake supervisor"); - let pid = child.id() as libc::pid_t; - guard.children.push(child); - drop(guard); - assert_eq!(unsafe { libc::kill(pid, 0) }, -1); } - #[cfg(unix)] #[test] - fn auto_reply_children_guard_kills_descendants_after_group_leader_exits() { - let mut guard = AutoReplyChildrenGuard::new(1); - let mut command = Command::new("/bin/sh"); - command.args([ - "-c", - "trap 'exit 0' TERM; (trap '' TERM HUP; exec sleep 30) & wait", - ]); - configure_auto_reply_process_group(&mut command); - let child = command.spawn().expect("spawn fake process group"); - let process_group = child.id() as libc::pid_t; - guard.children.push(child); - drop(guard); - assert_eq!(unsafe { libc::kill(-process_group, 0) }, -1); - } - - #[cfg(unix)] - #[test] - fn auto_reply_guardian_liveness_accepts_only_read_pipe_and_sets_cloexec() { - let mut descriptors = [-1; 2]; - assert_eq!(unsafe { libc::pipe(descriptors.as_mut_ptr()) }, 0); - let read_fd = descriptors[0]; - let write_fd = descriptors[1]; - - let read_pipe = validate_auto_reply_guardian_liveness_fd(read_fd) - .expect("read-only pipe should be accepted"); - let descriptor_flags = unsafe { libc::fcntl(read_pipe.as_raw_fd(), libc::F_GETFD) }; - assert_ne!(descriptor_flags & libc::FD_CLOEXEC, 0); - assert!(validate_auto_reply_guardian_liveness_fd(write_fd).is_err()); - - assert_eq!(unsafe { libc::close(write_fd) }, 0); - drop(read_pipe); - } - - #[cfg(unix)] - #[test] - fn guardian_eof_stops_auto_reply_root_and_descendant_group() { - static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); - let _environment_guard = ENV_LOCK.lock().expect("lock liveness environment"); - std::env::remove_var(AUTO_REPLY_GUARDIAN_LIVENESS_ENV); - AUTO_REPLY_STOP.store(false, Ordering::Release); - AUTO_REPLY_GUARDIAN_LOST.store(false, Ordering::Release); - - let mut descriptors = [-1; 2]; - assert_eq!(unsafe { libc::pipe(descriptors.as_mut_ptr()) }, 0); - let read_fd = descriptors[0]; - let write_fd = descriptors[1]; - let write_flags = unsafe { libc::fcntl(write_fd, libc::F_GETFD) }; - assert!(write_flags >= 0); - assert_eq!( - unsafe { libc::fcntl(write_fd, libc::F_SETFD, write_flags | libc::FD_CLOEXEC) }, - 0 - ); - std::env::set_var(AUTO_REPLY_GUARDIAN_LIVENESS_ENV, read_fd.to_string()); - let monitor = start_auto_reply_guardian_liveness_monitor(false) - .expect("start liveness monitor") - .expect("guarded mode should start a monitor"); - assert!(std::env::var_os(AUTO_REPLY_GUARDIAN_LIVENESS_ENV).is_none()); - - let mut guard = AutoReplyChildrenGuard::new(1); - let mut command = Command::new("/bin/sh"); - command.args([ - "-c", - "trap 'exit 0' TERM; (trap '' TERM HUP; exec sleep 30) & wait", - ]); - configure_auto_reply_process_group(&mut command); - let child = command.spawn().expect("spawn fake worker group"); - let process_group = child.id() as libc::pid_t; - guard.children.push(child); - - assert_eq!(unsafe { libc::close(write_fd) }, 0); - let deadline = std::time::Instant::now() + Duration::from_secs(2); - while !AUTO_REPLY_STOP.load(Ordering::Acquire) && std::time::Instant::now() < deadline { - thread::sleep(Duration::from_millis(10)); + fn auto_reply_command_accepts_repeated_chat_selectors() { + let cli = Cli::try_parse_from([ + "openkakao-cli", + "auto-reply", + "--chat", + "id:42", + "--chat", + "name:투자 공부방", + "--self-nickname", + "self", + "--reply-author", + "author", + "--check", + ]) + .expect("auto-reply should parse"); + match cli.command { + Commands::AutoReply { + chat, + check, + self_nickname, + reply_author, + .. + } => { + assert_eq!(chat, vec!["id:42", "name:투자 공부방"]); + assert_eq!(self_nickname.as_deref(), Some("self")); + assert_eq!(reply_author, vec!["author"]); + assert!(check); + } + other => panic!("expected auto-reply, got {other:?}"), } - assert!(AUTO_REPLY_GUARDIAN_LOST.load(Ordering::Acquire)); - assert!(AUTO_REPLY_STOP.load(Ordering::Acquire)); - guard.stop(); - monitor.join().expect("liveness monitor should exit"); - assert_eq!(unsafe { libc::kill(-process_group, 0) }, -1); - - AUTO_REPLY_STOP.store(false, Ordering::Release); - AUTO_REPLY_GUARDIAN_LOST.store(false, Ordering::Release); } #[test] @@ -10943,99 +3652,6 @@ connection.close() .is_err()); } - #[test] - fn local_send_preflight_requires_auto_reply_worker_identity() { - assert!(require_auto_reply_worker_preflight(true, false).is_err()); - require_auto_reply_worker_preflight(true, true) - .expect("the AutoReply worker may run the read-only preflight"); - require_auto_reply_worker_preflight(false, false) - .expect("ordinary sends are governed by their existing gates"); - } - - #[test] - fn worker_actual_send_setup_failure_finishes_before_ax_call() { - let setup = finish_worker_bound_local_send_setup::<()>( - Err(anyhow::anyhow!("readiness unavailable")), - true, - false, - "target", - true, - ) - .expect("a worker setup failure has a structured no-mutation outcome"); - let mut ax_called = false; - if setup.is_some() { - ax_called = true; - } - assert!(setup.is_none()); - assert!(!ax_called); - - assert!(finish_worker_bound_local_send_setup::<()>( - Err(anyhow::anyhow!("preflight readiness unavailable")), - true, - true, - "target", - true, - ) - .is_err()); - } - - #[cfg(unix)] - #[test] - fn worker_setup_lock_contention_is_immediate_and_never_reaches_ax() { - let root = tempfile::tempdir().expect("temporary worker lock root"); - let path = root.path().join("worker.lock"); - let open_lock = || { - fs::OpenOptions::new() - .create(true) - .truncate(false) - .read(true) - .write(true) - .open(&path) - .expect("open worker lock") - }; - let holder = open_lock(); - acquire_worker_setup_lock_nonblocking(&holder, "test holder") - .expect("acquire initial worker lock"); - let actual_contender = open_lock(); - let preflight_contender = open_lock(); - - // Release after a short bound so a regression to blocking flock fails - // as an acquired lock instead of hanging the test process forever. - let releaser = std::thread::spawn(move || { - std::thread::sleep(Duration::from_millis(250)); - assert_eq!(unsafe { libc::flock(holder.as_raw_fd(), libc::LOCK_UN) }, 0); - }); - let actual_error = - acquire_worker_setup_lock_nonblocking(&actual_contender, "contended AX send lock") - .expect_err("worker lock contention must fail without waiting"); - let preflight_error = acquire_worker_setup_lock_nonblocking( - &preflight_contender, - "contended owner-generation lock", - ) - .expect_err("preflight lock contention must fail without waiting"); - releaser.join().expect("release worker lock"); - - let setup = finish_worker_bound_local_send_setup::<()>( - Err(actual_error), - true, - false, - "target", - true, - ) - .expect("actual worker contention has a structured no-mutation outcome"); - let ax_called = setup.is_some(); - assert!(setup.is_none()); - assert!(!ax_called); - assert!(finish_worker_bound_local_send_setup::<()>( - Err(preflight_error), - true, - true, - "target", - true, - ) - .is_err()); - } - #[test] fn local_only_commands_suppress_deprecation_warning() { let ax_read = Cli::try_parse_from(["openkakao-cli", "ax-read", "나와의 채팅"]) @@ -11183,74 +3799,4 @@ connection.close() other => panic!("expected ax-watch, got {other:?}"), } } - fn bound_source_message( - log_id: i64, - author_id: i64, - sender_name: &str, - ) -> local_db::LocalMessage { - local_db::LocalMessage { - log_id, - chat_id: 417_780_809_780_519, - author_id, - is_self: false, - sender_name: sender_name.to_string(), - message: "body".to_string(), - attachment: String::new(), - message_type: 1, - sent_at: 1_700_000_000, - } - } - - #[test] - fn bound_source_message_prefers_the_page_that_was_already_read() { - let windowed = vec![bound_source_message(7, 4242, "권준혁")]; - let mut lookups = 0; - let resolved = resolve_bound_source_message(&windowed, 7, |_| { - lookups += 1; - Ok(None) - }) - .expect("a row inside the page resolves without a second read"); - assert_eq!(resolved.log_id, 7); - assert_eq!(resolved.sender_name, "권준혁"); - assert_eq!(lookups, 0, "the window hit must not re-query the DB"); - } - - #[test] - fn bound_source_message_falls_back_to_a_log_id_lookup() { - // The newest page holds only later rows, which is the normal shape of a - // scheduled reply in an active room. - let windowed = vec![bound_source_message(9, 1, "다른 사람")]; - let resolved = resolve_bound_source_message(&windowed, 7, |log_id| { - assert_eq!(log_id, 7); - Ok(Some(bound_source_message(7, 4242, "권준혁"))) - }) - .expect("the direct lookup resolves an older source row"); - assert_eq!(resolved.log_id, 7); - assert_eq!(resolved.author_id, 4242); - } - - #[test] - fn bound_source_message_fails_closed_when_the_row_is_gone() { - let windowed = vec![bound_source_message(9, 1, "다른 사람")]; - let error = resolve_bound_source_message(&windowed, 7, |_| Ok(None)) - .expect_err("a missing source row must not become a send"); - assert!(error - .to_string() - .contains("scheduled reply source row is unavailable")); - } - - #[test] - fn bound_source_message_reports_a_lookup_failure() { - let windowed: Vec = Vec::new(); - let error = resolve_bound_source_message(&windowed, 7, |_| anyhow::bail!("database gone")) - .expect_err("a read failure must surface instead of a silent miss"); - // The context wraps the read error, so the cause is on the chain - // rather than in the outermost message. - let chain = format!("{error:#}"); - assert!(chain.contains("database gone"), "{chain}"); - assert!( - chain.contains("read the scheduled reply source row"), - "{chain}" - ); - } } From 302b58f3727443cf9fe3d807717de78ef8fb4e43 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:56:33 +0900 Subject: [PATCH 59/67] refactor(cli): move the context-* command bodies into commands::context The ten offline context-* arms of main() (index, repair-self-sends, search, reply-bundle, style-search, recipient-style, response-time, reply-search, reply-record, reply-update) become cmd_context_* functions. compare-arm confirms each body is rustfmt of the original arm. The eight-flag reply-bundle function carries the same too_many_arguments allowance run_auto_reply already uses for one-to-one CLI flags. Co-Authored-By: Claude Opus 5.5 --- src/commands/context.rs | 401 ++++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 1 + src/main.rs | 330 +++------------------------------ 3 files changed, 423 insertions(+), 309 deletions(-) create mode 100644 src/commands/context.rs diff --git a/src/commands/context.rs b/src/commands/context.rs new file mode 100644 index 00000000..40973ba5 --- /dev/null +++ b/src/commands/context.rs @@ -0,0 +1,401 @@ +//! Offline `context-*` commands over the local context index: indexing, +//! search, reply bundles, style and response-time profiles, and the +//! reply-decision ledger. + +use anyhow::Result; + +use crate::util; + +pub fn cmd_context_index( + input: String, + chat: String, + db: Option, + queue: Option, + json: bool, +) -> Result<()> { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let queue_path = queue.as_deref().map(std::path::Path::new); + let count = openkakao_cli::context::index_csv_with_queue( + &db_path, + &chat, + std::path::Path::new(&input), + queue_path, + )?; + if json { + println!( + "{}", + serde_json::json!({ + "action": "index", + "chat": chat, + "input": openkakao_cli::context::provenance_id(&input), + "db": openkakao_cli::context::provenance_id( + &db_path.display().to_string() + ), + "messages": count, + "network": false + }) + ); + } else { + println!( + "Indexed {} messages for '{}' into {} (offline).", + count, + chat, + db_path.display() + ); + } + Ok(()) +} + +pub fn cmd_context_repair_self_sends( + chat_id: i64, + chat: String, + db: Option, + queue: Option, + dry_run: bool, + json: bool, +) -> Result<()> { + if chat.trim().is_empty() || chat.len() > 512 || chat.chars().any(char::is_control) { + anyhow::bail!("style repair chat name is invalid"); + } + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + openkakao_cli::context::ensure_live_context_schema(&db_path)?; + let confirmed_sends = match queue.as_deref() { + Some(path) => { + openkakao_cli::context::confirmed_self_sends_from_queue(std::path::Path::new(path))? + } + None => Vec::new(), + }; + let report = openkakao_cli::context::repair_bot_sent_style_samples( + &db_path, + &chat, + chat_id, + &confirmed_sends, + dry_run, + )?; + if json { + println!("{}", serde_json::to_string_pretty(&report)?); + } else { + println!( + "{}: {} matched of {} scanned rows ({} already ineligible, {} ambiguous), {} response samples and {} recipient samples dropped, {} sources rebuilt (dry_run={}).", + chat, + report.matched_rows, + report.scanned_rows, + report.already_ineligible, + report.ambiguous_rows, + report.deleted_response_samples, + report.deleted_recipient_samples, + report.rebuilt_sources.len(), + report.dry_run, + ); + for matched in &report.matches { + println!( + " [{}|{}s] {} <- {}", + matched.log_id, + matched.offset_seconds, + matched.message.chars().take(60).collect::(), + matched.matched_event_id, + ); + } + } + Ok(()) +} + +pub fn cmd_context_search( + query: String, + chat: Option, + mode: String, + limit: usize, + source: Option, + db: Option, + json: bool, +) -> Result<()> { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let mut results = openkakao_cli::context::search( + &db_path, + chat.as_deref(), + source.as_deref(), + &query, + &mode, + limit, + )?; + if json { + openkakao_cli::context::redact_context_results(&mut results); + println!("{}", serde_json::to_string_pretty(&results)?); + } else { + for result in &results { + println!( + "[{:.3}] {} [{}] {}: {}", + result.score, + result.chat, + result.date, + result.user, + util::truncate(&result.message, 160) + ); + } + println!("{} results (offline {} search)", results.len(), mode); + } + Ok(()) +} + +#[allow( + clippy::too_many_arguments, + reason = "context-reply-bundle CLI flags map one-to-one onto the parameters" +)] +pub fn cmd_context_reply_bundle( + query: String, + chat: String, + chat_id: Option, + current_log_id: Option, + exclude_log_id: Vec, + recipient: Option, + source: Option, + db: Option, +) -> Result<()> { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let output = match (recipient.as_deref(), chat_id, current_log_id) { + (Some(recipient), Some(chat_id), Some(current_log_id)) => { + let mut excluded_log_ids = Vec::with_capacity(1 + exclude_log_id.len()); + excluded_log_ids.push(current_log_id); + excluded_log_ids.extend(exclude_log_id); + openkakao_cli::context::context_reply_bundle_for_recipient_excluding_live_events_json( + &db_path, + &chat, + &query, + source.as_deref(), + recipient, + chat_id, + &excluded_log_ids, + )? + } + (None, None, None) if exclude_log_id.is_empty() => openkakao_cli::context::context_reply_bundle_json( + &db_path, + &chat, + &query, + source.as_deref(), + )?, + _ => anyhow::bail!( + "recipient, chat-id, and current-log-id must be supplied together; exclude-log-id requires that recipient bundle mode" + ), + }; + println!("{output}"); + Ok(()) +} + +pub fn cmd_context_style_search( + query: String, + limit: usize, + db: Option, + chat: Option, + json: bool, +) -> Result<()> { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let mut results = + openkakao_cli::context::style_search(&db_path, chat.as_deref(), &query, limit)?; + if json { + openkakao_cli::context::redact_context_results(&mut results); + println!("{}", serde_json::to_string_pretty(&results)?); + } else { + for result in &results { + println!( + "[{:.3}] {} [{}] {}: {}", + result.score, + result.chat, + result.date, + result.user, + util::truncate(&result.message, 160) + ); + } + println!( + "{} results (offline 최연우 style vector search)", + results.len() + ); + } + Ok(()) +} + +pub fn cmd_context_recipient_style( + chat: String, + recipient: String, + source: Option, + db: Option, + json: bool, +) -> Result<()> { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let profile = openkakao_cli::context::recipient_style_profile( + &db_path, + &chat, + &recipient, + source.as_deref(), + )?; + if json { + match openkakao_cli::context::recipient_style_profile_json( + &db_path, + &chat, + &recipient, + source.as_deref(), + )? { + Some(payload) => println!("{payload}"), + None => println!("null"), + } + } else if let Some(profile) = profile { + println!( + "{} -> {}: {} direct samples (fallback={}, avg {:.1} chars)", + profile.profile.user, + profile.recipient, + profile.direct_sample_count, + profile.used_fallback, + profile.profile.average_character_length + ); + } else { + println!( + "No recipient style profile for '{}' in '{}'.", + recipient, chat + ); + } + Ok(()) +} + +pub fn cmd_context_response_time( + chat: String, + user: String, + source: Option, + db: Option, + json: bool, +) -> Result<()> { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let stats = + openkakao_cli::context::response_time_stats(&db_path, &chat, &user, source.as_deref())?; + if json { + let mut redacted_stats = stats; + openkakao_cli::context::redact_response_time_stats(&mut redacted_stats); + println!("{}", serde_json::to_string_pretty(&redacted_stats)?); + } else if let Some(stats) = stats { + println!( + "{} average response: {:.1}s (median {:.1}s, p90 {:.1}s, {} samples)", + stats.user, + stats.average_seconds, + stats.median_seconds, + stats.p90_seconds, + stats.sample_count + ); + } else { + println!("No response-time samples for '{}' in '{}'.", user, chat); + } + Ok(()) +} + +pub fn cmd_context_reply_search( + query: String, + chat: String, + limit: usize, + db: Option, + json: bool, +) -> Result<()> { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let mut results = + openkakao_cli::context::reply_decision_search(&db_path, &chat, &query, limit)?; + if json { + openkakao_cli::context::redact_reply_decisions(&mut results); + println!("{}", serde_json::to_string_pretty(&results)?); + } else { + for result in &results { + println!( + "[{:.3}] {} {} [{}] {}: {}", + result.score, + result.decision, + result.status, + result.category, + result.author, + util::truncate(&result.message, 160) + ); + } + println!( + "{} results (offline reply-decision vector search)", + results.len() + ); + } + Ok(()) +} + +pub fn cmd_context_reply_record(record: String, db: Option, json: bool) -> Result<()> { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let applied = openkakao_cli::context::record_reply_decision(&db_path, &record)?; + if json { + println!( + "{}", + serde_json::json!({ + "recorded": applied, + "applied": applied, + "db": openkakao_cli::context::provenance_id( + &db_path.display().to_string() + ), + "network": false + }) + ); + } else if applied { + println!( + "Recorded reply decision in {} (offline).", + db_path.display() + ); + } else { + println!("Reply decision was stale and was not recorded (offline)."); + } + Ok(()) +} + +pub fn cmd_context_reply_update( + event_id: String, + status: String, + reply: Option, + sent_at: Option, + db: Option, + json: bool, +) -> Result<()> { + let db_path = db + .map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path); + let updated = openkakao_cli::context::update_reply_decision( + &db_path, + &event_id, + &status, + reply.as_deref(), + sent_at.as_deref(), + )?; + if json { + println!( + "{}", + serde_json::json!({ + "updated": updated, + "applied": updated, + "event_id": event_id, + "db": openkakao_cli::context::provenance_id( + &db_path.display().to_string() + ), + "network": false + }) + ); + } else if updated { + println!("Updated reply decision {} (offline).", event_id); + } else { + println!("Reply decision {} was not found.", event_id); + } + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 189f1137..c651a0c1 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -5,6 +5,7 @@ pub mod auto_reply_host; pub mod ax_read; pub mod ax_watch; pub mod chats; +pub mod context; pub mod context_sync_local; pub mod dataset; pub mod doctor; diff --git a/src/main.rs b/src/main.rs index 35ffbf02..8acac343 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1721,40 +1721,7 @@ fn main() -> Result<()> { chat, db, queue, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let queue_path = queue.as_deref().map(std::path::Path::new); - let count = openkakao_cli::context::index_csv_with_queue( - &db_path, - &chat, - std::path::Path::new(&input), - queue_path, - )?; - if json { - println!( - "{}", - serde_json::json!({ - "action": "index", - "chat": chat, - "input": openkakao_cli::context::provenance_id(&input), - "db": openkakao_cli::context::provenance_id( - &db_path.display().to_string() - ), - "messages": count, - "network": false - }) - ); - } else { - println!( - "Indexed {} messages for '{}' into {} (offline).", - count, - chat, - db_path.display() - ); - } - } + } => commands::context::cmd_context_index(input, chat, db, queue, json)?, Commands::ContextSyncLocal { chat_id, chat, @@ -1775,53 +1742,9 @@ fn main() -> Result<()> { db, queue, dry_run, - } => { - if chat.trim().is_empty() || chat.len() > 512 || chat.chars().any(char::is_control) { - anyhow::bail!("style repair chat name is invalid"); - } - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - openkakao_cli::context::ensure_live_context_schema(&db_path)?; - let confirmed_sends = match queue.as_deref() { - Some(path) => openkakao_cli::context::confirmed_self_sends_from_queue( - std::path::Path::new(path), - )?, - None => Vec::new(), - }; - let report = openkakao_cli::context::repair_bot_sent_style_samples( - &db_path, - &chat, - chat_id, - &confirmed_sends, - dry_run, - )?; - if json { - println!("{}", serde_json::to_string_pretty(&report)?); - } else { - println!( - "{}: {} matched of {} scanned rows ({} already ineligible, {} ambiguous), {} response samples and {} recipient samples dropped, {} sources rebuilt (dry_run={}).", - chat, - report.matched_rows, - report.scanned_rows, - report.already_ineligible, - report.ambiguous_rows, - report.deleted_response_samples, - report.deleted_recipient_samples, - report.rebuilt_sources.len(), - report.dry_run, - ); - for matched in &report.matches { - println!( - " [{}|{}s] {} <- {}", - matched.log_id, - matched.offset_seconds, - matched.message.chars().take(60).collect::(), - matched.matched_event_id, - ); - } - } - } + } => commands::context::cmd_context_repair_self_sends( + chat_id, chat, db, queue, dry_run, json, + )?, Commands::ContextSearch { query, chat, @@ -1829,35 +1752,7 @@ fn main() -> Result<()> { limit, source, db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let mut results = openkakao_cli::context::search( - &db_path, - chat.as_deref(), - source.as_deref(), - &query, - &mode, - limit, - )?; - if json { - openkakao_cli::context::redact_context_results(&mut results); - println!("{}", serde_json::to_string_pretty(&results)?); - } else { - for result in &results { - println!( - "[{:.3}] {} [{}] {}: {}", - result.score, - result.chat, - result.date, - result.user, - util::truncate(&result.message, 160) - ); - } - println!("{} results (offline {} search)", results.len(), mode); - } - } + } => commands::context::cmd_context_search(query, chat, mode, limit, source, db, json)?, Commands::ContextReplyBundle { query, chat, @@ -1867,198 +1762,42 @@ fn main() -> Result<()> { recipient, source, db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let output = match (recipient.as_deref(), chat_id, current_log_id) { - (Some(recipient), Some(chat_id), Some(current_log_id)) => { - let mut excluded_log_ids = Vec::with_capacity(1 + exclude_log_id.len()); - excluded_log_ids.push(current_log_id); - excluded_log_ids.extend(exclude_log_id); - openkakao_cli::context::context_reply_bundle_for_recipient_excluding_live_events_json( - &db_path, - &chat, - &query, - source.as_deref(), - recipient, - chat_id, - &excluded_log_ids, - )? - } - (None, None, None) if exclude_log_id.is_empty() => openkakao_cli::context::context_reply_bundle_json( - &db_path, - &chat, - &query, - source.as_deref(), - )?, - _ => anyhow::bail!( - "recipient, chat-id, and current-log-id must be supplied together; exclude-log-id requires that recipient bundle mode" - ), - }; - println!("{output}"); - } + } => commands::context::cmd_context_reply_bundle( + query, + chat, + chat_id, + current_log_id, + exclude_log_id, + recipient, + source, + db, + )?, Commands::ContextStyleSearch { query, limit, db, chat, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let mut results = - openkakao_cli::context::style_search(&db_path, chat.as_deref(), &query, limit)?; - if json { - openkakao_cli::context::redact_context_results(&mut results); - println!("{}", serde_json::to_string_pretty(&results)?); - } else { - for result in &results { - println!( - "[{:.3}] {} [{}] {}: {}", - result.score, - result.chat, - result.date, - result.user, - util::truncate(&result.message, 160) - ); - } - println!( - "{} results (offline 최연우 style vector search)", - results.len() - ); - } - } + } => commands::context::cmd_context_style_search(query, limit, db, chat, json)?, Commands::ContextRecipientStyle { chat, recipient, source, db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let profile = openkakao_cli::context::recipient_style_profile( - &db_path, - &chat, - &recipient, - source.as_deref(), - )?; - if json { - match openkakao_cli::context::recipient_style_profile_json( - &db_path, - &chat, - &recipient, - source.as_deref(), - )? { - Some(payload) => println!("{payload}"), - None => println!("null"), - } - } else if let Some(profile) = profile { - println!( - "{} -> {}: {} direct samples (fallback={}, avg {:.1} chars)", - profile.profile.user, - profile.recipient, - profile.direct_sample_count, - profile.used_fallback, - profile.profile.average_character_length - ); - } else { - println!( - "No recipient style profile for '{}' in '{}'.", - recipient, chat - ); - } - } + } => commands::context::cmd_context_recipient_style(chat, recipient, source, db, json)?, Commands::ContextResponseTime { chat, user, source, db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let stats = openkakao_cli::context::response_time_stats( - &db_path, - &chat, - &user, - source.as_deref(), - )?; - if json { - let mut redacted_stats = stats; - openkakao_cli::context::redact_response_time_stats(&mut redacted_stats); - println!("{}", serde_json::to_string_pretty(&redacted_stats)?); - } else if let Some(stats) = stats { - println!( - "{} average response: {:.1}s (median {:.1}s, p90 {:.1}s, {} samples)", - stats.user, - stats.average_seconds, - stats.median_seconds, - stats.p90_seconds, - stats.sample_count - ); - } else { - println!("No response-time samples for '{}' in '{}'.", user, chat); - } - } + } => commands::context::cmd_context_response_time(chat, user, source, db, json)?, Commands::ContextReplySearch { query, chat, limit, db, - } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let mut results = - openkakao_cli::context::reply_decision_search(&db_path, &chat, &query, limit)?; - if json { - openkakao_cli::context::redact_reply_decisions(&mut results); - println!("{}", serde_json::to_string_pretty(&results)?); - } else { - for result in &results { - println!( - "[{:.3}] {} {} [{}] {}: {}", - result.score, - result.decision, - result.status, - result.category, - result.author, - util::truncate(&result.message, 160) - ); - } - println!( - "{} results (offline reply-decision vector search)", - results.len() - ); - } - } + } => commands::context::cmd_context_reply_search(query, chat, limit, db, json)?, Commands::ContextReplyRecord { record, db } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let applied = openkakao_cli::context::record_reply_decision(&db_path, &record)?; - if json { - println!( - "{}", - serde_json::json!({ - "recorded": applied, - "applied": applied, - "db": openkakao_cli::context::provenance_id( - &db_path.display().to_string() - ), - "network": false - }) - ); - } else if applied { - println!( - "Recorded reply decision in {} (offline).", - db_path.display() - ); - } else { - println!("Reply decision was stale and was not recorded (offline)."); - } + commands::context::cmd_context_reply_record(record, db, json)? } Commands::ContextReplyUpdate { event_id, @@ -2067,34 +1806,7 @@ fn main() -> Result<()> { sent_at, db, } => { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); - let updated = openkakao_cli::context::update_reply_decision( - &db_path, - &event_id, - &status, - reply.as_deref(), - sent_at.as_deref(), - )?; - if json { - println!( - "{}", - serde_json::json!({ - "updated": updated, - "applied": updated, - "event_id": event_id, - "db": openkakao_cli::context::provenance_id( - &db_path.display().to_string() - ), - "network": false - }) - ); - } else if updated { - println!("Updated reply decision {} (offline).", event_id); - } else { - println!("Reply decision {} was not found.", event_id); - } + commands::context::cmd_context_reply_update(event_id, status, reply, sent_at, db, json)? } Commands::AxServiceScrapeOnce => { println!( From 0ddb540f300ea88793093e7321b2525243fe8d15 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:00:30 +0900 Subject: [PATCH 60/67] refactor(cli): move the read-only local-* command bodies into commands::local local-chats, local-read, local-poll, local-search, and local-schema become cmd_local_* functions. Four bodies are rustfmt of the original arm (compare-arm). cmd_local_poll drops the Ok(()) that would follow its infinite loop, since that would be unreachable. local-chats --groups used to return from main() before the --completion-promise [DONE] line. main() keeps that early return explicitly, so the output is unchanged. Co-Authored-By: Claude Opus 5.5 --- src/commands/local.rs | 177 ++++++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 1 + src/main.rs | 158 ++----------------------------------- 3 files changed, 185 insertions(+), 151 deletions(-) create mode 100644 src/commands/local.rs diff --git a/src/commands/local.rs b/src/commands/local.rs new file mode 100644 index 00000000..0ffbcc4c --- /dev/null +++ b/src/commands/local.rs @@ -0,0 +1,177 @@ +//! Read-only `local-*` commands over the local KakaoTalk database: chats, +//! messages, polling, search, and schema. None of them contact a server. + +use std::io::{self, Write}; + +use anyhow::Result; +use chrono::TimeZone; + +use crate::{local_db, util}; + +pub fn cmd_local_chats(limit: usize, groups: bool, json: bool) -> Result<()> { + let reader = local_db::LocalDbReader::open()?; + if groups { + let chats = reader.list_group_chats(limit)?; + if json { + println!("{}", serde_json::to_string_pretty(&chats)?); + } else if chats.is_empty() { + println!("No group chats found in local database."); + } else { + for c in &chats { + println!( + " {} | {} | {} ({})", + c.chat_id, c.chat_type, c.title, c.members, + ); + } + println!( + "\n{} group chats (local DB, no server contact)", + chats.len() + ); + } + return Ok(()); + } + let chats = reader.list_chats(limit)?; + if json { + println!("{}", serde_json::to_string_pretty(&chats)?); + } else { + if chats.is_empty() { + println!("No chats found in local database."); + } else { + let chat_type_label = |t: i32| -> &'static str { + match t { + 0 => "DM", + 1 => "Group", + _ => "Other", + } + }; + for c in &chats { + let ts = chrono::Local + .timestamp_opt(c.last_updated_at, 0) + .single() + .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string()) + .unwrap_or_default(); + let unread = if c.unread_count > 0 { + format!(" [{}]", c.unread_count) + } else { + String::new() + }; + println!( + " {} | {} | {} ({}){} | {}", + c.chat_id, + chat_type_label(c.chat_type), + c.chat_name, + c.active_members_count, + unread, + ts, + ); + } + println!("\n{} chats (local DB, no server contact)", chats.len()); + } + } + Ok(()) +} + +pub fn cmd_local_read(chat_id: i64, count: usize, since: Option, json: bool) -> Result<()> { + let since_ts = util::parse_since_date(since.as_deref())?; + let reader = local_db::LocalDbReader::open()?; + let mut messages = reader.read_messages(chat_id, count, since_ts)?; + messages.reverse(); // chronological order + if json { + println!("{}", serde_json::to_string_pretty(&messages)?); + } else { + if messages.is_empty() { + println!("No messages found in local database for chat {}.", chat_id); + } else { + for m in &messages { + let ts = chrono::Local + .timestamp_opt(m.sent_at, 0) + .single() + .map(|dt| dt.format("%H:%M:%S").to_string()) + .unwrap_or_default(); + let sender = if m.sender_name.is_empty() { + format!("{}", m.author_id) + } else { + m.sender_name.clone() + }; + let type_tag = util::message_type_label(m.message_type); + if m.message_type == 1 { + println!(" [{}] {}: {}", ts, sender, m.message); + } else { + println!(" [{}] {}: [{}] {}", ts, sender, type_tag, m.message); + } + } + println!( + "\n{} messages (local DB, no server contact)", + messages.len() + ); + } + } + Ok(()) +} + +pub fn cmd_local_poll(chat_id: i64, count: usize, interval: f64) -> Result<()> { + let reader = local_db::LocalDbReader::open()?; + loop { + let envelope = reader.poll(chat_id, count)?; + println!("{}", serde_json::to_string(&envelope)?); + io::stdout().flush()?; + std::thread::sleep(std::time::Duration::from_secs_f64(interval)); + } +} + +pub fn cmd_local_search( + query: String, + count: usize, + chat_id: Option, + json: bool, +) -> Result<()> { + let reader = local_db::LocalDbReader::open()?; + let results = reader.search_messages(&query, count, chat_id)?; + if json { + println!("{}", serde_json::to_string_pretty(&results)?); + } else { + if results.is_empty() { + println!("No messages matching '{}' in local database.", query); + } else { + for m in &results { + let ts = chrono::Local + .timestamp_opt(m.sent_at, 0) + .single() + .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string()) + .unwrap_or_default(); + let sender = if m.sender_name.is_empty() { + format!("{}", m.author_id) + } else { + m.sender_name.clone() + }; + println!( + " [{}] chat={} {}: {}", + ts, + m.chat_id, + sender, + util::truncate(&m.message, 80) + ); + } + println!("\n{} results (local DB, no server contact)", results.len()); + } + } + Ok(()) +} + +pub fn cmd_local_schema(json: bool) -> Result<()> { + let reader = local_db::LocalDbReader::open()?; + let tables = reader.schema()?; + if json { + let items: Vec = tables + .iter() + .map(|(name, sql)| serde_json::json!({"name": name, "sql": sql})) + .collect(); + println!("{}", serde_json::to_string_pretty(&items)?); + } else { + for (name, sql) in &tables { + println!("-- {}", name); + println!("{}\n", sql); + } + } + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index c651a0c1..3490d6c7 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -11,6 +11,7 @@ pub mod dataset; pub mod doctor; pub mod download; pub mod experiment; +pub mod local; pub mod local_send; pub mod members; pub mod probe; diff --git a/src/main.rs b/src/main.rs index 8acac343..76d9b6be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,11 +19,10 @@ mod state; mod util; use openkakao_cli::room_catalog; -use std::io::{self, Write}; +use std::io; use std::sync::atomic::Ordering; use anyhow::{Context, Result}; -use chrono::TimeZone; use clap::{CommandFactory, Parser, Subcommand}; use clap_complete::{generate, Shell}; @@ -1551,171 +1550,28 @@ fn main() -> Result<()> { commands::probe::cmd_loco_probe(&method, body.as_deref(), json, false)? } Commands::LocalChats { limit, groups } => { - let reader = local_db::LocalDbReader::open()?; + commands::local::cmd_local_chats(limit, groups, json)?; + // `--groups` has always returned before the completion promise. if groups { - let chats = reader.list_group_chats(limit)?; - if json { - println!("{}", serde_json::to_string_pretty(&chats)?); - } else if chats.is_empty() { - println!("No group chats found in local database."); - } else { - for c in &chats { - println!( - " {} | {} | {} ({})", - c.chat_id, c.chat_type, c.title, c.members, - ); - } - println!( - "\n{} group chats (local DB, no server contact)", - chats.len() - ); - } return Ok(()); } - let chats = reader.list_chats(limit)?; - if json { - println!("{}", serde_json::to_string_pretty(&chats)?); - } else { - if chats.is_empty() { - println!("No chats found in local database."); - } else { - let chat_type_label = |t: i32| -> &'static str { - match t { - 0 => "DM", - 1 => "Group", - _ => "Other", - } - }; - for c in &chats { - let ts = chrono::Local - .timestamp_opt(c.last_updated_at, 0) - .single() - .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string()) - .unwrap_or_default(); - let unread = if c.unread_count > 0 { - format!(" [{}]", c.unread_count) - } else { - String::new() - }; - println!( - " {} | {} | {} ({}){} | {}", - c.chat_id, - chat_type_label(c.chat_type), - c.chat_name, - c.active_members_count, - unread, - ts, - ); - } - println!("\n{} chats (local DB, no server contact)", chats.len()); - } - } } Commands::LocalRead { chat_id, count, since, - } => { - let since_ts = util::parse_since_date(since.as_deref())?; - let reader = local_db::LocalDbReader::open()?; - let mut messages = reader.read_messages(chat_id, count, since_ts)?; - messages.reverse(); // chronological order - if json { - println!("{}", serde_json::to_string_pretty(&messages)?); - } else { - if messages.is_empty() { - println!("No messages found in local database for chat {}.", chat_id); - } else { - for m in &messages { - let ts = chrono::Local - .timestamp_opt(m.sent_at, 0) - .single() - .map(|dt| dt.format("%H:%M:%S").to_string()) - .unwrap_or_default(); - let sender = if m.sender_name.is_empty() { - format!("{}", m.author_id) - } else { - m.sender_name.clone() - }; - let type_tag = util::message_type_label(m.message_type); - if m.message_type == 1 { - println!(" [{}] {}: {}", ts, sender, m.message); - } else { - println!(" [{}] {}: [{}] {}", ts, sender, type_tag, m.message); - } - } - println!( - "\n{} messages (local DB, no server contact)", - messages.len() - ); - } - } - } + } => commands::local::cmd_local_read(chat_id, count, since, json)?, Commands::LocalPoll { chat_id, count, interval, - } => { - let reader = local_db::LocalDbReader::open()?; - loop { - let envelope = reader.poll(chat_id, count)?; - println!("{}", serde_json::to_string(&envelope)?); - io::stdout().flush()?; - std::thread::sleep(std::time::Duration::from_secs_f64(interval)); - } - } + } => commands::local::cmd_local_poll(chat_id, count, interval)?, Commands::LocalSearch { query, count, chat_id, - } => { - let reader = local_db::LocalDbReader::open()?; - let results = reader.search_messages(&query, count, chat_id)?; - if json { - println!("{}", serde_json::to_string_pretty(&results)?); - } else { - if results.is_empty() { - println!("No messages matching '{}' in local database.", query); - } else { - for m in &results { - let ts = chrono::Local - .timestamp_opt(m.sent_at, 0) - .single() - .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string()) - .unwrap_or_default(); - let sender = if m.sender_name.is_empty() { - format!("{}", m.author_id) - } else { - m.sender_name.clone() - }; - println!( - " [{}] chat={} {}: {}", - ts, - m.chat_id, - sender, - util::truncate(&m.message, 80) - ); - } - println!("\n{} results (local DB, no server contact)", results.len()); - } - } - } - Commands::LocalSchema => { - let reader = local_db::LocalDbReader::open()?; - let tables = reader.schema()?; - if json { - let items: Vec = tables - .iter() - .map(|(name, sql)| serde_json::json!({"name": name, "sql": sql})) - .collect(); - println!("{}", serde_json::to_string_pretty(&items)?); - } else { - for (name, sql) in &tables { - println!("-- {}", name); - println!("{}\n", sql); - } - } - } + } => commands::local::cmd_local_search(query, count, chat_id, json)?, + Commands::LocalSchema => commands::local::cmd_local_schema(json)?, Commands::ContextIndex { input, chat, From 34acf41f39e01a29a196056249ca20b87c3e099d Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:04:14 +0900 Subject: [PATCH 61/67] refactor(cli): move the ax-probe body next to ax-read The AxProbe arm's dump/probe output becomes commands::ax_read::cmd_ax_probe, beside the other read-only AX command. The body is rustfmt of the original arm (compare-arm). Co-Authored-By: Claude Opus 5.5 --- src/commands/ax_read.rs | 23 +++++++++++++++++++++++ src/main.rs | 22 +--------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/commands/ax_read.rs b/src/commands/ax_read.rs index 2dcad8e6..d5b925ae 100644 --- a/src/commands/ax_read.rs +++ b/src/commands/ax_read.rs @@ -50,3 +50,26 @@ pub fn cmd_ax_read(opts: AxReadOptions) -> Result<()> { Ok(()) } + +pub fn cmd_ax_probe(chat: String, dump: bool, max_nodes: usize, json: bool) -> Result<()> { + if dump { + for line in ax_send::dump_ax(&chat, max_nodes)? { + println!("{line}"); + } + } else { + let report = ax_send::probe_ax(&chat)?; + if json { + crate::util::output_json(&report)?; + } else { + println!( + "chat={:?} window_found={} composer_found={} elapsed_ms={} label={:?}", + report.chat_name, + report.window_found, + report.composer_found, + report.elapsed_ms, + report.label + ); + } + } + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 76d9b6be..11778e9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1717,27 +1717,7 @@ fn main() -> Result<()> { chat, dump, max_nodes, - } => { - if dump { - for line in ax_send::dump_ax(&chat, max_nodes)? { - println!("{line}"); - } - } else { - let report = ax_send::probe_ax(&chat)?; - if json { - crate::util::output_json(&report)?; - } else { - println!( - "chat={:?} window_found={} composer_found={} elapsed_ms={} label={:?}", - report.chat_name, - report.window_found, - report.composer_found, - report.elapsed_ms, - report.label - ); - } - } - } + } => commands::ax_read::cmd_ax_probe(chat, dump, max_nodes, json)?, Commands::AxWatch { interval, hook_cmd, From 5359412ccf05d46b71b749d8f512d2282d1866ba Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:06:38 +0900 Subject: [PATCH 62/67] refactor(cli): name the default context index path once Eleven context command bodies each spelled out "use --db, or fall back to openkakao_cli::context::default_db_path()". They now call commands::context::context_db_path, so the fallback lives in one place. Co-Authored-By: Claude Opus 5.5 --- src/commands/context.rs | 46 +++++++++++------------------- src/commands/context_sync_local.rs | 5 ++-- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/src/commands/context.rs b/src/commands/context.rs index 40973ba5..6d783132 100644 --- a/src/commands/context.rs +++ b/src/commands/context.rs @@ -6,6 +6,12 @@ use anyhow::Result; use crate::util; +/// The context index named by `--db`, or the default index when it is omitted. +pub fn context_db_path(db: Option) -> std::path::PathBuf { + db.map(std::path::PathBuf::from) + .unwrap_or_else(openkakao_cli::context::default_db_path) +} + pub fn cmd_context_index( input: String, chat: String, @@ -13,9 +19,7 @@ pub fn cmd_context_index( queue: Option, json: bool, ) -> Result<()> { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); let queue_path = queue.as_deref().map(std::path::Path::new); let count = openkakao_cli::context::index_csv_with_queue( &db_path, @@ -59,9 +63,7 @@ pub fn cmd_context_repair_self_sends( if chat.trim().is_empty() || chat.len() > 512 || chat.chars().any(char::is_control) { anyhow::bail!("style repair chat name is invalid"); } - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); openkakao_cli::context::ensure_live_context_schema(&db_path)?; let confirmed_sends = match queue.as_deref() { Some(path) => { @@ -113,9 +115,7 @@ pub fn cmd_context_search( db: Option, json: bool, ) -> Result<()> { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); let mut results = openkakao_cli::context::search( &db_path, chat.as_deref(), @@ -157,9 +157,7 @@ pub fn cmd_context_reply_bundle( source: Option, db: Option, ) -> Result<()> { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); let output = match (recipient.as_deref(), chat_id, current_log_id) { (Some(recipient), Some(chat_id), Some(current_log_id)) => { let mut excluded_log_ids = Vec::with_capacity(1 + exclude_log_id.len()); @@ -196,9 +194,7 @@ pub fn cmd_context_style_search( chat: Option, json: bool, ) -> Result<()> { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); let mut results = openkakao_cli::context::style_search(&db_path, chat.as_deref(), &query, limit)?; if json { @@ -230,9 +226,7 @@ pub fn cmd_context_recipient_style( db: Option, json: bool, ) -> Result<()> { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); let profile = openkakao_cli::context::recipient_style_profile( &db_path, &chat, @@ -274,9 +268,7 @@ pub fn cmd_context_response_time( db: Option, json: bool, ) -> Result<()> { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); let stats = openkakao_cli::context::response_time_stats(&db_path, &chat, &user, source.as_deref())?; if json { @@ -305,9 +297,7 @@ pub fn cmd_context_reply_search( db: Option, json: bool, ) -> Result<()> { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); let mut results = openkakao_cli::context::reply_decision_search(&db_path, &chat, &query, limit)?; if json { @@ -334,9 +324,7 @@ pub fn cmd_context_reply_search( } pub fn cmd_context_reply_record(record: String, db: Option, json: bool) -> Result<()> { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); let applied = openkakao_cli::context::record_reply_decision(&db_path, &record)?; if json { println!( @@ -369,9 +357,7 @@ pub fn cmd_context_reply_update( db: Option, json: bool, ) -> Result<()> { - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); let updated = openkakao_cli::context::update_reply_decision( &db_path, &event_id, diff --git a/src/commands/context_sync_local.rs b/src/commands/context_sync_local.rs index 2aea620b..37b3fe5a 100644 --- a/src/commands/context_sync_local.rs +++ b/src/commands/context_sync_local.rs @@ -3,6 +3,7 @@ use std::time::Duration; use anyhow::{Context, Result}; +use crate::commands::context::context_db_path; use crate::context_sync_replica; use crate::local_db; @@ -17,9 +18,7 @@ pub fn cmd_context_sync_local( if chat.trim().is_empty() || chat.len() > 512 || chat.chars().any(char::is_control) { anyhow::bail!("context sync chat name is invalid"); } - let db_path = db - .map(std::path::PathBuf::from) - .unwrap_or_else(openkakao_cli::context::default_db_path); + let db_path = context_db_path(db); openkakao_cli::context::ensure_live_context_schema(&db_path)?; let confirmed_sends = match queue.as_deref() { Some(path) => { From f9d559457d61fa51b379c55cb952f07c5e6d3ede Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:11:10 +0900 Subject: [PATCH 63/67] test(cli): move watch, profile, and member tests next to their code Ten tests in main.rs exercised commands::watch, commands::profile, and commands::members internals rather than the CLI. They now sit in those modules (profile gets a tests.rs that sees its re-exports through super::*). The tests are verbatim and the same 229 binary unit test names pass. Tests for util, loco_helpers, the library error type, and room_catalog stay in main.rs because those modules are outside this slice. Co-Authored-By: Claude Opus 5.5 --- src/commands/members.rs | 47 +++++ src/commands/profile/mod.rs | 2 + src/commands/profile/tests.rs | 184 ++++++++++++++++++++ src/commands/watch.rs | 76 +++++++++ src/main.rs | 311 ---------------------------------- 5 files changed, 309 insertions(+), 311 deletions(-) create mode 100644 src/commands/profile/tests.rs diff --git a/src/commands/members.rs b/src/commands/members.rs index 6a561114..bed58d0d 100644 --- a/src/commands/members.rs +++ b/src/commands/members.rs @@ -389,3 +389,50 @@ pub fn cmd_loco_blocked(json: bool) -> Result<()> { Ok(()) }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_loco_member_profile_from_getmem_doc() { + let doc = bson::doc! { + "userId": 100000002_i64, + "accountId": 200000001_i64, + "nickName": "Alice", + "countryIso": "kr", + "statusMessage": "hello", + "profileImageUrl": "https://example.com/p.jpg", + "fullProfileImageUrl": "https://example.com/p-full.jpg", + "originalProfileImageUrl": "https://example.com/p-original.jpg", + "accessPermit": "permit-token", + "suspicion": "", + "suspended": false, + "memorial": false, + "type": 0_i32, + "ut": 100_i64, + }; + + let profile = LocoMemberProfile::from_getmem_doc(&doc); + assert_eq!( + profile, + LocoMemberProfile { + user_id: 100000002, + account_id: 200000001, + nickname: "Alice".into(), + country_iso: "kr".into(), + status_message: "hello".into(), + profile_image_url: "https://example.com/p.jpg".into(), + full_profile_image_url: "https://example.com/p-full.jpg".into(), + original_profile_image_url: "https://example.com/p-original.jpg".into(), + access_permit: "permit-token".into(), + suspicion: String::new(), + suspended: false, + memorial: false, + member_type: 0, + ut: 100, + } + ); + assert_eq!(profile.as_chat_member().display_name(), "Alice"); + } +} diff --git a/src/commands/profile/mod.rs b/src/commands/profile/mod.rs index 95f92571..0f3ced48 100644 --- a/src/commands/profile/mod.rs +++ b/src/commands/profile/mod.rs @@ -2,6 +2,8 @@ pub(crate) mod app_state; pub(crate) mod graph; pub(crate) mod hints; pub(crate) mod probe; +#[cfg(test)] +mod tests; // Re-export public API items used from main.rs, rest.rs, and tests. pub use app_state::*; diff --git a/src/commands/profile/tests.rs b/src/commands/profile/tests.rs new file mode 100644 index 00000000..9473a87f --- /dev/null +++ b/src/commands/profile/tests.rs @@ -0,0 +1,184 @@ +use super::*; + +#[test] +fn parse_friend_profile_cache_hint_extracts_ids_and_access_permit() { + let hint = parse_profile_cache_hint( + 136, + "https://katalk.kakao.com/mac/profile3/friend.json?accessPermit=example-access-permit-token&chatId=900000000000002&id=100000002", + true, + ); + + assert_eq!(hint.kind, "friend"); + assert_eq!(hint.user_ids, vec![100000002]); + assert_eq!(hint.chat_id, Some(900000000000002)); + assert_eq!( + hint.access_permit.as_deref(), + Some("example-access-permit-token") + ); + assert!(hint.data_on_fs); +} + +#[test] +fn parse_friends_profile_cache_hint_extracts_ids_list() { + let hint = parse_profile_cache_hint( + 88, + "https://katalk.kakao.com/mac/profile3/friends.json?category=action&ids=%5B100000004%2C100000005%2C100000006%5D", + false, + ); + + assert_eq!(hint.kind, "friends"); + assert_eq!(hint.user_ids, vec![100000004, 100000005, 100000006]); + assert_eq!(hint.category.as_deref(), Some("action")); + assert_eq!(hint.chat_id, None); + assert_eq!(hint.access_permit, None); +} + +#[test] +fn collect_hint_chat_ids_prefers_user_specific_chat_hints() { + let hints = vec![ + ProfileCacheHint { + entry_id: 1, + kind: "friend".into(), + request_key: String::new(), + user_ids: vec![100000002], + chat_id: Some(900000000000002), + access_permit: Some("permit-a".into()), + category: None, + data_on_fs: true, + }, + ProfileCacheHint { + entry_id: 2, + kind: "friend".into(), + request_key: String::new(), + user_ids: vec![100000002], + chat_id: Some(900000000000002), + access_permit: Some("permit-b".into()), + category: None, + data_on_fs: true, + }, + ProfileCacheHint { + entry_id: 3, + kind: "friend".into(), + request_key: String::new(), + user_ids: vec![100000003], + chat_id: Some(900000000000003), + access_permit: Some("permit-c".into()), + category: None, + data_on_fs: true, + }, + ]; + + assert_eq!( + collect_hint_chat_ids(&hints, 100000002), + vec![900000000000002] + ); + assert_eq!( + collect_hint_chat_ids(&hints, 100000003), + vec![900000000000003] + ); + assert!(collect_hint_chat_ids(&hints, 999).is_empty()); +} + +#[test] +fn local_graph_summary_carries_getmem_tokens() { + let snapshot = LocalFriendGraphSnapshot { + user_count: 1, + chat_count: 1, + failed_chat_ids: Vec::new(), + chat_meta: vec![LocalFriendGraphChatMeta { + chat_id: 900000000000002, + title: "Example".into(), + getmem_token: Some(777), + member_count: 2, + }], + entries: vec![LocalFriendGraphEntry { + user_id: 100000002, + account_id: 200000001, + nickname: "Alice".into(), + country_iso: "KR".into(), + status_message: String::new(), + profile_image_url: String::new(), + full_profile_image_url: String::new(), + original_profile_image_url: String::new(), + access_permits: vec!["permit-token".into()], + suspicion: String::new(), + suspended: false, + memorial: false, + member_type: 0, + chat_ids: vec![900000000000002], + chat_titles: vec!["Example".into()], + is_self: false, + hidden_like: false, + hidden_block_type: None, + }], + }; + let hints = vec![ProfileCacheHint { + entry_id: 1, + kind: "friend".into(), + request_key: String::new(), + user_ids: vec![100000002], + chat_id: Some(900000000000002), + access_permit: Some("permit-token".into()), + category: None, + data_on_fs: true, + }]; + + let summary = local_graph_hint_summary(&snapshot, &hints); + assert_eq!(summary.candidate_matches.len(), 1); + assert_eq!( + summary.candidate_matches[0].candidate_getmem_tokens, + vec![777] + ); +} + +#[test] +fn syncmainpf_candidates_include_getmem_token_fields() { + let snapshot = LocalFriendGraphSnapshot { + user_count: 1, + chat_count: 1, + failed_chat_ids: Vec::new(), + chat_meta: vec![LocalFriendGraphChatMeta { + chat_id: 900000000000002, + title: "Example".into(), + getmem_token: Some(777), + member_count: 2, + }], + entries: vec![LocalFriendGraphEntry { + user_id: 100000002, + account_id: 200000001, + nickname: "Alice".into(), + country_iso: "KR".into(), + status_message: String::new(), + profile_image_url: String::new(), + full_profile_image_url: String::new(), + original_profile_image_url: String::new(), + access_permits: vec!["permit-token".into()], + suspicion: String::new(), + suspended: false, + memorial: false, + member_type: 0, + chat_ids: vec![900000000000002], + chat_titles: vec!["Example".into()], + is_self: false, + hidden_like: false, + hidden_block_type: None, + }], + }; + + let candidate = + build_syncmainpf_candidate(&snapshot, &[], 100000002).expect("candidate should be built"); + + assert_eq!(candidate.getmem_tokens, vec![777]); + assert!(candidate + .bodies + .iter() + .any(|body| body.get("token").and_then(|v| v.as_i64()) == Some(777))); + assert!(candidate + .bodies + .iter() + .any(|body| body.get("profileToken").and_then(|v| v.as_i64()) == Some(777))); + assert!(candidate + .uplinkprof_bodies + .iter() + .any(|body| body.get("token").and_then(|v| v.as_i64()) == Some(777))); +} diff --git a/src/commands/watch.rs b/src/commands/watch.rs index 3f193372..f8cde288 100644 --- a/src/commands/watch.rs +++ b/src/commands/watch.rs @@ -1355,4 +1355,80 @@ mod tests { assert!(!watch_hook_matches(&config, &event)); assert!(dispatch_watch_sinks(&config, &event).await.is_ok()); } + + #[test] + fn watch_hook_filters_match_expected_events() { + let config = WatchHookConfig { + command: Some("cat".to_string()), + webhook_url: None, + webhook_headers: Vec::new(), + webhook_signing_secret: None, + webhook_format: WebhookFormat::Raw, + chat_ids: vec![42], + chat_names: vec![], + keywords: vec!["urgent".to_string()], + message_types: vec![1], + fail_fast: false, + min_hook_interval_secs: 2, + min_webhook_interval_secs: 2, + hook_timeout_secs: 20, + webhook_timeout_secs: 10, + }; + let event = WatchMessageEvent { + event_type: "message", + received_at: "2026-03-08T00:00:00Z".to_string(), + method: "MSG".to_string(), + chat_id: 42, + chat_name: "test".to_string(), + log_id: 7, + author_id: 9, + author_nickname: "alice".to_string(), + message_type: 1, + message: "urgent: ping me".to_string(), + attachment: String::new(), + unread: 0, + }; + + assert!(watch_hook_matches(&config, &event)); + + let wrong_chat = WatchMessageEvent { + chat_id: 99, + ..event.clone() + }; + assert!(!watch_hook_matches(&config, &wrong_chat)); + + let wrong_keyword = WatchMessageEvent { + message: "casual update".to_string(), + ..event.clone() + }; + assert!(!watch_hook_matches(&config, &wrong_keyword)); + } + + #[test] + fn webhook_header_requires_name_and_value() { + assert_eq!( + parse_webhook_header("Authorization: Bearer test").unwrap(), + ("Authorization".to_string(), "Bearer test".to_string()) + ); + assert!(parse_webhook_header("MissingSeparator").is_err()); + assert!(parse_webhook_header("Header: ").is_err()); + } + + #[test] + fn webhook_signature_is_stable_for_known_input() { + let signature = build_webhook_signature("secret", "1700000000", br#"{"ok":true}"#).unwrap(); + assert_eq!( + signature, + "sha256=c1afc7c2df3db0690d7d75954610ed1a1d959ce96355ccb8c0a8bc09fd0cfc27" + ); + } + + #[test] + fn webhook_url_requires_https_or_loopback_http() { + assert!(validate_webhook_url("https://example.com/hook", false).is_ok()); + assert!(validate_webhook_url("http://localhost:3000/hook", false).is_ok()); + assert!(validate_webhook_url("http://127.0.0.1:4000/hook", false).is_ok()); + assert!(validate_webhook_url("http://example.com/hook", false).is_err()); + assert!(validate_webhook_url("http://example.com/hook", true).is_ok()); + } } diff --git a/src/main.rs b/src/main.rs index 11778e9c..d1ffc9f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1894,16 +1894,6 @@ mod tests { ]) .is_err()); } - use crate::commands::members::LocoMemberProfile; - use crate::commands::profile::{ - build_syncmainpf_candidate, collect_hint_chat_ids, local_graph_hint_summary, - parse_profile_cache_hint, LocalFriendGraphChatMeta, LocalFriendGraphEntry, - LocalFriendGraphSnapshot, ProfileCacheHint, - }; - use crate::commands::watch::{ - build_webhook_signature, parse_webhook_header, validate_webhook_url, watch_hook_matches, - WatchHookConfig, WatchMessageEvent, - }; use crate::loco_helpers::should_retry_loco_probe_error; use crate::util::{require_permission, validate_outbound_message}; @@ -1979,54 +1969,6 @@ mod tests { ); } - #[test] - fn watch_hook_filters_match_expected_events() { - let config = WatchHookConfig { - command: Some("cat".to_string()), - webhook_url: None, - webhook_headers: Vec::new(), - webhook_signing_secret: None, - webhook_format: WebhookFormat::Raw, - chat_ids: vec![42], - chat_names: vec![], - keywords: vec!["urgent".to_string()], - message_types: vec![1], - fail_fast: false, - min_hook_interval_secs: 2, - min_webhook_interval_secs: 2, - hook_timeout_secs: 20, - webhook_timeout_secs: 10, - }; - let event = WatchMessageEvent { - event_type: "message", - received_at: "2026-03-08T00:00:00Z".to_string(), - method: "MSG".to_string(), - chat_id: 42, - chat_name: "test".to_string(), - log_id: 7, - author_id: 9, - author_nickname: "alice".to_string(), - message_type: 1, - message: "urgent: ping me".to_string(), - attachment: String::new(), - unread: 0, - }; - - assert!(watch_hook_matches(&config, &event)); - - let wrong_chat = WatchMessageEvent { - chat_id: 99, - ..event.clone() - }; - assert!(!watch_hook_matches(&config, &wrong_chat)); - - let wrong_keyword = WatchMessageEvent { - message: "casual update".to_string(), - ..event.clone() - }; - assert!(!watch_hook_matches(&config, &wrong_keyword)); - } - #[test] fn watch_accepts_hook_flags() { let cli = Cli::try_parse_from([ @@ -2332,231 +2274,6 @@ mod tests { ))); } - #[test] - fn parse_friend_profile_cache_hint_extracts_ids_and_access_permit() { - let hint = parse_profile_cache_hint( - 136, - "https://katalk.kakao.com/mac/profile3/friend.json?accessPermit=example-access-permit-token&chatId=900000000000002&id=100000002", - true, - ); - - assert_eq!(hint.kind, "friend"); - assert_eq!(hint.user_ids, vec![100000002]); - assert_eq!(hint.chat_id, Some(900000000000002)); - assert_eq!( - hint.access_permit.as_deref(), - Some("example-access-permit-token") - ); - assert!(hint.data_on_fs); - } - - #[test] - fn parse_friends_profile_cache_hint_extracts_ids_list() { - let hint = parse_profile_cache_hint( - 88, - "https://katalk.kakao.com/mac/profile3/friends.json?category=action&ids=%5B100000004%2C100000005%2C100000006%5D", - false, - ); - - assert_eq!(hint.kind, "friends"); - assert_eq!(hint.user_ids, vec![100000004, 100000005, 100000006]); - assert_eq!(hint.category.as_deref(), Some("action")); - assert_eq!(hint.chat_id, None); - assert_eq!(hint.access_permit, None); - } - - #[test] - fn collect_hint_chat_ids_prefers_user_specific_chat_hints() { - let hints = vec![ - ProfileCacheHint { - entry_id: 1, - kind: "friend".into(), - request_key: String::new(), - user_ids: vec![100000002], - chat_id: Some(900000000000002), - access_permit: Some("permit-a".into()), - category: None, - data_on_fs: true, - }, - ProfileCacheHint { - entry_id: 2, - kind: "friend".into(), - request_key: String::new(), - user_ids: vec![100000002], - chat_id: Some(900000000000002), - access_permit: Some("permit-b".into()), - category: None, - data_on_fs: true, - }, - ProfileCacheHint { - entry_id: 3, - kind: "friend".into(), - request_key: String::new(), - user_ids: vec![100000003], - chat_id: Some(900000000000003), - access_permit: Some("permit-c".into()), - category: None, - data_on_fs: true, - }, - ]; - - assert_eq!( - collect_hint_chat_ids(&hints, 100000002), - vec![900000000000002] - ); - assert_eq!( - collect_hint_chat_ids(&hints, 100000003), - vec![900000000000003] - ); - assert!(collect_hint_chat_ids(&hints, 999).is_empty()); - } - - #[test] - fn parse_loco_member_profile_from_getmem_doc() { - let doc = bson::doc! { - "userId": 100000002_i64, - "accountId": 200000001_i64, - "nickName": "Alice", - "countryIso": "kr", - "statusMessage": "hello", - "profileImageUrl": "https://example.com/p.jpg", - "fullProfileImageUrl": "https://example.com/p-full.jpg", - "originalProfileImageUrl": "https://example.com/p-original.jpg", - "accessPermit": "permit-token", - "suspicion": "", - "suspended": false, - "memorial": false, - "type": 0_i32, - "ut": 100_i64, - }; - - let profile = LocoMemberProfile::from_getmem_doc(&doc); - assert_eq!( - profile, - LocoMemberProfile { - user_id: 100000002, - account_id: 200000001, - nickname: "Alice".into(), - country_iso: "kr".into(), - status_message: "hello".into(), - profile_image_url: "https://example.com/p.jpg".into(), - full_profile_image_url: "https://example.com/p-full.jpg".into(), - original_profile_image_url: "https://example.com/p-original.jpg".into(), - access_permit: "permit-token".into(), - suspicion: String::new(), - suspended: false, - memorial: false, - member_type: 0, - ut: 100, - } - ); - assert_eq!(profile.as_chat_member().display_name(), "Alice"); - } - - #[test] - fn local_graph_summary_carries_getmem_tokens() { - let snapshot = LocalFriendGraphSnapshot { - user_count: 1, - chat_count: 1, - failed_chat_ids: Vec::new(), - chat_meta: vec![LocalFriendGraphChatMeta { - chat_id: 900000000000002, - title: "Example".into(), - getmem_token: Some(777), - member_count: 2, - }], - entries: vec![LocalFriendGraphEntry { - user_id: 100000002, - account_id: 200000001, - nickname: "Alice".into(), - country_iso: "KR".into(), - status_message: String::new(), - profile_image_url: String::new(), - full_profile_image_url: String::new(), - original_profile_image_url: String::new(), - access_permits: vec!["permit-token".into()], - suspicion: String::new(), - suspended: false, - memorial: false, - member_type: 0, - chat_ids: vec![900000000000002], - chat_titles: vec!["Example".into()], - is_self: false, - hidden_like: false, - hidden_block_type: None, - }], - }; - let hints = vec![ProfileCacheHint { - entry_id: 1, - kind: "friend".into(), - request_key: String::new(), - user_ids: vec![100000002], - chat_id: Some(900000000000002), - access_permit: Some("permit-token".into()), - category: None, - data_on_fs: true, - }]; - - let summary = local_graph_hint_summary(&snapshot, &hints); - assert_eq!(summary.candidate_matches.len(), 1); - assert_eq!( - summary.candidate_matches[0].candidate_getmem_tokens, - vec![777] - ); - } - - #[test] - fn syncmainpf_candidates_include_getmem_token_fields() { - let snapshot = LocalFriendGraphSnapshot { - user_count: 1, - chat_count: 1, - failed_chat_ids: Vec::new(), - chat_meta: vec![LocalFriendGraphChatMeta { - chat_id: 900000000000002, - title: "Example".into(), - getmem_token: Some(777), - member_count: 2, - }], - entries: vec![LocalFriendGraphEntry { - user_id: 100000002, - account_id: 200000001, - nickname: "Alice".into(), - country_iso: "KR".into(), - status_message: String::new(), - profile_image_url: String::new(), - full_profile_image_url: String::new(), - original_profile_image_url: String::new(), - access_permits: vec!["permit-token".into()], - suspicion: String::new(), - suspended: false, - memorial: false, - member_type: 0, - chat_ids: vec![900000000000002], - chat_titles: vec!["Example".into()], - is_self: false, - hidden_like: false, - hidden_block_type: None, - }], - }; - - let candidate = build_syncmainpf_candidate(&snapshot, &[], 100000002) - .expect("candidate should be built"); - - assert_eq!(candidate.getmem_tokens, vec![777]); - assert!(candidate - .bodies - .iter() - .any(|body| body.get("token").and_then(|v| v.as_i64()) == Some(777))); - assert!(candidate - .bodies - .iter() - .any(|body| body.get("profileToken").and_then(|v| v.as_i64()) == Some(777))); - assert!(candidate - .uplinkprof_bodies - .iter() - .any(|body| body.get("token").and_then(|v| v.as_i64()) == Some(777))); - } - #[test] fn legacy_loco_read_remains_available() { let cli = Cli::try_parse_from(["openkakao-cli", "loco-read", "123", "--all"]) @@ -2620,34 +2337,6 @@ mod tests { } } - #[test] - fn webhook_header_requires_name_and_value() { - assert_eq!( - parse_webhook_header("Authorization: Bearer test").unwrap(), - ("Authorization".to_string(), "Bearer test".to_string()) - ); - assert!(parse_webhook_header("MissingSeparator").is_err()); - assert!(parse_webhook_header("Header: ").is_err()); - } - - #[test] - fn webhook_signature_is_stable_for_known_input() { - let signature = build_webhook_signature("secret", "1700000000", br#"{"ok":true}"#).unwrap(); - assert_eq!( - signature, - "sha256=c1afc7c2df3db0690d7d75954610ed1a1d959ce96355ccb8c0a8bc09fd0cfc27" - ); - } - - #[test] - fn webhook_url_requires_https_or_loopback_http() { - assert!(validate_webhook_url("https://example.com/hook", false).is_ok()); - assert!(validate_webhook_url("http://localhost:3000/hook", false).is_ok()); - assert!(validate_webhook_url("http://127.0.0.1:4000/hook", false).is_ok()); - assert!(validate_webhook_url("http://example.com/hook", false).is_err()); - assert!(validate_webhook_url("http://example.com/hook", true).is_ok()); - } - #[test] fn outbound_message_must_not_be_blank() { assert!(validate_outbound_message("hello").is_ok()); From 417487eea89cde1273b5bf108efa34f415bcfbfb Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:14:21 +0900 Subject: [PATCH 64/67] refactor(desktop): share one copy of the JSON and scene helpers The object-narrowing check existed four times (contracts, runtime x3, graph-model), next to second copies of the string and finite-number guards. runtime.ts and graph-model.ts now import record, text and finiteNumber from contracts.ts. JarvisCore and KnowledgeHologram each carried their own spherePoint, CSS-token colour lookup and geometry disposal walk; both now use core/scene-helpers.ts. The palette fallbacks stay at the call sites, so the design-contract test still sees them. CoreRenderState.setSignals uses load-mapping's clamp01 instead of building its own closure on every poll. Co-Authored-By: Claude Fable 5.2 --- desktop/src/contracts.ts | 6 +-- desktop/src/core/jarvis-core.ts | 21 ++-------- desktop/src/core/load-mapping.ts | 2 +- desktop/src/core/render-state.ts | 14 +++---- desktop/src/core/scene-helpers.ts | 26 +++++++++++++ desktop/src/knowledge/graph-model.ts | 52 ++++++++++--------------- desktop/src/knowledge/hologram.ts | 24 +----------- desktop/src/runtime.ts | 57 +++++++++++++--------------- 8 files changed, 88 insertions(+), 114 deletions(-) create mode 100644 desktop/src/core/scene-helpers.ts diff --git a/desktop/src/contracts.ts b/desktop/src/contracts.ts index bc7615e7..7271c3f8 100644 --- a/desktop/src/contracts.ts +++ b/desktop/src/contracts.ts @@ -161,7 +161,7 @@ const PIPELINE_OUTCOMES = new Set([ "none", "sent", "skipped", "deferred", "scheduled", "failed", "aborted", ]); -function record(value: unknown): JsonRecord | null { +export function record(value: unknown): JsonRecord | null { return value !== null && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : null; @@ -171,7 +171,7 @@ function list(value: unknown): unknown[] { return Array.isArray(value) ? value : []; } -function finiteNumber(value: unknown, fallback = 0): number { +export function finiteNumber(value: unknown, fallback = 0): number { return typeof value === "number" && Number.isFinite(value) ? value : fallback; } @@ -187,7 +187,7 @@ function nonNegativeInt(value: unknown): number { return Math.max(0, Math.trunc(finiteNumber(value, 0))); } -function text(value: unknown, fallback = ""): string { +export function text(value: unknown, fallback = ""): string { return typeof value === "string" ? value : fallback; } diff --git a/desktop/src/core/jarvis-core.ts b/desktop/src/core/jarvis-core.ts index cb3e889f..77f60c2b 100644 --- a/desktop/src/core/jarvis-core.ts +++ b/desktop/src/core/jarvis-core.ts @@ -4,19 +4,12 @@ import type { SourceLoads } from "./load-mapping"; import { createNucleusMaterial } from "./nucleus-material"; import { buildPulseLattice, pulseLatticeDrawCount } from "./pulse-lattice"; import { CoreRenderState, ringTilt } from "./render-state"; +import { cssColor, disposeObject, spherePoint } from "./scene-helpers"; const NEURON_COUNT = 96; const SYNAPSES_PER_NEURON = 3; const PARTICLE_COUNT = 30; -function spherePoint(index: number, count: number, radius: number): THREE.Vector3 { - const offset = 2 / count; - const y = (index * offset - 1) + offset / 2; - const r = Math.sqrt(Math.max(0, 1 - y * y)); - const phi = index * Math.PI * (3 - Math.sqrt(5)); - return new THREE.Vector3(Math.cos(phi) * r * radius, y * radius, Math.sin(phi) * r * radius); -} - export class JarvisCore { private readonly renderer: THREE.WebGLRenderer; private readonly scene = new THREE.Scene(); @@ -44,8 +37,8 @@ export class JarvisCore { this.camera.position.set(0, 0, 4.25); this.scene.add(this.root); - const accent = new THREE.Color(getComputedStyle(document.documentElement).getPropertyValue("--accent").trim() || "#B88A45"); - const muted = new THREE.Color(getComputedStyle(document.documentElement).getPropertyValue("--muted").trim() || "#6D655B"); + const accent = cssColor("--accent", "#B88A45"); + const muted = cssColor("--muted", "#6D655B"); const ringGeometry = new THREE.TorusGeometry(1.02, 0.012, 8, 96); for (let index = 0; index < 3; index += 1) { @@ -131,13 +124,7 @@ export class JarvisCore { dispose(): void { this.stop(); - this.scene.traverse((object) => { - if (object instanceof THREE.Mesh || object instanceof THREE.Points || object instanceof THREE.LineSegments) { - object.geometry.dispose(); - const materials = Array.isArray(object.material) ? object.material : [object.material]; - materials.forEach((material) => material.dispose()); - } - }); + disposeObject(this.scene); this.renderer.dispose(); } diff --git a/desktop/src/core/load-mapping.ts b/desktop/src/core/load-mapping.ts index 8142f1cf..f056bca8 100644 --- a/desktop/src/core/load-mapping.ts +++ b/desktop/src/core/load-mapping.ts @@ -34,7 +34,7 @@ const PIPELINE_STAGE_WEIGHTS: Readonly> = { confirm: 0.25, }; -function clamp01(value: number): number { +export function clamp01(value: number): number { if (!Number.isFinite(value)) return 0; return Math.min(1, Math.max(0, value)); } diff --git a/desktop/src/core/render-state.ts b/desktop/src/core/render-state.ts index 990a99e6..2c9dd4c7 100644 --- a/desktop/src/core/render-state.ts +++ b/desktop/src/core/render-state.ts @@ -1,4 +1,5 @@ import { + clamp01, writeLatticePulse, writeRingTargetVelocities, type LatticePulse, @@ -67,16 +68,15 @@ export class CoreRenderState { /** Clamp and store the incoming load signals. Never throws on bad input. */ setSignals(jobLoad: number, voiceRms: number, sources?: SourceLoads): void { - const safe = (value: number): number => Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : 0; - this.targetLoad = safe(jobLoad); - this.targetVoiceRms = safe(voiceRms); + this.targetLoad = clamp01(jobLoad); + this.targetVoiceRms = clamp01(voiceRms); const target = this.targetSources; target.voice = this.targetVoiceRms; if (sources) { - target.reply = safe(sources.reply); - target.geeknews = safe(sources.geeknews); - target.dbSync = safe(sources.dbSync); - target.total = Math.max(safe(sources.total), this.targetLoad, target.voice); + target.reply = clamp01(sources.reply); + target.geeknews = clamp01(sources.geeknews); + target.dbSync = clamp01(sources.dbSync); + target.total = Math.max(clamp01(sources.total), this.targetLoad, target.voice); return; } target.reply = this.targetLoad; diff --git a/desktop/src/core/scene-helpers.ts b/desktop/src/core/scene-helpers.ts new file mode 100644 index 00000000..97c5ccf5 --- /dev/null +++ b/desktop/src/core/scene-helpers.ts @@ -0,0 +1,26 @@ +import * as THREE from "three"; + +/** Read a design token from the document root; the fallback covers a page without the stylesheet. */ +export function cssColor(name: string, fallback: string): THREE.Color { + const value = getComputedStyle(document.documentElement).getPropertyValue(name).trim(); + return new THREE.Color(value || fallback); +} + +/** Point `index` of `count` spread evenly over a sphere along a golden-angle spiral. */ +export function spherePoint(index: number, count: number, radius: number): THREE.Vector3 { + const offset = 2 / Math.max(count, 1); + const y = (index * offset - 1) + offset / 2; + const ring = Math.sqrt(Math.max(0, 1 - y * y)); + const phi = index * Math.PI * (3 - Math.sqrt(5)); + return new THREE.Vector3(Math.cos(phi) * ring * radius, y * radius, Math.sin(phi) * ring * radius); +} + +export function disposeObject(object: THREE.Object3D): void { + object.traverse((child) => { + if (child instanceof THREE.Mesh || child instanceof THREE.Points || child instanceof THREE.Line) { + child.geometry.dispose(); + const materials = Array.isArray(child.material) ? child.material : [child.material]; + materials.forEach((material) => material.dispose()); + } + }); +} diff --git a/desktop/src/knowledge/graph-model.ts b/desktop/src/knowledge/graph-model.ts index 331c17f1..d87cc272 100644 --- a/desktop/src/knowledge/graph-model.ts +++ b/desktop/src/knowledge/graph-model.ts @@ -1,3 +1,5 @@ +import { finiteNumber, record, text } from "../contracts"; + export const DEFAULT_FOCUS_HOPS = 2; export const MAX_FOCUS_HOPS = 3; export const FOCUS_NEIGHBOR_LIMIT = 10; @@ -51,22 +53,8 @@ const EMPTY_EVIDENCE: KnowledgeEvidence = Object.freeze({ retracted: false, }); -function objectValue(value: unknown): Record | null { - return value && typeof value === "object" && !Array.isArray(value) - ? value as Record - : null; -} - -function stringValue(value: unknown): string { - return typeof value === "string" ? value : ""; -} - -function numberValue(value: unknown): number { - return typeof value === "number" && Number.isFinite(value) ? value : 0; -} - function parseEvidence(value: unknown): KnowledgeEvidence { - const item = objectValue(value); + const item = record(value); if (!item) return { ...EMPTY_EVIDENCE, sourceEventIds: [] }; const ids = Array.isArray(item.source_event_ids) ? item.source_event_ids.filter((entry): entry is string => typeof entry === "string").slice(0, 16) @@ -74,7 +62,7 @@ function parseEvidence(value: unknown): KnowledgeEvidence { return { kind: item.kind === "ledger" ? "ledger" : "seed", sourceEventIds: ids, - chatId: stringValue(item.chat_id), + chatId: text(item.chat_id), confirmedAt: typeof item.confirmed_at === "string" ? item.confirmed_at : null, retracted: item.retracted === true, }; @@ -83,17 +71,17 @@ function parseEvidence(value: unknown): KnowledgeEvidence { export function parseKnowledgeGraph(payload: Record | null): KnowledgeGraph { const nodes = Array.isArray(payload?.nodes) ? payload.nodes.flatMap((raw): KnowledgeNode[] => { - const item = objectValue(raw); + const item = record(raw); if (!item) return []; - const id = stringValue(item.id); - const label = stringValue(item.label); + const id = text(item.id); + const label = text(item.label); if (!id || !label || id.startsWith("message:") || id.startsWith("msg:")) return []; return [{ id, label, - category: stringValue(item.category), - importance: Math.max(0, Math.min(100, numberValue(item.importance))), - updatedAt: Math.max(0, numberValue(item.updated_at)), + category: text(item.category), + importance: Math.max(0, Math.min(100, finiteNumber(item.importance))), + updatedAt: Math.max(0, finiteNumber(item.updated_at)), evidence: parseEvidence(item.evidence), }]; }) @@ -101,21 +89,21 @@ export function parseKnowledgeGraph(payload: Record | null): Kn const nodeIds = new Set(nodes.map((node) => node.id)); const edges = Array.isArray(payload?.edges) ? payload.edges.flatMap((raw): KnowledgeEdge[] => { - const item = objectValue(raw); + const item = record(raw); if (!item) return []; - const source = stringValue(item.source); - const target = stringValue(item.target); + const source = text(item.source); + const target = text(item.target); if (!nodeIds.has(source) || !nodeIds.has(target)) return []; return [{ source, - relation: stringValue(item.relation), + relation: text(item.relation), target, - context: stringValue(item.context), - weight: Math.max(0, numberValue(item.weight)), - roomId: stringValue(item.room_id), - validFrom: stringValue(item.valid_from), - validTo: stringValue(item.valid_to), - evidenceMessageId: stringValue(item.evidence_message_id), + context: text(item.context), + weight: Math.max(0, finiteNumber(item.weight)), + roomId: text(item.room_id), + validFrom: text(item.valid_from), + validTo: text(item.valid_to), + evidenceMessageId: text(item.evidence_message_id), evidence: parseEvidence(item.evidence), }]; }) diff --git a/desktop/src/knowledge/hologram.ts b/desktop/src/knowledge/hologram.ts index c25af53e..3cdcd594 100644 --- a/desktop/src/knowledge/hologram.ts +++ b/desktop/src/knowledge/hologram.ts @@ -1,5 +1,6 @@ import * as THREE from "three"; import { AnimationLoop } from "../core/animation-loop"; +import { cssColor, disposeObject, spherePoint } from "../core/scene-helpers"; import { KnowledgeDrilldown, type KnowledgeGraph, @@ -14,29 +15,6 @@ export interface KnowledgeFocusEvent { type FocusHandler = (event: KnowledgeFocusEvent) => void; -function cssColor(name: string, fallback: string): THREE.Color { - const value = getComputedStyle(document.documentElement).getPropertyValue(name).trim(); - return new THREE.Color(value || fallback); -} - -function spherePoint(index: number, count: number, radius: number): THREE.Vector3 { - const offset = 2 / Math.max(count, 1); - const y = (index * offset - 1) + offset / 2; - const ring = Math.sqrt(Math.max(0, 1 - y * y)); - const phi = index * Math.PI * (3 - Math.sqrt(5)); - return new THREE.Vector3(Math.cos(phi) * ring * radius, y * radius, Math.sin(phi) * ring * radius); -} - -function disposeObject(object: THREE.Object3D): void { - object.traverse((child) => { - if (child instanceof THREE.Mesh || child instanceof THREE.LineSegments || child instanceof THREE.Line) { - child.geometry.dispose(); - const materials = Array.isArray(child.material) ? child.material : [child.material]; - materials.forEach((material) => material.dispose()); - } - }); -} - export class KnowledgeHologram { private readonly renderer: THREE.WebGLRenderer; private readonly scene = new THREE.Scene(); diff --git a/desktop/src/runtime.ts b/desktop/src/runtime.ts index f8cc86e9..306c1e42 100644 --- a/desktop/src/runtime.ts +++ b/desktop/src/runtime.ts @@ -1,6 +1,6 @@ import { invoke } from "@tauri-apps/api/core"; import type { CancellationToken, RuntimeSnapshot } from "./contracts"; -import { parseRuntimeSnapshot, unavailableSnapshot } from "./contracts"; +import { parseRuntimeSnapshot, record, unavailableSnapshot } from "./contracts"; import { RESIDENT_MODEL_ID, SWAP_MODEL_ID } from "./tokens"; type SettingsAction = @@ -98,26 +98,22 @@ function validBrowserToolInput(input: BrowserToolInput, token: CancellationToken } function parseBrowserToolResult(value: unknown): BrowserToolResult { - if (!value || typeof value !== "object" || Array.isArray(value)) { + const payload = record(value); + if (!payload || Object.keys(payload).sort().join(",") !== "errorCode,ok,result,status") { return failedBrowserTool("browser_bridge_unavailable"); } - const record = value as Record; - const keys = Object.keys(record).sort(); - if (keys.join(",") !== "errorCode,ok,result,status") { - return failedBrowserTool("browser_bridge_unavailable"); - } - const status = typeof record.status === "string" && BROWSER_TOOL_STATUSES.has(record.status as BrowserToolStatus) - ? record.status as BrowserToolStatus + const status = typeof payload.status === "string" && BROWSER_TOOL_STATUSES.has(payload.status as BrowserToolStatus) + ? payload.status as BrowserToolStatus : null; - const errorCode = typeof record.errorCode === "string" ? record.errorCode : ""; - const result = typeof record.result === "string" ? record.result : null; + const errorCode = typeof payload.errorCode === "string" ? payload.errorCode : ""; + const result = typeof payload.result === "string" ? payload.result : null; if (!status || result === null || new TextEncoder().encode(result).byteLength > BROWSER_TOOL_RESULT_LIMIT_BYTES) { return failedBrowserTool("browser_result_invalid"); } - if (record.ok === true && status === "completed" && errorCode === "") { + if (payload.ok === true && status === "completed" && errorCode === "") { return { ok: true, status, errorCode, result }; } - if (record.ok !== false || !BROWSER_TOOL_ERROR_CODES.has(errorCode) || result !== "") { + if (payload.ok !== false || !BROWSER_TOOL_ERROR_CODES.has(errorCode) || result !== "") { return failedBrowserTool("browser_bridge_unavailable"); } return { ok: false, status, errorCode, result: "" }; @@ -174,12 +170,12 @@ function parseModelAction( action: ModelAction, model: LocalModelId, ): ModelActionResult { - if (!value || typeof value !== "object" || Array.isArray(value)) return failedModelAction(action, model); - const record = value as Record; - const contractMatches = record.ok === true && record.action === action && record.model === model; - const stored = record.stored === true; - const prepared = record.prepared === true; - const needsPrepare = record.needs_prepare === true; + const payload = record(value); + if (!payload) return failedModelAction(action, model); + const contractMatches = payload.ok === true && payload.action === action && payload.model === model; + const stored = payload.stored === true; + const prepared = payload.prepared === true; + const needsPrepare = payload.needs_prepare === true; const completed = action === "model-set" ? stored : prepared; if (!contractMatches || !completed) return failedModelAction(action, model); return { ok: true, action, model, stored, prepared, needsPrepare }; @@ -235,22 +231,21 @@ function failedModelSwap(reason = "model_owner_unknown", stage = "failed"): Mode } function parseModelSwapAction(value: unknown): ModelSwapActionResult { - if (!value || typeof value !== "object" || Array.isArray(value)) return failedModelSwap(); - const record = value as Record; - if (record.action !== "model-swap" || record.model !== SWAP_MODEL_ID) return failedModelSwap(); - const stage = typeof record.stage === "string" && MODEL_SWAP_STAGES.has(record.stage) - ? record.stage + const payload = record(value); + if (!payload || payload.action !== "model-swap" || payload.model !== SWAP_MODEL_ID) return failedModelSwap(); + const stage = typeof payload.stage === "string" && MODEL_SWAP_STAGES.has(payload.stage) + ? payload.stage : "failed"; - const reason = typeof record.reason === "string" && MODEL_SWAP_REASONS.has(record.reason) - ? record.reason + const reason = typeof payload.reason === "string" && MODEL_SWAP_REASONS.has(payload.reason) + ? payload.reason : "model_owner_unknown"; - const stages = Array.isArray(record.stages) - ? record.stages.filter((item): item is string => typeof item === "string" && MODEL_SWAP_STAGES.has(item)).slice(0, 12) + const stages = Array.isArray(payload.stages) + ? payload.stages.filter((item): item is string => typeof item === "string" && MODEL_SWAP_STAGES.has(item)).slice(0, 12) : []; - const stored = record.stored === true; - const prepared = record.prepared === true; + const stored = payload.stored === true; + const prepared = payload.prepared === true; return { - ok: record.ok === true && stage === "ready" && stored && prepared + ok: payload.ok === true && stage === "ready" && stored && prepared && (reason === "ready" || reason === "already_resident"), action: "model-swap", model: SWAP_MODEL_ID, From abc063bcca97493345a1e9afbdd21e5709e218a3 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:16:18 +0900 Subject: [PATCH 65/67] refactor(desktop): key the pipeline stage weights by PipelineStage The table was typed Record, so a misspelled stage would have compiled and silently weighed nothing. Types only: the built bundle is byte-identical. Co-Authored-By: Claude Fable 5.2 --- desktop/src/core/load-mapping.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/desktop/src/core/load-mapping.ts b/desktop/src/core/load-mapping.ts index f056bca8..11c74e50 100644 --- a/desktop/src/core/load-mapping.ts +++ b/desktop/src/core/load-mapping.ts @@ -1,4 +1,4 @@ -import type { BackgroundStatus, JobEvent, PipelineStatus } from "../contracts"; +import type { BackgroundStatus, JobEvent, PipelineStage, PipelineStatus } from "../contracts"; const TOTAL_BOOST = 0.35; @@ -23,7 +23,7 @@ export interface LatticePulse { // queue .20, context .35, model .85, delay .05, send .60, confirm .25. // The table lives at module scope: the poll path rebuilt it on every call // before this (2026-09-22). -const PIPELINE_STAGE_WEIGHTS: Readonly> = { +const PIPELINE_STAGE_WEIGHTS: Readonly>> = { detect: 0.10, authorize: 0.15, queue: 0.20, From 11c82ca73337347706e4ab59c5bc0ff7d1d0005d Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:17:26 +0900 Subject: [PATCH 66/67] docs(comments): drop history narration and a code that is never returned Several comments told the reader what the code used to do, with the date it changed. Git holds that; the comments now state only the constraint that still applies. The stage-weight comment restated the table under it and is removed. attest_app_owned_server's docstring listed attest_unknown, which the function cannot return. The built front-end bundle is byte-identical. Co-Authored-By: Claude Fable 5.2 --- desktop/src/core/jarvis-core.ts | 2 +- desktop/src/core/lifecycle-wiring.ts | 5 ++--- desktop/src/core/lifecycle.ts | 12 +++++------- desktop/src/core/load-mapping.ts | 12 +++--------- desktop/src/core/render-state.ts | 2 +- scripts/mlx_serve_lifecycle.py | 5 ++--- 6 files changed, 14 insertions(+), 24 deletions(-) diff --git a/desktop/src/core/jarvis-core.ts b/desktop/src/core/jarvis-core.ts index 77f60c2b..9700fbeb 100644 --- a/desktop/src/core/jarvis-core.ts +++ b/desktop/src/core/jarvis-core.ts @@ -130,7 +130,7 @@ export class JarvisCore { private render(dt: number): void { // One reused frame object per tick: no array, closure or object is built - // inside the 15-30fps loop (2026-09-22). + // inside the 15-30fps loop. const frame = this.state.step(dt, this.ringBaseVelocity, this.ringGain); const rings = this.rings; const velocities = frame.ringVelocities; diff --git a/desktop/src/core/lifecycle-wiring.ts b/desktop/src/core/lifecycle-wiring.ts index 35192a9e..b1e10f46 100644 --- a/desktop/src/core/lifecycle-wiring.ts +++ b/desktop/src/core/lifecycle-wiring.ts @@ -15,8 +15,7 @@ export type VisibilityReader = () => Promise; * A hidden menubar window cannot be trusted to report itself: whether * `document.visibilityState` and `blur` fire at all depends on how AppKit * ordered the window out. The 3D loop has to stop on hide, not "usually", so - * Rust emits the state after `show()`/`hide()` has already returned - * (2026-09-22). + * Rust emits the state after `show()`/`hide()` has already returned. */ export const tauriVisibilitySubscriber: VisibilitySubscriber = async (handler) => { const unlisten = await listen<{ visible?: unknown }>(VISIBILITY_EVENT, (event) => { @@ -30,7 +29,7 @@ export const tauriVisibilitySubscriber: VisibilitySubscriber = async (handler) = * * The event above only reports changes, so a webview that boots while its * window is hidden never learns that from the stream. Until this answers, the - * render lifecycle stays hidden (2026-09-22). + * render lifecycle stays hidden. */ export const tauriVisibilityReader: VisibilityReader = async () => { const visible = await invoke("window_is_visible"); diff --git a/desktop/src/core/lifecycle.ts b/desktop/src/core/lifecycle.ts index acc76739..045c5f4b 100644 --- a/desktop/src/core/lifecycle.ts +++ b/desktop/src/core/lifecycle.ts @@ -17,10 +17,9 @@ export class RenderLifecycle { transition(state: LifecycleState): void { if (state === "visible") { if (this.active) return; - // `active` only follows a start that actually happened. Claiming it first - // meant a throwing `start()` left the panel inactive-but-marked-active, - // so every later visible signal returned early and nothing rendered until - // the next hide/show cycle (2026-09-22). + // `active` only follows a start that actually happened. Set before a + // throwing `start()`, it would make every later visible signal return + // early, and nothing would render until the next hide/show cycle. try { this.loop.start(); } catch { @@ -31,9 +30,8 @@ export class RenderLifecycle { return; } this.active = false; - // A throwing timer cleanup used to skip the loop stop and leave a render - // loop running behind a hidden window, so each half is guarded on its own - // (2026-09-22). + // Each half is guarded on its own: a throwing timer cleanup must not skip + // the loop stop and leave a render loop running behind a hidden window. this.safely(this.stopTimers); this.safely(() => this.loop.stop()); } diff --git a/desktop/src/core/load-mapping.ts b/desktop/src/core/load-mapping.ts index 11c74e50..8d6c0dcc 100644 --- a/desktop/src/core/load-mapping.ts +++ b/desktop/src/core/load-mapping.ts @@ -19,10 +19,6 @@ export interface LatticePulse { density: number; } -// Active reply phases map to bounded load: detect .10, authorize .15, -// queue .20, context .35, model .85, delay .05, send .60, confirm .25. -// The table lives at module scope: the poll path rebuilt it on every call -// before this (2026-09-22). const PIPELINE_STAGE_WEIGHTS: Readonly>> = { detect: 0.10, authorize: 0.15, @@ -39,9 +35,8 @@ export function clamp01(value: number): number { return Math.min(1, Math.max(0, value)); } -// Branch instead of building `[reply, geeknews, dbSync]` per frame. The order -// and the "0 for anything past the third ring" fallback match the array the -// allocating path used. +// Branch instead of building `[reply, geeknews, dbSync]` per frame. A ring +// past the third has no source of its own. function sourceAt(loads: SourceLoads, index: number): number { if (index === 0) return loads.reply; if (index === 1) return loads.geeknews; @@ -84,8 +79,7 @@ export function ringTargetVelocities( * * (1 + 0.35 * max(clamp01(total), clamp01(voice))). The last factor is * exactly 1 when both global signals are zero. * - * The render loop owns one scratch array and writes into it, so a 15-30fps - * loop stops allocating a fresh array per frame (2026-09-22). + * The render loop passes its own scratch array, so a frame allocates nothing. */ export function writeRingTargetVelocities( out: number[], diff --git a/desktop/src/core/render-state.ts b/desktop/src/core/render-state.ts index 2c9dd4c7..d2656358 100644 --- a/desktop/src/core/render-state.ts +++ b/desktop/src/core/render-state.ts @@ -39,7 +39,7 @@ export function ringTilt(index: number): number { * The state object is also the frame result: `step` returns `this`, so the * 15-30fps loop reads fields that the math already wrote instead of copying * them into a second object. Every container it reads or writes is a stable - * field, so a frame allocates nothing (2026-09-22). + * field, so a frame allocates nothing. */ export class CoreRenderState { /** Clamped seconds of the last step; 0 for a refused step. */ diff --git a/scripts/mlx_serve_lifecycle.py b/scripts/mlx_serve_lifecycle.py index fd2f1ddf..f329aafd 100644 --- a/scripts/mlx_serve_lifecycle.py +++ b/scripts/mlx_serve_lifecycle.py @@ -665,9 +665,8 @@ def attest_app_owned_server( ) -> str: """Return an empty string when the recorded pid is provably app-owned. - Any other value is one of four bounded codes rather than a free-form - reason: attest_not_alive, attest_command_mismatch, attest_not_listening, - or attest_unknown. + Any other value is one of three bounded codes rather than a free-form + reason: attest_not_alive, attest_command_mismatch, or attest_not_listening. """ runtime = hooks or LaunchHooks() From 53c6a2859fd2371bdf8e8a28da287cec84012726 Mon Sep 17 00:00:00 2001 From: Yeonwoo Choi <32544727+twoimo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:31:59 +0900 Subject: [PATCH 67/67] test: read moved Rust sources from their new modules Two Python tests read Rust source text at fixed paths. The context split moved the reference-pack schema and prefix filter out of src/context/mod.rs, and the main.rs split moved LOCAL_MLX_PROBE_TIMEOUT into src/commands/auto_reply/llm.rs. Read the whole context module and the new llm.rs instead. Co-Authored-By: Claude Opus 5.5 --- tests/test_auto_reply_reference_store.py | 5 ++++- tests/test_auto_reply_service_entry.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_auto_reply_reference_store.py b/tests/test_auto_reply_reference_store.py index e34c86e4..62156031 100644 --- a/tests/test_auto_reply_reference_store.py +++ b/tests/test_auto_reply_reference_store.py @@ -511,7 +511,10 @@ def test_menubar_lists_reference_source_and_swift_exposes_menu(self): self.assertIn("누가 무엇을 어떻게 왜", source) changelog = (ROOT / "CHANGELOG.md").read_text(encoding="utf-8") self.assertIn("lecture-pack-v2", changelog) - rust = (ROOT / "src" / "context" / "mod.rs").read_text(encoding="utf-8") + rust = "\n".join( + path.read_text(encoding="utf-8") + for path in sorted((ROOT / "src" / "context").glob("*.rs")) + ) self.assertIn("CREATE TABLE IF NOT EXISTS context_reference_packs(", rust) self.assertIn('item.result.message.starts_with(REFERENCE_PREFIX)', rust) menubar = MENUBAR.read_text(encoding="utf-8") diff --git a/tests/test_auto_reply_service_entry.py b/tests/test_auto_reply_service_entry.py index 3aa1f331..e236a1e4 100644 --- a/tests/test_auto_reply_service_entry.py +++ b/tests/test_auto_reply_service_entry.py @@ -395,7 +395,7 @@ def transient_then_ok(_binary, _config, selectors, **_kwargs): def test_local_mlx_probe_timeout_covers_cold_start_within_service_budget(self): module = load_entry("auto_reply_probe_budget_test") - source = (ROOT / "src" / "main.rs").read_text(encoding="utf-8") + source = (ROOT / "src" / "commands" / "auto_reply" / "llm.rs").read_text(encoding="utf-8") marker = "const LOCAL_MLX_PROBE_TIMEOUT: Duration = Duration::from_secs(" start = source.index(marker) + len(marker) end = source.index(");", start)