From e32745142941ebc3f16a75ef14de48919756125d Mon Sep 17 00:00:00 2001 From: shua0326 Date: Mon, 14 Sep 2026 23:16:43 +1000 Subject: [PATCH 0001/1618] fix(server): create runtime directory before acquiring spawn lock --- src/cli/dispatch.rs | 14 +++++++++++++- src/cli/dispatch_tests.rs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/cli/dispatch.rs b/src/cli/dispatch.rs index 3b7f08c0772..c735a2245bc 100644 --- a/src/cli/dispatch.rs +++ b/src/cli/dispatch.rs @@ -1,6 +1,6 @@ #![cfg_attr(test, allow(clippy::await_holding_lock))] -use anyhow::Result; +use anyhow::{Context, Result}; use std::io::IsTerminal; use std::process::{Command as ProcessCommand, Stdio}; use std::time::Instant; @@ -1196,6 +1196,18 @@ fn try_acquire_spawn_lock(path: &std::path::Path) -> Result Date: Wed, 23 Sep 2026 10:19:35 -0400 Subject: [PATCH 0002/1618] fix(gemini): default to non-dated gemini-pro-latest alias 2026-09-19: a transient NOT_FOUND on gemini-3.1-pro-preview walked the fallback chain down to gemini-2.5-pro and set_model() made the downgrade sticky. Every new session then started on the stale hardcoded default for days with no error. Use the server-side gemini-pro-latest alias as DEFAULT_MODEL and first fallback so the default tracks the newest stable pro model instead of a dated snapshot. --- .../jcode-provider-gemini-runtime/src/gemini_tests.rs | 1 + crates/jcode-provider-gemini/src/lib.rs | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/jcode-provider-gemini-runtime/src/gemini_tests.rs b/crates/jcode-provider-gemini-runtime/src/gemini_tests.rs index 2bfeee33b99..f89834aa508 100644 --- a/crates/jcode-provider-gemini-runtime/src/gemini_tests.rs +++ b/crates/jcode-provider-gemini-runtime/src/gemini_tests.rs @@ -66,6 +66,7 @@ fn fallback_models_skip_current_model() { assert_eq!( gemini_fallback_models("gemini-2.5-flash"), vec![ + "gemini-pro-latest", "gemini-3.1-pro-preview", "gemini-3-pro-preview", "gemini-2.5-pro", diff --git a/crates/jcode-provider-gemini/src/lib.rs b/crates/jcode-provider-gemini/src/lib.rs index 33e86db65cb..d22c2586015 100644 --- a/crates/jcode-provider-gemini/src/lib.rs +++ b/crates/jcode-provider-gemini/src/lib.rs @@ -4,8 +4,14 @@ use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use std::collections::HashSet; -pub const DEFAULT_MODEL: &str = "gemini-2.5-pro"; +// Non-dated server-side alias: always resolves to the newest stable pro +// model, so the default can never go stale or 404 into a downgrade chain +// (2026-09-19: a transient NOT_FOUND on gemini-3.1-pro-preview silently +// pinned every session to gemini-2.5-pro for days). +pub const DEFAULT_MODEL: &str = "gemini-pro-latest"; pub const AVAILABLE_MODELS: &[&str] = &[ + "gemini-pro-latest", + "gemini-flash-latest", "gemini-3.1-pro-preview", "gemini-3-pro-preview", "gemini-3-flash-preview", @@ -16,6 +22,7 @@ pub const AVAILABLE_MODELS: &[&str] = &[ "gemini-1.5-flash", ]; pub const FALLBACK_MODELS: &[&str] = &[ + "gemini-pro-latest", "gemini-3.1-pro-preview", "gemini-3-pro-preview", "gemini-2.5-pro", @@ -719,6 +726,7 @@ mod tests { assert_eq!( gemini_fallback_models("gemini-2.5-flash"), vec![ + "gemini-pro-latest", "gemini-3.1-pro-preview", "gemini-3-pro-preview", "gemini-2.5-pro", From 94fddc5f2133b6474465c379dbf63c22010a6589 Mon Sep 17 00:00:00 2001 From: adminwat Date: Wed, 23 Sep 2026 12:32:03 -0400 Subject: [PATCH 0003/1618] fix(anthropic): refresh OAuth model catalog behind custom base URL proxies The model-catalog prefetch bailed out whenever the messages URL differed from the official endpoint (ANTHROPIC_BASE_URL / named gateway profiles). That guard exists so gateway API keys are never sent to Anthropic's official /v1/models endpoint, but it also caught users who route /v1/messages through a local proxy (e.g. a caching proxy) while authenticating with genuine Anthropic OAuth subscription credentials. For them the catalog silently froze at the last disk snapshot, so newly released models (claude-opus-5-5 today) never appeared in the picker. Observed live: post_auth_model_refresh_completed duration_ms=0 while the disk cache stayed 12 days stale. Keep the guard for non-OAuth credentials (gateway API keys), but let genuine OAuth tokens refresh the catalog: those credentials belong to Anthropic and the catalog fetch goes directly to api.anthropic.com regardless of the proxy. Verified live: with ANTHROPIC_BASE_URL set to a local proxy, the cache now refreshes and lists claude-opus-5-5. --- .../jcode-provider-anthropic-runtime/src/lib.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/jcode-provider-anthropic-runtime/src/lib.rs b/crates/jcode-provider-anthropic-runtime/src/lib.rs index 1d0e0f3de29..13018e35240 100644 --- a/crates/jcode-provider-anthropic-runtime/src/lib.rs +++ b/crates/jcode-provider-anthropic-runtime/src/lib.rs @@ -1511,14 +1511,23 @@ impl Provider for AnthropicProvider { } async fn prefetch_models(&self) -> Result<()> { - if self.direct_transport.api_url != API_URL { - // Never send named gateway credentials to Anthropic's catalog. - return Ok(()); - } + // A custom messages URL (ANTHROPIC_BASE_URL / named profile) must not + // leak gateway API keys to Anthropic's official model-catalog + // endpoint. Genuine Anthropic OAuth credentials are a different case: + // users often route /v1/messages through a local proxy (e.g. a + // caching proxy) while authenticating with their real subscription, + // and skipping the refresh for them silently freezes the model list + // at the last disk snapshot, hiding newly released models. The + // catalog fetch itself always goes directly to api.anthropic.com. + let custom_gateway = self.direct_transport.api_url != API_URL; // Discovery is independent of the selected chat credential mode. Do not // mutate that mode on this shared provider just to inspect another route. // API discovery gets first opportunity, OAuth still runs on API failure. for oauth in [false, true] { + if custom_gateway && !oauth { + // Never send named gateway API keys to Anthropic's catalog. + continue; + } let configured = if oauth { auth::claude::load_credentials().is_ok() } else { From 091f665e39dda1b93ab43558ab91335f68bbfcc4 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Thu, 24 Sep 2026 07:06:39 +0800 Subject: [PATCH 0004/1618] Guard account command token boundaries --- .../src/tui/app/auth_account_commands.rs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/jcode-tui/src/tui/app/auth_account_commands.rs b/crates/jcode-tui/src/tui/app/auth_account_commands.rs index 6ae2fb84836..b8bfa3232dd 100644 --- a/crates/jcode-tui/src/tui/app/auth_account_commands.rs +++ b/crates/jcode-tui/src/tui/app/auth_account_commands.rs @@ -114,8 +114,11 @@ pub(crate) async fn handle_account_command_remote( fn parse_account_command(trimmed: &str) -> Option> { let rest = trimmed - .strip_prefix("/account") - .or_else(|| trimmed.strip_prefix("/accounts"))?; + .strip_prefix("/accounts") + .or_else(|| trimmed.strip_prefix("/account"))?; + if !rest.is_empty() && !rest.chars().next().is_some_and(char::is_whitespace) { + return None; + } let rest = rest.trim(); if rest.is_empty() { return Some(Ok(AccountCommand::OpenOverlay { @@ -1196,6 +1199,20 @@ mod tests { )); } + #[test] + fn parse_account_command_requires_a_token_boundary() { + assert!( + parse_account_command("/accounting").is_none(), + "/accounting must not be parsed as /account with label `ing`" + ); + assert!(matches!( + parse_account_command("/accounts"), + Some(Ok(AccountCommand::OpenOverlay { + provider_filter: None + })) + )); + } + #[test] fn render_auth_doctor_markdown_includes_recovery_steps() { let _guard = crate::storage::lock_test_env(); From 43bc4363f8229cb7adbd4a320c6bbe3dd0c547f6 Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Thu, 24 Sep 2026 11:28:40 +0100 Subject: [PATCH 0005/1618] fix(id): never reuse an occupied session identity while one is free Each allocation advanced a process-wide cursor once per candidate. When other creators advanced it between steps, a caller's len steps could skip every free name and fall back to reusing an occupied one, which the existing test caught under parallel test load ('allocator reused an available identity'). Keep the cursor walk (distinct candidates for concurrent creators) and scan deterministically before any reuse. The new test holds one name free while four threads advance the cursor; it fails 3/3 on the old allocator and passes on this one. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AneFCxyosVWum8kYPUkcEk --- crates/jcode-core/src/id.rs | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/jcode-core/src/id.rs b/crates/jcode-core/src/id.rs index 99f536dcaef..e30ca2895f6 100644 --- a/crates/jcode-core/src/id.rs +++ b/crates/jcode-core/src/id.rs @@ -271,6 +271,14 @@ pub fn new_memorable_session_id_avoiding(used_names: &HashSet) -> (Strin let (word, _) = SESSION_NAMES[idx]; (!used_names.contains(word)).then_some(word) }) + // Concurrent creators advance the shared cursor between our steps, so the loop above can + // skip every free name; scan deterministically before falling back to reuse. + .or_else(|| { + SESSION_NAMES + .iter() + .map(|(word, _)| *word) + .find(|word| !used_names.contains(*word)) + }) .unwrap_or_else(|| { let idx = cursor.fetch_add(1, Ordering::Relaxed) % SESSION_NAMES.len(); SESSION_NAMES[idx].0 @@ -435,6 +443,38 @@ mod tests { assert!(used.contains(&reused)); } + #[test] + fn avoiding_allocator_finds_the_last_free_identity_while_others_advance_the_cursor() { + let free = SESSION_NAMES[SESSION_NAMES.len() / 2].0; + let used: HashSet = SESSION_NAMES + .iter() + .map(|(word, _)| word.to_string()) + .filter(|word| word != free) + .collect(); + let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let noise: Vec<_> = (0..4) + .map(|_| { + let stop = stop.clone(); + std::thread::spawn(move || { + while !stop.load(Ordering::Relaxed) { + let _ = new_memorable_session_id(); + } + }) + }) + .collect(); + let names: Vec = (0..200) + .map(|_| new_memorable_session_id_avoiding(&used).1) + .collect(); + stop.store(true, Ordering::Relaxed); + for thread in noise { + let _ = thread.join(); + } + assert!( + names.iter().all(|name| name == free), + "reused an occupied identity while one was free" + ); + } + /// Returns true for emoji that commonly fail to render as a single glyph on /// older terminal fonts or in window titles: ZWJ sequences (split into /// pieces), codepoints added in Unicode 13.0 or later (rendered as tofu From 88332e8f562c13444577c61a499780498ddaa2e1 Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Thu, 24 Sep 2026 09:53:41 +0100 Subject: [PATCH 0006/1618] test(tui): pin the Updates box empty in the live batch prepare test The test's last assertion scans the whole rendered screen for '#1'/'#2' index labels, but the screen includes the Updates box, which lists the build's recent commit subjects. Any subject referencing a #1xxx/#2xxx PR fails it; CI never sees this because actions/checkout fetches one commit. Use the existing test override around prepare_messages, reset before any assertion. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AneFCxyosVWum8kYPUkcEk --- crates/jcode-tui/src/tui/ui_tests/prepare.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/jcode-tui/src/tui/ui_tests/prepare.rs b/crates/jcode-tui/src/tui/ui_tests/prepare.rs index 13633d7d9cb..92332573dff 100644 --- a/crates/jcode-tui/src/tui/ui_tests/prepare.rs +++ b/crates/jcode-tui/src/tui/ui_tests/prepare.rs @@ -581,7 +581,10 @@ fn test_prepare_messages_shows_live_batch_progress_in_chat_history() { ..Default::default() }; + // The Updates box lists recent commit subjects ("… PR #1440"); they trip the #N check below. + crate::tui::ui::header::set_unseen_changelog_entries_override_for_tests(Some(Vec::new())); let prepared = prepare::prepare_messages(&state, 100, 30); + crate::tui::ui::header::set_unseen_changelog_entries_override_for_tests(None); let rendered: Vec = prepared .materialize_all_lines() .iter() From 92d868767f2a456efb6c67bb163e52918f44d4dc Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Thu, 24 Sep 2026 09:56:13 +0100 Subject: [PATCH 0007/1618] test(browser): keep the hanging CLI fixture a single process The fixture was '#!/bin/sh' + 'sleep 600'. kill_on_drop kills only the direct child, so on the timeout path the shell died and the sleep grandchild lived on for ten minutes with the inherited pipes (and any lock descriptor the test process held). 'exec sleep 600' matches a real single-process CLI; no orphan remains after the test. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AneFCxyosVWum8kYPUkcEk --- crates/jcode-base/src/browser_tests.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/jcode-base/src/browser_tests.rs b/crates/jcode-base/src/browser_tests.rs index 1e03ccf5e92..0b63ac6cf83 100644 --- a/crates/jcode-base/src/browser_tests.rs +++ b/crates/jcode-base/src/browser_tests.rs @@ -297,8 +297,9 @@ fn write_executable(path: &std::path::Path, script: &str) { async fn hanging_browser_cli_times_out_instead_of_blocking_forever() { let temp = tempfile::tempdir().expect("temp dir"); let bin = temp.path().join("browser"); - // Stands in for a CLI waiting on a bridge that will never answer. - write_executable(&bin, "#!/bin/sh\nsleep 600\n"); + // Stands in for a CLI waiting on a bridge that will never answer. `exec` keeps it one + // process: kill_on_drop kills only the direct child, so a forked sleep outlived the test. + write_executable(&bin, "#!/bin/sh\nexec sleep 600\n"); let started = std::time::Instant::now(); let result = run_browser_cli_capped(&bin, &["ping"], std::time::Duration::from_millis(300)) From 645b1b62a6f333a17bc4afe7aad77e9573958097 Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Thu, 24 Sep 2026 14:34:42 +0100 Subject: [PATCH 0008/1618] test(jev): allow loaded clients to reach the mock listener The accept deadline bounds a broken fixture; it is not a latency assertion. Allow thirty seconds for a scheduled client to connect while preserving the request assertions and the accepted socket's existing read timeout. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AneFCxyosVWum8kYPUkcEk --- crates/jcode-base/src/jev.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/jcode-base/src/jev.rs b/crates/jcode-base/src/jev.rs index 3b583a8290b..03f86e6cadd 100644 --- a/crates/jcode-base/src/jev.rs +++ b/crates/jcode-base/src/jev.rs @@ -1107,7 +1107,7 @@ mod tests { let worker = std::thread::spawn(move || { let mut requests = Vec::new(); for _ in 0..count { - let deadline = std::time::Instant::now() + Duration::from_secs(5); + let deadline = std::time::Instant::now() + Duration::from_secs(30); let mut stream = loop { match listener.accept() { Ok((stream, _)) => break stream, From 7acc781b4cf4325226d7108a926d28fdb5f51eb9 Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Thu, 24 Sep 2026 14:39:40 +0100 Subject: [PATCH 0009/1618] fix(auth): recognize test binaries in custom Cargo target directories Identify Cargo's deps directory and hashed executable name instead of requiring an ancestor literally named target. Cover default, custom, cross-target and Windows paths and reject installed executable paths. Keep the explicit interactive-test opt-out unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AneFCxyosVWum8kYPUkcEk --- crates/jcode-base/src/auth/mod.rs | 29 ++++++++++++++++++++------ crates/jcode-base/src/auth/tests.rs | 32 +++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/crates/jcode-base/src/auth/mod.rs b/crates/jcode-base/src/auth/mod.rs index cb57d3f9dec..ac005d78b44 100644 --- a/crates/jcode-base/src/auth/mod.rs +++ b/crates/jcode-base/src/auth/mod.rs @@ -136,8 +136,8 @@ fn browser_unusable_here() -> bool { } /// True when the current process is a Rust test binary (`cargo test` / -/// `cargo nextest`). Test binaries always run from `target/**/deps/`, a -/// location no installed or self-dev jcode binary ever runs from. +/// `cargo nextest`). Cargo places hashed test executables in `deps/` beneath +/// its build directory, which can be relocated with `CARGO_TARGET_DIR`. /// /// Used to keep tests from opening real browser windows (OAuth login pages, /// files) on the developer's desktop: many login/onboarding flows are @@ -152,14 +152,31 @@ pub fn running_in_test_harness() -> bool { } std::env::current_exe() .ok() - .map(|exe| { - let path = exe.to_string_lossy().replace('\\', "/"); - path.contains("/target/") && path.contains("/deps/") - }) + .map(|exe| is_test_harness_path(&exe.to_string_lossy())) .unwrap_or(false) }) } +fn is_test_harness_path(path: &str) -> bool { + let path = path.replace('\\', "/"); + // Preserve detection for the default target tree, including custom runners. + if path.contains("/target/") && path.contains("/deps/") { + return true; + } + let mut components = path.rsplit('/'); + let Some(filename) = components.next() else { + return false; + }; + if components.next() != Some("deps") { + return false; + } + let stem = filename.strip_suffix(".exe").unwrap_or(filename); + let Some((name, hash)) = stem.rsplit_once('-') else { + return false; + }; + !name.is_empty() && hash.len() == 16 && hash.bytes().all(|byte| byte.is_ascii_hexdigit()) +} + fn env_truthy(key: &str) -> bool { std::env::var(key) .ok() diff --git a/crates/jcode-base/src/auth/tests.rs b/crates/jcode-base/src/auth/tests.rs index c43640e5570..10f184b43fb 100644 --- a/crates/jcode-base/src/auth/tests.rs +++ b/crates/jcode-base/src/auth/tests.rs @@ -947,13 +947,13 @@ fn claude_oauth_provider_reports_oauth_independently_of_api_key() { /// Test binaries must never open real browser windows: login/onboarding flows /// are exercised heavily by unit tests, and each ungated `open::that` pops an /// OAuth page on the developer's desktop. `running_in_test_harness` detects -/// the `target/**/deps/` test-binary path, and `browser_suppressed` must honor +/// the Cargo `deps/` test-binary path, and `browser_suppressed` must honor /// it even without --no-browser or NO_BROWSER/JCODE_NO_BROWSER. #[test] fn browser_suppressed_inside_test_harness_without_env_overrides() { assert!( super::running_in_test_harness(), - "test binary should be detected as a test harness (exe under target/**/deps/)" + "test binary should be detected even with a custom Cargo target directory" ); assert!( super::browser_suppressed(false), @@ -961,6 +961,34 @@ fn browser_suppressed_inside_test_harness_without_env_overrides() { ); } +#[test] +fn test_harness_paths_include_custom_cargo_target_directories() { + for path in [ + "/work/target/debug/deps/test_binary-0123456789abcdef", + "/work/target/debug/deps/custom_runner", + "/build-cache/debug/deps/test_binary-0123456789abcdef", + "/build-cache/aarch64-apple-darwin/release/deps/test_binary-0123456789abcdef", + r"C:\build-cache\debug\deps\test_binary-0123456789abcdef.exe", + ] { + assert!( + super::is_test_harness_path(path), + "missed test binary: {path}" + ); + } + for path in [ + "/usr/local/bin/jcode", + "/work/target/debug/jcode", + "/build-cache/debug/deps/jcode", + "/build-cache/debug/deps/libjcode-0123456789abcdef.rlib", + "/build-cache/debug/deps/test_binary-not_a_cargo_hash", + ] { + assert!( + !super::is_test_harness_path(path), + "not a test binary: {path}" + ); + } +} + /// Antigravity/Gemini access tokens live about an hour and are refreshed /// transparently on the next request. Reporting `Expired` just because the /// cached access token aged out made a fully working provider render as broken From 8f80622938736af1ea70c973f513194b8017e303 Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Thu, 24 Sep 2026 11:28:40 +0100 Subject: [PATCH 0010/1618] test(panel): search the global bus for the first panel event panel_registered_lifecycle_and_validation asserted that the head of the process-global bus was its own SidePanelUpdated; a concurrent test's event failed it under load. Drain and search, as the test's later PDF check does. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AneFCxyosVWum8kYPUkcEk --- crates/jcode-app-core/src/tool/panel_tests.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/jcode-app-core/src/tool/panel_tests.rs b/crates/jcode-app-core/src/tool/panel_tests.rs index e5ff0225cc6..a988c8c15a4 100644 --- a/crates/jcode-app-core/src/tool/panel_tests.rs +++ b/crates/jcode-app-core/src/tool/panel_tests.rs @@ -55,9 +55,14 @@ async fn panel_registered_lifecycle_and_validation() { assert!(first.output.contains(&format!( "panel_id: {id}\nidentity: side-panel://panel-test/{id}" ))); - assert!( - matches!(events.try_recv().unwrap(), crate::bus::BusEvent::SidePanelUpdated(update) if update.snapshot == first_state) - ); + // The bus is process-global: concurrent tests publish on it too, so search, don't take the head. + let mut saw_first = false; + while let Ok(event) = events.try_recv() { + if let crate::bus::BusEvent::SidePanelUpdated(update) = event { + saw_first |= update.snapshot == first_state; + } + } + assert!(saw_first, "the first panel write must publish its snapshot"); let second = registry .execute( "panel", From 3ae25f2289f64ddb36ed0238d4e45e84897c8764 Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Thu, 24 Sep 2026 14:41:13 +0100 Subject: [PATCH 0011/1618] test(panel): inject unrelated traffic before the expected snapshot Make the process-global event ordering regression deterministic by placing another session's panel event before the test's own write. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AneFCxyosVWum8kYPUkcEk --- crates/jcode-app-core/src/tool/panel_tests.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/jcode-app-core/src/tool/panel_tests.rs b/crates/jcode-app-core/src/tool/panel_tests.rs index a988c8c15a4..5f9b2bb8591 100644 --- a/crates/jcode-app-core/src/tool/panel_tests.rs +++ b/crates/jcode-app-core/src/tool/panel_tests.rs @@ -42,6 +42,13 @@ async fn panel_registered_lifecycle_and_validation() { .expect("model-visible panel definition"); assert!(definition.description.contains("desktop panels")); let mut events = crate::bus::Bus::global().subscribe(); + // Another session's event may arrive before this test's panel write. + crate::bus::Bus::global().publish(crate::bus::BusEvent::SidePanelUpdated( + crate::bus::SidePanelUpdated { + session_id: "other-panel-test".into(), + snapshot: SidePanelSnapshot::default(), + }, + )); let first = registry .execute( "panel", From b8aa0bcda6111d6c9c42e927d40427e494d4587e Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Thu, 24 Sep 2026 14:45:24 +0100 Subject: [PATCH 0012/1618] test(bash): hold the environment lock during the scratch-directory probe Other tests repoint JCODE_HOME and remove their temporary directories. Keep the environment lock until the final directory assertion and run the same configured command synchronously, avoiding a MutexGuard across await. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AneFCxyosVWum8kYPUkcEk --- crates/jcode-app-core/src/tool/bash.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/jcode-app-core/src/tool/bash.rs b/crates/jcode-app-core/src/tool/bash.rs index 23843103ba5..a3263546470 100644 --- a/crates/jcode-app-core/src/tool/bash.rs +++ b/crates/jcode-app-core/src/tool/bash.rs @@ -785,12 +785,15 @@ mod utf8_truncation_tests { } #[cfg(unix)] - #[tokio::test] - async fn build_shell_command_uses_disk_backed_scratch_directory() { + #[test] + fn build_shell_command_uses_disk_backed_scratch_directory() { + // Keep JCODE_HOME stable until the final directory assertion. Running + // synchronously avoids holding the environment MutexGuard across await. + let _env_lock = crate::storage::lock_test_env(); let expected = super::tool_scratch_dir().expect("jcode scratch directory"); let output = build_shell_command("printf '%s\\n%s\\n' \"$TMPDIR\" \"$JCODE_SCRATCH_DIR\"") + .as_std_mut() .output() - .await .expect("run bash command"); assert!(output.status.success(), "bash command should succeed"); let stdout = String::from_utf8(output.stdout).expect("utf-8 scratch paths"); From c9b6e3c0127d50b96d2635eb787afc578768dcc4 Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Thu, 24 Sep 2026 09:53:42 +0100 Subject: [PATCH 0013/1618] test(e2e): opt scenario homes out of telemetry A concurrency lease that finishes during runtime shutdown ran after the TempDir removed the home and recreated telemetry_concurrency_v2/registry.lock, leaving one jcode-e2e-home-* directory per test in $TMPDIR (24 per run). A no_telemetry marker in the scenario home stops the lease and the telemetry round-trips: e2e 60/0 now leaves no directory and runs in ~5 s, not ~33 s. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AneFCxyosVWum8kYPUkcEk --- tests/e2e/test_support/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/e2e/test_support/mod.rs b/tests/e2e/test_support/mod.rs index 8f2dad063a8..bf1dafa32eb 100644 --- a/tests/e2e/test_support/mod.rs +++ b/tests/e2e/test_support/mod.rs @@ -71,6 +71,9 @@ impl TestEnvGuard { let temp_home = tempfile::Builder::new() .prefix("jcode-e2e-home-") .tempdir()?; + // No telemetry: a concurrency lease finishing at runtime shutdown otherwise recreates + // telemetry_concurrency_v2/ after the TempDir is removed, leaking one dir per test. + std::fs::write(temp_home.path().join("no_telemetry"), b"")?; let prev_home = std::env::var_os("JCODE_HOME"); let prev_runtime_dir = std::env::var_os("JCODE_RUNTIME_DIR"); let prev_test_session = std::env::var_os("JCODE_TEST_SESSION"); From f5215c06d7db260aeefd0597e0cca927d1b8c076 Mon Sep 17 00:00:00 2001 From: Khaled Date: Thu, 24 Sep 2026 21:42:43 +0700 Subject: [PATCH 0014/1618] fix(mermaid): detect foot as a sixel terminal foot has built-in sixel support (since 1.2.0, no libsixel) and sets TERM=foot with no TERM_PROGRAM, but env-based protocol inference had no foot branch. With TERM=foot it returned None, the picker stayed on Halfblocks, native_image_protocol_available() was false, and mermaid blocks fell back to source text. Map foot to ProtocolType::Sixel so diagrams render without the opt-in JCODE_MERMAID_PICKER_PROBE startup probe. foot only advertises DA1 attribute 4 when sixel is enabled, so a foot built or configured without sixel ([tweak].sixel=no) is still mis-detected here; JCODE_MERMAID_PICKER_PROBE=1 probes the real DA1 and overrides the protocol. Fixes #1475 --- .../jcode-tui-mermaid/src/mermaid_runtime.rs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/jcode-tui-mermaid/src/mermaid_runtime.rs b/crates/jcode-tui-mermaid/src/mermaid_runtime.rs index b8a1a381f2e..a9d23bd3a2d 100644 --- a/crates/jcode-tui-mermaid/src/mermaid_runtime.rs +++ b/crates/jcode-tui-mermaid/src/mermaid_runtime.rs @@ -218,6 +218,16 @@ pub(super) fn infer_protocol_from_env( return None; } + // foot has built-in sixel support (since 1.2.0, no libsixel) and sets + // TERM=foot with no TERM_PROGRAM, so TERM is the signal. foot only + // advertises DA1 attribute 4 when sixel is enabled at runtime + // ([tweak].sixel=no) and compile time, so a disabled-sixel foot is still + // mis-detected here; JCODE_MERMAID_PICKER_PROBE=1 probes the real DA1 and + // overrides the protocol with the truthful answer. + if term.starts_with("foot") || term_program.contains("foot") { + return Some(ProtocolType::Sixel); + } + if term.contains("sixel") { return Some(ProtocolType::Sixel); } @@ -773,6 +783,15 @@ mod tests { infer_protocol_from_env(Some("xterm-sixel"), None, None, None), Some(ProtocolType::Sixel) ); + assert_eq!( + infer_protocol_from_env(Some("foot"), Some("foot"), None, None), + Some(ProtocolType::Sixel) + ); + // foot sets TERM=foot but no TERM_PROGRAM. + assert_eq!( + infer_protocol_from_env(Some("foot"), None, None, None), + Some(ProtocolType::Sixel) + ); } #[test] @@ -786,10 +805,6 @@ mod tests { ), Some(ProtocolType::Iterm2) ); - assert_eq!( - infer_protocol_from_env(Some("foot"), Some("foot"), None, None), - None - ); assert_eq!( infer_protocol_from_env(Some("xterm-256color"), Some("konsole"), None, None), None From 9b65e775f25082059957af284b369edc360003b4 Mon Sep 17 00:00:00 2001 From: adminwat Date: Thu, 24 Sep 2026 16:48:23 -0400 Subject: [PATCH 0015/1618] fix(agentgrep): bump agentgrep to v0.1.7 so rg exit-2 partial results are not discarded Observed on macOS (2026-09-24): 52 "agentgrep failure ... rg failed with exit code 2" log entries. When agentgrep scans from ~, rg walks SIP/TCC-protected dirs (~/Library/Trial, ~/Library/Caches/ com.apple.ap.adprivacyd, ~/Library/Mail, ...) and gets "Operation not permitted (os error 1)" per dir. rg then exits 2 even when the rest of the tree searched fine. Live repro: `rg -l jcode Library/Trial Library/Caches/com.apple.ap.adprivacyd Documents` -> exit=2 with 6 valid matches on stdout. agentgrep v0.1.6 treated any rg exit code other than 0/1 as a hard failure and threw away the partial stdout, so the whole tool call failed. v0.1.7 upstream already fixes this: exit 2 with stdout is treated as authoritative partial output, and exit 2 with no stdout falls back to the native (non-rg) search path instead of erroring. Adaptation: v0.1.7 adds an opt-in `no_follow` guardrail field to GrepArgs/FindArgs; initialize it to false to keep the pre-v0.1.7 follow-symlinks behavior. Verified: all 33 jcode-app-core agentgrep tests pass. --- Cargo.lock | 10 +++++----- crates/jcode-app-core/Cargo.toml | 2 +- crates/jcode-app-core/src/tool/agentgrep/args.rs | 4 ++++ crates/jcode-app-core/src/tool/agentgrep_tests.rs | 1 + 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17dd8160ac8..b86cf060033 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,8 +30,8 @@ dependencies = [ [[package]] name = "agentgrep" -version = "0.1.6" -source = "git+https://github.com/1jehuang/agentgrep.git?tag=v0.1.6#b01b804008ab0662fa14e6b60b10bff61716e6f1" +version = "0.1.7" +source = "git+https://github.com/1jehuang/agentgrep.git?tag=v0.1.7#525fc54117962f5f75f93cbffb53b8becf7b3add" dependencies = [ "clap", "globset", @@ -143,7 +143,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -154,7 +154,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -8870,7 +8870,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] diff --git a/crates/jcode-app-core/Cargo.toml b/crates/jcode-app-core/Cargo.toml index 407dd5a0a6c..ab53a3b838d 100644 --- a/crates/jcode-app-core/Cargo.toml +++ b/crates/jcode-app-core/Cargo.toml @@ -106,7 +106,7 @@ jcode-tool-types = { path = "../jcode-tool-types" } flate2 = "1" tar = "0.4" tempfile = "3" -agentgrep = { git = "https://github.com/1jehuang/agentgrep.git", tag = "v0.1.6" } +agentgrep = { git = "https://github.com/1jehuang/agentgrep.git", tag = "v0.1.7" } [features] default = ["pdf", "bedrock"] diff --git a/crates/jcode-app-core/src/tool/agentgrep/args.rs b/crates/jcode-app-core/src/tool/agentgrep/args.rs index 0f2f9440eb7..e2e78b64ff5 100644 --- a/crates/jcode-app-core/src/tool/agentgrep/args.rs +++ b/crates/jcode-app-core/src/tool/agentgrep/args.rs @@ -61,6 +61,8 @@ pub(super) fn build_grep_args(params: &AgentGrepInput, ctx: &ToolContext) -> Res paths_only: params.paths_only.unwrap_or(false), hidden: params.hidden.unwrap_or(false), no_ignore: params.no_ignore.unwrap_or(false), + // Keep following symlinks (pre-v0.1.7 behavior); the guardrail is opt-in. + no_follow: false, path: scope.root, glob: scope.glob, }) @@ -93,6 +95,8 @@ pub(super) fn build_find_args(params: &AgentGrepInput, ctx: &ToolContext) -> Res max_files: params.max_files.unwrap_or(10), hidden: params.hidden.unwrap_or(false), no_ignore: params.no_ignore.unwrap_or(false), + // Keep following symlinks (pre-v0.1.7 behavior); the guardrail is opt-in. + no_follow: false, path: scope.root, glob: scope.glob, }) diff --git a/crates/jcode-app-core/src/tool/agentgrep_tests.rs b/crates/jcode-app-core/src/tool/agentgrep_tests.rs index 199d3515994..ed80f980c8e 100644 --- a/crates/jcode-app-core/src/tool/agentgrep_tests.rs +++ b/crates/jcode-app-core/src/tool/agentgrep_tests.rs @@ -111,6 +111,7 @@ fn render_compacts_huge_grep_match_lines() { paths_only: false, hidden: false, no_ignore: false, + no_follow: false, path: None, glob: None, }; From 56392f252b1003bf6f232369b218c1b2719d8d83 Mon Sep 17 00:00:00 2001 From: Iraklis Analitis Date: Thu, 24 Sep 2026 17:44:41 -0500 Subject: [PATCH 0016/1618] test(copilot): keep save_github_token_creates_config_dir out of the real config The test unset JCODE_HOME so save_github_token resolved the trust entry to $HOME/.jcode/config.toml and appended a dead tempdir path on every run (126 accumulated locally). Point HOME at the tempdir and assert the config lands there. Same defect exists on origin/master. --- crates/jcode-base/src/auth/copilot_auth_tests.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/jcode-base/src/auth/copilot_auth_tests.rs b/crates/jcode-base/src/auth/copilot_auth_tests.rs index d40f4facafb..105aaa950d4 100644 --- a/crates/jcode-base/src/auth/copilot_auth_tests.rs +++ b/crates/jcode-base/src/auth/copilot_auth_tests.rs @@ -204,8 +204,13 @@ fn save_github_token_creates_config_dir() -> Result<()> { let config_dir = dir.path().join("github-copilot"); let prev_jcode_home = std::env::var_os("JCODE_HOME"); let prev_xdg_config_home = std::env::var_os("XDG_CONFIG_HOME"); + let prev_home = std::env::var_os("HOME"); + // With JCODE_HOME unset, save_github_token's trust entry goes to + // $HOME/.jcode/config.toml. Point HOME at the tempdir so the test never + // writes into the developer's real config. crate::env::remove_var("JCODE_HOME"); + crate::env::set_var("HOME", dir.path().join("home")); crate::env::set_var( "XDG_CONFIG_HOME", dir.path() @@ -221,6 +226,13 @@ fn save_github_token_creates_config_dir() -> Result<()> { let loaded = load_token_from_json(&hosts_path)?; assert_eq!(loaded, "gho_newtoken"); + assert!(dir.path().join("home/.jcode/config.toml").exists()); + + if let Some(prev) = prev_home { + crate::env::set_var("HOME", prev); + } else { + crate::env::remove_var("HOME"); + } if let Some(prev) = prev_jcode_home { crate::env::set_var("JCODE_HOME", prev); From 6906ae3f5bae8a1ab9b60ab943604cf3030662e8 Mon Sep 17 00:00:00 2001 From: Iraklis Analitis Date: Thu, 24 Sep 2026 18:30:53 -0500 Subject: [PATCH 0017/1618] test(communicate): isolate JCODE_HOME so server tests ignore the real agents.swarm_model The in-process server spawns reuse the developer's ~/.jcode/config.toml, so a configured agents.swarm_model (for example an unauthenticated opencode-go route) failed nine spawn tests with 'Cannot spawn session on model'. Point JCODE_HOME at each test's runtime tempdir alongside JCODE_RUNTIME_DIR. --- .../src/tool/communicate_tests/assignment.rs | 12 ++++++++++++ .../src/tool/communicate_tests/end_to_end.rs | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs b/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs index 8ace3ed9fa5..a4341114aa3 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs @@ -5,6 +5,8 @@ async fn communicate_assign_task_can_spawn_fallback_agent() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); @@ -118,6 +120,8 @@ async fn communicate_assign_next_assigns_next_runnable_task() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); @@ -226,6 +230,8 @@ async fn communicate_assign_next_can_prefer_fresh_spawn_server_side() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); @@ -334,6 +340,8 @@ async fn communicate_assign_next_can_spawn_if_needed_server_side() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); @@ -424,6 +432,8 @@ async fn communicate_fill_slots_tops_up_to_concurrency_limit() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); @@ -516,6 +526,8 @@ async fn communicate_assign_task_can_prefer_fresh_spawn_over_reuse() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); diff --git a/crates/jcode-app-core/src/tool/communicate_tests/end_to_end.rs b/crates/jcode-app-core/src/tool/communicate_tests/end_to_end.rs index b45b907a19f..d142dde11a6 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests/end_to_end.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests/end_to_end.rs @@ -5,6 +5,8 @@ async fn communicate_list_and_await_members_work_end_to_end() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); // Independently created root sessions own separate swarms (83dbc36dc); @@ -136,6 +138,8 @@ async fn communicate_await_members_background_returns_immediately_and_notifies() let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); // Independently created root sessions own separate swarms (83dbc36dc); @@ -242,6 +246,8 @@ async fn communicate_run_plan_with_empty_plan_returns_inline_even_in_background_ let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); @@ -297,6 +303,8 @@ async fn communicate_status_returns_busy_snapshot_for_running_member() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); // Independently created root sessions own separate swarms (83dbc36dc); @@ -382,6 +390,8 @@ async fn communicate_spawn_reports_completion_back_to_spawner() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); @@ -460,6 +470,8 @@ async fn communicate_spawn_with_prompt_and_summary_work_end_to_end() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); @@ -562,6 +574,8 @@ async fn communicate_message_routes_as_dm_while_broadcast_targets_swarm() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Keep the developer's real config (agents.swarm_model, providers) out of spawns. + let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); // Independently created root sessions own separate swarms (83dbc36dc); From dc16df5f58ac0479c75db1818ba791a8e5a6eb6c Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Fri, 25 Sep 2026 00:58:57 +0100 Subject: [PATCH 0018/1618] test(tui): serialize and scope changelog render fixtures --- .../src/tui/app/tests/onboarding_golden.rs | 5 ++- crates/jcode-tui/src/tui/ui_header.rs | 40 +++++++++++++++++++ .../src/tui/ui_tests/basic/frame_flicker.rs | 6 +-- crates/jcode-tui/src/tui/ui_tests/prepare.rs | 5 ++- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/crates/jcode-tui/src/tui/app/tests/onboarding_golden.rs b/crates/jcode-tui/src/tui/app/tests/onboarding_golden.rs index d5fbfa72019..ead967447c8 100644 --- a/crates/jcode-tui/src/tui/app/tests/onboarding_golden.rs +++ b/crates/jcode-tui/src/tui/app/tests/onboarding_golden.rs @@ -705,10 +705,12 @@ fn onboarding_import_happy_path_images() { // ---- done (review turn): the suggested architecture review accepted ---- { + let _lock = crate::tui::ui::render_state_test_lock(); // The full frame includes the "Updates" box when the machine running // the generator has unseen changelog entries, which makes the artifact // depend on developer-local state. Force it empty for determinism. - crate::tui::ui::header::set_unseen_changelog_entries_override_for_tests(Some(Vec::new())); + let _fixture = + crate::tui::ui::header::scoped_unseen_changelog_entries_override_for_tests(Vec::new()); // The git info widget would otherwise capture the live ahead/behind and // dirty counts of the repo the generator runs in. Pin it to a clean // fixture branch. (The version label is compile-time build meta, which @@ -734,7 +736,6 @@ fn onboarding_import_happy_path_images() { app.push_display_message(DisplayMessage::user(prompt)); app.is_processing = true; write_full_frame_svg(&output_dir, "review-turn.svg", &app, width, height); - crate::tui::ui::header::set_unseen_changelog_entries_override_for_tests(None); } crate::env::remove_var("OPENROUTER_API_KEY"); crate::auth::AuthStatus::invalidate_cached_status(); diff --git a/crates/jcode-tui/src/tui/ui_header.rs b/crates/jcode-tui/src/tui/ui_header.rs index 2a3120597de..5f86c1b7ea3 100644 --- a/crates/jcode-tui/src/tui/ui_header.rs +++ b/crates/jcode-tui/src/tui/ui_header.rs @@ -36,6 +36,26 @@ pub(crate) fn set_unseen_changelog_entries_override_for_tests(entries: Option, +) -> ChangelogEntriesOverrideGuard { + set_unseen_changelog_entries_override_for_tests(Some(entries)); + ChangelogEntriesOverrideGuard +} + +#[cfg(test)] +impl Drop for ChangelogEntriesOverrideGuard { + fn drop(&mut self) { + set_unseen_changelog_entries_override_for_tests(None); + } +} + pub(crate) fn capitalize(s: &str) -> String { let mut chars = s.chars(); match chars.next() { @@ -1015,6 +1035,26 @@ mod tests { use std::sync::Arc; use std::sync::OnceLock; + #[test] + fn changelog_override_is_cleared_after_a_render_test_panics() { + let _lock = crate::tui::ui::render_state_test_lock(); + let panic = std::panic::catch_unwind(|| { + let _fixture = scoped_unseen_changelog_entries_override_for_tests(vec![ + "temporary changelog entry".to_owned(), + ]); + assert_eq!(unseen_changelog_entries(), ["temporary changelog entry"]); + panic!("injected render failure"); + }); + assert!(panic.is_err()); + assert!( + unseen_changelog_entries_override() + .lock() + .unwrap() + .is_none(), + "a failed render test must not leak its changelog fixture" + ); + } + struct MockProvider; #[async_trait] diff --git a/crates/jcode-tui/src/tui/ui_tests/basic/frame_flicker.rs b/crates/jcode-tui/src/tui/ui_tests/basic/frame_flicker.rs index 82115006469..347b2c644e4 100644 --- a/crates/jcode-tui/src/tui/ui_tests/basic/frame_flicker.rs +++ b/crates/jcode-tui/src/tui/ui_tests/basic/frame_flicker.rs @@ -519,13 +519,13 @@ fn test_changelog_overlay_repeated_renders_are_stable() { #[test] fn test_updates_header_repeated_renders_stay_stable_near_scrollbar_threshold() { let _lock = viewport_snapshot_test_lock(); - super::header::set_unseen_changelog_entries_override_for_tests(Some(vec![ + let _fixture = super::header::scoped_unseen_changelog_entries_override_for_tests(vec![ "Update one".to_string(), "Update two".to_string(), "Update three".to_string(), "Update four".to_string(), "Update five".to_string(), - ])); + ]); let state = TestState { display_messages: vec![DisplayMessage::assistant("ok")], @@ -554,8 +554,6 @@ fn test_updates_header_repeated_renders_stay_stable_near_scrollbar_threshold() { } } - super::header::set_unseen_changelog_entries_override_for_tests(None); - assert!( unstable.is_empty(), "expected updates header to render stably near scrollbar threshold, found unstable sizes: {unstable:#?}" diff --git a/crates/jcode-tui/src/tui/ui_tests/prepare.rs b/crates/jcode-tui/src/tui/ui_tests/prepare.rs index 92332573dff..6e468c93235 100644 --- a/crates/jcode-tui/src/tui/ui_tests/prepare.rs +++ b/crates/jcode-tui/src/tui/ui_tests/prepare.rs @@ -529,6 +529,7 @@ fn test_prepare_messages_centered_live_batch_rows_keep_dedicated_padding_span() #[test] fn test_prepare_messages_shows_live_batch_progress_in_chat_history() { + let _lock = viewport_snapshot_test_lock(); let state = TestState { display_messages: vec![DisplayMessage { role: "user".to_string(), @@ -582,9 +583,9 @@ fn test_prepare_messages_shows_live_batch_progress_in_chat_history() { }; // The Updates box lists recent commit subjects ("… PR #1440"); they trip the #N check below. - crate::tui::ui::header::set_unseen_changelog_entries_override_for_tests(Some(Vec::new())); + let _fixture = + crate::tui::ui::header::scoped_unseen_changelog_entries_override_for_tests(Vec::new()); let prepared = prepare::prepare_messages(&state, 100, 30); - crate::tui::ui::header::set_unseen_changelog_entries_override_for_tests(None); let rendered: Vec = prepared .materialize_all_lines() .iter() From 5c74ffe13560d3d07d807ea22e3dc9125e777ef1 Mon Sep 17 00:00:00 2001 From: Dr Kennedy Umege <205099287+Kenmege@users.noreply.github.com> Date: Fri, 25 Sep 2026 01:17:02 +0100 Subject: [PATCH 0019/1618] test(id): force cursor interference deterministically --- crates/jcode-core/src/id.rs | 59 +++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/crates/jcode-core/src/id.rs b/crates/jcode-core/src/id.rs index e30ca2895f6..52f4b957e99 100644 --- a/crates/jcode-core/src/id.rs +++ b/crates/jcode-core/src/id.rs @@ -264,11 +264,26 @@ pub fn new_memorable_session_id_avoiding(used_names: &HashSet) -> (Strin let ts = Utc::now().timestamp_millis(); let rand: u64 = rand::random(); - let cursor = session_name_cursor(); + let word = select_available_session_name(used_names, session_name_cursor(), |_| {}); + + let short_name = word.to_string(); + let full_id = format!("session_{}_{ts}_{rand:016x}", word); + + (full_id, short_name) +} + +fn select_available_session_name( + used_names: &HashSet, + cursor: &AtomicUsize, + mut after_candidate: impl FnMut(usize), +) -> &'static str { let word = (0..SESSION_NAMES.len()) .find_map(|_| { let idx = cursor.fetch_add(1, Ordering::Relaxed) % SESSION_NAMES.len(); let (word, _) = SESSION_NAMES[idx]; + // A competing allocator can advance the cursor before our next check. + // The no-op production callback lets the regression test force that interleaving. + after_candidate(idx); (!used_names.contains(word)).then_some(word) }) // Concurrent creators advance the shared cursor between our steps, so the loop above can @@ -284,10 +299,7 @@ pub fn new_memorable_session_id_avoiding(used_names: &HashSet) -> (Strin SESSION_NAMES[idx].0 }); - let short_name = word.to_string(); - let full_id = format!("session_{}_{ts}_{rand:016x}", word); - - (full_id, short_name) + word } /// Try to extract the memorable name from a session ID @@ -445,34 +457,25 @@ mod tests { #[test] fn avoiding_allocator_finds_the_last_free_identity_while_others_advance_the_cursor() { - let free = SESSION_NAMES[SESSION_NAMES.len() / 2].0; + let free_index = SESSION_NAMES.len() / 2; + let free = SESSION_NAMES[free_index].0; let used: HashSet = SESSION_NAMES .iter() .map(|(word, _)| word.to_string()) .filter(|word| word != free) .collect(); - let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); - let noise: Vec<_> = (0..4) - .map(|_| { - let stop = stop.clone(); - std::thread::spawn(move || { - while !stop.load(Ordering::Relaxed) { - let _ = new_memorable_session_id(); - } - }) - }) - .collect(); - let names: Vec = (0..200) - .map(|_| new_memorable_session_id_avoiding(&used).1) - .collect(); - stop.store(true, Ordering::Relaxed); - for thread in noise { - let _ = thread.join(); - } - assert!( - names.iter().all(|name| name == free), - "reused an occupied identity while one was free" - ); + let cursor = AtomicUsize::new(0); + let mut advanced = false; + let name = select_available_session_name(&used, &cursor, |index| { + if index + 1 == free_index && !advanced { + // Simulate another creator consuming the sole free candidate + // between this creator's candidate checks, independent of CPU scheduling. + cursor.fetch_add(1, Ordering::Relaxed); + advanced = true; + } + }); + assert!(advanced, "the competing cursor advance must occur"); + assert_eq!(name, free, "reused an occupied identity while one was free"); } /// Returns true for emoji that commonly fail to render as a single glyph on From 29161388ffed58a873b141ef016ad336e9421447 Mon Sep 17 00:00:00 2001 From: Iraklis Analitis Date: Sat, 26 Sep 2026 02:55:19 -0500 Subject: [PATCH 0020/1618] test(copilot): isolate trust config via JCODE_HOME and restore env on drop HOME does not redirect the home dir on Windows, so the save path now runs under JCODE_HOME in a tempdir. XDG resolution is checked as a pure path computation, and env vars are restored by a Drop guard so a failed assertion cannot leak state into later tests. --- .../jcode-base/src/auth/copilot_auth_tests.rs | 78 +++++++++++-------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/crates/jcode-base/src/auth/copilot_auth_tests.rs b/crates/jcode-base/src/auth/copilot_auth_tests.rs index 105aaa950d4..796c0343ec2 100644 --- a/crates/jcode-base/src/auth/copilot_auth_tests.rs +++ b/crates/jcode-base/src/auth/copilot_auth_tests.rs @@ -197,54 +197,66 @@ fn save_and_load_github_token() -> Result<()> { Ok(()) } +/// Restores an environment variable on drop, so a failed assertion cannot +/// leave later tests pointed at a removed temp directory. +struct EnvRestore { + key: &'static str, + previous: Option, +} + +impl EnvRestore { + fn capture(key: &'static str) -> Self { + Self { + key, + previous: std::env::var_os(key), + } + } +} + +impl Drop for EnvRestore { + fn drop(&mut self) { + match &self.previous { + Some(value) => crate::env::set_var(self.key, value), + None => crate::env::remove_var(self.key), + } + } +} + #[test] fn save_github_token_creates_config_dir() -> Result<()> { let _guard = crate::storage::lock_test_env(); + let _restore_jcode_home = EnvRestore::capture("JCODE_HOME"); + let _restore_xdg = EnvRestore::capture("XDG_CONFIG_HOME"); let dir = TempDir::new().map_err(|e| anyhow!(e))?; - let config_dir = dir.path().join("github-copilot"); - let prev_jcode_home = std::env::var_os("JCODE_HOME"); - let prev_xdg_config_home = std::env::var_os("XDG_CONFIG_HOME"); - let prev_home = std::env::var_os("HOME"); - // With JCODE_HOME unset, save_github_token's trust entry goes to - // $HOME/.jcode/config.toml. Point HOME at the tempdir so the test never - // writes into the developer's real config. + // The XDG location is only used when JCODE_HOME is unset. Check that + // resolution as a pure path computation, without writing anything. crate::env::remove_var("JCODE_HOME"); - crate::env::set_var("HOME", dir.path().join("home")); - crate::env::set_var( - "XDG_CONFIG_HOME", - dir.path() - .to_str() - .ok_or_else(|| anyhow!("temp dir path should be valid UTF-8"))?, + crate::env::set_var("XDG_CONFIG_HOME", dir.path().join("xdg")); + assert_eq!( + legacy_copilot_config_dir(), + dir.path().join("xdg").join("github-copilot") ); + // Saving also records a trust entry in `/config.toml`. Only + // JCODE_HOME redirects that on every platform (on Windows the home dir + // ignores HOME), so the write happens entirely inside the tempdir. + let jcode_home = dir.path().join("jcode-home"); + crate::env::set_var("JCODE_HOME", &jcode_home); + let result = save_github_token("gho_newtoken", "testuser"); assert!(result.is_ok()); - let hosts_path = config_dir.join("hosts.json"); + let hosts_path = jcode_home + .join("external") + .join(".config") + .join("github-copilot") + .join("hosts.json"); assert!(hosts_path.exists()); let loaded = load_token_from_json(&hosts_path)?; assert_eq!(loaded, "gho_newtoken"); - assert!(dir.path().join("home/.jcode/config.toml").exists()); - - if let Some(prev) = prev_home { - crate::env::set_var("HOME", prev); - } else { - crate::env::remove_var("HOME"); - } - - if let Some(prev) = prev_jcode_home { - crate::env::set_var("JCODE_HOME", prev); - } else { - crate::env::remove_var("JCODE_HOME"); - } - - if let Some(prev) = prev_xdg_config_home { - crate::env::set_var("XDG_CONFIG_HOME", prev); - } else { - crate::env::remove_var("XDG_CONFIG_HOME"); - } + assert!(jcode_home.join("config.toml").exists()); Ok(()) } From 9e04dbcfd26e5576bd37299e2284da4feebea47c Mon Sep 17 00:00:00 2001 From: Iraklis Analitis Date: Sat, 26 Sep 2026 02:58:35 -0500 Subject: [PATCH 0021/1618] test(communicate): prove spawn isolation against a conflicting outer config --- .../src/tool/communicate_tests/assignment.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs b/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs index a4341114aa3..67040882e3b 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs @@ -5,10 +5,25 @@ async fn communicate_assign_task_can_spawn_fallback_agent() { let repo_dir = std::env::current_dir().expect("repo cwd"); let socket_path = runtime_dir.path().join("jcode.sock"); let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); + // Stand in for a developer config that pins spawns to an unrelated model. + // The isolation guard below must hide it, or this test fails. + let outer_home = tempfile::TempDir::new().expect("outer home tempdir"); + std::fs::write( + outer_home.path().join("config.toml"), + "[agents]\nswarm_model = \"openai-api:isolation-probe-model\"\n", + ) + .expect("write conflicting outer config"); + let _outer_home = EnvGuard::set("JCODE_HOME", outer_home.path()); // Keep the developer's real config (agents.swarm_model, providers) out of spawns. let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); + crate::config::invalidate_config_cache(); + assert_eq!( + crate::config::config().agents.swarm_model, + None, + "spawn config must come from the isolated JCODE_HOME" + ); let provider: Arc = Arc::new(DelayedTestProvider { delay: Duration::from_millis(100), From cf9370a0671a3241f6fcd053c15c912ac9c04367 Mon Sep 17 00:00:00 2001 From: Iraklis Analitis Date: Sat, 26 Sep 2026 03:18:42 -0500 Subject: [PATCH 0022/1618] test(communicate): clear JCODE_SWARM_MODEL in the isolation test The env override sets agents.swarm_model regardless of JCODE_HOME, so a developer with it exported failed the isolation assertion. --- crates/jcode-app-core/src/tool/communicate_tests.rs | 6 ++++++ .../jcode-app-core/src/tool/communicate_tests/assignment.rs | 2 ++ 2 files changed, 8 insertions(+) diff --git a/crates/jcode-app-core/src/tool/communicate_tests.rs b/crates/jcode-app-core/src/tool/communicate_tests.rs index e97ed2c3c6d..9ce5ff174f0 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests.rs @@ -1379,6 +1379,12 @@ impl EnvGuard { crate::env::set_var(key, value); Self { key, original } } + + fn remove(key: &'static str) -> Self { + let original = std::env::var_os(key); + crate::env::remove_var(key); + Self { key, original } + } } impl Drop for EnvGuard { diff --git a/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs b/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs index 67040882e3b..7f5e23f3fd9 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs @@ -16,6 +16,8 @@ async fn communicate_assign_task_can_spawn_fallback_agent() { let _outer_home = EnvGuard::set("JCODE_HOME", outer_home.path()); // Keep the developer's real config (agents.swarm_model, providers) out of spawns. let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); + // The env override would set agents.swarm_model regardless of JCODE_HOME. + let _swarm_model = EnvGuard::remove("JCODE_SWARM_MODEL"); let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); crate::config::invalidate_config_cache(); From 3dcdc80eb8db0d86a04e9d542bd74df01b64bdc0 Mon Sep 17 00:00:00 2001 From: Takumi Mori <51111242+takumi3488@users.noreply.github.com> Date: Sat, 26 Sep 2026 02:22:35 +0900 Subject: [PATCH 0023/1618] fix(mcp): fail pending requests when a stdio server exits A stdio MCP server that exits before answering `initialize` (e.g. a bridge that needs an env var and bails) left its pending request waiting out the full reply timeout. With `timeout_secs: 86400` that is 24h, and because the startup `connect_all` holds `mcp_manager.write()` the whole time, the `mcp` tool's `list` action (which takes `read()`) blocked for the same duration. - Reader task: on stdout EOF/read error, mark the handle closed and drop all pending senders under the `pending` lock; `request` checks the flag under the same lock before inserting, so it fails immediately instead of hanging. The dead server now fails connect at once, which also releases the manager write lock promptly. - Regression test: server `sh -c 'echo boom >&2; exit 1'` with `timeout_secs=86400` must fail connect within 10s. --- crates/jcode-base/src/mcp/client.rs | 37 +++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/crates/jcode-base/src/mcp/client.rs b/crates/jcode-base/src/mcp/client.rs index e78c6138a45..7793b551973 100644 --- a/crates/jcode-base/src/mcp/client.rs +++ b/crates/jcode-base/src/mcp/client.rs @@ -6,7 +6,7 @@ use serde_json::Value; use std::collections::HashMap; use std::process::Stdio; use std::sync::Arc; -use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::process::{Child, Command}; use tokio::sync::{Mutex, mpsc, oneshot}; @@ -19,6 +19,8 @@ pub struct McpHandle { pub(crate) name: String, request_id: Arc, pending: Arc>>>, + /// Set by the reader on stdout EOF: no reply can arrive, so requests fail fast. + closed: Arc, writer_tx: mpsc::Sender, server_info: Arc>>, capabilities: Arc>, @@ -48,6 +50,9 @@ impl McpHandle { let (tx, rx) = oneshot::channel(); { let mut pending = self.pending.lock().await; + if self.closed.load(Ordering::SeqCst) { + anyhow::bail!("MCP server '{}' exited (stdout closed)", self.name); + } pending.insert(id, tx); } @@ -66,7 +71,7 @@ impl McpHandle { self.name ) })? - .context("Channel closed")?; + .with_context(|| format!("MCP server '{}' exited (stdout closed)", self.name))?; if let Some(err) = &response.error { anyhow::bail!("MCP error {}: {}", err.code, err.message); @@ -231,6 +236,8 @@ impl McpClient { // Spawn reader task let pending_clone = Arc::clone(&pending); + let closed = Arc::new(AtomicBool::new(false)); + let closed_clone = Arc::clone(&closed); let reader_name = name.clone(); let mut reader = BufReader::new(stdout); tokio::spawn(async move { @@ -266,12 +273,17 @@ impl McpClient { } } } + // Server can never reply now: drop in-flight senders, fail later requests fast. + let mut pending = pending_clone.lock().await; + closed_clone.store(true, Ordering::SeqCst); + pending.clear(); }); let handle = McpHandle { name: name.clone(), request_id: Arc::new(AtomicU64::new(1)), pending, + closed, writer_tx, server_info: Arc::new(std::sync::RwLock::new(None)), capabilities: Arc::new(std::sync::RwLock::new(ServerCapabilities::default())), @@ -494,6 +506,27 @@ done } } + #[tokio::test] + async fn connect_fails_fast_when_server_exits_before_initialize() { + // A server that prints to stderr and exits before answering + // `initialize` must fail connect promptly, even with a huge + // `timeout_secs` (previously the pending request waited it out). + let config = McpServerConfig { + command: "/bin/sh".to_string(), + args: vec!["-c".to_string(), "echo boom >&2; exit 1".to_string()], + timeout_secs: Some(86_400), + ..fake_server_config() + }; + let result = tokio::time::timeout( + std::time::Duration::from_secs(10), + McpClient::connect("dead".to_string(), &config), + ) + .await + .expect("connect must not hang on a server that exited"); + let err = format!("{:#}", result.err().expect("connect must fail")); + assert!(err.contains("exited"), "unexpected error: {err}"); + } + #[tokio::test] async fn connect_in_dir_sets_subprocess_cwd() { // Issue #557: owned MCP servers must run in the session project dir. From efbfd2f7dfa9835c9ac03cb4c97897bdcb6072e6 Mon Sep 17 00:00:00 2001 From: WorkShop1 Date: Sat, 26 Sep 2026 20:36:58 +0500 Subject: [PATCH 0024/1618] openrouter: honour input modalities declared in the model catalog The OpenRouter catalog already states which modalities each model accepts under `architecture.input_modalities`, but `ModelInfo` had no field for it, so the declared value was dropped during deserialization and never reached `supports_image_input()`. On the native OpenRouter route that function falls through to `!self.supports_provider_features`, which is `false` for openrouter.ai, so every image was replaced with the clamp marker `[Image omitted: this provider/model does not support image input]` regardless of what the provider advertised. This keeps the information and uses it, rather than hardcoding another model list next to the existing DeepSeek allowlist from #1221: - `ModelInfo.input` carries the declared modalities, parsed from `architecture.input_modalities` and from the flat `input_modalities` and `input` keys that other `/v1/models` gateways use. - `supports_image_input()` consults the catalog before the provider-level fallback. The change is additive: explicit per-model config still wins, the #1221 DeepSeek allowlist is untouched, and a profile that rejects images is still rejected. - A model absent from the catalog, or one that declares only text, keeps the existing behaviour, so a missing field never becomes a capability claim. The catalog read is synchronous (`try_read`) because `supports_image_input` is a sync trait method; a busy lock falls back rather than guessing. Adding a field to a public struct is a breaking change for callers that exhaustively list its fields, so the existing literals now use `..Default::default()`. Tests: six new cases in `openrouter_input_modalities_tests`, including the negative controls that preserve the fallback and the missing-field behaviour. The positive case was verified to fail when the catalog consultation is removed, so it is load-bearing rather than decorative. Note on the test suite: `cargo test -p jcode-provider-openrouter-runtime --lib` reports 7 failures in this environment (autodetect*, named_openai_compatible_loads_api_key_from_env_file, opencode_session_header_is_sent_on_the_wire_only_to_opencode_hosts, midstream_transport_fault_emits_retry_rollback_before_replay, does_not_guess_when_multiple_saved_openai_compatible_profiles_exist). They fail identically on base commit b5a4cde7a with this change stashed, so they are not caused by it. --- crates/jcode-base/src/auth/lifecycle.rs | 2 + .../jcode-base/src/provider/catalog_routes.rs | 2 + crates/jcode-base/src/provider/tests.rs | 1 + .../jcode-base/src/provider_catalog_tests.rs | 8 + .../src/lib.rs | 4 + .../src/models_catalog_parse.rs | 28 ++++ .../src/openrouter_catalog_merge_tests.rs | 1 + .../src/openrouter_input_modalities_tests.rs | 140 ++++++++++++++++++ .../src/openrouter_provider_impl.rs | 32 ++++ crates/jcode-provider-openrouter/src/lib.rs | 7 +- 10 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 crates/jcode-provider-openrouter-runtime/src/openrouter_input_modalities_tests.rs diff --git a/crates/jcode-base/src/auth/lifecycle.rs b/crates/jcode-base/src/auth/lifecycle.rs index ceb6f29ff1a..9311cb5ed3b 100644 --- a/crates/jcode-base/src/auth/lifecycle.rs +++ b/crates/jcode-base/src/auth/lifecycle.rs @@ -2182,6 +2182,7 @@ mod tests { context_length: None, pricing: Default::default(), created: Some(1_700_000_000), + ..Default::default() }, jcode_provider_openrouter::ModelInfo { id: "qwen-3-235b-a22b-instruct-2507".to_string(), @@ -2189,6 +2190,7 @@ mod tests { context_length: None, pricing: Default::default(), created: Some(1_800_000_000), + ..Default::default() }, ], Some("https://api.cerebras.ai/v1"), diff --git a/crates/jcode-base/src/provider/catalog_routes.rs b/crates/jcode-base/src/provider/catalog_routes.rs index cdeece13279..89ad121497e 100644 --- a/crates/jcode-base/src/provider/catalog_routes.rs +++ b/crates/jcode-base/src/provider/catalog_routes.rs @@ -1312,6 +1312,7 @@ mod tests { context_length: None, pricing: jcode_provider_openrouter::ModelPricing::default(), created: None, + ..Default::default() }) .collect(), }; @@ -1645,6 +1646,7 @@ mod tests { context_length: None, pricing: jcode_provider_openrouter::ModelPricing::default(), created: None, + ..Default::default() }) .collect(), }; diff --git a/crates/jcode-base/src/provider/tests.rs b/crates/jcode-base/src/provider/tests.rs index a1dbab68744..d56a3b301d2 100644 --- a/crates/jcode-base/src/provider/tests.rs +++ b/crates/jcode-base/src/provider/tests.rs @@ -169,6 +169,7 @@ fn save_test_openrouter_model_cache(namespace: &str, source_api_base: &str, mode context_length: None, pricing: jcode_provider_openrouter::ModelPricing::default(), created: None, + ..Default::default() }) .collect(), }; diff --git a/crates/jcode-base/src/provider_catalog_tests.rs b/crates/jcode-base/src/provider_catalog_tests.rs index c2188242826..7ef62c584ea 100644 --- a/crates/jcode-base/src/provider_catalog_tests.rs +++ b/crates/jcode-base/src/provider_catalog_tests.rs @@ -250,6 +250,7 @@ fn resolved_named_profile_suggests_newest_cached_live_release() { context_length: None, pricing: Default::default(), created: Some(1_700_000_000), + ..Default::default() }, jcode_provider_openrouter::ModelInfo { id: "newer-model".to_string(), @@ -257,6 +258,7 @@ fn resolved_named_profile_suggests_newest_cached_live_release() { context_length: None, pricing: Default::default(), created: Some(1_800_000_000), + ..Default::default() }, ], Some(CEREBRAS_PROFILE.api_base), @@ -287,6 +289,7 @@ fn resolved_named_profile_skips_non_chat_models_when_picking_newest_default() { context_length: None, pricing: Default::default(), created: Some(1_700_000_000), + ..Default::default() }, jcode_provider_openrouter::ModelInfo { id: "newer-chat-model".to_string(), @@ -294,6 +297,7 @@ fn resolved_named_profile_skips_non_chat_models_when_picking_newest_default() { context_length: None, pricing: Default::default(), created: Some(1_800_000_000), + ..Default::default() }, // Newest of all, but a non-chat (TTS) model that must be skipped. jcode_provider_openrouter::ModelInfo { @@ -302,6 +306,7 @@ fn resolved_named_profile_skips_non_chat_models_when_picking_newest_default() { context_length: None, pricing: Default::default(), created: Some(1_900_000_000), + ..Default::default() }, jcode_provider_openrouter::ModelInfo { id: "whisper-large-v3".to_string(), @@ -309,6 +314,7 @@ fn resolved_named_profile_skips_non_chat_models_when_picking_newest_default() { context_length: None, pricing: Default::default(), created: Some(1_950_000_000), + ..Default::default() }, ], Some(CEREBRAS_PROFILE.api_base), @@ -1145,6 +1151,7 @@ fn newest_release_picker_prefers_strongest_tier_over_newest_cheap() { context_length: None, pricing: Default::default(), created: Some(created), + ..Default::default() }; // A heterogeneous proxy catalog (like OpenCode Zen): the NEWEST model is a @@ -1181,6 +1188,7 @@ fn newest_release_picker_uses_recency_within_a_tier() { context_length: None, pricing: Default::default(), created: Some(created), + ..Default::default() }; // All same (bare frontier) tier: recency decides. diff --git a/crates/jcode-provider-openrouter-runtime/src/lib.rs b/crates/jcode-provider-openrouter-runtime/src/lib.rs index ee44a79c9ea..2f6250fd492 100644 --- a/crates/jcode-provider-openrouter-runtime/src/lib.rs +++ b/crates/jcode-provider-openrouter-runtime/src/lib.rs @@ -2933,6 +2933,10 @@ mod openrouter_catalog_merge_tests; #[path = "openrouter_pricing_deadlock_tests.rs"] mod openrouter_pricing_deadlock_tests; +#[cfg(test)] +#[path = "openrouter_input_modalities_tests.rs"] +mod openrouter_input_modalities_tests; + #[cfg(test)] #[path = "issue_1056_tests.rs"] mod issue_1056_tests; diff --git a/crates/jcode-provider-openrouter-runtime/src/models_catalog_parse.rs b/crates/jcode-provider-openrouter-runtime/src/models_catalog_parse.rs index 62d62fd429b..141eda7400a 100644 --- a/crates/jcode-provider-openrouter-runtime/src/models_catalog_parse.rs +++ b/crates/jcode-provider-openrouter-runtime/src/models_catalog_parse.rs @@ -77,9 +77,37 @@ pub(crate) fn parse_model_info_value(value: &Value) -> Option { }), pricing: parse_model_pricing(object.get("pricing")), created: object.get("created").and_then(value_as_u64), + input: parse_input_modalities(object), }) } +/// Read the modalities a catalog entry declares for its input. +/// +/// OpenRouter nests them under `architecture.input_modalities`; other +/// gateways that speak `/v1/models` use a flat `input_modalities` or `input` +/// key. Anything else yields an empty list, which callers treat as "not +/// declared" so a missing field never becomes a capability claim. +pub(crate) fn parse_input_modalities(object: &serde_json::Map) -> Vec { + let declared = object + .get("architecture") + .and_then(Value::as_object) + .and_then(|architecture| architecture.get("input_modalities")) + .or_else(|| object.get("input_modalities")) + .or_else(|| object.get("input")); + + let Some(Value::Array(items)) = declared else { + return Vec::new(); + }; + + items + .iter() + .filter_map(Value::as_str) + .map(str::trim) + .filter(|modality| !modality.is_empty()) + .map(str::to_ascii_lowercase) + .collect() +} + pub(crate) fn first_u64_field( object: &serde_json::Map, keys: &[&str], diff --git a/crates/jcode-provider-openrouter-runtime/src/openrouter_catalog_merge_tests.rs b/crates/jcode-provider-openrouter-runtime/src/openrouter_catalog_merge_tests.rs index 66e170f1955..c12b4bb4eaa 100644 --- a/crates/jcode-provider-openrouter-runtime/src/openrouter_catalog_merge_tests.rs +++ b/crates/jcode-provider-openrouter-runtime/src/openrouter_catalog_merge_tests.rs @@ -62,6 +62,7 @@ fn named_profile_static_models_survive_live_catalog_refresh() { context_length: Some(128_000), pricing: Default::default(), created: None, + ..Default::default() }]; cache.fetched = true; cache.cached_at = Some(1); diff --git a/crates/jcode-provider-openrouter-runtime/src/openrouter_input_modalities_tests.rs b/crates/jcode-provider-openrouter-runtime/src/openrouter_input_modalities_tests.rs new file mode 100644 index 00000000000..c262854dc42 --- /dev/null +++ b/crates/jcode-provider-openrouter-runtime/src/openrouter_input_modalities_tests.rs @@ -0,0 +1,140 @@ +//! Regression tests for input modalities declared in the model catalog. +//! +//! The OpenRouter catalog states which modalities each model accepts under +//! `architecture.input_modalities`. Before this was parsed, a vision-capable +//! model on the native OpenRouter route still fell through to the per-provider +//! fallback, which is `false` when `supports_provider_features` is true, so the +//! outbound image clamp replaced the image with a text marker. + +use crate::tests::{ENV_LOCK, EnvVarGuard}; +use crate::*; + +/// The catalogue declares image support, so the model must be treated as +/// vision-capable even though the provider-level fallback says otherwise. +#[test] +fn catalog_declared_image_modality_enables_image_input() { + let _lock = ENV_LOCK.lock(); + let _key = EnvVarGuard::set("OPENROUTER_API_KEY", "test-key"); + + let provider = OpenRouterProvider::new_openrouter_api_key_runtime() + .expect("openrouter runtime should initialize with a test key"); + provider + .set_model("vendor/vision-model") + .expect("openrouter accepts any model id"); + + { + let mut cache = provider.models_cache.blocking_write(); + cache.models = vec![jcode_provider_openrouter::ModelInfo { + id: "vendor/vision-model".to_string(), + name: "vision model".to_string(), + context_length: Some(128_000), + input: vec!["text".to_string(), "image".to_string()], + ..Default::default() + }]; + cache.fetched = true; + } + + assert!( + provider.supports_image_input(), + "a model the catalog declares image-capable must not be clamped to text" + ); +} + +/// Negative control: a catalog entry that says text only must keep the existing +/// behaviour. This is the safety property, so a missing `image` entry must never +/// be read as permission. +#[test] +fn catalog_without_image_modality_keeps_legacy_behaviour() { + let _lock = ENV_LOCK.lock(); + let _key = EnvVarGuard::set("OPENROUTER_API_KEY", "test-key"); + + let provider = OpenRouterProvider::new_openrouter_api_key_runtime() + .expect("openrouter runtime should initialize with a test key"); + provider + .set_model("vendor/text-only-model") + .expect("openrouter accepts any model id"); + + { + let mut cache = provider.models_cache.blocking_write(); + cache.models = vec![jcode_provider_openrouter::ModelInfo { + id: "vendor/text-only-model".to_string(), + name: "text only model".to_string(), + context_length: Some(128_000), + input: vec!["text".to_string()], + ..Default::default() + }]; + cache.fetched = true; + } + + assert!( + !provider.supports_image_input(), + "a text-only catalog entry must not be treated as vision-capable" + ); +} + +/// A model absent from the catalog keeps the provider-level fallback. +#[test] +fn absent_catalog_entry_keeps_provider_fallback() { + let _lock = ENV_LOCK.lock(); + let _key = EnvVarGuard::set("OPENROUTER_API_KEY", "test-key"); + + let provider = OpenRouterProvider::new_openrouter_api_key_runtime() + .expect("openrouter runtime should initialize with a test key"); + provider + .set_model("vendor/never-listed") + .expect("openrouter accepts any model id"); + + assert!( + !provider.supports_image_input(), + "an unlisted model must fall back to the provider default" + ); +} + +/// The nested OpenRouter shape is what the provider actually serves. +#[test] +fn openrouter_architecture_modalities_are_parsed() { + let raw = r#"{ + "id": "vendor/vision-model", + "architecture": { "input_modalities": ["text", "image", "file"] } + }"#; + let value: serde_json::Value = serde_json::from_str(raw).expect("valid json"); + let model = crate::models_catalog_parse::parse_model_info_value(&value) + .expect("catalog entry should parse"); + + assert_eq!( + model.input, + vec!["text".to_string(), "image".to_string(), "file".to_string()], + "declared input modalities must survive parsing" + ); +} + +/// A catalog with no modality information must produce an empty list rather +/// than a guess, so absence stays absence. +#[test] +fn missing_architecture_yields_no_modalities() { + let value: serde_json::Value = + serde_json::from_str(r#"{"id": "vendor/plain"}"#).expect("valid json"); + let model = crate::models_catalog_parse::parse_model_info_value(&value) + .expect("catalog entry should parse"); + + assert!( + model.input.is_empty(), + "an entry that declares nothing must not become a capability claim" + ); +} + +/// Gateways that flatten the field are accepted too, not only the nested shape. +#[test] +fn flat_input_modalities_are_parsed() { + let value: serde_json::Value = + serde_json::from_str(r#"{"id": "m", "input_modalities": ["text", "image"]}"#) + .expect("valid json"); + let model = crate::models_catalog_parse::parse_model_info_value(&value) + .expect("catalog entry should parse"); + + assert_eq!( + model.input, + vec!["text".to_string(), "image".to_string()], + "a flat input_modalities key must be honoured" + ); +} diff --git a/crates/jcode-provider-openrouter-runtime/src/openrouter_provider_impl.rs b/crates/jcode-provider-openrouter-runtime/src/openrouter_provider_impl.rs index f0013d829ff..af6857526b3 100644 --- a/crates/jcode-provider-openrouter-runtime/src/openrouter_provider_impl.rs +++ b/crates/jcode-provider-openrouter-runtime/src/openrouter_provider_impl.rs @@ -337,6 +337,14 @@ impl Provider for OpenRouterProvider { return false; } + // The catalog already states which modalities each model accepts, so + // honour that before falling back to a per-provider guess. Without this + // a vision-capable model on the native OpenRouter route is clamped to + // text even though the provider advertised image input for it. + if self.catalog_declares_image_input(&model_id) { + return true; + } + // Direct OpenAI-compatible local providers such as Ollama and LM Studio // document image content support on /v1/chat/completions. We already // serialize image blocks using OpenAI's image_url content-part shape in @@ -887,4 +895,28 @@ impl OpenRouterProvider { // `/models` catalog refreshes (issue #579). self.supports_provider_features || self.profile_id.is_none() || self.is_user_named_profile() } + + /// Whether the fetched model catalog declares `image` as an accepted input + /// modality for this model. + /// + /// Returns false when the catalog is unavailable, still being refreshed, or + /// silent about this model, so a missing field stays missing instead of + /// being read as a capability the provider never claimed. `supports_image_input` + /// is a sync trait method, so this uses `try_read` rather than awaiting the + /// tokio lock; a busy lock simply falls back to the existing behaviour. + pub(crate) fn catalog_declares_image_input(&self, model_id: &str) -> bool { + if !self.supports_model_catalog { + return false; + } + let Ok(cache) = self.models_cache.try_read() else { + return false; + }; + cache.models.iter().any(|model| { + model.id.trim().to_ascii_lowercase() == model_id + && model + .input + .iter() + .any(|modality| modality.eq_ignore_ascii_case("image")) + }) + } } diff --git a/crates/jcode-provider-openrouter/src/lib.rs b/crates/jcode-provider-openrouter/src/lib.rs index 2066a48c4d2..51248d3842c 100644 --- a/crates/jcode-provider-openrouter/src/lib.rs +++ b/crates/jcode-provider-openrouter/src/lib.rs @@ -41,7 +41,7 @@ pub fn known_providers() -> Vec { KNOWN_PROVIDERS.iter().map(|p| (*p).to_string()).collect() } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct ModelInfo { pub id: String, #[serde(default)] @@ -51,6 +51,11 @@ pub struct ModelInfo { pub pricing: ModelPricing, #[serde(default)] pub created: Option, + /// Declared input modalities, e.g. `["text", "image"]`. Empty when the + /// catalog did not say, in which case callers keep their existing default + /// rather than inferring a capability the source never claimed. + #[serde(default)] + pub input: Vec, } #[derive(Debug, Clone, Default, Serialize, Deserialize)] From 7c1e22f837494896f90bd7e13765bff31619ce6f Mon Sep 17 00:00:00 2001 From: WorkShop1 Date: Sat, 26 Sep 2026 20:55:17 +0500 Subject: [PATCH 0025/1618] transport: probe Windows named pipes without a Tokio reactor `is_socket_path` and `remove_socket` are synchronous by contract, but both opened the pipe with `ClientOptions::new().open()`. Tokio's named-pipe client registers the pipe handle with the ambient reactor, so calling either from a thread that has no runtime context panics: ``` thread '' panicked at tokio-1.49.0/src/net/windows/named_pipe.rs:1005 there is no reactor running, must be called from the context of a Tokio 1.x runtime ``` Jcode Desktop drives the SDK from plain `std::thread`s, so its bridge processes take this path and abort. The panic happens on the `open` call itself, before any success or failure is known, so it is not specific to a missing endpoint. Both probes now go through a small `probe_pipe` helper built on `std::fs::OpenOptions`, the same approach `SyncStream::connect` already uses. `ERROR_PIPE_BUSY` still maps to "a server is listening", so the busy-pipe behaviour is unchanged. Test: `sync_pipe_probes_do_not_panic_without_a_tokio_reactor` binds a real listener inside a `#[tokio::test]` runtime, then probes it from a `std::thread` that deliberately has no runtime. That split is the situation in the bug: the server owns a runtime, the synchronous probes do not. The test is load-bearing, verified rather than assumed. Two things had to be got right for it to mean anything: - the pipe must actually exist. An earlier version probed a path that was never bound, and it passed with and without this fix, because the open fails before tokio reaches the reactor registration; - the listener must be bound under a runtime. An earlier version used a plain `#[test]`, which then panicked inside `Listener::bind` instead. With `probe_pipe` reverted to `ClientOptions::open`, the spawned thread panics with the message above and this test fails; the other six tests in the crate pass in both states, so it isolates this defect. --- crates/jcode-transport/src/windows.rs | 61 ++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/crates/jcode-transport/src/windows.rs b/crates/jcode-transport/src/windows.rs index 37df039456f..051f0c2146b 100644 --- a/crates/jcode-transport/src/windows.rs +++ b/crates/jcode-transport/src/windows.rs @@ -340,7 +340,7 @@ impl io::Write for SyncStream { pub fn is_socket_path(path: &Path) -> bool { let pipe_name = path_to_pipe_name(path); - match ClientOptions::new().open(&pipe_name) { + match probe_pipe(&pipe_name) { Ok(_) => true, Err(error) if error.raw_os_error() @@ -357,7 +357,7 @@ pub fn is_socket_path(path: &Path) -> bool { pub fn remove_socket(path: &Path) { let pipe_name = path_to_pipe_name(path); - if ClientOptions::new().open(&pipe_name).is_ok() { + if probe_pipe(&pipe_name).is_ok() { eprintln!( "[windows] Named pipe {} still open, will be replaced by new server", pipe_name @@ -365,6 +365,22 @@ pub fn remove_socket(path: &Path) { } } +/// Probe whether a named pipe is currently reachable, without a tokio reactor. +/// +/// `ClientOptions::open` registers the pipe handle with the ambient tokio +/// reactor, so it panics with "there is no reactor running" when called from a +/// thread that has no runtime context. The two probes above are synchronous by +/// contract and are reachable from plain `std::thread` callers, so they must +/// not touch the tokio client API. `SyncStream::connect` already opens pipes +/// with `std::fs`, which is the same approach. +fn probe_pipe(pipe_name: &str) -> io::Result<()> { + std::fs::OpenOptions::new() + .read(true) + .write(true) + .open(pipe_name) + .map(|_handle| ()) +} + pub fn stream_pair() -> io::Result<(Stream, Stream)> { Stream::pair() } @@ -378,6 +394,47 @@ mod tests { static BUSY_PIPE_TEST_COUNTER: AtomicU64 = AtomicU64::new(0); + /// Jcode Desktop drives the SDK from plain `std::thread`s, where tokio's + /// named-pipe client panics with "there is no reactor running". The two + /// synchronous probes must therefore stay off the tokio client API. + /// + /// The pipe has to be a real, bound one: an absent pipe makes the open fail + /// before tokio reaches the reactor registration, so probing a missing + /// endpoint would pass with or without the fix and prove nothing. + /// + /// The test itself is a `#[tokio::test]` so that `Listener::bind` has a + /// runtime, while the `std::thread` doing the probe deliberately has none. + /// That split is the situation this bug is about: the server owns a runtime, + /// the synchronous probes are called from plain threads that do not. + #[tokio::test] + async fn sync_pipe_probes_do_not_panic_without_a_tokio_reactor() { + let path = std::env::temp_dir().join(format!( + "jcode-plain-thread-probe-{}-{}.sock", + std::process::id(), + BUSY_PIPE_TEST_COUNTER.fetch_add(1, Ordering::Relaxed) + )); + let _listener = Listener::bind(&path).expect("bind named pipe"); + + let probe = path.clone(); + let joined = std::thread::spawn(move || { + let reachable = is_socket_path(&probe); + remove_socket(&probe); + reachable + }) + .join(); + + assert!( + joined.is_ok(), + "probing a live pipe on a plain thread panicked: {:?}", + joined.err() + ); + assert_eq!( + joined.expect("thread joined"), + true, + "a bound named pipe must be reported as a live endpoint" + ); + } + /// The TypeScript SDK derives this name independently, so the two must /// agree exactly or a Windows client dials a pipe nobody is listening on. /// Pinning literal values here gives that duplicate a fixed contract to From 7f6ca8d1a1f7446f9706cecc66d7b829cb0cb1a6 Mon Sep 17 00:00:00 2001 From: WorkShop1 Date: Sat, 26 Sep 2026 21:14:31 +0500 Subject: [PATCH 0026/1618] openrouter: consult the persisted catalog for image support Reported as P1 on #1523: `catalog_declares_image_input` read only `models_cache`, which the constructor initialises empty, so the first request after startup still clamped images even though the catalog was already on disk from the previous run. The fix only took effect once a refresh had populated memory. Memory still decides whenever it holds an opinion about the model, so a freshly fetched catalog is never overruled by a stale disk copy. Only when memory is silent - cold start, a refresh in flight, or the lock held - does the persisted catalog answer. Both branches match the model id before looking at its modalities. An earlier version of the disk branch used `any(declares_image_input)` over every entry, which asks "does any model declare image" rather than "does this one", and would have marked every model vision-capable on a host whose catalog lists hundreds of image-capable models. The existing negative control caught it. New test: `cold_start_uses_the_persisted_catalog_for_image_support`. It writes the disk catalog, asserts it loads, asserts the in-memory catalog is empty, and then requires the declaration to be honoured. Both setup assertions are there so the test cannot pass vacuously. With the disk fallback disabled it is the only one of the seven that fails, so it isolates this defect rather than noticing that something changed. --- .../src/openrouter_input_modalities_tests.rs | 68 +++++++++++++++++++ .../src/openrouter_provider_impl.rs | 53 ++++++++++----- 2 files changed, 105 insertions(+), 16 deletions(-) diff --git a/crates/jcode-provider-openrouter-runtime/src/openrouter_input_modalities_tests.rs b/crates/jcode-provider-openrouter-runtime/src/openrouter_input_modalities_tests.rs index c262854dc42..3a27ea41b56 100644 --- a/crates/jcode-provider-openrouter-runtime/src/openrouter_input_modalities_tests.rs +++ b/crates/jcode-provider-openrouter-runtime/src/openrouter_input_modalities_tests.rs @@ -138,3 +138,71 @@ fn flat_input_modalities_are_parsed() { "a flat input_modalities key must be honoured" ); } + +/// Cold start: the in-memory catalog is empty and the catalog is only on disk. +/// +/// The in-memory cache is initialised empty and the catalog is normally already +/// persisted from the previous run, so this is the state of the very first +/// request after startup. An earlier version of the fix consulted memory only, +/// which meant the first request still clamped images even though the disk +/// catalog declared image input, and the defect only showed up after a refresh +/// had populated memory. +#[test] +fn cold_start_uses_the_persisted_catalog_for_image_support() { + let _lock = ENV_LOCK.lock(); + let temp = tempfile::tempdir().expect("temp jcode home"); + let _home = EnvVarGuard::set("JCODE_HOME", temp.path().to_str().expect("utf8 path")); + let _namespace = EnvVarGuard::remove("JCODE_OPENROUTER_CACHE_NAMESPACE"); + let _key = EnvVarGuard::set("TEST_COLD_START_KEY", "test-key"); + + let api_base = "https://coldstart.models.test/v1"; + let profile = jcode_base::config::NamedProviderConfig { + base_url: api_base.to_string(), + api_key_env: Some("TEST_COLD_START_KEY".to_string()), + model_catalog: true, + ..Default::default() + }; + let provider = OpenRouterProvider::new_named_openai_compatible("coldstart", &profile) + .expect("named profile should initialize"); + + let cache_dir = temp.path().join("cache"); + std::fs::create_dir_all(&cache_dir).expect("create cache dir"); + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("clock") + .as_secs(); + // The persisted shape is the serialized ModelInfo, so the declared + // modalities live under `input` rather than the API's `architecture` wrapper. + let cache = serde_json::json!({ + "cached_at": now, + "source_api_base": api_base, + "models": [{ + "id": "vendor/cold-vision", + "name": "cold vision", + "context_length": null, + "input": ["text", "image"], + }], + }); + std::fs::write( + cache_dir.join("coldstart_models.json"), + serde_json::to_string(&cache).expect("serialize cache"), + ) + .expect("write cache"); + + assert!( + provider.load_usable_model_disk_cache_entry().is_some(), + "test setup: the disk cache must load, or this test would pass vacuously" + ); + { + let memory = provider.models_cache.blocking_read(); + assert!( + memory.models.is_empty(), + "test setup: the in-memory catalog must start cold" + ); + } + + assert!( + provider.catalog_declares_image_input("vendor/cold-vision"), + "a cold provider must still honour the persisted image declaration" + ); +} diff --git a/crates/jcode-provider-openrouter-runtime/src/openrouter_provider_impl.rs b/crates/jcode-provider-openrouter-runtime/src/openrouter_provider_impl.rs index af6857526b3..2042226350d 100644 --- a/crates/jcode-provider-openrouter-runtime/src/openrouter_provider_impl.rs +++ b/crates/jcode-provider-openrouter-runtime/src/openrouter_provider_impl.rs @@ -896,27 +896,48 @@ impl OpenRouterProvider { self.supports_provider_features || self.profile_id.is_none() || self.is_user_named_profile() } - /// Whether the fetched model catalog declares `image` as an accepted input - /// modality for this model. + /// Whether the model catalog declares `image` as an accepted input modality + /// for this model. /// - /// Returns false when the catalog is unavailable, still being refreshed, or - /// silent about this model, so a missing field stays missing instead of - /// being read as a capability the provider never claimed. `supports_image_input` - /// is a sync trait method, so this uses `try_read` rather than awaiting the - /// tokio lock; a busy lock simply falls back to the existing behaviour. + /// Memory decides whenever it holds an opinion about the model, so a freshly + /// fetched catalog is never overruled by a stale copy on disk. Only when + /// memory is silent does the persisted catalog answer, and that fallback is + /// what makes the first request after startup behave: the in-memory cache is + /// initialised empty while the catalog is normally already on disk from the + /// previous run, so memory alone would keep clamping images until the first + /// refresh completed. + /// + /// `supports_image_input` is a sync trait method, so the in-memory read uses + /// `try_read` rather than awaiting the tokio lock; a busy lock defers to the + /// disk copy instead of blocking. pub(crate) fn catalog_declares_image_input(&self, model_id: &str) -> bool { if !self.supports_model_catalog { return false; } - let Ok(cache) = self.models_cache.try_read() else { - return false; - }; - cache.models.iter().any(|model| { - model.id.trim().to_ascii_lowercase() == model_id - && model - .input + if let Ok(cache) = self.models_cache.try_read() { + if let Some(model) = cache + .models + .iter() + .find(|model| model.id.trim().eq_ignore_ascii_case(model_id)) + { + return declares_image_input(model); + } + } + self.load_usable_model_disk_cache_entry() + .is_some_and(|entry| { + entry + .models .iter() - .any(|modality| modality.eq_ignore_ascii_case("image")) - }) + .find(|model| model.id.trim().eq_ignore_ascii_case(model_id)) + .is_some_and(declares_image_input) + }) } } + +/// Whether one catalog entry declares `image` as an accepted input modality. +fn declares_image_input(model: &jcode_provider_openrouter::ModelInfo) -> bool { + model + .input + .iter() + .any(|modality| modality.eq_ignore_ascii_case("image")) +} From 6c83e653b85d04e7098e60438ccbf3f4ec3d99e0 Mon Sep 17 00:00:00 2001 From: WorkShop1 Date: Sat, 26 Sep 2026 21:15:51 +0500 Subject: [PATCH 0027/1618] transport: probe remove_socket on its own bound pipe Reported as P2 on #1525: the regression test called `is_socket_path` and then `remove_socket` on the same single-instance listener. Probing consumes the only available instance of a Windows named pipe, so the second open can return ERROR_PIPE_BUSY and return an error without ever reaching the code under test. A reverted `ClientOptions::open` in `remove_socket` could therefore have slipped through while the test still passed. The test now binds a second, separate pipe for `remove_socket`, so each probe hits a freshly available instance and each is independently covered. Load-bearing proof repeated after the change, since the previous proof was for the previous version of the test: with `probe_pipe` reverted to `ClientOptions::open`, `sync_pipe_probes_do_not_panic_without_a_tokio_reactor` is the only one of the seven that fails, and it fails with the reactor panic in the spawned thread. --- crates/jcode-transport/src/windows.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/jcode-transport/src/windows.rs b/crates/jcode-transport/src/windows.rs index 051f0c2146b..fe416663730 100644 --- a/crates/jcode-transport/src/windows.rs +++ b/crates/jcode-transport/src/windows.rs @@ -415,10 +415,22 @@ mod tests { )); let _listener = Listener::bind(&path).expect("bind named pipe"); + // A second, separately bound pipe for `remove_socket`. Probing consumes + // the only available instance of a pipe, so reusing `path` would let the + // second probe return ERROR_PIPE_BUSY and never reach the code under + // test, which is exactly the coverage a single shared pipe left out. + let remove_path = std::env::temp_dir().join(format!( + "jcode-plain-thread-remove-{}-{}.sock", + std::process::id(), + BUSY_PIPE_TEST_COUNTER.fetch_add(1, Ordering::Relaxed) + )); + let _remove_listener = Listener::bind(&remove_path).expect("bind second named pipe"); + let probe = path.clone(); + let remove_probe = remove_path.clone(); let joined = std::thread::spawn(move || { let reachable = is_socket_path(&probe); - remove_socket(&probe); + remove_socket(&remove_probe); reachable }) .join(); From e372fcb8a68919f7b086f423665828037c803729 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 26 Sep 2026 16:05:38 -0700 Subject: [PATCH 0028/1618] applet tool: teach select options as {value,label} and host.send_prompt{prompt}; test documented shapes validate --- crates/jcode-app-core/src/tool/applet.rs | 4 +-- .../jcode-app-core/src/tool/applet_tests.rs | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/crates/jcode-app-core/src/tool/applet.rs b/crates/jcode-app-core/src/tool/applet.rs index 60431707637..52dfe6e30eb 100644 --- a/crates/jcode-app-core/src/tool/applet.rs +++ b/crates/jcode-app-core/src/tool/applet.rs @@ -197,10 +197,10 @@ impl Tool for AppletTool { "Show interactive native UI (an applet) in Jcode Desktop. By default the card replaces this tool call in the transcript. \ View is a tree of nodes, each {\"type\":...}. Layout: stack{direction:vertical|horizontal,gap,padding,align,children}, grid{min_column_width,children}, scroll{max_height,children}, card{title,children} (don't nest), tabs{bind,tabs:[{id,label,children}]}, spacer, divider. \ Content: text{text,style:body|title|heading|caption|mono,tone,max_lines}, markdown{text}, code{text,language}, image{source:{url|data|asset},alt,aspect_ratio}, icon{name}, key_value{rows:[{key,value}]}, table{columns,rows}, progress{value 0-1,label}, empty{title,detail}, error{message,retry}. \ -Controls (always pills): button{label,variant:primary|secondary|compact|danger,on_press}, chip{label,on_press}, toggle{label,bind}, input{bind,placeholder,multiline,on_submit}, select{bind,options:[{id,label}]}, list{children:[list_item{title,subtitle,meta,badges,on_press}]}. Escape hatch: html{source,height}. \ +Controls (always pills): button{label,variant:primary|secondary|compact|danger,on_press}, chip{label,on_press}, toggle{label,bind}, input{bind,placeholder,multiline,on_submit}, select{bind,options:[{value,label}]}, list{children:[list_item{title,subtitle,meta,badges,on_press}]}. Escape hatch: html{source,height}. \ Spacing tokens none|xs|sm|md|lg|xl; tones default|dim|accent|success|warning|danger. Inputs/toggles/selects/tabs bind to keys in `state`. \ Actions are {action,args}. Custom names come back to you as an `[applet action]` message with the current state (or as this tool's output with wait=true). \ -Host actions run locally: host.open_url{url}, host.copy{text}, host.send_prompt{text}, host.close, host.set_state{...}. \ +Host actions run locally: host.open_url{url}, host.copy{text}, host.send_prompt{prompt}, host.start_chat{prompt}, host.close, host.set_state{key,value}. \ Use update to replace the document, patch for small changes (ops on /view, /state, /title), move to change placement, close to remove." } diff --git a/crates/jcode-app-core/src/tool/applet_tests.rs b/crates/jcode-app-core/src/tool/applet_tests.rs index 86bb7c9a114..0870aee2219 100644 --- a/crates/jcode-app-core/src/tool/applet_tests.rs +++ b/crates/jcode-app-core/src/tool/applet_tests.rs @@ -147,3 +147,33 @@ fn action_message_is_compact() { "[applet action] instance `chart-3f2a` (\"Title\"): `select` args {\"id\":2}\nstate: {\"q\":\"x\"}" ); } + +/// The description teaches the agent field names. Documents built exactly as +/// described must validate, or every agent-built control of that kind fails. +#[test] +fn documented_shapes_validate() { + let description = AppletTool::new().description().to_string(); + assert!(description.contains("select{bind,options:[{value,label}]}")); + assert!(description.contains("host.send_prompt{prompt}")); + let view = json!({"type":"stack","children":[ + {"type":"select","bind":"env","options":[{"value":"a","label":"A"}]}, + {"type":"tabs","bind":"t","tabs":[{"id":"x","label":"X","children":[]}]}, + {"type":"key_value","rows":[{"key":"k","value":"v"}]}, + {"type":"button","label":"Go","variant":"danger","on_press":{"action":"host.send_prompt","args":{"prompt":"hi"}}}, + {"type":"toggle","label":"T","bind":"on"}, + {"type":"input","bind":"q","placeholder":"p","multiline":true}, + {"type":"list","children":[{"type":"list_item","title":"a","subtitle":"b","meta":"c","badges":["d"],"on_press":{"action":"open"}}]}, + {"type":"progress","value":0.5,"label":"half"}, + {"type":"text","text":"t","style":"caption","tone":"dim","max_lines":2} + ]}); + let document: jcode_applet_types::Document = serde_json::from_value( + json!({"revision":1,"title":"T","state":{"env":"a"},"view":view}), + ) + .expect("documented shapes deserialize"); + jcode_applet_types::validate_document( + &document, + &jcode_applet_types::agent::manifest(), + &Default::default(), + ) + .expect("documented shapes validate"); +} From 40bf41971a4b0e01a9b174408fcaf886180f709d Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Fri, 25 Sep 2026 23:58:17 -0700 Subject: [PATCH 0029/1618] voice: built-in voice input for the CLI, like Jcode Desktop Ctrl+Space (or /voice) records, press again to send, Esc cancels. On terminals that report key releases, hold and let go to send. Audio streams to Nari with a live transcript and level meter in the status line. The final text is sent wrapped in tags (rendered with a mic marker) and any typed draft is kept. Terminal builds do not link cpal, so NariRecording now also captures through an external recorder (pw-record, parecord, arecord, rec, ffmpeg, or [dictation] recorder). Native cpal capture is unchanged. Also adds the missing applets field to History test fixtures. --- crates/jcode-base/examples/voice_recorder.rs | 62 +++ crates/jcode-base/src/bus.rs | 3 + crates/jcode-base/src/config.rs | 7 + crates/jcode-base/src/config/default_file.rs | 10 + crates/jcode-base/src/config/env_overrides.rs | 6 + crates/jcode-base/src/voice.rs | 7 +- .../jcode-base/src/voice/command_capture.rs | 515 ++++++++++++++++++ crates/jcode-base/src/voice/nari.rs | 1 - .../jcode-base/src/voice/streaming_capture.rs | 93 +++- crates/jcode-config-types/src/keybindings.rs | 8 + crates/jcode-config-types/src/lib.rs | 4 + crates/jcode-session-types/src/lib.rs | 4 + .../jcode-session-types/src/transcription.rs | 84 +++ crates/jcode-tui/src/tui/app.rs | 7 + crates/jcode-tui/src/tui/app/commands.rs | 12 + .../src/tui/app/commands_dispatch.rs | 1 + .../jcode-tui/src/tui/app/hotkey_feedback.rs | 13 + crates/jcode-tui/src/tui/app/input_help.rs | 3 + crates/jcode-tui/src/tui/app/local.rs | 6 +- crates/jcode-tui/src/tui/app/remote.rs | 27 +- .../src/tui/app/remote/input_dispatch.rs | 61 +++ crates/jcode-tui/src/tui/app/remote_tests.rs | 1 + .../src/tui/app/state_ui_input_helpers.rs | 1 + crates/jcode-tui/src/tui/app/tests.rs | 7 + .../tests/remote_events_reload_01/part_01.rs | 10 + .../tests/remote_events_reload_01/part_02.rs | 1 + .../tests/remote_events_reload_03/part_01.rs | 6 + .../tests/remote_events_reload_03/part_02.rs | 2 + .../tests/remote_startup_input_03/part_02.rs | 2 + .../jcode-tui/src/tui/app/tests/ssh_remote.rs | 1 + .../src/tui/app/tests/voice_input.rs | 115 ++++ crates/jcode-tui/src/tui/app/tui_lifecycle.rs | 7 + crates/jcode-tui/src/tui/app/tui_state.rs | 4 + crates/jcode-tui/src/tui/app/turn.rs | 15 +- crates/jcode-tui/src/tui/app/voice_input.rs | 385 +++++++++++++ crates/jcode-tui/src/tui/keybind.rs | 17 + crates/jcode-tui/src/tui/mod.rs | 8 + crates/jcode-tui/src/tui/ui_input.rs | 9 + crates/jcode-tui/src/tui/ui_overlays.rs | 4 + crates/jcode-tui/src/tui/ui_prepare.rs | 8 + 40 files changed, 1519 insertions(+), 18 deletions(-) create mode 100644 crates/jcode-base/examples/voice_recorder.rs create mode 100644 crates/jcode-base/src/voice/command_capture.rs create mode 100644 crates/jcode-session-types/src/transcription.rs create mode 100644 crates/jcode-tui/src/tui/app/tests/voice_input.rs create mode 100644 crates/jcode-tui/src/tui/app/voice_input.rs diff --git a/crates/jcode-base/examples/voice_recorder.rs b/crates/jcode-base/examples/voice_recorder.rs new file mode 100644 index 00000000000..f28b2864b7f --- /dev/null +++ b/crates/jcode-base/examples/voice_recorder.rs @@ -0,0 +1,62 @@ +//! Push-to-talk through an external recorder, the path terminal builds use. +//! Streams to Nari, so it uses credits. +//! +//! cargo run -p jcode-base --example voice_recorder -- [recorder] +//! +//! With no recorder argument, auto-detects pw-record, parecord, arecord, rec, +//! or ffmpeg, exactly like the TUI. A recorder argument is any shell command +//! printing raw mono 16 kHz s16le PCM, for example a synthetic speech file: +//! espeak-ng -w /tmp/s.wav "fix the flaky test" +//! ... -- 4 "ffmpeg -loglevel error -re -i /tmp/s.wav -ac 1 -ar 16000 -f s16le -" +//! +//! Prints stage timings and the final transcript. +use jcode_base::voice::{NariEvent, NariRecording, nari_api_key, timing}; +use std::{ + sync::{Arc, atomic::AtomicBool}, + time::{Duration, Instant}, +}; + +fn main() { + // SAFETY: single-threaded before any other thread starts. + unsafe { std::env::set_var("JCODE_VOICE_TIMING", "1") }; + let mut args = std::env::args().skip(1); + let secs: u64 = args.next().and_then(|s| s.parse().ok()).unwrap_or(4); + let recorder = args.next().unwrap_or_default(); + let key = nari_api_key().expect("Nari key not configured"); + timing::begin(); + let recording = NariRecording::start_auto(Arc::new(AtomicBool::new(false)), &key, &recorder) + .expect("start failed"); + let until = Instant::now() + Duration::from_secs(secs); + let mut peak = 0.0f32; + let mut early = None; + while Instant::now() < until && early.is_none() { + peak = peak.max(recording.audio_level()); + while let Some(event) = recording.try_event() { + match event { + NariEvent::Transcript(t) => eprintln!("partial: {t}"), + NariEvent::Finished(r) => early = Some(r), + NariEvent::Started => {} + } + } + std::thread::sleep(Duration::from_millis(20)); + } + let result = early.unwrap_or_else(|| { + timing::release(); + recording.stop(); + loop { + match recording.try_event() { + Some(NariEvent::Finished(r)) => break r, + Some(_) => {} + None => std::thread::sleep(Duration::from_millis(2)), + } + } + }); + eprintln!("peak level: {peak:.3}"); + match result { + Ok(text) => println!("{text}"), + Err(error) => { + eprintln!("error: {error}"); + std::process::exit(1); + } + } +} diff --git a/crates/jcode-base/src/bus.rs b/crates/jcode-base/src/bus.rs index 9c21a2ecec2..9f038cdc711 100644 --- a/crates/jcode-base/src/bus.rs +++ b/crates/jcode-base/src/bus.rs @@ -455,6 +455,9 @@ pub enum BusEvent { session_id: Option, message: String, }, + /// Built-in voice input has new state (meter level, partial transcript, + /// or a final result). The owning client polls its recording on receipt. + VoiceInputWake, /// Background compaction task finished (check_and_apply should be called) CompactionFinished, /// Provider's available models list may have changed diff --git a/crates/jcode-base/src/config.rs b/crates/jcode-base/src/config.rs index 747520738ba..617211dd205 100644 --- a/crates/jcode-base/src/config.rs +++ b/crates/jcode-base/src/config.rs @@ -67,6 +67,7 @@ const CONFIG_ENV_KEYS: &[&str] = &[ "JCODE_DEBUG_SOCKET", "JCODE_DEFAULT_REASONING_DISPLAY", "JCODE_DICTATION_COMMAND", + "JCODE_DICTATION_RECORDER", "JCODE_DICTATION_KEY", "JCODE_DICTATION_MODE", "JCODE_DICTATION_TIMEOUT_SECS", @@ -191,6 +192,7 @@ const CONFIG_ENV_KEYS: &[&str] = &[ "JCODE_TRUSTED_EXTERNAL_AUTH_SOURCES", "JCODE_TYPING_SCROLL_LOCK_TOGGLE_KEY", "JCODE_UPDATE_CHANNEL", + "JCODE_VOICE_INPUT_KEY", "JCODE_WEBSEARCH_ENGINE", "JCODE_WEBSEARCH_FALLBACK_ENGINES", "JCODE_WORKSPACE_DOWN_KEY", @@ -815,6 +817,10 @@ pub struct DictationConfig { /// Extra names or terms sent as recognition context to built-in voice /// transcription, added to Jcode's own product names. pub vocabulary: Vec, + /// Built-in voice input: optional shell command that records the + /// microphone and prints raw mono 16 kHz s16le PCM to stdout. Empty means + /// auto-detect (pw-record, parecord, arecord, rec, ffmpeg). + pub recorder: String, } impl Default for DictationConfig { @@ -825,6 +831,7 @@ impl Default for DictationConfig { key: "off".to_string(), timeout_secs: 90, vocabulary: Vec::new(), + recorder: String::new(), } } } diff --git a/crates/jcode-base/src/config/default_file.rs b/crates/jcode-base/src/config/default_file.rs index d91d8505c3b..f6bd08c184a 100644 --- a/crates/jcode-base/src/config/default_file.rs +++ b/crates/jcode-base/src/config/default_file.rs @@ -100,6 +100,11 @@ swarm_panel_focus = "alt+n" # Default: Cmd+B on macOS, Alt+R on Windows/Linux. Set "" to disable. # open_resume = "cmd+b" +# Built-in voice input: press to record, press again to send the transcript. +# Esc cancels. Needs a Nari API key (NARI_API_KEY or ~/.config/jcode/nari.env). +# Default: Ctrl+Space. Set "" to disable. +# voice_input = "ctrl+space" + # /resume picker Enter behavior. Options: "current-terminal" or "new-terminal". # By default Enter resumes in this terminal; Ctrl+Enter performs the alternate action. session_picker_enter = "current-terminal" @@ -127,6 +132,11 @@ timeout_secs = 90 # Jcode's own product names are always included. # vocabulary = ["Kubernetes", "Alice Zhang"] +# Microphone recorder for built-in voice input (keybindings.voice_input). +# Empty auto-detects pw-record, parecord, arecord, rec (SoX), or ffmpeg. +# A custom command must print raw mono 16 kHz signed 16-bit little-endian PCM. +# recorder = "arecord -q -t raw -f S16_LE -r 16000 -c 1 -" + [display] # Diff display mode: "off", "inline" (default), "full-inline", or "file" diff_mode = "inline" diff --git a/crates/jcode-base/src/config/env_overrides.rs b/crates/jcode-base/src/config/env_overrides.rs index 7548b9c9225..d78bd310047 100644 --- a/crates/jcode-base/src/config/env_overrides.rs +++ b/crates/jcode-base/src/config/env_overrides.rs @@ -90,6 +90,12 @@ impl Config { if let Ok(v) = std::env::var("JCODE_NEW_TERMINAL_KEY") { self.keybindings.new_terminal = v; } + if let Ok(v) = std::env::var("JCODE_VOICE_INPUT_KEY") { + self.keybindings.voice_input = v; + } + if let Ok(v) = std::env::var("JCODE_DICTATION_RECORDER") { + self.dictation.recorder = v; + } // Dictation if let Ok(v) = std::env::var("JCODE_DICTATION_COMMAND") { diff --git a/crates/jcode-base/src/voice.rs b/crates/jcode-base/src/voice.rs index a02479851a5..b925b057167 100644 --- a/crates/jcode-base/src/voice.rs +++ b/crates/jcode-base/src/voice.rs @@ -16,13 +16,16 @@ pub use nari::{ NARI_PCM_CHUNK_SAMPLES, NARI_USD_PER_AUDIO_HOUR, NariEvent, NariSession, correct_transcript, estimated_transcription_usd, nari_api_key, nari_pcm_channel, recognition_prompt, }; +mod command_capture; #[cfg(any(feature = "voice-capture", test))] mod resample; -#[cfg(feature = "voice-capture")] mod streaming_capture; -#[cfg(feature = "voice-capture")] +pub use command_capture::{RecorderCommand, detect_recorders}; pub use streaming_capture::{NariRecording, PcmRecording}; +/// Native (cpal) microphone capture is compiled in. Without it, voice input +/// records through an external recorder process. +pub const NATIVE_CAPTURE: bool = cfg!(feature = "voice-capture"); pub const MAX_AUDIO_BYTES: usize = 10 * 1024 * 1024; pub const MAX_RECORDING_DURATION: Duration = Duration::from_secs(5 * 60); pub const TRANSCRIPTION_TIMEOUT: Duration = Duration::from_secs(120); diff --git a/crates/jcode-base/src/voice/command_capture.rs b/crates/jcode-base/src/voice/command_capture.rs new file mode 100644 index 00000000000..9bd0c5075bc --- /dev/null +++ b/crates/jcode-base/src/voice/command_capture.rs @@ -0,0 +1,515 @@ +//! Microphone capture through an external recorder process. +//! +//! Terminal builds do not link a native audio stack, so they stream raw mono +//! 16 kHz signed 16-bit little-endian PCM from a recorder on stdout: PipeWire +//! `pw-record`, PulseAudio `parecord`, ALSA `arecord`, SoX `rec`, or `ffmpeg`. +//! A custom `[dictation] recorder` command may be any shell command that +//! prints that format. The recorder is started only after an explicit press, +//! and stop, cancel, drop, or the five-minute cap terminates it. +use super::{MAX_RECORDING_DURATION, PcmRecording, VoiceError}; +use std::{ + io::Read, + process::{Child, Command, Stdio}, + sync::{ + Arc, + atomic::{AtomicBool, AtomicU32, Ordering}, + mpsc, + }, + thread, + time::{Duration, Instant}, +}; + +/// 100 ms of 16 kHz audio per chunk, the same cadence as native capture. +const CHUNK_SAMPLES: usize = 1600; +const MAX_SAMPLES: usize = 16000 * MAX_RECORDING_DURATION.as_secs() as usize; +/// A recorder that prints nothing this long after starting is treated as +/// broken, so the next candidate can be tried. +const FIRST_AUDIO_TIMEOUT: Duration = Duration::from_secs(3); + +/// One way to start a recorder that prints raw s16le mono 16 kHz PCM. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RecorderCommand { + pub program: String, + pub args: Vec, +} + +impl RecorderCommand { + fn new(program: &str, args: &[&str]) -> Self { + Self { + program: program.to_string(), + args: args.iter().map(|arg| arg.to_string()).collect(), + } + } + + /// Short name for status text and errors. + pub fn label(&self) -> &str { + if self.program == SHELL { + "custom recorder" + } else { + &self.program + } + } + + fn command(&self) -> Command { + let mut command = Command::new(&self.program); + command + .args(&self.args) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()); + #[cfg(unix)] + { + use std::os::unix::process::CommandExt; + // Own process group so terminal Ctrl+C never reaches the recorder + // and a shell wrapper can be stopped together with its children. + command.process_group(0); + } + command + } +} + +#[cfg(windows)] +const SHELL: &str = "cmd"; +#[cfg(not(windows))] +const SHELL: &str = "sh"; + +/// Recorder candidates in preference order. A nonempty `custom` shell command +/// is the only candidate. Otherwise every known recorder found on `PATH`. +pub fn detect_recorders(custom: &str) -> Vec { + let custom = custom.trim(); + if !custom.is_empty() { + #[cfg(windows)] + let args = ["/C", custom]; + #[cfg(not(windows))] + let args = ["-c", custom]; + return vec![RecorderCommand::new(SHELL, &args)]; + } + known_recorders() + .into_iter() + .filter(|recorder| on_path(&recorder.program)) + .collect() +} + +fn known_recorders() -> Vec { + let mut recorders = vec![ + RecorderCommand::new( + "pw-record", + &[ + "--raw", + "--rate", + "16000", + "--channels", + "1", + "--format", + "s16", + "--latency", + "50ms", + "-", + ], + ), + RecorderCommand::new( + "parecord", + &[ + "--raw", + "--rate=16000", + "--channels=1", + "--format=s16le", + "--latency-msec=50", + ], + ), + RecorderCommand::new( + "arecord", + &[ + "-q", "-t", "raw", "-f", "S16_LE", "-r", "16000", "-c", "1", "-", + ], + ), + RecorderCommand::new( + "rec", + &[ + "-q", + "-t", + "raw", + "-r", + "16000", + "-c", + "1", + "-b", + "16", + "-e", + "signed-integer", + "-", + ], + ), + ]; + let ffmpeg_input: &[&str] = if cfg!(target_os = "macos") { + &["-f", "avfoundation", "-i", ":default"] + } else if cfg!(windows) { + &["-f", "dshow", "-i", "audio=default"] + } else { + &["-f", "pulse", "-i", "default"] + }; + let mut ffmpeg = vec!["-hide_banner", "-loglevel", "error", "-nostdin"]; + ffmpeg.extend_from_slice(ffmpeg_input); + ffmpeg.extend_from_slice(&["-ac", "1", "-ar", "16000", "-f", "s16le", "-"]); + recorders.push(RecorderCommand::new("ffmpeg", &ffmpeg)); + recorders +} + +fn on_path(program: &str) -> bool { + let Some(path) = std::env::var_os("PATH") else { + return false; + }; + std::env::split_paths(&path).any(|dir| { + let candidate = dir.join(program); + candidate.is_file() || (cfg!(windows) && candidate.with_extension("exe").is_file()) + }) +} + +/// Blocking setup, call off the UI thread. Returns once one recorder delivers +/// audio, or the last failure. +pub(super) fn start_pcm( + cancel: Arc, + recorders: Vec, +) -> Result<(PcmRecording, tokio::sync::mpsc::Receiver>), VoiceError> { + PcmRecording::start_worker(cancel, move |tx, ready, cancel, stop, level| { + let mut last = VoiceError::MicrophoneUnavailable; + for recorder in &recorders { + match run_recorder(recorder, &tx, ready, &cancel, &stop, &level) { + Err(RecorderFailure::NoAudio(error)) => last = error, + Err(RecorderFailure::Fatal(error)) => return Err(error), + Ok(()) => return Ok(()), + } + } + Err(last) + }) +} + +enum RecorderFailure { + /// Never produced audio. The next recorder may work. + NoAudio(VoiceError), + /// Failed after capture began. Audio was already committed. + Fatal(VoiceError), +} + +struct KillOnDrop(Child); +impl Drop for KillOnDrop { + fn drop(&mut self) { + terminate(&mut self.0); + } +} + +fn terminate(child: &mut Child) { + if matches!(child.try_wait(), Ok(Some(_))) { + return; + } + #[cfg(unix)] + // SAFETY: signalling our own child's process group. No memory is shared. + unsafe { + let pgid = child.id() as libc::pid_t; + libc::kill(-pgid, libc::SIGTERM); + } + let deadline = Instant::now() + Duration::from_millis(300); + while Instant::now() < deadline { + if matches!(child.try_wait(), Ok(Some(_))) { + return; + } + thread::sleep(Duration::from_millis(10)); + } + #[cfg(unix)] + // SAFETY: as above. + unsafe { + libc::kill(-(child.id() as libc::pid_t), libc::SIGKILL); + } + let _ = child.kill(); + let _ = child.wait(); +} + +fn run_recorder( + recorder: &RecorderCommand, + tx: &tokio::sync::mpsc::Sender>, + ready: &mpsc::SyncSender>, + cancel: &AtomicBool, + stop: &AtomicBool, + level: &AtomicU32, +) -> Result<(), RecorderFailure> { + use RecorderFailure::{Fatal, NoAudio}; + if cancel.load(Ordering::SeqCst) { + return Err(Fatal(VoiceError::Cancelled)); + } + super::timing::mark("recorder spawning"); + let mut child = KillOnDrop( + recorder + .command() + .spawn() + .map_err(|_| NoAudio(VoiceError::MicrophoneUnavailable))?, + ); + let mut stdout = child + .0 + .stdout + .take() + .ok_or(NoAudio(VoiceError::CaptureFailed))?; + // Reads block, so a reader thread forwards bytes and the loop below stays + // responsive to stop and cancel. + let (bytes_tx, bytes_rx) = mpsc::sync_channel::>(64); + thread::Builder::new() + .name("voice-recorder-read".into()) + .spawn(move || { + let mut buffer = [0u8; 3200]; + loop { + match stdout.read(&mut buffer) { + Ok(0) | Err(_) => break, + Ok(n) => { + if bytes_tx.send(buffer[..n].to_vec()).is_err() { + break; + } + } + } + } + }) + .map_err(|_| NoAudio(VoiceError::CaptureFailed))?; + + let mut pcm = PcmAssembler::default(); + let started = Instant::now(); + let mut signalled = false; + loop { + if cancel.load(Ordering::SeqCst) { + return Err(Fatal(VoiceError::Cancelled)); + } + if signalled && stop.load(Ordering::SeqCst) { + break; + } + if !signalled && started.elapsed() >= FIRST_AUDIO_TIMEOUT { + return Err(NoAudio(VoiceError::MicrophoneUnavailable)); + } + match bytes_rx.recv_timeout(Duration::from_millis(10)) { + Ok(bytes) => { + if !signalled { + signalled = true; + super::timing::mark("recorder delivered first audio"); + let _ = ready.send(Ok(())); + } + for chunk in pcm.push(&bytes, level) { + // Never block. Fail closed rather than silently lose audio. + if tx.try_send(chunk).is_err() { + return Err(Fatal(VoiceError::CaptureFailed)); + } + } + if pcm.samples >= MAX_SAMPLES { + break; + } + } + Err(mpsc::RecvTimeoutError::Timeout) => {} + Err(mpsc::RecvTimeoutError::Disconnected) => { + if !signalled { + return Err(NoAudio(VoiceError::MicrophoneUnavailable)); + } + // The recorder exited on its own after capturing audio. + break; + } + } + } + drop(child); + // Bytes already read before the stop are part of the utterance. + while let Ok(bytes) = bytes_rx.try_recv() { + for chunk in pcm.push(&bytes, level) { + if tx.try_send(chunk).is_err() { + return Err(Fatal(VoiceError::CaptureFailed)); + } + } + } + if let Some(chunk) = pcm.finish() { + tx.try_send(chunk) + .map_err(|_| Fatal(VoiceError::CaptureFailed))?; + } + level.store(0, Ordering::Relaxed); + Ok(()) +} + +/// Turns a raw little-endian byte stream into bounded PCM chunks and keeps the +/// latest RMS level for the meter. +#[derive(Default)] +struct PcmAssembler { + odd_byte: Option, + chunk: Vec, + samples: usize, +} + +impl PcmAssembler { + fn push(&mut self, bytes: &[u8], level: &AtomicU32) -> Vec> { + let mut out = Vec::new(); + let mut energy = 0.0f64; + let mut count = 0usize; + let mut iter = bytes.iter().copied(); + let mut next_sample = |odd: &mut Option| -> Option { + let lo = odd.take().or_else(|| iter.next())?; + match iter.next() { + Some(hi) => Some(i16::from_le_bytes([lo, hi])), + None => { + *odd = Some(lo); + None + } + } + }; + while let Some(sample) = next_sample(&mut self.odd_byte) { + if self.samples >= MAX_SAMPLES { + break; + } + let normalized = f64::from(sample) / 32768.0; + energy += normalized * normalized; + count += 1; + self.chunk.push(sample); + self.samples += 1; + if self.chunk.len() >= CHUNK_SAMPLES { + out.push(std::mem::replace( + &mut self.chunk, + Vec::with_capacity(CHUNK_SAMPLES), + )); + } + } + if count > 0 { + let rms = (energy / count as f64).sqrt() as f32; + level.store(rms.clamp(0.0, 1.0).to_bits(), Ordering::Relaxed); + } + out + } + + fn finish(&mut self) -> Option> { + (!self.chunk.is_empty()).then(|| std::mem::take(&mut self.chunk)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn level_of(level: &AtomicU32) -> f32 { + f32::from_bits(level.load(Ordering::Relaxed)) + } + + #[test] + fn assembler_joins_split_samples_and_bounds_chunks() { + let level = AtomicU32::new(0); + let mut pcm = PcmAssembler::default(); + let sample = 16384i16.to_le_bytes(); + // A sample split across two reads must not be dropped or misaligned. + assert!(pcm.push(&[sample[0]], &level).is_empty()); + let mut bytes = vec![sample[1]]; + for _ in 0..CHUNK_SAMPLES { + bytes.extend_from_slice(&sample); + } + let chunks = pcm.push(&bytes, &level); + assert_eq!(chunks.len(), 1); + assert_eq!(chunks[0].len(), CHUNK_SAMPLES); + assert!(chunks[0].iter().all(|s| *s == 16384)); + assert_eq!(pcm.finish(), Some(vec![16384])); + assert_eq!(level_of(&level), 0.5); + } + + #[test] + fn assembler_stops_at_duration_cap() { + let level = AtomicU32::new(0); + let mut pcm = PcmAssembler { + samples: MAX_SAMPLES - 2, + ..Default::default() + }; + pcm.push(&[1, 0, 2, 0, 3, 0, 4, 0], &level); + assert_eq!(pcm.samples, MAX_SAMPLES); + assert_eq!(pcm.finish(), Some(vec![1, 2])); + } + + #[test] + fn custom_recorder_is_the_only_candidate() { + let recorders = detect_recorders(" my-mic --raw "); + assert_eq!(recorders.len(), 1); + assert_eq!(recorders[0].program, SHELL); + assert_eq!(recorders[0].args.last().unwrap(), "my-mic --raw"); + assert_eq!(recorders[0].label(), "custom recorder"); + } + + #[test] + fn known_recorders_request_raw_mono_16k_s16le() { + for recorder in known_recorders() { + let args = recorder.args.join(" "); + assert!(args.contains("16000"), "{}: {args}", recorder.program); + assert!( + args.contains("s16") || args.contains("S16_LE") || args.contains("signed-integer"), + "{}: {args}", + recorder.program + ); + } + } + + #[cfg(unix)] + fn shell(script: &str) -> RecorderCommand { + RecorderCommand::new("sh", &["-c", script]) + } + + #[cfg(unix)] + fn drain(rx: &mut tokio::sync::mpsc::Receiver>) -> Vec { + let mut all = Vec::new(); + while let Ok(chunk) = rx.try_recv() { + all.extend(chunk); + } + all + } + + #[cfg(unix)] + #[test] + fn recorder_streams_until_stop_then_is_terminated() { + // Endless 0x01 0x00 bytes = sample value 1. + let (recording, mut rx) = start_pcm( + Arc::new(AtomicBool::new(false)), + vec![shell("while :; do printf '\\001\\000\\001\\000'; done")], + ) + .expect("recorder starts"); + thread::sleep(Duration::from_millis(100)); + let started = Instant::now(); + recording.finish().expect("clean stop"); + assert!(started.elapsed() < Duration::from_secs(2)); + let samples = drain(&mut rx); + assert!(!samples.is_empty()); + assert!(samples.iter().all(|s| *s == 1)); + assert!(rx.try_recv().is_err(), "EOF after stop"); + } + + #[cfg(unix)] + #[test] + fn falls_through_broken_recorders_to_a_working_one() { + let (recording, mut rx) = start_pcm( + Arc::new(AtomicBool::new(false)), + vec![ + RecorderCommand::new("jcode-definitely-missing-recorder", &[]), + shell("exit 3"), + shell("printf '\\002\\000\\002\\000'; sleep 5"), + ], + ) + .expect("third recorder works"); + thread::sleep(Duration::from_millis(50)); + recording.finish().unwrap(); + assert_eq!(drain(&mut rx), vec![2, 2]); + } + + #[cfg(unix)] + #[test] + fn no_working_recorder_reports_microphone_unavailable() { + let result = start_pcm(Arc::new(AtomicBool::new(false)), vec![shell("exit 1")]); + assert!(matches!(result, Err(VoiceError::MicrophoneUnavailable))); + } + + #[cfg(unix)] + #[test] + fn recorder_exiting_after_audio_ends_the_stream_cleanly() { + let (recording, mut rx) = start_pcm( + Arc::new(AtomicBool::new(false)), + vec![shell("printf '\\003\\000'")], + ) + .unwrap(); + let deadline = Instant::now() + Duration::from_secs(2); + while !recording.is_finished() && Instant::now() < deadline { + thread::sleep(Duration::from_millis(5)); + } + assert!(recording.is_finished()); + recording.finish().unwrap(); + assert_eq!(drain(&mut rx), vec![3]); + } +} diff --git a/crates/jcode-base/src/voice/nari.rs b/crates/jcode-base/src/voice/nari.rs index b7928d84c2f..6c7d830d68e 100644 --- a/crates/jcode-base/src/voice/nari.rs +++ b/crates/jcode-base/src/voice/nari.rs @@ -208,7 +208,6 @@ pub fn nari_pcm_channel() -> (mpsc::Sender>, mpsc::Receiver>) /// Microphone-owned channel. Holds 100 ms chunks for longer than the setup /// timeout so audio captured during the handshake is buffered, never dropped. -#[cfg(feature = "voice-capture")] pub(super) fn capture_pcm_channel() -> (mpsc::Sender>, mpsc::Receiver>) { mpsc::channel((IO_TIMEOUT.as_secs() as usize + 5) * 10) } diff --git a/crates/jcode-base/src/voice/streaming_capture.rs b/crates/jcode-base/src/voice/streaming_capture.rs index 259bc056f6e..831aca0829e 100644 --- a/crates/jcode-base/src/voice/streaming_capture.rs +++ b/crates/jcode-base/src/voice/streaming_capture.rs @@ -1,5 +1,10 @@ -//! Dedicated-thread native streaming capture and nonblocking desktop handle. -use super::{resample::Resampler, *}; +//! Dedicated-thread streaming capture and the nonblocking push-to-talk handle. +//! Native (cpal) capture needs the `voice-capture` feature. External recorder +//! capture (`super::command_capture`) works in every build. +#[cfg(feature = "voice-capture")] +use super::resample::Resampler; +use super::*; +#[cfg(feature = "voice-capture")] use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use std::{ collections::VecDeque, @@ -11,14 +16,15 @@ use std::{ thread, }; -/// Native mono 16k PCM capture. Stop is a signal, EOF follows buffered chunks. +/// Mono 16k PCM capture. Stop is a signal, EOF follows buffered chunks. /// Dropping an unfinished handle cancels, never blocks the UI on native teardown. pub struct PcmRecording { - level: Arc, - stop: Arc, - cancel: Arc, - worker: Option>>, + pub(super) level: Arc, + pub(super) stop: Arc, + pub(super) cancel: Arc, + pub(super) worker: Option>>, } +#[cfg(feature = "voice-capture")] impl MicrophoneRecording { /// Blocking setup, call off the UI thread and only after explicit user consent. pub fn start_pcm_cancellable( @@ -28,8 +34,25 @@ impl MicrophoneRecording { } } impl PcmRecording { + #[cfg(feature = "voice-capture")] fn start( cancel: Arc, + ) -> Result<(Self, tokio::sync::mpsc::Receiver>), VoiceError> { + Self::start_worker(cancel, capture) + } + /// Shared setup: `body` owns the device on a dedicated thread, sends PCM on + /// the channel, reports readiness once, and returns after stop or cancel. + pub(super) fn start_worker( + cancel: Arc, + body: impl FnOnce( + tokio::sync::mpsc::Sender>, + &mpsc::SyncSender>, + Arc, + Arc, + Arc, + ) -> Result<(), VoiceError> + + Send + + 'static, ) -> Result<(Self, tokio::sync::mpsc::Receiver>), VoiceError> { if cancel.load(Ordering::SeqCst) { return Err(VoiceError::Cancelled); @@ -43,7 +66,7 @@ impl PcmRecording { let worker = thread::Builder::new() .name("voice-pcm".into()) .spawn(move || { - let result = capture(tx, &ready, c, s, worker_level); + let result = body(tx, &ready, c, s, worker_level); if let Err(e) = &result { let _ = ready.try_send(Err(e.clone())); } @@ -133,6 +156,7 @@ fn wait_started( } } +#[cfg(feature = "voice-capture")] struct Chunker { level: Arc, resampler: Resampler, @@ -143,6 +167,7 @@ struct Chunker { first_callback: bool, first_voice: bool, } +#[cfg(feature = "voice-capture")] impl Chunker { fn push(&mut self, data: &[T], channels: usize) where @@ -220,6 +245,7 @@ impl Chunker { } } } +#[cfg(feature = "voice-capture")] fn build( device: &cpal::Device, config: &cpal::StreamConfig, @@ -248,6 +274,7 @@ where ) .map_err(|_| VoiceError::MicrophoneUnavailable) } +#[cfg(feature = "voice-capture")] fn capture( tx: tokio::sync::mpsc::Sender>, ready: &mpsc::SyncSender>, @@ -406,6 +433,8 @@ pub struct NariRecording { mic_stop: Arc>>, } impl NariRecording { + /// Native microphone capture. Requires the `voice-capture` feature. + #[cfg(feature = "voice-capture")] pub fn start_cancellable(cancel: Arc, key: &str) -> Result { Self::start_with( cancel, @@ -414,6 +443,35 @@ impl NariRecording { MicrophoneRecording::start_pcm_cancellable, ) } + /// Capture through external recorder processes, tried in order until one + /// delivers audio. Works without the `voice-capture` feature. + pub fn start_with_recorders( + cancel: Arc, + key: &str, + recorders: Vec, + ) -> Result { + if recorders.is_empty() { + return Err(VoiceError::MicrophoneUnavailable); + } + Self::start_with(cancel, key, super::nari::URL, move |c| { + super::command_capture::start_pcm(c, recorders) + }) + } + /// Push-to-talk for terminal clients. A configured `recorder` command wins. + /// Otherwise native capture is used when compiled in, else the first + /// working external recorder found on `PATH`. + pub fn start_auto( + cancel: Arc, + key: &str, + recorder: &str, + ) -> Result { + let custom = recorder.trim(); + #[cfg(feature = "voice-capture")] + if custom.is_empty() { + return Self::start_cancellable(cancel, key); + } + Self::start_with_recorders(cancel, key, super::detect_recorders(custom)) + } fn start_with( cancel: Arc, key: &str, @@ -537,6 +595,15 @@ impl NariRecording { .unwrap_or_default(); async move { ready.notified().await } } + /// Shared wakeup behind [`Self::event_ready`], for a long-lived waker task + /// that does not hold the recording itself. Stores a permit when nobody + /// waits, so an event is never missed. + pub fn event_notify(&self) -> Arc { + self.events + .lock() + .map(|events| events.ready.clone()) + .unwrap_or_default() + } /// Latest microphone callback's linear RMS of the downmixed mono signal, /// before resampling, normalized to `0.0..=1.0` (not decibels). /// Silence is zero. No smoothing or artificial animation is applied. @@ -572,6 +639,7 @@ impl Drop for NariRecording { #[cfg(test)] mod tests { use super::*; + #[cfg(feature = "voice-capture")] fn level_chunker() -> Chunker { let (tx, _rx) = nari_pcm_channel(); Chunker { @@ -585,9 +653,11 @@ mod tests { first_voice: false, } } + #[cfg(feature = "voice-capture")] fn level(c: &Chunker) -> f32 { f32::from_bits(c.level.load(Ordering::Relaxed)) } + #[cfg(feature = "voice-capture")] #[test] fn callback_level_is_latest_rms_not_peak_or_signed_average() { let mut c = level_chunker(); @@ -601,6 +671,7 @@ mod tests { c.push::(&[], 1); assert_eq!(level(&c), 0.0); } + #[cfg(feature = "voice-capture")] #[test] fn callback_level_downmixes_and_normalizes_native_formats() { let mut c = level_chunker(); @@ -617,6 +688,7 @@ mod tests { c.push(&[0.25f64, -0.25], 1); assert_eq!(level(&c), 0.25); } + #[cfg(feature = "voice-capture")] #[test] fn callback_level_is_finite_and_bounded_for_invalid_float_samples() { let mut c = level_chunker(); @@ -625,6 +697,7 @@ mod tests { c.push(&[f32::NAN, f32::INFINITY, f32::NEG_INFINITY], 1); assert_eq!(level(&c), 0.0); } + #[cfg(feature = "voice-capture")] #[test] fn recording_level_reads_callback_atomic_without_consuming_events() { let mut c = level_chunker(); @@ -658,6 +731,7 @@ mod tests { } assert_eq!(recording.audio_level(), 0.0); } + #[cfg(feature = "voice-capture")] #[test] fn cancelled_constructors_never_open_microphone() { let cancel = Arc::new(AtomicBool::new(true)); @@ -670,6 +744,7 @@ mod tests { Err(VoiceError::Cancelled) )); } + #[cfg(feature = "voice-capture")] #[test] fn bounded_chunks_downmix_and_fail_on_backpressure() { let (tx, mut rx) = nari_pcm_channel(); @@ -919,6 +994,7 @@ mod tests { .await .unwrap(); } + #[cfg(feature = "voice-capture")] #[test] fn filter_tail_never_exceeds_duration_cap() { for rate in [8000, 16000, 44100, 48000, 96000, 192000] { @@ -945,6 +1021,7 @@ mod tests { } } + #[cfg(feature = "voice-capture")] #[test] fn upsampling_final_frame_cannot_exceed_cap() { let (tx, _rx) = nari_pcm_channel(); diff --git a/crates/jcode-config-types/src/keybindings.rs b/crates/jcode-config-types/src/keybindings.rs index 46e3b4b9276..67dacf39605 100644 --- a/crates/jcode-config-types/src/keybindings.rs +++ b/crates/jcode-config-types/src/keybindings.rs @@ -339,6 +339,14 @@ pub const KEYBINDING_DEFAULTS: &[KeybindingDefault] = &[ macos: PlatformDefault::dev("cmd+b"), other: PlatformDefault::dev("alt+r"), }, + KeybindingDefault { + id: "voice_input", + description: "Start or stop built-in voice input (Nari transcription)", + // Ctrl+Space is unused in jcode. Alt/Cmd+Space already route the next + // prompt to a new session. + macos: PlatformDefault::dev("ctrl+space"), + other: PlatformDefault::dev("ctrl+space"), + }, ]; /// Look up a keybinding action by id. diff --git a/crates/jcode-config-types/src/lib.rs b/crates/jcode-config-types/src/lib.rs index 436cf54ed2e..ad3da44bc07 100644 --- a/crates/jcode-config-types/src/lib.rs +++ b/crates/jcode-config-types/src/lib.rs @@ -1090,6 +1090,9 @@ pub struct KeybindingsConfig { /// Open the `/resume` session picker (default: "cmd+b" on macOS, "alt+r" /// elsewhere). Set "" to disable. pub open_resume: String, + /// Start/stop built-in voice input (default: "ctrl+space"). Speech streams + /// to Nari and the transcript is sent as a prompt. Set "" to disable. + pub voice_input: String, /// Session picker Enter action: "current-terminal" (default) or "new-terminal". /// Ctrl+Enter performs the alternate action. pub session_picker_enter: SessionPickerResumeAction, @@ -1142,6 +1145,7 @@ impl Default for KeybindingsConfig { "alt+r" }, ), + voice_input: get("voice_input", "ctrl+space"), session_picker_enter: SessionPickerResumeAction::CurrentTerminal, } } diff --git a/crates/jcode-session-types/src/lib.rs b/crates/jcode-session-types/src/lib.rs index 054debe6c77..1a5206f7d22 100644 --- a/crates/jcode-session-types/src/lib.rs +++ b/crates/jcode-session-types/src/lib.rs @@ -4,7 +4,11 @@ use serde::{Deserialize, Serialize}; use std::collections::HashSet; mod title; +mod transcription; pub use title::prompt_title; +pub use transcription::{ + TRANSCRIPTION_CLOSE, TRANSCRIPTION_OPEN, strip_transcription, wrap_transcription, +}; /// Identifies a session to resume, across the agent backends jcode can import /// from. This is pure data (only ids/paths) with no UI dependency; it lives in diff --git a/crates/jcode-session-types/src/transcription.rs b/crates/jcode-session-types/src/transcription.rs new file mode 100644 index 00000000000..7f6923ce474 --- /dev/null +++ b/crates/jcode-session-types/src/transcription.rs @@ -0,0 +1,84 @@ +//! Speech-to-text output is marked so the model knows the words were +//! transcribed (and may contain recognition errors), not typed. Same format as +//! Jcode Desktop, so sessions render identically in every client. + +use std::borrow::Cow; + +pub const TRANSCRIPTION_OPEN: &str = ""; +pub const TRANSCRIPTION_CLOSE: &str = ""; + +/// Wrap a transcript for the model. +pub fn wrap_transcription(text: &str) -> String { + format!( + "{TRANSCRIPTION_OPEN}\n{}\n{TRANSCRIPTION_CLOSE}", + text.trim() + ) +} + +/// Remove transcription tags for display. Returns the visible text and +/// whether any complete tagged segment was found. Unbalanced text is kept +/// verbatim so an unrelated literal `` is never hidden. +pub fn strip_transcription(text: &str) -> (Cow<'_, str>, bool) { + if !text.contains(TRANSCRIPTION_OPEN) || !text.contains(TRANSCRIPTION_CLOSE) { + return (Cow::Borrowed(text), false); + } + let mut out = String::with_capacity(text.len()); + let mut rest = text; + let mut found = false; + while let Some(start) = rest.find(TRANSCRIPTION_OPEN) { + let after_open = &rest[start + TRANSCRIPTION_OPEN.len()..]; + let Some(end) = after_open.find(TRANSCRIPTION_CLOSE) else { + break; + }; + out.push_str(&rest[..start]); + out.push_str(after_open[..end].trim_matches('\n')); + rest = &after_open[end + TRANSCRIPTION_CLOSE.len()..]; + found = true; + } + if !found { + return (Cow::Borrowed(text), false); + } + out.push_str(rest); + (Cow::Owned(out.trim().to_string()), true) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn wrap_and_strip_round_trip() { + let wrapped = wrap_transcription(" fix the flaky test "); + assert_eq!( + wrapped, + "\nfix the flaky test\n" + ); + assert_eq!( + strip_transcription(&wrapped), + (Cow::Borrowed("fix the flaky test"), true) + ); + } + + #[test] + fn strip_keeps_typed_text_and_multiple_segments() { + let text = format!( + "Typed first\n{}\nand {}", + wrap_transcription("one"), + wrap_transcription("two") + ); + let (visible, found) = strip_transcription(&text); + assert!(found); + assert_eq!(visible, "Typed first\none\nand two"); + } + + #[test] + fn unbalanced_or_absent_tags_are_untouched() { + for text in [ + "plain", + " open only", + "close only ", + ] { + assert_eq!(strip_transcription(text), (Cow::Borrowed(text), false)); + } + } +} diff --git a/crates/jcode-tui/src/tui/app.rs b/crates/jcode-tui/src/tui/app.rs index f2b756c3f3a..397d25d7507 100644 --- a/crates/jcode-tui/src/tui/app.rs +++ b/crates/jcode-tui/src/tui/app.rs @@ -99,6 +99,7 @@ mod state_ui; mod state_ui_input_helpers; mod update_sim; mod usage_reset; +mod voice_input; pub(crate) use state_ui_input_helpers::{registered_command_entries, registered_command_names}; mod state_ui_maintenance; mod state_ui_messages; @@ -1500,6 +1501,12 @@ pub struct App { new_terminal_key: OptionalBinding, // Optional configured keybinding for opening the /resume session picker open_resume_key: OptionalBinding, + // Keybinding that starts/stops built-in voice input (Ctrl+Space default) + voice_input_key: OptionalBinding, + // Active built-in voice input (Nari streaming), if recording or finishing + voice_input: Option, + // Last voice key press, to tell a held key's auto-repeat from a new press + voice_input_last_press: Option, // Optional configured keybinding for accepting the post-error fallback offer fallback_switch_key: OptionalBinding, // Config reload generation the keybinding snapshot above was parsed at. diff --git a/crates/jcode-tui/src/tui/app/commands.rs b/crates/jcode-tui/src/tui/app/commands.rs index 7119dee8fb0..a304e03ff90 100644 --- a/crates/jcode-tui/src/tui/app/commands.rs +++ b/crates/jcode-tui/src/tui/app/commands.rs @@ -2857,6 +2857,18 @@ pub(super) fn active_working_dir(app: &App) -> Option { } pub(super) fn handle_dictation_command(app: &mut App, trimmed: &str) -> bool { + if trimmed == "/voice" { + app.toggle_voice_input(); + return true; + } + if trimmed.starts_with("/voice ") { + let key = app.voice_input_key_label().unwrap_or("unbound").to_string(); + app.push_display_message(DisplayMessage::error(format!( + "Usage: /voice (or {key}) starts recording, run it again to send, Esc cancels.\n\ + Needs a Nari API key (NARI_API_KEY or ~/.config/jcode/nari.env)." + ))); + return true; + } if trimmed == "/dictate" || trimmed == "/dictation" { app.handle_dictation_trigger(); return true; diff --git a/crates/jcode-tui/src/tui/app/commands_dispatch.rs b/crates/jcode-tui/src/tui/app/commands_dispatch.rs index 575b8b0642e..c6879ec2022 100644 --- a/crates/jcode-tui/src/tui/app/commands_dispatch.rs +++ b/crates/jcode-tui/src/tui/app/commands_dispatch.rs @@ -83,6 +83,7 @@ pub(super) fn ssh_unsupported_command(input: &str) -> bool { | "/goals" | "/dictate" | "/dictation" + | "/voice" | "/debug-fixture" | "/debug-visual" | "/screenshot" diff --git a/crates/jcode-tui/src/tui/app/hotkey_feedback.rs b/crates/jcode-tui/src/tui/app/hotkey_feedback.rs index e4d10fbd979..ff4eb71db47 100644 --- a/crates/jcode-tui/src/tui/app/hotkey_feedback.rs +++ b/crates/jcode-tui/src/tui/app/hotkey_feedback.rs @@ -98,6 +98,7 @@ pub(super) struct RegistryInputs<'a> { pub dictation: &'a OptionalBinding, pub new_terminal: &'a OptionalBinding, pub open_resume: &'a OptionalBinding, + pub voice_input: &'a OptionalBinding, pub fallback_switch: &'a OptionalBinding, /// Workspace navigation only dispatches in remote/client mode. pub remote: bool, @@ -175,6 +176,11 @@ pub(super) fn build_registry(inputs: &RegistryInputs<'_>) -> Vec { "open_resume", "open the session picker", ); + push( + inputs.voice_input.binding.clone(), + "voice_input", + "start or stop voice input", + ); // Context-armed accept key (fallback offer / update merge). Quiet: it only // acts when an offer is on screen, which already explains itself. // Pushed directly (not via `push`), so re-create the closure afterwards to @@ -718,6 +724,7 @@ impl App { dictation: &self.dictation_key, new_terminal: &self.new_terminal_key, open_resume: &self.open_resume_key, + voice_input: &self.voice_input_key, fallback_switch: &self.fallback_switch_key, remote, }) @@ -891,6 +898,10 @@ mod tests { binding: Some(ctrl('y')), label: Some("Ctrl+Y".to_string()), }; + let voice_input = OptionalBinding { + binding: Some(key(KeyCode::Char(' '), KeyModifiers::CONTROL)), + label: Some("Ctrl+Space".to_string()), + }; build_registry(&RegistryInputs { model_switch: &model_switch, effort: &effort, @@ -901,6 +912,7 @@ mod tests { dictation: &dictation, new_terminal: &new_terminal, open_resume: &open_resume, + voice_input: &voice_input, fallback_switch: &fallback_switch, remote, }) @@ -1053,6 +1065,7 @@ mod tests { ("workspace_right", Some(&["workspace_right"])), ("new_terminal", Some(&["new_terminal"])), ("open_resume", Some(&["open_resume"])), + ("voice_input", Some(&["voice_input"])), ]; let registry = test_inputs_registry(true); diff --git a/crates/jcode-tui/src/tui/app/input_help.rs b/crates/jcode-tui/src/tui/app/input_help.rs index 973ecbefbcf..b3157406caf 100644 --- a/crates/jcode-tui/src/tui/app/input_help.rs +++ b/crates/jcode-tui/src/tui/app/input_help.rs @@ -123,6 +123,9 @@ impl App { "overnight" => { "/overnight [h|m] [mission]\nStart one overnight coordinator with a target wake/report time. The coordinator prioritizes verifiable, low-risk work, maintains structured logs, updates review notes, and generates a review HTML page.\n\n/overnight status\nShow the latest overnight run status.\n\n/overnight log\nShow recent overnight events.\n\n/overnight review\nOpen the generated review page.\n\n/overnight cancel\nRequest cancellation after the current coordinator turn reaches a safe boundary." } + "voice" => { + "/voice\nBuilt-in voice input, like Jcode Desktop. Run /voice or press Ctrl+Space to start recording, press again to send. On terminals that report key releases, hold Ctrl+Space and let go to send. Esc cancels.\n\nThe live transcript and a level meter show in the status line. The final text is sent to the agent wrapped in tags, and anything already typed in the composer stays there.\n\nSetup:\n Nari API key NARI_API_KEY or ~/.config/jcode/nari.env\n microphone auto-detects pw-record, parecord, arecord, rec, or ffmpeg\n\nConfigure in ~/.jcode/config.toml:\n [keybindings] voice_input = \"ctrl+space\"\n [dictation] recorder = \"\"\n vocabulary = [\"names\", \"terms\"]" + } "dictate" | "dictation" => { "/dictate\nRun the configured external speech-to-text command and inject the transcript into jcode.\n\nConfigure [dictation] in ~/.jcode/config.toml:\n command shell command that prints transcript to stdout,\n for example ~/.local/bin/my-whisper-script --grammar-target code\n mode insert|append|replace|send\n key optional hotkey (for example alt+;)\n timeout_secs max wait time" } diff --git a/crates/jcode-tui/src/tui/app/local.rs b/crates/jcode-tui/src/tui/app/local.rs index 5a55dd35567..382a6cbc76d 100644 --- a/crates/jcode-tui/src/tui/app/local.rs +++ b/crates/jcode-tui/src/tui/app/local.rs @@ -288,6 +288,7 @@ pub(super) fn handle_bus_event( app.handle_dictation_failure(message); true } + Ok(BusEvent::VoiceInputWake) => app.handle_voice_input_wake_local(), Ok(BusEvent::CompactionFinished) => app.poll_compaction_completion(), Ok(BusEvent::SidePanelUpdated(update)) => { if update.session_id == app.session.id { @@ -416,7 +417,10 @@ fn apply_terminal_event( crate::tui::ui::note_key_event_read(); app.note_client_interaction(); app.update_copy_badge_key_event(key); - if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + app.observe_voice_key_release(&key); + if app.handle_voice_key_event(&key) { + // Voice keys work from every screen and never type. + } else if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { app.handle_key_press_event(key)?; } Ok(true) diff --git a/crates/jcode-tui/src/tui/app/remote.rs b/crates/jcode-tui/src/tui/app/remote.rs index db95683d403..bf4ef427400 100644 --- a/crates/jcode-tui/src/tui/app/remote.rs +++ b/crates/jcode-tui/src/tui/app/remote.rs @@ -55,7 +55,8 @@ pub(super) use input_dispatch::{ apply_remote_transcript_event, apply_transcript_event, begin_remote_send, begin_remote_split_launch, finish_remote_split_launch, history_matches_pending_startup_prompt, route_prepared_input_to_new_remote_session, stage_turn_for_remote_tick_loop, - submit_prepared_remote_input, submit_remote_slash_input, + submit_prepared_remote_input, submit_remote_slash_input, submit_remote_voice_transcript, + submit_voice_transcript, }; pub(super) use key_handling::{ handle_remote_char_input, handle_remote_key, handle_remote_key_event, send_interleave_now, @@ -444,7 +445,10 @@ async fn apply_terminal_event( input_attribution.scroll_delta = key_scroll_delta(&key); app.note_client_interaction(); app.update_copy_badge_key_event(key); - if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + app.observe_voice_key_release(&key); + if app.handle_voice_key_event(&key) { + // Voice keys work from every screen and never type. + } else if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { handle_remote_key_event(app, key, remote).await?; if let Some(selection) = app.pending_route_selection.take() { app.pending_model_switch = None; @@ -703,6 +707,20 @@ pub(super) async fn handle_bus_event( app.handle_dictation_failure(message); true } + Ok(BusEvent::VoiceInputWake) => match app.poll_voice_input() { + super::voice_input::VoicePoll::Idle => false, + super::voice_input::VoicePoll::Changed => true, + super::voice_input::VoicePoll::Transcript(text) => { + if let Err(error) = submit_remote_voice_transcript(app, remote, &text).await { + app.push_display_message(DisplayMessage::error(format!( + "Failed to send voice transcript: {error}" + ))); + app.set_status_notice("Voice transcript not sent"); + } + process_remote_followups(app, remote).await; + true + } + }, _ => false, } } @@ -818,7 +836,10 @@ fn handle_terminal_event_while_disconnected( Some(Ok(Event::Key(key))) => { app.note_client_interaction(); app.update_copy_badge_key_event(key); - if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + app.observe_voice_key_release(&key); + if app.handle_voice_key_event(&key) { + // Voice keys work from every screen and never type. + } else if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { handle_disconnected_key_event(app, key)?; } needs_redraw = true; diff --git a/crates/jcode-tui/src/tui/app/remote/input_dispatch.rs b/crates/jcode-tui/src/tui/app/remote/input_dispatch.rs index 7cdfc62ac53..15de10f5edf 100644 --- a/crates/jcode-tui/src/tui/app/remote/input_dispatch.rs +++ b/crates/jcode-tui/src/tui/app/remote/input_dispatch.rs @@ -526,6 +526,67 @@ pub(in crate::tui::app) fn apply_transcript_event( app.follow_chat_bottom_for_typing(); } +/// Composer state set aside while a voice transcript is sent as its own +/// prompt, so text the user already typed is neither sent nor lost. +struct StashedDraft { + input: String, + cursor_pos: usize, + pasted_contents: Vec, + pending_images: Vec<(String, String)>, + undo: Vec<(String, usize)>, +} + +fn stash_draft_for_voice(app: &mut App, transcript: &str) -> StashedDraft { + let stash = StashedDraft { + input: std::mem::take(&mut app.input), + cursor_pos: std::mem::take(&mut app.cursor_pos), + pasted_contents: std::mem::take(&mut app.pasted_contents), + pending_images: std::mem::take(&mut app.pending_images), + undo: std::mem::take(&mut app.input_undo_stack), + }; + app.input = jcode_session_types::wrap_transcription(transcript); + app.cursor_pos = app.input.len(); + stash +} + +fn restore_draft_after_voice(app: &mut App, stash: StashedDraft) { + // Submission consumes the composer. Anything left means it was kept (for + // example an oversized prompt), which must not be silently replaced. + if !app.input.is_empty() { + return; + } + app.input = stash.input; + app.cursor_pos = stash.cursor_pos.min(app.input.len()); + app.pasted_contents = stash.pasted_contents; + app.pending_images = stash.pending_images; + app.input_undo_stack = stash.undo; + app.reset_tab_completion(); + app.sync_model_picker_preview_from_input(); +} + +/// Send a built-in voice transcript from a local (in-process) session. It is +/// wrapped in `` tags, sent as soon as possible like Enter +/// (steering an active turn), and the typed draft is left untouched. +pub(in crate::tui::app) fn submit_voice_transcript(app: &mut App, transcript: &str) { + let stash = stash_draft_for_voice(app, transcript); + submit_transcript_input(app); + restore_draft_after_voice(app, stash); + app.follow_chat_bottom_for_typing(); +} + +/// Remote-session counterpart of [`submit_voice_transcript`]. +pub(in crate::tui::app) async fn submit_remote_voice_transcript( + app: &mut App, + remote: &mut RemoteConnection, + transcript: &str, +) -> Result<()> { + let stash = stash_draft_for_voice(app, transcript); + let result = submit_remote_transcript_input(app, remote).await; + restore_draft_after_voice(app, stash); + app.follow_chat_bottom_for_typing(); + result +} + pub(in crate::tui::app) async fn apply_remote_transcript_event( app: &mut App, remote: &mut RemoteConnection, diff --git a/crates/jcode-tui/src/tui/app/remote_tests.rs b/crates/jcode-tui/src/tui/app/remote_tests.rs index 764ea75ce19..47b82d6eb9c 100644 --- a/crates/jcode-tui/src/tui/app/remote_tests.rs +++ b/crates/jcode-tui/src/tui/app/remote_tests.rs @@ -670,6 +670,7 @@ fn startup_history(session_id: &str) -> ServerEvent { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), } } diff --git a/crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs b/crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs index 008d533a11c..f922284cdd2 100644 --- a/crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs +++ b/crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs @@ -138,6 +138,7 @@ const REGISTERED_COMMANDS: &[RegisteredCommand] = &[ RegisteredCommand::public("/refactor", "Run a safe refactor loop"), RegisteredCommand::public("/compact", "Compact context"), RegisteredCommand::public("/fix", "Recover when the model cannot continue"), + RegisteredCommand::public("/voice", "Voice input: speak, then send (Ctrl+Space)"), RegisteredCommand::public("/dictate", "Run configured external dictation command"), RegisteredCommand::public("/dictation", "Alias for /dictate"), RegisteredCommand::public("/memory", "Toggle memory feature"), diff --git a/crates/jcode-tui/src/tui/app/tests.rs b/crates/jcode-tui/src/tui/app/tests.rs index b757777943b..ea27a5993e0 100644 --- a/crates/jcode-tui/src/tui/app/tests.rs +++ b/crates/jcode-tui/src/tui/app/tests.rs @@ -52,6 +52,7 @@ include!("tests/terminal_setup_command.rs"); include!("tests/issue_497_copy_ctrl_c.rs"); include!("tests/issue_699_ctrl_d_delete.rs"); include!("tests/issue_832_remote_ctrl_k.rs"); +include!("tests/voice_input.rs"); include!("tests/issue_998_model_status_overlay.rs"); include!("tests/spinner_slash_commands.rs"); include!("tests/command_suggestions_cache.rs"); @@ -1222,6 +1223,7 @@ fn stale_server_history_is_deferred_before_remote_state_is_applied() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -1316,6 +1318,7 @@ fn deferred_stale_server_history_captures_session_id_for_reload_handoff() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -1400,6 +1403,7 @@ fn ancient_server_history_is_deferred_via_client_side_release_check() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -1483,6 +1487,7 @@ fn older_server_reporting_no_update_is_still_deferred_via_client_check() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -1586,6 +1591,7 @@ fn older_server_history_repairs_stale_shared_server_channel_end_to_end() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -1663,6 +1669,7 @@ fn current_release_server_history_is_not_deferred_by_client_check() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); diff --git a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs index 015753283db..ed3a28cc74a 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs @@ -217,6 +217,7 @@ fn test_handle_server_event_history_clears_connection_type_on_session_change_whe compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -271,6 +272,7 @@ fn test_handle_server_event_history_preserves_connection_type_for_same_session_w compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -325,6 +327,7 @@ fn test_handle_server_event_history_preserves_reasoning_effort_for_same_session_ compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -406,6 +409,7 @@ fn test_handle_server_event_history_session_change_clears_streaming_preview_diag compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -510,6 +514,7 @@ fn test_handle_server_event_history_same_session_rewind_reapply_clears_streaming compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -620,6 +625,7 @@ fn test_handle_server_event_history_same_session_midstream_duplicate_is_dropped_ compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -703,6 +709,7 @@ fn test_handle_server_event_history_same_session_midstream_duplicate_is_dropped_ compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -806,6 +813,7 @@ fn test_handle_server_event_history_same_session_rewind_then_late_done_does_not_ compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -896,6 +904,7 @@ fn test_handle_server_event_history_session_change_clears_pending_interleaves() compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -2014,6 +2023,7 @@ fn test_pending_startup_notice_survives_history_bootstrap_for_fresh_session() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); diff --git a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_02.rs b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_02.rs index 81e492c365a..03f4b0c74a3 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_02.rs @@ -392,6 +392,7 @@ fn test_remote_rewind_completion_shows_undo_hint_after_history_refresh() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); diff --git a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_01.rs b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_01.rs index a4f60cbe245..dc1bf69e786 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_01.rs @@ -171,6 +171,7 @@ fn test_handle_server_event_history_with_interruption_queues_continuation() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -249,6 +250,7 @@ fn test_handle_server_event_history_uses_server_owned_reload_recovery_directive( compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }; app.handle_server_event(event.clone(), &mut remote); @@ -329,6 +331,7 @@ fn test_handle_server_event_history_without_interruption_does_not_queue() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -394,6 +397,7 @@ fn test_handle_server_event_history_after_reload_reports_no_continuation_needed( compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -698,6 +702,7 @@ fn test_handle_server_event_history_restores_side_panel_snapshot() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: side_panel.clone(), + applets: Default::default(), }, &mut remote, ); @@ -759,6 +764,7 @@ fn test_handle_server_event_history_restores_active_resume_processing_state() { current_tool_name: Some("batch".to_string()), }), side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); diff --git a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_02.rs b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_02.rs index 021cf122656..0691d496c51 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_02.rs @@ -67,6 +67,7 @@ fn test_metadata_only_history_preserves_fast_restored_startup_state() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -146,6 +147,7 @@ fn test_duplicate_history_for_same_session_is_ignored_after_fast_path_restore() compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); diff --git a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_03/part_02.rs b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_03/part_02.rs index e9e6140f280..1ebff9110a2 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_03/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_03/part_02.rs @@ -161,6 +161,7 @@ fn test_initial_history_bootstrap_preserves_restored_interleave_state() { compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); @@ -254,6 +255,7 @@ fn test_initial_history_bootstrap_skips_resubmit_when_prompt_already_in_history( compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }, &mut remote, ); diff --git a/crates/jcode-tui/src/tui/app/tests/ssh_remote.rs b/crates/jcode-tui/src/tui/app/tests/ssh_remote.rs index 2373fb1b680..71e5083ba48 100644 --- a/crates/jcode-tui/src/tui/app/tests/ssh_remote.rs +++ b/crates/jcode-tui/src/tui/app/tests/ssh_remote.rs @@ -194,6 +194,7 @@ fn ssh_remote_history_is_authoritative_even_when_empty_or_server_version_differs compaction_mode: crate::config::CompactionMode::Reactive, activity: None, side_panel: crate::side_panel::SidePanelSnapshot::default(), + applets: Default::default(), }; app.handle_server_event(event, &mut remote); assert!(remote.has_loaded_history()); diff --git a/crates/jcode-tui/src/tui/app/tests/voice_input.rs b/crates/jcode-tui/src/tui/app/tests/voice_input.rs new file mode 100644 index 00000000000..f0a548173f7 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/tests/voice_input.rs @@ -0,0 +1,115 @@ +// Built-in voice input (Ctrl+Space / /voice), matching Jcode Desktop. + +#[test] +fn voice_transcript_local_send_wraps_tags_and_keeps_typed_draft() { + let mut app = create_test_app(); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + app.set_input_for_test("half typed thought"); + app.cursor_pos = 4; + + super::remote::submit_voice_transcript(&mut app, "fix the flaky test"); + + let last = app + .display_messages() + .last() + .expect("user message displayed"); + assert_eq!(last.role, "user"); + assert_eq!( + last.content, + "\nfix the flaky test\n" + ); + assert!(app.pending_turn, "local send uses the normal submit path"); + assert_eq!(app.input(), "half typed thought", "typed draft is kept"); + assert_eq!(app.cursor_pos(), 4, "cursor is restored"); +} + +#[test] +fn voice_transcript_remote_send_wraps_tags_and_keeps_typed_draft() { + let mut app = create_test_app(); + app.is_remote = true; + app.set_input_for_test("draft stays"); + let rt = tokio::runtime::Runtime::new().expect("runtime"); + + rt.block_on(async { + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + super::remote::submit_remote_voice_transcript(&mut app, &mut remote, "rename the rows") + .await + }) + .expect("remote voice send succeeds"); + + let last = app + .display_messages() + .last() + .expect("user message displayed"); + assert_eq!(last.role, "user"); + assert_eq!( + last.content, + "\nrename the rows\n" + ); + assert!(app.is_processing, "remote send enters processing"); + assert!(!app.pending_turn, "remote send never uses local pending_turn"); + assert_eq!(app.input(), "draft stays"); +} + +#[test] +fn voice_transcript_while_busy_steers_instead_of_waiting() { + let mut app = create_test_app(); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + app.is_processing = true; + app.queue_mode = false; + + super::remote::submit_voice_transcript(&mut app, "also check the logs"); + + assert_eq!( + app.interleave_message.as_deref(), + Some("\nalso check the logs\n"), + "like Enter, a busy turn is steered right away" + ); +} + +#[test] +fn voice_key_is_consumed_and_never_types_or_opens_other_actions() { + let mut app = create_test_app(); + let key = crossterm::event::KeyEvent::new(KeyCode::Char(' '), KeyModifiers::CONTROL); + // Without a Nari key in the test sandbox this reports setup help rather + // than recording, but the key itself must still be consumed. + assert!(app.handle_voice_key_event(&key)); + assert!(app.input().is_empty()); + let plain = crossterm::event::KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE); + assert!(!app.handle_voice_key_event(&plain), "plain space still types"); +} + +#[test] +fn voice_key_auto_repeat_is_not_a_second_toggle() { + let mut app = create_test_app(); + let press = crossterm::event::KeyEvent::new(KeyCode::Char(' '), KeyModifiers::CONTROL); + let before = app.display_messages().len(); + assert!(app.handle_voice_key_event(&press)); + let after_first = app.display_messages().len(); + // A second press within the debounce window is the same physical hold. + assert!(app.handle_voice_key_event(&press)); + assert_eq!(app.display_messages().len(), after_first); + assert!(after_first >= before); +} + +#[test] +fn voice_esc_passes_through_when_not_recording() { + let mut app = create_test_app(); + let esc = crossterm::event::KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE); + assert!( + !app.handle_voice_key_event(&esc), + "Esc keeps its normal meaning when voice input is idle" + ); +} + +#[test] +fn voice_input_help_and_command_are_registered() { + assert!(super::commands_dispatch::contains_registered_slash_command("/voice")); + let mut app = create_test_app(); + app.set_input_for_test("/voice extra"); + app.submit_input(); + let last = app.display_messages().last().expect("usage shown"); + assert!(last.content.contains("Usage: /voice"), "{}", last.content); +} diff --git a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs index 0b7a36b5df4..b95593b9e4b 100644 --- a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs +++ b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs @@ -112,6 +112,7 @@ impl App { self.dictation_key = keybind::load_dictation_key(); self.new_terminal_key = keybind::load_new_terminal_key(); self.open_resume_key = keybind::load_open_resume_key(); + self.voice_input_key = keybind::load_voice_input_key(); self.fallback_switch_key = keybind::load_fallback_switch_key(); self.scroll_keys = keybind::load_scroll_keys(); crate::logging::info("KEYBINDINGS: reloaded from config change"); @@ -673,6 +674,9 @@ impl App { dictation_key: keybind::load_dictation_key(), new_terminal_key: keybind::load_new_terminal_key(), open_resume_key: keybind::load_open_resume_key(), + voice_input_key: keybind::load_voice_input_key(), + voice_input: None, + voice_input_last_press: None, fallback_switch_key: keybind::load_fallback_switch_key(), scroll_keys: keybind::load_scroll_keys(), keybindings_config_generation: crate::config::config_reload_generation(), @@ -1130,6 +1134,9 @@ impl App { dictation_key: keybind::load_dictation_key(), new_terminal_key: keybind::load_new_terminal_key(), open_resume_key: keybind::load_open_resume_key(), + voice_input_key: keybind::load_voice_input_key(), + voice_input: None, + voice_input_last_press: None, fallback_switch_key: keybind::load_fallback_switch_key(), scroll_keys: keybind::load_scroll_keys(), keybindings_config_generation: crate::config::config_reload_generation(), diff --git a/crates/jcode-tui/src/tui/app/tui_state.rs b/crates/jcode-tui/src/tui/app/tui_state.rs index 48e4561d243..02b1e3b6e22 100644 --- a/crates/jcode-tui/src/tui/app/tui_state.rs +++ b/crates/jcode-tui/src/tui/app/tui_state.rs @@ -982,6 +982,10 @@ impl crate::tui::TuiState for App { self.remote_client_count } + fn voice_input_status(&self) -> Option<(bool, String)> { + self.voice_input_status_line() + } + fn status_notice(&self) -> Option { if !self.is_remote && self.provider.uses_jcode_compaction() diff --git a/crates/jcode-tui/src/tui/app/turn.rs b/crates/jcode-tui/src/tui/app/turn.rs index eb64e046199..34512901da4 100644 --- a/crates/jcode-tui/src/tui/app/turn.rs +++ b/crates/jcode-tui/src/tui/app/turn.rs @@ -186,7 +186,10 @@ impl App { match event { Some(Ok(Event::Key(key))) => { self.update_copy_badge_key_event(key); - if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + self.observe_voice_key_release(&key); + if self.handle_voice_key_event(&key) { + // Voice keys work from every screen and never type. + } else if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { let scroll_only = super::input::is_scroll_only_key(self, key.code, key.modifiers); let _ = self.handle_key_press_event(key); if self.cancel_requested { @@ -372,7 +375,10 @@ impl App { match event { Some(Ok(Event::Key(key))) => { self.update_copy_badge_key_event(key); - if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + self.observe_voice_key_release(&key); + if self.handle_voice_key_event(&key) { + // Voice keys work from every screen and never type. + } else if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { let scroll_only = super::input::is_scroll_only_key(self, key.code, key.modifiers); let _ = self.handle_key_press_event(key); // Check for cancel request @@ -1393,7 +1399,10 @@ impl App { match event { Some(Ok(Event::Key(key))) => { self.update_copy_badge_key_event(key); - if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + self.observe_voice_key_release(&key); + if self.handle_voice_key_event(&key) { + // Voice keys work from every screen and never type. + } else if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { let scroll_only = super::input::is_scroll_only_key(self, key.code, key.modifiers); let _ = self.handle_key_press_event(key); if self.cancel_requested { diff --git a/crates/jcode-tui/src/tui/app/voice_input.rs b/crates/jcode-tui/src/tui/app/voice_input.rs new file mode 100644 index 00000000000..16b1d071a29 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/voice_input.rs @@ -0,0 +1,385 @@ +//! Built-in voice input for the terminal UI, matching Jcode Desktop. +//! +//! Press the voice key (Ctrl+Space by default) or run `/voice`, speak, then +//! press again to send. On terminals that report key releases (Kitty keyboard +//! protocol), holding the key and letting go also sends, like Desktop's +//! hold-to-talk. Esc cancels. Audio streams to Nari while recording, the live +//! transcript and a level meter show in the status line, and the final text is +//! sent to the agent wrapped in `` tags. Whatever is already +//! typed in the composer stays there, untouched. +use super::*; +use crate::voice::{self, NariEvent, NariRecording, VoiceError}; +use crossterm::event::KeyEvent; +use std::collections::VecDeque; +use std::sync::atomic::Ordering; + +/// Holding the key at least this long before releasing it means push-to-talk: +/// the release sends. A shorter tap leaves recording on until the next press. +const HOLD_TO_TALK: Duration = Duration::from_millis(400); +/// Terminals without release reporting repeat a held key as fresh presses. +/// Presses closer together than this are one continuous hold, not a toggle. +const REPEAT_DEBOUNCE: Duration = Duration::from_millis(300); +/// Meter and live transcript refresh cadence while voice input is active. +const METER_TICK: Duration = Duration::from_millis(80); +const METER_BARS: usize = 10; +const LIVE_TAIL_CHARS: usize = 56; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum VoicePhase { + Recording, + Transcribing, +} + +pub(crate) struct VoiceInput { + recording: NariRecording, + waker_done: Arc, + phase: VoicePhase, + started: Instant, + pressed_at: Instant, + live: String, + levels: VecDeque, +} + +impl Drop for VoiceInput { + fn drop(&mut self) { + // Dropping an unfinished NariRecording cancels capture and the stream. + self.waker_done.store(true, Ordering::SeqCst); + } +} + +/// Result of draining the active recording. +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum VoicePoll { + /// Nothing to repaint. + Idle, + /// Meter, live text, or phase changed. Repaint. + Changed, + /// Final transcript, ready to send. + Transcript(String), +} + +impl App { + pub(crate) fn voice_input_key_label(&self) -> Option<&str> { + self.voice_input_key.label.as_deref() + } + + fn voice_input_key_matches(&self, code: KeyCode, modifiers: KeyModifiers) -> bool { + self.voice_input_key + .binding + .as_ref() + .is_some_and(|binding| binding.matches(code, modifiers)) + } + + /// Voice keys, handled before any other key routing so they work from + /// every screen. Returns true when the event was consumed. + pub(crate) fn handle_voice_key_event(&mut self, event: &KeyEvent) -> bool { + if self.voice_input.is_some() && event.code == KeyCode::Esc { + if event.kind == KeyEventKind::Press { + let discarded = self + .voice_input + .as_ref() + .is_some_and(|voice| voice.phase == VoicePhase::Transcribing); + self.cancel_voice_input(if discarded { + "Voice input discarded" + } else { + "Voice input canceled" + }); + } + return true; + } + if !self.voice_input_key_matches(event.code, event.modifiers) { + return false; + } + if event.kind != KeyEventKind::Press { + // Repeats of a held key are part of the same press. Releases are + // observed separately in `observe_voice_key_release`. + return true; + } + let now = Instant::now(); + let previous = self.voice_input_last_press.replace(now); + if previous.is_some_and(|at| now.duration_since(at) < REPEAT_DEBOUNCE) { + return true; + } + self.toggle_voice_input(); + true + } + + /// Hold-to-talk on terminals that report key releases: letting go of a + /// held voice key sends. A quick tap keeps recording (toggle mode). + pub(crate) fn observe_voice_key_release(&mut self, event: &KeyEvent) { + if event.kind != KeyEventKind::Release { + return; + } + let Some(binding) = self.voice_input_key.binding.as_ref() else { + return; + }; + // Modifiers may already be up when the key itself is released. + if !event.code.eq(&binding.code) { + return; + } + let held_long_enough = self.voice_input.as_ref().is_some_and(|voice| { + voice.phase == VoicePhase::Recording && voice.pressed_at.elapsed() >= HOLD_TO_TALK + }); + if held_long_enough { + self.stop_voice_input(); + } + } + + pub(crate) fn toggle_voice_input(&mut self) { + match self.voice_input.as_ref().map(|voice| voice.phase) { + None => self.start_voice_input(), + Some(VoicePhase::Recording) => self.stop_voice_input(), + Some(VoicePhase::Transcribing) => { + self.set_status_notice("Still transcribing. Esc discards it.") + } + } + } + + fn start_voice_input(&mut self) { + let Some(key) = voice::nari_api_key() else { + self.push_display_message(DisplayMessage::error( + "Voice input needs a Nari API key. Set `NARI_API_KEY`, or add \ + `NARI_API_KEY=...` to `~/.config/jcode/nari.env`." + .to_string(), + )); + self.set_status_notice("Voice input not configured"); + return; + }; + let recorder = crate::config::config().dictation.recorder.clone(); + if !voice::NATIVE_CAPTURE + && recorder.trim().is_empty() + && voice::detect_recorders("").is_empty() + { + self.push_display_message(DisplayMessage::error( + "Voice input found no microphone recorder. Install one of \ + `pw-record` (PipeWire), `parecord` (PulseAudio), `arecord` \ + (ALSA), `rec` (SoX), or `ffmpeg`, or set `[dictation] recorder` \ + in `~/.jcode/config.toml`." + .to_string(), + )); + self.set_status_notice("No microphone recorder found"); + return; + } + voice::timing::begin(); + let cancel = Arc::new(AtomicBool::new(false)); + // Nonblocking: the microphone opens and the Nari handshake runs in the + // background while audio buffers, so the first words are kept. + let recording = match NariRecording::start_auto(cancel, &key, &recorder) { + Ok(recording) => recording, + Err(error) => { + self.report_voice_error(&error); + return; + } + }; + let waker_done = Arc::new(AtomicBool::new(false)); + spawn_voice_waker(recording.event_notify(), waker_done.clone()); + let now = Instant::now(); + self.note_client_interaction(); + self.voice_input = Some(VoiceInput { + recording, + waker_done, + phase: VoicePhase::Recording, + started: now, + pressed_at: now, + live: String::new(), + levels: VecDeque::with_capacity(METER_BARS), + }); + } + + fn stop_voice_input(&mut self) { + let Some(voice) = self.voice_input.as_mut() else { + return; + }; + if voice.phase != VoicePhase::Recording { + return; + } + voice::timing::release(); + voice.recording.stop(); + voice.phase = VoicePhase::Transcribing; + } + + pub(crate) fn cancel_voice_input(&mut self, notice: &str) { + if self.voice_input.take().is_some() { + self.set_status_notice(notice.to_string()); + } + } + + fn report_voice_error(&mut self, error: &VoiceError) { + let hint = match error { + VoiceError::MicrophoneUnavailable if !voice::NATIVE_CAPTURE => { + " Check that a microphone is connected, or set `[dictation] recorder`." + } + _ => "", + }; + self.push_display_message(DisplayMessage::error(format!( + "Voice input failed: {error}.{hint}" + ))); + self.set_status_notice("Voice input failed"); + } + + /// Drain the active recording: meter level, live transcript, final result. + pub(crate) fn poll_voice_input(&mut self) -> VoicePoll { + let Some(voice) = self.voice_input.as_mut() else { + return VoicePoll::Idle; + }; + let mut changed = false; + if voice.phase == VoicePhase::Recording { + if voice.levels.len() == METER_BARS { + voice.levels.pop_front(); + } + voice.levels.push_back(voice.recording.audio_level()); + changed = true; + } + let mut finished = drain_voice_events(voice, &mut changed); + if finished.is_none() && voice.recording.is_finished() { + // Re-check after observing worker completion so a final event + // published just after the first drain is not lost. + finished = + drain_voice_events(voice, &mut changed).or(Some(Err(VoiceError::CaptureFailed))); + } + let Some(result) = finished else { + return if changed { + VoicePoll::Changed + } else { + VoicePoll::Idle + }; + }; + self.voice_input = None; + match result { + Ok(text) if !text.trim().is_empty() => VoicePoll::Transcript(text), + Ok(_) => { + self.set_status_notice("No speech detected"); + VoicePoll::Changed + } + Err(VoiceError::Cancelled) => VoicePoll::Changed, + Err(error) => { + self.report_voice_error(&error); + VoicePoll::Changed + } + } + } + + /// Local (in-process) sessions: handle a voice wake from the bus. + pub(crate) fn handle_voice_input_wake_local(&mut self) -> bool { + match self.poll_voice_input() { + VoicePoll::Idle => false, + VoicePoll::Changed => true, + VoicePoll::Transcript(text) => { + super::remote::submit_voice_transcript(self, &text); + true + } + } + } + + /// Status-line text while voice input is active: `(recording, text)`. + pub(crate) fn voice_input_status_line(&self) -> Option<(bool, String)> { + let voice = self.voice_input.as_ref()?; + let key = self.voice_input_key_label().unwrap_or("/voice"); + let live = live_tail(&voice.live); + Some(match voice.phase { + VoicePhase::Recording => { + let secs = voice.started.elapsed().as_secs(); + let meter: String = voice.levels.iter().map(|level| meter_bar(*level)).collect(); + let heard = if live.is_empty() { + "listening…".to_string() + } else { + live + }; + ( + true, + format!( + "● {}:{:02} {meter} {heard} · {key} send · Esc cancel", + secs / 60, + secs % 60 + ), + ) + } + VoicePhase::Transcribing => { + let heard = if live.is_empty() { + String::new() + } else { + format!(" {live}") + }; + (false, format!("◌ Transcribing…{heard} · Esc discard")) + } + }) + } +} + +fn drain_voice_events( + voice: &mut VoiceInput, + changed: &mut bool, +) -> Option> { + // Bounded so a fast provider cannot monopolize the UI loop. + for _ in 0..64 { + match voice.recording.try_event()? { + NariEvent::Started => {} + NariEvent::Transcript(text) => { + voice.live = text; + *changed = true; + } + NariEvent::Finished(result) => return Some(result), + } + } + None +} + +/// Wakes the UI loop on every provider event and on a steady meter tick, so +/// the recording state repaints in every run loop without polling. +fn spawn_voice_waker(notify: Arc, done: Arc) { + let Ok(runtime) = tokio::runtime::Handle::try_current() else { + return; + }; + runtime.spawn(async move { + while !done.load(Ordering::SeqCst) { + let _ = tokio::time::timeout(METER_TICK, notify.notified()).await; + if done.load(Ordering::SeqCst) { + break; + } + Bus::global().publish(BusEvent::VoiceInputWake); + } + }); +} + +/// RMS level to a meter glyph. Speech RMS sits around 0.02 to 0.2, so the +/// scale is compressed to keep normal speech visibly moving. +fn meter_bar(level: f32) -> char { + const BARS: [char; 8] = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█']; + let scaled = (level.max(0.0) * 4.0).sqrt().min(1.0); + BARS[(scaled * 7.0).round() as usize] +} + +fn live_tail(text: &str) -> String { + let text = text.trim(); + let count = text.chars().count(); + if count <= LIVE_TAIL_CHARS { + return text.to_string(); + } + let tail: String = text.chars().skip(count - LIVE_TAIL_CHARS).collect(); + format!("…{}", tail.trim_start()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn meter_is_flat_for_silence_and_full_for_loud_speech() { + assert_eq!(meter_bar(0.0), '▁'); + assert_eq!(meter_bar(-1.0), '▁'); + assert_eq!(meter_bar(0.25), '█'); + assert_eq!(meter_bar(1.0), '█'); + let speech = meter_bar(0.05); + assert!(speech != '▁' && speech != '█', "{speech}"); + } + + #[test] + fn live_tail_keeps_the_most_recent_words() { + assert_eq!(live_tail(" short "), "short"); + let long = format!("{} the end", "word ".repeat(30)); + let tail = live_tail(&long); + assert!(tail.starts_with('…')); + assert!(tail.ends_with("the end")); + assert!(tail.chars().count() <= LIVE_TAIL_CHARS + 1); + } +} diff --git a/crates/jcode-tui/src/tui/keybind.rs b/crates/jcode-tui/src/tui/keybind.rs index 896ebe40150..13446bbaebb 100644 --- a/crates/jcode-tui/src/tui/keybind.rs +++ b/crates/jcode-tui/src/tui/keybind.rs @@ -611,6 +611,23 @@ pub fn load_new_terminal_key() -> OptionalBinding { } } +/// Optional binding that starts/stops built-in voice input. +/// Default: Ctrl+Space. Set "" to disable. +pub fn load_voice_input_key() -> OptionalBinding { + let cfg = config(); + let raw = cfg.keybindings.voice_input.trim(); + if raw.is_empty() || is_disabled(raw) { + return OptionalBinding::default(); + } + match parse_keybinding(raw) { + Some(binding) => OptionalBinding { + label: Some(format_binding(&binding)), + binding: Some(binding), + }, + None => OptionalBinding::default(), + } +} + /// Optional binding that opens the `/resume` session picker. /// Default: Cmd+B on macOS, Alt+R elsewhere. Set "" to disable. pub fn load_open_resume_key() -> OptionalBinding { diff --git a/crates/jcode-tui/src/tui/mod.rs b/crates/jcode-tui/src/tui/mod.rs index a90ad55a7a5..d9bd61f07a1 100644 --- a/crates/jcode-tui/src/tui/mod.rs +++ b/crates/jcode-tui/src/tui/mod.rs @@ -606,6 +606,11 @@ pub trait TuiState { fn connected_clients(&self) -> Option; /// Short-lived notice shown in the status line (e.g., model switch, toggle diff) fn status_notice(&self) -> Option; + /// Built-in voice input status while recording or transcribing: + /// `(recording, text)`. Shown ahead of every other notice. + fn voice_input_status(&self) -> Option<(bool, String)> { + None + } /// How long since the user last pressed a key, scrolled, or pasted, or /// `None` when they have not interacted yet. /// @@ -883,6 +888,9 @@ pub trait TuiState { } /// Whether the notification line has content to show fn has_notification(&self) -> bool { + if self.voice_input_status().is_some() { + return true; + } if self.openai_reset_hint().is_some() { return true; } diff --git a/crates/jcode-tui/src/tui/ui_input.rs b/crates/jcode-tui/src/tui/ui_input.rs index 929c30e8d16..92f57efca44 100644 --- a/crates/jcode-tui/src/tui/ui_input.rs +++ b/crates/jcode-tui/src/tui/ui_input.rs @@ -1824,6 +1824,15 @@ pub(super) fn build_notification_spans(app: &dyn TuiState) -> Vec> } }; + if let Some((recording, text)) = app.voice_input_status() { + let color = if recording { + rgb(255, 110, 110) + } else { + rgb(140, 200, 255) + }; + spans.push(Span::styled(text, Style::default().fg(color).bold())); + } + if let Some(selection) = app.copy_selection_status() { let pane_label = selection.pane.label(); let label = if selection.has_action { diff --git a/crates/jcode-tui/src/tui/ui_overlays.rs b/crates/jcode-tui/src/tui/ui_overlays.rs index cca267f770d..44411ac1894 100644 --- a/crates/jcode-tui/src/tui/ui_overlays.rs +++ b/crates/jcode-tui/src/tui/ui_overlays.rs @@ -220,6 +220,10 @@ pub(super) fn draw_help_overlay(frame: &mut Frame, area: Rect, scroll: usize, ap lines.push(help_entry("/config", "Show active configuration")); lines.push(help_entry("/config init", "Create default config file")); lines.push(help_entry("/config edit", "Open config in $EDITOR")); + lines.push(help_entry( + "/voice", + "Voice input: speak, then send (Ctrl+Space)", + )); lines.push(help_entry("/dictate", "Run configured external dictation")); lines.push(help_entry( "/git [status]", diff --git a/crates/jcode-tui/src/tui/ui_prepare.rs b/crates/jcode-tui/src/tui/ui_prepare.rs index 4b3b4255066..28f50ae9a9d 100644 --- a/crates/jcode-tui/src/tui/ui_prepare.rs +++ b/crates/jcode-tui/src/tui/ui_prepare.rs @@ -368,6 +368,14 @@ fn push_user_prompt_lines( ) { let prefix_width = unicode_width::UnicodeWidthStr::width(prompt_num.to_string().as_str()) + unicode_width::UnicodeWidthStr::width("› "); + // Voice prompts carry tags for the model. Show the spoken + // words with a mic marker instead, like Jcode Desktop. + let (visible, transcribed) = jcode_session_types::strip_transcription(content); + let content: &str = if transcribed { + &format!("🎙 {visible}") + } else { + content + }; let normalized = content.replace("\r\n", "\n").replace('\r', "\n"); for (line_idx, content_line) in normalized.split('\n').enumerate() { let raw_line = raw_plain_lines.len(); From 28536e51b21ca2492a578d223fdf7811bcb19dc4 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 26 Sep 2026 00:01:44 -0700 Subject: [PATCH 0030/1618] voice: native mic capture for the CLI on macOS and Windows Enable jcode-base voice-capture (cpal over CoreAudio/WASAPI, the same config Jcode Desktop ships) for macOS and Windows CLI builds. Linux keeps the external recorder so release builds need no ALSA headers. Drop the ffmpeg avfoundation/dshow fallbacks, which had no valid default device. --- Cargo.lock | 1 + .../jcode-base/src/voice/command_capture.rs | 36 +++++++++++++------ crates/jcode-tui/Cargo.toml | 7 ++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca65ab9c716..8c2fd3467a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4159,6 +4159,7 @@ dependencies = [ "hex", "image", "jcode-app-core", + "jcode-base", "jcode-build-meta", "jcode-config-types", "jcode-core", diff --git a/crates/jcode-base/src/voice/command_capture.rs b/crates/jcode-base/src/voice/command_capture.rs index 9bd0c5075bc..d2d5e3f3659 100644 --- a/crates/jcode-base/src/voice/command_capture.rs +++ b/crates/jcode-base/src/voice/command_capture.rs @@ -141,17 +141,31 @@ fn known_recorders() -> Vec { ], ), ]; - let ffmpeg_input: &[&str] = if cfg!(target_os = "macos") { - &["-f", "avfoundation", "-i", ":default"] - } else if cfg!(windows) { - &["-f", "dshow", "-i", "audio=default"] - } else { - &["-f", "pulse", "-i", "default"] - }; - let mut ffmpeg = vec!["-hide_banner", "-loglevel", "error", "-nostdin"]; - ffmpeg.extend_from_slice(ffmpeg_input); - ffmpeg.extend_from_slice(&["-ac", "1", "-ar", "16000", "-f", "s16le", "-"]); - recorders.push(RecorderCommand::new("ffmpeg", &ffmpeg)); + // ffmpeg has no portable "default microphone" device name on macOS + // (avfoundation needs an index) or Windows (dshow needs the device name), + // and those platforms capture natively. Offer it only through PulseAudio. + if cfg!(not(any(target_os = "macos", windows))) { + recorders.push(RecorderCommand::new( + "ffmpeg", + &[ + "-hide_banner", + "-loglevel", + "error", + "-nostdin", + "-f", + "pulse", + "-i", + "default", + "-ac", + "1", + "-ar", + "16000", + "-f", + "s16le", + "-", + ], + )); + } recorders } diff --git a/crates/jcode-tui/Cargo.toml b/crates/jcode-tui/Cargo.toml index 6ddfac4cf4a..83aebfdc526 100644 --- a/crates/jcode-tui/Cargo.toml +++ b/crates/jcode-tui/Cargo.toml @@ -114,3 +114,10 @@ jcode-app-core = { path = "../jcode-app-core", default-features = false, feature [target.'cfg(target_os = "macos")'.dependencies] global-hotkey = "0.7" + +# Built-in voice input (/voice, Ctrl+Space). macOS and Windows capture natively +# through CoreAudio / WASAPI (system frameworks, like Jcode Desktop). Linux uses +# an external recorder (pw-record, parecord, arecord, ...) so release builds +# need no ALSA development headers. +[target.'cfg(any(target_os = "macos", target_os = "windows"))'.dependencies] +jcode-base = { path = "../jcode-base", default-features = false, features = ["voice-capture"] } From 2173da405b98daade565a6305bed7b23b7c19fe8 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 26 Sep 2026 16:23:16 -0700 Subject: [PATCH 0031/1618] swarm: cross-swarm DMs via to_swarm, unique swarm labels, list_swarms directory --- crates/jcode-app-core/src/server.rs | 2 + .../src/server/client_comm_message.rs | 121 +++++++++- .../src/server/client_comm_swarms.rs | 95 ++++++++ .../src/server/client_comm_tests.rs | 152 ++++++++++++ .../src/server/client_lifecycle.rs | 38 +++ .../src/server/client_lifecycle_logging.rs | 1 + .../src/server/client_lightweight_control.rs | 36 +++ .../jcode-app-core/src/server/swarm_labels.rs | 221 ++++++++++++++++++ crates/jcode-app-core/src/tool/communicate.rs | 138 ++++++++++- crates/jcode-protocol/src/lib.rs | 21 ++ crates/jcode-protocol/src/wire.rs | 24 ++ docs/SWARM_ARCHITECTURE.md | 14 ++ 12 files changed, 852 insertions(+), 11 deletions(-) create mode 100644 crates/jcode-app-core/src/server/client_comm_swarms.rs create mode 100644 crates/jcode-app-core/src/server/swarm_labels.rs diff --git a/crates/jcode-app-core/src/server.rs b/crates/jcode-app-core/src/server.rs index 392f319909a..f627de51993 100644 --- a/crates/jcode-app-core/src/server.rs +++ b/crates/jcode-app-core/src/server.rs @@ -7,6 +7,7 @@ mod client_comm; mod client_comm_channels; mod client_comm_context; mod client_comm_message; +mod client_comm_swarms; mod client_disconnect_cleanup; mod client_lifecycle; mod client_lifecycle_logging; @@ -45,6 +46,7 @@ mod runtime; mod socket; mod swarm; mod swarm_channels; +mod swarm_labels; mod swarm_mutation_state; mod swarm_persistence; mod util; diff --git a/crates/jcode-app-core/src/server/client_comm_message.rs b/crates/jcode-app-core/src/server/client_comm_message.rs index 34332d57952..631b1a7b37e 100644 --- a/crates/jcode-app-core/src/server/client_comm_message.rs +++ b/crates/jcode-app-core/src/server/client_comm_message.rs @@ -109,6 +109,7 @@ pub(super) async fn handle_comm_message( delivery: Option, wake: Option, tldr: Option, + to_swarm: Option, client_event_tx: &mpsc::UnboundedSender, sessions: &SessionAgents, soft_interrupt_queues: &SessionInterruptQueues, @@ -157,15 +158,83 @@ pub(super) async fn handle_comm_message( if let Some(swarm_id) = swarm_id { let friendly_name = friendly_name_for_session(&from_session, swarm_members).await; + // Cross-swarm DMs: resolve `to_swarm` (label or id). When it names a + // different swarm, the DM targets `to_session` inside that swarm, or + // its coordinator when no session is given. Channels and broadcasts + // never cross swarm boundaries. + let cross_swarm_id = match to_swarm.as_deref().map(str::trim) { + Some(target) if !target.is_empty() => { + let live_swarm_ids: HashSet = { + let swarms = swarms_by_id.read().await; + swarms + .iter() + .filter(|(_, sessions)| !sessions.is_empty()) + .map(|(id, _)| id.clone()) + .collect() + }; + match super::swarm_labels::resolve_swarm_target(target, &live_swarm_ids) { + Ok(resolved) if resolved == swarm_id => None, + Ok(resolved) => Some(resolved), + Err(err) => { + let _ = client_event_tx.send(ServerEvent::Error { + id, + message: format!("Cross-swarm DM failed: {err}"), + retry_after_secs: None, + }); + return; + } + } + } + _ => None, + }; + if cross_swarm_id.is_some() && channel.is_some() && to_session.is_none() { + let _ = client_event_tx.send(ServerEvent::Error { + id, + message: "Channels are swarm-local. Cross-swarm messages are DMs: pass to_swarm with an optional to_session.".to_string(), + retry_after_secs: None, + }); + return; + } + let delivery_swarm_id = cross_swarm_id.clone().unwrap_or_else(|| swarm_id.clone()); + let swarm_session_ids: Vec = { let swarms = swarms_by_id.read().await; swarms - .get(&swarm_id) + .get(&delivery_swarm_id) .map(|sessions| sessions.iter().cloned().collect()) .unwrap_or_default() }; - let resolved_to_session = if let Some(ref target) = to_session { + let cross_swarm_default_target = if cross_swarm_id.is_some() && to_session.is_none() { + let members = swarm_members.read().await; + let mut coordinators: Vec<&String> = swarm_session_ids + .iter() + .filter(|id| members.get(*id).is_some_and(|m| m.role == "coordinator")) + .collect(); + coordinators.sort(); + let fallback = || { + let mut ids: Vec<&String> = swarm_session_ids + .iter() + .filter(|id| { + members + .get(*id) + .is_some_and(|m| m.report_back_to_session_id.is_none()) + }) + .collect(); + ids.sort(); + ids.first().map(|id| (*id).clone()) + }; + match coordinators.first() { + Some(id) => Some((*id).clone()), + None => fallback().or_else(|| swarm_session_ids.iter().min().cloned()), + } + } else { + None + }; + + let resolved_to_session = if let Some(target) = cross_swarm_default_target { + Some(target) + } else if let Some(ref target) = to_session { match resolve_dm_target_session(target, &swarm_session_ids, swarm_members).await { Ok(session_id) => Some(session_id), Err(message) => { @@ -202,18 +271,35 @@ pub(super) async fn handle_comm_message( ("request_id", id.to_string()), ("from_session", from_session.clone()), ("target_session", target.clone()), - ("swarm_id", swarm_id.clone()), + ("swarm_id", delivery_swarm_id.clone()), ("elapsed_ms", started.elapsed().as_millis().to_string()), ], ); let _ = client_event_tx.send(ServerEvent::Error { id, - message: format!("DM failed: session '{}' not in swarm", target), + message: format!( + "DM failed: session '{}' not in swarm '{}'", + target, + super::swarm_labels::swarm_display_name(&delivery_swarm_id) + ), retry_after_secs: None, }); return; } + if cross_swarm_id.is_some() && resolved_to_session.is_none() { + let _ = client_event_tx.send(ServerEvent::Error { + id, + message: format!( + "Cross-swarm DM failed: swarm '{}' has no reachable members.", + super::swarm_labels::swarm_display_name(&delivery_swarm_id) + ), + retry_after_secs: None, + }); + return; + } + let sender_swarm_display = super::swarm_labels::swarm_display_name(&swarm_id); + let scope = if resolved_to_session.is_some() { "dm" } else if channel.is_some() { @@ -281,10 +367,16 @@ pub(super) async fn handle_comm_message( .clone() .unwrap_or_else(|| from_session[..8.min(from_session.len())].to_string()); let scope_label = match (scope, channel.as_deref()) { + _ if cross_swarm_id.is_some() => "Cross-swarm DM".to_string(), ("channel", Some(channel_name)) => format!("#{}", channel_name), ("dm", _) => "DM".to_string(), _ => "broadcast".to_string(), }; + let from_label = if cross_swarm_id.is_some() { + format!("{} (swarm '{}')", from_label, sender_swarm_display) + } else { + from_label + }; let delivery_mode = resolve_comm_delivery_mode(scope, delivery, wake); let notification_msg = format!("{} from {}: {}", scope_label, from_label, message); let _ = fanout_session_event( @@ -307,6 +399,10 @@ pub(super) async fn handle_comm_message( .clone() .unwrap_or_else(|| from_session.clone()); let reminder = match scope { + _ if cross_swarm_id.is_some() => Some(format!( + "You just received a cross-swarm direct message from {} in swarm '{}'. To reply, use swarm action=dm with to_swarm='{}' and to_session='{}'.", + sender_name, sender_swarm_display, sender_swarm_display, from_session + )), "dm" => Some(format!( "You just received a direct swarm message from {}. Review it and respond or act if useful.", sender_name @@ -397,11 +493,26 @@ pub(super) async fn handle_comm_message( friendly_name.clone(), Some(swarm_id.clone()), SwarmEventType::Notification { - notification_type: scope_value, + notification_type: scope_value.clone(), message: truncate_detail(&message, 220), }, ) .await; + if let Some(ref target_swarm) = cross_swarm_id { + record_swarm_event( + event_history, + event_counter, + swarm_event_tx, + from_session.clone(), + friendly_name.clone(), + Some(target_swarm.clone()), + SwarmEventType::Notification { + notification_type: "cross_swarm_dm".to_string(), + message: truncate_detail(&message, 220), + }, + ) + .await; + } let _ = client_event_tx.send(ServerEvent::Done { id }); crate::logging::event_info( diff --git a/crates/jcode-app-core/src/server/client_comm_swarms.rs b/crates/jcode-app-core/src/server/client_comm_swarms.rs new file mode 100644 index 00000000000..1c2608d7a90 --- /dev/null +++ b/crates/jcode-app-core/src/server/client_comm_swarms.rs @@ -0,0 +1,95 @@ +//! `comm_list_swarms` / `comm_set_swarm_label` request handlers. + +use super::{SwarmEvent, SwarmEventType, SwarmMember, record_swarm_event}; +use crate::protocol::ServerEvent; +use std::collections::{HashMap, HashSet}; +use std::sync::Arc; +use tokio::sync::{RwLock, broadcast, mpsc}; + +pub(super) async fn handle_comm_list_swarms( + id: u64, + req_session_id: String, + client_event_tx: &mpsc::UnboundedSender, + swarm_members: &Arc>>, + swarms_by_id: &Arc>>>, + swarm_coordinators: &Arc>>, +) { + let swarms = super::swarm_labels::list_swarms( + &req_session_id, + swarm_members, + swarms_by_id, + Some(swarm_coordinators), + ) + .await; + let _ = client_event_tx.send(ServerEvent::CommSwarms { id, swarms }); +} + +#[expect( + clippy::too_many_arguments, + reason = "label changes are recorded in swarm event history and answered with the directory" +)] +pub(super) async fn handle_comm_set_swarm_label( + id: u64, + req_session_id: String, + label: String, + client_event_tx: &mpsc::UnboundedSender, + swarm_members: &Arc>>, + swarms_by_id: &Arc>>>, + swarm_coordinators: &Arc>>, + event_history: &Arc>>, + event_counter: &Arc, + swarm_event_tx: &broadcast::Sender, +) { + let (swarm_id, friendly_name) = { + let members = swarm_members.read().await; + match members.get(&req_session_id) { + Some(member) => (member.swarm_id.clone(), member.friendly_name.clone()), + None => (None, None), + } + }; + let Some(swarm_id) = swarm_id else { + let _ = client_event_tx.send(ServerEvent::Error { + id, + message: "Not in a swarm, so there is no swarm to label.".to_string(), + retry_after_secs: None, + }); + return; + }; + let known: HashSet = swarms_by_id.read().await.keys().cloned().collect(); + match super::swarm_labels::set_swarm_label(&swarm_id, &label, &known) { + Ok(applied) => { + record_swarm_event( + event_history, + event_counter, + swarm_event_tx, + req_session_id.clone(), + friendly_name, + Some(swarm_id.clone()), + SwarmEventType::Notification { + notification_type: "swarm_label".to_string(), + message: match applied { + Some(label) => format!("Swarm labeled '{label}'"), + None => "Swarm label cleared".to_string(), + }, + }, + ) + .await; + handle_comm_list_swarms( + id, + req_session_id, + client_event_tx, + swarm_members, + swarms_by_id, + swarm_coordinators, + ) + .await; + } + Err(err) => { + let _ = client_event_tx.send(ServerEvent::Error { + id, + message: err.to_string(), + retry_after_secs: None, + }); + } + } +} diff --git a/crates/jcode-app-core/src/server/client_comm_tests.rs b/crates/jcode-app-core/src/server/client_comm_tests.rs index 78f524eb2d2..60f6d94388b 100644 --- a/crates/jcode-app-core/src/server/client_comm_tests.rs +++ b/crates/jcode-app-core/src/server/client_comm_tests.rs @@ -155,6 +155,7 @@ async fn comm_message_default_does_not_queue_soft_interrupt_for_connected_sessio None, None, None, + None, &client_event_tx, &sessions, &soft_interrupt_queues, @@ -317,6 +318,7 @@ async fn comm_message_with_wake_queues_soft_interrupt_for_busy_connected_session Some(CommDeliveryMode::Wake), None, None, + None, &client_event_tx, &sessions, &soft_interrupt_queues, @@ -560,6 +562,7 @@ async fn comm_message_accepts_friendly_name_dm_target() { Some(CommDeliveryMode::Notify), None, None, + None, &client_event_tx, &sessions, &soft_interrupt_queues, @@ -724,6 +727,7 @@ async fn comm_message_rejects_ambiguous_friendly_name_dm_target() { None, None, None, + None, &client_event_tx, &sessions, &soft_interrupt_queues, @@ -835,6 +839,7 @@ async fn comm_broadcast_reaches_only_senders_spawned_subtree() { None, None, None, + None, &client_event_tx, &sessions, &soft_interrupt_queues, @@ -876,6 +881,7 @@ async fn comm_broadcast_reaches_only_senders_spawned_subtree() { None, None, None, + None, &client_event_tx, &sessions, &soft_interrupt_queues, @@ -901,3 +907,149 @@ async fn comm_broadcast_reaches_only_senders_spawned_subtree() { Ok(ServerEvent::Notification { .. }) )); } + +/// Cross-swarm DMs: `to_swarm` resolves a label (or id) of another live +/// swarm. Without `to_session` the DM lands on that swarm's coordinator; with +/// it, on the named agent. Without `to_swarm`, DMs stay swarm-local. +#[tokio::test] +async fn comm_message_cross_swarm_dm_by_label() { + let _labels_guard = crate::server::swarm_labels::SWARM_LABELS_TEST_LOCK + .lock() + .unwrap_or_else(|p| p.into_inner()); + crate::server::swarm_labels::reset_swarm_labels_for_test(); + + fn member( + session_id: &str, + role: &str, + swarm_id: &str, + ) -> (SwarmMember, mpsc::UnboundedReceiver) { + let (event_tx, event_rx) = mpsc::unbounded_channel(); + ( + SwarmMember { + session_id: session_id.to_string(), + event_tx, + event_txs: HashMap::new(), + working_dir: None, + swarm_id: Some(swarm_id.to_string()), + swarm_enabled: true, + status: "ready".to_string(), + detail: None, + friendly_name: Some(session_id.to_string()), + report_back_to_session_id: None, + latest_completion_report: None, + role: role.to_string(), + joined_at: Instant::now(), + last_status_change: Instant::now(), + is_headless: true, + output_tail: None, + todo_progress: None, + todo_items: Vec::new(), + runtime: crate::protocol::SwarmMemberRuntime::default(), + task_label: None, + }, + event_rx, + ) + } + + let (alpha, _alpha_rx) = member("alpha", "coordinator", "swarm-a"); + let (beta_coord, mut beta_coord_rx) = member("beta-coord", "coordinator", "swarm-b"); + let (beta_worker, mut beta_worker_rx) = member("beta-worker", "agent", "swarm-b"); + let swarm_members = Arc::new(RwLock::new(HashMap::from([ + ("alpha".to_string(), alpha), + ("beta-coord".to_string(), beta_coord), + ("beta-worker".to_string(), beta_worker), + ]))); + let swarms_by_id = Arc::new(RwLock::new(HashMap::from([ + ("swarm-a".to_string(), HashSet::from(["alpha".to_string()])), + ( + "swarm-b".to_string(), + HashSet::from(["beta-coord".to_string(), "beta-worker".to_string()]), + ), + ]))); + let live: HashSet = ["swarm-a", "swarm-b"].iter().map(|s| s.to_string()).collect(); + crate::server::swarm_labels::set_swarm_label("swarm-b", "Backend", &live).unwrap(); + + let sessions = Arc::new(RwLock::new(HashMap::new())); + let soft_interrupt_queues: SessionInterruptQueues = Arc::new(RwLock::new(HashMap::new())); + let channel_subscriptions = Arc::new(RwLock::new(HashMap::new())); + let event_history: Arc>> = + Arc::new(RwLock::new(std::collections::VecDeque::new())); + let event_counter = Arc::new(AtomicU64::new(0)); + let (swarm_event_tx, _) = broadcast::channel(16); + let client_connections = Arc::new(RwLock::new(HashMap::new())); + let (client_event_tx, mut client_event_rx) = mpsc::unbounded_channel(); + + macro_rules! send { + ($id:expr, $to_session:expr, $to_swarm:expr) => { + handle_comm_message( + $id, + "alpha".to_string(), + "hello from alpha".to_string(), + $to_session, + None, + Some(CommDeliveryMode::Notify), + None, + None, + $to_swarm, + &client_event_tx, + &sessions, + &soft_interrupt_queues, + &swarm_members, + &swarms_by_id, + &channel_subscriptions, + &event_history, + &event_counter, + &swarm_event_tx, + &client_connections, + ) + .await + }; + } + + // Without to_swarm, a session in another swarm is unreachable. + send!(1, Some("beta-worker".to_string()), None); + assert!(matches!( + client_event_rx.recv().await, + Some(ServerEvent::Error { id: 1, .. }) + )); + assert!(beta_worker_rx.try_recv().is_err()); + + // Label only: delivered to the other swarm's coordinator. + send!(2, None, Some("backend".to_string())); + assert!(matches!( + client_event_rx.recv().await, + Some(ServerEvent::Done { id: 2 }) + )); + match beta_coord_rx.try_recv() { + Ok(ServerEvent::Notification { + message, + notification_type: NotificationType::Message { scope, .. }, + .. + }) => { + assert_eq!(scope.as_deref(), Some("dm")); + assert!(message.starts_with("Cross-swarm DM from alpha (swarm 'swarm-a')")); + } + other => panic!("expected cross-swarm DM, got {other:?}"), + } + assert!(beta_worker_rx.try_recv().is_err()); + + // Label + friendly name: delivered to that agent. + send!(3, Some("beta-worker".to_string()), Some("swarm-b".to_string())); + assert!(matches!( + client_event_rx.recv().await, + Some(ServerEvent::Done { id: 3 }) + )); + assert!(matches!( + beta_worker_rx.try_recv(), + Ok(ServerEvent::Notification { .. }) + )); + + // Unknown swarm label is rejected. + send!(4, None, Some("nope".to_string())); + assert!(matches!( + client_event_rx.recv().await, + Some(ServerEvent::Error { id: 4, .. }) + )); + + crate::server::swarm_labels::reset_swarm_labels_for_test(); +} diff --git a/crates/jcode-app-core/src/server/client_lifecycle.rs b/crates/jcode-app-core/src/server/client_lifecycle.rs index c1d56f85c00..914f536bbfc 100644 --- a/crates/jcode-app-core/src/server/client_lifecycle.rs +++ b/crates/jcode-app-core/src/server/client_lifecycle.rs @@ -10,6 +10,7 @@ use super::client_comm::{ handle_comm_read, handle_comm_share, handle_comm_subscribe_channel, handle_comm_unsubscribe_channel, }; +use super::client_comm_swarms::{handle_comm_list_swarms, handle_comm_set_swarm_label}; use super::client_disconnect_cleanup::{cleanup_client_connection, detach_client_attachment}; use super::client_lifecycle_logging::{ ServerRequestLifecycleFields, interrupt_request_log_fields, request_payload_summary, @@ -2430,6 +2431,7 @@ pub(super) async fn handle_client( delivery, wake, tldr, + to_swarm, } => { handle_comm_message( id, @@ -2440,6 +2442,7 @@ pub(super) async fn handle_client( delivery, wake, tldr, + to_swarm, &client_event_tx, &sessions, &soft_interrupt_queues, @@ -2471,6 +2474,41 @@ pub(super) async fn handle_client( .await; } + Request::CommListSwarms { + id, + session_id: req_session_id, + } => { + handle_comm_list_swarms( + id, + req_session_id, + &client_event_tx, + &swarm_members, + &swarms_by_id, + &swarm_coordinators, + ) + .await; + } + + Request::CommSetSwarmLabel { + id, + session_id: req_session_id, + label, + } => { + handle_comm_set_swarm_label( + id, + req_session_id, + label, + &client_event_tx, + &swarm_members, + &swarms_by_id, + &swarm_coordinators, + &event_history, + &event_counter, + &swarm_event_tx, + ) + .await; + } + Request::CommListChannels { id, session_id: req_session_id, diff --git a/crates/jcode-app-core/src/server/client_lifecycle_logging.rs b/crates/jcode-app-core/src/server/client_lifecycle_logging.rs index f1db9191579..563ad12954f 100644 --- a/crates/jcode-app-core/src/server/client_lifecycle_logging.rs +++ b/crates/jcode-app-core/src/server/client_lifecycle_logging.rs @@ -60,6 +60,7 @@ pub(super) fn request_type_is_read_only(kind: &str) -> bool { | "comm_read" | "comm_list" | "comm_list_channels" + | "comm_list_swarms" | "comm_channel_members" | "comm_summary" | "comm_status" diff --git a/crates/jcode-app-core/src/server/client_lightweight_control.rs b/crates/jcode-app-core/src/server/client_lightweight_control.rs index 9b325d32d7d..810f013523a 100644 --- a/crates/jcode-app-core/src/server/client_lightweight_control.rs +++ b/crates/jcode-app-core/src/server/client_lightweight_control.rs @@ -6,6 +6,7 @@ use super::client_comm::{ handle_comm_read, handle_comm_share, handle_comm_subscribe_channel, handle_comm_unsubscribe_channel, }; +use super::client_comm_swarms::{handle_comm_list_swarms, handle_comm_set_swarm_label}; use super::client_writer::write_direct_event; use super::comm_await::{CommAwaitMembersContext, handle_comm_await_members}; use super::comm_control::{ @@ -262,6 +263,7 @@ pub(super) async fn handle_lightweight_control_request( delivery, wake, tldr, + to_swarm, } => { handle_comm_message( id, @@ -272,6 +274,7 @@ pub(super) async fn handle_lightweight_control_request( delivery, wake, tldr, + to_swarm, &client_event_tx, sessions, soft_interrupt_queues, @@ -301,6 +304,39 @@ pub(super) async fn handle_lightweight_control_request( ) .await; } + Request::CommListSwarms { + id, + session_id: req_session_id, + } => { + handle_comm_list_swarms( + id, + req_session_id, + &client_event_tx, + swarm_members, + swarms_by_id, + swarm_coordinators, + ) + .await; + } + Request::CommSetSwarmLabel { + id, + session_id: req_session_id, + label, + } => { + handle_comm_set_swarm_label( + id, + req_session_id, + label, + &client_event_tx, + swarm_members, + swarms_by_id, + swarm_coordinators, + event_history, + event_counter, + swarm_event_tx, + ) + .await; + } Request::CommListChannels { id, session_id: req_session_id, diff --git a/crates/jcode-app-core/src/server/swarm_labels.rs b/crates/jcode-app-core/src/server/swarm_labels.rs new file mode 100644 index 00000000000..465d8cc6f61 --- /dev/null +++ b/crates/jcode-app-core/src/server/swarm_labels.rs @@ -0,0 +1,221 @@ +//! Human-readable swarm labels and cross-swarm target resolution. +//! +//! Swarm ids are opaque (`session:`, a git dir, or `JCODE_SWARM_ID`), so +//! agents in one swarm cannot reasonably address another swarm by id. Labels +//! give each swarm a short, unique, human-readable name ("frontend", +//! "release-train") that cross-swarm DMs and `list_swarms` use. +//! +//! Labels are keyed by swarm id and persisted in one small registry file under +//! the durable state dir so they survive server reloads and restarts. + +use super::SwarmMember; +use crate::protocol::SwarmInfo; +use crate::storage; +use std::collections::{HashMap, HashSet}; +use std::path::PathBuf; +use std::sync::{Arc, LazyLock, Mutex as StdMutex}; +use tokio::sync::RwLock; + +const LABELS_FILE: &str = "swarm-labels.json"; +pub(super) const MAX_SWARM_LABEL_CHARS: usize = 48; + +static LABELS: LazyLock>>> = + LazyLock::new(|| StdMutex::new(None)); + +#[cfg(not(test))] +fn labels_path() -> PathBuf { + storage::durable_state_dir().join(LABELS_FILE) +} + +#[cfg(test)] +fn labels_path() -> PathBuf { + std::env::temp_dir() + .join(format!("jcode-test-state-{}", std::process::id())) + .join(LABELS_FILE) +} + +fn with_labels(f: impl FnOnce(&mut HashMap) -> R) -> R { + let mut guard = LABELS.lock().unwrap_or_else(|p| p.into_inner()); + let labels = guard.get_or_insert_with(|| { + storage::read_json::>(&labels_path()).unwrap_or_default() + }); + f(labels) +} + +fn persist(labels: &HashMap) { + if let Err(err) = storage::write_json_fast(&labels_path(), labels) { + crate::logging::warn(&format!("Failed to persist swarm labels: {err}")); + } +} + +pub(super) fn swarm_label(swarm_id: &str) -> Option { + with_labels(|labels| labels.get(swarm_id).cloned()) +} + +/// Normalize a user-provided label: trimmed, internal whitespace collapsed. +pub(super) fn normalize_swarm_label(label: &str) -> String { + label.split_whitespace().collect::>().join(" ") +} + +/// Set or clear (empty label) the label of `swarm_id`. Labels are unique +/// case-insensitively across swarms, and must not collide with another +/// swarm's id so resolution stays unambiguous. +pub(super) fn set_swarm_label( + swarm_id: &str, + label: &str, + known_swarm_ids: &HashSet, +) -> anyhow::Result> { + let label = normalize_swarm_label(label); + if label.chars().count() > MAX_SWARM_LABEL_CHARS { + anyhow::bail!("Swarm label must be at most {MAX_SWARM_LABEL_CHARS} characters."); + } + with_labels(|labels| { + if label.is_empty() { + if labels.remove(swarm_id).is_some() { + persist(labels); + } + return Ok(None); + } + if known_swarm_ids + .iter() + .any(|other| other != swarm_id && other == &label) + { + anyhow::bail!("Swarm label '{label}' collides with another swarm's id."); + } + if let Some((other, _)) = labels + .iter() + .find(|(other, existing)| *other != swarm_id && existing.eq_ignore_ascii_case(&label)) + { + anyhow::bail!("Swarm label '{label}' is already used by swarm '{other}'."); + } + labels.insert(swarm_id.to_string(), label.clone()); + persist(labels); + Ok(Some(label)) + }) +} + +/// Resolve a cross-swarm target (label, case-insensitive, or exact swarm id) +/// to a live swarm id. +pub(super) fn resolve_swarm_target( + target: &str, + live_swarm_ids: &HashSet, +) -> anyhow::Result { + let target = target.trim(); + if target.is_empty() { + anyhow::bail!("'to_swarm' must not be blank."); + } + if live_swarm_ids.contains(target) { + return Ok(target.to_string()); + } + let normalized = normalize_swarm_label(target); + let by_label = with_labels(|labels| { + labels + .iter() + .find(|(_, label)| label.eq_ignore_ascii_case(&normalized)) + .map(|(swarm_id, _)| swarm_id.clone()) + }); + match by_label { + Some(swarm_id) if live_swarm_ids.contains(&swarm_id) => Ok(swarm_id), + Some(_) => anyhow::bail!("Swarm '{target}' has no live members right now."), + None => anyhow::bail!( + "Unknown swarm '{target}'. Use swarm action=list_swarms to see swarm labels and ids." + ), + } +} + +/// Build the swarm directory from live membership. +pub(super) async fn list_swarms( + requester_session: &str, + swarm_members: &Arc>>, + swarms_by_id: &Arc>>>, + swarm_coordinators: Option<&Arc>>>, +) -> Vec { + let swarms = swarms_by_id.read().await.clone(); + let coordinators = match swarm_coordinators { + Some(coordinators) => coordinators.read().await.clone(), + None => HashMap::new(), + }; + let members = swarm_members.read().await; + let own_swarm = members + .get(requester_session) + .and_then(|member| member.swarm_id.clone()); + let mut out: Vec = swarms + .into_iter() + .filter(|(_, sessions)| !sessions.is_empty()) + .map(|(swarm_id, sessions)| { + let coordinator_session_id = coordinators.get(&swarm_id).cloned().or_else(|| { + sessions + .iter() + .filter(|id| members.get(*id).is_some_and(|m| m.role == "coordinator")) + .min() + .cloned() + }); + let coordinator_name = coordinator_session_id + .as_ref() + .and_then(|id| members.get(id)) + .and_then(|member| member.friendly_name.clone()); + SwarmInfo { + label: swarm_label(&swarm_id), + is_own: own_swarm.as_deref() == Some(swarm_id.as_str()), + coordinator_session_id, + coordinator_name, + member_count: sessions.len(), + swarm_id, + } + }) + .collect(); + out.sort_by(|a, b| { + b.is_own + .cmp(&a.is_own) + .then_with(|| a.label.is_none().cmp(&b.label.is_none())) + .then_with(|| a.label.cmp(&b.label)) + .then_with(|| a.swarm_id.cmp(&b.swarm_id)) + }); + out +} + +/// Display name for a swarm: its label when set, else its id. +pub(super) fn swarm_display_name(swarm_id: &str) -> String { + swarm_label(swarm_id).unwrap_or_else(|| swarm_id.to_string()) +} + +#[cfg(test)] +pub(super) static SWARM_LABELS_TEST_LOCK: StdMutex<()> = StdMutex::new(()); + +#[cfg(test)] +pub(super) fn reset_swarm_labels_for_test() { + let mut guard = LABELS.lock().unwrap_or_else(|p| p.into_inner()); + *guard = Some(HashMap::new()); + let _ = std::fs::remove_file(labels_path()); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn labels_are_unique_and_resolve_case_insensitively() { + let _guard = SWARM_LABELS_TEST_LOCK + .lock() + .unwrap_or_else(|p| p.into_inner()); + reset_swarm_labels_for_test(); + let live: HashSet = ["swarm-a", "swarm-b"] + .iter() + .map(|s| s.to_string()) + .collect(); + + assert_eq!( + set_swarm_label("swarm-a", " Front End ", &live).unwrap(), + Some("Front End".to_string()) + ); + assert!(set_swarm_label("swarm-b", "front end", &live).is_err()); + assert!(set_swarm_label("swarm-b", "swarm-a", &live).is_err()); + assert_eq!(resolve_swarm_target("FRONT END", &live).unwrap(), "swarm-a"); + assert_eq!(resolve_swarm_target("swarm-b", &live).unwrap(), "swarm-b"); + assert!(resolve_swarm_target("nope", &live).is_err()); + + assert_eq!(set_swarm_label("swarm-a", "", &live).unwrap(), None); + assert!(resolve_swarm_target("front end", &live).is_err()); + reset_swarm_labels_for_test(); + } +} diff --git a/crates/jcode-app-core/src/tool/communicate.rs b/crates/jcode-app-core/src/tool/communicate.rs index 4c1484d970f..a247580c2ca 100644 --- a/crates/jcode-app-core/src/tool/communicate.rs +++ b/crates/jcode-app-core/src/tool/communicate.rs @@ -1211,6 +1211,7 @@ async fn broadcast_plan_alert(ctx: &ToolContext, message: &str) -> Result<()> { wake: None, delivery: None, tldr: Some("run_plan paused: credential failure wave; fix auth then retry".to_string()), + to_swarm: None, }; match send_request(request).await { Ok(response) => ensure_success(&response), @@ -1724,6 +1725,43 @@ fn default_await_target_statuses() -> Vec { default_comm_await_target_statuses() } +fn non_blank(value: Option) -> Option { + value.filter(|value| !value.trim().is_empty()) +} + +fn cross_swarm_confirmation(swarm: &str, to_session: Option<&str>, message: &str) -> String { + match to_session { + Some(target) => format!("Cross-swarm DM sent to {target} in swarm '{swarm}': {message}"), + None => format!("Cross-swarm DM sent to the coordinator of swarm '{swarm}': {message}"), + } +} + +/// Render the `list_swarms` directory: one line per live swarm with label, +/// id, coordinator, and member count. The caller's own swarm is marked. +pub(super) fn format_swarm_directory(swarms: &[crate::protocol::SwarmInfo]) -> String { + if swarms.is_empty() { + return "No live swarms.".to_string(); + } + let mut out = String::from("Live swarms:\n\n"); + for swarm in swarms { + let name = swarm.label.as_deref().unwrap_or("(unlabeled)"); + let own = if swarm.is_own { " [yours]" } else { "" }; + let coordinator = match (&swarm.coordinator_name, &swarm.coordinator_session_id) { + (Some(name), Some(id)) => format!("{name} [{id}]"), + (None, Some(id)) => id.clone(), + _ => "none".to_string(), + }; + out.push_str(&format!( + "- {name}{own}\n id: {}\n coordinator: {coordinator}\n members: {}\n", + swarm.swarm_id, swarm.member_count + )); + } + out.push_str( + "\nCross-swarm DM: action=dm with to_swarm=